temporary precision notes
[kivitendo-erp.git] / SL / IR.pm
index 8bd2f5f..90dbbab 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -418,7 +418,7 @@ SQL
                                 dbh          => $dbh);
 
     # link previous items with invoice items See IS.pm (no credit note -> no invoice item)
-    foreach (qw(delivery_order_items orderitems)) {
+    foreach (qw(delivery_order_items orderitems invoice)) {
       if (!$form->{useasnew} && $form->{"converted_from_${_}_id_$i"}) {
         RecordLinks->create_links('dbh'        => $dbh,
                                   'mode'       => 'ids',
@@ -690,7 +690,7 @@ SQL
   if ($payments_only) {
     $query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
     do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
-
+    $form->new_lastmtime('ap');
     if (!$provided_dbh) {
       $dbh->commit();
       $dbh->disconnect();
@@ -745,6 +745,7 @@ SQL
     do_query($form, $dbh, $query, conv_i($form->{id}));
   }
 
+  $form->new_lastmtime('ap');
 
   $form->{name} = $form->{vendor};
   $form->{name} =~ s/--\Q$form->{vendor_id}\E//;
@@ -757,16 +758,18 @@ SQL
 
   Common::webdav_folder($form);
 
-  # Link this record to the records it was created from.
-  if ($form->{convert_from_oe_ids}) {
-    RecordLinks->create_links('dbh'        => $dbh,
-                              'mode'       => 'ids',
-                              'from_table' => 'oe',
-                              'from_ids'   => $form->{convert_from_oe_ids},
-                              'to_table'   => 'ap',
-                              'to_id'      => $form->{id},
+  # Link this record to the records it was created from order or invoice (storno)
+  foreach (qw(oe ap)) {
+    if ($form->{"convert_from_${_}_ids"}) {
+      RecordLinks->create_links('dbh'        => $dbh,
+                                'mode'       => 'ids',
+                                'from_table' => $_,
+                                'from_ids'   => $form->{"convert_from_${_}_ids"},
+                                'to_table'   => 'ap',
+                                'to_id'      => $form->{id},
       );
-    delete $form->{convert_from_oe_ids};
+      delete $form->{"convert_from_${_}_ids"};
+    }
   }
 
   my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
@@ -800,6 +803,8 @@ SQL
 
   # safety check datev export
   if ($::instance_conf->get_datev_check_on_purchase_invoice) {
+    # if we need department for kostenstelle in DATEV check
+    $form->{department} = SL::DB::Manager::Department->find_by(id => $form->{department_id})->description if $form->{department_id};
     my $transdate = $::form->{invdate} ? DateTime->from_lxoffice($::form->{invdate}) : undef;
     $transdate  ||= DateTime->today;
 
@@ -873,17 +878,20 @@ sub reverse_invoice {
         #adjust allocated
         $form->update_balance($dbh, "invoice", "allocated", qq|id = $pthref->{id}|, $qty);
 
-        $form->update_balance($dbh, "acc_trans", "amount",
-                              qq|    (trans_id = $pthref->{trans_id})
-                                 AND (chart_id = $ref->{expense_accno_id})
-                                 AND (transdate = '$pthref->{transdate}')|,
-                              $amount);
+        if  ( $::instance_conf->get_inventory_system eq 'perpetual' ) {
 
-        $form->update_balance($dbh, "acc_trans", "amount",
-                              qq|    (trans_id = $pthref->{trans_id})
-                                 AND (chart_id = $ref->{inventory_accno_id})
-                                 AND (transdate = '$pthref->{transdate}')|,
-                              $amount * -1);
+          $form->update_balance($dbh, "acc_trans", "amount",
+                                qq|    (trans_id = $pthref->{trans_id})
+                                   AND (chart_id = $ref->{expense_accno_id})
+                                   AND (transdate = '$pthref->{transdate}')|,
+                                $amount);
+
+          $form->update_balance($dbh, "acc_trans", "amount",
+                                qq|    (trans_id = $pthref->{trans_id})
+                                   AND (chart_id = $ref->{inventory_accno_id})
+                                   AND (transdate = '$pthref->{transdate}')|,
+                                $amount * -1);
+        }
 
         last if (($ref->{allocated} -= $qty) <= 0);
       }
@@ -984,12 +992,15 @@ sub retrieve_invoice {
   $query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate,
                 orddate, quodate, globalproject_id,
                 ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate,
+                mtime, itime,
                 intnotes, (SELECT cu.name FROM currencies cu WHERE cu.id=ap.currency_id) AS currency, direct_debit,
                 delivery_term_id
               FROM ap
               WHERE id = ?|;
   $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
   map { $form->{$_} = $ref->{$_} } keys %$ref;
+  $form->{mtime} = $form->{itime} if !$form->{mtime};
+  $form->{lastmtime} = $form->{mtime};
 
   $form->{exchangerate}  = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell");
 
@@ -1129,7 +1140,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.notes AS intnotes,
          v.email, v.cc, v.bcc, v.language_id, v.payment_id, v.delivery_term_id,
          v.street, v.zipcode, v.city, v.country, v.taxzone_id, cu.name AS curr, v.direct_debit,
          $duedate + COALESCE(pt.terms_netto, 0) AS duedate,
@@ -1225,7 +1236,7 @@ sub retrieve_item {
     my $field = (split m{\.}, $table_column)[1];
     next unless $form->{"${field}_${i}"};
     $where .= " AND lower(${table_column}) LIKE lower(?)";
-    push @values, '%' . $form->{"${field}_${i}"} . '%';
+    push @values, like($form->{"${field}_${i}"});
   }
 
   my (%mm_by_id);
@@ -1239,7 +1250,7 @@ sub retrieve_item {
       LEFT JOIN parts ON parts.id = parts_id
       WHERE NOT parts.obsolete AND model ILIKE ? AND (make IS NULL OR make = ?);
     |;
-    my $mm_results = selectall_hashref_query($::form, $dbh, $mm_query, '%' . $form->{"partnumber_$i"} . '%', $::form->{vendor_id});
+    my $mm_results = selectall_hashref_query($::form, $dbh, $mm_query, like($form->{"partnumber_$i"}), $::form->{vendor_id});
     my @mm_ids     = map { $_->{parts_id} } @$mm_results;
     push @{$mm_by_id{ $_->{parts_id} } ||= []}, $_ for @$mm_results;
 
@@ -1336,7 +1347,7 @@ sub retrieve_item {
       push @{ $ref->{matches} ||= [] }, $::locale->text('Model') . ': ' . join ', ', map { $_->{model} } @{ $mm_by_id{$ref->{id}} };
     }
 
-    if ($ref->{ean} eq $::form->{"partnumber_$i"}) {
+    if (($::form->{"partnumber_$i"} ne '') && ($ref->{ean} eq $::form->{"partnumber_$i"})) {
       push @{ $ref->{matches} ||= [] }, $::locale->text('EAN') . ': ' . $ref->{ean};
     }
 
@@ -1454,8 +1465,8 @@ sub vendor_details {
        LIMIT 1|;
   my $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id}, @values);
 
-  # remove id and taxincluded before copy back
-  delete @$ref{qw(id taxincluded)};
+  # remove id,notes (double of vendornotes) and taxincluded before copy back
+  delete @$ref{qw(id taxincluded notes)};
 
   @wanted_vars = grep({ $_ } @wanted_vars);
   if (scalar(@wanted_vars) > 0) {