Helfermethoden in Belegobjekten.
[kivitendo-erp.git] / SL / IR.pm
index f87cdf8..edf55f1 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -83,7 +83,7 @@ sub post_invoice {
   if ($form->{currency} eq $defaultcurrency) {
     $form->{exchangerate} = 1;
   } else {
-    $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
+    $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{invdate}, 'sell');
   }
 
   $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
@@ -103,7 +103,11 @@ 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 $::lx_office_conf{system}->{eur};
+    if ( $::instance_conf->get_inventory_system eq 'periodic') {
+      # inventory account number is overwritten with expense account number, so
+      # never book incoming to inventory account but always to expense account
+      $form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"}
+    };
 
     # get item baseunit
     if (!$item_units{$form->{"id_$i"}}) {
@@ -203,15 +207,15 @@ sub post_invoice {
 
       next if $payments_only;
 
-      # update parts table
+      # update parts table by setting lastcost to current price, don't allow negative values by using abs
       $query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
-      @values = ($form->{"sellprice_$i"}, conv_i($form->{"id_$i"}));
+      @values = (abs($form->{"sellprice_$i"}), conv_i($form->{"id_$i"}));
       do_query($form, $dbh, $query, @values);
 
       # check if we sold the item already and
       # make an entry for the expense and inventory
       $query =
-        qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
+        qq|SELECT i.id, i.qty, i.allocated, i.trans_id, i.base_qty,
              p.inventory_accno_id, p.expense_accno_id, a.transdate
            FROM invoice i, ar a, parts p
            WHERE (i.parts_id = p.id)
@@ -219,6 +223,20 @@ sub post_invoice {
              AND ((i.base_qty + i.allocated) > 0)
              AND (i.trans_id = a.id)
            ORDER BY transdate|;
+           # ORDER BY transdate guarantees FIFO
+
+# sold two items without having bought them yet, example result of query:
+# id | qty | allocated | trans_id | inventory_accno_id | expense_accno_id | transdate
+# ---+-----+-----------+----------+--------------------+------------------+------------
+#  9 |   2 |         0 |        9 |                 15 |              151 | 2011-01-05
+
+# base_qty + allocated > 0 if article has already been sold but not bought yet
+
+# select qty,allocated,base_qty,sellprice from invoice where trans_id = 9;
+#  qty | allocated | base_qty | sellprice
+# -----+-----------+----------+------------
+#    2 |         0 |        2 | 1000.00000
+
       $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"id_$i"}));
 
       my $totalqty = $baseqty;
@@ -227,32 +245,38 @@ sub post_invoice {
         my $qty    = min $totalqty, ($ref->{base_qty} + $ref->{allocated});
         $linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2);
 
-        if ($ref->{allocated} < 0) {
-
-          # we have an entry for it already, adjust amount
-          $form->update_balance($dbh, "acc_trans", "amount",
-                                qq|    (trans_id = $ref->{trans_id})
-                                   AND (chart_id = $ref->{inventory_accno_id})
-                                   AND (transdate = '$ref->{transdate}')|,
-                                $linetotal);
-
-          $form->update_balance($dbh, "acc_trans", "amount",
-                                qq|    (trans_id = $ref->{trans_id})
-                                   AND (chart_id = $ref->{expense_accno_id})
-                                   AND (transdate = '$ref->{transdate}')|,
-                                $linetotal * -1);
-
-        } elsif ($linetotal != 0) {
-          # add entry for inventory, this one is for the sold item
-          $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|;
-          @values = ($ref->{trans_id},  $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id});
-          do_query($form, $dbh, $query, @values);
-
-          # add expense
-          $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|;
-          @values = ($ref->{trans_id},  $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id});
-          do_query($form, $dbh, $query, @values);
-        }
+        if  ( $::instance_conf->get_inventory_system eq 'perpetual' ) {
+        # Warenbestandsbuchungen nur bei Bestandsmethode
+
+          if ($ref->{allocated} < 0) {
+
+# we have an entry for it already, adjust amount
+            $form->update_balance($dbh, "acc_trans", "amount",
+                qq|    (trans_id = $ref->{trans_id})
+                AND (chart_id = $ref->{inventory_accno_id})
+                AND (transdate = '$ref->{transdate}')|,
+                $linetotal);
+
+            $form->update_balance($dbh, "acc_trans", "amount",
+                qq|    (trans_id = $ref->{trans_id})
+                AND (chart_id = $ref->{expense_accno_id})
+                AND (transdate = '$ref->{transdate}')|,
+                $linetotal * -1);
+
+          } elsif ($linetotal != 0) {
+
+            # allocated >= 0
+            # add entry for inventory, this one is for the sold item
+            $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|;
+            @values = ($ref->{trans_id},  $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id});
+            do_query($form, $dbh, $query, @values);
+
+# add expense
+            $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|;
+            @values = ($ref->{trans_id},  $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id});
+            do_query($form, $dbh, $query, @values);
+          }
+        };
 
         # update allocated for sold item
         $form->update_balance($dbh, "invoice", "allocated", qq|id = $ref->{id}|, $qty * -1);
