- 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 $i_discount =
- $form->round_amount(
- $sellprice * $form->parse_amount($myconfig,
- $form->{"discount_$i"}) / 100,
- $decimalplaces);
-
- my $discount =
- $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
-
- # keep a netprice as well, (sellprice - discount)
- #$form->{"netprice_$i"} = $sellprice - $discount;
- $form->{"netprice_$i"} = $sellprice - $i_discount;
- my $nodiscount_linetotal =
- $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
- my $linetotal =
- $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
-
- push(@{ $form->{netprice} },
- ($form->{"netprice_$i"} != 0)
- ? $form->format_amount(
- $myconfig, $form->{"netprice_$i"},
- $decimalplaces
- )
- : " ");
-
- $discount =
- ($discount != 0)
- ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
- : " ";
- $linetotal = ($linetotal != 0) ? $linetotal : " ";
-
- push(@{ $form->{discount} }, $discount);
- push(@{ $form->{p_discount} }, $form->{"discount_$i"});
-
- $form->{ordtotal} += $linetotal;
- $discount_subtotal += $linetotal;
+ 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}->{ship} }, $form->format_amount($myconfig, $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}->{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 $discount = $form->round_amount($form->{"qty_$i"} * $sellprice * $parsed_discount / 100 / $price_factor->{factor} - ($linetotal - $linetotal_exact),
+ $decimalplaces);
+ my $nodiscount_linetotal = $form->round_amount($form->{"qty_$i"} * $sellprice / $price_factor->{factor}, 2);
+ $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) : '';
+
+ $linetotal = ($linetotal != 0) ? $linetotal : '';
+
+ push @{ $form->{TEMPLATE_ARRAYS}->{discount} }, ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, 2) : '';
+ push @{ $form->{TEMPLATE_ARRAYS}->{p_discount} }, $form->{"discount_$i"};
+
+ $form->{ordtotal} += $linetotal;