Nicht-editierbare CVars bei "als neu speichern/verwenden" richtig setzen.
[kivitendo-erp.git] / SL / IR.pm
index 98dcc86..a87fca3 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -106,6 +106,8 @@ sub post_invoice {
   for my $i (1 .. $form->{rowcount}) {
     next unless $form->{"id_$i"};
 
+    my $position = $i;
+
     $form->{"qty_$i"}  = $form->parse_amount($myconfig, $form->{"qty_$i"});
     $form->{"qty_$i"} *= -1 if $form->{storno};
 
@@ -367,20 +369,27 @@ sub post_invoice {
 
     next if $payments_only;
 
+    CVar->get_non_editable_ic_cvars(form               => $form,
+                                    dbh                => $dbh,
+                                    row                => $i, 
+                                    sub_module         => 'invoice',
+                                    may_converted_from => ['delivery_order_items', 'orderitems', 'invoice']);
+
     if (!$form->{"invoice_id_$i"}) {
       # there is no persistent id, therefore create one with all necessary constraints
       my $q_invoice_id = qq|SELECT nextval('invoiceid')|;
       my $h_invoice_id = prepare_query($form, $dbh, $q_invoice_id);
       do_statement($form, $h_invoice_id, $q_invoice_id);
       $form->{"invoice_id_$i"}  = $h_invoice_id->fetchrow_array();
-      my $q_create_invoice_id = qq|INSERT INTO invoice (id, trans_id, parts_id) values (?, ?, ?)|;
-      do_query($form, $dbh, $q_create_invoice_id, conv_i($form->{"invoice_id_$i"}), conv_i($form->{id}), conv_i($form->{"id_$i"}));
+      my $q_create_invoice_id = qq|INSERT INTO invoice (id, trans_id, position, parts_id) values (?, ?, ?, ?)|;
+      do_query($form, $dbh, $q_create_invoice_id, conv_i($form->{"invoice_id_$i"}),
+               conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}));
       $h_invoice_id->finish();
     }
 
       # save detail record in invoice table
       $query = <<SQL;
-        UPDATE invoice SET trans_id = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
+        UPDATE invoice SET trans_id = ?, position = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
                            sellprice = ?, fxsellprice = ?, discount = ?, allocated = ?, unit = ?, deliverydate = ?,
                            project_id = ?, serialnumber = ?, price_factor_id = ?,
                            price_factor = (SELECT factor FROM price_factors WHERE id = ?), marge_price_factor = ?,
@@ -388,7 +397,7 @@ sub post_invoice {
         WHERE id = ?
 SQL
 
-    @values = (conv_i($form->{id}), conv_i($form->{"id_$i"}),
+    @values = (conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}),
                $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}), $form->{"qty_$i"} * -1,
                $baseqty * -1, $form->{"sellprice_$i"}, $fxsellprice, $form->{"discount_$i"}, $allocated,
                $form->{"unit_$i"}, conv_date($form->{deliverydate}),
@@ -407,6 +416,20 @@ SQL
                                 name_prefix  => 'ic_',
                                 name_postfix => "_$i",
                                 dbh          => $dbh);
+
+    # link previous items with invoice items See IS.pm (no credit note -> no invoice item)
+    foreach (qw(delivery_order_items orderitems)) {
+      if (!$form->{useasnew} && $form->{"converted_from_${_}_id_$i"}) {
+        RecordLinks->create_links('dbh'        => $dbh,
+                                  'mode'       => 'ids',
+                                  'from_table' => $_,
+                                  'from_ids'   => $form->{"converted_from_${_}_id_$i"},
+                                  'to_table'   => 'invoice',
+                                  'to_id'      => $form->{"invoice_id_$i"},
+        );
+      }
+      delete $form->{"converted_from_${_}_id_$i"};
+    }
   }
 
   $h_item_unit->finish();
@@ -735,14 +758,16 @@ SQL
   Common::webdav_folder($form);
 
   # Link this record to the records it was created from.
-  RecordLinks->create_links('dbh'        => $dbh,
-                            'mode'       => 'ids',
-                            'from_table' => 'oe',
-                            'from_ids'   => $form->{convert_from_oe_ids},
-                            'to_table'   => 'ap',
-                            'to_id'      => $form->{id},
-    );
-  delete $form->{convert_from_oe_ids};
+  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},
+      );
+    delete $form->{convert_from_oe_ids};
+  }
 
   my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
   if (scalar @convert_from_do_ids) {
@@ -1003,7 +1028,7 @@ sub retrieve_invoice {
 
         WHERE i.trans_id = ?
 
-        ORDER BY i.id|;
+        ORDER BY i.position|;
   $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
 
   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
@@ -1622,5 +1647,4 @@ sub get_duedate {
   return $duedate;
 }
 
-
 1;