Nicht-editierbare CVars bei "als neu speichern/verwenden" richtig setzen.
[kivitendo-erp.git] / SL / IS.pm
index e09c08c..43303e8 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -373,6 +373,12 @@ sub invoice_details {
         $sth->finish;
       }
 
+      CVar->get_non_editable_ic_cvars(form               => $form,
+                                      dbh                => $dbh,
+                                      row                => $i, 
+                                      sub_module         => 'invoice',
+                                      may_converted_from => ['delivery_order_items', 'orderitems', 'invoice']);
+
       push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
         CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
           for @{ $ic_cvar_configs };
@@ -578,13 +584,6 @@ sub post_invoice {
   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
   my $defaultcurrency = $form->{defaultcurrency};
 
-  # 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
-  if (!$form->{department_id}){
-    ($null, $form->{department_id}) = split(/--/, $form->{department});
-  }
-
   my $all_units = AM->retrieve_units($myconfig, $form);
 
   if (!$payments_only) {
@@ -655,6 +654,7 @@ sub post_invoice {
 
     if ($form->{"id_$i"}) {
       my $item_unit;
+      my $position = $i;
 
       if (defined($baseunits{$form->{"id_$i"}})) {
         $item_unit = $baseunits{$form->{"id_$i"}};
@@ -740,7 +740,7 @@ sub post_invoice {
 
         if ($form->{"assembly_$i"}) {
           # record assembly item as allocated
-          &process_assembly($dbh, $myconfig, $form, $form->{"id_$i"}, $baseqty);
+          &process_assembly($dbh, $myconfig, $form, $position, $form->{"id_$i"}, $baseqty);
 
         } else {
           $allocated = &cogs($dbh, $myconfig, $form, $form->{"id_$i"}, $baseqty, $basefactor, $i);
@@ -755,20 +755,27 @@ sub post_invoice {
       $pricegroup_id *= 1;
       $pricegroup_id  = undef if !$pricegroup_id;
 
+      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 = ?,
+        UPDATE invoice SET trans_id = ?, position = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?,
                            sellprice = ?, fxsellprice = ?, discount = ?, allocated = ?, assemblyitem = ?,
                            unit = ?, deliverydate = ?, project_id = ?, serialnumber = ?, pricegroup_id = ?,
                            ordnumber = ?, donumber = ?, transdate = ?, cusordnumber = ?, base_qty = ?, subtotal = ?,
@@ -777,7 +784,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"},
                  $form->{"sellprice_$i"}, $fxsellprice,
                  $form->{"discount_$i"}, $allocated, 'f',
@@ -803,6 +810,19 @@ SQL
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
     }
+    # link previous items with invoice items
+    foreach (qw(delivery_order_items orderitems invoice)) {
+      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"};
+    }
   }
 
   # total payments, don't move we need it here
@@ -1200,14 +1220,16 @@ SQL
   }
 
   # 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'   => 'ar',
-                            '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'   => 'ar',
+                              '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};
 
@@ -1380,7 +1402,7 @@ sub post_payment {
 sub process_assembly {
   $main::lxdebug->enter_sub();
 
-  my ($dbh, $myconfig, $form, $id, $totalqty) = @_;
+  my ($dbh, $myconfig, $form, $position, $id, $totalqty) = @_;
 
   my $query =
     qq|SELECT a.parts_id, a.qty, p.assembly, p.partnumber, p.description, p.unit,
@@ -1401,7 +1423,7 @@ sub process_assembly {
     $ref->{qty} *= $totalqty;
 
     if ($ref->{assembly}) {
-      &process_assembly($dbh, $myconfig, $form, $ref->{parts_id}, $ref->{qty});
+      &process_assembly($dbh, $myconfig, $form, $position, $ref->{parts_id}, $ref->{qty});
       next;
     } else {
       if ($ref->{inventory_accno_id}) {
@@ -1411,9 +1433,10 @@ sub process_assembly {
 
     # save detail record for individual assembly item in invoice table
     $query =
-      qq|INSERT INTO invoice (trans_id, description, parts_id, qty, sellprice, fxsellprice, allocated, assemblyitem, unit)
-         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
-    my @values = (conv_i($form->{id}), $ref->{description}, conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
+      qq|INSERT INTO invoice (trans_id, position, description, parts_id, qty, sellprice, fxsellprice, allocated, assemblyitem, unit)
+         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
+    my @values = (conv_i($form->{id}), conv_i($position), $ref->{description},
+                  conv_i($ref->{parts_id}), $ref->{qty}, 0, 0, $allocated, 't', $ref->{unit});
     do_query($form, $dbh, $query, @values);
 
   }
@@ -1715,7 +1738,7 @@ sub retrieve_invoice {
          LEFT JOIN chart c2 ON ((SELECT tc.income_accno_id FROM taxzone_charts tc WHERE tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c2.id)
          LEFT JOIN chart c3 ON ((SELECT tc.expense_accno_id FROM taxzone_charts tc WHERE tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c3.id)
 
-         WHERE (i.trans_id = ?) AND NOT (i.assemblyitem = '1') ORDER BY i.id|;
+         WHERE (i.trans_id = ?) AND NOT (i.assemblyitem = '1') ORDER BY i.position|;
 
     $sth = prepare_execute_query($form, $dbh, $query, $id);