Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 1c084510

Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt

  • ID 1c0845108811070224404f367b0e7bee1d5bbe9e
  • Vorgänger 548f4467
  • Nachfolger 4d20c181

Revision 2532 rückgängig gemacht (Befehl aus falschem Verzeichnis abgeschickt)

Unterschiede anzeigen:

SL/IS.pm
34 34

  
35 35
package IS;
36 36

  
37
use Data::Dumper;
37 38
use SL::AM;
38 39
use SL::Common;
39 40
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, $provided_dbh, $payments_only) = @_;
484
  my ($self, $myconfig, $form) = @_;
485 485

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

  
489
  my ($query, $sth, $null, $project_id, @values);
489
  my ($query, $sth, $null, $project_id, $deliverydate, @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 (!$payments_only) {
501
    if ($form->{id}) {
502
      &reverse_invoice($dbh, $form);
500
  if ($form->{id}) {
503 501

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

  
508
      $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
509
      do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
504
  } else {
505
    $query = qq|SELECT nextval('glid')|;
506
    ($form->{"id"}) = selectrow_query($form, $dbh, $query);
510 507

  
511
      if (!$form->{invnumber}) {
512
        $form->{invnumber} =
513
          $form->update_defaults($myconfig, $form->{type} eq "credit_note" ?
514
                                 "cnnumber" : "invnumber", $dbh);
515
      }
508
    $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|;
509
    do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"});
510

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

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

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

  
645
      next if $payments_only;
646

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

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

  
775 769
  # update exchangerate
776
  if (($form->{currency} ne $defaultcurrency) && !$exchangerate) {
770
  if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
777 771
    $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate},
778 772
                               $form->{exchangerate}, 0);
779 773
  }
......
783 777
  foreach my $trans_id (keys %{ $form->{amount} }) {
784 778
    foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
785 779
      next unless ($form->{expense_inventory} =~ /$accno/);
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)) {
780
      if (
781
          ($form->{amount}{$trans_id}{$accno} =
782
           $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
783
          ) != 0
784
        ) {
790 785
        $query =
791 786
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
792 787
             VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
......
798 793
    }
799 794

  
800 795
    foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
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)) {
796
      if (
797
          ($form->{amount}{$trans_id}{$accno} =
798
           $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
799
          ) != 0
800
        ) {
804 801
        $query =
805 802
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
806 803
             VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
......
833 830

  
834 831
      $exchangerate = 0;
835 832

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

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

  
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

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

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

  
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

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

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

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

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

  
1045 1031
  return $rc;
1046 1032
}
1047 1033

  
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

  
1090 1034
sub post_payment {
1091
  $main::lxdebug->enter_sub();
1092

  
1093
  my ($self, $myconfig, $form, $locale) = @_;
1035
  $main::lxdebug->enter_sub() and my ($self, $myconfig, $form, $locale) = @_;
1094 1036

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

  
1098
  my (%payments, $old_form, $row, $item, $query, %keep_vars);
1099

  
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});
1103

  
1104
  $old_form = save_form();
1105

  
1106
  # Delete all entries in acc_trans from prior payments.
1107
  $self->_delete_payments($form, $dbh);
1040
  $form->{datepaid} = $form->{invdate};
1108 1041

  
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});
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
  }
1111 1049

  
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 };
1050
  $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy");
1114 1051

  
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 };
1052
  # record payments and offsetting AR
1053
  for my $i (1 .. $form->{paidaccounts}) {
1054
    if ($form->{"paid_$i"}) {
1118 1055

  
1119
  # Retrieve the invoice from the database.
1120
  $self->retrieve_invoice($myconfig, $form);
1056
      my ($accno) = split /--/, $form->{"AR_paid_$i"};
1057
      $form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"});
1058
      $form->{datepaid} = $form->{"datepaid_$i"};
1121 1059

  
1122
  # Set up the content of $form in the way that IR::post_invoice() expects.
1123
  $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
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
      }
1124 1067

  
1125
  for $row (1 .. scalar @{ $form->{invoice_details} }) {
1126
    $item = $form->{invoice_details}->[$row - 1];
1068
      # record AR
1069
      $amount = $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"}, 2);
1127 1070

  
1128
    map { $item->{$_} = $form->format_amount($myconfig, $item->{$_}) } qw(qty sellprice discount);
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);
1129 1082

  
1130
    map { $form->{"${_}_${row}"} = $item->{$_} } keys %{ $item };
1131
  }
1083
      # record payment
1084
      $form->{"paid_$i"} *= -1;
1132 1085

  
1133
  $form->{rowcount} = scalar @{ $form->{invoice_details} };
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"});
1134 1092

  
1135
  delete @{$form}{qw(invoice_details paidaccounts storno paid)};
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);
1136 1099

  
1137
  # Restore the payment options from the user input.
1138
  map { $form->{$_} = $payments{$_} } keys %payments;
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;
1139 1103

  
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|;
1104
      $diff = 0;
1149 1105

  
1150
  ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
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
      }
1151 1110

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

  
1155
  restore_form($old_form);
1114
  # record exchange rate differences and gains/losses
1115
  foreach my $accno (keys %{ $form->{fx} }) {
1116
    foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
1156 1117

  
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});
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
      }
1160 1131

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

  
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;
1135
  # save AR record
1136
  delete $form->{datepaid} unless $form->{paid};
1168 1137

  
1169
  my $rc = 1;
1170
#   my $rc = $dbh->commit();
1171
  $dbh->disconnect();
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"}));
1172 1140

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

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

  
1178 1147
sub process_assembly {

Auch abrufbar als: Unified diff