- if ($form->{"qty_$i"} != 0) {
-
- # add number, description and qty to $form->{number}, ....
- push(@{ $form->{runningnumber} }, $i);
- push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
- push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
- push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
- push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
- push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
- push(@{ $form->{qty} },
- $form->format_amount($myconfig, $form->{"qty_$i"}));
- push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
- push(@{ $form->{deliverydate} }, qq|$form->{"deliverydate_$i"}|);
-
- push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
- push(@{ $form->{ordnumber_oe} }, qq|$form->{"ordnumber_$i"}|);
- push(@{ $form->{transdate_oe} }, qq|$form->{"transdate_$i"}|);
-
- if ($form->{lizenzen}) {
- if ($form->{"licensenumber_$i"}) {
- $query =
- qq|SELECT l.licensenumber, l.validuntil FROM license l WHERE l.id = $form->{"licensenumber_$i"}|;
- $sth = $dbh->prepare($query);
- $sth->execute || $form->dberror($query);
-
- ($licensenumber, $validuntil) = $sth->fetchrow_array;
- push(@{ $form->{licensenumber} }, $licensenumber);
- push(@{ $form->{validuntil} },
- $locale->date($myconfig, $validuntil, 0));
- $licensenumber = "Lizenz: " . $licensenumber;
- $sth->finish;
- push(@{ $form->{licensenumber} }, $licensenumber);
- } else {
- push(@{ $form->{licensenumber} }, "");
- push(@{ $form->{validuntil} }, "");
+ if ($form->{"id_$i"} != 0) {
+
+ # Prepare linked items for printing
+ if ( $form->{"invoice_id_$i"} ) {
+
+ require SL::DB::InvoiceItem;
+ my $invoice_item = SL::DB::Manager::InvoiceItem->find_by( id => $form->{"invoice_id_$i"} );
+ my $linkeditems = $invoice_item->linked_records( direction => 'from', recursive => 1 );
+
+ # check for (recursively) linked sales quotation items, sales order
+ # items and sales delivery order items.
+
+ # The checks for $form->{"ordnumber_$i"} and quo and do are for the old
+ # behaviour, where this data was stored in its own database fields in
+ # the invoice items, and there were no record links for the items.
+
+ # If this information were to be fetched in retrieve_invoice, e.g. for showing
+ # this information in the second row, then these fields will already have
+ # been set and won't be calculated again. This shouldn't be done there
+ # though, as each invocation creates several database calls per item, and would
+ # make the interface very slow for many items. So currently these
+ # requests are only made when printing the record.
+
+ # When using the workflow an invoice item can only be (recursively) linked to at
+ # most one sales quotation item and at most one delivery order item. But it may
+ # be linked back to several order items, if collective orders were involved. If
+ # that is the case we will always choose the very first order item from the
+ # original order, i.e. where it first appeared in an order.
+
+ # TODO: credit note items aren't checked for a record link to their
+ # invoice item
+
+ unless ( $form->{"ordnumber_$i"} ) {
+
+ # $form->{"ordnumber_$i"} comes from ordnumber in invoice, if an
+ # entry exists this must be from before the change from ordnumber to linked items.
+ # So we just use that value and don't check for linked items.
+ # In that case there won't be any links for quo or do items either
+
+ # sales order items are fetched and sorted by id, the lowest id is first
+ # It is assumed that the id always grows, so the item we want (the original) will have the lowest id
+ # better solution: filter the order_item that doesn't have any links from other order_items
+ # or maybe fetch linked_records with param save_path and order by _record_length_depth
+ my @linked_orderitems = grep { $_->isa("SL::DB::OrderItem") && $_->record->type eq 'sales_order' } @{$linkeditems};
+ if ( scalar @linked_orderitems ) {
+ @linked_orderitems = sort { $a->id <=> $b->id } @linked_orderitems;
+ my $orderitem = $linked_orderitems[0]; # 0: the original order item, -1: the last collective order item
+
+ $form->{"ordnumber_$i"} = $orderitem->record->record_number;
+ $form->{"transdate_oe_$i"} = $orderitem->record->transdate->to_kivitendo;
+ $form->{"cusordnumber_oe_$i"} = $orderitem->record->cusordnumber;
+ };
+
+ my @linked_quoitems = grep { $_->isa("SL::DB::OrderItem") && $_->record->type eq 'sales_quotation' } @{$linkeditems};
+ if ( scalar @linked_quoitems ) {
+ croak "an invoice item may only be linked back to 1 sales quotation item, something is wrong\n" unless scalar @linked_quoitems == 1;
+ $form->{"quonumber_$i"} = $linked_quoitems[0]->record->record_number;
+ $form->{"transdate_quo_$i"} = $linked_quoitems[0]->record->transdate->to_kivitendo;
+ };
+
+ my @linked_deliveryorderitems = grep { $_->isa("SL::DB::DeliveryOrderItem") && $_->record->type eq 'sales_delivery_order' } @{$linkeditems};
+ if ( scalar @linked_deliveryorderitems ) {
+ croak "an invoice item may only be linked back to 1 sales delivery item, something is wrong\n" unless scalar @linked_deliveryorderitems == 1;
+ $form->{"donumber_$i"} = $linked_deliveryorderitems[0]->record->record_number;
+ $form->{"transdate_do_$i"} = $linked_deliveryorderitems[0]->record->transdate->to_kivitendo;
+ };
+ };
+ };
+
+
+ # add number, description and qty to $form->{number},
+ if ($form->{"subtotal_$i"} && !$subtotal_header) {
+ $subtotal_header = $i;
+ $position = int($position);
+ $subposition = 0;
+ $position++;
+ } elsif ($subtotal_header) {
+ $subposition += 1;
+ $position = int($position);
+ $position = $position.".".$subposition;
+ } else {
+ $position = int($position);
+ $position++;
+ }
+
+ my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
+
+ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, $prepared_template_arrays{$_}[$i - 1]) for @prepared_arrays;
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{entry_type} }, 'normal';
+ push @{ $form->{TEMPLATE_ARRAYS}->{runningnumber} }, $position;
+ push @{ $form->{TEMPLATE_ARRAYS}->{number} }, $form->{"partnumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{serialnumber} }, $form->{"serialnumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{bin} }, $form->{"bin_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{partnotes} }, $form->{"partnotes_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{description} }, $form->{"description_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{longdescription} }, $form->{"longdescription_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
+ push @{ $form->{TEMPLATE_ARRAYS}->{qty_nofmt} }, $form->{"qty_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{unit} }, $form->{"unit_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{deliverydate_oe} }, $form->{"reqdate_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{sellprice} }, $form->{"sellprice_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{sellprice_nofmt} }, $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+ # linked item print variables
+ push @{ $form->{TEMPLATE_ARRAYS}->{quonumber_quo} }, $form->{"quonumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{transdate_quo} }, $form->{"transdate_quo_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{ordnumber_oe} }, $form->{"ordnumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{transdate_oe} }, $form->{"transdate_oe_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{cusordnumber_oe} }, $form->{"cusordnumber_oe_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{donumber_do} }, $form->{"donumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{transdate_do} }, $form->{"transdate_do_$i"};
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{invnumber} }, $form->{"invnumber"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{invdate} }, $form->{"invdate"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{price_factor} }, $price_factor->{formatted_factor};
+ push @{ $form->{TEMPLATE_ARRAYS}->{price_factor_name} }, $price_factor->{description};
+ push @{ $form->{TEMPLATE_ARRAYS}->{partsgroup} }, $form->{"partsgroup_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{reqdate} }, $form->{"reqdate_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{listprice} }, $form->format_amount($myconfig, $form->{"listprice_$i"}, 2);
+ push(@{ $form->{TEMPLATE_ARRAYS}->{listprice_nofmt} }, $form->{"listprice_$i"});
+
+ my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+ my ($dec) = ($sellprice =~ /\.(\d+)/);
+ my $decimalplaces = max 2, length($dec);
+
+ my $parsed_discount = $form->parse_amount($myconfig, $form->{"discount_$i"});
+
+ my $linetotal_exact = $form->{"qty_$i"} * $sellprice * (100 - $parsed_discount) / 100 / $price_factor->{factor};
+ my $linetotal = $form->round_amount($linetotal_exact, 2);
+
+ my $nodiscount_exact_linetotal = $form->{"qty_$i"} * $sellprice / $price_factor->{factor};
+ my $nodiscount_linetotal = $form->round_amount($nodiscount_exact_linetotal,2);
+
+ my $discount = $nodiscount_linetotal - $linetotal; # is always rounded because $nodiscount_linetotal and $linetotal are rounded
+
+ my $discount_round_error = $discount + ($linetotal_exact - $nodiscount_exact_linetotal); # not used
+
+ $form->{"netprice_$i"} = $form->round_amount($form->{"qty_$i"} ? ($linetotal / $form->{"qty_$i"}) : 0, $decimalplaces);
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{netprice} }, ($form->{"netprice_$i"} != 0) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : '';
+ push @{ $form->{TEMPLATE_ARRAYS}->{netprice_nofmt} }, ($form->{"netprice_$i"} != 0) ? $form->{"netprice_$i"} : '';
+
+ $linetotal = ($linetotal != 0) ? $linetotal : '';
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{discount} }, ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, 2) : '';
+ push @{ $form->{TEMPLATE_ARRAYS}->{discount_nofmt} }, ($discount != 0) ? $discount * -1 : '';
+ push @{ $form->{TEMPLATE_ARRAYS}->{p_discount} }, $form->{"discount_$i"};
+
+ if ( $prepared_template_arrays{separate}[$i - 1] ) {
+ my $pabbr = $prepared_template_arrays{separate}[$i - 1];
+ if ( ! $form->{"separate_${pabbr}_subtotal"} ) {
+ push @separate_totals , "separate_${pabbr}_subtotal";
+ $form->{"separate_${pabbr}_subtotal"} = 0;