@@ -265,6 +289,8 @@ sub post_invoice {
       $sth->finish();
 
     } else {                    # if ($form->{"inventory_accno_id_$i"})
+      # part doesn't have an inventory_accno_id
+      # lastcost of the part is updated at the end
 
       $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
 
@@ -473,6 +499,12 @@ sub post_invoice {
 
   # record payments and offsetting AP
   for my $i (1 .. $form->{paidaccounts}) {
+    if ($form->{"acc_trans_id_$i"}
+        && $payments_only
+        && ($::lx_office_conf{features}->{payments_changeable} == 0)) {
+      next;
+    }
+
     next if $form->{"paid_$i"} == 0;
 
     my ($accno)            = split /--/, $form->{"AP_paid_$i"};
@@ -492,11 +524,14 @@ sub post_invoice {
     }
 
     # record payment
-    $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, taxkey, project_id)
-                VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?,
+    my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
+
+    $query =
+      qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, project_id)
+                VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?,
                 (SELECT taxkey_id FROM chart WHERE accno = ?), ?)|;
     @values = (conv_i($form->{id}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"},
-               $form->{"source_$i"}, $form->{"memo_$i"}, $accno, $project_id);
+               $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, $project_id);
     do_query($form, $dbh, $query, @values);
 
     $exchangerate = 0;
