CVars: Lieferadressen-CVars bei Ein-/Verkaufsbelegen benutzen können
authorMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 5 Apr 2016 14:22:01 +0000 (16:22 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Wed, 6 Apr 2016 14:05:32 +0000 (16:05 +0200)
12 files changed:
SL/Common.pm
SL/DO.pm
SL/Form.pm
SL/IS.pm
SL/OE.pm
bin/mozilla/do.pl
bin/mozilla/io.pl
bin/mozilla/is.pl
bin/mozilla/oe.pl
templates/webpages/do/form_header.html
templates/webpages/io/ship_to.html
templates/webpages/oe/check_for_direct_delivery.html

index bcaeb62..dfdf3c5 100644 (file)
@@ -447,6 +447,13 @@ sub get_shipto_by_id {
 
   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
 
+  my $cvars = CVar->get_custom_variables(
+    dbh      => $dbh,
+    module   => 'ShipTo',
+    trans_id => $shipto_id,
+  );
+  $form->{"${prefix}shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
+
   $dbh->disconnect();
 
   $main::lxdebug->leave_sub();
index b098f71..e3ffc2f 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -263,6 +263,11 @@ sub save {
   if ($form->{id}) {
 
     # only delete shipto complete
+    $query = qq|DELETE FROM custom_variables
+                WHERE (config_id IN (SELECT id        FROM custom_variable_configs WHERE (module = 'ShipTo')))
+                  AND (trans_id  IN (SELECT shipto_id FROM shipto                  WHERE (module = 'DO') AND (trans_id = ?)))|;
+    do_query($form, $dbh, $query, $form->{id});
+
     $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
     do_query($form, $dbh, $query, conv_i($form->{id}));
 
@@ -748,6 +753,15 @@ sub retrieve {
     map { $form->{$_} = $ref->{$_} } keys %$ref;
     $sth->finish();
 
+    if ($form->{shipto_id}) {
+      my $cvars = CVar->get_custom_variables(
+        dbh      => $dbh,
+        module   => 'ShipTo',
+        trans_id => $form->{shipto_id},
+      );
+      $form->{"shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
+    }
+
     # get printed, emailed and queued
     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
     $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
index 98bb4db..266d3e5 100644 (file)
@@ -1848,14 +1848,19 @@ sub get_shipto {
     my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
     my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
     map({ $self->{$_} = $ref->{$_} } keys(%$ref));
+
+    my $cvars = CVar->get_custom_variables(
+      dbh      => $dbh,
+      module   => 'ShipTo',
+      trans_id => $self->{shipto_id},
+    );
+    $self->{"shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
   }
 
   $main::lxdebug->leave_sub();
 }
 
 sub add_shipto {
-  $main::lxdebug->enter_sub();
-
   my ($self, $dbh, $id, $module) = @_;
 
   my $shipto;
@@ -1869,54 +1874,68 @@ sub add_shipto {
     push(@values, $self->{"shipto${item}"});
   }
 
-  if ($shipto) {
-    if ($self->{shipto_id}) {
-      my $query = qq|UPDATE shipto set
-                       shiptoname = ?,
-                       shiptodepartment_1 = ?,
-                       shiptodepartment_2 = ?,
-                       shiptostreet = ?,
-                       shiptozipcode = ?,
-                       shiptocity = ?,
-                       shiptocountry = ?,
-                       shiptogln = ?,
-                       shiptocontact = ?,
-                       shiptocp_gender = ?,
-                       shiptophone = ?,
-                       shiptofax = ?,
-                       shiptoemail = ?
-                     WHERE shipto_id = ?|;
-      do_query($self, $dbh, $query, @values, $self->{shipto_id});
-    } else {
-      my $query = qq|SELECT * FROM shipto
-                     WHERE shiptoname = ? AND
-                       shiptodepartment_1 = ? AND
-                       shiptodepartment_2 = ? AND
-                       shiptostreet = ? AND
-                       shiptozipcode = ? AND
-                       shiptocity = ? AND
-                       shiptocountry = ? AND
-                       shiptogln = ? AND
-                       shiptocontact = ? AND
-                       shiptocp_gender = ? AND
-                       shiptophone = ? AND
-                       shiptofax = ? AND
-                       shiptoemail = ? AND
-                       module = ? AND
-                       trans_id = ?|;
-      my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
-      if(!$insert_check){
-        $query =
-          qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
-                                 shiptostreet, shiptozipcode, shiptocity, shiptocountry, shiptogln,
-                                 shiptocontact, shiptocp_gender, shiptophone, shiptofax, shiptoemail, module)
-             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
-        do_query($self, $dbh, $query, $id, @values, $module);
-      }
+  return if !$shipto;
+
+  my $shipto_id = $self->{shipto_id};
+
+  if ($self->{shipto_id}) {
+    my $query = qq|UPDATE shipto set
+                     shiptoname = ?,
+                     shiptodepartment_1 = ?,
+                     shiptodepartment_2 = ?,
+                     shiptostreet = ?,
+                     shiptozipcode = ?,
+                     shiptocity = ?,
+                     shiptocountry = ?,
+                     shiptogln = ?,
+                     shiptocontact = ?,
+                     shiptocp_gender = ?,
+                     shiptophone = ?,
+                     shiptofax = ?,
+                     shiptoemail = ?
+                   WHERE shipto_id = ?|;
+    do_query($self, $dbh, $query, @values, $self->{shipto_id});
+  } else {
+    my $query = qq|SELECT * FROM shipto
+                   WHERE shiptoname = ? AND
+                     shiptodepartment_1 = ? AND
+                     shiptodepartment_2 = ? AND
+                     shiptostreet = ? AND
+                     shiptozipcode = ? AND
+                     shiptocity = ? AND
+                     shiptocountry = ? AND
+                     shiptogln = ? AND
+                     shiptocontact = ? AND
+                     shiptocp_gender = ? AND
+                     shiptophone = ? AND
+                     shiptofax = ? AND
+                     shiptoemail = ? AND
+                     module = ? AND
+                     trans_id = ?|;
+    my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
+    if(!$insert_check){
+      my $insert_query =
+        qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
+                               shiptostreet, shiptozipcode, shiptocity, shiptocountry, shiptogln,
+                               shiptocontact, shiptocp_gender, shiptophone, shiptofax, shiptoemail, module)
+           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
+      do_query($self, $dbh, $insert_query, $id, @values, $module);
+
+      $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
     }
+
+    $shipto_id = $insert_check->{shipto_id};
   }
 
-  $main::lxdebug->leave_sub();
+  return unless $shipto_id;
+
+  CVar->save_custom_variables(
+    dbh         => $dbh,
+    module      => 'ShipTo',
+    trans_id    => $shipto_id,
+    variables   => $self,
+    name_prefix => 'shipto',
+  );
 }
 
 sub get_employee {
@@ -3375,10 +3394,17 @@ sub prepare_for_printing {
     $self->{"employee_${_}"} = $defaults->$_   for qw(address businessnumber co_ustid company duns sepa_creditor_id taxnumber);
   }
 
-  # Load shipping address from database if shipto_id is set.
-  if ($self->{shipto_id}) {
-    my $shipto  = SL::DB::Shipto->new(shipto_id => $self->{shipto_id})->load;
+  # Load shipping address from database. If shipto_id is set then it's
+  # one from the customer's/vendor's master data. Otherwise look an a
+  # customized address linking back to the current record.
+  my $shipto_module = $self->{type} =~ /_delivery_order$/                                             ? 'DO'
+                    : $self->{type} =~ /sales_order|sales_quotation|request_quotation|purchase_order/ ? 'OE'
+                    :                                                                                   'AR';
+  my $shipto        = $self->{shipto_id} ? SL::DB::Shipto->new(shipto_id => $self->{shipto_id})->load
+                    :                      SL::DB::Manager::Shipto->get_first(where => [ module => $shipto_module, trans_id => $self->{id} ]);
+  if ($shipto) {
     $self->{$_} = $shipto->$_ for grep { m{^shipto} } map { $_->name } @{ $shipto->meta->columns };
+    $self->{"shiptocvar_" . $_->config->name} = $_->value_as_text for @{ $shipto->cvars_by_config };
   }
 
   my $language = $self->{language} ? '_' . $self->{language} : '';
index 7d64130..566d154 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1818,6 +1818,11 @@ sub reverse_invoice {
   # delete acc_trans
   my @values = (conv_i($form->{id}));
   do_query($form, $dbh, qq|DELETE FROM acc_trans WHERE trans_id = ?|, @values);
+
+  $query = qq|DELETE FROM custom_variables
+              WHERE (config_id IN (SELECT id        FROM custom_variable_configs WHERE (module = 'ShipTo')))
+                AND (trans_id  IN (SELECT shipto_id FROM shipto                  WHERE (module = 'AR') AND (trans_id = ?)))|;
+  do_query($form, $dbh, $query, @values);
   do_query($form, $dbh, qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'AR')|, @values);
 
   $main::lxdebug->leave_sub();
@@ -2049,6 +2054,21 @@ sub retrieve_invoice {
     }
     $sth->finish;
 
+    # Fetch shipping address.
+    $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'AR'|;
+    $ref   = selectfirst_hashref_query($form, $dbh, $query, $form->{id});
+
+    $form->{$_} = $ref->{$_} for grep { $_ ne 'id' } keys %$ref;
+
+    if ($form->{shipto_id}) {
+      my $cvars = CVar->get_custom_variables(
+        dbh      => $dbh,
+        module   => 'ShipTo',
+        trans_id => $form->{shipto_id},
+      );
+      $form->{"shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
+    }
+
     Common::webdav_folder($form);
   }
 
index 079b14b..149a6cf 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -421,6 +421,11 @@ sub save {
   $form->{$number_field} ||= $trans_number->create_unique; # set $form->{ordnumber} or $form->{quonumber}
 
   if ($form->{id}) {
+    $query = qq|DELETE FROM custom_variables
+                WHERE (config_id IN (SELECT id        FROM custom_variable_configs WHERE (module = 'ShipTo')))
+                  AND (trans_id  IN (SELECT shipto_id FROM shipto                  WHERE (module = 'OE') AND (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});
@@ -1011,6 +1016,15 @@ sub retrieve {
       map { $form->{$_} = $ref->{$_} } keys %$ref;
       $sth->finish;
 
+      if ($form->{shipto_id}) {
+        my $cvars = CVar->get_custom_variables(
+          dbh      => $dbh,
+          module   => 'ShipTo',
+          trans_id => $form->{shipto_id},
+        );
+        $form->{"shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
+      }
+
       # get printed, emailed and queued
       $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
       $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
index ea96bca..51986b0 100644 (file)
@@ -327,6 +327,11 @@ sub form_header {
 
   $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery kivi.io autocomplete_customer autocomplete_part));
 
+  my @custom_hidden;
+  push @custom_hidden, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) };
+
+  $::form->{HIDDENS} = [ map { +{ name => $_, value => $::form->{$_} } } (@custom_hidden) ];
+
   $form->header();
   # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
   # und Erweiterung für Bug 1760:
index c3ab47e..1dc7a64 100644 (file)
@@ -1648,18 +1648,22 @@ sub ship_to {
   call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country gln contact email phone fax), $::form->{vc} . "number");
   $::form->{rowcount}--;
 
+  my $cvars         = SL::DB::Shipto->new->cvars_by_config;
   my @shipto_vars   = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln
                          shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
                          shiptodepartment_1 shiptodepartment_2);
-  my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
+  my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id), map { "shiptocvar_" . $_->config->name } @{ $cvars } ]);
   $::form->{title}  = $::locale->text('Ship to');
   $::form->header;
 
   my $vc_obj = ($::form->{vc} eq 'customer' ? "SL::DB::Customer" : "SL::DB::Vendor")->new(id => $::form->{$::form->{vc} . "_id"})->load;
 
+  $_->value($::form->{"shiptocvar_" . $_->config->name}) for @{ $cvars };
+
   print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
                                                      nextsub      => $::form->{display_form} || 'display_form',
                                                      vc_obj       => $vc_obj,
+                                                     cvars        => $cvars,
                                                    });
 
   $main::lxdebug->leave_sub();
index 9af6b21..aa9a9a8 100644 (file)
@@ -380,6 +380,8 @@ sub form_header {
   $TMPL_VAR{dateformat}          = $myconfig{dateformat};
   $TMPL_VAR{numberformat}        = $myconfig{numberformat};
 
+  push @custom_hiddens, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) };
+
   # hiddens
   $TMPL_VAR{HIDDENS} = [qw(
     id action type media format queued printed emailed title vc discount
index ed13848..8f68f4d 100644 (file)
@@ -473,6 +473,9 @@ sub form_header {
   if ($form->{CFDD_shipto} && $form->{CFDD_shipto_id} ) {
       $form->{shipto_id} = $form->{CFDD_shipto_id};
   }
+
+  push @custom_hiddens, map { "shiptocvar_" . $_->name } @{ SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'ShipTo' ]) };
+
   $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
      qw(id action type vc formname media format proforma queued printed emailed
         title creditlimit creditremaining tradediscount business
@@ -1731,6 +1734,8 @@ sub check_for_direct_delivery {
     return;
   }
 
+  my $cvars = SL::DB::Shipto->new->cvars_by_config;
+
   if ($form->{shipto_id}) {
     Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
 
@@ -1738,11 +1743,13 @@ sub check_for_direct_delivery {
     map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
   }
 
+  $_->value($::form->{"CFDD_shiptocvar_" . $_->config->name}) for @{ $cvars };
+
   delete $form->{action};
   $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
 
   $form->header();
-  print $form->parse_html_template("oe/check_for_direct_delivery");
+  print $form->parse_html_template("oe/check_for_direct_delivery", { cvars => $cvars });
 
   $main::lxdebug->leave_sub();
 
index 012c3d3..334c94b 100644 (file)
   <input type="hidden" name="type" value="[% HTML.escape(type) %]">
   <input type="hidden" name="vc" value="[% HTML.escape(vc) %]">
   <input type="hidden" name="lastmtime" value="[% HTML.escape(lastmtime) %]">
+[%- FOREACH row = HIDDENS %]
+   [% L.hidden_tag(row.name, row.value) %]
+[%- END %]
 
   <p>
    <table width="100%">
index 2ffed37..e3e6500 100644 (file)
@@ -15,6 +15,9 @@
       shiptophone:        "[% JavaScript.escape(vc_obj.phone) %]",
       shiptofax:          "[% JavaScript.escape(vc_obj.fax) %]",
       shiptoemail:        "[% JavaScript.escape(vc_obj.email) %]"
+[% FOREACH var = cvars %]
+      , "shiptocvar_[% JavaScript.escape(var.config.name) %]": ""
+[% END %]
     }
 
   [% FOREACH shipto = vc_obj.shipto %]
@@ -32,6 +35,9 @@
       shiptophone:        "[% JavaScript.escape(shipto.shiptophone) %]",
       shiptofax:          "[% JavaScript.escape(shipto.shiptofax) %]",
       shiptoemail:        "[% JavaScript.escape(shipto.shiptoemail) %]"
+    [% FOREACH var = shipto.cvars_by_config %]
+      , "shiptocvar_[% JavaScript.escape(var.config.name) %]": "[% JavaScript.escape(var.value_as_text) %]"
+    [% END %]
     }
   [% END %]
   ];
     <td>[% HTML.escape(email) %]</td>
     <td>[% L.input_tag("shiptoemail", shiptoemail, "size", "35") %]</td>
    </tr>
+[% FOREACH var = cvars %]
+   <tr valign="top">
+    <th align="right" nowrap>[% HTML.escape(var.config.description) %]</th>
+    <td></td>
+    <td>[% INCLUDE 'common/render_cvar_input.html' cvar_name_prefix='shiptocvar_' %]</td>
+   </tr>
+[% END %]
   </table>
 
   <hr size="3" noshade>
index 9070cab..8160188 100644 (file)
     <th align="right" valign="top">[% 'E-mail' | $T8 %]:</th>
     <td valign="top">[% HTML.escape(CFDD_shiptoemail) %]</td>
    </tr>
+
+[% FOREACH var = cvars %]
+   <tr valign="top">
+    <th align="right" nowrap>[% HTML.escape(var.config.description) %]</th>
+    <td>[% HTML.escape(var.value_as_text) %]</td>
+   </tr>
+[% END %]
   </table>
  </p>