1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # Changelog: Wann - Wer - Was
8 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik
9 # 08.11.2008 - information@richardson-bueren.de jb - Backport von Revision 7339 xplace - E-Mail-Vorlage automatisch auswählen
10 # 02.02.2009 - information@richardson-bueren.de jb - Backport von Revision 8535 xplace - Erweiterung der Waren bei Lieferantenauftrag um den Eintrag Mindestlagerbestand. Offen: Auswahlliste auf Lieferantenaufträge einschränken -> Erledigt 2.2.09 Prüfung wie das Skript heisst (oe.pl) -> das ist nur die halbe Miete, nochmal mb fragen -> mb gefragt und es gibt die variable is_purchase
11 #############################################################################
12 # SQL-Ledger, Accounting
13 # Copyright (c) 1998-2002
15 # Author: Dieter Simader
16 # Email: dsimader@sql-ledger.org
17 # Web: http://www.sql-ledger.org
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #######################################################################
35 # common routines used in is, ir, oe
37 #######################################################################
41 use List::MoreUtils qw(any uniq);
42 use List::Util qw(min max first);
52 use SL::Helper::Flash;
54 require "bin/mozilla/common.pl";
58 # any custom scripts for this one
59 if (-f "bin/mozilla/custom_io.pl") {
60 eval { require "bin/mozilla/custom_io.pl"; };
62 if (-f "bin/mozilla/$::form->{login}_io.pl") {
63 eval { require "bin/mozilla/$::form->{login}_io.pl"; };
70 # this is for our long dates
71 # $locale->text('January')
72 # $locale->text('February')
73 # $locale->text('March')
74 # $locale->text('April')
75 # $locale->text('May ')
76 # $locale->text('June')
77 # $locale->text('July')
78 # $locale->text('August')
79 # $locale->text('September')
80 # $locale->text('October')
81 # $locale->text('November')
82 # $locale->text('December')
84 # this is for our short month
85 # $locale->text('Jan')
86 # $locale->text('Feb')
87 # $locale->text('Mar')
88 # $locale->text('Apr')
89 # $locale->text('May')
90 # $locale->text('Jun')
91 # $locale->text('Jul')
92 # $locale->text('Aug')
93 # $locale->text('Sep')
94 # $locale->text('Oct')
95 # $locale->text('Nov')
96 # $locale->text('Dec')
103 $main::auth->assert('part_service_assembly_edit | vendor_invoice_edit | sales_order_edit | invoice_edit |' .
104 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | ' .
105 'purchase_delivery_order_edit | sales_delivery_order_edit');
108 ########################################
109 # Eintrag fuer Version 2.2.0 geaendert #
110 # neue Optik im Rechnungsformular #
111 ########################################
113 $main::lxdebug->enter_sub();
117 my $form = $main::form;
118 my %myconfig = %main::myconfig;
119 my $locale = $main::locale;
120 my $cgi = $::request->{cgi};
124 my ($stock_in_out, $stock_in_out_title);
126 my $is_purchase = (first { $_ eq $form->{type} } qw(request_quotation purchase_order purchase_delivery_order)) || ($form->{script} eq 'ir.pl');
127 my $show_min_order_qty = first { $_ eq $form->{type} } qw(request_quotation purchase_order);
128 my $is_delivery_order = $form->{type} =~ /_delivery_order$/;
129 my $is_s_p_order = (first { $_ eq $form->{type} } qw(sales_order purchase_order));
131 if ($is_delivery_order) {
132 if ($form->{type} eq 'sales_delivery_order') {
133 $stock_in_out_title = $locale->text('Release From Stock');
134 $stock_in_out = 'out';
136 $stock_in_out_title = $locale->text('Transfer To Stock');
137 $stock_in_out = 'in';
140 retrieve_partunits();
144 my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
146 { id => 'runningnumber', width => 5, value => $locale->text('No.'), display => 1, },
147 { id => 'partnumber', width => 8, value => $locale->text('Number'), display => 1, },
148 { id => 'description', width => 30, value => $locale->text('Part Description'), display => 1, },
149 { id => 'ship', width => 5, value => $locale->text('Delivered'), display => $is_s_p_order, },
150 { id => 'qty', width => 5, value => $locale->text('Qty'), display => 1, },
151 { id => 'price_factor', width => 5, value => $locale->text('Price Factor'), display => !$is_delivery_order, },
152 { id => 'unit', width => 5, value => $locale->text('Unit'), display => 1, },
153 { id => 'serialnr', width => 10, value => $locale->text('Serial No.'), display => 0, },
154 { id => 'projectnr', width => 10, value => $locale->text('Project'), display => 0, },
155 { id => 'sellprice', width => 15, value => $locale->text('Price'), display => !$is_delivery_order, },
156 { id => 'sellprice_pg', width => 8, value => $locale->text('Pricegroup'), display => !$is_delivery_order && !$is_purchase, },
157 { id => 'discount', width => 5, value => $locale->text('Discount'), display => !$is_delivery_order, },
158 { id => 'linetotal', width => 10, value => $locale->text('Extended'), display => !$is_delivery_order, },
159 { id => 'bin', width => 10, value => $locale->text('Bin'), display => 0, },
160 { id => 'stock_in_out', width => 10, value => $stock_in_out_title, display => $is_delivery_order, },
162 my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
166 my $all_units = AM->retrieve_units(\%myconfig, $form);
168 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
170 my $colspan = scalar @column_index;
172 $form->{invsubtotal} = 0;
173 map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
176 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
177 $form->{show_details} = $myconfig{show_form_details} unless (defined($form->{show_details}));
180 # translations, unused commented out
181 # $runningnumber = $locale->text('No.');
182 # my $deliverydate = $locale->text('Delivery Date');
183 my $serialnumber = $locale->text('Serial No.');
184 my $projectnumber = $locale->text('Project');
185 # $partsgroup = $locale->text('Group');
186 my $reqdate = $locale->text('Reqdate');
187 my $deliverydate = $locale->text('Required by');
190 my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal stock_in_out);
191 my %nowrap = map { $_ => 1 } qw(description unit);
193 $form->{marge_total} = 0;
194 $form->{sellprice_total} = 0;
195 $form->{lastcost_total} = 0;
196 my %projectnumber_labels = ();
197 my @projectnumber_values = ("");
199 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
200 push(@projectnumber_values, $item->{"id"});
201 $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
204 _update_part_information();
205 _update_ship() if ($is_s_p_order);
206 _update_custom_variables();
211 for my $i (1 .. $numrows) {
212 my %column_data = ();
215 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
216 qw(qty discount sellprice lastcost price_new price_old)
217 unless ($form->{simple_save});
220 $form->{"unit_old_$i"} ||= $form->{"unit_$i"};
221 $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
223 if ( !$all_units->{$form->{"selected_unit_$i"}} # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
224 || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
225 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"}; # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
227 # adjust prices by unit, ignore if pricegroup changed
228 if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
229 $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
230 $form->{"lastcost_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
231 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
233 my $this_unit = $form->{"unit_$i"};
234 $this_unit = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
236 if (0 < scalar @{ $form->{ALL_PRICE_FACTORS} }) {
237 my @values = ('', map { $_->{id} } @{ $form->{ALL_PRICE_FACTORS} });
238 my %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_PRICE_FACTORS} };
240 $column_data{price_factor} =
241 NTI($cgi->popup_menu('-name' => "price_factor_id_$i",
242 '-default' => $form->{"price_factor_id_$i"},
243 '-values' => \@values,
244 '-labels' => \%labels,
245 '-style' => 'width:90px'));
247 $column_data{price_factor} = ' ';
250 $column_data{"unit"} = AM->unit_select_html($all_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
253 #count the max of decimalplaces of sellprice and lastcost, so the same number of decimalplaces
254 #is shown for lastcost and sellprice.
255 my $decimalplaces = ($form->{"sellprice_$i"} =~ /\.(\d+)/) ? max 2, length $1 : 2;
256 $decimalplaces = ($form->{"lastcost_$i"} =~ /\.(\d+)/) ? max $decimalplaces, length $1 : $decimalplaces;
258 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || 1;
259 my $discount = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100 / $price_factor, 2);
260 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"sellprice_$i"} * (100 - $form->{"discount_$i"}) / 100 / $price_factor, 2);
261 my $rows = $form->numtextrows($form->{"description_$i"}, 30, 6);
263 $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5, -value => $i); # HuT
264 $column_data{partnumber} = $cgi->textfield(-name => "partnumber_$i", -size => 12, -value => $form->{"partnumber_$i"});
265 $column_data{description} = (($rows > 1) # if description is too large, use a textbox instead
266 ? $cgi->textarea( -name => "description_$i", -default => $form->{"description_$i"}, -rows => $rows, -columns => 30)
267 : $cgi->textfield(-name => "description_$i", -size => 30, -value => $form->{"description_$i"}))
268 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
270 my $qty_dec = ($form->{"qty_$i"} =~ /\.(\d+)/) ? length $1 : 2;
272 $column_data{qty} = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
273 $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
274 . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
275 if $form->{"formel_$i"};
277 $column_data{ship} = '';
278 if ($form->{"id_$i"}) {
279 my $ship_qty = $form->{"ship_$i"} * 1;
280 $ship_qty *= $all_units->{$form->{"partunit_$i"}}->{factor};
281 $ship_qty /= ( $all_units->{$form->{"unit_$i"}}->{factor} || 1 );
283 $column_data{ship} = $form->format_amount(\%myconfig, $form->round_amount($ship_qty, 2) * 1) . ' ' . $form->{"unit_$i"};
286 # build in drop down list for pricesgroups
287 # $sellprice_value setzt den Wert etwas unabhängiger von der Darstellung.
288 # Hintergrund: Preisgruppen werden hier überprüft und neu berechnet.
289 # Vorher wurde der ganze cgi->textfield Block zweimal identisch eingebaut, dass passiert
290 # jetzt nach der Abfrage.
292 if ($form->{"prices_$i"}) {
293 $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i" style="width: 8em">$form->{"prices_$i"}</select>|;
294 $sellprice_value =($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
295 ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
296 : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
298 # for last row and report
299 # set pricegroup drop down list from report menu
300 if ($form->{"sellprice_$i"} != 0) {
301 # remember the pricegroup_id in pricegroup_old
302 # but don't overwrite it
303 $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
304 my $default_option = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
305 $column_data{sellprice_pg} = NTI($cgi->popup_menu("sellprice_pg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
307 $column_data{sellprice_pg} = qq| |;
309 $sellprice_value = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
312 # Falls der Benutzer die Preise nicht anpassen sollte, wird das entsprechende
313 # Textfield auf readonly gesetzt. Anm. von Sven: Manipulation der Preise ist
314 # immer noch möglich, konsequenterweise sollten diese NUR aus der Datenbank
316 my $edit_prices = $main::auth->assert('edit_prices', 1);
317 $column_data{sellprice} = (!$edit_prices)
318 ? $cgi->textfield(-readonly => "readonly",
319 -name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value)
320 : $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value => $sellprice_value);
321 $column_data{discount} = (!$edit_prices)
322 ? $cgi->textfield(-readonly => "readonly",
323 -name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}))
324 : $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
325 $column_data{linetotal} = $form->format_amount(\%myconfig, $linetotal, 2);
326 $column_data{bin} = $form->{"bin_$i"};
328 if ($is_delivery_order) {
329 $column_data{stock_in_out} = calculate_stock_in_out($i);
332 my @ROW1 = map { value => $column_data{$_}, align => $align{$_}, nowrap => $nowrap{$_} }, @column_index;
336 push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
337 if $form->{type} !~ /_quotation/;
338 push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name' => "project_id_$i", '-values' => \@projectnumber_values,
339 '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
340 push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
341 if ($form->{type} =~ /order/ || $form->{type} =~ /invoice/);
342 push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
343 $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
345 # begin marge calculations
346 $form->{"lastcost_$i"} *= 1;
347 $form->{"marge_percent_$i"} = 0;
351 if ( $form->{taxincluded} and $form->{"qty_$i"} * 1 and $form->{$form->{"taxaccounts_$i"} . "_rate"} * 1) {
352 # if we use taxincluded we need to calculate the marge from the net_value
353 # all the marge calculations are based on linetotal which we need to
354 # convert to net first
356 # there is no direct form value for the tax_rate of the item, but
357 # form->{taxaccounts_$i} gives the tax account (e.g. 3806) and 3806_rate
358 # gives the tax percentage (e.g. 0.19)
359 $real_sellprice = $linetotal / (1 + $form->{$form->{"taxaccounts_$i"} . "_rate"});
361 $real_sellprice = $linetotal;
363 my $real_lastcost = $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / $price_factor, 2);
364 my $marge_percent_warn = $myconfig{marge_percent_warn} * 1 || 15;
365 my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
367 if ($real_sellprice * 1 && ($form->{"qty_$i"} * 1)) {
368 $form->{"marge_percent_$i"} = ($real_sellprice - $real_lastcost) * 100 / $real_sellprice;
369 $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && $form->{"marge_percent_$i"} < $marge_percent_warn;
372 $form->{"marge_absolut_$i"} = ($real_sellprice - $real_lastcost) * $marge_adjust_credit_note;
373 $form->{"marge_total"} += $form->{"marge_absolut_$i"};
374 $form->{"lastcost_total"} += $real_lastcost;
375 $form->{"sellprice_total"} += $real_sellprice;
377 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
379 push @ROW2, { value => sprintf qq|
380 <font %s><b>%s</b> %s %s%% </font>
382 <b>%s</b> <input size="5" name="lastcost_$i" value="%s">|,
383 $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
384 $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
385 $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces) }
386 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/ || $form->{type} =~ /^credit_note$/ ) && !$is_delivery_order;
388 $form->{"listprice_$i"} = $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2)
389 if $form->{"id_$i"} && ($form->{type} =~ /^sales_/ || $form->{type} =~ /invoice/) ;
390 # / marge calculations ending
393 if ($form->{"id_$i"}) {
394 my $part = IC->get_basic_part_info(id => $form->{"id_$i"});
395 my $onhand_color = $part->{onhand} < $part->{rop} ? 'color="#ff0000"' : '';
396 push @ROW2, { value => sprintf "<b>%s</b> <font %s>%s %s</font>",
397 $locale->text('On Hand'),
399 $form->format_amount(\%myconfig, $part->{onhand}, 2),
407 if ($is_delivery_order) {
408 map { $form->{"${_}_${i}"} = $form->format_amount(\%myconfig, $form->{"${_}_${i}"}) } qw(sellprice discount lastcost);
409 push @hidden_vars, qw(sellprice discount not_discountable price_factor_id lastcost pricegroup_id);
410 push @hidden_vars, "stock_${stock_in_out}_sum_qty", "stock_${stock_in_out}";
413 my @HIDDENS = map { value => $_}, (
414 $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
415 $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
416 map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
417 (qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
418 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
419 longdescription basefactor marge_absolut marge_percent marge_price_factor), @hidden_vars)
422 map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
424 $form->{invsubtotal} += $linetotal;
426 # Benutzerdefinierte Variablen für Waren/Dienstleistungen/Erzeugnisse
427 _render_custom_variables_inputs(ROW2 => \@ROW2, row => $i, part_id => $form->{"id_$i"});
429 push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, error => $form->{"row_error_$i"}, };
432 print $form->parse_html_template('oe/sales_order', { ROWS => \@ROWS,
436 if (0 != ($form->{sellprice_total} * 1)) {
437 $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
440 $main::lxdebug->leave_sub();
443 ##################################################
444 # build html-code for pricegroups in variable $form->{prices_$j}
447 $main::lxdebug->enter_sub();
449 my $form = $main::form;
450 my $locale = $main::locale;
451 my $cgi = $::request->{cgi};
455 my $rowcount = shift;
456 for my $j (1 .. $rowcount) {
457 next unless $form->{PRICES}{$j};
458 # build drop down list for pricegroups
459 my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
460 $form->{"prices_$j"} = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} }
461 (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
463 foreach my $item (@{ $form->{PRICES}{$j} }) {
464 # set new selectedpricegroup_id and prices for "Preis"
465 $form->{"pricegroup_old_$j"} = $item->{pricegroup_id} if $item->{selected} && $item->{pricegroup_id};
466 $form->{"sellprice_$j"} = $item->{price} if $item->{selected} && $item->{pricegroup_id};
467 $form->{"price_new_$j"} = $form->{"sellprice_$j"} if $item->{selected} || !$item->{pricegroup_id};
470 $main::lxdebug->leave_sub();
474 $main::lxdebug->enter_sub();
477 my $mode = $params{mode} || croak "Missing parameter 'mode'";
481 my $previous_form = $::auth->save_form_in_session(form => $::form);
482 $::form->{title} = $::locale->text('Select from one of the items below');
485 my @item_list = map {
486 $_->{display_sellprice} = $_->{sellprice} * (1 - $::form->{tradediscount});
487 $_->{display_sellprice} /= $_->{price_factor} if ($_->{price_factor});
489 } @{ $::form->{item_list} };
491 # delete action variable
492 delete @{$::form}{qw(action item_list)};
494 print $::form->parse_html_template('io/select_item', { PREVIOUS_FORM => $previous_form,
496 ITEM_LIST => \@item_list,
497 IS_PURCHASE => $mode eq 'IS' });
499 $main::lxdebug->leave_sub();
503 $main::lxdebug->enter_sub();
505 my $form = $main::form;
506 my %myconfig = %main::myconfig;
510 $::auth->restore_form_from_session($form->{select_item_previous_form} || croak('Missing previous form ID'), form => $form);
512 my $mode = delete($form->{select_item_mode}) || croak 'Missing item selection mode';
513 my $id = delete($form->{select_item_id}) || croak 'Missing item selection ID';
514 my $i = $form->{ $mode eq 'IC' ? 'assembly_rows' : 'rowcount' };
516 $form->{"id_${i}"} = $id;
519 IS->retrieve_item(\%myconfig, \%$form);
520 } elsif ($mode eq 'IR') {
521 IR->retrieve_item(\%myconfig, \%$form);
522 } elsif ($mode eq 'IC') {
523 IC->assembly_item(\%myconfig, \%$form);
525 croak "Invalid item selection mode '${mode}'";
528 my $new_item = $form->{item_list}->[0] || croak "No item found for mode '${mode}' and ID '${id}'";
530 # if there was a price entered, override it
531 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
534 qw(id partnumber description sellprice listprice inventory_accno
535 income_accno expense_accno bin unit weight assembly taxaccounts
536 partsgroup formel longdescription not_discountable partnotes lastcost
537 price_factor_id price_factor);
539 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
540 push @new_fields, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
542 map { $form->{"${_}_$i"} = $new_item->{$_} } @new_fields;
544 $form->{"marge_price_factor_$i"} = $new_item->{price_factor};
546 if ($form->{"part_payment_id_$i"} ne "") {
547 $form->{payment_id} = $form->{"part_payment_id_$i"};
550 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
552 my $decimalplaces = ($dec > 2) ? $dec : 2;
555 $form->{"sellprice_$i"} = $sellprice;
558 # if there is an exchange rate adjust sellprice
559 if (($form->{exchangerate} * 1) != 0) {
560 $form->{"sellprice_$i"} /= $form->{exchangerate};
561 $form->{"sellprice_$i"} =
562 $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
566 if ($::form->{tradediscount}) {
567 $::form->{"sellprice_$i"} *= 1 - $::form->{tradediscount};
571 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
572 qw(sellprice listprice weight);
574 $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
575 $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
577 if ($form->{"not_discountable_$i"}) {
578 $form->{"discount_$i"} = 0;
582 $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
584 map { $form->{"${_}_base"} += $amount }
585 (split / /, $form->{"taxaccounts_$i"});
586 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
587 $form->{"taxaccounts_$i"}
588 if !$form->{taxincluded};
590 $form->{creditremaining} -= $amount;
592 $form->{"runningnumber_$i"} = $i;
594 delete $form->{nextsub};
599 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
600 } qw(sellprice listprice lastcost) if $form->{item} ne 'assembly';
602 # get pricegroups for parts
603 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
605 # build up html code for prices_$i
606 set_pricegroup($form->{rowcount});
610 $main::lxdebug->leave_sub();
614 $main::lxdebug->enter_sub();
616 my $form = $main::form;
617 my %myconfig = %main::myconfig;
621 my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
624 $form->{old_callback} = $form->escape($form->{callback}, 1);
625 $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
627 # save all form variables except action in the session and keep the key in the previousform variable
628 my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
631 push @HIDDENS, { 'name' => 'previousform', 'value' => $previousform };
632 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{$_} }, qw(rowcount vc);
633 push @HIDDENS, map +{ 'name' => $_, 'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
634 push @HIDDENS, { 'name' => 'taxaccount2', 'value' => $form->{taxaccounts} };
635 push @HIDDENS, { 'name' => $price_key, 'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
636 push @HIDDENS, { 'name' => 'notes', 'value' => $form->{"longdescription_$form->{rowcount}"} };
639 print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
641 $main::lxdebug->leave_sub();
645 $main::lxdebug->enter_sub();
647 my $form = $main::form;
648 my %myconfig = %main::myconfig;
655 # remove any makes or model rows
656 if ($form->{item} eq 'part') {
657 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
658 qw(listprice sellprice lastcost weight rop);
660 } elsif ($form->{item} eq 'assembly') {
662 # fuer assemblies auskommentiert. seiteneffekte? ;-) wird die woanders benoetigt?
663 #$form->{sellprice} = 0;
665 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
666 qw(listprice sellprice rop stock);
668 my @flds = qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup lastcost);
670 for my $i (1 .. ($form->{assembly_rows} - 1)) {
671 if ($form->{"qty_$i"}) {
675 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
677 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
679 #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
681 # fuer assemblies auskommentiert. siehe oben
682 # $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"} / ($form->{"price_factor_$i"} || 1));
683 $form->{weight} += ($form->{"qty_$i"} * $form->{"weight_$i"} / ($form->{"price_factor_$i"} || 1));
687 # kann das hier auch weg? s.o. jb
688 $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
690 $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
691 $form->{assembly_rows} = $count;
693 } elsif ($form->{item} eq 'service') {
694 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
697 remove_emptied_rows(1);
699 $form->{creditremaining} -= &invoicetotal;
704 if ( $form->{type} =~ (/sales_quotation/)
705 or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
706 or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
707 or ($form->{type} =~ /sales_order/)) {
709 # get pricegroups for parts
710 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
712 # build up html code for prices_$i
713 set_pricegroup($form->{rowcount});
719 $main::lxdebug->leave_sub();
722 sub remove_emptied_rows {
723 my $dont_add_empty = shift;
726 return unless $form->{rowcount};
728 my @flds = qw(id partnumber description qty ship sellprice unit
729 discount inventory_accno income_accno expense_accno listprice
730 taxaccounts bin assembly weight projectnumber project_id
731 oldprojectnumber runningnumber serialnumber partsgroup payment_id
732 not_discountable shop ve gv buchungsgruppen_id language_values
733 sellprice_pg pricegroup_old price_old price_new unit_old ordnumber
734 transdate longdescription basefactor marge_total marge_percent
735 marge_price_factor lastcost price_factor_id partnotes
736 stock_out stock_in has_sernumber reqdate);
738 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
739 push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
742 for my $i (1 .. $form->{rowcount} - 1) {
743 next unless $form->{"partnumber_$i"};
745 push @new_rows, { map { $_ => $form->{"${_}_$i" } } @flds };
748 my $new_rowcount = scalar @new_rows;
749 $form->redo_rows(\@flds, \@new_rows, $new_rowcount, $form->{rowcount});
750 $form->{rowcount} = $new_rowcount + ($dont_add_empty ? 0 : 1);
754 $main::lxdebug->enter_sub();
756 my $form = $main::form;
757 my %myconfig = %main::myconfig;
761 $form->{oldinvtotal} = 0;
763 # add all parts and deduct paid
764 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
766 my ($amount, $sellprice, $discount, $qty);
768 for my $i (1 .. $form->{rowcount}) {
769 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
770 $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
771 $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
773 #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
775 $amount = $sellprice * (1 - $discount / 100) * $qty;
776 map { $form->{"${_}_base"} += $amount }
777 (split (/ /, $form->{"taxaccounts_$i"}));
778 $form->{oldinvtotal} += $amount;
781 map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
782 split(/ /, $form->{taxaccounts})
783 if !$form->{taxincluded};
785 $form->{oldtotalpaid} = 0;
786 for my $i (1 .. $form->{paidaccounts}) {
787 $form->{oldtotalpaid} += $form->{"paid_$i"};
790 $main::lxdebug->leave_sub();
793 return ($form->{oldinvtotal} - $form->{oldtotalpaid});
797 $main::lxdebug->enter_sub();
799 my $form = $main::form;
800 my $locale = $main::locale;
804 # check if items are valid
805 if ($form->{rowcount} == 1) {
806 flash('warning', $::locale->text('The action you\'ve chosen has not been executed because the document does not contain any item yet.'));
811 for my $i (1 .. $form->{rowcount} - 1) {
812 $form->isblank("partnumber_$i",
813 $locale->text('Number missing in Row') . " $i");
816 $main::lxdebug->leave_sub();
820 $main::lxdebug->enter_sub();
822 my $form = $main::form;
823 my %myconfig = %main::myconfig;
824 my $locale = $main::locale;
828 if ($form->{second_run}) {
829 $form->{print_and_post} = 0;
831 $form->{ordnumber} = $form->{invnumber};
833 $form->{old_employee_id} = $form->{employee_id};
834 $form->{old_salesman_id} = $form->{salesman_id};
836 map { delete $form->{$_} } qw(id printed emailed queued);
838 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
839 $form->{title} = $locale->text('Add Purchase Order');
840 $form->{vc} = 'vendor';
841 $form->{type} = 'purchase_order';
844 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
845 $form->{title} = $locale->text('Add Sales Order');
846 $form->{vc} = 'customer';
847 $form->{type} = 'sales_order';
850 $form->{script} = 'oe.pl';
858 require "bin/mozilla/$form->{script}";
859 my $script = $form->{"script"};
862 $locale = new Locale($::lx_office_conf{system}->{language}, $script);
864 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
866 my $currency = $form->{currency};
870 $form->{currency} = $currency;
871 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
872 $form->{exchangerate} = $form->{forex} || '';
874 for my $i (1 .. $form->{rowcount}) {
875 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
876 if ($form->{"${_}_${i}"}) }
877 qw(ship qty sellprice listprice basefactor discount));
883 $main::lxdebug->leave_sub();
887 $main::lxdebug->enter_sub();
889 my $form = $main::form;
890 my %myconfig = %main::myconfig;
891 my $locale = $main::locale;
895 if ($form->{second_run}) {
896 $form->{print_and_post} = 0;
898 map { delete $form->{$_} } qw(id printed emailed queued);
901 if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
902 $form->{title} = $locale->text('Add Request for Quotation');
903 $form->{vc} = 'vendor';
904 $form->{type} = 'request_quotation';
907 if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
908 $form->{title} = $locale->text('Add Quotation');
909 $form->{vc} = 'customer';
910 $form->{type} = 'sales_quotation';
916 $form->{script} = 'oe.pl';
922 require "bin/mozilla/$form->{script}";
924 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
926 my $currency = $form->{currency};
930 $form->{currency} = $currency;
931 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, $buysell);
932 $form->{exchangerate} = $form->{forex} || '';
934 for my $i (1 .. $form->{rowcount}) {
935 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
936 $form->{"${_}_${i}"})
937 if ($form->{"${_}_${i}"}) }
938 qw(ship qty sellprice listprice basefactor discount));
944 $main::lxdebug->leave_sub();
947 sub request_for_quotation {
952 $main::lxdebug->enter_sub();
954 my $form = $main::form;
955 my %myconfig = %main::myconfig;
956 my $locale = $main::locale;
960 if ($form->{second_run}) {
961 $form->{print_and_post} = 0;
962 $form->{resubmit} = 0;
965 $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
967 if ($form->{"cp_id"}) {
968 CT->get_contact(\%myconfig, $form);
969 $form->{"email"} = $form->{"cp_email"} if $form->{"cp_email"};
972 $form->{language} = $form->get_template_language(\%myconfig);
973 $form->{language} = "_" . $form->{language} if $form->{language};
975 my $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
977 $form->{oldmedia} = $form->{media};
978 $form->{media} = "email";
980 my $attachment_filename = $form->generate_attachment_filename();
981 my $subject = $form->{subject} || $form->generate_email_subject();
983 $::request->{layout}->focus($form->{"email"} ? "#subject" : "#email");
986 my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
987 @dont_hide_key_list = qw(action email cc bcc subject message sendmode format header override login password);
988 @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
989 @hidden_keys = sort grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
991 print $form->parse_html_template('generic/edit_email',
993 a_filename => $attachment_filename,
995 print_options => print_options('inline' => 1),
996 HIDDEN => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
997 SHOW_BCC => $::auth->assert('email_bcc', 'may fail') });
999 $main::lxdebug->leave_sub();
1003 $main::lxdebug->enter_sub();
1005 my $form = $main::form;
1006 my %myconfig = %main::myconfig;
1010 my $callback = $form->{script} . "?action=edit";
1011 map({ $callback .= "\&${_}=" . E($form->{$_}); } qw(type id));
1013 print_form("return");
1015 Common->save_email_status(\%myconfig, $form);
1017 $form->{callback} = $callback;
1020 $main::lxdebug->leave_sub();
1023 # generate the printing options displayed at the bottom of oe and is forms.
1024 # this function will attempt to guess what type of form is displayed, and will generate according options
1027 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
1028 # the opthash function builds hashrefs which are then pieced together for the template arrays.
1029 # unneeded options are "undef"ed out, and then grepped out.
1031 # the inline options is untested, but intended to be used later in metatemplating
1033 $main::lxdebug->enter_sub();
1035 my $form = $main::form;
1036 my %myconfig = %main::myconfig;
1037 my $locale = $main::locale;
1043 # names 3 parameters and returns a hashref, for use in templates
1044 sub opthash { +{ value => shift, selected => shift, oname => shift } }
1045 my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1047 # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1048 $form->{sendmode} = "attachment";
1049 $form->{format} = $form->{format} || $myconfig{template_format} || "pdf";
1050 $form->{copies} = $form->{copies} || $myconfig{copies} || 3;
1051 $form->{media} = $form->{media} || $myconfig{default_media} || "screen";
1052 $form->{printer_id} = defined $form->{printer_id} ? $form->{printer_id} :
1053 defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1055 $form->{PD}{ $form->{formname} } = "selected";
1056 $form->{DF}{ $form->{format} } = "selected";
1057 $form->{OP}{ $form->{media} } = "selected";
1058 $form->{SM}{ $form->{formname} } = "selected";
1060 push @FORMNAME, grep $_,
1061 ($form->{type} eq 'purchase_order') ? (
1062 opthash("purchase_order", $form->{PD}{purchase_order}, $locale->text('Purchase Order')),
1063 opthash("bin_list", $form->{PD}{bin_list}, $locale->text('Bin List'))
1065 ($form->{type} eq 'credit_note') ?
1066 opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef,
1067 ($form->{type} eq 'sales_order') ? (
1068 opthash("sales_order", $form->{PD}{sales_order}, $locale->text('Confirmation')),
1069 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1071 ($form->{type} =~ /sales_quotation$/) ?
1072 opthash('sales_quotation', $form->{PD}{sales_quotation}, $locale->text('Quotation')) : undef,
1073 ($form->{type} =~ /request_quotation$/) ?
1074 opthash('request_quotation', $form->{PD}{request_quotation}, $locale->text('Request for Quotation')) : undef,
1075 ($form->{type} eq 'invoice') ? (
1076 opthash("invoice", $form->{PD}{invoice}, $locale->text('Invoice')),
1077 opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1079 ($form->{type} eq 'invoice' && $form->{storno}) ? (
1080 opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
1082 ($form->{type} =~ /_delivery_order$/) ? (
1083 opthash($form->{type}, $form->{PD}{$form->{type}}, $locale->text('Delivery Order')),
1084 opthash('pick_list', $form->{PD}{pick_list}, $locale->text('Pick List')),
1088 opthash("attachment", $form->{SM}{attachment}, $locale->text('Attachment')),
1089 opthash("inline", $form->{SM}{inline}, $locale->text('In-line'))
1090 if ($form->{media} eq 'email');
1092 my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
1093 push @MEDIA, grep $_,
1094 opthash("screen", $form->{OP}{screen}, $locale->text('Screen')),
1095 ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
1096 opthash("printer", $form->{OP}{printer}, $locale->text('Printer')) : undef,
1097 ($printable_templates && !$options{no_queue}) ?
1098 opthash("queue", $form->{OP}{queue}, $locale->text('Queue')) : undef
1099 if ($form->{media} ne 'email');
1101 push @FORMAT, grep $_,
1102 ($::lx_office_conf{print_templates}->{opendocument} && $::lx_office_conf{applications}->{openofficeorg_writer} && $::lx_office_conf{applications}->{xvfb}
1103 && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
1104 && !$options{no_opendocument_pdf}) ?
1105 opthash("opendocument_pdf", $form->{DF}{"opendocument_pdf"}, $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1106 ($::lx_office_conf{print_templates}->{latex}) ?
1107 opthash("pdf", $form->{DF}{pdf}, $locale->text('PDF')) : undef,
1108 ($::lx_office_conf{print_templates}->{latex} && !$options{no_postscript}) ?
1109 opthash("postscript", $form->{DF}{postscript}, $locale->text('Postscript')) : undef,
1110 (!$options{no_html}) ?
1111 opthash("html", $form->{DF}{html}, "HTML") : undef,
1112 ($::lx_office_conf{print_templates}->{opendocument} && !$options{no_opendocument}) ?
1113 opthash("opendocument", $form->{DF}{opendocument}, $locale->text("OpenDocument/OASIS")) : undef,
1114 ($::lx_office_conf{print_templates}->{excel} && !$options{no_excel}) ?
1115 opthash("excel", $form->{DF}{excel}, $locale->text("Excel")) : undef;
1118 map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1119 if (ref $form->{languages} eq 'ARRAY');
1122 map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1123 if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1128 show => !$options{"hide_" . $_->[1]} && scalar @{ $_->[0] }
1130 [ \@FORMNAME, 'formname', ],
1131 [ \@LANGUAGE_ID, 'language_id', ],
1132 [ \@FORMAT, 'format', ],
1133 [ \@SENDMODE, 'sendmode', ],
1134 [ \@MEDIA, 'media', ],
1135 [ \@PRINTER_ID, 'printer_id', ];
1137 my %dont_display_groupitems = (
1141 my %template_vars = (
1142 display_copies => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
1143 display_remove_draft => (!$form->{id} && $form->{draft_id}),
1144 display_groupitems => !$dont_display_groupitems{$form->{type}},
1145 groupitems_checked => $form->{groupitems} ? "checked" : '',
1146 remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1149 my $print_options = $form->parse_html_template("generic/print_options", { SELECTS => \@SELECTS, %template_vars } );
1151 if ($options{inline}) {
1152 $main::lxdebug->leave_sub();
1153 return $print_options;
1156 print $print_options;
1158 $main::lxdebug->leave_sub();
1162 $main::lxdebug->enter_sub();
1164 my $form = $main::form;
1165 my $locale = $main::locale;
1169 if ($form->{print_nextsub}) {
1170 call_sub($form->{print_nextsub});
1171 $main::lxdebug->leave_sub();
1175 # if this goes to the printer pass through
1177 if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1178 $form->error($locale->text('Select postscript or PDF!'))
1179 if ($form->{format} !~ /(postscript|pdf)/);
1181 $old_form = new Form;
1182 map { $old_form->{$_} = $form->{$_} } keys %$form;
1185 if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1186 if ($form->{formname} eq "proforma") {
1187 $form->{proforma} = 1;
1189 $form->{print_and_save} = 1;
1190 my $formname = $form->{formname};
1192 $form->{formname} = $formname;
1194 $::lxdebug->leave_sub();
1198 &print_form($old_form);
1200 $main::lxdebug->leave_sub();
1204 $main::lxdebug->enter_sub();
1206 my $form = $main::form;
1207 my %myconfig = %main::myconfig;
1208 my $locale = $main::locale;
1212 my ($old_form) = @_;
1216 my $numberfld = "invnumber";
1220 ($form->{display_form}) ? $form->{display_form} : "display_form";
1222 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1223 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1225 if ($form->{formname} eq "invoice") {
1226 $form->{label} = $locale->text('Invoice');
1228 if ($form->{formname} eq 'sales_order') {
1231 $form->{"${inv}date"} = $form->{transdate};
1232 $form->{label} = $locale->text('Confirmation');
1233 $numberfld = "sonumber";
1237 if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1240 $form->{"${inv}date"} = $form->{invdate};
1241 $form->{label} = $locale->text('Proforma Invoice');
1242 $numberfld = "sonumber";
1246 if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1249 $form->{"${inv}date"} = $form->{transdate};
1250 $form->{"invdate"} = $form->{transdate};
1251 $form->{invnumber} = $form->{ordnumber};
1252 $form->{label} = $locale->text('Proforma Invoice');
1253 $numberfld = "sonumber";
1257 if ($form->{formname} eq 'purchase_order') {
1260 $form->{"${inv}date"} = $form->{transdate};
1261 $form->{label} = $locale->text('Purchase Order');
1262 $numberfld = "ponumber";
1265 if ($form->{formname} eq 'bin_list') {
1268 $form->{"${inv}date"} = $form->{transdate};
1269 $form->{label} = $locale->text('Bin List');
1272 if ($form->{formname} eq 'sales_quotation') {
1275 $form->{"${inv}date"} = $form->{transdate};
1276 $form->{label} = $locale->text('Quotation');
1277 $numberfld = "sqnumber";
1281 if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1284 $form->{"${inv}date"} = $form->{transdate};
1285 $form->{"invdate"} = $form->{transdate};
1286 $form->{label} = $locale->text('Proforma Invoice');
1287 $numberfld = "sqnumber";
1291 if ($form->{formname} eq 'request_quotation') {
1294 $form->{"${inv}date"} = $form->{transdate};
1295 $form->{label} = $locale->text('RFQ');
1296 $numberfld = "rfqnumber";
1300 if ($form->{type} =~ /_delivery_order$/) {
1303 $form->{"${inv}date"} = $form->{transdate};
1304 $numberfld = $form->{type} =~ /^sales/ ? 'sdonumber' : 'pdonumber';
1305 $form->{label} = $form->{formname} eq 'pick_list' ? $locale->text('Pick List') : $locale->text('Delivery Order');
1308 $form->isblank("email", $locale->text('E-mail address missing!'))
1309 if ($form->{media} eq 'email');
1310 $form->isblank("${inv}date",
1311 $locale->text($form->{label})
1313 . $locale->text(' Date missing!'));
1315 # $locale->text('Invoice Number missing!')
1316 # $locale->text('Invoice Date missing!')
1317 # $locale->text('Order Number missing!')
1318 # $locale->text('Order Date missing!')
1319 # $locale->text('Quotation Number missing!')
1320 # $locale->text('Quotation Date missing!')
1323 $form->{what_done} = $form->{formname};
1324 if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1325 $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1326 if ($form->{media} ne 'email') {
1328 # get pricegroups for parts
1329 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1331 # build up html code for prices_$i
1332 set_pricegroup($form->{rowcount});
1334 $form->{rowcount}--;
1336 call_sub($display_form);
1337 # saving the history
1338 if(!exists $form->{addition}) {
1339 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1340 $form->{addition} = "PRINTED";
1341 $form->save_history;
1343 # /saving the history
1350 # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1351 my ($saved_email, $saved_cc, $saved_bcc) =
1352 ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1354 my $language_saved = $form->{language_id};
1355 my $payment_id_saved = $form->{payment_id};
1356 my $salesman_id_saved = $form->{salesman_id};
1357 my $cp_id_saved = $form->{cp_id};
1358 my $taxzone_id_saved = $form->{taxzone_id};
1359 my $currency_saved = $form->{currency};
1361 call_sub("$form->{vc}_details") if ($form->{vc});
1363 $form->{language_id} = $language_saved;
1364 $form->{payment_id} = $payment_id_saved;
1365 $form->{taxzone_id} = $taxzone_id_saved;
1366 $form->{currency} = $currency_saved;
1368 $form->{"email"} = $saved_email if ($saved_email);
1369 $form->{"cc"} = $saved_cc if ($saved_cc);
1370 $form->{"bcc"} = $saved_bcc if ($saved_bcc);
1372 if (!$cp_id_saved) {
1373 # No contact was selected. Delete all contact variables because
1374 # IS->customer_details() and IR->vendor_details() get the default
1376 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1379 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1380 if ($form->{"language_id"}) {
1381 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1382 AM->get_language_details(\%myconfig, $form, $form->{language_id});
1384 $output_dateformat = $myconfig{"dateformat"};
1385 $output_numberformat = $myconfig{"numberformat"};
1386 $output_longdates = 1;
1389 # Store the output number format so that the template modules know
1390 # how to parse the amounts back if requested.
1391 $myconfig{output_numberformat} = $output_numberformat || $myconfig{numberformat};
1393 ($form->{employee}) = split /--/, $form->{employee};
1395 # create the form variables
1396 if ($form->{type} =~ /_delivery_order$/) {
1397 DO->order_details();
1399 OE->order_details(\%myconfig, \%$form);
1401 IS->invoice_details(\%myconfig, \%$form, $locale);
1404 $form->get_employee_data('prefix' => 'employee', 'id' => $form->{employee_id});
1405 $form->get_employee_data('prefix' => 'salesman', 'id' => $salesman_id_saved);
1407 if ($form->{shipto_id}) {
1408 $form->get_shipto(\%myconfig);
1411 my @a = qw(name department_1 department_2 street zipcode city country contact phone fax email);
1415 # if there is no shipto fill it in from billto
1416 foreach my $item (@a) {
1417 if ($form->{"shipto$item"}) {
1424 if ( $form->{formname} eq 'purchase_order'
1425 || $form->{formname} eq 'request_quotation') {
1426 $form->{shiptoname} = $myconfig{company};
1427 $form->{shiptostreet} = $myconfig{address};
1429 map { $form->{"shipto$_"} = $form->{$_} } @a;
1433 $form->{notes} =~ s/^\s+//g;
1435 $form->{templates} = "$myconfig{templates}";
1437 delete $form->{printer_command};
1439 $form->{language} = $form->get_template_language(\%myconfig);
1442 if ($form->{media} ne 'email') {
1443 $printer_code = $form->get_printer_code(\%myconfig);
1444 if ($printer_code ne "") {
1445 $printer_code = "_" . $printer_code;
1449 if ($form->{language} ne "") {
1450 my $template_arrays = $form->{TEMPLATE_ARRAYS} || $form;
1451 map { $template_arrays->{unit}->[$_] = AM->translate_units($form, $form->{language}, $template_arrays->{unit}->[$_], $template_arrays->{qty}->[$_]); } (0..scalar(@{ $template_arrays->{unit} }) - 1);
1453 $form->{language} = "_" . $form->{language};
1457 format_dates($output_dateformat, $output_longdates,
1458 qw(invdate orddate quodate pldate duedate reqdate transdate
1459 shippingdate deliverydate validitydate paymentdate
1460 datepaid transdate_oe deliverydate_oe dodate
1461 employee_startdate employee_enddate
1463 grep({ /^datepaid_\d+$/ ||
1464 /^transdate_oe_\d+$/ ||
1465 /^deliverydate_oe_\d+$/ ||
1467 /^deliverydate_\d+$/ ||
1471 reformat_numbers($output_numberformat, 2,
1472 qw(invtotal ordtotal quototal subtotal linetotal
1473 listprice sellprice netprice discount
1474 tax taxbase total paid payment),
1475 grep({ /^(?:linetotal|nodiscount_linetotal|listprice|sellprice|netprice|taxbase|discount|p_discount|discount_sub|nodiscount_sub|paid|subtotal|total|tax)_\d+$/ } keys(%{$form})));
1477 reformat_numbers($output_numberformat, undef,
1478 qw(qty price_factor),
1482 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1484 if (scalar @{ $cvar_date_fields }) {
1485 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1488 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1489 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1492 my $extension = 'html';
1493 if ($form->{format} eq 'postscript') {
1494 $form->{postscript} = 1;
1497 } elsif ($form->{"format"} =~ /pdf/) {
1499 $extension = $form->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
1501 } elsif ($form->{"format"} =~ /opendocument/) {
1502 $form->{opendocument} = 1;
1504 } elsif ($form->{"format"} =~ /excel/) {
1509 # search for the template
1511 push @template_files, "$form->{formname}_email$form->{language}$printer_code.$extension" if $form->{media} eq 'email';
1512 push @template_files, "$form->{formname}$form->{language}$printer_code.$extension";
1513 push @template_files, "$form->{formname}.$extension";
1514 push @template_files, "default.$extension";
1515 @template_files = uniq @template_files;
1516 $form->{IN} = first { -f "$myconfig{templates}/$_" } @template_files;
1518 if (!defined $form->{IN}) {
1519 $::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));
1522 delete $form->{OUT};
1524 if ($form->{media} eq 'printer') {
1525 $form->{OUT} = $form->{printer_command};
1526 $form->{OUT_MODE} = '|-';
1527 $form->{printed} .= " $form->{formname}";
1528 $form->{printed} =~ s/^ //;
1530 my $printed = $form->{printed};
1532 if ($form->{media} eq 'email') {
1533 $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1534 unless $form->{subject};
1536 $form->{emailed} .= " $form->{formname}";
1537 $form->{emailed} =~ s/^ //;
1539 my $emailed = $form->{emailed};
1541 if ($form->{media} eq 'queue') {
1542 my %queued = map { s|.*[/\\]||; $_ } split / /, $form->{queued};
1545 my $suffix = ($form->{postscript}) ? '.ps' : '.pdf';
1546 if ($filename = $queued{ $form->{formname} }) {
1547 unlink $::lx_office_conf{paths}->{spool} . "/$filename";
1548 delete $queued{ $form->{formname} };
1550 $form->{queued} = join ' ', %queued;
1551 $filename =~ s/\..*$//g;
1552 $filename .= $suffix;
1553 $form->{OUT} = $::lx_office_conf{paths}->{spool} . "/$filename";
1554 $form->{OUT_MODE} = '>';
1558 ($temp_fh, $filename) = File::Temp::tempfile(
1559 'kivitendo-spoolXXXXXX',
1560 SUFFIX => "$suffix",
1561 DIR => $::lx_office_conf{paths}->{spool},
1565 $form->{OUT} = "$filename";
1566 # use >> for OUT_MODE because file is already created by File::Temp
1567 $form->{OUT_MODE} = '>>';
1568 # strip directory so that only filename is stored in table status
1569 ($filename) = $filename =~ /^$::lx_office_conf{paths}->{spool}\/(.*)/;
1573 $form->{queued} .= " $form->{formname} $filename";
1574 $form->{queued} =~ s/^ //;
1576 my $queued = $form->{queued};
1578 # saving the history
1579 if(!exists $form->{addition}) {
1580 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1581 if($form->{media} =~ /printer/) {
1582 $form->{addition} = "PRINTED";
1584 elsif($form->{media} =~ /email/) {
1585 $form->{addition} = "MAILED";
1587 elsif($form->{media} =~ /queue/) {
1588 $form->{addition} = "QUEUED";
1590 elsif($form->{media} =~ /screen/) {
1591 $form->{addition} = "SCREENED";
1593 $form->save_history;
1595 # /saving the history
1597 # prepare meta information for template introspection
1598 $form->{template_meta} = {
1599 formname => $form->{formname},
1600 language => SL::DB::Manager::Language->find_by_or_create(id => $form->{language_id}),
1601 format => $form->{format},
1602 media => $form->{media},
1603 extension => $extension,
1604 printer => SL::DB::Manager::Printer->find_by_or_create(id => $form->{printer_id}),
1605 today => DateTime->today,
1608 $form->parse_template(\%myconfig);
1610 $form->{callback} = "";
1612 if ($form->{media} eq 'email') {
1613 $form->{message} = $locale->text('sent') unless $form->{message};
1615 my $message = $form->{message};
1617 # if we got back here restore the previous form
1618 if ($form->{media} =~ /(printer|email|queue)/) {
1620 $form->update_status(\%myconfig)
1621 if ($form->{media} eq 'queue' && $form->{id});
1623 return $main::lxdebug->leave_sub() if ($old_form eq "return");
1627 $old_form->{"${inv}number"} = $form->{"${inv}number"};
1629 # restore and display form
1630 map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1632 $form->{queued} = $queued;
1633 $form->{printed} = $printed;
1634 $form->{emailed} = $emailed;
1635 $form->{message} = $message;
1637 $form->{rowcount}--;
1638 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1639 qw(exchangerate creditlimit creditremaining);
1641 for my $i (1 .. $form->{paidaccounts}) {
1643 $form->{"${_}_$i"} =
1644 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1645 } qw(paid exchangerate);
1648 call_sub($display_form);
1653 ($form->{media} eq 'printer')
1654 ? $locale->text('sent to printer')
1655 : $locale->text('emailed to') . " $form->{email}";
1656 $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1658 if ($form->{printing}) {
1659 call_sub($display_form);
1663 $main::lxdebug->leave_sub();
1666 sub customer_details {
1667 $main::lxdebug->enter_sub();
1669 my $form = $main::form;
1670 my %myconfig = %main::myconfig;
1672 IS->customer_details(\%myconfig, \%$form, @_);
1674 $main::lxdebug->leave_sub();
1677 sub vendor_details {
1678 $main::lxdebug->enter_sub();
1680 my $form = $main::form;
1681 my %myconfig = %main::myconfig;
1683 IR->vendor_details(\%myconfig, \%$form, @_);
1685 $main::lxdebug->leave_sub();
1689 $main::lxdebug->enter_sub();
1691 my $form = $main::form;
1695 $form->{postasnew} = 1;
1696 map { delete $form->{$_} } qw(printed emailed queued);
1700 $main::lxdebug->leave_sub();
1704 $main::lxdebug->enter_sub();
1708 $::form->{print_and_post} = 0 if $::form->{second_run};
1710 map { $::form->{$_} = $::form->parse_amount(\%::myconfig, $::form->{$_}) } qw(exchangerate creditlimit creditremaining);
1712 # get details for customer/vendor
1713 call_sub($::form->{vc} . "_details", qw(name department_1 department_2 street zipcode city country contact email phone fax), $::form->{vc} . "number");
1715 # get pricegroups for parts
1716 IS->get_pricegroups_for_parts(\%::myconfig, \%$::form);
1718 # build up html code for prices_$i
1719 set_pricegroup($::form->{rowcount});
1721 $::form->{rowcount}--;
1723 my @shipto_vars = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1724 shiptocontact shiptocp_gender shiptophone shiptofax shiptoemail
1725 shiptodepartment_1 shiptodepartment_2);
1726 my $previous_form = $::auth->save_form_in_session(skip_keys => [ @shipto_vars, qw(header shipto_id) ]);
1727 $::form->{title} = $::locale->text('Ship to');
1730 print $::form->parse_html_template('io/ship_to', { previousform => $previous_form,
1731 nextsub => $::form->{display_form} || 'display_form',
1734 $main::lxdebug->leave_sub();
1737 sub ship_to_entered {
1738 $::auth->restore_form_from_session(delete $::form->{previousform});
1739 call_sub($::form->{nextsub});
1742 sub relink_accounts {
1743 $main::lxdebug->enter_sub();
1745 my $form = $main::form;
1746 my %myconfig = %main::myconfig;
1750 $form->{"taxaccounts"} =~ s/\s*$//;
1751 $form->{"taxaccounts"} =~ s/^\s*//;
1752 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1753 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1755 $form->{"taxaccounts"} = "";
1757 IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
1759 $main::lxdebug->leave_sub();
1763 $main::lxdebug->enter_sub();
1765 my $form = $main::form;
1766 my %myconfig = %main::myconfig;
1770 my $invdate = $form->{invdate} eq 'undefined' ? undef : $form->{invdate};
1771 my $duedate = $form->get_duedate(\%myconfig, $invdate);
1773 print $form->ajax_response_header() . $duedate;
1775 $main::lxdebug->leave_sub();
1778 sub _update_part_information {
1779 $main::lxdebug->enter_sub();
1781 my $form = $main::form;
1783 my %part_information = IC->get_basic_part_info('id' => [ grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount}) ],
1784 'vendor_id' => $form->{vendor_id});
1786 $form->{PART_INFORMATION} = \%part_information;
1788 foreach my $i (1..$form->{rowcount}) {
1789 next unless ($form->{"id_${i}"});
1791 my $info = $form->{PART_INFORMATION}->{$form->{"id_${i}"}} || { };
1792 $form->{"partunit_${i}"} = $info->{unit};
1795 $main::lxdebug->leave_sub();
1799 $main::lxdebug->enter_sub();
1801 my $form = $main::form;
1802 my %myconfig = %main::myconfig;
1804 if (!$form->{ordnumber} || !$form->{id}) {
1805 map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
1806 $main::lxdebug->leave_sub();
1810 my $all_units = AM->retrieve_all_units();
1812 my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales',
1813 'oe_id' => $form->{id},);
1815 foreach my $i (1..$form->{rowcount}) {
1816 next unless ($form->{"id_${i}"});
1818 $form->{"ship_$i"} = 0;
1820 my $ship_entry = $ship{$form->{"id_$i"}};
1822 next if (!$ship_entry || ($ship_entry->{qty} <= 0));
1825 ($form->{simple_save} ? $form->{"qty_$i"} : $form->parse_amount(\%myconfig, $form->{"qty_$i"}))
1826 * $all_units->{$form->{"unit_$i"}}->{factor}
1827 / $all_units->{$form->{"partunit_$i"}}->{factor};
1829 $form->{"ship_$i"} = min($rowqty, $ship_entry->{qty});
1830 $ship_entry->{qty} -= $form->{"ship_$i"};
1833 foreach my $i (1..$form->{rowcount}) {
1834 next unless ($form->{"id_${i}"});
1836 my $ship_entry = $ship{$form->{"id_$i"}};
1838 next if (!$ship_entry || ($ship_entry->{qty} <= 0.01));
1840 $form->{"ship_$i"} += $ship_entry->{qty};
1841 $ship_entry->{qty} = 0;
1844 $main::lxdebug->leave_sub();
1847 sub _update_custom_variables {
1848 $main::lxdebug->enter_sub();
1850 my $form = $main::form;
1852 $form->{CVAR_CONFIGS} = { } unless ref $form->{CVAR_CONFIGS} eq 'HASH';
1853 $form->{CVAR_CONFIGS}->{IC} ||= CVar->get_configs(module => 'IC');
1855 $main::lxdebug->leave_sub();
1858 sub _render_custom_variables_inputs {
1859 $main::lxdebug->enter_sub(2);
1861 my $form = $main::form;
1865 if (!$form->{CVAR_CONFIGS}->{IC}) {
1866 $main::lxdebug->leave_sub();
1870 my $valid = CVar->custom_variables_validity_by_trans_id(trans_id => $params{part_id});
1872 my $num_visible_cvars = 0;
1873 foreach my $cvar (@{ $form->{CVAR_CONFIGS}->{IC} }) {
1874 $cvar->{valid} = $params{part_id} && $valid->($cvar->{id});
1876 my $description = '';
1877 if ($cvar->{flag_editable} && $cvar->{valid}) {
1878 $num_visible_cvars++;
1879 $description = $cvar->{description} . ' ';
1882 my $form_key = "ic_cvar_" . $cvar->{name} . "_$params{row}";
1884 push @{ $params{ROW2} }, {
1885 line_break => $num_visible_cvars == 1,
1886 description => $description,
1889 hide_non_editable => 1,
1891 name_prefix => 'ic_',
1892 name_postfix => "_$params{row}",
1893 valid => $cvar->{valid},
1894 value => CVar->parse($::form->{$form_key}, $cvar),
1899 $main::lxdebug->leave_sub(2);