Nicht-editierbare CVars auch im Workflow richtig setzen. ...
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Tue, 3 Feb 2015 11:29:01 +0000 (12:29 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 4 Feb 2015 17:41:28 +0000 (18:41 +0100)
Zudem das Ermitteln der Werte in eine Subroutine ausgelagert.

SL/CVar.pm
SL/DO.pm
SL/IR.pm
SL/IS.pm
SL/OE.pm

index 6da9b36..0ace069 100644 (file)
@@ -652,6 +652,56 @@ sub format_to_template {
   return $value;
 }
 
+sub get_non_editable_ic_cvars {
+  $main::lxdebug->enter_sub(2);
+  my $self     = shift;
+  my %params   = @_;
+
+  Common::check_params(\%params, qw(form dbh row sub_module may_converted_from));
+  my $form               = $params{form};
+  my $dbh                = $params{dbh};
+  my $row                = $params{row};
+  my $sub_module         = $params{sub_module};
+  my $may_converted_from = $params{may_converted_from};
+
+  my $cvars;
+  if (! $form->{"${sub_module}_id_${row}"}) {
+    my $conv_from = 0;
+    foreach (@{ $may_converted_from }) {
+      if ($form->{"converted_from_${_}_id_$row"}) {
+        $cvars = CVar->get_custom_variables(dbh        => $dbh,
+                                            module     => 'IC',
+                                            sub_module => $_,
+                                            trans_id   => $form->{"converted_from_${_}_id_$row"},
+                                           );
+        $conv_from = 1;
+        last;
+      }
+    }
+    # get values for CVars from master data for new items
+    if (!$conv_from) {
+      $cvars = CVar->get_custom_variables(dbh      => $dbh,
+                                          module   => 'IC',
+                                          trans_id => $form->{"id_$row"},
+                                         );
+    }
+  } else {
+    # get values for CVars from custom_variables for existing items
+    $cvars = CVar->get_custom_variables(dbh        => $dbh,
+                                        module     => 'IC',
+                                        sub_module => $sub_module,
+                                        trans_id   => $form->{"${sub_module}_id_${row}"},
+                                       );
+  }
+  # map only non-editable CVars to form
+  foreach (@{ $cvars }) {
+    next if $_->{flag_editable};
+    $form->{"ic_cvar_$_->{name}_$row"} = $_->{value}
+  }
+
+  $main::lxdebug->leave_sub(2);
+}
+
 1;
 
 __END__
index 9fdd6b7..d537c92 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -296,9 +296,14 @@ SQL
   for my $i (1 .. $form->{rowcount}) {
     next if (!$form->{"id_$i"});
 
+    CVar->get_non_editable_ic_cvars(form               => $form,
+                                    dbh                => $dbh,
+                                    row                => $i, 
+                                    sub_module         => 'delivery_order_items',
+                                    may_converted_from => ['orderitems']);
+
     my $position = $i;
 
-    my $cvars;
     if (!$form->{"delivery_order_items_id_$i"}) {
       # there is no persistent id, therefore create one with all necessary constraints
       my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
@@ -309,22 +314,7 @@ SQL
       do_query($form, $dbh, $query, conv_i($form->{"delivery_order_items_id_$i"}),
                 conv_i($form->{"id"}), conv_i($position), conv_i($form->{"id_$i"}));
       $h_item_id->finish();
-
-      # get values for CVars from master data for new items
-      $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                          module   => 'IC',
-                                          trans_id => $form->{"id_$i"},
-                                         );
-    } else {
-      # get values for CVars from custom_variables for existing items
-      $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                          module     => 'IC',
-                                          sub_module => 'delivery_order_items',
-                                          trans_id   => $form->{"delivery_order_items_id_$i"},
-                                         );
     }
-    # map only non-editable CVars to form (editable ones are already there)
-    map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
 
     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
@@ -1015,23 +1005,11 @@ sub order_details {
       }
     }
 
-    my $cvars;
-    if (! $form->{"delivery_order_items_id_$i"}) {
-      # get values for CVars from master data for new items
-      $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                          module   => 'IC',
-                                          trans_id => $form->{"id_$i"},
-                                         );
-    } else {
-      # get values for CVars from custom_variables for existing items
-      $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                          module     => 'IC',
-                                          sub_module => 'delivery_order_items',
-                                          trans_id   => $form->{"delivery_order_items_id_$i"},
-                                         );
-    }
-    # map only non-editable CVars to form (editable ones are already there)
-    map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
+    CVar->get_non_editable_ic_cvars(form               => $form,
+                                    dbh                => $dbh,
+                                    row                => $i, 
+                                    sub_module         => 'delivery_order_items',
+                                    may_converted_from => ['orderitems']);
 
     push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
       CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
