FlattenToForm: nur für Positionsartikel gültige CVars übernehmen
[kivitendo-erp.git] / SL / DB / Helper / FlattenToForm.pm
index a9c0264..7712c9d 100644 (file)
@@ -5,7 +5,7 @@ use strict;
 use parent qw(Exporter);
 our @EXPORT = qw(flatten_to_form);
 
-use List::MoreUtils qw(any);
+use List::MoreUtils qw(uniq any);
 
 sub flatten_to_form {
   my ($self, $form, %params) = @_;
@@ -18,6 +18,12 @@ sub flatten_to_form {
                                     orddate quodate reqdate gldate duedate deliverydate datepaid transdate delivery_term_id));
   $form->{currency} = $form->{curr} = $self->currency_id ? $self->currency->name || '' : '';
 
+  if ( $vc eq 'customer' ) {
+    $form->{customer_id} = $self->customer_id;
+  } else {
+    $form->{vendor_id} = $self->vendor_id;
+  };
+
   if (_has($self, 'transdate')) {
     my $transdate_idx = ref($self) eq 'SL::DB::Order'   ? ($self->quotation ? 'quodate' : 'orddate')
                       : ref($self) eq 'SL::DB::Invoice' ? 'invdate'
@@ -28,7 +34,7 @@ sub flatten_to_form {
   $form->{vc} = $vc if ref($self) =~ m{^SL::DB::(?:.*Invoice|Order)};
 
   my @vc_fields          = (qw(account_number bank bank_code bic business city contact country creditlimit
-                               department_1 department_2 discount email fax homepage iban language name
+                               department_1 department_2 discount email fax gln homepage iban language name
                                payment_terms phone street taxnumber ustid zipcode),
                             "${vc}number",
                             ($vc eq 'customer')? 'c_vendor_id': 'v_customer_id');
@@ -47,8 +53,16 @@ sub flatten_to_form {
   $form->{employee}   = $self->employee->name          if _has($self, 'employee_id');
   $form->{language}   = $self->language->template_code if _has($self, 'language_id');
   $form->{department} = $self->department->description if _has($self, 'department_id');
+  $form->{business}   = $self->$vc->business->description if _has($self->$vc, 'business_id');
   $form->{rowcount}   = scalar(@{ $self->items });
 
+  my $items_name = ref($self) eq 'SL::DB::Order'         ? 'orderitems'
+                 : ref($self) eq 'SL::DB::DeliveryOrder' ? 'delivery_order_items'
+                 : ref($self) eq 'SL::DB::Invoice'       ? 'invoice'
+                 : '';
+
+  my %cvar_validity = _determine_cvar_validity($self, $vc);
+
   my $idx = 0;
   my $format_amounts = $params{format_amounts} ? 1 : 0;
   my $format_notnull = $params{format_amounts} ? 2 : 0;
@@ -60,16 +74,18 @@ sub flatten_to_form {
     $idx++;
 
     $form->{"partsgroup_${idx}"} = $item->part->partsgroup->partsgroup if _has($item->part, 'partsgroup_id');
-    _copy($item->part,    $form, '',        "_${idx}", 0,               qw(id partnumber weight));
-    _copy($item->part,    $form, '',        "_${idx}", 0,               qw(listprice));
-    _copy($item,          $form, '',        "_${idx}", 0,               qw(description project_id ship serialnumber pricegroup_id ordnumber donumber cusordnumber unit
-                                                                           subtotal longdescription price_factor_id marge_price_factor approved_sellprice reqdate transdate));
-    _copy($item,          $form, '',        "_${idx}", $format_noround, qw(qty sellprice));
-    _copy($item,          $form, '',        "_${idx}", $format_amounts, qw(marge_total marge_percent lastcost));
-    _copy($item,          $form, '',        "_${idx}", $format_percent, qw(discount));
-    _copy($item->project, $form, 'project', "_${idx}", 0,               qw(number description)) if _has($item, 'project_id');
-
-    _copy_custom_variables($item, $form, 'ic_cvar_', "_${idx}");
+    _copy($item,          $form, "${items_name}_", "_${idx}", 0,               qw(id)) if $items_name;
+    _copy($item->part,    $form, '',               "_${idx}", 0,               qw(id partnumber weight));
+    _copy($item->part,    $form, '',               "_${idx}", 0,               qw(listprice));
+    _copy($item,          $form, '',               "_${idx}", 0,               qw(description project_id ship serialnumber pricegroup_id ordnumber donumber cusordnumber unit
+                                                                                  subtotal longdescription price_factor_id marge_price_factor approved_sellprice reqdate transdate
+                                                                                  active_price_source active_discount_source));
+    _copy($item,          $form, '',              "_${idx}", $format_noround, qw(qty sellprice));
+    _copy($item,          $form, '',              "_${idx}", $format_amounts, qw(marge_total marge_percent lastcost));
+    _copy($item,          $form, '',              "_${idx}", $format_percent, qw(discount));
+    _copy($item->project, $form, 'project',       "_${idx}", 0,               qw(number description)) if _has($item, 'project_id');
+
+    _copy_custom_variables($item, $form, 'ic_cvar_', "_${idx}", $cvar_validity{items}->{ $item->parts_id });
 
     if (ref($self) eq 'SL::DB::Invoice') {
       my $date                          = $item->deliverydate ? $item->deliverydate->to_lxoffice : undef;
@@ -78,7 +94,7 @@ sub flatten_to_form {
     }
   }
 
-  _copy_custom_variables($self, $form, 'vc_cvar_', '');
+  _copy_custom_variables($self, $form, 'vc_cvar_', '', $cvar_validity{vc});
 
   return $self;
 }
@@ -94,22 +110,24 @@ sub _copy {
   @columns = grep { $src->can($_) } @columns;
 
   map { $form->{"${prefix}${_}${postfix}"} = ref($src->$_) eq 'DateTime' ? $src->$_->to_lxoffice : $src->$_             } @columns if !$format_amounts;
-  map { $form->{"${prefix}${_}${postfix}"} =                $::form->format_amount(\%::myconfig, $src->$_ * 1, 2)       } @columns if  $format_amounts == 1;
-  map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1, 2) : 0   } @columns if  $format_amounts == 2;
+  map { $form->{"${prefix}${_}${postfix}"} =                $::form->format_amount(\%::myconfig, $src->$_ * 1,   2)     } @columns if  $format_amounts == 1;
+  map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1,   2) : 0 } @columns if  $format_amounts == 2;
   map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 100, 2) : 0 } @columns if  $format_amounts == 3;
