FlattenToForm: item_ids berücksichtigen.
[kivitendo-erp.git] / SL / DB / Helper / FlattenToForm.pm
index 9cf2d11..8beccf2 100644 (file)
@@ -7,8 +7,6 @@ our @EXPORT = qw(flatten_to_form);
 
 use List::MoreUtils qw(any);
 
-use SL::CVar;
-
 sub flatten_to_form {
   my ($self, $form, %params) = @_;
 
@@ -27,7 +25,7 @@ sub flatten_to_form {
     $form->{$transdate_idx} = $self->transdate->to_lxoffice;
   }
 
-  $form->{vc} = $vc if ref($self) =~ /^SL::DB::.*Invoice/;
+  $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
@@ -51,21 +49,30 @@ sub flatten_to_form {
   $form->{department} = $self->department->description if _has($self, 'department_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 $idx = 0;
   my $format_amounts = $params{format_amounts} ? 1 : 0;
   my $format_notnull = $params{format_amounts} ? 2 : 0;
   my $format_percent = $params{format_amounts} ? 3 : 0;
+  my $format_noround = $params{format_amounts} ? 4 : 0;
   foreach my $item (@{ $self->items_sorted }) {
     next if _has($item, 'assemblyitem');
 
     $idx++;
 
     $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));
     _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_amounts, qw(qty sellprice marge_total marge_percent lastcost));
+                                                                           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');
 
@@ -94,29 +101,28 @@ 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,  -2) : 0 } @columns if  $format_amounts == 4;
 
   return $src;
 }
 
 sub _copy_custom_variables {
-  my ($src, $form, $prefix, $postfix) = @_;
+  my ($src, $form, $prefix, $postfix, $format_amounts) = @_;
 
-  my ($module, $sub_module, $trans_id) = ref($src) eq 'SL::DB::OrderItem'         ? ('IC', 'orderitems',           $src->id)
-                                       : ref($src) eq 'SL::DB::DeliveryOrderItem' ? ('IC', 'delivery_order_items', $src->id)
-                                       : ref($src) eq 'SL::DB::InvoiceItem'       ? ('IC', 'invoice',              $src->id)
-                                       :                                            ('CT', undef,                  _has($src, 'customer_id') ? $src->customer_id : $src->vendor_id);
+  my $obj = (any { ref($src) eq $_ } qw(SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::InvoiceItem))
+          ? $src
+          : $src->customervendor;
 
-  return unless $trans_id;
+  foreach my $cvar (@{ $obj->cvars_by_config }) {
+    my $value = ($cvar->config->type =~ m{^(?:bool|customer|vendor|part)$})
+              ? $cvar->value
+              : $cvar->value_as_text;
 
-  my $cvars = CVar->get_custom_variables(dbh        => $src->db->dbh,
-                                         module     => $module,
-                                         sub_module => $sub_module,
-                                         trans_id   => $trans_id,
-                                        );
-  map { $form->{ $prefix . $_->{name} . $postfix } = $_->{value} } @{ $cvars };
+    $form->{ $prefix . $cvar->config->name . $postfix } = $value;
+  }
 
   return $src;
 }