1 package SL::DB::Helper::FlattenToForm;
5 use parent qw(Exporter);
6 our @EXPORT = qw(flatten_to_form);
8 use List::MoreUtils qw(uniq any);
11 my ($self, $form, %params) = @_;
13 my $vc = $self->can('customer_id') && $self->customer_id ? 'customer' : 'vendor';
15 _copy($self, $form, '', '', 0, qw(id type taxzone_id ordnumber quonumber invnumber donumber cusordnumber taxincluded shippingpoint shipvia notes intnotes cp_id
16 employee_id salesman_id closed department_id language_id payment_id delivery_customer_id delivery_vendor_id shipto_id proforma
17 globalproject_id delivered transaction_description container_type accepted_by_customer invoice storno storno_id dunning_config_id
18 orddate quodate reqdate gldate duedate deliverydate datepaid transdate delivery_term_id));
19 $form->{currency} = $form->{curr} = $self->currency_id ? $self->currency->name || '' : '';
21 if ( $vc eq 'customer' ) {
22 $form->{customer_id} = $self->customer_id;
23 $form->{customer} = $self->customer->name if $self->customer;
25 $form->{vendor_id} = $self->vendor_id;
26 $form->{vendor} = $self->vendor->name if $self->vendor;
29 if (_has($self, 'transdate')) {
30 my $transdate_idx = ref($self) eq 'SL::DB::Order' ? ($self->quotation ? 'quodate' : 'orddate')
31 : ref($self) eq 'SL::DB::Invoice' ? 'invdate'
33 $form->{$transdate_idx} = $self->transdate->to_lxoffice;
36 $form->{vc} = $vc if ref($self) =~ m{^SL::DB::(?:.*Invoice|.*Order)};
38 my @vc_fields = (qw(account_number bank bank_code bic business city contact country creditlimit
39 department_1 department_2 discount email fax gln homepage iban language name
40 phone street taxnumber ustid zipcode),
42 ($vc eq 'customer')? 'c_vendor_id': 'v_customer_id');
43 my @vc_prefixed_fields = qw(email fax notes number phone);
45 _copy($self, $form, '', '', 1, qw(amount netamount marge_total marge_percent container_remaining_weight container_remaining_volume paid));
46 _copy($self->$vc, $form, '', '', 0, @vc_fields);
47 _copy($self->$vc, $form, $vc, '', 0, @vc_prefixed_fields);
48 _copy($self->contact, $form, '', '', 0, grep { /^cp_/ } map { $_->name } SL::DB::Contact->meta->columns) if _has($self, 'cp_id');
49 _copy($self->shipto, $form, '', '', 0, grep { /^shipto/ } map { $_->name } SL::DB::Shipto->meta->columns) if _has($self, 'shipto_id');
50 _copy($self->globalproject, $form, 'globalproject', '', 0, qw(number description)) if _has($self, 'globalproject_id');
51 _copy($self->employee, $form, 'employee_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'employee_id');
52 _copy($self->salesman, $form, 'salesman_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'salesman_id');
53 _copy($self->acceptance_confirmed_by, $form, 'acceptance_confirmed_by_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'acceptance_confirmed_by_id');
55 if (_has($self, 'employee_id')) {
56 my $user = User->new(login => $self->employee->login);
57 $form->{"employee_$_"} = $user->{$_} for qw(tel email fax);
59 # company is employee and login independent
60 $form->{"${_}_company"} = $::instance_conf->get_company for qw (employee salesman);
62 $form->{employee} = $self->employee->name if _has($self, 'employee_id');
63 $form->{language} = $self->language->template_code if _has($self, 'language_id');
64 $form->{department} = $self->department->description if _has($self, 'department_id');
65 $form->{business} = $self->$vc->business->description if _has($self->$vc, 'business_id');
66 $form->{rowcount} = scalar(@{ $self->items });
68 my $items_name = ref($self) eq 'SL::DB::Order' ? 'orderitems'
69 : ref($self) eq 'SL::DB::DeliveryOrder' ? 'delivery_order_items'
70 : ref($self) eq 'SL::DB::Invoice' ? 'invoice'
73 my %cvar_validity = _determine_cvar_validity($self, $vc);
76 my $format_amounts = $params{format_amounts} ? 1 : 0;
77 my $format_notnull = $params{format_amounts} ? 2 : 0;
78 my $format_percent = $params{format_amounts} ? 3 : 0;
79 my $format_noround = $params{format_amounts} ? 4 : 0;
80 foreach my $item (@{ $self->items_sorted }) {
81 next if _has($item, 'assemblyitem');
85 $form->{"std_warehouse_${idx}"} = $item->part->warehouse->description if _has($item->part, 'warehouse_id');
86 $form->{"std_bin_${idx}"} = $item->part->bin->description if _has($item->part, 'bin_id');
87 $form->{"partsgroup_${idx}"} = $item->part->partsgroup->partsgroup if _has($item->part, 'partsgroup_id');
88 _copy($item, $form, "${items_name}_", "_${idx}", 0, qw(id)) if $items_name;
89 # TODO: is part_type correct here? Do we need to set part_type as default?
90 _copy($item->part, $form, '', "_${idx}", 0, qw(id partnumber weight part_type));
91 _copy($item->part, $form, '', "_${idx}", 0, qw(listprice));
92 _copy($item, $form, '', "_${idx}", 0, qw(description project_id ship serialnumber pricegroup_id ordnumber donumber cusordnumber unit
93 subtotal longdescription price_factor_id marge_price_factor reqdate transdate
94 active_price_source active_discount_source));
95 _copy($item, $form, '', "_${idx}", $format_noround, qw(qty sellprice));
96 _copy($item, $form, '', "_${idx}", $format_amounts, qw(marge_total marge_percent lastcost));
97 _copy($item, $form, '', "_${idx}", $format_percent, qw(discount));
98 _copy($item->project, $form, 'project', "_${idx}", 0, qw(number description)) if _has($item, 'project_id');
100 _copy_custom_variables($item, $form, 'ic_cvar_', "_${idx}", $cvar_validity{items}->{ $item->parts_id });
102 if (ref($self) eq 'SL::DB::Invoice') {
103 my $date = $item->deliverydate ? $item->deliverydate->to_lxoffice : undef;
104 $form->{"deliverydate_oe_${idx}"} = $date;
105 $form->{"reqdate_${idx}"} = $date;
109 _copy_custom_variables($self, $form, 'vc_cvar_', '', $cvar_validity{vc});
110 _copy_custom_variables($self->contact, $form, 'cp_cvar_', '') if $self->contact;
116 my ($obj, $column) = @_;
117 return $obj->can($column) && $obj->$column;
121 my ($src, $form, $prefix, $postfix, $format_amounts, @columns) = @_;
123 @columns = grep { $src->can($_) } @columns;
125 map { $form->{"${prefix}${_}${postfix}"} = ref($src->$_) eq 'DateTime' ? $src->$_->to_lxoffice : $src->$_ } @columns if !$format_amounts;
126 map { $form->{"${prefix}${_}${postfix}"} = $::form->format_amount(\%::myconfig, $src->$_ * 1, 2) } @columns if $format_amounts == 1;
127 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1, 2) : 0 } @columns if $format_amounts == 2;
128 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 100, 2) : 0 } @columns if $format_amounts == 3;
129 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1, -2) : 0 } @columns if $format_amounts == 4;
134 sub _copy_custom_variables {
135 my ($src, $form, $prefix, $postfix, $cvar_validity) = @_;
137 my $obj = (any { ref($src) eq $_ } qw(SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::InvoiceItem SL::DB::Contact))
139 : $src->customervendor;
141 foreach my $cvar (@{ $obj->cvars_by_config }) {
142 next if $cvar_validity && !$cvar_validity->{ $cvar->config_id };
144 my $value = ($cvar->config->type =~ m{^(?:bool|customer|vendor|part)$})
146 : $cvar->value_as_text;
148 $form->{ $prefix . $cvar->config->name . $postfix } = $value;
154 sub _determine_cvar_validity {
155 my ($self, $vc) = @_;
157 my @part_ids = uniq map { $_->parts_id } @{ $self->items };
158 my @parts = map { SL::DB::Part->new(id => $_)->load } @part_ids;
160 my %item_cvar_validity;
161 foreach my $part (@parts) {
162 $item_cvar_validity{ $part->id } = { map { ($_->config_id => $_->is_valid) } @{ $part->cvars_by_config } };
165 my %vc_cvar_validity = map { ($_->config_id => $_->is_valid) } @{ $self->$vc->cvars_by_config };
168 items => \%item_cvar_validity,
169 vc => \%vc_cvar_validity,