A5 sollte als Papiergröße ebenfalls funktionieren.
[kivitendo-erp.git] / SL / AR.pm
index 410ac99..5fc6032 100644 (file)
--- a/SL/AR.pm
+++ b/SL/AR.pm
@@ -38,6 +38,8 @@ use Data::Dumper;
 use SL::DBUtils;
 use SL::MoreCommon;
 
+our (%myconfig, $form);
+
 sub post_transaction {
   $main::lxdebug->enter_sub();
 
@@ -50,6 +52,7 @@ sub post_transaction {
   my @values;
 
   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+  $form->{defaultcurrency} = $form->get_default_currency($myconfig);
 
   # set exchangerate
   $form->{exchangerate} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 :
@@ -101,18 +104,21 @@ sub post_transaction {
   }
 
   # adjust paidaccounts if there is no date in the last row
-  $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
-  $form->{paid} = 0;
-
-  # add payments
-  for $i (1 .. $form->{paidaccounts}) {
-    $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
-    $form->{paid}     += $form->{"paid_$i"};
-    $form->{datepaid}  = $form->{"datepaid_$i"};
-  }
+  # this does not apply to stornos, where the paid field is set manually
+  unless ($form->{storno}) {
+    $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
+    $form->{paid} = 0;
+
+    # add payments
+    for $i (1 .. $form->{paidaccounts}) {
+      $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
+      $form->{paid}     += $form->{"paid_$i"};
+      $form->{datepaid}  = $form->{"datepaid_$i"};
+    }
 
-  $form->{amount} = $form->{netamount} + $form->{total_tax};
-  $form->{paid}   = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
+    $form->{amount} = $form->{netamount} + $form->{total_tax};
+  }
+  $form->{paid}   = $form->round_amount($form->{paid} * ($form->{exchangerate} || 1), 2);
 
   ($null, $form->{employee_id}) = split /--/, $form->{employee};
 
@@ -147,7 +153,7 @@ sub post_transaction {
 
   # update exchangerate
   $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0)
-    if ($form->{currency} ne $form->{defaultcurrency}) && $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
+    if ($form->{currency} ne $form->{defaultcurrency}) && !$form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
 
   if (!$payments_only) {
     $query =
@@ -405,7 +411,8 @@ sub ar_transactions {
     qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate, | .
     qq|  a.duedate, a.netamount, a.amount, a.paid, | .
     qq|  a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | .
-    qq|  a.shippingpoint, a.storno, a.globalproject_id, | .
+    qq|  a.shippingpoint, a.storno, a.storno_id, a.globalproject_id, | .
+    qq|  a.marge_total, a.marge_percent, | .
     qq|  a.transaction_description, | .
     qq|  pr.projectnumber AS globalprojectnumber, | .
     qq|  c.name, | .
@@ -604,7 +611,62 @@ sub setup_form {
   $form->{tax} = $taxamount;
 
   $form->{invtotal} = $totalamount + $totaltax;
+
+  $main::lxdebug->leave_sub();
+}
+
+sub storno {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $form, $myconfig, $id) = @_;
+
+  my ($query, $new_id, $storno_row, $acc_trans_rows);
+  my $dbh = $form->get_standard_dbh($myconfig);
+
+  $query = qq|SELECT nextval('glid')|;
+  ($new_id) = selectrow_query($form, $dbh, $query);
+
+  $query = qq|SELECT * FROM ar WHERE id = ?|;
+  $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
+
+  $storno_row->{id}         = $new_id;
+  $storno_row->{storno_id}  = $id;
+  $storno_row->{storno}     = 't';
+  $storno_row->{invnumber}  = 'Storno-' . $storno_row->{invnumber};
+  $storno_row->{amount}    *= -1;
+  $storno_row->{netamount} *= -1;
+  $storno_row->{paid}       = $storno_row->{amount};
+
+  delete @$storno_row{qw(itime mtime)};
+
+  $query = sprintf 'INSERT INTO ar (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
+  do_query($form, $dbh, $query, (values %$storno_row));
+
+  $query = qq|UPDATE ar SET paid = amount + paid, storno = 't' WHERE id = ?|;
+  do_query($form, $dbh, $query, $id);
+
+  # now copy acc_trans entries
+  $query = qq|SELECT a.*, c.link FROM acc_trans a LEFT JOIN chart c ON a.chart_id = c.id WHERE a.trans_id = ? ORDER BY a.oid|;
+  my $rowref = selectall_hashref_query($form, $dbh, $query, $id); 
+
+  # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
+  while ($rowref->[-1]{link} =~ /paid/) {
+    splice(@$rowref, -2);
+  }
+
+  for my $row (@$rowref) {
+    delete @$row{qw(itime mtime link)};
+    $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));
+  }
+
+  $dbh->commit;
+
+  $main::lxdebug->leave_sub();
 }
 
+
 1;