Bug 1579: Einkaufsrechnung mit bestimmten Werten und 'Steuer im Preis inbegriffen'
[kivitendo-erp.git] / SL / IR.pm
index 7e24d4e..602ffc3 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -103,7 +103,7 @@ sub post_invoice {
     $form->{"qty_$i"}  = $form->parse_amount($myconfig, $form->{"qty_$i"});
     $form->{"qty_$i"} *= -1 if $form->{storno};
 
-    $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $main::eur;
+    $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $::lx_office_conf{system}->{eur};
 
     # get item baseunit
     if (!$item_units{$form->{"id_$i"}}) {
@@ -158,8 +158,10 @@ sub post_invoice {
       $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
 
       if ($form->{taxincluded}) {
+        
         $taxamount              = $linetotal * ($taxrate / (1 + $taxrate));
         $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
+
       } else {
         $taxamount = $linetotal * $taxrate;
       }
@@ -318,12 +320,12 @@ sub post_invoice {
     my ($invoice_id) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('invoiceid')|);
 
     $query =
-      qq|INSERT INTO invoice (id, trans_id, parts_id, description, qty, base_qty,
+      qq|INSERT INTO invoice (id, trans_id, parts_id, description, longdescription, qty, base_qty,
                               sellprice, fxsellprice, discount, allocated, unit, deliverydate,
                               project_id, serialnumber, price_factor_id, price_factor, marge_price_factor)
-         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT factor FROM price_factors WHERE id = ?), ?)|;
+         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT factor FROM price_factors WHERE id = ?), ?)|;
     @values = ($invoice_id, conv_i($form->{id}), conv_i($form->{"id_$i"}),
-               $form->{"description_$i"}, $form->{"qty_$i"} * -1,
+               $form->{"description_$i"}, $form->{"longdescription_$i"}, $form->{"qty_$i"} * -1,
                $baseqty * -1, $form->{"sellprice_$i"}, $fxsellprice, $form->{"discount_$i"}, $allocated,
                $form->{"unit_$i"}, conv_date($form->{deliverydate}),
                conv_i($form->{"project_id_$i"}), $form->{"serialnumber_$i"},
@@ -380,11 +382,43 @@ sub post_invoice {
     $invoicediff += $paiddiff;
     $expensediff += $paiddiff;
 
-    ######## this only applies to tax included
+######## this only applies to tax included
+
+    # in the sales invoice case rounding errors only have to be corrected for
+    # income accounts, it is enough to add the total rounding error to one of
+    # the income accounts, with the one assigned to the last row being used
+    # (lastinventoryaccno)
+    
+    # in the purchase invoice case rounding errors may be split between
+    # inventory accounts and expense accounts. After rounding, an error of 1
+    # cent is introduced if the total rounding error exceeds 0.005. The total
+    # error is made up of $invoicediff and $expensediff, however, so if both
+    # values are below 0.005, but add up to a total >= 0.005, correcting
+    # lastinventoryaccno and lastexpenseaccno separately has no effect after
+    # rounding. This caused bug 1579. Therefore when the combined total exceeds
+    # 0.005, but neither do individually, the account with the larger value
+    # shall receive the total rounding error, and the next time it is rounded
+    # the 1 cent correction will be introduced.
+
+    my $total_rounding_diff = $invoicediff+$expensediff;
 
     $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno;
     $form->{amount}{ $form->{id} }{$lastexpenseaccno}   -= $expensediff if $lastexpenseaccno;
 
+    if ( ($expensediff+$invoicediff) >= 0.005 and $expensediff < 0.005 and $invoicediff < 0.005 ) {
+
+      # in total the rounding error adds up to 1 cent effectively, correct the
+      # larger of the two numbers
+
+      if ( abs($form->{amount}{ $form->{id} }{$lastinventoryaccno}) > abs($form->{amount}{ $form->{id} }{$lastexpenseaccno}) ) {
+        # $invoicediff has already been deducted, now also deduct expensediff
+        $form->{amount}{ $form->{id} }{$lastinventoryaccno}   -= $expensediff;
+      } else {
+        # expensediff has already been deducted, now also deduct invoicediff
+        $form->{amount}{ $form->{id} }{$lastexpenseaccno}   -= $invoicediff;
+      };
+    };
+
   } else {
     $amount    = $form->round_amount($netamount * $form->{exchangerate}, 2);
     $paiddiff  = $amount - $netamount * $form->{exchangerate};
@@ -405,21 +439,22 @@ sub post_invoice {
 
   $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0;
 
-  # update exchangerate
+# update exchangerate
 
   $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate})
     if ($form->{currency} ne $defaultcurrency) && !$exchangerate;
 
-  # record acc_trans transactions
+# record acc_trans transactions
   foreach my $trans_id (keys %{ $form->{amount} }) {
     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
       $form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2);
 
+
       next if $payments_only || !$form->{amount}{$trans_id}{$accno};
 
       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id)
                   VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?,
