]> wagnertech.de Git - mfinanz.git/blobdiff - SL/OE.pm
Ticket 29 Verknüpfte Belege -> keine Verknüpfung von Rechnung nach Auftrag
[mfinanz.git] / SL / OE.pm
index 2985e89d02334cb104e295672927de910676f70a..55e488346266924c4fa3610ca249d56543d1edc6 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -512,13 +512,30 @@ sub save {
       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;
 
@@ -561,6 +578,19 @@ SQL
                                   name_prefix  => 'ic_',
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
+      # link previous items with orderitems
+      foreach (qw(orderitems invoice)) {
+        if ($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
@@ -643,23 +673,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) {
@@ -1377,6 +1412,24 @@ 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 };
+
       push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
         CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
           for @{ $ic_cvar_configs };