+ my $real_sellprice = $form->{"sellprice_$i"} - $discount;
+
+ # marge calculations
+ my ($marge_font_start, $marge_font_end);
+
+ $form->{"lastcost_$i"} *= 1;
+
+ if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
+ $form->{"marge_percent_$i"} = ($real_sellprice - $form->{"lastcost_$i"}) * 100 / $real_sellprice;
+ $myconfig{"marge_percent_warn"} = 15 unless (defined($myconfig{"marge_percent_warn"}));
+
+ if ($form->{"id_$i"} &&
+ ($form->{"marge_percent_$i"} < (1 * $myconfig{"marge_percent_warn"}))) {
+ $marge_font_start = "<font color=\"#ff0000\">";
+ $marge_font_end = "</font>";
+ }
+
+ } else {
+ $form->{"marge_percent_$i"} = 0;
+ }
+
+ my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
+ $form->{"marge_absolut_$i"} = ($real_sellprice - $form->{"lastcost_$i"}) * $form->{"qty_$i"} * $marge_adjust_credit_note;
+ $form->{"marge_total"} += $form->{"marge_absolut_$i"};
+ $form->{"lastcost_total"} += $form->{"lastcost_$i"} * $form->{"qty_$i"};
+ $form->{"sellprice_total"} += $real_sellprice * $form->{"qty_$i"};
+
+ map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);