- push(@{ $form->{runningnumber} }, $i);
- push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
- push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
- push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
- push(@{ $form->{qty} },
- $form->format_amount($myconfig, $form->{"qty_$i"}));
- push(@{ $form->{ship} },
- $form->format_amount($myconfig, $form->{"ship_$i"}));
- push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
- push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
- push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
- push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
- push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
-
- push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
-
- push(@{ $form->{listprice} }, $form->{"listprice_$i"});
-
- my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
- my ($dec) = ($sellprice =~ /\.(\d+)/);
- $dec = length $dec;
- my $decimalplaces = ($dec > 2) ? $dec : 2;
+ my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{runningnumber} }, $position;
+ push @{ $form->{TEMPLATE_ARRAYS}->{number} }, $form->{"partnumber_$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}->{ship} }, $form->format_amount($myconfig, $form->{"ship_$i"});
+ push @{ $form->{TEMPLATE_ARRAYS}->{ship_nofmt} }, $form->{"ship_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{unit} }, $form->{"unit_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{bin} }, $form->{"bin_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{partnotes} }, $form->{"partnotes_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{serialnumber} }, $form->{"serialnumber_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{reqdate} }, $form->{"reqdate_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{sellprice} }, $form->{"sellprice_$i"};
+ push @{ $form->{TEMPLATE_ARRAYS}->{sellprice_nofmt} }, $form->parse_amount($myconfig, $form->{"sellprice_$i"});
+ push @{ $form->{TEMPLATE_ARRAYS}->{listprice} }, $form->{"listprice_$i"};
+ 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"};
+
+ 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, 2);
+
+ 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 : '';