-                          (SELECT taxkey_id  FROM chart WHERE accno = ?), ?)|;
+                  (SELECT taxkey_id  FROM chart WHERE accno = ?), ?)|;
       @values = ($trans_id, $accno, $form->{amount}{$trans_id}{$accno},
                  conv_date($form->{invdate}), $accno, $project_id);
       do_query($form, $dbh, $query, @values);
@@ -527,7 +562,14 @@ sub post_invoice {
 
   # set values which could be empty
   my $taxzone_id         = $form->{taxzone_id} * 1;
-  $form->{department_id} = (split /--/, $form->{department})[1];
+
+  # Seit neuestem wird die department_id schon übergeben UND $form->department nicht mehr
+  # korrekt zusammengebaut. Sehr wahrscheinlich beim Umstieg auf T8 kaputt gegangen
+  # Ich lass den Code von 2005 erstmal noch stehen ;-) jb 03-2011
+  # copy & paste von IS.pm
+  if (!$form->{department_id}){
+    $form->{department_id} = (split /--/, $form->{department})[1];
+  }
   $form->{invnumber}     = $form->{id} unless $form->{invnumber};
 
   $taxzone_id = 0 if (3 < $taxzone_id) || (0 > $taxzone_id);
@@ -577,7 +619,7 @@ sub post_invoice {
   # delete zero entries
   do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE amount = 0|);
 
-  Common::webdav_folder($form) if ($main::webdav);
+  Common::webdav_folder($form);
 
   # Link this record to the records it was created from.
   RecordLinks->create_links('dbh'        => $dbh,
@@ -799,7 +841,7 @@ sub retrieve_invoice {
         c3.accno AS expense_accno,   c3.new_chart_id AS expense_new_chart,   date($transdate) - c3.valid_from AS expense_valid,
 
         i.id AS invoice_id,
-        i.description, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber,
+        i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber,
         i.price_factor_id, i.price_factor, i.marge_price_factor, i.discount,
         p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, p.bin, pr.projectnumber, pg.partsgroup
 
@@ -875,7 +917,7 @@ sub retrieve_invoice {
   }
   $sth->finish();
 
-  Common::webdav_folder($form) if ($main::webdav);
+  Common::webdav_folder($form);
 
   $dbh->disconnect();
 
@@ -917,7 +959,7 @@ sub get_vendor {
   my $query =
     qq|SELECT
          v.id AS vendor_id, v.name AS vendor, v.discount as vendor_discount,
-        v.creditlimit, v.terms, v.notes AS intnotes,
+         v.creditlimit, v.terms, v.notes AS intnotes,
          v.email, v.cc, v.bcc, v.language_id, v.payment_id,
          v.street, v.zipcode, v.city, v.country, v.taxzone_id,
          $duedate + COALESCE(pt.terms_netto, 0) AS duedate,