1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # Changelog: Wann - Wer - Was
8 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik
9 # 08.11.2008 - information@richardson-bueren.de jb - Backport von Revision 7339 xplace - E-Mail-Vorlage automatisch auswählen
10 # 02.02.2009 - information@richardson-bueren.de jb - Backport von Revision 8535 xplace - Erweiterung der Waren bei Lieferantenauftrag um den Eintrag Mindestlagerbestand. Offen: Auswahlliste auf Lieferantenaufträge einschränken -> Erledigt 2.2.09 Prüfung wie das Skript heisst (oe.pl) -> das ist nur die halbe Miete, nochmal mb fragen -> mb gefragt und es gibt die variable is_purchase
11 #############################################################################
12 # SQL-Ledger, Accounting
13 # Copyright (c) 1998-2002
15 # Author: Dieter Simader
16 # Email: dsimader@sql-ledger.org
17 # Web: http://www.sql-ledger.org
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #######################################################################
35 # common routines used in is, ir, oe
37 #######################################################################
41 use List::MoreUtils qw(any uniq);
42 use List::Util qw(min max first);
53 use SL::Helper::Flash;
55 require "bin/mozilla/common.pl";
59 # any custom scripts for this one
60 if (-f "bin/mozilla/custom_io.pl") {
61 eval { require "bin/mozilla/custom_io.pl"; };
63 if (-f "bin/mozilla/$::form->{login}_io.pl") {
64 eval { require "bin/mozilla/$::form->{login}_io.pl"; };
71 # this is for our long dates
72 # $locale->text('January')
73 # $locale->text('February')
74 # $locale->text('March')
75 # $locale->text('April')
76 # $locale->text('May ')
77 # $locale->text('June')
78 # $locale->text('July')
79 # $locale->text('August')
80 # $locale->text('September')
81 # $locale->text('October')
82 # $locale->text('November')
83 # $locale->text('December')
85 # this is for our short month
86 # $locale->text('Jan')
87 # $locale->text('Feb')
88 # $locale->text('Mar')
89 # $locale->text('Apr')
90 # $locale->text('May')
91 # $locale->text('Jun')
92 # $locale->text('Jul')
93 # $locale->text('Aug')
94 # $locale->text('Sep')
95 # $locale->text('Oct')
96 # $locale->text('Nov')
97 # $locale->text('Dec')
104 $main::auth->assert('part_service_assembly_edit | vendor_invoice_edit | sales_order_edit | invoice_edit |' .
105 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | ' .
106 'purchase_delivery_order_edit | sales_delivery_order_edit | part_service_assembly_details');
109 ########################################
110 # Eintrag fuer Version 2.2.0 geaendert #
111 # neue Optik im Rechnungsformular #
112 ########################################
114 $main::lxdebug->enter_sub();
118 my $form = $main::form;
119 my %myconfig = %main::myconfig;
120 my $locale = $main::locale;
121 my $cgi = $::request->{cgi};
125 my ($stock_in_out, $stock_in_out_title);
127 my $defaults = AM->get_defaults();
128 $form->{show_weight} = $defaults->{show_weight};
129 $form->{weightunit} = $defaults->{weightunit};
131 my $is_purchase = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
132 my $show_min_order_qty = first { $_ eq $form->{type} } qw(request_quotation purchase_order);
133 my $is_delivery_order = $form->{type} =~ /_delivery_order$/;
134 my $is_s_p_order = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
136 if ($is_delivery_order) {
137 if ($form->{type} eq 'sales_delivery_order') {
138 $stock_in_out_title = $locale->text('Release From Stock');
139 $stock_in_out = 'out';
141 $stock_in_out_title = $locale->text('Transfer To Stock');
142 $stock_in_out = 'in';
145 retrieve_partunits();
149 my @header_sort = qw(runningnumber partnumber description ship qty unit weight sellprice_pg sellprice discount linetotal);
151 { id => 'runningnumber', width => 5, value => $locale->text('No.'), display => 1, },
152 { id => 'partnumber', width => 8, value => $locale->text('Number'), display => 1, },
153 { id => 'description', width => 30, value => $locale->text('Part Description'), display => 1, },
154 { id => 'ship', width => 5, value => $locale->text('Delivered'), display => $is_s_p_order, },
155 { id => 'qty', width => 5, value => $locale->text('Qty'), display => 1, },
156 { id => 'price_factor', width => 5, value => $locale->text('Price Factor'), display => !$is_delivery_order, },
157 { id => 'unit', width => 5, value => $locale->text('Unit'), display => 1, },
158 { id => 'weight', width => 5, value => $locale->text('Weight'), display => $defaults->{show_weight}, },
159 { id => 'serialnr', width => 10, value => $locale->text('Serial No.'), display => 0, },
160 { id => 'projectnr', width => 10, value => $locale->text('Project'), display => 0, },
161 { id => 'sellprice', width => 15, value => $locale->text('Price'), display => !$is_delivery_order, },
162 { id => 'sellprice_pg', width => 8, value => $locale->text('Pricegroup'), display => !$is_delivery_order && !$is_purchase, },
163 { id => 'discount', width => 5, value => $locale->text('Discount'), display => !$is_delivery_order, },
164 { id => 'linetotal', width => 10, value => $locale->text('Extended'), display => !$is_delivery_order, },
165 { id => 'bin', width => 10, value => $locale->text('Bin'), display => 0, },
166 { id => 'stock_in_out', width => 10, value => $stock_in_out_title, display => $is_delivery_order, },
168 my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
172 my $all_units = AM->retrieve_units(\%myconfig, $form);
174 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
176 my $colspan = scalar @column_index;
178 $form->{invsubtotal} = 0;
179 map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
182 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
183 $form->{show_details} = $myconfig{show_form_details} unless (defined($form->{show_details}));
186 # translations, unused commented out
187 # $runningnumber = $locale->text('No.');
188 # my $deliverydate = $locale->text('Delivery Date');
189 my $serialnumber = $locale->text('Serial No.');
190 my $projectnumber = $locale->text('Project');
191 # $partsgroup = $locale->text('Group');
192 my $reqdate = $locale->text('Reqdate');
193 my $deliverydate = $locale->text('Required by');
196 my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out weight);
197 my %nowrap = map { $_ => 1 } qw(description unit);
199 $form->{marge_total} = 0;
200 $form->{sellprice_total} = 0;
201 $form->{lastcost_total} = 0;
202 $form->{totalweight} = 0;
203 my %projectnumber_labels = ();
204 my @projectnumber_values = ("");
206 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
207 push(@projectnumber_values, $item->{"id"});
208 $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
211 _update_part_information();
212 _update_ship() if ($is_s_p_order);
213 _update_custom_variables();
220 for my $i (1 .. $numrows) {
221 my %column_data = ();
224 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
225 qw(qty discount sellprice lastcost price_new price_old)
226 unless ($form->{simple_save});
228 if ($form->{"prices_$i"} && ($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})) {
229 $form->{"sellprice_$i"} = $form->{"price_new_$i"};
233 $form->{"unit_old_$i"} ||= $form->{"unit_$i"};
234 $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
236 if ( !$all_units->{$form->{"selected_unit_$i"}} # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
237 || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
238 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"}; # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
240 # adjust prices by unit, ignore if pricegroup changed
241 if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
242 $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
243 $form->{"lastcost_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
244 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
246 my $this_unit = $form->{"unit_$i"};
247 $this_unit = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
249 if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
250 my @values = ('', map { $_->{id} } @{ $form->{ALL_PRICE_FACTORS} });
251 my %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
253 $column_data{price_factor} =
254 NTI($cgi->popup_menu('-name' => "price_factor_id_$i",
255 '-default' => $form->{"price_factor_id_$i"},
256 '-values' => \@values,
257 '-labels' => \%labels,
258 '-style' => 'width:90px'));
260 $column_data{price_factor} = ' ';
262 $form->{"weight_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"partunit_$i"}, $all_units) || 1;
264 $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
267 #count the max of decimalplaces of sellprice and lastcost, so the same number of decimalplaces
268 #is shown for lastcost and sellprice.
269 my $decimalplaces = ($form->{"sellprice_$i"} =~ /\.(\d+)/) ? max 2, length $1 : 2;
270 $decimalplaces = ($form->{"lastcost_$i"} =~ /\.(\d+)/) ? max $decimalplaces, length $1 : $decimalplaces;
272 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || 1;
273 my $discount = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100 / $price_factor, 2);
274 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
275 my $rows = $form->numtextrows($form->{"description_$i"}, 30, 6);
277 $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -id => "runningnumber_$i", -size => 5, -value => $i); # HuT
278 $column_data{partnumber} = $cgi->textfield(-name => "partnumber_$i", -id => "partnumber_$i", -size => 12, -value => $form->{"partnumber_$i"});
279 $column_data{description} = (($rows > 1) # if description is too large, use a textbox instead
280 ? $cgi->textarea( -name => "description_$i", -id => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
281 : $cgi->textfield(-name => "description_$i", -id => "description_$i", -value => $form->{"description_$i"}, -size => 30))
282 . $cgi->button(-value => $locale->text('L'), -onClick => "kivi.SalesPurchase.edit_longdescription($i)");
284 my $qty_dec = ($form->{"qty_$i"} =~ /\.(\d+)/) ? length $1 : 2;
286 $column_data{qty} = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
287 $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
288 . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
289 if $form->{"formel_$i"};
291 $column_data{ship} = '';
292 if ($form->{"id_$i"}) {
293 my $ship_qty = $form->{"ship_$i"} * 1;
294 $ship_qty *= $all_units->{$form->{"partunit_$i"}}->{factor};
295 $ship_qty /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
297 $column_data{ship} = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
300 # build in drop down list for pricesgroups
301 # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
302 # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
303 # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
304 # jetzt nach der Abfrage.
306 if ($form->{"prices_$i"}) {
307 $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
308 $sellprice_value =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
309 ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
310 : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
312 # for last row and report
313 # set pricegroup drop down list from report menu
314 if ($form->{"sellprice_$i"} != 0) {
315 # remember the pricegroup_id in pricegroup_old
316 # but don't overwrite it
317 $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
318 my $default_option = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
319 $column_data{sellprice_pg} = NTI($cgi->popup_menu("sellprice_pg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
321 $column_data{sellprice_pg} = qq| |;
323 $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
326 # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
327 # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
328 # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
330 my $edit_prices = $main::auth->assert('edit_prices', 1);
331 $column_data{sellprice} = (!$edit_prices)
332 ? $cgi->textfield(-readonly => "readonly",
333 -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
334 : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
335 $column_data{discount} = (!$edit_prices)
336 ? $cgi->textfield(-readonly => "readonly",
337 -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
338 : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
339 $column_data{linetotal} = $form->format_amount(\%myconfig, $linetotal, 2);
340 $column_data{bin} = $form->{"bin_$i"};
342 $column_data{weight} = $form->format_amount(\%myconfig, $form->{"qty_$i"} * $form->{"weight_$i"}, 3) . ' ' . $defaults->{weightunit} if $defaults->{show_weight};
344 if ($is_delivery_order) {
345 $column_data{stock_in_out} = calculate_stock_in_out($i);
348 my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
352 push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
353 if $form->{type} !~ /_quotation/;
354 push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name' => "project_id_$i", '-values' => \@projectnumber_values,
355 '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
356 push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
357 if ($form->{type} =~ /order/ || $form->{type} =~ /invoice/);
358 push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
359 $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
361 # begin marge calculations
362 $form->{"lastcost_$i"} *= 1;
363 $form->{"marge_percent_$i"} = 0;
367 if ( $form->{taxincluded} and $form->{"qty_$i"} * 1 and $form->{$form->{"taxaccounts_$i"} . "_rate"} * 1) {
368 # if we use taxincluded we need to calculate the marge from the net_value
369 # all the marge calculations are based on linetotal which we need to
370 # convert to net first
372 # there is no direct form value for the tax_rate of the item, but
373 # form->{taxaccounts_$i} gives the tax account (e.g. 3806) and 3806_rate
374 # gives the tax percentage (e.g. 0.19)
375 $real_sellprice = $linetotal / (1 + $form->{$form->{"taxaccounts_$i"} . "_rate"});
377 $real_sellprice = $linetotal;
379 my $real_lastcost = $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / $price_factor, 2);
380 my $marge_percent_warn = $myconfig{marge_percent_warn} * 1 || 15;
381 my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
383 if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
384 $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
385 $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
388 $form->{"marge_absolut_$i"} = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
389 $form->{"marge_total"} += $form->{"marge_absolut_$i"};
390 $form->{"lastcost_total"} += $real_lastcost;
391 $form->{"sellprice_total"} += $real_sellprice;
393 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
395 push @ROW2, { value => sprintf qq|
396 <font %s><b>%s</b> %s %s%% </font>
398 <b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
399 $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
400 $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
401 $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces) }
402 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
404 $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
405 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/) ;
406 # / marge calculations ending
408 # Calculate total weight
409 $totalweight += ($form->{"qty_$i"} * $form->{"weight_$i"});
412 if ($form->{"id_$i"}) {
413 my $part = IC->get_basic_part_info(id => $form->{"id_$i"});
414 my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
415 push @ROW2, { value => sprintf "<b>%s</b> <font %s>%s %s</font>",
416 $locale->text('On Hand'),
418 $form->format_amount(\%myconfig, $part->{onhand}, 2),
426 if ($is_delivery_order) {
427 map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
428 $form->{"pricegroup_id_$i"} = $form->{"pricegroup_old_$i"} if $form->{"pricegroup_old_$i"};
429 $form->{"sellprice_pg_$i"} = $form->{"hidden_prices_$i"} if $form->{"hidden_prices_$i"};
430 push @hidden_vars, grep { defined $form->{"${_}_${i}"} } qw(sellprice discount not_discountable price_factor_id lastcost pricegroup_id sellprice_pg);
431 push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
434 my @HIDDENS = map { value => $_}, (
435 $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
436 $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
437 map { ($cgi->hidden("-name" => $_, "-id" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
438 (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
439 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
440 longdescription basefactor marge_absolut marge_percent marge_price_factor weight), @hidden_vars)
443 map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
445 $form->{invsubtotal} += $linetotal;
447 # Benutzerdefinierte Variablen für Waren/Dienstleistungen/Erzeugnisse
448 _render_custom_variables_inputs(ROW2 => \@ROW2, row => $i, part_id => $form->{"id_$i"});
450 push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
453 $form->{totalweight} = $totalweight;
455 print $form->parse_html_template('oe/sales_order', { ROWS => \@ROWS,
459 if (0 != ($form->{sellprice_total} * 1)) {
460 $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
463 $main::lxdebug->leave_sub();
466 ##################################################
467 # build html-code for pricegroups in variable $form->{prices_$j}
470 $main::lxdebug->enter_sub();
472 my $form = $main::form;
473 my $locale = $main::locale;
474 my $cgi = $::request->{cgi};
478 my $rowcount = shift;
479 for my $j (1 .. $rowcount) {
480 next unless $form->{PRICES}{$j};
481 # build drop down list for pricegroups
482 my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
483 $form->{"prices_$j"} = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} }
484 (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
486 foreach my $item (@{ $form->{PRICES}{$j} }) {
487 # set new selectedpricegroup_id and prices for "Preis"
488 $form->{"pricegroup_old_$j"} = $item->{pricegroup_id} if $item->{selected} && $item->{pricegroup_id};
489 $form->{"sellprice_$j"} = $item->{price} if $item->{selected} && $item->{pricegroup_id};
490 $form->{"price_new_$j"} = $form->{"sellprice_$j"} if $item->{selected} || !$item->{pricegroup_id};
493 # save hidden pricegroups for delivery_orders
494 next unless my @selected_prices = grep { $_->{selected} } @{ $form->{PRICES}{$j} };
495 $form->{"hidden_prices_$j"} = $selected_prices[-1]{price} . "--" . $selected_prices[-1]{pricegroup_id};
497 $main::lxdebug->leave_sub();
501 $main::lxdebug->enter_sub();
504 my $mode = $params{mode} || croak "Missing parameter 'mode'";
508 my $previous_form = $::auth->save_form_in_session(form => $::form);
509 $::form->{title} = $::locale->text('Select from one of the items below');
512 my @item_list = map {
513 $_->{display_sellprice} = $_->{sellprice} * (1 - $::form->{tradediscount});
514 $_->{display_sellprice} /= $_->{price_factor} if ($_->{price_factor});
516 } @{ $::form->{item_list} };
518 # delete action variable
519 delete @{$::form}{qw(action item_list)};
521 print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
523 ITEM_LIST => \@item_list,
524 IS_PURCHASE => $mode eq 'IS' });
526 $main::lxdebug->leave_sub();
530 $main::lxdebug->enter_sub();
532 my $form = $main::form;
533 my %myconfig = %main::myconfig;
537 $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
539 my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
540 my $id = delete($form->{select_item_id}) || croak 'Missing item selection ID';
541 my $i = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
543 $form->{"id_${i}"} = $id;
546 IS->retrieve_item(\%myconfig, \%$form);
547 } elsif ($mode eq 'IR') {
548 IR->retrieve_item(\%myconfig, \%$form);
549 } elsif ($mode eq 'IC') {
550 IC->assembly_item(\%myconfig, \%$form);
552 croak "Invalid item selection mode '${mode}'";
555 my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
557 # if there was a price entered, override it
558 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
561 qw(id partnumber description sellprice listprice inventory_accno
562 income_accno expense_accno bin unit weight assembly taxaccounts
563 partsgroup formel longdescription not_discountable partnotes lastcost
564 price_factor_id price_factor);
566 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
567 push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
569 map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
571 $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
573 if ($form->{"part_payment_id_$i"} ne "") {
574 $form->{payment_id} = $form->{"part_payment_id_$i"};
577 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
579 my $decimalplaces = ($dec > 2) ? $dec : 2;
582 $form->{"sellprice_$i"} = $sellprice;
585 # if there is an exchange rate adjust sellprice
586 if (($form->{exchangerate} * 1) != 0) {
587 $form->{"sellprice_$i"} /= $form->{exchangerate};
588 $form->{"sellprice_$i"} =
589 $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
593 if ($::form->{tradediscount}) {
594 $::form->{"sellprice_$i"} *= 1 - $::form->{tradediscount};
598 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
599 qw(sellprice listprice weight);
601 $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
603 if ($form->{"not_discountable_$i"}) {
604 $form->{"discount_$i"} = 0;
608 $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
610 map { $form->{"${_}_base"} += $amount }
611 (split / /, $form->{"taxaccounts_$i"});
612 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
613 $form->{"taxaccounts_$i"}
614 if !$form->{taxincluded};
616 $form->{creditremaining} -= $amount;
618 $form->{"runningnumber_$i"} = $i;
620 delete $form->{nextsub};
625 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
626 } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
628 # get pricegroups for parts
629 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
631 # build up html code for prices_$i
632 set_pricegroup($form->{rowcount});
636 $main::lxdebug->leave_sub();
640 $main::lxdebug->enter_sub();
642 my $form = $main::form;
643 my %myconfig = %main::myconfig;
647 my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
650 $form->{old_callback} = $form->escape($form->{callback}, 1);
651 $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
653 # save all form variables except action in the session and keep the key in the previousform variable
654 my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
657 push @HIDDENS, { 'name' => 'previousform', 'value' => $previousform };
658 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(rowcount vc);
659 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
660 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
661 push @HIDDENS, { 'name' => $price_key, 'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
662 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{"longdescription_$form->{rowcount}"} };
665 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
667 $main::lxdebug->leave_sub();
671 $main::lxdebug->enter_sub();
673 my $form = $main::form;
674 my %myconfig = %main::myconfig;
681 # remove any makes or model rows
682 if ($form->{item} eq 'assembly') {
684 # fuer assemblies auskommentiert. seiteneffekte? ;-) wird die woanders benoetigt?
685 #$form->{sellprice} = 0;
687 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
688 qw(listprice sellprice rop stock);
690 my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
692 for my $i (1 .. ($form->{assembly_rows} - 1)) {
693 if ($form->{"qty_$i"}) {
697 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
699 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
701 #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
703 # fuer assemblies auskommentiert. siehe oben
704 # $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
705 $form->{weight} += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
709 # kann das hier auch weg? s.o. jb
710 $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
712 $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
713 $form->{assembly_rows} = $count;
715 } elsif ($form->{item} !~ m{^(?:part|service)$}) {
716 remove_emptied_rows(1);
718 $form->{creditremaining} -= &invoicetotal;
723 if ( $form->{type} =~ (/sales_quotation/)
724 or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
725 or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
726 or ($form->{type} =~ /sales_order/)) {
728 # get pricegroups for parts
729 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
731 # build up html code for prices_$i
732 set_pricegroup($form->{rowcount});
738 $main::lxdebug->leave_sub();
741 sub remove_emptied_rows {
742 my $dont_add_empty = shift;
745 return unless $form->{rowcount};
747 my @flds = qw(id partnumber description qty ship sellprice unit
748 discount inventory_accno income_accno expense_accno listprice
749 taxaccounts bin assembly weight projectnumber project_id
750 oldprojectnumber runningnumber serialnumber partsgroup payment_id
751 not_discountable shop ve gv buchungsgruppen_id language_values
752 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
753 transdate longdescription basefactor marge_total marge_percent
754 marge_price_factor lastcost price_factor_id partnotes
755 stock_out stock_in has_sernumber reqdate);
757 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
758 push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
761 for my $i (1 .. $form->{rowcount} - 1) {
762 next unless $form->{"partnumber_$i"};
764 push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
767 my $new_rowcount = scalar @new_rows;
768 $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
769 $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
773 $main::lxdebug->enter_sub();
775 my $form = $main::form;
776 my %myconfig = %main::myconfig;
780 $form->{oldinvtotal} = 0;
782 # add all parts and deduct paid
783 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
785 my ($amount, $sellprice, $discount, $qty);
787 for my $i (1 .. $form->{rowcount}) {
788 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
789 $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
790 $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
792 #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
794 $amount = $sellprice * (1 - $discount / 100) * $qty;
795 map { $form->{"${_}_base"} += $amount }
796 (split (/ /, $form->{"taxaccounts_$i"}));
797 $form->{oldinvtotal} += $amount;
800 map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
801 split(/ /, $form->{taxaccounts})
802 if !$form->{taxincluded};
804 $form->{oldtotalpaid} = 0;
805 for my $i (1 .. $form->{paidaccounts}) {
806 $form->{oldtotalpaid} += $form->{"paid_$i"};
809 $main::lxdebug->leave_sub();
812 return ($form->{oldinvtotal} - $form->{oldtotalpaid});
816 $main::lxdebug->enter_sub();
818 my $form = $main::form;
819 my $locale = $main::locale;
823 # check if items are valid
824 if ($form->{rowcount} == 1) {
825 flash('warning', $::locale->text('The action you\'ve chosen has not been executed because the document does not contain any item yet.'));
830 for my $i (1 .. $form->{rowcount} - 1) {
831 $form->isblank("partnumber_$i",
832 $locale->text('Number missing in Row') . " $i");
835 $main::lxdebug->leave_sub();
839 $main::lxdebug->enter_sub();
841 my $form = $main::form;
842 my %myconfig = %main::myconfig;
843 my $locale = $main::locale;
847 if ($form->{second_run}) {
848 $form->{print_and_post} = 0;
850 $form->{ordnumber} = $form->{invnumber};
852 $form->{old_employee_id} = $form->{employee_id};
853 $form->{old_salesman_id} = $form->{salesman_id};
855 map { delete $form->{$_} } qw(id printed emailed queued);
857 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
858 $form->{title} = $locale->text('Add Purchase Order');
859 $form->{vc} = 'vendor';
860 $form->{type} = 'purchase_order';
863 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
864 $form->{title} = $locale->text('Add Sales Order');
865 $form->{vc} = 'customer';
866 $form->{type} = 'sales_order';
869 $form->{script} = 'oe.pl';
877 require "bin/mozilla/$form->{script}";
878 my $script = $form->{"script"};
881 $locale = new Locale($::lx_office_conf{system}->{language}, $script);
883 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
885 my $currency = $form->{currency};
889 $form->{currency} = $currency;
890 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
891 $form->{exchangerate} = $form->{forex} || '';
893 for my $i (1 .. $form->{rowcount}) {
894 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
895 if ($form->{"${_}_${i}"}) }
896 qw(ship qty sellprice listprice basefactor discount));
902 $main::lxdebug->leave_sub();
906 $main::lxdebug->enter_sub();
908 my $form = $main::form;
909 my %myconfig = %main::myconfig;
910 my $locale = $main::locale;
914 if ($form->{second_run}) {
915 $form->{print_and_post} = 0;
917 map { delete $form->{$_} } qw(id printed emailed queued);
920 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
921 $form->{title} = $locale->text('Add Request for Quotation');
922 $form->{vc} = 'vendor';
923 $form->{type} = 'request_quotation';
926 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
927 $form->{title} = $locale->text('Add Quotation');
928 $form->{vc} = 'customer';
929 $form->{type} = 'sales_quotation';
935 $form->{script} = 'oe.pl';
941 require "bin/mozilla/$form->{script}";
943 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
945 my $currency = $form->{currency};
949 $form->{currency} = $currency;
950 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
951 $form->{exchangerate} = $form->{forex} || '';
953 for my $i (1 .. $form->{rowcount}) {
954 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
955 $form->{"${_}_${i}"})
956 if ($form->{"${_}_${i}"}) }
957 qw(ship qty sellprice listprice basefactor discount lastcost));
963 $main::lxdebug->leave_sub();
966 sub request_for_quotation {
971 $main::lxdebug->enter_sub();
973 my $form = $main::form;
974 my %myconfig = %main::myconfig;
975 my $locale = $main::locale;
979 if ($form->{second_run}) {
980 $form->{print_and_post} = 0;
981 $form->{resubmit} = 0;
984 $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
986 if ($form->{"cp_id"}) {
987 CT->get_contact(\%myconfig, $form);
988 $form->{"email"} = $form->{"cp_email"} if $form->{"cp_email"};
991 $form->{language} = $form->get_template_language(\%myconfig);
992 $form->{language} = "_" . $form->{language} if $form->{language};
994 my $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
996 $form->{oldmedia} = $form->{media};
997 $form->{media} = "email";
999 my $attachment_filename = $form->generate_attachment_filename();
1000 my $subject = $form->{subject} || $form->generate_email_subject();
1004 my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
1005 @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
1006 @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
1007 @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
1009 print $form->parse_html_template('generic/edit_email',
1011 a_filename => $attachment_filename,
1012 subject => $subject,
1013 print_options => print_options('inline' => 1),
1014 HIDDEN => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
1015 SHOW_BCC => $::auth->assert('email_bcc', 'may fail') });
1017 $main::lxdebug->leave_sub();
1021 $main::lxdebug->enter_sub();
1023 my $form = $main::form;
1024 my %myconfig = %main::myconfig;
1028 my $callback = $form->{script} . "?action=edit";
1029 map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
1031 print_form("return");
1033 Common->save_email_status(\%myconfig, $form);
1035 $form->{callback} = $callback;
1038 $main::lxdebug->leave_sub();
1041 # generate the printing options displayed at the bottom of oe and is forms.
1042 # this function will attempt to guess what type of form is displayed, and will generate according options
1045 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
1046 # the opthash function builds hashrefs which are then pieced together for the template arrays.
1047 # unneeded options are "undef"ed out, and then grepped out.
1049 # the inline options is untested, but intended to be used later in metatemplating
1051 $main::lxdebug->enter_sub();
1053 my $form = $main::form;
1054 my %myconfig = %main::myconfig;
1055 my $locale = $main::locale;
1061 # names 3 parameters and returns a hashref, for use in templates
1062 sub opthash { +{ value => shift, selected => shift, oname => shift } }
1063 my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1065 # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1066 $form->{sendmode} = "attachment";
1067 $form->{format} = $form->{format} || $myconfig{template_format} || "pdf";
1068 $form->{copies} = $form->{copies} || $myconfig{copies} || 3;
1069 $form->{media} = $form->{media} || $myconfig{default_media} || "screen";
1070 $form->{printer_id} = defined $form->{printer_id} ? $form->{printer_id} :
1071 defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1073 $form->{PD}{ $form->{formname} } = "selected";
1074 $form->{DF}{ $form->{format} } = "selected";
1075 $form->{OP}{ $form->{media} } = "selected";
1076 $form->{SM}{ $form->{formname} } = "selected";
1078 push @FORMNAME, grep $_,
1079 ($form->{type} eq 'purchase_order') ? (
1080 opthash("purchase_order", $form->{PD}{purchase_order}, $locale->text('Purchase Order')),
1081 opthash("bin_list", $form->{PD}{bin_list}, $locale->text('Bin List'))
1083 ($form->{type} eq 'credit_note') ?
1084 opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef,
1085 ($form->{type} eq 'sales_order') ? (
1086 opthash("sales_order", $form->{PD}{sales_order}, $locale->text('Confirmation')),
1087 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1089 ($form->{type} =~ /sales_quotation$/) ?
1090 opthash('sales_quotation', $form->{PD}{sales_quotation}, $locale->text('Quotation')) : undef,
1091 ($form->{type} =~ /request_quotation$/) ?
1092 opthash('request_quotation', $form->{PD}{request_quotation}, $locale->text('Request for Quotation')) : undef,
1093 ($form->{type} eq 'invoice') ? (
1094 opthash("invoice", $form->{PD}{invoice}, $locale->text('Invoice')),
1095 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1097 ($form->{type} eq 'invoice' && $form->{storno}) ? (
1098 opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
1100 ($form->{type} =~ /_delivery_order$/) ? (
1101 opthash($form->{type}, $form->{PD}{$form->{type}}, $locale->text('Delivery Order')),
1102 opthash('pick_list', $form->{PD}{pick_list}, $locale->text('Pick List')),
1106 opthash("attachment", $form->{SM}{attachment}, $locale->text('Attachment')),
1107 opthash("inline", $form->{SM}{inline}, $locale->text('In-line'))
1108 if ($form->{media} eq 'email');
1110 my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
1111 push @MEDIA, grep $_,
1112 opthash("screen", $form->{OP}{screen}, $locale->text('Screen')),
1113 ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
1114 opthash("printer", $form->{OP}{printer}, $locale->text('Printer')) : undef,
1115 ($printable_templates && !$options{no_queue}) ?
1116 opthash("queue", $form->{OP}{queue}, $locale->text('Queue')) : undef
1117 if ($form->{media} ne 'email');
1119 push @FORMAT, grep $_,
1120 ($::lx_office_conf{print_templates}->{opendocument} && $::lx_office_conf{applications}->{openofficeorg_writer} && $::lx_office_conf{applications}->{xvfb}
1121 && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
1122 && !$options{no_opendocument_pdf}) ?
1123 opthash("opendocument_pdf", $form->{DF}{"opendocument_pdf"}, $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1124 ($::lx_office_conf{print_templates}->{latex}) ?
1125 opthash("pdf", $form->{DF}{pdf}, $locale->text('PDF')) : undef,
1126 ($::lx_office_conf{print_templates}->{latex} && !$options{no_postscript}) ?
1127 opthash("postscript", $form->{DF}{postscript}, $locale->text('Postscript')) : undef,
1128 (!$options{no_html}) ?
1129 opthash("html", $form->{DF}{html}, "HTML") : undef,
1130 ($::lx_office_conf{print_templates}->{opendocument} && !$options{no_opendocument}) ?
1131 opthash("opendocument", $form->{DF}{opendocument}, $locale->text("OpenDocument/OASIS")) : undef,
1132 ($::lx_office_conf{print_templates}->{excel} && !$options{no_excel}) ?
1133 opthash("excel", $form->{DF}{excel}, $locale->text("Excel")) : undef;
1136 map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1137 if (ref $form->{languages} eq 'ARRAY');
1140 map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1141 if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1146 show => !$options{"hide_" . $_->[1]} && scalar @{ $_->[0] }
1148 [ \@FORMNAME, 'formname', ],
1149 [ \@LANGUAGE_ID, 'language_id', ],
1150 [ \@FORMAT, 'format', ],
1151 [ \@SENDMODE, 'sendmode', ],
1152 [ \@MEDIA, 'media', ],
1153 [ \@PRINTER_ID, 'printer_id', ];
1155 my %dont_display_groupitems = (
1159 my %template_vars = (
1160 display_copies => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
1161 display_remove_draft => (!$form->{id} && $form->{draft_id}),
1162 display_groupitems => !$dont_display_groupitems{$form->{type}},
1163 groupitems_checked => $form->{groupitems} ? "checked" : '',
1164 remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1167 my $print_options = $form->parse_html_template("generic/print_options", { SELECTS => \@SELECTS, %template_vars } );
1169 if ($options{inline}) {
1170 $main::lxdebug->leave_sub();
1171 return $print_options;
1174 print $print_options;
1176 $main::lxdebug->leave_sub();
1180 $main::lxdebug->enter_sub();
1182 my $form = $main::form;
1183 my $locale = $main::locale;
1187 if ($form->{print_nextsub}) {
1188 call_sub($form->{print_nextsub});
1189 $main::lxdebug->leave_sub();
1193 # if this goes to the printer pass through
1195 if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1196 $form->error($locale->text('Select postscript or PDF!'))
1197 if ($form->{format} !~ /(postscript|pdf)/);
1199 $old_form = new Form;
1200 map { $old_form->{$_} = $form->{$_} } keys %$form;
1203 if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1204 if ($form->{formname} eq "proforma") {
1205 $form->{proforma} = 1;
1207 $form->{print_and_save} = 1;
1208 my $formname = $form->{formname};
1210 $form->{formname} = $formname;
1212 $::lxdebug->leave_sub();
1215 elsif (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)) {
1216 $form->{print_and_save} = 1;
1221 &print_form($old_form);
1223 $main::lxdebug->leave_sub();
1227 $main::lxdebug->enter_sub();
1229 my $form = $main::form;
1230 my %myconfig = %main::myconfig;
1231 my $locale = $main::locale;
1235 my $defaults = SL::DB::Default->get;
1236 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1237 $form->{templates} = $defaults->templates;
1239 my ($old_form) = @_;
1243 my $numberfld = "invnumber";
1247 ($form->{display_form}) ? $form->{display_form} : "display_form";
1249 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1250 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1252 if ($form->{formname} eq "invoice") {
1253 $form->{label} = $locale->text('Invoice');
1255 if ($form->{formname} eq 'sales_order') {
1258 $form->{"${inv}date"} = $form->{transdate};
1259 $form->{label} = $locale->text('Confirmation');
1260 $numberfld = "sonumber";
1264 if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1267 $form->{"${inv}date"} = $form->{invdate};
1268 $form->{label} = $locale->text('Proforma Invoice');
1269 $numberfld = "sonumber";
1273 if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1276 $form->{"${inv}date"} = $form->{transdate};
1277 $form->{"invdate"} = $form->{transdate};
1278 $form->{invnumber} = $form->{ordnumber};
1279 $form->{label} = $locale->text('Proforma Invoice');
1280 $numberfld = "sonumber";
1284 if ($form->{formname} eq 'purchase_order') {
1287 $form->{"${inv}date"} = $form->{transdate};
1288 $form->{label} = $locale->text('Purchase Order');
1289 $numberfld = "ponumber";
1292 if ($form->{formname} eq 'bin_list') {
1295 $form->{"${inv}date"} = $form->{transdate};
1296 $form->{label} = $locale->text('Bin List');
1299 if ($form->{formname} eq 'sales_quotation') {
1302 $form->{"${inv}date"} = $form->{transdate};
1303 $form->{label} = $locale->text('Quotation');
1304 $numberfld = "sqnumber";
1308 if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1311 $form->{"${inv}date"} = $form->{transdate};
1312 $form->{"invdate"} = $form->{transdate};
1313 $form->{label} = $locale->text('Proforma Invoice');
1314 $numberfld = "sqnumber";
1318 if ($form->{formname} eq 'request_quotation') {
1321 $form->{"${inv}date"} = $form->{transdate};
1322 $form->{label} = $locale->text('RFQ');
1323 $numberfld = "rfqnumber";
1327 if ($form->{type} =~ /_delivery_order$/) {
1330 $form->{"${inv}date"} = $form->{transdate};
1331 $numberfld = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
1332 $form->{label} = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
1335 $form->isblank("email", $locale->text('E-mail address missing!'))
1336 if ($form->{media} eq 'email');
1337 $form->isblank("${inv}date",
1338 $locale->text($form->{label})
1340 . $locale->text(' Date missing!'));
1342 # $locale->text('Invoice Number missing!')
1343 # $locale->text('Invoice Date missing!')
1344 # $locale->text('Order Number missing!')
1345 # $locale->text('Order Date missing!')
1346 # $locale->text('Quotation Number missing!')
1347 # $locale->text('Quotation Date missing!')
1349 $form->{what_done} = $form->{formname};
1353 # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1354 my ($saved_email, $saved_cc, $saved_bcc) =
1355 ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1357 my $language_saved = $form->{language_id};
1358 my $payment_id_saved = $form->{payment_id};
1359 my $delivery_term_id_saved = $form->{delivery_term_id};
1360 my $salesman_id_saved = $form->{salesman_id};
1361 my $cp_id_saved = $form->{cp_id};
1362 my $taxzone_id_saved = $form->{taxzone_id};
1363 my $currency_saved = $form->{currency};
1365 call_sub("$form->{vc}_details") if ($form->{vc});
1367 $form->{language_id} = $language_saved;
1368 $form->{payment_id} = $payment_id_saved;
1369 $form->{delivery_term_id} = $delivery_term_id_saved;
1370 $form->{taxzone_id} = $taxzone_id_saved;
1371 $form->{currency} = $currency_saved;
1373 $form->{"email"} = $saved_email if ($saved_email);
1374 $form->{"cc"} = $saved_cc if ($saved_cc);
1375 $form->{"bcc"} = $saved_bcc if ($saved_bcc);
1377 if (!$cp_id_saved) {
1378 # No contact was selected. Delete all contact variables because
1379 # IS->customer_details() and IR->vendor_details() get the default
1381 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1384 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1385 if ($form->{"language_id"}) {
1386 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1387 AM->get_language_details(\%myconfig, $form, $form->{language_id});
1389 $output_dateformat = $myconfig{"dateformat"};
1390 $output_numberformat = $myconfig{"numberformat"};
1391 $output_longdates = 1;
1394 # Store the output number format so that the template modules know
1395 # how to parse the amounts back if requested.
1396 $myconfig{output_numberformat} = $output_numberformat || $myconfig{numberformat};
1398 ($form->{employee}) = split /--/, $form->{employee};
1400 # create the form variables
1401 if ($form->{type} =~ /_delivery_order$/) {
1402 DO->order_details(\%myconfig, \%$form);
1404 OE->order_details(\%myconfig, \%$form);
1406 IS->invoice_details(\%myconfig, \%$form, $locale);
1409 $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
1410 $form->get_employee_data('prefix' => 'salesman', 'id' => $salesman_id_saved);
1412 if ($form->{shipto_id}) {
1413 $form->get_shipto(\%myconfig);
1416 my @a = qw(name department_1 department_2 street zipcode city country contact phone fax email);
1420 # if there is no shipto fill it in from billto
1421 foreach my $item (@a) {
1422 if ($form->{"shipto$item"}) {
1429 if ( $form->{formname} eq 'purchase_order'
1430 || $form->{formname} eq 'request_quotation') {
1431 $form->{shiptoname} = $defaults->company;
1432 $form->{shiptostreet} = $defaults->address;
1434 map { $form->{"shipto$_"} = $form->{$_} } @a;
1438 $form->{notes} =~ s/^\s+//g;
1440 delete $form->{printer_command};
1442 $form->{language} = $form->get_template_language(\%myconfig);
1445 if ($form->{media} ne 'email') {
1446 $printer_code = $form->get_printer_code(\%myconfig);
1447 if ($printer_code ne "") {
1448 $printer_code = "_" . $printer_code;
1452 if ($form->{language} ne "") {
1453 my $template_arrays = $form->{TEMPLATE_ARRAYS} || $form;
1454 map { $template_arrays->{unit}->[$_] = AM->translate_units($form, $form->{language}, $template_arrays->{unit}->[$_], $template_arrays->{qty}->[$_]); } (0..scalar(@{ $template_arrays->{unit} }) - 1);
1456 $form->{language} = "_" . $form->{language};
1460 format_dates($output_dateformat, $output_longdates,
1461 qw(invdate orddate quodate pldate duedate reqdate transdate
1462 shippingdate deliverydate validitydate paymentdate
1463 datepaid transdate_oe deliverydate_oe dodate
1464 employee_startdate employee_enddate
1466 grep({ /^datepaid_\d+$/ ||
1467 /^transdate_oe_\d+$/ ||
1468 /^deliverydate_oe_\d+$/ ||
1470 /^deliverydate_\d+$/ ||
1474 reformat_numbers($output_numberformat, 2,
1475 qw(invtotal ordtotal quototal subtotal linetotal
1476 listprice sellprice netprice discount
1477 tax taxbase total paid payment),
1478 grep({ /^(?:linetotal|nodiscount_linetotal|listprice|sellprice|netprice|taxbase|discount|p_discount|discount_sub|nodiscount_sub|paid|subtotal|total|tax)_\d+$/ } keys(%{$form})));
1480 reformat_numbers($output_numberformat, undef,
1481 qw(qty price_factor),
1485 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1487 if (scalar @{ $cvar_date_fields }) {
1488 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1491 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1492 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1495 my $extension = 'html';
1496 if ($form->{format} eq 'postscript') {
1497 $form->{postscript} = 1;
1500 } elsif ($form->{"format"} =~ /pdf/) {
1502 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
1504 } elsif ($form->{"format"} =~ /opendocument/) {
1505 $form->{opendocument} = 1;
1507 } elsif ($form->{"format"} =~ /excel/) {
1512 # search for the template
1514 push @template_files, "$form->{formname}_email$form->{language}$printer_code.$extension" if $form->{media} eq 'email';
1515 push @template_files, "$form->{formname}$form->{language}$printer_code.$extension";
1516 push @template_files, "$form->{formname}.$extension";
1517 push @template_files, "default.$extension";
1518 @template_files = uniq @template_files;
1519 $form->{IN} = first { -f ($defaults->templates . "/$_") } @template_files;
1521 if (!defined $form->{IN}) {
1522 $::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
1525 delete $form->{OUT};
1527 if ($form->{media} eq 'printer') {
1528 $form->{OUT} = $form->{printer_command};
1529 $form->{OUT_MODE} = '|-';
1530 $form->{printed} .= " $form->{formname}";
1531 $form->{printed} =~ s/^ //;
1533 my $printed = $form->{printed};
1535 if ($form->{media} eq 'email') {
1536 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1537 unless $form->{subject};
1539 $form->{emailed} .= " $form->{formname}";
1540 $form->{emailed} =~ s/^ //;
1542 my $emailed = $form->{emailed};
1544 if ($form->{media} eq 'queue') {
1545 my %queued = map { s|.*[/\\]||; $_ } split / /, $form->{queued};
1548 my $suffix = ($form->{postscript}) ? '.ps' : '.pdf';
1549 if ($filename = $queued{ $form->{formname} }) {
1550 unlink $::lx_office_conf{paths}->{spool} . "/$filename";
1551 delete $queued{ $form->{formname} };
1553 $form->{queued} = join ' ', %queued;
1554 $filename =~ s/\..*$//g;
1555 $filename .= $suffix;
1556 $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
1557 $form->{OUT_MODE} = '>';
1561 ($temp_fh, $filename) = File::Temp::tempfile(
1562 'kivitendo-spoolXXXXXX',
1563 SUFFIX => "$suffix",
1564 DIR => $::lx_office_conf{paths}->{spool},
1568 $form->{OUT} = "$filename";
1569 # use >> for OUT_MODE because file is already created by File::Temp
1570 $form->{OUT_MODE} = '>>';
1571 # strip directory so that only filename is stored in table status
1572 ($filename) = $filename =~ /^$::lx_office_conf{paths}->{spool}\/(.*)/;
1576 $form->{queued} .= " $form->{formname} $filename";
1577 $form->{queued} =~ s/^ //;
1579 my $queued = $form->{queued};
1581 # saving the history
1582 if(!exists $form->{addition}) {
1583 $form->{snumbers} = "${inv}number" . "_" . $form->{"${inv}number"};
1584 if($form->{media} =~ /printer/) {
1585 $form->{addition} = "PRINTED";
1587 elsif($form->{media} =~ /email/) {
1588 $form->{addition} = "MAILED";
1590 elsif($form->{media} =~ /queue/) {
1591 $form->{addition} = "QUEUED";
1593 elsif($form->{media} =~ /screen/) {
1594 $form->{addition} = "SCREENED";
1596 $form->save_history;
1598 # /saving the history
1600 # prepare meta information for template introspection
1601 $form->{template_meta} = {
1602 formname => $form->{formname},
1603 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id} || undef),
1604 format => $form->{format},
1605 media => $form->{media},
1606 extension => $extension,
1607 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id} || undef),
1608 today => DateTime->today,
1611 $form->parse_template(\%myconfig);
1613 $form->{callback} = "";
1615 if ($form->{media} eq 'email') {
1616 $form->{message} = $locale->text('sent') unless $form->{message};
1618 my $message = $form->{message};
1620 # if we got back here restore the previous form
1621 if ($form->{media} =~ /(printer|email|queue)/) {
1623 $form->update_status(\%myconfig)
1624 if ($form->{media} eq 'queue' && $form->{id});
1626 return $main::lxdebug->leave_sub() if ($old_form eq "return");
1630 $old_form->{"${inv}number"} = $form->{"${inv}number"};
1632 # restore and display form
1633 map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1635 $form->{queued} = $queued;
1636 $form->{printed} = $printed;
1637 $form->{emailed} = $emailed;
1638 $form->{message} = $message;
1640 $form->{rowcount}--;
1641 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1642 qw(exchangerate creditlimit creditremaining);
1644 for my $i (1 .. $form->{paidaccounts}) {
1646 $form->{"${_}_$i"} =
1647 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1648 } qw(paid exchangerate);
1651 call_sub($display_form);
1656 ($form->{media} eq 'printer')
1657 ? $locale->text('sent to printer')
1658 : $locale->text('emailed to') . " $form->{email}";
1659 $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1661 if ($form->{printing}) {
1662 call_sub($display_form);
1666 $main::lxdebug->leave_sub();
1669 sub customer_details {
1670 $main::lxdebug->enter_sub();
1672 my $form = $main::form;
1673 my %myconfig = %main::myconfig;
1675 IS->customer_details(\%myconfig, \%$form, @_);
1677 $main::lxdebug->leave_sub();
1680 sub vendor_details {
1681 $main::lxdebug->enter_sub();
1683 my $form = $main::form;
1684 my %myconfig = %main::myconfig;
1686 IR->vendor_details(\%myconfig, \%$form, @_);
1688 $main::lxdebug->leave_sub();
1692 $main::lxdebug->enter_sub();
1694 my $form = $main::form;
1698 $form->{postasnew} = 1;
1699 map { delete $form->{$_} } qw(printed emailed queued);
1703 $main::lxdebug->leave_sub();
1707 $main::lxdebug->enter_sub();
1711 $::form->{print_and_post} = 0 if $::form->{second_run};
1713 map { $::form->{$_} = $::form->parse_amount(\%::myconfig, $::form->{$_}) } qw(exchangerate creditlimit creditremaining);
1715 # get details for customer/vendor
1716 call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country contact email phone fax), $::form->{vc} . "number");
1718 # get pricegroups for parts
1719 IS->get_pricegroups_for_parts(\%::myconfig, \%$::form);
1721 # build up html code for prices_$i
1722 set_pricegroup($::form->{rowcount});
1724 $::form->{rowcount}--;
1726 my @shipto_vars = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1727 shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
1728 shiptodepartment_1 shiptodepartment_2);
1729 my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
1730 $::form->{title} = $::locale->text('Ship to');
1733 print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
1734 nextsub => $::form->{display_form} || 'display_form',
1737 $main::lxdebug->leave_sub();
1740 sub ship_to_entered {
1741 $::auth->restore_form_from_session(delete $::form->{previousform});
1742 call_sub($::form->{nextsub});
1745 sub relink_accounts {
1746 $main::lxdebug->enter_sub();
1748 my $form = $main::form;
1749 my %myconfig = %main::myconfig;
1753 $form->{"taxaccounts"} =~ s/\s*$//;
1754 $form->{"taxaccounts"} =~ s/^\s*//;
1755 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1756 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1758 $form->{"taxaccounts"} = "";
1760 IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
1762 $main::lxdebug->leave_sub();
1766 $main::lxdebug->enter_sub();
1768 my $form = $main::form;
1769 my %myconfig = %main::myconfig;
1773 my $invdate = $form->{invdate} eq 'undefined' ? undef : $form->{invdate};
1774 my $duedate = $form->get_duedate(\%myconfig, $invdate);
1776 print $form->ajax_response_header() . ($duedate || $invdate);
1778 $main::lxdebug->leave_sub();
1781 sub _update_part_information {
1782 $main::lxdebug->enter_sub();
1784 my $form = $main::form;
1786 my %part_information = IC->get_basic_part_info('id' => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
1787 'vendor_id' => $form->{vendor_id});
1789 $form->{PART_INFORMATION} = \%part_information;
1791 foreach my $i (1..$form->{rowcount}) {
1792 next unless ($form->{"id_${i}"});
1794 my $info = $form->{PART_INFORMATION}->{$form->{"id_${i}"}} || { };
1795 $form->{"partunit_${i}"} = $info->{unit};
1796 $form->{"weight_$i"} = $info->{weight};
1799 $main::lxdebug->leave_sub();
1803 $main::lxdebug->enter_sub();
1805 my $form = $main::form;
1806 my %myconfig = %main::myconfig;
1808 if (!$form->{ordnumber} || !$form->{id}) {
1809 map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
1810 $main::lxdebug->leave_sub();
1814 my $all_units = AM->retrieve_all_units();
1816 my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales',
1817 'oe_id' => $form->{id},);
1819 foreach my $i (1..$form->{rowcount}) {
1820 next unless ($form->{"id_${i}"});
1822 $form->{"ship_$i"} = 0;
1824 my $ship_entry = $ship{$form->{"id_$i"}};
1826 next if (!$ship_entry || ($ship_entry->{qty} <= 0));
1829 ($form->{simple_save} ? $form->{"qty_$i"} : $form->parse_amount(\%myconfig, $form->{"qty_$i"}))
1830 * $all_units->{$form->{"unit_$i"}}->{factor}
1831 / $all_units->{$form->{"partunit_$i"}}->{factor};
1833 $form->{"ship_$i"} = min($rowqty, $ship_entry->{qty});
1834 $ship_entry->{qty} -= $form->{"ship_$i"};
1837 foreach my $i (1..$form->{rowcount}) {
1838 next unless ($form->{"id_${i}"});
1840 my $ship_entry = $ship{$form->{"id_$i"}};
1842 next if (!$ship_entry || ($ship_entry->{qty} <= 0.01));
1844 $form->{"ship_$i"} += $ship_entry->{qty};
1845 $ship_entry->{qty} = 0;
1848 $main::lxdebug->leave_sub();
1851 sub _update_custom_variables {
1852 $main::lxdebug->enter_sub();
1854 my $form = $main::form;
1856 $form->{CVAR_CONFIGS} = { } unless ref $form->{CVAR_CONFIGS} eq 'HASH';
1857 $form->{CVAR_CONFIGS}->{IC} ||= CVar->get_configs(module => 'IC');
1859 $main::lxdebug->leave_sub();
1862 sub _render_custom_variables_inputs {
1863 $main::lxdebug->enter_sub(2);
1865 my $form = $main::form;
1869 if (!$form->{CVAR_CONFIGS}->{IC}) {
1870 $main::lxdebug->leave_sub();
1874 my $valid = CVar->custom_variables_validity_by_trans_id(trans_id => $params{part_id});
1876 my $num_visible_cvars = 0;
1877 foreach my $cvar (@{ $form->{CVAR_CONFIGS}->{IC} }) {
1878 $cvar->{valid} = $params{part_id} && $valid->($cvar->{id});
1880 my $description = '';
1881 if ($cvar->{flag_editable} && $cvar->{valid}) {
1882 $num_visible_cvars++;
1883 $description = $cvar->{description} . ' ';
1886 my $form_key = "ic_cvar_" . $cvar->{name} . "_$params{row}";
1888 push @{ $params{ROW2} }, {
1889 line_break => $num_visible_cvars == 1,
1890 description => $description,
1893 hide_non_editable => 1,
1895 name_prefix => 'ic_',
1896 name_postfix => "_$params{row}",
1897 valid => $cvar->{valid},
1898 value => CVar->parse($::form->{$form_key}, $cvar),
1903 $main::lxdebug->leave_sub(2);
1906 sub _remove_billed_or_delivered_rows {
1909 croak "Missing parameter 'quantities'" if !$params{quantities};
1911 my @fields = map { s/_1$//; $_ } grep { m/_1$/ } keys %{ $::form };
1914 my $make_key = sub {
1916 return $::form->{"id_${row}"} unless $::form->{"serialnumber_${row}"};
1917 my $key = $::form->{"id_${row}"} . ':' . $::form->{"serialnumber_${row}"};
1918 return exists $params{quantities}->{$key} ? $key : $::form->{"id_${row}"};
1921 my $removed_rows = 0;
1923 while ($row < $::form->{rowcount}) {
1925 next unless $::form->{"id_$row"};
1927 my $parts_id = $::form->{"id_$row"};
1928 my $base_qty = $::form->parse_amount(\%::myconfig, $::form->{"qty_$row"}) * SL::DB::Manager::Unit->find_by(name => $::form->{"unit_$row"})->base_factor;
1930 my $key = $make_key->($row);
1931 my $sub_qty = min($base_qty, $params{quantities}->{$key});
1932 $params{quantities}->{$key} -= $sub_qty;
1934 if (!$sub_qty || ($sub_qty != $base_qty)) {
1935 $::form->{"qty_${row}"} = $::form->format_amount(\%::myconfig, ($base_qty - $sub_qty) / SL::DB::Manager::Unit->find_by(name => $::form->{"unit_$row"})->base_factor);
1936 push @new_rows, { map { $_ => $::form->{"${_}_${row}"} } @fields };
1943 $::form->redo_rows(\@fields, \@new_rows, scalar(@new_rows), $::form->{rowcount});
1944 $::form->{rowcount} -= $removed_rows;