admin muss mit login.pl nicht mit am.pl verknuepft werden.
[kivitendo-erp.git] / SL / IS.pm
index 7943fa6..57d4a6c 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -103,7 +103,7 @@ sub invoice_details {
 
     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
-    if ($form->{"qty_$i"} != 0) {
+    if ($form->{"id_$i"} != 0) {
 
       # add number, description and qty to $form->{number},
       if ($form->{"subtotal_$i"} && !$subtotal_header) {
@@ -355,11 +355,11 @@ sub invoice_details {
 
   $form->{invtotal} =
     ($form->{taxincluded}) ? $form->{total} : $form->{total} + $tax;
+  $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
+  $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
   $form->{total} =
     $form->format_amount($myconfig, $form->{invtotal} - $form->{paid}, 2);
-  $form->{invtotal} = $form->format_amount($myconfig, $form->{invtotal}, 2);
   $form->set_payment_options($myconfig, $form->{invdate});
-  $form->{paid} = $form->format_amount($myconfig, $form->{paid}, 2);
 
   $form->{username} = $myconfig->{name};
 
@@ -391,7 +391,7 @@ sub project_description {
 sub customer_details {
   $main::lxdebug->enter_sub();
 
-  my ($self, $myconfig, $form) = @_;
+  my ($self, $myconfig, $form, @wanted_vars) = @_;
 
   # connect to database
   my $dbh = $form->dbconnect($myconfig);
@@ -416,6 +416,14 @@ sub customer_details {
 
   # remove id and taxincluded before copy back
   delete @$ref{qw(id taxincluded)};
+
+  @wanted_vars = grep({ $_ } @wanted_vars);
+  if (scalar(@wanted_vars) > 0) {
+    my %h_wanted_vars;
+    map({ $h_wanted_vars{$_} = 1; } @wanted_vars);
+    map({ delete($ref->{$_}) unless ($h_wanted_vars{$_}); } keys(%{$ref}));
+  }
+
   map { $form->{$_} = $ref->{$_} } keys %$ref;
   $sth->finish;
 
@@ -543,7 +551,7 @@ sub post_invoice {
       $form->{"qty_$i"} *= -1;
     }
 
-    if ($form->{"qty_$i"} != 0) {
+    if ($form->{"id_$i"}) {
 
       # get item baseunit
       $query = qq|SELECT p.unit
@@ -672,7 +680,7 @@ sub post_invoice {
                                 $baseqty * -1)
             unless $form->{shipped};
 
-          $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor);
+          $allocated = &cogs($dbh, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
         }
       }
 
@@ -1239,14 +1247,17 @@ sub process_assembly {
 sub cogs {
   $main::lxdebug->enter_sub();
 
-  my ($dbh, $form, $id, $totalqty, $basefactor) = @_;
-
+  my ($dbh, $form, $id, $totalqty, $basefactor, $row) = @_;
+  $form->{taxzone_id} *=1;
+  my $transdate = ($form->{invdate}) ? "'$form->{invdate}'" : "current_date";
   my $query = qq|SELECT i.id, i.trans_id, i.base_qty, i.allocated, i.sellprice,
-                   (SELECT c.accno FROM chart c
-                   WHERE p.inventory_accno_id = c.id) AS inventory_accno,
-                  (SELECT c.accno FROM chart c
-                   WHERE p.expense_accno_id = c.id) AS expense_accno
+                        c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
+                       c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate)  - c2.valid_from as income_valid,
+                       c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid
                  FROM invoice i, parts p
+                  LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
+                  LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
+                  LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
                  WHERE i.parts_id = p.id
                  AND i.parts_id = $id
                  AND (i.base_qty + i.allocated) < 0
@@ -1269,12 +1280,12 @@ sub cogs {
     # sellprice is the cost of the item
     $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / $basefactor, 2);
 
-    if (!$eur) {
-
+    if (!$main::eur) {
+      $ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
       # add to expense
       $form->{amount}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
       $form->{expense_inventory} .= " " . $ref->{expense_accno};
-
+      $ref->{inventory_accno} = ($form->{"inventory_accno_$row"}) ? $form->{"inventory_accno_$row"} : $ref->{inventory_accno};
       # deduct inventory
       $form->{amount}{ $form->{id} }{ $ref->{inventory_accno} } -= -$linetotal;
       $form->{expense_inventory} .= " " . $ref->{inventory_accno};
@@ -1472,7 +1483,7 @@ sub retrieve_invoice {
 
     # retrieve invoice
     $query = qq|SELECT a.invnumber, a.ordnumber, a.quonumber, a.cusordnumber,
-                a.transdate AS invdate, a.deliverydate, a.paid, a.storno,
+                a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate,
                 a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id,
                a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
                a.employee_id, e.name AS employee, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type
@@ -1597,10 +1608,10 @@ sub retrieve_invoice {
       # get tax rates and description
       $accno_id =
         ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
-      $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
-                FROM tax t LEFT join chart c ON (c.id=t.chart_id)
-                WHERE t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
-                ORDER BY accno|;
+    $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
+             FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
+             WHERE t.id in (SELECT tk.tax_id from taxkeys tk where tk.chart_id = (SELECT id from chart WHERE accno='$accno_id') AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
+             ORDER BY c.accno|;
       $stw = $dbh->prepare($query);
       $stw->execute || $form->dberror($query);
       $ref->{taxaccounts} = "";
@@ -1957,7 +1968,7 @@ sub retrieve_item {
       ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
     $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
              FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
-             WHERE t.taxkey in (SELECT c2.taxkey_id from chart c2 where c2.accno = '$accno_id')
+             WHERE t.id in (SELECT tk.tax_id from taxkeys tk where tk.chart_id = (SELECT id from chart WHERE accno='$accno_id') AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
              ORDER BY c.accno|;
     $stw = $dbh->prepare($query);
     $stw->execute || $form->dberror($query);