Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 548f4467

Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt

  • ID 548f4467739c9fea1addee0e0717a89e7be75429
  • Vorgänger c90ba6c8
  • Nachfolger 1c084510

Unterschiede anzeigen:

SL/IS.pm
34 34

  
35 35
package IS;
36 36

  
37
use Data::Dumper;
38 37
use SL::AM;
39 38
use SL::Common;
40 39
use SL::DBUtils;
40
use SL::MoreCommon;
41 41

  
42 42
sub invoice_details {
43 43
  $main::lxdebug->enter_sub();
......
481 481
sub post_invoice {
482 482
  $main::lxdebug->enter_sub();
483 483

  
484
  my ($self, $myconfig, $form) = @_;
484
  my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
485 485

  
486 486
  # connect to database, turn off autocommit
487
  my $dbh = $form->dbconnect_noauto($myconfig);
487
  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
488 488

  
489
  my ($query, $sth, $null, $project_id, $deliverydate, @values);
489
  my ($query, $sth, $null, $project_id, @values);
490 490
  my $exchangerate = 0;
491 491

  
492 492
  if (!$form->{employee_id}) {
......
497 497

  
498 498
  my $all_units = AM->retrieve_units($myconfig, $form);
499 499

  
500
  if ($form->{id}) {
501

  
502
    &reverse_invoice($dbh, $form);
500
  if (!$payments_only) {
501
    if ($form->{id}) {
502
      &reverse_invoice($dbh, $form);
503 503

  
504
  } else {
505
    $query = qq|SELECT nextval('glid')|;
506
    ($form->{"id"}) = selectrow_query($form, $dbh, $query);
504
    } else {
505
      $query = qq|SELECT nextval('glid')|;
506
      ($form->{"id"}) = selectrow_query($form, $dbh, $query);
507 507

  
508
    $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
509
    do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
508
      $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
509
      do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
510 510

  
511
    if (!$form->{invnumber}) {
512
      $form->{invnumber} =
513
        $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
514
                               "cnnumber" : "invnumber", $dbh);
511
      if (!$form->{invnumber}) {
512
        $form->{invnumber} =
513
          $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
514
                                 "cnnumber" : "invnumber", $dbh);
515
      }
515 516
    }
516 517
  }
517 518

  
518 519
  my ($netamount, $invoicediff) = (0, 0);
519 520
  my ($amount, $linetotal, $lastincomeaccno);
520 521

  
521
  if ($form->{currency} eq $form->{defaultcurrency}) {
522
  my ($currencies)    = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|);
523
  my $defaultcurrency = (split m/:/, $currencies)[0];
524

  
525
  if ($form->{currency} eq $defaultcurrency) {
522 526
    $form->{exchangerate} = 1;
523 527
  } else {
524 528
    $exchangerate =
......
638 642
        $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
639 643
                            $decimalplaces);
640 644

  
645
      next if $payments_only;
646

  
641 647
      if ($form->{"inventory_accno_$i"} || $form->{"assembly_$i"}) {
642 648

  
643 649
        # adjust parts onhand quantity
......
767 773
  $form->{amount}{ $form->{id} }{ $form->{AR} } *= -1;
768 774

  
769 775
  # update exchangerate
770
  if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
776
  if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
771 777
    $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
772 778
                               $form->{exchangerate}, 0);
773 779
  }
......
777 783
  foreach my $trans_id (keys %{ $form->{amount} }) {
778 784
    foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
779 785
      next unless ($form->{expense_inventory} =~ /$accno/);
780
      if (
781
          ($form->{amount}{$trans_id}{$accno} =
782
           $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
783
          ) != 0
784
        ) {
786

  
787
      $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
788

  
789
      if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
785 790
        $query =
786 791
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
787 792
             VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
......
793 798
    }
794 799

  
795 800
    foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
796
      if (
797
          ($form->{amount}{$trans_id}{$accno} =
798
           $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
799
          ) != 0
800
        ) {
801
      $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
802

  
803
      if (!$payments_only && ($form->{amount}{$trans_id}{$accno} != 0)) {
801 804
        $query =
802 805
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
803 806
             VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
......
830 833

  
831 834
      $exchangerate = 0;
832 835

  
833
      if ($form->{currency} eq $form->{defaultcurrency}) {
836
      if ($form->{currency} eq $defaultcurrency) {
834 837
        $form->{"exchangerate_$i"} = 1;
835 838
      } else {
836 839
        $exchangerate =
......
884 887
      $diff = 0;
885 888

  
886 889
      # update exchange rate
887
      if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
890
      if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
888 891
        $form->update_exchangerate($dbh, $form->{currency},
889 892
                                   $form->{"datepaid_$i"},
890 893
                                   $form->{"exchangerate_$i"}, 0);
......
892 895
    }
893 896
  }
894 897

  
898
  if ($payments_only) {
899
    $query = qq|UPDATE ar SET paid = ?, datepaid = ? WHERE id = ?|;
900
    do_query($form, $dbh, $query,  $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
901

  
902
    if (!$provided_dbh) {
903
      $dbh->commit();
904
      $dbh->disconnect();
905
    }
906

  
907
    $main::lxdebug->leave_sub();
908
    return;
909
  }
910

  
895 911
  # record exchange rate differences and gains/losses
896 912
  foreach my $accno (keys %{ $form->{fx} }) {
897 913
    foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
......
913 929

  
914 930
  $amount = $netamount + $tax;
915 931

  
916
  # set values which could be empty to 0
917
  my $datepaid = conv_date($form->{paid});
918
  my $duedate  = conv_date($form->{duedate});
919
  $deliverydate = conv_date($form->{deliverydate});
920

  
921 932
  # fill in subject if there is none
922 933
  $form->{subject} = qq|$form->{label} $form->{invnumber}|
923 934
    unless $form->{subject};
......
1023 1034

  
1024 1035
  Common::webdav_folder($form) if ($main::webdav);
1025 1036

  
1026
  my $rc = $dbh->commit;
1027
  $dbh->disconnect;
1037
  my $rc = 1;
1038
  if (!$provided_dbh) {
1039
    $dbh->commit();
1040
    $dbh->disconnect();
1041
  }
1028 1042

  
1029 1043
  $main::lxdebug->leave_sub();
1030 1044

  
1031 1045
  return $rc;
1032 1046
}
1033 1047

  
1048
sub _delete_payments {
1049
  $main::lxdebug->enter_sub();
1050

  
1051
  my ($self, $form, $dbh) = @_;
1052

  
1053
  my (@delete_oids, $delete_next, $sth, $ref);
1054

  
1055
  # Delete old payment entries from acc_trans.
1056
  my $query =
1057
    qq|SELECT at.oid, at.*, c.link
1058
       FROM acc_trans at
1059
       LEFT JOIN chart c ON (at.chart_id = c.id)
1060
       WHERE (trans_id = ?)
1061
       ORDER BY at.oid|;
1062

  
1063
  $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
1064

  
1065
  while ($ref = $sth->fetchrow_hashref()) {
1066
    if ($delete_next) {
1067
      push @delete_oids, $ref->{oid};
1068
      undef $delete_next;
1069
      next;
1070
    }
1071

  
1072
    $ref->{LINKS} = { map { $_, 1 } split m/:/, $ref->{link} };
1073

  
1074
    if ($ref->{fx_transaction} || ($ref->{LINKS}->{AR} && ($ref->{amount} >= 0))) {
1075
      push @delete_oids, $ref->{oid};
1076
      $delete_next = $ref->{LINKS}->{AR};
1077
    }
1078
  }
1079

  
1080
  $sth->finish();
1081

  
1082
  if (@delete_oids) {
1083
    $query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
1084
    do_query($form, $dbh, $query);
1085
  }
1086

  
1087
  $main::lxdebug->leave_sub();
1088
}
1089

  
1034 1090
sub post_payment {
1035
  $main::lxdebug->enter_sub() and my ($self, $myconfig, $form, $locale) = @_;
1091
  $main::lxdebug->enter_sub();
1092

  
1093
  my ($self, $myconfig, $form, $locale) = @_;
1036 1094

  
1037 1095
  # connect to database, turn off autocommit
1038 1096
  my $dbh = $form->dbconnect_noauto($myconfig);
1039 1097

  
1040
  $form->{datepaid} = $form->{invdate};
1098
  my (%payments, $old_form, $row, $item, $query, %keep_vars);
1041 1099

  
1042
  # total payments, don't move we need it here
1043
  for my $i ( 1 .. $form->{paidaccounts} ) {
1044
    $form->{"paid_$i"}  = $form->parse_amount($myconfig, $form->{"paid_$i"});
1045
    $form->{"paid_$i"} *= -1                     if $form->{type} eq "credit_note";
1046
    $form->{"paid"}    += $form->{"paid_$i"};
1047
    $form->{"datepaid"} = $form->{"datepaid_$i"} if $form->{"datepaid_$i"};
1048
  }
1100
  my @prior;
1101
  push @prior, selectall_hashref_query($form, $dbh, qq|SELECT id, paid, datepaid FROM ar WHERE id = ?|, $form->{id});
1102
  push @prior, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
1049 1103

  
1050
  $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
1104
  $old_form = save_form();
1051 1105

  
1052
  # record payments and offsetting AR
1053
  for my $i (1 .. $form->{paidaccounts}) {
1054
    if ($form->{"paid_$i"}) {
1106
  # Delete all entries in acc_trans from prior payments.
1107
  $self->_delete_payments($form, $dbh);
1055 1108

  
1056
      my ($accno) = split /--/, $form->{"AR_paid_$i"};
1057
      $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
1058
      $form->{datepaid} = $form->{"datepaid_$i"};
1109
  my @after_dp;
1110
  push @after_dp, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
1059 1111

  
1060
      $exchangerate = 0;
1061
      if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
1062
        $form->{"exchangerate_$i"} = 1;
1063
      } else {
1064
        $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
1065
        $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
1066
      }
1112
  # Save the new payments the user made before cleaning up $form.
1113
  map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AR_paid_\d+$|^paidaccounts$/, keys %{ $form };
1067 1114

  
1068
      # record AR
1069
      $amount = $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"}, 2);
1115
  # Clean up $form so that old content won't tamper the results.
1116
  %keep_vars = map { $_, 1 } qw(login password id);
1117
  map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
1070 1118

  
1071
      $query =
1072
        qq|DELETE FROM acc_trans
1073
           WHERE (trans_id = ?)
1074
             AND (chart_id = (SELECT id FROM chart WHERE accno = ?))
1075
             AND (amount = ?) AND (transdate = ?)|;
1076
      do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, conv_date($form->{"datepaid_$i"}));
1077
      $query =
1078
        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
1079
           VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?,
1080
                   (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1081
      do_query($form, $dbh, $query, $form->{id}, $form->{AR}, $amount, $form->{"datepaid_$i"}, conv_i($form->{"globalproject_id"}), $accno);
1119
  # Retrieve the invoice from the database.
1120
  $self->retrieve_invoice($myconfig, $form);
1082 1121

  
1083
      # record payment
1084
      $form->{"paid_$i"} *= -1;
1122
  # Set up the content of $form in the way that IR::post_invoice() expects.
1123
  $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
1085 1124

  
1086
      $query =
1087
        qq|DELETE FROM acc_trans
1088
           WHERE (trans_id = ?)
1089
             AND (chart_id = (SELECT id FROM chart WHERE accno = ?))
1090
             AND (amount = ?) AND (transdate = ?) AND (source = ?) AND (memo = ?)|;
1091
      do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"});
1125
  for $row (1 .. scalar @{ $form->{invoice_details} }) {
1126
    $item = $form->{invoice_details}->[$row - 1];
1092 1127

  
1093
      $query =
1094
        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey)
1095
           VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?,
1096
                   (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1097
      do_query($form, $dbh, $query, $form->{id}, $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, $form->{"source_$i"}, $form->{"memo_$i"},
1098
               conv_i($form->{"globalproject_id"}), $accno);
1128
    map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice discount);
1099 1129

  
1100
      # gain/loss
1101
      $amount = $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} * $form->{"exchangerate_$i"};
1102
      $form->{fx}{ $form->{($amount > 0 ? 'fxgain_accno' : 'fxloss_accno')} }{ $form->{"datepaid_$i"} } += $amount;
1130
    map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
1131
  }
1103 1132

  
1104
      $diff = 0;
1133
  $form->{rowcount} = scalar @{ $form->{invoice_details} };
1105 1134

  
1106
      # update exchange rate
1107
      if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
1108
        $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
1109
      }
1135
  delete @{$form}{qw(invoice_details paidaccounts storno paid)};
1110 1136

  
1111
    }
1112
  }
1137
  # Restore the payment options from the user input.
1138
  map { $form->{$_} = $payments{$_} } keys %payments;
1113 1139

  
1114
  # record exchange rate differences and gains/losses
1115
  foreach my $accno (keys %{ $form->{fx} }) {
1116
    foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1140
  # Get the AP accno (which is normally done by Form::create_links()).
1141
  $query =
1142
    qq|SELECT c.accno
1143
       FROM acc_trans at
1144
       LEFT JOIN chart c ON (at.chart_id = c.id)
1145
       WHERE (trans_id = ?)
1146
         AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
1147
       ORDER BY at.oid
1148
       LIMIT 1|;
1117 1149

  
1118
      if ($form->{fx}{$accno}{$transdate} = $form->round_amount($form->{fx}{$accno}{$transdate}, 2)) { # '=' is no typo, it's an assignment
1119
        $query =
1120
          qq|DELETE FROM acc_trans
1121
             WHERE (trans_id = ?)
1122
               AND (chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?))
1123
               AND (amount = ?) AND (transdate = ?) AND (cleared = ?) AND (fx_transaction = ?)|;
1124
        do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1);
1125
        $query =
1126
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey)
1127
             VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?,
1128
                     (SELECT taxkey_id FROM chart WHERE accno = ?))|;
1129
        do_query($form, $dbh, $query, $form->{id}, $accno, $form->{fx}{$accno}{$transdate}, $transdate, 0, 1, conv_i($form->{"globalproject_id"}), $accno);
1130
      }
