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 tax_point delivery_term_id billing_address_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 greeting homepage iban language name
40 natural_person phone street taxnumber ustid zipcode),
42 ($vc eq 'customer')? qw(c_vendor_id c_vendor_routing_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 exchangerate));
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->globalproject, $form, 'globalproject', '', 0, qw(number description)) if _has($self, 'globalproject_id');
50 _copy($self->employee, $form, 'employee_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'employee_id');
51 _copy($self->salesman, $form, 'salesman_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'salesman_id');
52 _copy($self->acceptance_confirmed_by, $form, 'acceptance_confirmed_by_', '', 0, map { $_->name } SL::DB::Employee->meta->columns) if _has($self, 'acceptance_confirmed_by_id');
54 # Copy selected shipto to form, if set. Else, copy custom shipto, if set.
55 my $shipto = _has($self, 'shipto_id') ? $self->shipto
56 : _has($self, 'custom_shipto') ? $self->custom_shipto
59 _copy($shipto, $form, '', '', 0, grep { m{^shipto(?!_id$)} } map { $_->name } SL::DB::Shipto->meta->columns);
60 _copy_custom_variables($shipto, $form, 'shiptocvar_', '');
63 _handle_user_data($self, $form);
65 # company is employee and login independent
66 $form->{"${_}_company"} = $::instance_conf->get_company for qw (employee salesman);
68 $form->{employee} = $self->employee->name if _has($self, 'employee_id');
69 $form->{language} = $self->language->template_code if _has($self, 'language_id');
70 $form->{department} = $self->department->description if _has($self, 'department_id');
71 $form->{business} = $self->$vc->business->description if _has($self->$vc, 'business_id');
72 $form->{rowcount} = scalar(@{ $self->items });
74 my $items_name = ref($self) eq 'SL::DB::Order' ? 'orderitems'
75 : ref($self) eq 'SL::DB::DeliveryOrder' ? 'delivery_order_items'
76 : ref($self) eq 'SL::DB::Invoice' ? 'invoice'
79 my %cvar_validity = _determine_cvar_validity($self, $vc);
82 my $format_amounts = $params{format_amounts} ? 1 : 0;
83 my $format_notnull = $params{format_amounts} ? 2 : 0;
84 my $format_percent = $params{format_amounts} ? 3 : 0;
85 my $format_noround = $params{format_amounts} ? 4 : 0;
86 foreach my $item (@{ $self->items_sorted }) {
87 next if _has($item, 'assemblyitem');
91 $form->{"std_warehouse_${idx}"} = $item->part->warehouse->description if _has($item->part, 'warehouse_id');
92 $form->{"std_bin_${idx}"} = $item->part->bin->description if _has($item->part, 'bin_id');
93 $form->{"partsgroup_${idx}"} = $item->part->partsgroup->partsgroup if _has($item->part, 'partsgroup_id');
94 _copy($item, $form, "${items_name}_", "_${idx}", 0, qw(id)) if $items_name;
95 # TODO: is part_type correct here? Do we need to set part_type as default?
96 _copy($item->part, $form, '', "_${idx}", 0, qw(id partnumber weight part_type));
97 _copy($item->part, $form, '', "_${idx}", 0, qw(listprice));
98 _copy($item, $form, '', "_${idx}", 0, qw(description project_id ship serialnumber pricegroup_id ordnumber donumber cusordnumber unit
99 subtotal longdescription price_factor_id marge_price_factor reqdate transdate
100 active_price_source active_discount_source optional));
101 _copy($item, $form, '', "_${idx}", $format_noround, qw(qty sellprice fxsellprice));
102 _copy($item, $form, '', "_${idx}", $format_amounts, qw(marge_total marge_percent lastcost));
103 _copy($item, $form, '', "_${idx}", $format_percent, qw(discount));
104 _copy($item->project, $form, 'project', "_${idx}", 0, qw(number description)) if _has($item, 'project_id');
106 _copy_custom_variables($item, $form, 'ic_cvar_', "_${idx}", $cvar_validity{items}->{ $item->parts_id });
108 if (ref($self) eq 'SL::DB::Invoice') {
109 my $date = $item->deliverydate ? $item->deliverydate->to_lxoffice : undef;
110 $form->{"deliverydate_oe_${idx}"} = $date;
111 $form->{"reqdate_${idx}"} = $date;
115 _copy_custom_variables($self, $form, 'vc_cvar_', '', $cvar_validity{vc});
116 _copy_custom_variables($self->contact, $form, 'cp_cvar_', '') if $self->contact;
122 my ($obj, $column) = @_;
123 return $obj->can($column) && $obj->$column;
127 my ($src, $form, $prefix, $postfix, $format_amounts, @columns) = @_;
129 @columns = grep { $src->can($_) } @columns;
131 map { $form->{"${prefix}${_}${postfix}"} = ref($src->$_) eq 'DateTime' ? $src->$_->to_lxoffice : $src->$_ } @columns if !$format_amounts;
132 map { $form->{"${prefix}${_}${postfix}"} = $::form->format_amount(\%::myconfig, $src->$_ * 1, 2) } @columns if $format_amounts == 1;
133 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1, 2) : 0 } @columns if $format_amounts == 2;
134 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 100, 2) : 0 } @columns if $format_amounts == 3;
135 map { $form->{"${prefix}${_}${postfix}"} = $src->$_ * 1 ? $::form->format_amount(\%::myconfig, $src->$_ * 1, -2) : 0 } @columns if $format_amounts == 4;
140 sub _copy_custom_variables {
141 my ($src, $form, $prefix, $postfix, $cvar_validity) = @_;
143 my $obj = (any { ref($src) eq $_ } qw(SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::InvoiceItem SL::DB::Contact SL::DB::Shipto))
145 : $src->customervendor;
147 foreach my $cvar (@{ $obj->cvars_by_config }) {
148 next if $cvar_validity && !$cvar_validity->{ $cvar->config_id };
150 my $value = ($cvar->config->type =~ m{^(?:bool|customer|vendor|part)$})
152 : $cvar->value_as_text;
154 $form->{ $prefix . $cvar->config->name . $postfix } = $value;
160 sub _determine_cvar_validity {
161 my ($self, $vc) = @_;
163 my @part_ids = uniq map { $_->parts_id } @{ $self->items };
164 my @parts = map { SL::DB::Part->new(id => $_)->load } @part_ids;
166 my %item_cvar_validity;
167 foreach my $part (@parts) {
168 $item_cvar_validity{ $part->id } = { map { ($_->config_id => $_->is_valid) } @{ $part->cvars_by_config } };
171 my %vc_cvar_validity = map { ($_->config_id => $_->is_valid) } @{ $self->$vc->cvars_by_config };
174 items => \%item_cvar_validity,
175 vc => \%vc_cvar_validity,
179 sub _handle_user_data {
180 my ($self, $form) = @_;
182 foreach my $type (qw(employee salesman)) {
183 next if !_has($self, "${type}_id");
185 my $user = User->new(login => $self->$type->login);
186 $form->{"${type}_$_"} = $user->{$_} for qw(tel email fax signature);
188 if ($self->$type->deleted) {
189 for my $key (grep { $_ =~ m{^deleted_} } SL::DB::Employee->meta->columns) {
190 $key =~ s{^deleted_}{};
191 $form->{"${type}_${key}"} = $form->{"${type}_deleted_${key}"}