@@ -1289,5 +1267,4 @@ sub is_marked_as_delivered {
   return $delivered ? 1 : 0;
 }
 
-
 1;
index dd04248..33f3ec4 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -369,7 +369,12 @@ sub post_invoice {
 
     next if $payments_only;
 
-    my $cvars;
+    CVar->get_non_editable_ic_cvars(form               => $form,
+                                    dbh                => $dbh,
+                                    row                => $i, 
+                                    sub_module         => 'invoice',
+                                    may_converted_from => ['delivery_order_items', 'orderitems']);
+
     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')|;
@@ -380,22 +385,7 @@ sub post_invoice {
       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();
-
-      # get values for CVars from master data for new items
-      $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                          module   => 'IC',
-                                          trans_id => $form->{"id_$i"},
-                                         );
-    } else {
-      # get values for CVars from custom_variables for existing items
-      $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                          module     => 'IC',
-                                          sub_module => 'invoice',
-                                          trans_id   => $form->{"invoice_id_$i"},
-                                         );
     }
-    # map only non-editable CVars to form (editable ones are already there)
-    map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
 
       # save detail record in invoice table
       $query = <<SQL;
@@ -1656,5 +1646,4 @@ sub get_duedate {
   return $duedate;
 }
 
-
 1;
index a647653..7522bd2 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -373,23 +373,11 @@ sub invoice_details {
         $sth->finish;
       }
 
-      my $cvars;
-      if (! $form->{"invoice_id_$i"}) {
-        # get values for CVars from master data for new items
-        $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                            module   => 'IC',
-                                            trans_id => $form->{"id_$i"},
-                                           );
-      } else {
-        # get values for CVars from custom_variables for existing items
-        $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                            module     => 'IC',
-                                            sub_module => 'invoice',
-                                            trans_id   => $form->{"invoice_id_$i"},
-                                           );
-      }
-      # map only non-editable CVars to form (editable ones are already there)
-      map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
+      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"}, $_), $_)
@@ -767,7 +755,12 @@ sub post_invoice {
       $pricegroup_id *= 1;
       $pricegroup_id  = undef if !$pricegroup_id;
 
-      my $cvars;
+      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')|;
@@ -778,22 +771,7 @@ sub post_invoice {
         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();
-
-        # get values for CVars from master data for new items
-        $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                            module   => 'IC',
-                                            trans_id => $form->{"id_$i"},
-                                           );
-      } else {
-        # get values for CVars from custom_variables for existing items
-        $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                            module     => 'IC',
-                                            sub_module => 'invoice',
-                                            trans_id   => $form->{"invoice_id_$i"},
-                                           );
       }
-      # map only non-editable CVars to form (editable ones are already there)
-      map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
 
       # save detail record in invoice table
       $query = <<SQL;
index ab4e877..df43e2a 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -509,32 +509,22 @@ 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
-      my $cvars;
       if (! $form->{"orderitems_id_$i"}) {
         $query = qq|SELECT nextval('orderitemsid')|;
         ($form->{"orderitems_id_$i"}) = selectrow_query($form, $dbh, $query);
 
         $query = qq|INSERT INTO orderitems (id, position) VALUES (?, ?)|;
         do_query($form, $dbh, $query, $form->{"orderitems_id_$i"}, conv_i($position));
-
-        # get values for CVars from master data for new items
-        $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                            module   => 'IC',
-                                            trans_id => $form->{"id_$i"},
-                                           );
-      } else {
-        # get values for CVars from custom_variables for existing items
-        $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                            module     => 'IC',
-                                            sub_module => 'orderitems',
-                                            trans_id   => $form->{"orderitems_id_$i"},
-                                           );
       }
-      # map only non-editable CVars to form (editable ones are already there)
-      map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
 
       my $orderitems_id = $form->{"orderitems_id_$i"};
       push @processed_orderitems, $orderitems_id;
@@ -1412,23 +1402,11 @@ sub order_details {
         $sth->finish;
       }
 
-      my $cvars;
-      if (! $form->{"orderitems_id_$i"}) {
-        # get values for CVars from master data for new items
-        $cvars = CVar->get_custom_variables(dbh      => $dbh,
-                                            module   => 'IC',
-                                            trans_id => $form->{"id_$i"},
-                                           );
-      } else {
-        # get values for CVars from custom_variables for existing items
-        $cvars = CVar->get_custom_variables(dbh        => $dbh,
-                                            module     => 'IC',
-                                            sub_module => 'orderitems',
-                                            trans_id   => $form->{"orderitems_id_$i"},
-                                           );
-      }
-      # map only non-editable CVars to form (editable ones are already there)
-      map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
+      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"}, $_), $_)