Kunden- und Liferantenstammdatenliste auf die Verwendung von ReportGenerator umgestellt.
[kivitendo-erp.git] / SL / IS.pm
index 86f4d97..261c7c1 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -492,6 +492,8 @@ sub post_invoice {
   if (!$form->{employee_id}) {
     $form->get_employee($dbh);
   }
+  
+  $form->{defaultcurrency} = $form->get_default_currency($myconfig);
 
   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
@@ -549,6 +551,10 @@ sub post_invoice {
     my $basefactor;
     my $basqty;
 
+    $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
+    $form->{"marge_absolut_$i"} = $form->parse_amount($myconfig, $form->{"marge_absolut_$i"}) * 1;
+    $form->{"lastcost_$i"} = $form->{"lastcost_$i"} * 1;
+
     if ($form->{storno}) {
       $form->{"qty_$i"} *= -1;
     }
@@ -686,8 +692,9 @@ sub post_invoice {
         qq|INSERT INTO invoice (trans_id, parts_id, description, longdescription, qty,
                                 sellprice, fxsellprice, discount, allocated, assemblyitem,
                                 unit, deliverydate, project_id, serialnumber, pricegroup_id,
-                                ordnumber, transdate, cusordnumber, base_qty, subtotal)
-           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
+                                ordnumber, transdate, cusordnumber, base_qty, subtotal,
+                                marge_percent, marge_total, lastcost)
+           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
       @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
                  $form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"},
@@ -696,7 +703,9 @@ sub post_invoice {
                  $form->{"unit_$i"}, conv_date($form->{"deliverydate_$i"}), conv_i($form->{"project_id_$i"}),
                  $form->{"serialnumber_$i"}, conv_i($pricegroup_id),
                  $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
-                 $form->{"cusordnumber_$i"}, $baseqty, $subtotal);
+                 $form->{"cusordnumber_$i"}, $baseqty, $subtotal,
+                 $form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
+                 $form->{"lastcost_$i"});
       do_query($form, $dbh, $query, @values);
 
       if ($form->{lizenzen} && $form->{"licensenumber_$i"}) {
@@ -984,7 +993,9 @@ Message: $form->{message}\r| if $form->{message};
                 storno = ?,
                 globalproject_id = ?,
                 cp_id = ?,
-                transaction_description = ?
+                transaction_description = ?,
+                marge_total = ?,
+                marge_percent = ?
               WHERE id = ?|;
   @values = ($form->{"invnumber"}, $form->{"ordnumber"}, $form->{"quonumber"}, $form->{"cusordnumber"},
              conv_date($form->{"invdate"}), conv_date($form->{"orddate"}), conv_date($form->{"quodate"}),
@@ -999,6 +1010,7 @@ Message: $form->{message}\r| if $form->{message};
              conv_i($form->{"employee_id"}), conv_i($form->{"salesman_id"}),
              $form->{"storno"} ? 't' : 'f', conv_i($form->{"globalproject_id"}),
              conv_i($form->{"cp_id"}), $form->{transaction_description},
+             $form->{marge_total}, $form->{marge_percent},
              conv_i($form->{"id"}));
   do_query($form, $dbh, $query, @values);
   
@@ -1050,34 +1062,31 @@ sub _delete_payments {
 
   my ($self, $form, $dbh) = @_;
 
-  my (@delete_oids, $delete_next, $sth, $ref);
+  my @delete_oids;
 
   # Delete old payment entries from acc_trans.
   my $query =
-    qq|SELECT at.oid, at.*, c.link
-       FROM acc_trans at
-       LEFT JOIN chart c ON (at.chart_id = c.id)
-       WHERE (trans_id = ?)
-       ORDER BY at.oid|;
+    qq|SELECT oid
+       FROM acc_trans
+       WHERE (trans_id = ?) AND fx_transaction
 
-  $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
+       UNION
 
-  while ($ref = $sth->fetchrow_hashref()) {
-    if ($delete_next) {
-      push @delete_oids, $ref->{oid};
-      undef $delete_next;
-      next;
-    }
-
-    $ref->{LINKS} = { map { $_, 1 } split m/:/, $ref->{link} };
-
-    if ($ref->{fx_transaction} || ($ref->{LINKS}->{AR} && ($ref->{amount} >= 0))) {
-      push @delete_oids, $ref->{oid};
-      $delete_next = $ref->{LINKS}->{AR};
-    }
-  }
+       SELECT at.oid
+       FROM acc_trans at
+       LEFT JOIN chart c ON (at.chart_id = c.id)
+       WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|;
+  push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
 
-  $sth->finish();
+  $query =
+    qq|SELECT at.oid
+       FROM acc_trans at
+       LEFT JOIN chart c ON (at.chart_id = c.id)
+       WHERE (trans_id = ?)
+         AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
+       ORDER BY at.oid
+       OFFSET 1|;
+  push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
 
   if (@delete_oids) {
     $query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
@@ -1097,18 +1106,11 @@ sub post_payment {
 
   my (%payments, $old_form, $row, $item, $query, %keep_vars);
 
-  my @prior;
-  push @prior, selectall_hashref_query($form, $dbh, qq|SELECT id, paid, datepaid FROM ar WHERE id = ?|, $form->{id});
-  push @prior, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
-
   $old_form = save_form();
 
   # Delete all entries in acc_trans from prior payments.
   $self->_delete_payments($form, $dbh);
 
-  my @after_dp;
-  push @after_dp, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
-
   # Save the new payments the user made before cleaning up $form.
   map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AR_paid_\d+$|^paidaccounts$/, keys %{ $form };
 
@@ -1119,7 +1121,7 @@ sub post_payment {
   # Retrieve the invoice from the database.
   $self->retrieve_invoice($myconfig, $form);
 
-  # Set up the content of $form in the way that IR::post_invoice() expects.
+  # Set up the content of $form in the way that IS::post_invoice() expects.
   $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
 
   for $row (1 .. scalar @{ $form->{invoice_details} }) {
@@ -1137,7 +1139,7 @@ sub post_payment {
   # Restore the payment options from the user input.
   map { $form->{$_} = $payments{$_} } keys %payments;
 
-  # Get the AP accno (which is normally done by Form::create_links()).
+  # Get the AR accno (which is normally done by Form::create_links()).
   $query =
     qq|SELECT c.accno
        FROM acc_trans at
@@ -1154,20 +1156,7 @@ sub post_payment {
 
   restore_form($old_form);
 
-  my @after;
-  push @after, selectall_hashref_query($form, $dbh, qq|SELECT id, paid, datepaid FROM ar WHERE id = ?|, $form->{id});
-  push @after, selectall_hashref_query($form, $dbh, qq|SELECT * FROM acc_trans WHERE trans_id = ? ORDER BY oid|, $form->{id});
-
-  foreach my $rows (@prior, @after_dp, @after) {
-    map { delete @{$_}{qw(itime mtime)} } @{ $rows };
-  }
-
-  map { $main::lxdebug->dump_sql_result(0, 'davor ', $_) } @prior;
-  map { $main::lxdebug->dump_sql_result(0, 'nachDP', $_) } @after_dp;
-  map { $main::lxdebug->dump_sql_result(0, 'danach', $_) } @after;
-
-  my $rc = 1;
-#   my $rc = $dbh->commit();
+  my $rc = $dbh->commit();
   $dbh->disconnect();
 
   $main::lxdebug->leave_sub();
@@ -1498,7 +1487,7 @@ sub retrieve_invoice {
            i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice,
            i.discount, i.parts_id AS id, i.unit, i.deliverydate,
            i.project_id, i.serialnumber, i.id AS invoice_pos, i.pricegroup_id,
-           i.ordnumber, i.transdate, i.cusordnumber, i.subtotal,
+           i.ordnumber, i.transdate, i.cusordnumber, i.subtotal, i.lastcost,
 
            p.partnumber, p.assembly, p.bin, p.notes AS partnotes,
            p.inventory_accno_id AS part_inventory_accno_id, p.formel,
@@ -1815,7 +1804,7 @@ sub retrieve_item {
   my $query =
     qq|SELECT
          p.id, p.partnumber, p.description, p.sellprice,
-         p.listprice, p.inventory_accno_id,
+         p.listprice, p.inventory_accno_id, p.lastcost,
 
          c1.accno AS inventory_accno,
          c1.new_chart_id AS inventory_new_chart,