employee_company und salesman_company für flattentoform
[kivitendo-erp.git] / SL / DB / Helper / FlattenToForm.pm
index 4d7f54c..3627cfa 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) = @_;
@@ -20,8 +20,10 @@ sub flatten_to_form {
 
   if ( $vc eq 'customer' ) {
     $form->{customer_id} = $self->customer_id;
+    $form->{customer}    = $self->customer->name if $self->customer;
   } else {
-    $form->{vendor_id} = $self->vendor_id;
+    $form->{vendor_id}   = $self->vendor_id;
+    $form->{vendor}      = $self->vendor->name if $self->vendor;
   };
 
   if (_has($self, 'transdate')) {
@@ -31,11 +33,11 @@ sub flatten_to_form {
     $form->{$transdate_idx} = $self->transdate->to_lxoffice;
   }
 
-  $form->{vc} = $vc if ref($self) =~ m{^SL::DB::(?:.*Invoice|Order)};
+  $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 gln homepage iban language name
-                               payment_terms phone street taxnumber ustid zipcode),
+                               phone street taxnumber ustid zipcode),
                             "${vc}number",
                             ($vc eq 'customer')? 'c_vendor_id': 'v_customer_id');
   my @vc_prefixed_fields = qw(email fax notes number phone);
@@ -50,6 +52,13 @@ sub flatten_to_form {
   _copy($self->salesman,                $form, 'salesman_',     '', 0, map { $_->name } SL::DB::Employee->meta->columns)                   if _has($self, 'salesman_id');
   _copy($self->acceptance_confirmed_by, $form, 'acceptance_confirmed_by_', '', 0, map { $_->name } SL::DB::Employee->meta->columns)        if _has($self, 'acceptance_confirmed_by_id');
 
+  if (_has($self, 'employee_id')) {
+    my $user = User->new(login => $self->employee->login);
+    $form->{"employee_$_"} = $user->{$_} for qw(tel email fax);
+  }
+  # company is employee and login independent
+  $form->{"${_}_company"}  = $::instance_conf->get_company for qw (employee salesman);
+
   $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');
@@ -61,6 +70,8 @@ sub flatten_to_form {
                  : 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;
@@ -73,7 +84,8 @@ sub flatten_to_form {
 
     $form->{"partsgroup_${idx}"} = $item->part->partsgroup->partsgroup if _has($item->part, 'partsgroup_id');
     _copy($item,          $form, "${items_name}_", "_${idx}", 0,               qw(id)) if $items_name;
-    _copy($item->part,    $form, '',               "_${idx}", 0,               qw(id partnumber weight));
+    # TODO: is part_type correct here? Do we need to set part_type as default?
+    _copy($item->part,    $form, '',               "_${idx}", 0,               qw(id partnumber weight part_type));
     _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
@@ -83,7 +95,7 @@ sub flatten_to_form {
     _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_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;
@@ -92,7 +104,7 @@ sub flatten_to_form {
     }
   }
 
-  _copy_custom_variables($self, $form, 'vc_cvar_', '');
+  _copy_custom_variables($self, $form, 'vc_cvar_', '', $cvar_validity{vc});
 
   return $self;
 }
@@ -117,13 +129,15 @@ sub _copy {
 }
 
 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;
@@ -134,4 +148,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;