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::Util qw(min max first);
49 require "bin/mozilla/common.pl";
53 # any custom scripts for this one
54 if (-f "bin/mozilla/custom_io.pl") {
55 eval { require "bin/mozilla/custom_io.pl"; };
57 if (-f "bin/mozilla/$::form->{login}_io.pl") {
58 eval { require "bin/mozilla/$::form->{login}_io.pl"; };
65 # this is for our long dates
66 # $locale->text('January')
67 # $locale->text('February')
68 # $locale->text('March')
69 # $locale->text('April')
70 # $locale->text('May ')
71 # $locale->text('June')
72 # $locale->text('July')
73 # $locale->text('August')
74 # $locale->text('September')
75 # $locale->text('October')
76 # $locale->text('November')
77 # $locale->text('December')
79 # this is for our short month
80 # $locale->text('Jan')
81 # $locale->text('Feb')
82 # $locale->text('Mar')
83 # $locale->text('Apr')
84 # $locale->text('May')
85 # $locale->text('Jun')
86 # $locale->text('Jul')
87 # $locale->text('Aug')
88 # $locale->text('Sep')
89 # $locale->text('Oct')
90 # $locale->text('Nov')
91 # $locale->text('Dec')
98 $main::auth->assert('part_service_assembly_edit | vendor_invoice_edit | sales_order_edit | invoice_edit |' .
99 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | ' .
100 'purchase_delivery_order_edit | sales_delivery_order_edit');
103 ########################################
104 # Eintrag fuer Version 2.2.0 geaendert #
105 # neue Optik im Rechnungsformular #
106 ########################################
108 $main::lxdebug->enter_sub();
112 my $form = $main::form;
113 my %myconfig = %main::myconfig;
114 my $locale = $main::locale;
115 my $cgi = $::request->{cgi};
119 my ($stock_in_out, $stock_in_out_title);
121 my $is_purchase = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
122 my $show_min_order_qty = first { $_ eq $form->{type} } qw(request_quotation purchase_order);
123 my $is_delivery_order = $form->{type} =~ /_delivery_order$/;
124 my $is_s_p_order = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
126 if ($is_delivery_order) {
127 if ($form->{type} eq 'sales_delivery_order') {
128 $stock_in_out_title = $locale->text('Release From Stock');
129 $stock_in_out = 'out';
131 $stock_in_out_title = $locale->text('Transfer To Stock');
132 $stock_in_out = 'in';
135 retrieve_partunits();
139 my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
141 { id => 'runningnumber', width => 5, value => $locale->text('No.'), display => 1, },
142 { id => 'partnumber', width => 8, value => $locale->text('Number'), display => 1, },
143 { id => 'description', width => 30, value => $locale->text('Part Description'), display => 1, },
144 { id => 'ship', width => 5, value => $locale->text('Delivered'), display => $is_s_p_order, },
145 { id => 'qty', width => 5, value => $locale->text('Qty'), display => 1, },
146 { id => 'price_factor', width => 5, value => $locale->text('Price Factor'), display => !$is_delivery_order, },
147 { id => 'unit', width => 5, value => $locale->text('Unit'), display => 1, },
148 { id => 'serialnr', width => 10, value => $locale->text('Serial No.'), display => 0, },
149 { id => 'projectnr', width => 10, value => $locale->text('Project'), display => 0, },
150 { id => 'sellprice', width => 15, value => $locale->text('Price'), display => !$is_delivery_order, },
151 { id => 'sellprice_pg', width => 8, value => $locale->text('Pricegroup'), display => !$is_delivery_order && !$is_purchase, },
152 { id => 'discount', width => 5, value => $locale->text('Discount'), display => !$is_delivery_order, },
153 { id => 'linetotal', width => 10, value => $locale->text('Extended'), display => !$is_delivery_order, },
154 { id => 'bin', width => 10, value => $locale->text('Bin'), display => 0, },
155 { id => 'stock_in_out', width => 10, value => $stock_in_out_title, display => $is_delivery_order, },
157 my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
161 my $all_units = AM->retrieve_units(\%myconfig, $form);
163 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
165 my $colspan = scalar @column_index;
167 $form->{invsubtotal} = 0;
168 map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
171 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
172 $form->{show_details} = $myconfig{show_form_details} unless (defined($form->{show_details}));
175 # translations, unused commented out
176 # $runningnumber = $locale->text('No.');
177 # my $deliverydate = $locale->text('Delivery Date');
178 my $serialnumber = $locale->text('Serial No.');
179 my $projectnumber = $locale->text('Project');
180 # $partsgroup = $locale->text('Group');
181 my $reqdate = $locale->text('Reqdate');
182 my $deliverydate = $locale->text('Required by');
185 my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out);
186 my %nowrap = map { $_ => 1 } qw(description unit);
188 $form->{marge_total} = 0;
189 $form->{sellprice_total} = 0;
190 $form->{lastcost_total} = 0;
191 my %projectnumber_labels = ();
192 my @projectnumber_values = ("");
194 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
195 push(@projectnumber_values, $item->{"id"});
196 $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
199 _update_part_information();
200 _update_ship() if ($is_s_p_order);
201 _update_custom_variables();
206 for my $i (1 .. $numrows) {
207 my %column_data = ();
210 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
211 qw(qty discount sellprice lastcost price_new price_old)
212 unless ($form->{simple_save});
215 $form->{"unit_old_$i"} ||= $form->{"unit_$i"};
216 $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
218 if ( !$all_units->{$form->{"selected_unit_$i"}} # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
219 || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
220 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"}; # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
222 # adjust prices by unit, ignore if pricegroup changed
223 if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
224 $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
225 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
227 my $this_unit = $form->{"unit_$i"};
228 $this_unit = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
230 if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
231 my @values = ('', map { $_->{id} } @{ $form->{ALL_PRICE_FACTORS} });
232 my %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
234 $column_data{price_factor} =
235 NTI($cgi->popup_menu('-name' => "price_factor_id_$i",
236 '-default' => $form->{"price_factor_id_$i"},
237 '-values' => \@values,
238 '-labels' => \%labels,
239 '-style' => 'width:90px'));
241 $column_data{price_factor} = ' ';
244 $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
247 my $decimalplaces = ($form->{"sellprice_$i"} =~ /\.(\d+)/) ? max 2, length $1 : 2;
249 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || 1;
250 my $discount = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100 / $price_factor, 2);
251 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
252 my $rows = $form->numtextrows($form->{"description_$i"}, 30, 6);
254 $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5, -value => $i); # HuT
255 $column_data{partnumber} = $cgi->textfield(-name => "partnumber_$i", -size => 12, -value => $form->{"partnumber_$i"});
256 $column_data{description} = (($rows > 1) # if description is too large, use a textbox instead
257 ? $cgi->textarea( -name => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
258 : $cgi->textfield(-name => "description_$i", -size => 30, -value => $form->{"description_$i"}))
259 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
261 my $qty_dec = ($form->{"qty_$i"} =~ /\.(\d+)/) ? length $1 : 2;
263 $column_data{qty} = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
264 $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
265 . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
266 if $form->{"formel_$i"};
268 $column_data{ship} = '';
269 if ($form->{"id_$i"}) {
270 my $ship_qty = $form->{"ship_$i"} * 1;
271 $ship_qty *= $all_units->{$form->{"partunit_$i"}}->{factor};
272 $ship_qty /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
274 $column_data{ship} = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
277 # build in drop down list for pricesgroups
278 # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
279 # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
280 # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
281 # jetzt nach der Abfrage.
283 if ($form->{"prices_$i"}) {
284 $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
285 $sellprice_value =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
286 ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
287 : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
289 # for last row and report
290 # set pricegroup drop down list from report menu
291 if ($form->{"sellprice_$i"} != 0) {
292 # remember the pricegroup_id in pricegroup_old
293 # but don't overwrite it
294 $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
295 my $default_option = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
296 $column_data{sellprice_pg} = NTI($cgi->popup_menu("sellprice_pg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
298 $column_data{sellprice_pg} = qq| |;
300 $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
303 # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
304 # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
305 # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
307 my $edit_prices = $main::auth->assert('edit_prices', 1);
308 $column_data{sellprice} = (!$edit_prices)
309 ? $cgi->textfield(-readonly => "readonly",
310 -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
311 : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
312 $column_data{discount} = (!$edit_prices)
313 ? $cgi->textfield(-readonly => "readonly",
314 -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
315 : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
316 $column_data{linetotal} = $form->format_amount(\%myconfig, $linetotal, 2);
317 $column_data{bin} = $form->{"bin_$i"};
319 if ($is_delivery_order) {
320 $column_data{stock_in_out} = calculate_stock_in_out($i);
323 my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
327 push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
328 if $form->{type} !~ /_quotation/;
329 push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name' => "project_id_$i", '-values' => \@projectnumber_values,
330 '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
331 push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
332 if ($form->{type} =~ /order/ || $form->{type} =~ /invoice/);
333 push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
334 $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
336 # begin marge calculations
337 $form->{"lastcost_$i"} *= 1;
338 $form->{"marge_percent_$i"} = 0;
341 my $real_sellprice = $linetotal;
342 my $real_lastcost = $form->{"lastcost_$i"} * $form->{"qty_$i"} / ( $form->{"marge_price_factor_$i"} || 1 );
343 my $marge_percent_warn = $myconfig{marge_percent_warn} * 1 || 15;
344 my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
346 if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
347 $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
348 $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
351 $form->{"marge_absolut_$i"} = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
352 $form->{"marge_total"} += $form->{"marge_absolut_$i"};
353 $form->{"lastcost_total"} += $real_lastcost;
354 $form->{"sellprice_total"} += $real_sellprice;
356 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
358 push @ROW2, { value => sprintf qq|
359 <font %s><b>%s</b> %s %s%% </font>
361 <b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
362 $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
363 $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
364 $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
365 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
367 $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
368 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/) ;
369 # / marge calculations ending
372 if ($form->{"id_$i"}) {
373 my $part = IC->get_basic_part_info(id => $form->{"id_$i"});
374 my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
375 push @ROW2, { value => sprintf "<b>%s</b> <font %s>%s %s</font>",
376 $locale->text('On Hand'),
378 $form->format_amount(\%myconfig, $part->{onhand}, 2),
386 if ($is_delivery_order) {
387 map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
388 push @hidden_vars, qw(sellprice discount not_discountable price_factor_id lastcost);
389 push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
392 my @HIDDENS = map { value => $_}, (
393 $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
394 $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
395 map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
396 (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
397 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
398 longdescription basefactor marge_absolut marge_percent marge_price_factor), @hidden_vars)
401 map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
403 $form->{invsubtotal} += $linetotal;
405 # Benutzerdefinierte Variablen für Waren/Dienstleistungen/Erzeugnisse
406 _render_custom_variables_inputs(ROW2 => \@ROW2, row => $i, part_id => $form->{"id_$i"});
408 push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
411 print $form->parse_html_template('oe/sales_order', { ROWS => \@ROWS,
415 if (0 != ($form->{sellprice_total} * 1)) {
416 $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
419 $main::lxdebug->leave_sub();
422 ##################################################
423 # build html-code for pricegroups in variable $form->{prices_$j}
426 $main::lxdebug->enter_sub();
428 my $form = $main::form;
429 my $locale = $main::locale;
430 my $cgi = $::request->{cgi};
434 my $rowcount = shift;
435 for my $j (1 .. $rowcount) {
436 next unless $form->{PRICES}{$j};
437 # build drop down list for pricegroups
438 my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
439 $form->{"prices_$j"} = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} }
440 (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
442 foreach my $item (@{ $form->{PRICES}{$j} }) {
443 # set new selectedpricegroup_id and prices for "Preis"
444 $form->{"pricegroup_old_$j"} = $item->{pricegroup_id} if $item->{selected} && $item->{pricegroup_id};
445 $form->{"sellprice_$j"} = $item->{price} if $item->{selected} && $item->{pricegroup_id};
446 $form->{"price_new_$j"} = $form->{"sellprice_$j"} if $item->{selected} || !$item->{pricegroup_id};
449 $main::lxdebug->leave_sub();
453 $main::lxdebug->enter_sub();
456 my $mode = $params{mode} || croak "Missing parameter 'mode'";
460 my $previous_form = $::auth->save_form_in_session(form => $::form);
461 $::form->{title} = $::locale->text('Select from one of the items below');
464 my @item_list = map {
465 $_->{display_sellprice} = $_->{sellprice} * (1 - $::form->{tradediscount});
466 $_->{display_sellprice} /= $_->{price_factor} if ($_->{price_factor});
468 } @{ $::form->{item_list} };
470 # delete action variable
471 delete @{$::form}{qw(action item_list header)};
473 print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
475 ITEM_LIST => \@item_list,
476 IS_PURCHASE => $mode eq 'IS' });
478 $main::lxdebug->leave_sub();
482 $main::lxdebug->enter_sub();
484 my $form = $main::form;
485 my %myconfig = %main::myconfig;
489 $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
491 my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
492 my $id = delete($form->{select_item_id}) || croak 'Missing item selection ID';
493 my $i = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
495 $form->{"id_${i}"} = $id;
498 IS->retrieve_item(\%myconfig, \%$form);
499 } elsif ($mode eq 'IR') {
500 IR->retrieve_item(\%myconfig, \%$form);
501 } elsif ($mode eq 'IC') {
502 IC->assembly_item(\%myconfig, \%$form);
504 croak "Invalid item selection mode '${mode}'";
507 my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
509 # if there was a price entered, override it
510 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
513 qw(id partnumber description sellprice listprice inventory_accno
514 income_accno expense_accno bin unit weight assembly taxaccounts
515 partsgroup formel longdescription not_discountable partnotes lastcost
516 price_factor_id price_factor);
518 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
519 push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
521 map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
523 $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
525 if ($form->{"part_payment_id_$i"} ne "") {
526 $form->{payment_id} = $form->{"part_payment_id_$i"};
529 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
531 my $decimalplaces = ($dec > 2) ? $dec : 2;
534 $form->{"sellprice_$i"} = $sellprice;
537 # if there is an exchange rate adjust sellprice
538 if (($form->{exchangerate} * 1) != 0) {
539 $form->{"sellprice_$i"} /= $form->{exchangerate};
540 $form->{"sellprice_$i"} =
541 $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
545 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
546 qw(sellprice listprice weight);
548 $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
549 $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
551 if ($form->{"not_discountable_$i"}) {
552 $form->{"discount_$i"} = 0;
556 $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
558 map { $form->{"${_}_base"} += $amount }
559 (split / /, $form->{"taxaccounts_$i"});
560 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
561 $form->{"taxaccounts_$i"}
562 if !$form->{taxincluded};
564 $form->{creditremaining} -= $amount;
566 $form->{"runningnumber_$i"} = $i;
568 delete $form->{nextsub};
573 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
574 } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
576 # get pricegroups for parts
577 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
579 # build up html code for prices_$i
580 set_pricegroup($form->{rowcount});
584 $main::lxdebug->leave_sub();
588 $main::lxdebug->enter_sub();
590 my $form = $main::form;
591 my %myconfig = %main::myconfig;
595 my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
598 $form->{old_callback} = $form->escape($form->{callback}, 1);
599 $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
601 # save all form variables except action in the session and keep the key in the previousform variable
602 my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
605 push @HIDDENS, { 'name' => 'previousform', 'value' => $previousform };
606 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(rowcount vc);
607 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
608 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
609 push @HIDDENS, { 'name' => $price_key, 'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
610 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{"longdescription_$form->{rowcount}"} };
613 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
615 $main::lxdebug->leave_sub();
619 $main::lxdebug->enter_sub();
621 my $form = $main::form;
622 my %myconfig = %main::myconfig;
629 # remove any makes or model rows
630 if ($form->{item} eq 'part') {
631 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
632 qw(listprice sellprice lastcost weight rop);
634 } elsif ($form->{item} eq 'assembly') {
636 # fuer assemblies auskommentiert. seiteneffekte? ;-) wird die woanders benoetigt?
637 #$form->{sellprice} = 0;
639 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
640 qw(listprice sellprice rop stock);
642 my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
644 for my $i (1 .. ($form->{assembly_rows} - 1)) {
645 if ($form->{"qty_$i"}) {
649 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
651 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
653 #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
655 # fuer assemblies auskommentiert. siehe oben
656 # $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
657 $form->{weight} += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
661 # kann das hier auch weg? s.o. jb
662 $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
664 $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
665 $form->{assembly_rows} = $count;
667 } elsif ($form->{item} eq 'service') {
668 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
671 remove_emptied_rows(1);
673 $form->{creditremaining} -= &invoicetotal;
678 if ( $form->{type} =~ (/sales_quotation/)
679 or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
680 or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
681 or ($form->{type} =~ /sales_order/)) {
683 # get pricegroups for parts
684 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
686 # build up html code for prices_$i
687 set_pricegroup($form->{rowcount});
693 $main::lxdebug->leave_sub();
696 sub remove_emptied_rows {
697 my $dont_add_empty = shift;
700 return unless $form->{rowcount};
702 my @flds = qw(id partnumber description qty ship sellprice unit
703 discount inventory_accno income_accno expense_accno listprice
704 taxaccounts bin assembly weight projectnumber project_id
705 oldprojectnumber runningnumber serialnumber partsgroup payment_id
706 not_discountable shop ve gv buchungsgruppen_id language_values
707 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
708 transdate longdescription basefactor marge_total marge_percent
709 marge_price_factor lastcost price_factor_id partnotes
710 stock_out stock_in has_sernumber reqdate);
712 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
713 push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
716 for my $i (1 .. $form->{rowcount} - 1) {
717 next unless $form->{"partnumber_$i"};
719 push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
722 my $new_rowcount = scalar @new_rows;
723 $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
724 $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
728 $main::lxdebug->enter_sub();
730 my $form = $main::form;
731 my %myconfig = %main::myconfig;
735 $form->{oldinvtotal} = 0;
737 # add all parts and deduct paid
738 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
740 my ($amount, $sellprice, $discount, $qty);
742 for my $i (1 .. $form->{rowcount}) {
743 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
744 $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
745 $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
747 #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
749 $amount = $sellprice * (1 - $discount / 100) * $qty;
750 map { $form->{"${_}_base"} += $amount }
751 (split (/ /, $form->{"taxaccounts_$i"}));
752 $form->{oldinvtotal} += $amount;
755 map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
756 split(/ /, $form->{taxaccounts})
757 if !$form->{taxincluded};
759 $form->{oldtotalpaid} = 0;
760 for my $i (1 .. $form->{paidaccounts}) {
761 $form->{oldtotalpaid} += $form->{"paid_$i"};
764 $main::lxdebug->leave_sub();
767 return ($form->{oldinvtotal} - $form->{oldtotalpaid});
771 $main::lxdebug->enter_sub();
773 my $form = $main::form;
774 my $locale = $main::locale;
778 # check if items are valid
779 if ($form->{rowcount} == 1) {
784 for my $i (1 .. $form->{rowcount} - 1) {
785 $form->isblank("partnumber_$i",
786 $locale->text('Number missing in Row') . " $i");
789 $main::lxdebug->leave_sub();
793 $main::lxdebug->enter_sub();
795 my $form = $main::form;
796 my %myconfig = %main::myconfig;
797 my $locale = $main::locale;
801 if ($form->{second_run}) {
802 $form->{print_and_post} = 0;
804 $form->{ordnumber} = $form->{invnumber};
806 $form->{old_employee_id} = $form->{employee_id};
807 $form->{old_salesman_id} = $form->{salesman_id};
809 map { delete $form->{$_} } qw(id printed emailed queued);
811 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
812 $form->{title} = $locale->text('Add Purchase Order');
813 $form->{vc} = 'vendor';
814 $form->{type} = 'purchase_order';
817 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
818 $form->{title} = $locale->text('Add Sales Order');
819 $form->{vc} = 'customer';
820 $form->{type} = 'sales_order';
823 $form->{script} = 'oe.pl';
831 require "bin/mozilla/$form->{script}";
832 my $script = $form->{"script"};
835 $locale = new Locale($::lx_office_conf{system}->{language}, $script);
837 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
839 my $currency = $form->{currency};
843 $form->{currency} = $currency;
844 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
845 $form->{exchangerate} = $form->{forex} || '';
847 for my $i (1 .. $form->{rowcount}) {
848 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
849 if ($form->{"${_}_${i}"}) }
850 qw(ship qty sellprice listprice basefactor discount));
856 $main::lxdebug->leave_sub();
860 $main::lxdebug->enter_sub();
862 my $form = $main::form;
863 my %myconfig = %main::myconfig;
864 my $locale = $main::locale;
868 if ($form->{second_run}) {
869 $form->{print_and_post} = 0;
871 map { delete $form->{$_} } qw(id printed emailed queued);
874 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
875 $form->{title} = $locale->text('Add Request for Quotation');
876 $form->{vc} = 'vendor';
877 $form->{type} = 'request_quotation';
880 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
881 $form->{title} = $locale->text('Add Quotation');
882 $form->{vc} = 'customer';
883 $form->{type} = 'sales_quotation';
889 $form->{script} = 'oe.pl';
895 require "bin/mozilla/$form->{script}";
897 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
899 my $currency = $form->{currency};
903 $form->{currency} = $currency;
904 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
905 $form->{exchangerate} = $form->{forex} || '';
907 for my $i (1 .. $form->{rowcount}) {
908 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
909 $form->{"${_}_${i}"})
910 if ($form->{"${_}_${i}"}) }
911 qw(ship qty sellprice listprice basefactor discount));
917 $main::lxdebug->leave_sub();
920 sub request_for_quotation {
925 $main::lxdebug->enter_sub();
927 my $form = $main::form;
928 my %myconfig = %main::myconfig;
929 my $locale = $main::locale;
933 if ($form->{second_run}) {
934 $form->{print_and_post} = 0;
935 $form->{resubmit} = 0;
938 $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
940 if ($form->{"cp_id"}) {
941 CT->get_contact(\%myconfig, $form);
942 $form->{"email"} = $form->{"cp_email"} if $form->{"cp_email"};
945 my $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
947 $form->{oldmedia} = $form->{media};
948 $form->{media} = "email";
950 my $attachment_filename = $form->generate_attachment_filename();
951 my $subject = $form->{subject} || $form->generate_email_subject();
953 $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
956 my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
957 @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
958 @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
959 @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
961 print $form->parse_html_template('generic/edit_email',
963 a_filename => $attachment_filename,
965 print_options => print_options('inline' => 1),
966 HIDDEN => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
967 SHOW_BCC => $::auth->assert('email_bcc', 'may fail') });
969 $main::lxdebug->leave_sub();
973 $main::lxdebug->enter_sub();
975 my $form = $main::form;
976 my %myconfig = %main::myconfig;
980 my $callback = $form->{script} . "?action=edit";
981 map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
983 print_form("return");
985 Common->save_email_status(\%myconfig, $form);
987 $form->{callback} = $callback;
990 $main::lxdebug->leave_sub();
993 # generate the printing options displayed at the bottom of oe and is forms.
994 # this function will attempt to guess what type of form is displayed, and will generate according options
997 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
998 # the opthash function builds hashrefs which are then pieced together for the template arrays.
999 # unneeded options are "undef"ed out, and then grepped out.
1001 # the inline options is untested, but intended to be used later in metatemplating
1003 $main::lxdebug->enter_sub();
1005 my $form = $main::form;
1006 my %myconfig = %main::myconfig;
1007 my $locale = $main::locale;
1013 # names 3 parameters and returns a hashref, for use in templates
1014 sub opthash { +{ value => shift, selected => shift, oname => shift } }
1015 my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1017 # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1018 $form->{sendmode} = "attachment";
1019 $form->{format} = $form->{format} || $myconfig{template_format} || "pdf";
1020 $form->{copies} = $form->{copies} || $myconfig{copies} || 3;
1021 $form->{media} = $form->{media} || $myconfig{default_media} || "screen";
1022 $form->{printer_id} = defined $form->{printer_id} ? $form->{printer_id} :
1023 defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1025 $form->{PD}{ $form->{formname} } = "selected";
1026 $form->{DF}{ $form->{format} } = "selected";
1027 $form->{OP}{ $form->{media} } = "selected";
1028 $form->{SM}{ $form->{formname} } = "selected";
1030 push @FORMNAME, grep $_,
1031 ($form->{type} eq 'purchase_order') ? (
1032 opthash("purchase_order", $form->{PD}{purchase_order}, $locale->text('Purchase Order')),
1033 opthash("bin_list", $form->{PD}{bin_list}, $locale->text('Bin List'))
1035 ($form->{type} eq 'credit_note') ?
1036 opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef,
1037 ($form->{type} eq 'sales_order') ? (
1038 opthash("sales_order", $form->{PD}{sales_order}, $locale->text('Confirmation')),
1039 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1041 ($form->{type} =~ /sales_quotation$/) ?
1042 opthash('sales_quotation', $form->{PD}{sales_quotation}, $locale->text('Quotation')) : undef,
1043 ($form->{type} =~ /request_quotation$/) ?
1044 opthash('request_quotation', $form->{PD}{request_quotation}, $locale->text('Request for Quotation')) : undef,
1045 ($form->{type} eq 'invoice') ? (
1046 opthash("invoice", $form->{PD}{invoice}, $locale->text('Invoice')),
1047 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1049 ($form->{type} eq 'invoice' && $form->{storno}) ? (
1050 opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
1052 ($form->{type} =~ /_delivery_order$/) ? (
1053 opthash($form->{type}, $form->{PD}{$form->{type}}, $locale->text('Delivery Order')),
1054 opthash('pick_list', $form->{PD}{pick_list}, $locale->text('Pick List')),
1058 opthash("attachment", $form->{SM}{attachment}, $locale->text('Attachment')),
1059 opthash("inline", $form->{SM}{inline}, $locale->text('In-line'))
1060 if ($form->{media} eq 'email');
1062 push @MEDIA, grep $_,
1063 opthash("screen", $form->{OP}{screen}, $locale->text('Screen')),
1064 ($form->{printers} && scalar @{ $form->{printers} } && $::lx_office_conf{print_templates}->{latex}) ?
1065 opthash("printer", $form->{OP}{printer}, $locale->text('Printer')) : undef,
1066 ($::lx_office_conf{print_templates}->{latex} && !$options{no_queue}) ?
1067 opthash("queue", $form->{OP}{queue}, $locale->text('Queue')) : undef
1068 if ($form->{media} ne 'email');
1070 push @FORMAT, grep $_,
1071 ($::lx_office_conf{print_templates}->{opendocument} && $::lx_office_conf{applications}->{openofficeorg_writer} && $::lx_office_conf{applications}->{xvfb}
1072 && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
1073 && !$options{no_opendocument_pdf}) ?
1074 opthash("opendocument_pdf", $form->{DF}{"opendocument_pdf"}, $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1075 ($::lx_office_conf{print_templates}->{latex}) ?
1076 opthash("pdf", $form->{DF}{pdf}, $locale->text('PDF')) : undef,
1077 ($::lx_office_conf{print_templates}->{latex} && !$options{no_postscript}) ?
1078 opthash("postscript", $form->{DF}{postscript}, $locale->text('Postscript')) : undef,
1079 (!$options{no_html}) ?
1080 opthash("html", $form->{DF}{html}, "HTML") : undef,
1081 ($::lx_office_conf{print_templates}->{opendocument} && !$options{no_opendocument}) ?
1082 opthash("opendocument", $form->{DF}{opendocument}, $locale->text("OpenDocument/OASIS")) : undef,
1083 ($::lx_office_conf{print_templates}->{excel} && !$options{no_excel}) ?
1084 opthash("excel", $form->{DF}{excel}, $locale->text("Excel")) : undef;
1087 map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1088 if (ref $form->{languages} eq 'ARRAY');
1091 map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1092 if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1097 show => !$options{"hide_" . $_->[1]} && scalar @{ $_->[0] }
1099 [ \@FORMNAME, 'formname', ],
1100 [ \@LANGUAGE_ID, 'language_id', ],
1101 [ \@FORMAT, 'format', ],
1102 [ \@SENDMODE, 'sendmode', ],
1103 [ \@MEDIA, 'media', ],
1104 [ \@PRINTER_ID, 'printer_id', ];
1106 my %dont_display_groupitems = (
1110 my %template_vars = (
1111 display_copies => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
1112 display_remove_draft => (!$form->{id} && $form->{draft_id}),
1113 display_groupitems => !$dont_display_groupitems{$form->{type}},
1114 groupitems_checked => $form->{groupitems} ? "checked" : '',
1115 remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1118 my $print_options = $form->parse_html_template("generic/print_options", { SELECTS => \@SELECTS, %template_vars } );
1120 if ($options{inline}) {
1121 $main::lxdebug->leave_sub();
1122 return $print_options;
1125 print $print_options;
1127 $main::lxdebug->leave_sub();
1131 $main::lxdebug->enter_sub();
1133 my $form = $main::form;
1134 my $locale = $main::locale;
1138 if ($form->{print_nextsub}) {
1139 call_sub($form->{print_nextsub});
1140 $main::lxdebug->leave_sub();
1144 # if this goes to the printer pass through
1146 if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1147 $form->error($locale->text('Select postscript or PDF!'))
1148 if ($form->{format} !~ /(postscript|pdf)/);
1150 $old_form = new Form;
1151 map { $old_form->{$_} = $form->{$_} } keys %$form;
1154 if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1155 if ($form->{formname} eq "proforma") {
1156 $form->{proforma} = 1;
1158 $form->{print_and_save} = 1;
1159 my $formname = $form->{formname};
1161 $form->{formname} = $formname;
1163 $::lxdebug->leave_sub();
1167 &print_form($old_form);
1169 $main::lxdebug->leave_sub();
1173 $main::lxdebug->enter_sub();
1175 my $form = $main::form;
1176 my %myconfig = %main::myconfig;
1177 my $locale = $main::locale;
1181 my ($old_form) = @_;
1185 my $numberfld = "invnumber";
1189 ($form->{display_form}) ? $form->{display_form} : "display_form";
1191 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1192 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1194 if ($form->{formname} eq "invoice") {
1195 $form->{label} = $locale->text('Invoice');
1197 if ($form->{formname} eq 'sales_order') {
1200 $form->{"${inv}date"} = $form->{transdate};
1201 $form->{label} = $locale->text('Confirmation');
1202 $numberfld = "sonumber";
1206 if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1209 $form->{"${inv}date"} = $form->{invdate};
1210 $form->{label} = $locale->text('Proforma Invoice');
1211 $numberfld = "sonumber";
1215 if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1218 $form->{"${inv}date"} = $form->{transdate};
1219 $form->{"invdate"} = $form->{transdate};
1220 $form->{invnumber} = $form->{ordnumber};
1221 $form->{label} = $locale->text('Proforma Invoice');
1222 $numberfld = "sonumber";
1226 if ($form->{formname} eq 'purchase_order') {
1229 $form->{"${inv}date"} = $form->{transdate};
1230 $form->{label} = $locale->text('Purchase Order');
1231 $numberfld = "ponumber";
1234 if ($form->{formname} eq 'bin_list') {
1237 $form->{"${inv}date"} = $form->{transdate};
1238 $form->{label} = $locale->text('Bin List');
1241 if ($form->{formname} eq 'sales_quotation') {
1244 $form->{"${inv}date"} = $form->{transdate};
1245 $form->{label} = $locale->text('Quotation');
1246 $numberfld = "sqnumber";
1250 if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1253 $form->{"${inv}date"} = $form->{transdate};
1254 $form->{"invdate"} = $form->{transdate};
1255 $form->{label} = $locale->text('Proforma Invoice');
1256 $numberfld = "sqnumber";
1260 if ($form->{formname} eq 'request_quotation') {
1263 $form->{"${inv}date"} = $form->{transdate};
1264 $form->{label} = $locale->text('RFQ');
1265 $numberfld = "rfqnumber";
1269 if ($form->{type} =~ /_delivery_order$/) {
1272 $form->{"${inv}date"} = $form->{transdate};
1273 $numberfld = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
1274 $form->{label} = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
1277 $form->isblank("email", $locale->text('E-mail address missing!'))
1278 if ($form->{media} eq 'email');
1279 $form->isblank("${inv}date",
1280 $locale->text($form->{label})
1282 . $locale->text(' Date missing!'));
1284 # $locale->text('Invoice Number missing!')
1285 # $locale->text('Invoice Date missing!')
1286 # $locale->text('Order Number missing!')
1287 # $locale->text('Order Date missing!')
1288 # $locale->text('Quotation Number missing!')
1289 # $locale->text('Quotation Date missing!')
1292 $form->{what_done} = $form->{formname};
1293 if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1294 $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1295 if ($form->{media} ne 'email') {
1297 # get pricegroups for parts
1298 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1300 # build up html code for prices_$i
1301 set_pricegroup($form->{rowcount});
1303 $form->{rowcount}--;
1305 call_sub($display_form);
1306 # saving the history
1307 if(!exists $form->{addition}) {
1308 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1309 $form->{addition} = "PRINTED";
1310 $form->save_history;
1312 # /saving the history
1319 # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1320 my ($saved_email, $saved_cc, $saved_bcc) =
1321 ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1323 my $language_saved = $form->{language_id};
1324 my $payment_id_saved = $form->{payment_id};
1325 my $salesman_id_saved = $form->{salesman_id};
1326 my $cp_id_saved = $form->{cp_id};
1327 my $taxzone_id_saved = $form->{taxzone_id};
1329 call_sub("$form->{vc}_details") if ($form->{vc});
1331 $form->{language_id} = $language_saved;
1332 $form->{payment_id} = $payment_id_saved;
1333 $form->{taxzone_id} = $taxzone_id_saved;
1335 $form->{"email"} = $saved_email if ($saved_email);
1336 $form->{"cc"} = $saved_cc if ($saved_cc);
1337 $form->{"bcc"} = $saved_bcc if ($saved_bcc);
1339 if (!$cp_id_saved) {
1340 # No contact was selected. Delete all contact variables because
1341 # IS->customer_details() and IR->vendor_details() get the default
1343 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1346 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1347 if ($form->{"language_id"}) {
1348 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1349 AM->get_language_details(\%myconfig, $form, $form->{language_id});
1351 $output_dateformat = $myconfig{"dateformat"};
1352 $output_numberformat = $myconfig{"numberformat"};
1353 $output_longdates = 1;
1356 # Store the output number format so that the template modules know
1357 # how to parse the amounts back if requested.
1358 $myconfig{output_numberformat} = $output_numberformat || $myconfig{numberformat};
1360 ($form->{employee}) = split /--/, $form->{employee};
1362 # create the form variables
1363 if ($form->{type} =~ /_delivery_order$/) {
1364 DO->order_details();
1366 OE->order_details(\%myconfig, \%$form);
1368 IS->invoice_details(\%myconfig, \%$form, $locale);
1371 $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
1372 $form->get_employee_data('prefix' => 'salesman', 'id' => $salesman_id_saved);
1374 if ($form->{shipto_id}) {
1375 $form->get_shipto(\%myconfig);
1378 my @a = qw(name street zipcode city country contact);
1382 # if there is no shipto fill it in from billto
1383 foreach my $item (@a) {
1384 if ($form->{"shipto$item"}) {
1391 if ( $form->{formname} eq 'purchase_order'
1392 || $form->{formname} eq 'request_quotation') {
1393 $form->{shiptoname} = $myconfig{company};
1394 $form->{shiptostreet} = $myconfig{address};
1396 map { $form->{"shipto$_"} = $form->{$_} } @a;
1400 $form->{notes} =~ s/^\s+//g;
1402 $form->{templates} = "$myconfig{templates}";
1404 delete $form->{printer_command};
1406 $form->{language} = $form->get_template_language(\%myconfig);
1409 if ($form->{media} ne 'email') {
1410 $printer_code = $form->get_printer_code(\%myconfig);
1411 if ($printer_code ne "") {
1412 $printer_code = "_" . $printer_code;
1416 if ($form->{language} ne "") {
1417 my $template_arrays = $form->{TEMPLATE_ARRAYS} || $form;
1418 map { $template_arrays->{unit}->[$_] = AM->translate_units($form, $form->{language}, $template_arrays->{unit}->[$_], $template_arrays->{qty}->[$_]); } (0..scalar(@{ $template_arrays->{unit} }) - 1);
1420 $form->{language} = "_" . $form->{language};
1424 format_dates($output_dateformat, $output_longdates,
1425 qw(invdate orddate quodate pldate duedate reqdate transdate
1426 shippingdate deliverydate validitydate paymentdate
1427 datepaid transdate_oe deliverydate_oe
1428 employee_startdate employee_enddate
1430 grep({ /^datepaid_\d+$/ ||
1431 /^transdate_oe_\d+$/ ||
1432 /^deliverydate_oe_\d+$/ ||
1434 /^deliverydate_\d+$/ ||
1438 reformat_numbers($output_numberformat, 2,
1439 qw(invtotal ordtotal quototal subtotal linetotal
1440 listprice sellprice netprice discount
1441 tax taxbase total paid),
1442 grep({ /^(?:linetotal|nodiscount_linetotal|listprice|sellprice|netprice|taxbase|discount|p_discount|discount_sub|nodiscount_sub|paid|subtotal|total|tax)_\d+$/ } keys(%{$form})));
1444 reformat_numbers($output_numberformat, undef,
1445 qw(qty price_factor),
1449 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1451 if (scalar @{ $cvar_date_fields }) {
1452 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1455 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1456 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1459 my $extension = 'html';
1460 if ($form->{format} eq 'postscript') {
1461 $form->{postscript} = 1;
1464 } elsif ($form->{"format"} =~ /pdf/) {
1466 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
1468 } elsif ($form->{"format"} =~ /opendocument/) {
1469 $form->{opendocument} = 1;
1471 } elsif ($form->{"format"} =~ /excel/) {
1476 my $email_extension = (($form->{media} eq 'email') && (-f "$myconfig{templates}/$form->{formname}_email$form->{language}${printer_code}.${extension}")) ? '_email' : '';
1478 $form->{IN} = "$form->{formname}${email_extension}$form->{language}${printer_code}.${extension}";
1480 delete $form->{OUT};
1482 if ($form->{media} eq 'printer') {
1483 #$form->{OUT} = "| $form->{printer_command} &>/dev/null";
1484 $form->{OUT} = $form->{printer_command};
1485 $form->{OUT_MODE} = '|-';
1486 $form->{printed} .= " $form->{formname}";
1487 $form->{printed} =~ s/^ //;
1489 my $printed = $form->{printed};
1491 if ($form->{media} eq 'email') {
1492 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1493 unless $form->{subject};
1495 $form->{emailed} .= " $form->{formname}";
1496 $form->{emailed} =~ s/^ //;
1498 my $emailed = $form->{emailed};
1500 if ($form->{media} eq 'queue') {
1501 my %queued = map { s|.*/|| } split / /, $form->{queued};
1504 if ($filename = $queued{ $form->{formname} }) {
1505 $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
1506 unlink $::lx_office_conf{paths}->{spool} . "/$filename";
1507 $filename =~ s/\..*$//g;
1513 $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1514 $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
1515 $form->{OUT_MODE} = '>';
1518 $form->{queued} .= " $form->{formname} $filename";
1520 $form->{queued} =~ s/^ //;
1522 my $queued = $form->{queued};
1524 # saving the history
1525 if(!exists $form->{addition}) {
1526 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1527 if($form->{media} =~ /printer/) {
1528 $form->{addition} = "PRINTED";
1530 elsif($form->{media} =~ /email/) {
1531 $form->{addition} = "MAILED";
1533 elsif($form->{media} =~ /queue/) {
1534 $form->{addition} = "QUEUED";
1536 elsif($form->{media} =~ /screen/) {
1537 $form->{addition} = "SCREENED";
1539 $form->save_history;
1541 # /saving the history
1543 $form->parse_template(\%myconfig);
1545 $form->{callback} = "";
1547 if ($form->{media} eq 'email') {
1548 $form->{message} = $locale->text('sent') unless $form->{message};
1550 my $message = $form->{message};
1552 # if we got back here restore the previous form
1553 if ($form->{media} =~ /(printer|email|queue)/) {
1555 $form->update_status(\%myconfig)
1556 if ($form->{media} eq 'queue' && $form->{id});
1558 return $main::lxdebug->leave_sub() if ($old_form eq "return");
1562 $old_form->{"${inv}number"} = $form->{"${inv}number"};
1564 # restore and display form
1565 map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1567 $form->{queued} = $queued;
1568 $form->{printed} = $printed;
1569 $form->{emailed} = $emailed;
1570 $form->{message} = $message;
1572 $form->{rowcount}--;
1573 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1574 qw(exchangerate creditlimit creditremaining);
1576 for my $i (1 .. $form->{paidaccounts}) {
1578 $form->{"${_}_$i"} =
1579 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1580 } qw(paid exchangerate);
1583 call_sub($display_form);
1588 ($form->{media} eq 'printer')
1589 ? $locale->text('sent to printer')
1590 : $locale->text('emailed to') . " $form->{email}";
1591 $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1593 if ($form->{printing}) {
1594 call_sub($display_form);
1598 $main::lxdebug->leave_sub();
1601 sub customer_details {
1602 $main::lxdebug->enter_sub();
1604 my $form = $main::form;
1605 my %myconfig = %main::myconfig;
1607 IS->customer_details(\%myconfig, \%$form, @_);
1609 $main::lxdebug->leave_sub();
1612 sub vendor_details {
1613 $main::lxdebug->enter_sub();
1615 my $form = $main::form;
1616 my %myconfig = %main::myconfig;
1618 IR->vendor_details(\%myconfig, \%$form, @_);
1620 $main::lxdebug->leave_sub();
1624 $main::lxdebug->enter_sub();
1626 my $form = $main::form;
1630 $form->{postasnew} = 1;
1631 map { delete $form->{$_} } qw(printed emailed queued);
1635 $main::lxdebug->leave_sub();
1639 $main::lxdebug->enter_sub();
1643 $::form->{print_and_post} = 0 if $::form->{second_run};
1645 map { $::form->{$_} = $::form->parse_amount(\%::myconfig, $::form->{$_}) } qw(exchangerate creditlimit creditremaining);
1647 # get details for customer/vendor
1648 call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country contact email phone fax), $::form->{vc} . "number");
1650 # get pricegroups for parts
1651 IS->get_pricegroups_for_parts(\%::myconfig, \%$::form);
1653 # build up html code for prices_$i
1654 set_pricegroup($::form->{rowcount});
1656 $::form->{rowcount}--;
1658 my @shipto_vars = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1659 shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
1660 shiptodepartment_1 shiptodepartment_2);
1661 my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
1662 $::form->{title} = $::locale->text('Ship to');
1665 print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
1666 nextsub => $::form->{display_form} || 'display_form',
1669 $main::lxdebug->leave_sub();
1672 sub ship_to_entered {
1673 $::auth->restore_form_from_session(delete $::form->{previousform});
1674 call_sub($::form->{nextsub});
1677 sub relink_accounts {
1678 $main::lxdebug->enter_sub();
1680 my $form = $main::form;
1681 my %myconfig = %main::myconfig;
1685 $form->{"taxaccounts"} =~ s/\s*$//;
1686 $form->{"taxaccounts"} =~ s/^\s*//;
1687 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1688 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1690 $form->{"taxaccounts"} = "";
1692 IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
1694 $main::lxdebug->leave_sub();
1698 $main::lxdebug->enter_sub();
1700 my $form = $main::form;
1701 my %myconfig = %main::myconfig;
1705 my $invdate = $form->{invdate} eq 'undefined' ? undef : $form->{invdate};
1706 my $duedate = $form->get_duedate(\%myconfig, $invdate);
1708 print $form->ajax_response_header() . $duedate;
1710 $main::lxdebug->leave_sub();
1713 sub _update_part_information {
1714 $main::lxdebug->enter_sub();
1716 my $form = $main::form;
1718 my %part_information = IC->get_basic_part_info('id' => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
1719 'vendor_id' => $form->{vendor_id});
1721 $form->{PART_INFORMATION} = \%part_information;
1723 foreach my $i (1..$form->{rowcount}) {
1724 next unless ($form->{"id_${i}"});
1726 my $info = $form->{PART_INFORMATION}->{$form->{"id_${i}"}} || { };
1727 $form->{"partunit_${i}"} = $info->{unit};
1730 $main::lxdebug->leave_sub();
1734 $main::lxdebug->enter_sub();
1736 my $form = $main::form;
1737 my %myconfig = %main::myconfig;
1739 if (!$form->{ordnumber} || !$form->{id}) {
1740 map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
1741 $main::lxdebug->leave_sub();
1745 my $all_units = AM->retrieve_all_units();
1747 my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales',
1748 'oe_id' => $form->{id},);
1750 foreach my $i (1..$form->{rowcount}) {
1751 next unless ($form->{"id_${i}"});
1753 $form->{"ship_$i"} = 0;
1755 my $ship_entry = $ship{$form->{"id_$i"}};
1757 next if (!$ship_entry || ($ship_entry->{qty} <= 0));
1760 ($form->{simple_save} ? $form->{"qty_$i"} : $form->parse_amount(\%myconfig, $form->{"qty_$i"}))
1761 * $all_units->{$form->{"unit_$i"}}->{factor}
1762 / $all_units->{$form->{"partunit_$i"}}->{factor};
1764 $form->{"ship_$i"} = min($rowqty, $ship_entry->{qty});
1765 $ship_entry->{qty} -= $form->{"ship_$i"};
1768 foreach my $i (1..$form->{rowcount}) {
1769 next unless ($form->{"id_${i}"});
1771 my $ship_entry = $ship{$form->{"id_$i"}};
1773 next if (!$ship_entry || ($ship_entry->{qty} <= 0.01));
1775 $form->{"ship_$i"} += $ship_entry->{qty};
1776 $ship_entry->{qty} = 0;
1779 $main::lxdebug->leave_sub();
1782 sub _update_custom_variables {
1783 $main::lxdebug->enter_sub();
1785 my $form = $main::form;
1787 $form->{CVAR_CONFIGS} = { } unless ref $form->{CVAR_CONFIGS} eq 'HASH';
1788 $form->{CVAR_CONFIGS}->{IC} ||= CVar->get_configs(module => 'IC');
1790 $main::lxdebug->leave_sub();
1793 sub _render_custom_variables_inputs {
1794 $main::lxdebug->enter_sub(2);
1796 my $form = $main::form;
1800 if (!$form->{CVAR_CONFIGS}->{IC}) {
1801 $main::lxdebug->leave_sub();
1805 my $valid = CVar->custom_variables_validity_by_trans_id(trans_id => $params{part_id});
1807 my $num_visible_cvars = 0;
1808 foreach my $cvar (@{ $form->{CVAR_CONFIGS}->{IC} }) {
1809 $cvar->{valid} = $params{part_id} && $valid->($cvar->{id});
1811 my $description = '';
1812 if ($cvar->{flag_editable} && $cvar->{valid}) {
1813 $num_visible_cvars++;
1814 $description = $cvar->{description} . ' ';
1817 my $form_key = "ic_cvar_" . $cvar->{name} . "_$params{row}";
1819 push @{ $params{ROW2} }, {
1820 line_break => $num_visible_cvars == 1,
1821 description => $description,
1824 hide_non_editable => 1,
1826 name_prefix => 'ic_',
1827 name_postfix => "_$params{row}",
1828 valid => $cvar->{valid},
1829 value => CVar->parse($::form->{$form_key}, $cvar),
1834 $main::lxdebug->leave_sub(2);