+ $column_data{serialnr} = qq|<input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">|;
+ $column_data{projectnr} = NTI($cgi->popup_menu(
+ '-name' => "project_id_$i",
+ '-values' => \@projectnumber_values,
+ '-labels' => \%projectnumber_labels,
+ '-default' => $form->{"project_id_$i"}
+ ));
+ $column_data{reqdate} = qq|<input name="reqdate_$i" size="11" onchange="check_right_date_format(this)" value="$form->{"reqdate_$i"}">|;
+ $column_data{subtotal} = sprintf qq|<input type="checkbox" name="subtotal_$i" value="1" %s>|, $form->{"subtotal_$i"} ? 'checked' : '';
+
+# begin marge calculations
+ $form->{"lastcost_$i"} *= 1;
+ $form->{"marge_percent_$i"} = 0;
+
+ my $marge_color;
+ my $real_sellprice;
+ if ( $form->{taxincluded} and $form->{"qty_$i"} * 1 and $form->{$form->{"taxaccounts_$i"} . "_rate"} * 1) {
+ # if we use taxincluded we need to calculate the marge from the net_value
+ # all the marge calculations are based on linetotal which we need to
+ # convert to net first
+
+ # there is no direct form value for the tax_rate of the item, but
+ # form->{taxaccounts_$i} gives the tax account (e.g. 3806) and 3806_rate
+ # gives the tax percentage (e.g. 0.19)
+ $real_sellprice = $linetotal / (1 + $form->{$form->{"taxaccounts_$i"} . "_rate"});
+ } else {
+ $real_sellprice = $linetotal;
+ };
+ my $real_lastcost = $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / $price_factor, 2);
+ my $marge_percent_warn = $myconfig{marge_percent_warn} * 1 || 15;
+ my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
+
+ if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
+ $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
+ $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
+ }
+
+ $form->{"marge_absolut_$i"} = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
+ $form->{"marge_total"} += $form->{"marge_absolut_$i"};
+ $form->{"lastcost_total"} += $real_lastcost;
+ $form->{"sellprice_total"} += $real_sellprice;
+
+ map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
+
+ $column_data{marge} = sprintf qq|<font %s>%s %s%%</font>|,
+ $marge_color, $form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"};
+ $column_data{listprice} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2);
+ $column_data{lastcost} = sprintf qq|<input size="5" name="lastcost_$i" value="%s">|, $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
+# / marge calculations ending
+
+# Calculate total weight
+ $totalweight += ($form->{"qty_$i"} * $form->{"weight_$i"});
+
+# calculate onhand
+ if ($form->{"id_$i"}) {
+ my $part = IC->get_basic_part_info(id => $form->{"id_$i"});
+ my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
+ $column_data{onhand} = sprintf "<font %s>%s %s</font>",
+ $onhand_color,
+ $form->format_amount(\%myconfig, $part->{onhand}, 2),
+ $part->{unit};
+ }
+# / calculate onhand
+
+ my @ROW1 = map { { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} } } grep { $column_def{$_}{display} } @header_sort;
+ my @ROW2 = map { { value => sprintf "<b>%s</b> %s", $column_def{$_}{value}, $column_data{$_} } } grep { $column_def{$_}{display} } @row2_sort;
+
+ my @hidden_vars;
+ # add hidden ids for persistent (item|invoice)_ids and previous (converted_from*) ids
+ if ($is_quotation) {
+ push @hidden_vars, qw(orderitems_id converted_from_orderitems_id);
+ }
+ if ($is_s_p_order) {
+ push @hidden_vars, qw(orderitems_id converted_from_orderitems_id converted_from_invoice_id);
+ }
+ if ($is_invoice) {
+ push @hidden_vars, qw(invoice_id converted_from_orderitems_id converted_from_delivery_order_items_id converted_from_invoice_id);
+ }
+ if ($::form->{type} =~ /credit_note/) {
+ push @hidden_vars, qw(invoice_id converted_from_invoice_id);
+ }
+ if ($is_delivery_order) {
+ map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
+ push @hidden_vars, grep { defined $form->{"${_}_${i}"} } qw(sellprice discount not_discountable price_factor_id lastcost);
+ push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
+ push @hidden_vars, qw(delivery_order_items_id converted_from_orderitems_id converted_from_delivery_order_items_id);
+ }