Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[kivitendo-erp.git] / SL / AP.pm
index d4fe484..e4ff230 100644 (file)
--- a/SL/AP.pm
+++ b/SL/AP.pm
 package AP;
 
 use SL::DBUtils;
+use SL::IO;
 use SL::MoreCommon;
 
+use Data::Dumper;
+
 use strict;
 
 sub post_transaction {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
-
+  my $rc = 0; # return code auf false setzen
   # connect to database
   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
 
@@ -51,6 +54,7 @@ sub post_transaction {
   my $exchangerate = 0;
 
   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
+  delete $form->{currency} unless $form->{defaultcurrency};
 
   ($null, $form->{department_id}) = split(/--/, $form->{department});
   $form->{department_id} *= 1;
@@ -67,7 +71,7 @@ sub post_transaction {
       (split(/--/, $form->{"AP_amount_$i"}))[0];
   }
   ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
-  ($form->{AP}{payables})         = split(/--/, $form->{APselected});
+  ($form->{AP_payables})          = split(/--/, $form->{APselected});
 
   # reverse and parse amounts
   for my $i (1 .. $form->{rowcount}) {
@@ -104,26 +108,14 @@ sub post_transaction {
 
     my ($tax, $diff);
     if ($form->{taxincluded} *= 1) {
-      if (!$form->{"korrektur_$i"}) {
-        $tax =
-          $form->{"amount_$i"} -
-          ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
-      } else {
-        $tax = $form->{"tax_$i"};
-      }
+      $tax = $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
       $amount = $form->{"amount_$i"} - $tax;
       $form->{"amount_$i"} = $form->round_amount($amount, 2);
       $diff += $amount - $form->{"amount_$i"};
       $form->{"tax_$i"} = $form->round_amount($tax, 2);
       $form->{netamount} += $form->{"amount_$i"};
     } else {
-      if (!$form->{"korrektur_$i"}) {
-        $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
-      } else {
-        $tax = $form->{"tax_$i"};
-      }
-      $form->{"tax_$i"} =
-        $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
+      $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
       $form->{netamount} += $form->{"amount_$i"};
     }
     $form->{total_tax} += $form->{"tax_$i"} * -1;
@@ -155,9 +147,6 @@ sub post_transaction {
   # amount for total AP
   $form->{payables} = $form->{invtotal};
 
-  $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
-  my $datepaid = ($form->{invpaid} != 0) ? $form->{datepaid} : undef;
-
   # update exchangerate
   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
     $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
@@ -195,14 +184,14 @@ sub post_transaction {
 
     $query = qq|UPDATE ap SET
                 invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
-                amount = ?, duedate = ?, paid = ?, datepaid = ?, netamount = ?,
+                amount = ?, duedate = ?, paid = ?, netamount = ?,
                 curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?
                WHERE id = ?|;
     @values = ($form->{invnumber}, conv_date($form->{transdate}),
                   $form->{ordnumber}, conv_i($form->{vendor_id}),
                   $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
                   conv_date($form->{duedate}), $form->{invpaid},
-                  conv_date($datepaid), $form->{netamount},
+                  $form->{netamount},
                   $form->{currency}, $form->{notes},
                   conv_i($form->{department_id}), $form->{storno},
                   $form->{storno_id}, $form->{id});
@@ -272,7 +261,7 @@ sub post_transaction {
 
       # get paid account
 
-      ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
+      ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
       $form->{"datepaid_$i"} = $form->{transdate}
         unless ($form->{"datepaid_$i"});
 
@@ -289,9 +278,9 @@ sub post_transaction {
           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
-        @values = ($form->{id}, $form->{AP}{payables}, $amount,
+        @values = ($form->{id}, $form->{AP_payables}, $amount,
                    conv_date($form->{"datepaid_$i"}), $project_id,
-                   $form->{AP}{payables});
+                   $form->{AP_payables});
         do_query($form, $dbh, $query, @values);
       }
       $form->{payables} = $amount;
@@ -301,9 +290,9 @@ sub post_transaction {
         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
         qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
         qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
-      @values = ($form->{id}, $form->{AP}{"paid_$i"}, $form->{"paid_$i"},
+      @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
                  conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
-                 $form->{"memo_$i"}, $project_id, $form->{AP}{"paid_$i"});
+                 $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"});
       do_query($form, $dbh, $query, @values);
 
       # add exchange rate difference
@@ -315,9 +304,9 @@ sub post_transaction {
           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
-        @values = ($form->{id}, $form->{AP}{"paid_$i"}, $amount,
+        @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
                    conv_date($form->{"datepaid_$i"}), $project_id,
-                   $form->{AP}{"paid_$i"});
+                   $form->{"AP_paid_account_$i"});
         do_query($form, $dbh, $query, @values);
       }
 
@@ -354,12 +343,15 @@ sub post_transaction {
     do_query($form, $dbh, $query,  $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
   }
 
-  my $rc = 1;
+  IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
+
   if (!$provided_dbh) {
     $dbh->commit();
     $dbh->disconnect();
   }
 
+  $rc = 1; #  Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
+
   $main::lxdebug->leave_sub();
 
   return $rc;
@@ -368,7 +360,7 @@ sub post_transaction {
 sub delete_transaction {
   $main::lxdebug->enter_sub();
 
-  my ($self, $myconfig, $form, $spool) = @_;
+  my ($self, $myconfig, $form) = @_;
 
   # connect to database
   my $dbh = $form->dbconnect_noauto($myconfig);
@@ -475,7 +467,7 @@ sub ap_transactions {
   }
 
   my @a = qw(transdate invnumber name);
-  push @a, "employee" if $self->{l_employee};
+  push @a, "employee" if $form->{l_employee};
   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
   my $sortorder = join(', ', map { "$_ $sortdir" } @a);
 
@@ -584,11 +576,9 @@ sub post_payment {
 
   $self->setup_form($form);
 
-  ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|);
-  $form->{defaultcurrency}   = (split m/:/, $form->{defaultcurrency})[0];
-  $form->{currency}          = $form->{defaultcurrency} if ($form->{defaultcurrency} && ($form->{currency} =~ m/^\s*$/));
-
-  $form->{exchangerate}      = $form->format_amount($myconfig, $form->{exchangerate});
+  $form->{exchangerate}    = $form->format_amount($myconfig, $form->{exchangerate});
+  $form->{defaultcurrency} = $form->get_default_currency($myconfig);
+  delete $form->{currency} unless $form->{defaultcurrency};
 
   # Get the AP accno.
   $query =
@@ -642,6 +632,7 @@ sub setup_form {
     $j = 0;
     $k = 0;
 
+    next unless $form->{acc_trans}{$key};
     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
       if ($key eq "AP_paid") {
@@ -778,13 +769,15 @@ sub storno {
   }
 
   for my $row (@$rowref) {
-    delete @$row{qw(itime mtime link)};
+    delete @$row{qw(itime mtime link acc_trans_id)};
     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
     $row->{trans_id}   = $new_id;
     $row->{amount}    *= -1;
     do_query($form, $dbh, $query, (values %$row));
   }
 
+  map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
+
   $dbh->commit;
 
   $main::lxdebug->leave_sub();