@@ -818,6 +853,9 @@ sub retrieve_invoice {
   $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
+  # remove any trailing whitespace
+  $form->{currency} =~ s/\s*$//;
+
   $form->{exchangerate}  = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell");
 
   # get shipto
@@ -959,7 +997,7 @@ sub get_vendor {
          v.id AS vendor_id, v.name AS vendor, v.discount as vendor_discount,
          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,
+         v.street, v.zipcode, v.city, v.country, v.taxzone_id, v.curr,
          $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
          b.description AS business
        FROM vendor v
@@ -969,6 +1007,12 @@ sub get_vendor {
   my $ref = selectfirst_hashref_query($form, $dbh, $query, @values);
   map { $params->{$_} = $ref->{$_} } keys %$ref;
 
+  # remove any trailing whitespace
+  $form->{curr} =~ s/\s*$//;
+
+  # use vendor currency if not empty
+  $form->{currency} = $form->{curr} if $form->{curr};
+
   $params->{creditremaining} = $params->{creditlimit};
 
   $query = qq|SELECT SUM(amount - paid) FROM ap WHERE vendor_id = ?|;
@@ -1067,9 +1111,10 @@ sub retrieve_item {
       push @values, $form->{"partnumber_$i"};
    }
 
+  # Search for part ID overrides all other criteria.
   if ($form->{"id_${i}"}) {
-    $where .= qq| AND p.id = ?|;
-    push @values, $form->{"id_${i}"};
+    $where  = qq|p.id = ?|;
+    @values = ($form->{"id_${i}"});
   }
 
   if ($form->{"description_$i"}) {
@@ -1129,6 +1174,19 @@ sub retrieve_item {
        WHERE $where|;
   my $sth = prepare_execute_query($form, $dbh, $query, @values);
 
+  my @translation_queries = ( [ qq|SELECT tr.translation, tr.longdescription
+                                   FROM translation tr
+                                   WHERE tr.language_id = ? AND tr.parts_id = ?| ],
+                              [ qq|SELECT tr.translation, tr.longdescription
+                                   FROM translation tr
+                                   WHERE tr.language_id IN
+                                     (SELECT id
+                                      FROM language
+                                      WHERE article_code = (SELECT article_code FROM language WHERE id = ?))
+                                     AND tr.parts_id = ?
+                                   LIMIT 1| ] );
+  map { push @{ $_ }, prepare_query($form, $dbh, $_->[0]) } @translation_queries;
+
   $form->{item_list} = [];
   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
@@ -1178,6 +1236,16 @@ sub retrieve_item {
         $form->{taxaccounts}                 .= "$ptr->{accno} ";
       }
 
+      if ($form->{language_id}) {
+        for my $spec (@translation_queries) {
+          do_statement($form, $spec->[1], $spec->[0], conv_i($form->{language_id}), conv_i($ref->{id}));
+          my ($translation, $longdescription) = $spec->[1]->fetchrow_array;
+          next unless $translation;
+          $ref->{description} = $translation;
+          $ref->{longdescription} = $longdescription;
+          last;
+        }
+      }
     }
 
     $stw->finish();
@@ -1190,6 +1258,7 @@ sub retrieve_item {
   }
 
   $sth->finish();
+  $_->[1]->finish for @translation_queries;
 
   foreach my $item (@{ $form->{item_list} }) {
     my $custom_variables = CVar->get_custom_variables(module   => 'IC',
@@ -1226,7 +1295,8 @@ sub vendor_details {
   # get rest for the vendor
   # fax and phone and email as vendor*
   my $query =
-    qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail
+    qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail,
+         ct.curr AS currency
        FROM vendor ct
        LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id)
        WHERE (ct.id = ?) $contact
@@ -1245,6 +1315,8 @@ sub vendor_details {
   }
 
   map { $form->{$_} = $ref->{$_} } keys %$ref;
+  # remove any trailing whitespace
+  $form->{currency} =~ s/\s*$// if ($form->{currency});
 
   my $custom_variables = CVar->get_custom_variables('dbh'      => $dbh,
                                                     'module'   => 'CT',
@@ -1343,10 +1415,12 @@ sub post_payment {
   $old_form = save_form();
 
   # Delete all entries in acc_trans from prior payments.
-  $self->_delete_payments($form, $dbh);
+  if ($::lx_office_conf{features}->{payments_changeable} != 0) {
+    $self->_delete_payments($form, $dbh);
+  }
 
   # 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+$|^AP_paid_\d+$|^paidaccounts$/, keys %{ $form };
+  map { $payments{$_} = $form->{$_} } grep m/^datepaid_\d+$|^gldate_\d+$|^acc_trans_id_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^AP_paid_\d+$|^paidaccounts$/, keys %{ $form };
 
   # Clean up $form so that old content won't tamper the results.
   %keep_vars = map { $_, 1 } qw(login password id);
@@ -1399,38 +1473,26 @@ sub post_payment {
 }
 
 sub get_duedate {
-  $main::lxdebug->enter_sub();
+  $::lxdebug->enter_sub;
 
-  my $self     = shift;
-  my %params   = @_;
+  my ($self, %params) = @_;
 
   if (!$params{vendor_id} || !$params{invdate}) {
-    $main::lxdebug->leave_sub();
+    $::lxdebug->leave_sub;
     return $params{default};
   }
 
-  my $myconfig = \%main::myconfig;
-  my $form     = $main::form;
-
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
-
+  my $dbh      = $::form->get_standard_dbh;
   my $query    = qq|SELECT ?::date + pt.terms_netto
                     FROM vendor v
                     LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)
                     WHERE v.id = ?|;
 
-  my ($sth, $duedate);
-
-  if (($sth = $dbh->prepare($query)) && $sth->execute($params{invdate}, conv_i($params{vendor_id}))) {
-    ($duedate) = $sth->fetchrow_array();
-    $sth->finish();
-  } else {
-    $dbh->rollback();
-  }
+  my ($duedate) = selectfirst_array_query($::form, $dbh, $query, $params{invdate}, $params{vendor_id});
 
   $duedate ||= $params{default};
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->leave_sub;
 
   return $duedate;
 }