-  map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1)    : 0   } @columns if  $format_amounts == 4;
+  map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1,  -2) : 0 } @columns if  $format_amounts == 4;
 
   return $src;
 }
 
 sub _copy_custom_variables {
-  my ($src, $form, $prefix, $postfix, $format_amounts) = @_;
+  my ($src, $form, $prefix, $postfix, $cvar_validity) = @_;
 
   my $obj = (any { ref($src) eq $_ } qw(SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::InvoiceItem))
           ? $src
           : $src->customervendor;
 
   foreach my $cvar (@{ $obj->cvars_by_config }) {
+    next if $cvar_validity && !$cvar_validity->{ $cvar->config_id };
+
     my $value = ($cvar->config->type =~ m{^(?:bool|customer|vendor|part)$})
               ? $cvar->value
               : $cvar->value_as_text;
@@ -120,4 +138,23 @@ sub _copy_custom_variables {
   return $src;
 }
 
+sub _determine_cvar_validity {
+  my ($self, $vc) = @_;
+
+  my @part_ids    = uniq map { $_->parts_id } @{ $self->items };
+  my @parts       = map { SL::DB::Part->new(id => $_)->load } @part_ids;
+
+  my %item_cvar_validity;
+  foreach my $part (@parts) {
+    $item_cvar_validity{ $part->id } = { map { ($_->config_id => $_->is_valid) } @{ $part->cvars_by_config } };
+  }
+
+  my %vc_cvar_validity = map { ($_->config_id => $_->is_valid) } @{ $self->$vc->cvars_by_config };
+
+  return (
+    items => \%item_cvar_validity,
+    vc    => \%vc_cvar_validity,
+  );
+}
+
 1;