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);
52 require "bin/mozilla/common.pl";
56 # any custom scripts for this one
57 if (-f "bin/mozilla/custom_io.pl") {
58 eval { require "bin/mozilla/custom_io.pl"; };
60 if (-f "bin/mozilla/$::form->{login}_io.pl") {
61 eval { require "bin/mozilla/$::form->{login}_io.pl"; };
68 # this is for our long dates
69 # $locale->text('January')
70 # $locale->text('February')
71 # $locale->text('March')
72 # $locale->text('April')
73 # $locale->text('May ')
74 # $locale->text('June')
75 # $locale->text('July')
76 # $locale->text('August')
77 # $locale->text('September')
78 # $locale->text('October')
79 # $locale->text('November')
80 # $locale->text('December')
82 # this is for our short month
83 # $locale->text('Jan')
84 # $locale->text('Feb')
85 # $locale->text('Mar')
86 # $locale->text('Apr')
87 # $locale->text('May')
88 # $locale->text('Jun')
89 # $locale->text('Jul')
90 # $locale->text('Aug')
91 # $locale->text('Sep')
92 # $locale->text('Oct')
93 # $locale->text('Nov')
94 # $locale->text('Dec')
101 $main::auth->assert('part_service_assembly_edit | vendor_invoice_edit | sales_order_edit | invoice_edit |' .
102 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | ' .
103 'purchase_delivery_order_edit | sales_delivery_order_edit');
106 ########################################
107 # Eintrag fuer Version 2.2.0 geaendert #
108 # neue Optik im Rechnungsformular #
109 ########################################
111 $main::lxdebug->enter_sub();
115 my $form = $main::form;
116 my %myconfig = %main::myconfig;
117 my $locale = $main::locale;
118 my $cgi = $::request->{cgi};
122 my ($stock_in_out, $stock_in_out_title);
124 my $is_purchase = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
125 my $show_min_order_qty = first { $_ eq $form->{type} } qw(request_quotation purchase_order);
126 my $is_delivery_order = $form->{type} =~ /_delivery_order$/;
127 my $is_s_p_order = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
129 if ($is_delivery_order) {
130 if ($form->{type} eq 'sales_delivery_order') {
131 $stock_in_out_title = $locale->text('Release From Stock');
132 $stock_in_out = 'out';
134 $stock_in_out_title = $locale->text('Transfer To Stock');
135 $stock_in_out = 'in';
138 retrieve_partunits();
142 my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
144 { id => 'runningnumber', width => 5, value => $locale->text('No.'), display => 1, },
145 { id => 'partnumber', width => 8, value => $locale->text('Number'), display => 1, },
146 { id => 'description', width => 30, value => $locale->text('Part Description'), display => 1, },
147 { id => 'ship', width => 5, value => $locale->text('Delivered'), display => $is_s_p_order, },
148 { id => 'qty', width => 5, value => $locale->text('Qty'), display => 1, },
149 { id => 'price_factor', width => 5, value => $locale->text('Price Factor'), display => !$is_delivery_order, },
150 { id => 'unit', width => 5, value => $locale->text('Unit'), display => 1, },
151 { id => 'serialnr', width => 10, value => $locale->text('Serial No.'), display => 0, },
152 { id => 'projectnr', width => 10, value => $locale->text('Project'), display => 0, },
153 { id => 'sellprice', width => 15, value => $locale->text('Price'), display => !$is_delivery_order, },
154 { id => 'sellprice_pg', width => 8, value => $locale->text('Pricegroup'), display => !$is_delivery_order && !$is_purchase, },
155 { id => 'discount', width => 5, value => $locale->text('Discount'), display => !$is_delivery_order, },
156 { id => 'linetotal', width => 10, value => $locale->text('Extended'), display => !$is_delivery_order, },
157 { id => 'bin', width => 10, value => $locale->text('Bin'), display => 0, },
158 { id => 'stock_in_out', width => 10, value => $stock_in_out_title, display => $is_delivery_order, },
160 my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
164 my $all_units = AM->retrieve_units(\%myconfig, $form);
166 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
168 my $colspan = scalar @column_index;
170 $form->{invsubtotal} = 0;
171 map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
174 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
175 $form->{show_details} = $myconfig{show_form_details} unless (defined($form->{show_details}));
178 # translations, unused commented out
179 # $runningnumber = $locale->text('No.');
180 # my $deliverydate = $locale->text('Delivery Date');
181 my $serialnumber = $locale->text('Serial No.');
182 my $projectnumber = $locale->text('Project');
183 # $partsgroup = $locale->text('Group');
184 my $reqdate = $locale->text('Reqdate');
185 my $deliverydate = $locale->text('Required by');
188 my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out);
189 my %nowrap = map { $_ => 1 } qw(description unit);
191 $form->{marge_total} = 0;
192 $form->{sellprice_total} = 0;
193 $form->{lastcost_total} = 0;
194 my %projectnumber_labels = ();
195 my @projectnumber_values = ("");
197 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
198 push(@projectnumber_values, $item->{"id"});
199 $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
202 _update_part_information();
203 _update_ship() if ($is_s_p_order);
204 _update_custom_variables();
209 for my $i (1 .. $numrows) {
210 my %column_data = ();
213 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
214 qw(qty discount sellprice lastcost price_new price_old)
215 unless ($form->{simple_save});
218 $form->{"unit_old_$i"} ||= $form->{"unit_$i"};
219 $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
221 if ( !$all_units->{$form->{"selected_unit_$i"}} # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
222 || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
223 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"}; # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
225 # adjust prices by unit, ignore if pricegroup changed
226 if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
227 $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
228 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
230 my $this_unit = $form->{"unit_$i"};
231 $this_unit = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
233 if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
234 my @values = ('', map { $_->{id} } @{ $form->{ALL_PRICE_FACTORS} });
235 my %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
237 $column_data{price_factor} =
238 NTI($cgi->popup_menu('-name' => "price_factor_id_$i",
239 '-default' => $form->{"price_factor_id_$i"},
240 '-values' => \@values,
241 '-labels' => \%labels,
242 '-style' => 'width:90px'));
244 $column_data{price_factor} = ' ';
247 $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
250 my $decimalplaces = ($form->{"sellprice_$i"} =~ /\.(\d+)/) ? max 2, length $1 : 2;
252 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || 1;
253 my $discount = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100 / $price_factor, 2);
254 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
255 my $rows = $form->numtextrows($form->{"description_$i"}, 30, 6);
257 $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5, -value => $i); # HuT
258 $column_data{partnumber} = $cgi->textfield(-name => "partnumber_$i", -size => 12, -value => $form->{"partnumber_$i"});
259 $column_data{description} = (($rows > 1) # if description is too large, use a textbox instead
260 ? $cgi->textarea( -name => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
261 : $cgi->textfield(-name => "description_$i", -size => 30, -value => $form->{"description_$i"}))
262 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
264 my $qty_dec = ($form->{"qty_$i"} =~ /\.(\d+)/) ? length $1 : 2;
266 $column_data{qty} = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
267 $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
268 . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
269 if $form->{"formel_$i"};
271 $column_data{ship} = '';
272 if ($form->{"id_$i"}) {
273 my $ship_qty = $form->{"ship_$i"} * 1;
274 $ship_qty *= $all_units->{$form->{"partunit_$i"}}->{factor};
275 $ship_qty /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
277 $column_data{ship} = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
280 # build in drop down list for pricesgroups
281 # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
282 # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
283 # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
284 # jetzt nach der Abfrage.
286 if ($form->{"prices_$i"}) {
287 $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
288 $sellprice_value =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
289 ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
290 : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
292 # for last row and report
293 # set pricegroup drop down list from report menu
294 if ($form->{"sellprice_$i"} != 0) {
295 # remember the pricegroup_id in pricegroup_old
296 # but don't overwrite it
297 $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
298 my $default_option = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
299 $column_data{sellprice_pg} = NTI($cgi->popup_menu("sellprice_pg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
301 $column_data{sellprice_pg} = qq| |;
303 $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
306 # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
307 # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
308 # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
310 my $edit_prices = $main::auth->assert('edit_prices', 1);
311 $column_data{sellprice} = (!$edit_prices)
312 ? $cgi->textfield(-readonly => "readonly",
313 -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
314 : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
315 $column_data{discount} = (!$edit_prices)
316 ? $cgi->textfield(-readonly => "readonly",
317 -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
318 : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
319 $column_data{linetotal} = $form->format_amount(\%myconfig, $linetotal, 2);
320 $column_data{bin} = $form->{"bin_$i"};
322 if ($is_delivery_order) {
323 $column_data{stock_in_out} = calculate_stock_in_out($i);
326 my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
330 push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
331 if $form->{type} !~ /_quotation/;
332 push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name' => "project_id_$i", '-values' => \@projectnumber_values,
333 '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
334 push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
335 if ($form->{type} =~ /order/ || $form->{type} =~ /invoice/);
336 push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
337 $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
339 # begin marge calculations
340 $form->{"lastcost_$i"} *= 1;
341 $form->{"marge_percent_$i"} = 0;
344 my $real_sellprice = $linetotal;
345 my $real_lastcost = $form->{"lastcost_$i"} * $form->{"qty_$i"} / ( $form->{"marge_price_factor_$i"} || 1 );
346 my $marge_percent_warn = $myconfig{marge_percent_warn} * 1 || 15;
347 my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
349 if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
350 $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
351 $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
354 $form->{"marge_absolut_$i"} = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
355 $form->{"marge_total"} += $form->{"marge_absolut_$i"};
356 $form->{"lastcost_total"} += $real_lastcost;
357 $form->{"sellprice_total"} += $real_sellprice;
359 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
361 push @ROW2, { value => sprintf qq|
362 <font %s><b>%s</b> %s %s%% </font>
364 <b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
365 $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
366 $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
367 $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
368 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
370 $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
371 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/) ;
372 # / marge calculations ending
375 if ($form->{"id_$i"}) {
376 my $part = IC->get_basic_part_info(id => $form->{"id_$i"});
377 my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
378 push @ROW2, { value => sprintf "<b>%s</b> <font %s>%s %s</font>",
379 $locale->text('On Hand'),
381 $form->format_amount(\%myconfig, $part->{onhand}, 2),
389 if ($is_delivery_order) {
390 map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
391 push @hidden_vars, qw(sellprice discount not_discountable price_factor_id lastcost);
392 push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
395 my @HIDDENS = map { value => $_}, (
396 $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
397 $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
398 map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
399 (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
400 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
401 longdescription basefactor marge_absolut marge_percent marge_price_factor), @hidden_vars)
404 map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
406 $form->{invsubtotal} += $linetotal;
408 # Benutzerdefinierte Variablen für Waren/Dienstleistungen/Erzeugnisse
409 _render_custom_variables_inputs(ROW2 => \@ROW2, row => $i, part_id => $form->{"id_$i"});
411 push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
414 print $form->parse_html_template('oe/sales_order', { ROWS => \@ROWS,
418 if (0 != ($form->{sellprice_total} * 1)) {
419 $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
422 $main::lxdebug->leave_sub();
425 ##################################################
426 # build html-code for pricegroups in variable $form->{prices_$j}
429 $main::lxdebug->enter_sub();
431 my $form = $main::form;
432 my $locale = $main::locale;
433 my $cgi = $::request->{cgi};
437 my $rowcount = shift;
438 for my $j (1 .. $rowcount) {
439 next unless $form->{PRICES}{$j};
440 # build drop down list for pricegroups
441 my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
442 $form->{"prices_$j"} = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} }
443 (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
445 foreach my $item (@{ $form->{PRICES}{$j} }) {
446 # set new selectedpricegroup_id and prices for "Preis"
447 $form->{"pricegroup_old_$j"} = $item->{pricegroup_id} if $item->{selected} && $item->{pricegroup_id};
448 $form->{"sellprice_$j"} = $item->{price} if $item->{selected} && $item->{pricegroup_id};
449 $form->{"price_new_$j"} = $form->{"sellprice_$j"} if $item->{selected} || !$item->{pricegroup_id};
452 $main::lxdebug->leave_sub();
456 $main::lxdebug->enter_sub();
459 my $mode = $params{mode} || croak "Missing parameter 'mode'";
463 my $previous_form = $::auth->save_form_in_session(form => $::form);
464 $::form->{title} = $::locale->text('Select from one of the items below');
467 my @item_list = map {
468 $_->{display_sellprice} = $_->{sellprice} * (1 - $::form->{tradediscount});
469 $_->{display_sellprice} /= $_->{price_factor} if ($_->{price_factor});
471 } @{ $::form->{item_list} };
473 # delete action variable
474 delete @{$::form}{qw(action item_list header)};
476 print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
478 ITEM_LIST => \@item_list,
479 IS_PURCHASE => $mode eq 'IS' });
481 $main::lxdebug->leave_sub();
485 $main::lxdebug->enter_sub();
487 my $form = $main::form;
488 my %myconfig = %main::myconfig;
492 $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
494 my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
495 my $id = delete($form->{select_item_id}) || croak 'Missing item selection ID';
496 my $i = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
498 $form->{"id_${i}"} = $id;
501 IS->retrieve_item(\%myconfig, \%$form);
502 } elsif ($mode eq 'IR') {
503 IR->retrieve_item(\%myconfig, \%$form);
504 } elsif ($mode eq 'IC') {
505 IC->assembly_item(\%myconfig, \%$form);
507 croak "Invalid item selection mode '${mode}'";
510 my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
512 # if there was a price entered, override it
513 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
516 qw(id partnumber description sellprice listprice inventory_accno
517 income_accno expense_accno bin unit weight assembly taxaccounts
518 partsgroup formel longdescription not_discountable partnotes lastcost
519 price_factor_id price_factor);
521 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
522 push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
524 map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
526 $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
528 if ($form->{"part_payment_id_$i"} ne "") {
529 $form->{payment_id} = $form->{"part_payment_id_$i"};
532 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
534 my $decimalplaces = ($dec > 2) ? $dec : 2;
537 $form->{"sellprice_$i"} = $sellprice;
540 # if there is an exchange rate adjust sellprice
541 if (($form->{exchangerate} * 1) != 0) {
542 $form->{"sellprice_$i"} /= $form->{exchangerate};
543 $form->{"sellprice_$i"} =
544 $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
548 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
549 qw(sellprice listprice weight);
551 $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
552 $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
554 if ($form->{"not_discountable_$i"}) {
555 $form->{"discount_$i"} = 0;
559 $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
561 map { $form->{"${_}_base"} += $amount }
562 (split / /, $form->{"taxaccounts_$i"});
563 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
564 $form->{"taxaccounts_$i"}
565 if !$form->{taxincluded};
567 $form->{creditremaining} -= $amount;
569 $form->{"runningnumber_$i"} = $i;
571 delete $form->{nextsub};
576 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
577 } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
579 # get pricegroups for parts
580 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
582 # build up html code for prices_$i
583 set_pricegroup($form->{rowcount});
587 $main::lxdebug->leave_sub();
591 $main::lxdebug->enter_sub();
593 my $form = $main::form;
594 my %myconfig = %main::myconfig;
598 my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
601 $form->{old_callback} = $form->escape($form->{callback}, 1);
602 $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
604 # save all form variables except action in the session and keep the key in the previousform variable
605 my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
608 push @HIDDENS, { 'name' => 'previousform', 'value' => $previousform };
609 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(rowcount vc);
610 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
611 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
612 push @HIDDENS, { 'name' => $price_key, 'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
613 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{"longdescription_$form->{rowcount}"} };
616 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
618 $main::lxdebug->leave_sub();
622 $main::lxdebug->enter_sub();
624 my $form = $main::form;
625 my %myconfig = %main::myconfig;
632 # remove any makes or model rows
633 if ($form->{item} eq 'part') {
634 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
635 qw(listprice sellprice lastcost weight rop);
637 } elsif ($form->{item} eq 'assembly') {
639 # fuer assemblies auskommentiert. seiteneffekte? ;-) wird die woanders benoetigt?
640 #$form->{sellprice} = 0;
642 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
643 qw(listprice sellprice rop stock);
645 my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
647 for my $i (1 .. ($form->{assembly_rows} - 1)) {
648 if ($form->{"qty_$i"}) {
652 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
654 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
656 #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
658 # fuer assemblies auskommentiert. siehe oben
659 # $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
660 $form->{weight} += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
664 # kann das hier auch weg? s.o. jb
665 $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
667 $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
668 $form->{assembly_rows} = $count;
670 } elsif ($form->{item} eq 'service') {
671 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
674 remove_emptied_rows(1);
676 $form->{creditremaining} -= &invoicetotal;
681 if ( $form->{type} =~ (/sales_quotation/)
682 or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
683 or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
684 or ($form->{type} =~ /sales_order/)) {
686 # get pricegroups for parts
687 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
689 # build up html code for prices_$i
690 set_pricegroup($form->{rowcount});
696 $main::lxdebug->leave_sub();
699 sub remove_emptied_rows {
700 my $dont_add_empty = shift;
703 return unless $form->{rowcount};
705 my @flds = qw(id partnumber description qty ship sellprice unit
706 discount inventory_accno income_accno expense_accno listprice
707 taxaccounts bin assembly weight projectnumber project_id
708 oldprojectnumber runningnumber serialnumber partsgroup payment_id
709 not_discountable shop ve gv buchungsgruppen_id language_values
710 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
711 transdate longdescription basefactor marge_total marge_percent
712 marge_price_factor lastcost price_factor_id partnotes
713 stock_out stock_in has_sernumber reqdate);
715 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
716 push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
719 for my $i (1 .. $form->{rowcount} - 1) {
720 next unless $form->{"partnumber_$i"};
722 push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
725 my $new_rowcount = scalar @new_rows;
726 $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
727 $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
731 $main::lxdebug->enter_sub();
733 my $form = $main::form;
734 my %myconfig = %main::myconfig;
738 $form->{oldinvtotal} = 0;
740 # add all parts and deduct paid
741 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
743 my ($amount, $sellprice, $discount, $qty);
745 for my $i (1 .. $form->{rowcount}) {
746 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
747 $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
748 $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
750 #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
752 $amount = $sellprice * (1 - $discount / 100) * $qty;
753 map { $form->{"${_}_base"} += $amount }
754 (split (/ /, $form->{"taxaccounts_$i"}));
755 $form->{oldinvtotal} += $amount;
758 map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
759 split(/ /, $form->{taxaccounts})
760 if !$form->{taxincluded};
762 $form->{oldtotalpaid} = 0;
763 for my $i (1 .. $form->{paidaccounts}) {
764 $form->{oldtotalpaid} += $form->{"paid_$i"};
767 $main::lxdebug->leave_sub();
770 return ($form->{oldinvtotal} - $form->{oldtotalpaid});
774 $main::lxdebug->enter_sub();
776 my $form = $main::form;
777 my $locale = $main::locale;
781 # check if items are valid
782 if ($form->{rowcount} == 1) {
787 for my $i (1 .. $form->{rowcount} - 1) {
788 $form->isblank("partnumber_$i",
789 $locale->text('Number missing in Row') . " $i");
792 $main::lxdebug->leave_sub();
796 $main::lxdebug->enter_sub();
798 my $form = $main::form;
799 my %myconfig = %main::myconfig;
800 my $locale = $main::locale;
804 if ($form->{second_run}) {
805 $form->{print_and_post} = 0;
807 $form->{ordnumber} = $form->{invnumber};
809 $form->{old_employee_id} = $form->{employee_id};
810 $form->{old_salesman_id} = $form->{salesman_id};
812 map { delete $form->{$_} } qw(id printed emailed queued);
814 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
815 $form->{title} = $locale->text('Add Purchase Order');
816 $form->{vc} = 'vendor';
817 $form->{type} = 'purchase_order';
820 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
821 $form->{title} = $locale->text('Add Sales Order');
822 $form->{vc} = 'customer';
823 $form->{type} = 'sales_order';
826 $form->{script} = 'oe.pl';
834 require "bin/mozilla/$form->{script}";
835 my $script = $form->{"script"};
838 $locale = new Locale($::lx_office_conf{system}->{language}, $script);
840 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
842 my $currency = $form->{currency};
846 $form->{currency} = $currency;
847 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
848 $form->{exchangerate} = $form->{forex} || '';
850 for my $i (1 .. $form->{rowcount}) {
851 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
852 if ($form->{"${_}_${i}"}) }
853 qw(ship qty sellprice listprice basefactor discount));
859 $main::lxdebug->leave_sub();
863 $main::lxdebug->enter_sub();
865 my $form = $main::form;
866 my %myconfig = %main::myconfig;
867 my $locale = $main::locale;
871 if ($form->{second_run}) {
872 $form->{print_and_post} = 0;
874 map { delete $form->{$_} } qw(id printed emailed queued);
877 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
878 $form->{title} = $locale->text('Add Request for Quotation');
879 $form->{vc} = 'vendor';
880 $form->{type} = 'request_quotation';
883 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
884 $form->{title} = $locale->text('Add Quotation');
885 $form->{vc} = 'customer';
886 $form->{type} = 'sales_quotation';
892 $form->{script} = 'oe.pl';
898 require "bin/mozilla/$form->{script}";
900 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
902 my $currency = $form->{currency};
906 $form->{currency} = $currency;
907 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
908 $form->{exchangerate} = $form->{forex} || '';
910 for my $i (1 .. $form->{rowcount}) {
911 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
912 $form->{"${_}_${i}"})
913 if ($form->{"${_}_${i}"}) }
914 qw(ship qty sellprice listprice basefactor discount));
920 $main::lxdebug->leave_sub();
923 sub request_for_quotation {
928 $main::lxdebug->enter_sub();
930 my $form = $main::form;
931 my %myconfig = %main::myconfig;
932 my $locale = $main::locale;
936 if ($form->{second_run}) {
937 $form->{print_and_post} = 0;
938 $form->{resubmit} = 0;
941 $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
943 if ($form->{"cp_id"}) {
944 CT->get_contact(\%myconfig, $form);
945 $form->{"email"} = $form->{"cp_email"} if $form->{"cp_email"};
948 my $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
950 $form->{oldmedia} = $form->{media};
951 $form->{media} = "email";
953 my $attachment_filename = $form->generate_attachment_filename();
954 my $subject = $form->{subject} || $form->generate_email_subject();
956 $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
959 my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
960 @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
961 @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
962 @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
964 print $form->parse_html_template('generic/edit_email',
966 a_filename => $attachment_filename,
968 print_options => print_options('inline' => 1),
969 HIDDEN => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
970 SHOW_BCC => $::auth->assert('email_bcc', 'may fail') });
972 $main::lxdebug->leave_sub();
976 $main::lxdebug->enter_sub();
978 my $form = $main::form;
979 my %myconfig = %main::myconfig;
983 my $callback = $form->{script} . "?action=edit";
984 map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
986 print_form("return");
988 Common->save_email_status(\%myconfig, $form);
990 $form->{callback} = $callback;
993 $main::lxdebug->leave_sub();
996 # generate the printing options displayed at the bottom of oe and is forms.
997 # this function will attempt to guess what type of form is displayed, and will generate according options
1000 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
1001 # the opthash function builds hashrefs which are then pieced together for the template arrays.
1002 # unneeded options are "undef"ed out, and then grepped out.
1004 # the inline options is untested, but intended to be used later in metatemplating
1006 $main::lxdebug->enter_sub();
1008 my $form = $main::form;
1009 my %myconfig = %main::myconfig;
1010 my $locale = $main::locale;
1016 # names 3 parameters and returns a hashref, for use in templates
1017 sub opthash { +{ value => shift, selected => shift, oname => shift } }
1018 my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1020 # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1021 $form->{sendmode} = "attachment";
1022 $form->{format} = $form->{format} || $myconfig{template_format} || "pdf";
1023 $form->{copies} = $form->{copies} || $myconfig{copies} || 3;
1024 $form->{media} = $form->{media} || $myconfig{default_media} || "screen";
1025 $form->{printer_id} = defined $form->{printer_id} ? $form->{printer_id} :
1026 defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1028 $form->{PD}{ $form->{formname} } = "selected";
1029 $form->{DF}{ $form->{format} } = "selected";
1030 $form->{OP}{ $form->{media} } = "selected";
1031 $form->{SM}{ $form->{formname} } = "selected";
1033 push @FORMNAME, grep $_,
1034 ($form->{type} eq 'purchase_order') ? (
1035 opthash("purchase_order", $form->{PD}{purchase_order}, $locale->text('Purchase Order')),
1036 opthash("bin_list", $form->{PD}{bin_list}, $locale->text('Bin List'))
1038 ($form->{type} eq 'credit_note') ?
1039 opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef,
1040 ($form->{type} eq 'sales_order') ? (
1041 opthash("sales_order", $form->{PD}{sales_order}, $locale->text('Confirmation')),
1042 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1044 ($form->{type} =~ /sales_quotation$/) ?
1045 opthash('sales_quotation', $form->{PD}{sales_quotation}, $locale->text('Quotation')) : undef,
1046 ($form->{type} =~ /request_quotation$/) ?
1047 opthash('request_quotation', $form->{PD}{request_quotation}, $locale->text('Request for Quotation')) : undef,
1048 ($form->{type} eq 'invoice') ? (
1049 opthash("invoice", $form->{PD}{invoice}, $locale->text('Invoice')),
1050 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1052 ($form->{type} eq 'invoice' && $form->{storno}) ? (
1053 opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
1055 ($form->{type} =~ /_delivery_order$/) ? (
1056 opthash($form->{type}, $form->{PD}{$form->{type}}, $locale->text('Delivery Order')),
1057 opthash('pick_list', $form->{PD}{pick_list}, $locale->text('Pick List')),
1061 opthash("attachment", $form->{SM}{attachment}, $locale->text('Attachment')),
1062 opthash("inline", $form->{SM}{inline}, $locale->text('In-line'))
1063 if ($form->{media} eq 'email');
1065 push @MEDIA, grep $_,
1066 opthash("screen", $form->{OP}{screen}, $locale->text('Screen')),
1067 ($form->{printers} && scalar @{ $form->{printers} } && $::lx_office_conf{print_templates}->{latex}) ?
1068 opthash("printer", $form->{OP}{printer}, $locale->text('Printer')) : undef,
1069 ($::lx_office_conf{print_templates}->{latex} && !$options{no_queue}) ?
1070 opthash("queue", $form->{OP}{queue}, $locale->text('Queue')) : undef
1071 if ($form->{media} ne 'email');
1073 push @FORMAT, grep $_,
1074 ($::lx_office_conf{print_templates}->{opendocument} && $::lx_office_conf{applications}->{openofficeorg_writer} && $::lx_office_conf{applications}->{xvfb}
1075 && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
1076 && !$options{no_opendocument_pdf}) ?
1077 opthash("opendocument_pdf", $form->{DF}{"opendocument_pdf"}, $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1078 ($::lx_office_conf{print_templates}->{latex}) ?
1079 opthash("pdf", $form->{DF}{pdf}, $locale->text('PDF')) : undef,
1080 ($::lx_office_conf{print_templates}->{latex} && !$options{no_postscript}) ?
1081 opthash("postscript", $form->{DF}{postscript}, $locale->text('Postscript')) : undef,
1082 (!$options{no_html}) ?
1083 opthash("html", $form->{DF}{html}, "HTML") : undef,
1084 ($::lx_office_conf{print_templates}->{opendocument} && !$options{no_opendocument}) ?
1085 opthash("opendocument", $form->{DF}{opendocument}, $locale->text("OpenDocument/OASIS")) : undef,
1086 ($::lx_office_conf{print_templates}->{excel} && !$options{no_excel}) ?
1087 opthash("excel", $form->{DF}{excel}, $locale->text("Excel")) : undef;
1090 map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1091 if (ref $form->{languages} eq 'ARRAY');
1094 map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1095 if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1100 show => !$options{"hide_" . $_->[1]} && scalar @{ $_->[0] }
1102 [ \@FORMNAME, 'formname', ],
1103 [ \@LANGUAGE_ID, 'language_id', ],
1104 [ \@FORMAT, 'format', ],
1105 [ \@SENDMODE, 'sendmode', ],
1106 [ \@MEDIA, 'media', ],
1107 [ \@PRINTER_ID, 'printer_id', ];
1109 my %dont_display_groupitems = (
1113 my %template_vars = (
1114 display_copies => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
1115 display_remove_draft => (!$form->{id} && $form->{draft_id}),
1116 display_groupitems => !$dont_display_groupitems{$form->{type}},
1117 groupitems_checked => $form->{groupitems} ? "checked" : '',
1118 remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1121 my $print_options = $form->parse_html_template("generic/print_options", { SELECTS => \@SELECTS, %template_vars } );
1123 if ($options{inline}) {
1124 $main::lxdebug->leave_sub();
1125 return $print_options;
1128 print $print_options;
1130 $main::lxdebug->leave_sub();
1134 $main::lxdebug->enter_sub();
1136 my $form = $main::form;
1137 my $locale = $main::locale;
1141 if ($form->{print_nextsub}) {
1142 call_sub($form->{print_nextsub});
1143 $main::lxdebug->leave_sub();
1147 # if this goes to the printer pass through
1149 if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1150 $form->error($locale->text('Select postscript or PDF!'))
1151 if ($form->{format} !~ /(postscript|pdf)/);
1153 $old_form = new Form;
1154 map { $old_form->{$_} = $form->{$_} } keys %$form;
1157 if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1158 if ($form->{formname} eq "proforma") {
1159 $form->{proforma} = 1;
1161 $form->{print_and_save} = 1;
1162 my $formname = $form->{formname};
1164 $form->{formname} = $formname;
1166 $::lxdebug->leave_sub();
1170 &print_form($old_form);
1172 $main::lxdebug->leave_sub();
1176 $main::lxdebug->enter_sub();
1178 my $form = $main::form;
1179 my %myconfig = %main::myconfig;
1180 my $locale = $main::locale;
1184 my ($old_form) = @_;
1188 my $numberfld = "invnumber";
1192 ($form->{display_form}) ? $form->{display_form} : "display_form";
1194 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1195 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1197 if ($form->{formname} eq "invoice") {
1198 $form->{label} = $locale->text('Invoice');
1200 if ($form->{formname} eq 'sales_order') {
1203 $form->{"${inv}date"} = $form->{transdate};
1204 $form->{label} = $locale->text('Confirmation');
1205 $numberfld = "sonumber";
1209 if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1212 $form->{"${inv}date"} = $form->{invdate};
1213 $form->{label} = $locale->text('Proforma Invoice');
1214 $numberfld = "sonumber";
1218 if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1221 $form->{"${inv}date"} = $form->{transdate};
1222 $form->{"invdate"} = $form->{transdate};
1223 $form->{invnumber} = $form->{ordnumber};
1224 $form->{label} = $locale->text('Proforma Invoice');
1225 $numberfld = "sonumber";
1229 if ($form->{formname} eq 'purchase_order') {
1232 $form->{"${inv}date"} = $form->{transdate};
1233 $form->{label} = $locale->text('Purchase Order');
1234 $numberfld = "ponumber";
1237 if ($form->{formname} eq 'bin_list') {
1240 $form->{"${inv}date"} = $form->{transdate};
1241 $form->{label} = $locale->text('Bin List');
1244 if ($form->{formname} eq 'sales_quotation') {
1247 $form->{"${inv}date"} = $form->{transdate};
1248 $form->{label} = $locale->text('Quotation');
1249 $numberfld = "sqnumber";
1253 if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1256 $form->{"${inv}date"} = $form->{transdate};
1257 $form->{"invdate"} = $form->{transdate};
1258 $form->{label} = $locale->text('Proforma Invoice');
1259 $numberfld = "sqnumber";
1263 if ($form->{formname} eq 'request_quotation') {
1266 $form->{"${inv}date"} = $form->{transdate};
1267 $form->{label} = $locale->text('RFQ');
1268 $numberfld = "rfqnumber";
1272 if ($form->{type} =~ /_delivery_order$/) {
1275 $form->{"${inv}date"} = $form->{transdate};
1276 $numberfld = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
1277 $form->{label} = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
1280 $form->isblank("email", $locale->text('E-mail address missing!'))
1281 if ($form->{media} eq 'email');
1282 $form->isblank("${inv}date",
1283 $locale->text($form->{label})
1285 . $locale->text(' Date missing!'));
1287 # $locale->text('Invoice Number missing!')
1288 # $locale->text('Invoice Date missing!')
1289 # $locale->text('Order Number missing!')
1290 # $locale->text('Order Date missing!')
1291 # $locale->text('Quotation Number missing!')
1292 # $locale->text('Quotation Date missing!')
1295 $form->{what_done} = $form->{formname};
1296 if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1297 $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1298 if ($form->{media} ne 'email') {
1300 # get pricegroups for parts
1301 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1303 # build up html code for prices_$i
1304 set_pricegroup($form->{rowcount});
1306 $form->{rowcount}--;
1308 call_sub($display_form);
1309 # saving the history
1310 if(!exists $form->{addition}) {
1311 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1312 $form->{addition} = "PRINTED";
1313 $form->save_history;
1315 # /saving the history
1322 # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1323 my ($saved_email, $saved_cc, $saved_bcc) =
1324 ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1326 my $language_saved = $form->{language_id};
1327 my $payment_id_saved = $form->{payment_id};
1328 my $salesman_id_saved = $form->{salesman_id};
1329 my $cp_id_saved = $form->{cp_id};
1330 my $taxzone_id_saved = $form->{taxzone_id};
1331 my $currency_saved = $form->{currency};
1333 call_sub("$form->{vc}_details") if ($form->{vc});
1335 $form->{language_id} = $language_saved;
1336 $form->{payment_id} = $payment_id_saved;
1337 $form->{taxzone_id} = $taxzone_id_saved;
1338 $form->{currency} = $currency_saved;
1340 $form->{"email"} = $saved_email if ($saved_email);
1341 $form->{"cc"} = $saved_cc if ($saved_cc);
1342 $form->{"bcc"} = $saved_bcc if ($saved_bcc);
1344 if (!$cp_id_saved) {
1345 # No contact was selected. Delete all contact variables because
1346 # IS->customer_details() and IR->vendor_details() get the default
1348 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1351 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1352 if ($form->{"language_id"}) {
1353 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1354 AM->get_language_details(\%myconfig, $form, $form->{language_id});
1356 $output_dateformat = $myconfig{"dateformat"};
1357 $output_numberformat = $myconfig{"numberformat"};
1358 $output_longdates = 1;
1361 # Store the output number format so that the template modules know
1362 # how to parse the amounts back if requested.
1363 $myconfig{output_numberformat} = $output_numberformat || $myconfig{numberformat};
1365 ($form->{employee}) = split /--/, $form->{employee};
1367 # create the form variables
1368 if ($form->{type} =~ /_delivery_order$/) {
1369 DO->order_details();
1371 OE->order_details(\%myconfig, \%$form);
1373 IS->invoice_details(\%myconfig, \%$form, $locale);
1376 $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
1377 $form->get_employee_data('prefix' => 'salesman', 'id' => $salesman_id_saved);
1379 if ($form->{shipto_id}) {
1380 $form->get_shipto(\%myconfig);
1383 my @a = qw(name street zipcode city country contact);
1387 # if there is no shipto fill it in from billto
1388 foreach my $item (@a) {
1389 if ($form->{"shipto$item"}) {
1396 if ( $form->{formname} eq 'purchase_order'
1397 || $form->{formname} eq 'request_quotation') {
1398 $form->{shiptoname} = $myconfig{company};
1399 $form->{shiptostreet} = $myconfig{address};
1401 map { $form->{"shipto$_"} = $form->{$_} } @a;
1405 $form->{notes} =~ s/^\s+//g;
1407 $form->{templates} = "$myconfig{templates}";
1409 delete $form->{printer_command};
1411 $form->{language} = $form->get_template_language(\%myconfig);
1414 if ($form->{media} ne 'email') {
1415 $printer_code = $form->get_printer_code(\%myconfig);
1416 if ($printer_code ne "") {
1417 $printer_code = "_" . $printer_code;
1421 if ($form->{language} ne "") {
1422 my $template_arrays = $form->{TEMPLATE_ARRAYS} || $form;
1423 map { $template_arrays->{unit}->[$_] = AM->translate_units($form, $form->{language}, $template_arrays->{unit}->[$_], $template_arrays->{qty}->[$_]); } (0..scalar(@{ $template_arrays->{unit} }) - 1);
1425 $form->{language} = "_" . $form->{language};
1429 format_dates($output_dateformat, $output_longdates,
1430 qw(invdate orddate quodate pldate duedate reqdate transdate
1431 shippingdate deliverydate validitydate paymentdate
1432 datepaid transdate_oe deliverydate_oe
1433 employee_startdate employee_enddate
1435 grep({ /^datepaid_\d+$/ ||
1436 /^transdate_oe_\d+$/ ||
1437 /^deliverydate_oe_\d+$/ ||
1439 /^deliverydate_\d+$/ ||
1443 reformat_numbers($output_numberformat, 2,
1444 qw(invtotal ordtotal quototal subtotal linetotal
1445 listprice sellprice netprice discount
1446 tax taxbase total paid),
1447 grep({ /^(?:linetotal|nodiscount_linetotal|listprice|sellprice|netprice|taxbase|discount|p_discount|discount_sub|nodiscount_sub|paid|subtotal|total|tax)_\d+$/ } keys(%{$form})));
1449 reformat_numbers($output_numberformat, undef,
1450 qw(qty price_factor),
1454 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1456 if (scalar @{ $cvar_date_fields }) {
1457 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1460 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1461 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1464 my $extension = 'html';
1465 if ($form->{format} eq 'postscript') {
1466 $form->{postscript} = 1;
1469 } elsif ($form->{"format"} =~ /pdf/) {
1471 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
1473 } elsif ($form->{"format"} =~ /opendocument/) {
1474 $form->{opendocument} = 1;
1476 } elsif ($form->{"format"} =~ /excel/) {
1481 # search for the template
1483 push @template_files, "$form->{formname}_email$form->{language}$printer_code.$extension" if $form->{media} eq 'email';
1484 push @template_files, "$form->{formname}$form->{language}$printer_code.$extension";
1485 push @template_files, "$form->{formname}.$extension";
1486 push @template_files, "default.$extension";
1488 $form->{IN} = undef;
1489 for my $filename (@template_files) {
1490 if (-f "$myconfig{templates}/$filename") {
1491 $form->{IN} = $filename;
1496 if (!defined $form->{IN}) {
1497 $::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));
1500 delete $form->{OUT};
1502 if ($form->{media} eq 'printer') {
1503 #$form->{OUT} = "| $form->{printer_command} &>/dev/null";
1504 $form->{OUT} = $form->{printer_command};
1505 $form->{OUT_MODE} = '|-';
1506 $form->{printed} .= " $form->{formname}";
1507 $form->{printed} =~ s/^ //;
1509 my $printed = $form->{printed};
1511 if ($form->{media} eq 'email') {
1512 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1513 unless $form->{subject};
1515 $form->{emailed} .= " $form->{formname}";
1516 $form->{emailed} =~ s/^ //;
1518 my $emailed = $form->{emailed};
1520 if ($form->{media} eq 'queue') {
1521 my %queued = map { s|.*/|| } split / /, $form->{queued};
1524 if ($filename = $queued{ $form->{formname} }) {
1525 $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
1526 unlink $::lx_office_conf{paths}->{spool} . "/$filename";
1527 $filename =~ s/\..*$//g;
1533 $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1534 $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
1535 $form->{OUT_MODE} = '>';
1538 $form->{queued} .= " $form->{formname} $filename";
1540 $form->{queued} =~ s/^ //;
1542 my $queued = $form->{queued};
1544 # saving the history
1545 if(!exists $form->{addition}) {
1546 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1547 if($form->{media} =~ /printer/) {
1548 $form->{addition} = "PRINTED";
1550 elsif($form->{media} =~ /email/) {
1551 $form->{addition} = "MAILED";
1553 elsif($form->{media} =~ /queue/) {
1554 $form->{addition} = "QUEUED";
1556 elsif($form->{media} =~ /screen/) {
1557 $form->{addition} = "SCREENED";
1559 $form->save_history;
1561 # /saving the history
1563 # prepare meta information for template introspection
1564 $form->{template_meta} = {
1565 formname => $form->{formname},
1566 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
1567 format => $form->{format},
1568 media => $form->{media},
1569 extension => $extension,
1570 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
1573 $form->parse_template(\%myconfig);
1575 $form->{callback} = "";
1577 if ($form->{media} eq 'email') {
1578 $form->{message} = $locale->text('sent') unless $form->{message};
1580 my $message = $form->{message};
1582 # if we got back here restore the previous form
1583 if ($form->{media} =~ /(printer|email|queue)/) {
1585 $form->update_status(\%myconfig)
1586 if ($form->{media} eq 'queue' && $form->{id});
1588 return $main::lxdebug->leave_sub() if ($old_form eq "return");
1592 $old_form->{"${inv}number"} = $form->{"${inv}number"};
1594 # restore and display form
1595 map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1597 $form->{queued} = $queued;
1598 $form->{printed} = $printed;
1599 $form->{emailed} = $emailed;
1600 $form->{message} = $message;
1602 $form->{rowcount}--;
1603 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1604 qw(exchangerate creditlimit creditremaining);
1606 for my $i (1 .. $form->{paidaccounts}) {
1608 $form->{"${_}_$i"} =
1609 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1610 } qw(paid exchangerate);
1613 call_sub($display_form);
1618 ($form->{media} eq 'printer')
1619 ? $locale->text('sent to printer')
1620 : $locale->text('emailed to') . " $form->{email}";
1621 $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1623 if ($form->{printing}) {
1624 call_sub($display_form);
1628 $main::lxdebug->leave_sub();
1631 sub customer_details {
1632 $main::lxdebug->enter_sub();
1634 my $form = $main::form;
1635 my %myconfig = %main::myconfig;
1637 IS->customer_details(\%myconfig, \%$form, @_);
1639 $main::lxdebug->leave_sub();
1642 sub vendor_details {
1643 $main::lxdebug->enter_sub();
1645 my $form = $main::form;
1646 my %myconfig = %main::myconfig;
1648 IR->vendor_details(\%myconfig, \%$form, @_);
1650 $main::lxdebug->leave_sub();
1654 $main::lxdebug->enter_sub();
1656 my $form = $main::form;
1660 $form->{postasnew} = 1;
1661 map { delete $form->{$_} } qw(printed emailed queued);
1665 $main::lxdebug->leave_sub();
1669 $main::lxdebug->enter_sub();
1673 $::form->{print_and_post} = 0 if $::form->{second_run};
1675 map { $::form->{$_} = $::form->parse_amount(\%::myconfig, $::form->{$_}) } qw(exchangerate creditlimit creditremaining);
1677 # get details for customer/vendor
1678 call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country contact email phone fax), $::form->{vc} . "number");
1680 # get pricegroups for parts
1681 IS->get_pricegroups_for_parts(\%::myconfig, \%$::form);
1683 # build up html code for prices_$i
1684 set_pricegroup($::form->{rowcount});
1686 $::form->{rowcount}--;
1688 my @shipto_vars = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1689 shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
1690 shiptodepartment_1 shiptodepartment_2);
1691 my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
1692 $::form->{title} = $::locale->text('Ship to');
1695 print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
1696 nextsub => $::form->{display_form} || 'display_form',
1699 $main::lxdebug->leave_sub();
1702 sub ship_to_entered {
1703 $::auth->restore_form_from_session(delete $::form->{previousform});
1704 call_sub($::form->{nextsub});
1707 sub relink_accounts {
1708 $main::lxdebug->enter_sub();
1710 my $form = $main::form;
1711 my %myconfig = %main::myconfig;
1715 $form->{"taxaccounts"} =~ s/\s*$//;
1716 $form->{"taxaccounts"} =~ s/^\s*//;
1717 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1718 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1720 $form->{"taxaccounts"} = "";
1722 IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
1724 $main::lxdebug->leave_sub();
1728 $main::lxdebug->enter_sub();
1730 my $form = $main::form;
1731 my %myconfig = %main::myconfig;
1735 my $invdate = $form->{invdate} eq 'undefined' ? undef : $form->{invdate};
1736 my $duedate = $form->get_duedate(\%myconfig, $invdate);
1738 print $form->ajax_response_header() . $duedate;
1740 $main::lxdebug->leave_sub();
1743 sub _update_part_information {
1744 $main::lxdebug->enter_sub();
1746 my $form = $main::form;
1748 my %part_information = IC->get_basic_part_info('id' => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
1749 'vendor_id' => $form->{vendor_id});
1751 $form->{PART_INFORMATION} = \%part_information;
1753 foreach my $i (1..$form->{rowcount}) {
1754 next unless ($form->{"id_${i}"});
1756 my $info = $form->{PART_INFORMATION}->{$form->{"id_${i}"}} || { };
1757 $form->{"partunit_${i}"} = $info->{unit};
1760 $main::lxdebug->leave_sub();
1764 $main::lxdebug->enter_sub();
1766 my $form = $main::form;
1767 my %myconfig = %main::myconfig;
1769 if (!$form->{ordnumber} || !$form->{id}) {
1770 map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
1771 $main::lxdebug->leave_sub();
1775 my $all_units = AM->retrieve_all_units();
1777 my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales',
1778 'oe_id' => $form->{id},);
1780 foreach my $i (1..$form->{rowcount}) {
1781 next unless ($form->{"id_${i}"});
1783 $form->{"ship_$i"} = 0;
1785 my $ship_entry = $ship{$form->{"id_$i"}};
1787 next if (!$ship_entry || ($ship_entry->{qty} <= 0));
1790 ($form->{simple_save} ? $form->{"qty_$i"} : $form->parse_amount(\%myconfig, $form->{"qty_$i"}))
1791 * $all_units->{$form->{"unit_$i"}}->{factor}
1792 / $all_units->{$form->{"partunit_$i"}}->{factor};
1794 $form->{"ship_$i"} = min($rowqty, $ship_entry->{qty});
1795 $ship_entry->{qty} -= $form->{"ship_$i"};
1798 foreach my $i (1..$form->{rowcount}) {
1799 next unless ($form->{"id_${i}"});
1801 my $ship_entry = $ship{$form->{"id_$i"}};
1803 next if (!$ship_entry || ($ship_entry->{qty} <= 0.01));
1805 $form->{"ship_$i"} += $ship_entry->{qty};
1806 $ship_entry->{qty} = 0;
1809 $main::lxdebug->leave_sub();
1812 sub _update_custom_variables {
1813 $main::lxdebug->enter_sub();
1815 my $form = $main::form;
1817 $form->{CVAR_CONFIGS} = { } unless ref $form->{CVAR_CONFIGS} eq 'HASH';
1818 $form->{CVAR_CONFIGS}->{IC} ||= CVar->get_configs(module => 'IC');
1820 $main::lxdebug->leave_sub();
1823 sub _render_custom_variables_inputs {
1824 $main::lxdebug->enter_sub(2);
1826 my $form = $main::form;
1830 if (!$form->{CVAR_CONFIGS}->{IC}) {
1831 $main::lxdebug->leave_sub();
1835 my $valid = CVar->custom_variables_validity_by_trans_id(trans_id => $params{part_id});
1837 my $num_visible_cvars = 0;
1838 foreach my $cvar (@{ $form->{CVAR_CONFIGS}->{IC} }) {
1839 $cvar->{valid} = $params{part_id} && $valid->($cvar->{id});
1841 my $description = '';
1842 if ($cvar->{flag_editable} && $cvar->{valid}) {
1843 $num_visible_cvars++;
1844 $description = $cvar->{description} . ' ';
1847 my $form_key = "ic_cvar_" . $cvar->{name} . "_$params{row}";
1849 push @{ $params{ROW2} }, {
1850 line_break => $num_visible_cvars == 1,
1851 description => $description,
1854 hide_non_editable => 1,
1856 name_prefix => 'ic_',
1857 name_postfix => "_$params{row}",
1858 valid => $cvar->{valid},
1859 value => CVar->parse($::form->{$form_key}, $cvar),
1864 $main::lxdebug->leave_sub(2);