Nicht-editierbare CVars bei "als neu speichern/verwenden" richtig setzen.
[kivitendo-erp.git] / SL / OE.pm
index 58792ac..c2cde73 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -381,12 +381,6 @@ sub save {
   $form->{$number_field} ||= $trans_number->create_unique;
 
   if ($form->{id}) {
-    $query = qq|DELETE FROM custom_variables
-                WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC'))
-                  AND (sub_module = 'orderitems')
-                  AND (trans_id IN (SELECT id FROM orderitems WHERE trans_id = ?))|;
-    do_query($form, $dbh, $query, $form->{id});
-
     $query = qq|DELETE FROM shipto | .
              qq|WHERE trans_id = ? AND module = 'OE'|;
     do_query($form, $dbh, $query, $form->{id});
@@ -515,6 +509,12 @@ sub save {
       $pricegroup_id *= 1;
       $pricegroup_id  = undef if !$pricegroup_id;
 
+      CVar->get_non_editable_ic_cvars(form               => $form,
+                                      dbh                => $dbh,
+                                      row                => $i, 
+                                      sub_module         => 'orderitems',
+                                      may_converted_from => ['orderitems', 'invoice']);
+
       my $position = $i;
 
       # save detail record in orderitems table
@@ -525,6 +525,7 @@ sub save {
         $query = qq|INSERT INTO orderitems (id, position) VALUES (?, ?)|;
         do_query($form, $dbh, $query, $form->{"orderitems_id_$i"}, conv_i($position));
       }
+
       my $orderitems_id = $form->{"orderitems_id_$i"};
       push @processed_orderitems, $orderitems_id;
 
@@ -567,8 +568,23 @@ SQL
                                   name_prefix  => 'ic_',
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
+
+      # link previous items with orderitems
+      foreach (qw(orderitems invoice)) {
+        if (!$form->{saveasnew} && $form->{"converted_from_${_}_id_$i"}) {
+          RecordLinks->create_links('dbh'        => $dbh,
+                                    'mode'       => 'ids',
+                                    'from_table' => $_,
+                                    'from_ids'   => $form->{"converted_from_${_}_id_$i"},
+                                    'to_table'   => 'orderitems',
+                                    'to_id'      => $orderitems_id,
+          );
+        }
+        delete $form->{"converted_from_${_}_id_$i"};
+      }
     }
   }
+
   # search for orphaned ids
   $query  = sprintf 'SELECT id FROM orderitems WHERE trans_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_orderitems;
   @values = (conv_i($form->{id}), map { conv_i($_) } @processed_orderitems);
@@ -649,23 +665,28 @@ SQL
   $form->save_status($dbh);
 
   # Link this record to the records it was created from.
+  # check every record type we may link. i am not happy with converting the string to array back
+  # should be a array from the start (OE.pm -> retrieve).
+  #  and that i need the local array ref for close_quotation_rfqs. better ideas welcome
   $form->{convert_from_oe_ids} =~ s/^\s+//;
   $form->{convert_from_oe_ids} =~ s/\s+$//;
   my @convert_from_oe_ids      =  split m/\s+/, $form->{convert_from_oe_ids};
   delete $form->{convert_from_oe_ids};
-
-  if (scalar @convert_from_oe_ids) {
-    RecordLinks->create_links('dbh'        => $dbh,
-                              'mode'       => 'ids',
-                              'from_table' => 'oe',
-                              'from_ids'   => \@convert_from_oe_ids,
-                              'to_table'   => 'oe',
-                              'to_id'      => $form->{id},
-      );
-
+  @{ $form->{convert_from_oe_ids} }      =  @convert_from_oe_ids;
+  foreach (qw(ar oe)) {
+    if ($form->{"convert_from_${_}_ids"}) {
+      RecordLinks->create_links('dbh'        => $dbh,
+                                'mode'       => 'ids',
+                                'from_table' => $_,
+                                'from_ids'   => $form->{"convert_from_${_}_ids"},
+                                'to_table'   => 'oe',
+                                'to_id'      => $form->{id},
+        );
+      delete $form->{"convert_from_${_}_ids"};
+    }
     $self->_close_quotations_rfqs('dbh'     => $dbh,
                                   'from_id' => \@convert_from_oe_ids,
-                                  'to_id'   => $form->{id});
+                                  'to_id'   => $form->{id}) if $_ eq 'oe';
   }
 
   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
@@ -821,14 +842,14 @@ sub retrieve {
   my $is_collective_order = scalar @ids;
 
   if (!$form->{id}) {
-    my $wday         = (localtime(time))[6];
-    my $next_workday = $wday == 5 ? 3 : $wday == 6 ? 2 : 1;
+    my $extra_days   = $form->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval : 1;
+    my $next_workday = DateTime->today_local->add(days => $extra_days);
+    my $day_of_week  = $next_workday->day_of_week;
 
-    # if we have a client configured interval for sales quotation, we add this
-    $next_workday   += $::instance_conf->get_reqdate_interval if ($::instance_conf->get_reqdate_interval &&
-                                                                    $form->{type} eq 'sales_quotation' );
+    $next_workday->add(days => (8 - $day_of_week)) if $day_of_week >= 6;
 
-    $query_add       = qq|, current_date AS transdate, date(current_date + interval '${next_workday} days') AS reqdate|;
+    $form->{transdate} = DateTime->today_local->to_kivitendo;
+    $form->{reqdate}   = $next_workday->to_kivitendo;
   }
 
   # get default accounts
@@ -1383,6 +1404,12 @@ sub order_details {
         $sth->finish;
       }
 
+      CVar->get_non_editable_ic_cvars(form               => $form,
+                                      dbh                => $dbh,
+                                      row                => $i, 
+                                      sub_module         => 'orderitems',
+                                      may_converted_from => ['orderitems', 'invoice']);
+
       push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
         CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
           for @{ $ic_cvar_configs };