1150
  ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
1131 1151

  
1132
    }
1152
  # Post the new payments.
1153
  $self->post_invoice($myconfig, $form, $dbh, 1);
1154

  
1155
  restore_form($old_form);
1156

  
1157
  my @after;
1158
  push @after, selectall_hashref_query($form, $dbh, qq|SELECT id, paid, datepaid FROM ar WHERE id = ?|, $form->{id});
1159
  push @after, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
1160

  
1161
  foreach my $rows (@prior, @after_dp, @after) {
1162
    map { delete @{$_}{qw(itime mtime)} } @{ $rows };
1133 1163
  }
1134 1164

  
1135
  # save AR record
1136
  delete $form->{datepaid} unless $form->{paid};
1165
  map { $main::lxdebug->dump_sql_result(0, 'davor ', $_) } @prior;
1166
  map { $main::lxdebug->dump_sql_result(0, 'nachDP', $_) } @after_dp;
1167
  map { $main::lxdebug->dump_sql_result(0, 'danach', $_) } @after;
1137 1168

  
1138
  my $query = qq|UPDATE ar SET paid = ?, datepaid = ? WHERE id = ?|;
1139
  do_query($form, $dbh, $query, $form->{"paid"}, conv_date($form->{"datepaid"}), conv_i($form->{"id"}));
1169
  my $rc = 1;
1170
#   my $rc = $dbh->commit();
1171
  $dbh->disconnect();
1140 1172

  
1141
  my $rc = $dbh->commit;
1142
  $dbh->disconnect;
1173
  $main::lxdebug->leave_sub();
1143 1174

  
1144
  $main::lxdebug->leave_sub() and return $rc;
1175
  return $rc;
1145 1176
}
1146 1177

  
1147 1178
sub process_assembly {

Auch abrufbar als: Unified diff