Optik: Jeden Button voneinander absetzen.
[kivitendo-erp.git] / bin / mozilla / io.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #############################################################################
7 # Veraendert 2005-01-05 - Marco Welter <mawe@linux-studio.de> - Neue Optik  #
8 #############################################################################
9 # SQL-Ledger, Accounting
10 # Copyright (c) 1998-2002
11 #
12 #  Author: Dieter Simader
13 #   Email: dsimader@sql-ledger.org
14 #     Web: http://www.sql-ledger.org
15 #
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #
30 #######################################################################
31 #
32 # common routines used in is, ir, oe
33 #
34 #######################################################################
35
36 use SL::CT;
37 use SL::IC;
38 use CGI::Ajax;
39 use CGI;
40
41 require "bin/mozilla/common.pl";
42
43 # any custom scripts for this one
44 if (-f "bin/mozilla/custom_io.pl") {
45   eval { require "bin/mozilla/custom_io.pl"; };
46 }
47 if (-f "bin/mozilla/$form->{login}_io.pl") {
48   eval { require "bin/mozilla/$form->{login}_io.pl"; };
49 }
50
51 1;
52
53 # end of main
54
55 # this is for our long dates
56 # $locale->text('January')
57 # $locale->text('February')
58 # $locale->text('March')
59 # $locale->text('April')
60 # $locale->text('May ')
61 # $locale->text('June')
62 # $locale->text('July')
63 # $locale->text('August')
64 # $locale->text('September')
65 # $locale->text('October')
66 # $locale->text('November')
67 # $locale->text('December')
68
69 # this is for our short month
70 # $locale->text('Jan')
71 # $locale->text('Feb')
72 # $locale->text('Mar')
73 # $locale->text('Apr')
74 # $locale->text('May')
75 # $locale->text('Jun')
76 # $locale->text('Jul')
77 # $locale->text('Aug')
78 # $locale->text('Sep')
79 # $locale->text('Oct')
80 # $locale->text('Nov')
81 # $locale->text('Dec')
82 use SL::IS;
83 use SL::PE;
84 use SL::AM;
85 use Data::Dumper;
86 ########################################
87 # Eintrag fuer Version 2.2.0 geaendert #
88 # neue Optik im Rechnungsformular      #
89 ########################################
90 sub display_row {
91   $lxdebug->enter_sub();
92   my $numrows = shift;
93
94   my $is_sales =
95     (substr($form->{type}, 0, 6) eq "sales_")
96     || (($form->{type} eq "invoice") && ($form->{script} eq "is.pl"))
97     || ($form->{type} eq 'credit_note');
98
99   if ($lizenzen && $form->{vc} eq "customer") {
100     if ($form->{type} =~ /sales_order/) {
101       @column_index = (runningnumber, partnumber, description, ship, qty);
102     } elsif ($form->{type} =~ /sales_quotation/) {
103       @column_index = (runningnumber, partnumber, description, qty);
104     } else {
105       @column_index = (runningnumber, partnumber, description, qty);
106     }
107   } else {
108     if (   ($form->{type} =~ /purchase_order/)
109         || ($form->{type} =~ /sales_order/)) {
110       @column_index = (runningnumber, partnumber, description, ship, qty);
111         } else {
112       @column_index = (runningnumber, partnumber, description, qty);
113     }
114   }
115 ############## ENDE Neueintrag ##################
116
117   my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
118   my $service_units = AM->retrieve_units(\%myconfig, $form, "service");
119   my $all_units = AM->retrieve_units(\%myconfig, $form);
120
121   push @column_index, qw(unit);
122
123   #for pricegroups column
124   if (   $form->{type} =~ (/sales_quotation/)
125       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
126       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
127       or ($form->{type} =~ /sales_order/)) {
128     push @column_index, qw(sellprice_pg);
129   }
130
131   push @column_index, qw(sellprice);
132
133   if ($form->{vc} eq 'customer') {
134     push @column_index, qw(discount);
135   }
136
137   push @column_index, "linetotal";
138
139   my $colspan = $#column_index + 1;
140
141   $form->{invsubtotal} = 0;
142   map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
143
144 ########################################
145   # Eintrag fuer Version 2.2.0 geaendert #
146   # neue Optik im Rechnungsformular      #
147 ########################################
148   $column_data{runningnumber} =
149       qq|<th align="left" nowrap width="5" class="listheading">|
150     . $locale->text('No.')
151     . qq|</th>|;
152   $column_data{partnumber} =
153       qq|<th align="left" nowrap width="12" class="listheading">|
154     . $locale->text('Number')
155     . qq|</th>|;
156   $column_data{description} =
157       qq|<th align="left" nowrap width="30" class="listheading">|
158     . $locale->text('Part Description')
159     . qq|</th>|;
160   if ($form->{"type"} eq "purchase_order") {
161     $column_data{ship} =
162       qq|<th align="left" nowrap width="5" class="listheading">|
163       . $locale->text('Ship rcvd')
164       . qq|</th>|;
165   } else {
166     $column_data{ship} =
167       qq|<th align="left" nowrap width="5" class="listheading">|
168       . $locale->text('Ship')
169       . qq|</th>|;
170   }
171   $column_data{qty} =
172       qq|<th align="left" nowrap width="5" class="listheading">|
173     . $locale->text('Qty')
174     . qq|</th>|;
175   $column_data{unit} =
176       qq|<th align="left" nowrap width="5" class="listheading">|
177     . $locale->text('Unit')
178     . qq|</th>|;
179   $column_data{license} =
180       qq|<th align="left" nowrap width="10" class="listheading">|
181     . $locale->text('License')
182     . qq|</th>|;
183   $column_data{serialnr} =
184       qq|<th align="left" nowrap width="10" class="listheading">|
185     . $locale->text('Serial No.')
186     . qq|</th>|;
187   $column_data{projectnr} =
188       qq|<th align="left" nowrap width="10" class="listheading">|
189     . $locale->text('Project')
190     . qq|</th>|;
191   $column_data{sellprice} =
192       qq|<th align="left" nowrap width="15" class="listheading">|
193     . $locale->text('Price')
194     . qq|</th>|;
195   $column_data{sellprice_pg} =
196       qq|<th align="left" nowrap width="15" class="listheading">|
197     . $locale->text('Pricegroup')
198     . qq|</th>|;
199   $column_data{discount} =
200       qq|<th align="left" class="listheading">|
201     . $locale->text('Discount')
202     . qq|</th>|;
203   $column_data{linetotal} =
204       qq|<th align="left" nowrap width="10" class="listheading">|
205     . $locale->text('Extended')
206     . qq|</th>|;
207   $column_data{bin} =
208       qq|<th align="left" nowrap width="10" class="listheading">|
209     . $locale->text('Bin')
210     . qq|</th>|;
211 ############## ENDE Neueintrag ##################
212
213   $myconfig{"show_form_details"} = 1
214     unless (defined($myconfig{"show_form_details"}));
215   $form->{"show_details"} = $myconfig{"show_form_details"}
216     unless (defined($form->{"show_details"}));
217   $form->{"show_details"} = $form->{"show_details"} ? 1 : 0;
218   my $show_details_new = 1 - $form->{"show_details"};
219   my $show_details_checked = $form->{"show_details"} ? "checked" : "";
220
221   print qq|
222   <tr>
223     <td>| . $cgi->hidden("-name" => "show_details", "-value" => $form->{show_details}) . qq|
224       <input type="checkbox" id="cb_show_details" onclick="show_form_details($show_details_new);" $show_details_checked>
225       <label for="cb_show_details">| . $locale->text("Show details") . qq|</label><br>
226       <table width="100%">
227         <tr class="listheading">|;
228
229   map { print "\n$column_data{$_}" } @column_index;
230
231   print qq|
232         </tr>
233 |;
234
235   $runningnumber = $locale->text('No.');
236   $deliverydate  = $locale->text('Delivery Date');
237   $serialnumber  = $locale->text('Serial No.');
238   $projectnumber = $locale->text('Project');
239   $partsgroup    = $locale->text('Group');
240   $reqdate       = $locale->text('Reqdate');
241
242   $delvar = 'deliverydate';
243
244   if ($form->{type} =~ /_order$/ || $form->{type} =~ /_quotation$/) {
245     $deliverydate = $locale->text('Required by');
246     $delvar       = 'reqdate';
247   }
248
249   $form->{marge_total} = 0;
250   $form->{sellprice_total} = 0;
251   $form->{lastcost_total} = 0;
252   my %projectnumber_labels = ();
253   my @projectnumber_values = ("");
254   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
255     push(@projectnumber_values, $item->{"id"});
256     $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
257   }
258
259   for $i (1 .. $numrows) {
260
261     # undo formatting
262     map {
263       $form->{"${_}_$i"} =
264         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
265     } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
266
267     if (!$form->{"unit_old_$i"}) {
268       # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
269       # Einheit, wie sie in den Stammdaten hinterlegt wurde.
270       # Es sollte also angenommen werden, dass diese ausgewaehlt war.
271       $form->{"unit_old_$i"} = $form->{"unit_$i"};
272     }
273
274     # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
275     # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
276     $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
277
278     my $check_units = $form->{"inventory_accno_$i"} ? $dimension_units : $service_units;
279     if (!$check_units->{$form->{"selected_unit_$i"}} ||
280         ($check_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
281          $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
282       # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
283       # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
284       # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
285       $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
286     }
287     if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
288       if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
289         my $basefactor = 1;
290         if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
291             $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
292           $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
293             $all_units->{$form->{"unit_old_$i"}}->{"factor"};
294         }
295         $form->{"sellprice_$i"} *= $basefactor;
296         $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
297       }
298     }
299     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
300     $dec           = length $dec;
301     $decimalplaces = ($dec > 2) ? $dec : 2;
302
303     $discount =
304       $form->round_amount(
305                         $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
306                         $decimalplaces);
307
308     $linetotal =
309       $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
310     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
311     my $real_sellprice = $form->{"sellprice_$i"} - $discount;
312
313     # marge calculations
314     my ($marge_font_start, $marge_font_end);
315
316     $form->{"lastcost_$i"} *= 1;
317
318     if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
319       $form->{"marge_percent_$i"}     = ($real_sellprice - $form->{"lastcost_$i"}) * 100 / $real_sellprice;
320       $myconfig{"marge_percent_warn"} = 15 unless (defined($myconfig{"marge_percent_warn"}));
321
322       if ($form->{"id_$i"} &&
323           ($form->{"marge_percent_$i"} < (1 * $myconfig{"marge_percent_warn"}))) {
324         $marge_font_start = "<font color=\"#ff0000\">";
325         $marge_font_end   = "</font>";
326       }
327
328     } else {
329       $form->{"marge_percent_$i"} = 0;
330     }
331
332     my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
333     $form->{"marge_absolut_$i"}  = ($real_sellprice - $form->{"lastcost_$i"}) * $form->{"qty_$i"} * $marge_adjust_credit_note;
334     $form->{"marge_total"}      += $form->{"marge_absolut_$i"};
335     $form->{"lastcost_total"}   += $form->{"lastcost_$i"} * $form->{"qty_$i"};
336     $form->{"sellprice_total"}  += $real_sellprice * $form->{"qty_$i"};
337
338     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
339
340     # convert " to &quot;
341     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
342       qw(partnumber description unit unit_old);
343
344 ########################################
345     # Eintrag fuer Version 2.2.0 geaendert #
346     # neue Optik im Rechnungsformular      #
347 ########################################
348     $column_data{runningnumber} =
349       qq|<td><input name="runningnumber_$i" size="5" value="$i"></td>|;    # HuT
350 ############## ENDE Neueintrag ##################
351
352     $column_data{partnumber} =
353       qq|<td><input name="partnumber_$i" size=12 value="$form->{"partnumber_$i"}"></td>|;
354
355     if (($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) {
356       $column_data{description} =
357         qq|<td><textarea name="description_$i" rows="$rows" cols="30" wrap="soft">| . H($form->{"description_$i"}) . qq|</textarea><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
358     } else {
359       $column_data{description} =
360         qq|<td><input name="description_$i" size="30" value="| . $form->quote($form->{"description_$i"}) . qq|"><button type="button" onclick="set_longdescription_window('longdescription_$i')">| . $locale->text('L') . qq|</button></td>|;
361     }
362
363     (my $qty_dec) = ($form->{"qty_$i"} =~ /\.(\d+)/);
364     $qty_dec = length $qty_dec;
365
366     $column_data{qty} =
367         qq|<td align="right"><input name="qty_$i" size="5" value="|
368       . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec) .qq|">|;
369     if ($form->{"formel_$i"}) {
370       $column_data{qty} .= qq|<button type="button" onclick="calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)">| . $locale->text('*/') . qq|</button>|
371         . $cgi->hidden("-name" => "formel_$i", "-value" => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"});
372     }
373     $column_data{qty} .= qq|</td>|;
374     $column_data{ship} =
375         qq|<td align="right"><input name="ship_$i" size=5 value="|
376       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
377       . qq|"></td>|;
378
379     my $is_part     = $form->{"inventory_accno_$i"};
380     my $is_assembly = $form->{"assembly_$i"};
381     my $is_assigned = $form->{"id_$i"};
382     my $this_unit = $form->{"unit_$i"};
383     if ($form->{"selected_unit_$i"} && $this_unit &&
384         $all_units->{$form->{"selected_unit_$i"}} && $all_units->{$this_unit} &&
385         ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} eq $all_units->{$this_unit}->{"base_unit"})) {
386       $this_unit = $form->{"selected_unit_$i"};
387     } elsif (!$is_assigned ||
388              ($is_part && !$this_unit && ($all_units->{$this_unit} && ($all_units->{$this_unit}->{"base_unit"} eq $all_units->{"kg"}->{"base_unit"})))) {
389       $this_unit = "kg";
390     }
391
392     $column_data{"unit"} = "<td>" .
393        AM->unit_select_html($is_part || $is_assembly ? $dimension_units :
394                             $is_assigned ? $service_units : $all_units,
395                             "unit_$i", $this_unit,
396                             $is_assigned ? $form->{"unit_$i"} : undef)
397       . "</td>";
398
399     # build in drop down list for pricesgroups
400     if ($form->{"prices_$i"}) {
401       if  ($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"}) {
402         $price_tmp = $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces);
403       } else {
404         $price_tmp = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
405       }
406
407       $column_data{sellprice_pg} =
408       qq|<td align="right"><select name="sellprice_pg_$i">$form->{"prices_$i"}</select></td>|;
409       $column_data{sellprice} =
410       qq|<td><input name="sellprice_$i" size="10" value="$price_tmp" onBlur=\"check_right_number_format(this)\"></td>|;
411     } else {
412
413       # for last row and report
414       # set pricegroup drop down list from report menu
415       if ($form->{"sellprice_$i"} != 0) {
416         $prices =
417           qq|<option value="$form->{"sellprice_$i"}--$form->{"pricegroup_id_$i"}" selected>$form->{"pricegroup_$i"}</option>\n|;
418
419         $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
420
421         $column_data{sellprice_pg} =
422           qq|<td align="right"><select name="sellprice_pg_$i">$prices</select></td>|;
423
424       } else {
425
426         # for last row
427         $column_data{sellprice_pg} = qq|<td align="right">&nbsp;</td>|;
428         }
429         
430       $column_data{sellprice} =
431       qq|<td><input name="sellprice_$i" size="10" onBlur=\"check_right_number_format(this)\" value="|
432         . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
433                                $decimalplaces)
434         . qq|"></td>|;
435     }
436     $column_data{discount} =
437         qq|<td align="right"><input name="discount_$i" size=3 value="|
438       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
439       . qq|"></td>|;
440     $column_data{linetotal} =
441         qq|<td align="right">|
442       . $form->format_amount(\%myconfig, $linetotal, 2)
443       . qq|</td>|;
444     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
445
446 ########################################
447     # Eintrag fuer Version 2.2.0 geaendert #
448     # neue Optik im Rechnungsformular      #
449 ########################################
450     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
451     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
452     #     }
453     #
454     #     if ($form->{type} !~ /_quotation/) {
455     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
456     #     }
457     #
458     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
459 ############## ENDE Neueintrag ##################
460     my $j = $i % 2;
461     print qq|
462
463         <tr valign="top" class="listrow$j">|;
464
465     map { print "\n$column_data{$_}" } @column_index;
466
467     print("</tr>\n" .
468           $cgi->hidden("-name" => "unit_old_$i",
469                        "-value" => $form->{"selected_unit_$i"})
470           . "\n" .
471           $cgi->hidden("-name" => "price_new_$i",
472                        "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"}))
473           . "\n");
474     map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_}) . "\n"); }
475         ("orderitems_id_$i", "bo_$i", "pricegroup_old_$i", "price_old_$i",
476          "id_$i", "inventory_accno_$i", "bin_$i", "partsgroup_$i", "partnotes_$i",
477          "income_accno_$i", "expense_accno_$i", "listprice_$i", "assembly_$i",
478          "taxaccounts_$i", "ordnumber_$i", "transdate_$i", "cusordnumber_$i",
479          "longdescription_$i", "basefactor_$i", "marge_absolut_$i", "marge_percent_$i", "lastcost_$i"));
480
481 ########################################
482     # Eintrag fuer Version 2.2.0 geaendert #
483     # neue Optik im Rechnungsformular      #
484 ########################################
485
486     my $row_style_attr =
487       'style="display:none;"' if (!$form->{"show_details"});
488
489     # print second row
490     print qq|
491         <tr  class="listrow$j" $row_style_attr>
492           <td colspan="$colspan">
493 |;
494     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
495       my $selected = $form->{"licensenumber_$i"};
496       my $lizenzen_quoted;
497       $form->{"lizenzen_$i"} =~ s/ selected//g;
498       $form->{"lizenzen_$i"} =~
499         s/value="${selected}"\>/value="${selected}" selected\>/;
500       $lizenzen_quoted = $form->{"lizenzen_$i"};
501       $lizenzen_quoted =~ s/\"/&quot;/g;
502       print qq|
503         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size="1">
504         $form->{"lizenzen_$i"}
505         </select>
506         <input type="hidden" name="lizenzen_$i" value="${lizenzen_quoted}">
507 |;
508     }
509     if ($form->{type} !~ /_quotation/) {
510       print qq|
511           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">|;
512     }
513
514     print qq|<b>$projectnumber</b>&nbsp;| .
515       NTI($cgi->popup_menu('-name' => "project_id_$i",
516                            '-values' => \@projectnumber_values,
517                            '-labels' => \%projectnumber_labels,
518                            '-default' => $form->{"project_id_$i"}));
519
520     if ($form->{type} eq 'invoice' or $form->{type} =~ /order/) {
521       my $reqdate_term =
522         ($form->{type} eq 'invoice')
523         ? 'deliverydate'
524         : 'reqdate';    # invoice uses a different term for the same thing.
525       print qq|
526         <b>${$reqdate_term}</b>&nbsp;<input name="${reqdate_term}_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"${reqdate_term}_$i"}">
527 |;
528     }
529     my $subtotalchecked = ($form->{"subtotal_$i"}) ? "checked" : "";
530     print qq|
531           <b>|.$locale->text('Subtotal').qq|</b>&nbsp;<input type="checkbox" name="subtotal_$i" value="1" "$subtotalchecked">
532 |;
533
534     if ($form->{"id_$i"} && $is_sales) {
535       print qq|
536           ${marge_font_start}<b>| . $locale->text('Ertrag') . qq|</b>&nbsp;$form->{"marge_absolut_$i"} &nbsp;$form->{"marge_percent_$i"} % ${marge_font_end}
537           &nbsp;<b>| . $locale->text('LP') . qq|</b>&nbsp;| . $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2) . qq|
538           &nbsp;<b>| . $locale->text('EK') . qq|</b>&nbsp;| . $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2);
539     }
540
541     print qq|
542           </td>
543         </tr>
544 |;
545
546 ############## ENDE Neueintrag ##################
547
548     map { $form->{"${_}_base"} += $linetotal }
549       (split(/ /, $form->{"taxaccounts_$i"}));
550
551     $form->{invsubtotal} += $linetotal;
552   }
553
554   print qq|
555       </table>
556     </td>
557   </tr>
558 |;
559
560   if (0 != ($form->{sellprice_total} * 1)) {
561     $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
562   }
563
564   $lxdebug->leave_sub();
565 }
566
567 ##################################################
568 # build html-code for pricegroups in variable $form->{prices_$j}
569
570 sub set_pricegroup {
571   $lxdebug->enter_sub();
572   my $rowcount = shift;
573   for $j (1 .. $rowcount) {
574     my $pricegroup_old = $form->{"pricegroup_old_$i"};
575     if ($form->{PRICES}{$j}) {
576       $len    = 0;
577       $prices = '<option value="--">' . $locale->text("none (pricegroup)") . '</option>';
578       $price  = 0;
579       foreach $item (@{ $form->{PRICES}{$j} }) {
580
581         #$price = $form->round_amount($myconfig,  $item->{price}, 5);
582         #$price = $form->format_amount($myconfig, $item->{price}, 2);
583         $price         = $item->{price};
584         $pricegroup_id = $item->{pricegroup_id};
585         $pricegroup    = $item->{pricegroup};
586
587         # build drop down list for pricegroups
588         $prices .=
589           qq|<option value="$price--$pricegroup_id"$item->{selected}>$pricegroup</option>\n|;
590
591         $len += 1;
592
593         #        map {
594         #               $form->{"${_}_$j"} =
595         #               $form->format_amount(\%myconfig, $form->{"${_}_$j"})
596         #              } qw(sellprice price_new price_old);
597
598         # set new selectedpricegroup_id and prices for "Preis"
599         if ($item->{selected} && ($pricegroup_id != 0)) {
600           $form->{"pricegroup_old_$j"} = $pricegroup_id;
601           $form->{"price_new_$j"}      = $price;
602           $form->{"sellprice_$j"}      = $price;
603         }
604         if ($pricegroup_id == 0) {
605           $form->{"price_new_$j"} = $form->{"sellprice_$j"};
606         }
607       }
608       $form->{"prices_$j"} = $prices;
609     }
610   }
611   $lxdebug->leave_sub();
612 }
613
614 sub select_item {
615   $lxdebug->enter_sub();
616   @column_index = qw(ndx partnumber description onhand unit sellprice);
617
618   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
619   $column_data{partnumber} =
620     qq|<th class="listheading">| . $locale->text('Number') . qq|</th>|;
621   $column_data{description} =
622     qq|<th class="listheading">| . $locale->text('Part Description') . qq|</th>|;
623   $column_data{sellprice} =
624     qq|<th class="listheading">| . $locale->text('Price') . qq|</th>|;
625   $column_data{onhand} =
626     qq|<th class="listheading">| . $locale->text('Qty') . qq|</th>|;
627   $column_data{unit} =
628     qq|<th class="listheading">| . $locale->text('Unit') . qq|</th>|;
629   # list items with radio button on a form
630   $form->header;
631
632   $title   = $locale->text('Select from one of the items below');
633   $colspan = $#column_index + 1;
634
635   print qq|
636   <body>
637
638 <form method="post" action="$form->{script}">
639
640 <table width="100%">
641   <tr>
642     <th class="listtop" colspan="$colspan">$title</th>
643   </tr>
644   <tr height="5"></tr>
645   <tr class="listheading">|;
646
647   map { print "\n$column_data{$_}" } @column_index;
648
649   print qq|</tr>|;
650
651   my $i = 0;
652   foreach $ref (@{ $form->{item_list} }) {
653     $checked = ($i++) ? "" : "checked";
654
655     if ($lizenzen) {
656       if ($ref->{inventory_accno} > 0) {
657         $ref->{"lizenzen"} = qq|<option></option>|;
658         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
659           $ref->{"lizenzen"} .=
660             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
661         }
662         $ref->{"lizenzen"} .= qq|<option value="-1">Neue Lizenz</option>|;
663         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
664       }
665     }
666
667     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
668
669     #sk tradediscount
670     $ref->{sellprice} =
671       $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
672     $column_data{ndx} =
673       qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
674     $column_data{partnumber} =
675       qq|<td><input name="new_partnumber_$i" type="hidden" value="$ref->{partnumber}">$ref->{partnumber}</td>|;
676     $column_data{description} =
677       qq|<td><input name="new_description_$i" type="hidden" value="$ref->{description}">$ref->{description}</td>|;
678     $column_data{sellprice} =
679       qq|<td align="right"><input name="new_sellprice_$i" type="hidden" value="$ref->{sellprice}">|
680       . $form->format_amount(\%myconfig, $ref->{sellprice}, 2, "&nbsp;")
681       . qq|</td>|;
682     $column_data{onhand} =
683       qq|<td align="right"><input name="new_onhand_$i" type="hidden" value="$ref->{onhand}">|
684       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
685       . qq|</td>|;
686     $column_data{unit} =
687       qq|<td>$ref->{unit}</td>|;
688     $j++;
689     $j %= 2;
690     print qq|
691 <tr class=listrow$j>|;
692
693     map { print "\n$column_data{$_}" } @column_index;
694
695     print("</tr>\n");
696
697     my @new_fields =
698       qw(bin listprice inventory_accno income_accno expense_accno unit weight
699          assembly taxaccounts partsgroup formel longdescription not_discountable
700          part_payment_id partnotes id lastcost);
701     push(@new_fields, "lizenzen") if ($lizenzen);
702
703     print join "\n", map { $cgi->hidden("-name" => "new_${_}_$i", "-value" => $ref->{$_}) } @new_fields;
704     print "\n";
705   }
706
707   print qq|
708 <tr><td colspan="8"><hr size="3" noshade></td></tr>
709 </table>
710
711 <input name="lastndx" type="hidden" value="$i">
712
713 |;
714
715   # delete action variable
716   map { delete $form->{$_} } qw(action item_list header);
717
718   # save all other form variables
719   foreach $key (keys %${form}) {
720     $form->{$key} =~ s/\"/&quot;/g;
721     print qq|<input name="$key" type="hidden" value="$form->{$key}">\n|;
722   }
723
724   print qq|
725 <input type="hidden" name="nextsub" value="item_selected">
726
727 <br>
728 <input class="submit" type="submit" name="action" value="|
729     . $locale->text('Continue') . qq|">
730 </form>
731
732 </body>
733 </html>
734 |;
735
736   $lxdebug->leave_sub();
737 }
738
739 sub item_selected {
740   $lxdebug->enter_sub();
741
742   # replace the last row with the checked row
743   $i = $form->{rowcount};
744   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
745
746   # index for new item
747   $j = $form->{ndx};
748
749   #sk
750   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
751   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
752
753   # if there was a price entered, override it
754   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
755
756   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
757     qw(id partnumber description sellprice listprice inventory_accno
758        income_accno expense_accno bin unit weight assembly taxaccounts
759        partsgroup formel longdescription not_discountable partnotes lastcost);
760   if ($form->{"part_payment_id_$i"} ne "") {
761     $form->{payment_id} = $form->{"part_payment_id_$i"};
762   }
763
764   if ($lizenzen) {
765     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
766   }
767
768   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
769   $dec           = length $dec;
770   $decimalplaces = ($dec > 2) ? $dec : 2;
771
772   if ($sellprice) {
773     $form->{"sellprice_$i"} = $sellprice;
774   } else {
775
776     # if there is an exchange rate adjust sellprice
777     if (($form->{exchangerate} * 1) != 0) {
778       $form->{"sellprice_$i"} /= $form->{exchangerate};
779       $form->{"sellprice_$i"} =
780         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
781     }
782   }
783
784   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
785     qw(sellprice listprice weight);
786
787   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
788   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
789   
790   if ($form->{"not_discountable_$i"}) {
791     $form->{"discount_$i"} = 0;
792   }
793
794   $amount =
795     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
796     $form->{"qty_$i"};
797   map { $form->{"${_}_base"} += $amount }
798     (split / /, $form->{"taxaccounts_$i"});
799   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
800     $form->{"taxaccounts_$i"}
801     if !$form->{taxincluded};
802
803   $form->{creditremaining} -= $amount;
804
805   $form->{"runningnumber_$i"} = $i;
806
807   # delete all the new_ variables
808   for $i (1 .. $form->{lastndx}) {
809     map { delete $form->{"new_${_}_$i"} }
810       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
811   }
812
813   map { delete $form->{$_} } qw(ndx lastndx nextsub);
814
815   # format amounts
816   map {
817     $form->{"${_}_$i"} =
818       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
819   } qw(sellprice listprice) if $form->{item} ne 'assembly';
820
821   # get pricegroups for parts
822   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
823
824   # build up html code for prices_$i
825   set_pricegroup($form->{rowcount});
826
827   &display_form;
828
829   $lxdebug->leave_sub();
830 }
831
832 sub new_item {
833   $lxdebug->enter_sub();
834
835   # change callback
836   $form->{old_callback} = $form->escape($form->{callback}, 1);
837   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
838
839   # delete action
840   delete $form->{action};
841
842   # save all other form variables in a previousform variable
843   foreach $key (keys %$form) {
844
845     # escape ampersands
846     $form->{$key} =~ s/&/%26/g;
847     $previousform .= qq|$key=$form->{$key}&|;
848   }
849   chop $previousform;
850   $previousform = $form->escape($previousform, 1);
851
852   $i = $form->{rowcount};
853   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
854
855   $form->header;
856
857   print qq|
858 <body>
859
860 <h4 class="error">| . $locale->text('Item not on file!') . qq|
861
862 <p>
863 | . $locale->text('What type of item is this?') . qq|</h4>
864
865 <form method="post" action="ic.pl">
866
867 <p>
868
869   <input class="radio" type="radio" name="item" value="part" checked>&nbsp;|
870     . $locale->text('Part') . qq|<br>
871   <input class="radio" type="radio" name="item" value="service">&nbsp;|
872     . $locale->text('Service');
873 print $cgi->hidden("-name" => "previousform", "-value" => $previousform);
874 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } 
875      qw(rowcount vc login password));
876      map({ print($cgi->hidden("-name" => $_, "-value" => $form->{"$__$i"})); }
877      ("partnumber", "description"));
878 print $cgi->hidden("-name" => "taxaccount2", "-value" => $form->{taxaccounts});
879
880 print qq|
881 <input type="hidden" name="nextsub" value="add">
882
883 <p>
884 <input class="submit" type="submit" name="action" value="|
885     . $locale->text('Continue') . qq|">
886 </form>
887
888 </body>
889 </html>
890 |;
891
892   $lxdebug->leave_sub();
893 }
894
895 sub display_form {
896   $lxdebug->enter_sub();
897
898   relink_accounts();
899
900   my $new_rowcount = $form->{"rowcount"} * 1 + 1;
901   $form->{"project_id_${new_rowcount}"} = $form->{"globalproject_id"};
902
903   $form->language_payment(\%myconfig);
904
905   # if we have a display_form
906   if ($form->{display_form}) {
907     call_sub($form->{"display_form"});
908     exit;
909   }
910
911   Common::webdav_folder($form) if ($webdav);
912
913   #   if (   $form->{print_and_post}
914   #       && $form->{second_run}
915   #       && ($form->{action} eq "display_form")) {
916   #     for (keys %$form) { $old_form->{$_} = $form->{$_} }
917   #     $old_form->{rowcount}++;
918   #
919   #     #$form->{rowcount}--;
920   #     #$form->{rowcount}--;
921   #
922   #     $form->{print_and_post} = 0;
923   #
924   #     &print_form($old_form);
925   #     exit;
926   #   }
927   #
928   #   $form->{action}   = "";
929   #   $form->{resubmit} = 0;
930   #
931   #   if ($form->{print_and_post} && !$form->{second_run}) {
932   #     $form->{second_run} = 1;
933   #     $form->{action}     = "display_form";
934   #     $form->{rowcount}--;
935   #     my $rowcount = $form->{rowcount};
936   #
937   #     # get pricegroups for parts
938   #     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
939   #
940   #     # build up html code for prices_$i
941   #     set_pricegroup($rowcount);
942   #
943   #     $form->{resubmit} = 1;
944   #
945   #   }
946   &form_header;
947
948   $numrows    = ++$form->{rowcount};
949   $subroutine = "display_row";
950
951   if ($form->{item} eq 'part') {
952
953     #set preisgruppenanzahl
954     $numrows    = $form->{price_rows};
955     $subroutine = "price_row";
956
957     &{$subroutine}($numrows);
958
959     $numrows    = ++$form->{makemodel_rows};
960     $subroutine = "makemodel_row";
961   }
962   if ($form->{item} eq 'assembly') {
963     $numrows    = $form->{price_rows};
964     $subroutine = "price_row";
965
966     &{$subroutine}($numrows);
967
968     $numrows    = ++$form->{makemodel_rows};
969     $subroutine = "makemodel_row";
970
971     # create makemodel rows
972     &{$subroutine}($numrows);
973
974     $numrows    = ++$form->{assembly_rows};
975     $subroutine = "assembly_row";
976   }
977   if ($form->{item} eq 'service') {
978     $numrows    = $form->{price_rows};
979     $subroutine = "price_row";
980
981     &{$subroutine}($numrows);
982
983     $numrows = 0;
984   }
985
986   # create rows
987   &{$subroutine}($numrows) if $numrows;
988
989   &form_footer;
990
991   $lxdebug->leave_sub();
992 }
993
994 sub check_form {
995   $lxdebug->enter_sub();
996   my @a     = ();
997   my $count = 0;
998   my @flds  = (
999     qw(id partnumber description qty ship sellprice unit discount inventory_accno income_accno expense_accno listprice taxaccounts bin assembly weight projectnumber project_id oldprojectnumber runningnumber serialnumber partsgroup payment_id not_discountable shop ve gv buchungsgruppen_id language_values sellprice_pg pricegroup_old price_old price_new unit_old ordnumber transdate longdescription basefactor marge_absolut marge_percent lastcost )
1000   );
1001
1002
1003   # remove any makes or model rows
1004   if ($form->{item} eq 'part') {
1005     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1006       qw(listprice sellprice lastcost weight rop);
1007
1008     @flds = (make, model);
1009     for my $i (1 .. ($form->{makemodel_rows})) {
1010       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1011         push @a, {};
1012         my $j = $#a;
1013
1014         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1015         $count++;
1016       }
1017     }
1018
1019     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1020     $form->{makemodel_rows} = $count;
1021
1022   } elsif ($form->{item} eq 'assembly') {
1023
1024     $form->{sellprice} = 0;
1025     $form->{weight}    = 0;
1026     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1027       qw(listprice rop stock);
1028
1029     @flds =
1030       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
1031
1032     for my $i (1 .. ($form->{assembly_rows} - 1)) {
1033       if ($form->{"qty_$i"}) {
1034         push @a, {};
1035         my $j = $#a;
1036
1037         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1038
1039         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1040
1041         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1042
1043         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
1044         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
1045         $count++;
1046       }
1047     }
1048
1049     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
1050
1051     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
1052     $form->{assembly_rows} = $count;
1053
1054     $count = 0;
1055     @flds  = qw(make model);
1056     @a     = ();
1057
1058     for my $i (1 .. ($form->{makemodel_rows})) {
1059       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
1060         push @a, {};
1061         my $j = $#a;
1062
1063         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1064         $count++;
1065       }
1066     }
1067
1068     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
1069     $form->{makemodel_rows} = $count;
1070
1071   } elsif ($form->{item} eq 'service') {
1072     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
1073
1074   } else {
1075
1076     # this section applies to invoices and orders
1077     # remove any empty numbers
1078     if ($form->{rowcount}) {
1079       for my $i (1 .. $form->{rowcount} - 1) {
1080         if ($form->{"partnumber_$i"}) {
1081           push @a, {};
1082           my $j = $#a;
1083
1084           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1085           $count++;
1086           if ($lizenzen) {
1087             if ($form->{"licensenumber_$i"} == -1) {
1088               &new_license($i);
1089               exit;
1090             }
1091           }
1092         }
1093       }
1094
1095       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1096       $form->{rowcount} = $count;
1097
1098       $form->{creditremaining} -= &invoicetotal;
1099
1100     }
1101   }
1102
1103   #sk
1104   # if pricegroups
1105   if (   $form->{type} =~ (/sales_quotation/)
1106       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
1107       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
1108       or ($form->{type} =~ /sales_order/)) {
1109
1110     # get pricegroups for parts
1111     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1112
1113     # build up html code for prices_$i
1114     set_pricegroup($form->{rowcount});
1115
1116   }
1117
1118   &display_form;
1119
1120   $lxdebug->leave_sub();
1121 }
1122
1123 sub invoicetotal {
1124   $lxdebug->enter_sub();
1125
1126   $form->{oldinvtotal} = 0;
1127
1128   # add all parts and deduct paid
1129   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
1130
1131   my ($amount, $sellprice, $discount, $qty);
1132
1133   for my $i (1 .. $form->{rowcount}) {
1134     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1135     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
1136     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1137
1138     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
1139
1140     $amount = $sellprice * (1 - $discount / 100) * $qty;
1141     map { $form->{"${_}_base"} += $amount }
1142       (split (/ /, $form->{"taxaccounts_$i"}));
1143     $form->{oldinvtotal} += $amount;
1144   }
1145
1146   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1147     split(/ /, $form->{taxaccounts})
1148     if !$form->{taxincluded};
1149
1150   $form->{oldtotalpaid} = 0;
1151   for $i (1 .. $form->{paidaccounts}) {
1152     $form->{oldtotalpaid} += $form->{"paid_$i"};
1153   }
1154
1155   $lxdebug->leave_sub();
1156
1157   # return total
1158   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
1159 }
1160
1161 sub validate_items {
1162   $lxdebug->enter_sub();
1163
1164   # check if items are valid
1165   if ($form->{rowcount} == 1) {
1166     &update;
1167     exit;
1168   }
1169
1170   for $i (1 .. $form->{rowcount} - 1) {
1171     $form->isblank("partnumber_$i",
1172                    $locale->text('Number missing in Row') . " $i");
1173   }
1174
1175   $lxdebug->leave_sub();
1176 }
1177
1178 sub order {
1179   $lxdebug->enter_sub();
1180   if ($form->{second_run}) {
1181     $form->{print_and_post} = 0;
1182   }
1183   $form->{ordnumber} = $form->{invnumber};
1184
1185   $form->{old_employee_id} = $form->{employee_id};
1186   $form->{old_salesman_id} = $form->{salesman_id};
1187
1188   map { delete $form->{$_} } qw(id printed emailed queued);
1189   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
1190     $form->{title} = $locale->text('Add Purchase Order');
1191     $form->{vc}    = 'vendor';
1192     $form->{type}  = 'purchase_order';
1193     $buysell       = 'sell';
1194   }
1195   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
1196     $form->{title} = $locale->text('Add Sales Order');
1197     $form->{vc}    = 'customer';
1198     $form->{type}  = 'sales_order';
1199     $buysell       = 'buy';
1200   }
1201   $form->{script} = 'oe.pl';
1202
1203   $form->{shipto} = 1;
1204
1205   $form->{rowcount}--;
1206
1207   $form->{cp_id} *= 1;
1208
1209   require "bin/mozilla/$form->{script}";
1210   my $script = $form->{"script"};
1211   $script =~ s|.*/||;
1212   $script =~ s|.pl$||;
1213   $locale = new Locale($language, $script);
1214
1215   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1216
1217   $currency = $form->{currency};
1218
1219   &order_links;
1220
1221   $form->{currency}     = $currency;
1222   $form->{exchangerate} = "";
1223   $form->{forex}        = "";
1224   $form->{exchangerate} = $exchangerate
1225     if (
1226         $form->{forex} = (
1227                   $exchangerate =
1228                     $form->check_exchangerate(
1229                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1230                     )));
1231
1232   for $i (1 .. $form->{rowcount}) {
1233     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1234                                                      $form->{"${_}_${i}"})
1235             if ($form->{"${_}_${i}"}) }
1236         qw(ship qty sellprice listprice basefactor));
1237   }
1238
1239   &prepare_order;
1240   &display_form;
1241
1242   $lxdebug->leave_sub();
1243 }
1244
1245 sub quotation {
1246   $lxdebug->enter_sub();
1247   if ($form->{second_run}) {
1248     $form->{print_and_post} = 0;
1249   }
1250   map { delete $form->{$_} } qw(id printed emailed queued);
1251
1252   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
1253     $form->{title} = $locale->text('Add Request for Quotation');
1254     $form->{vc}    = 'vendor';
1255     $form->{type}  = 'request_quotation';
1256     $buysell       = 'sell';
1257   }
1258   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
1259     $form->{title} = $locale->text('Add Quotation');
1260     $form->{vc}    = 'customer';
1261     $form->{type}  = 'sales_quotation';
1262     $buysell       = 'buy';
1263   }
1264
1265   $form->{cp_id} *= 1;
1266
1267   $form->{script} = 'oe.pl';
1268
1269   $form->{shipto} = 1;
1270
1271   $form->{rowcount}--;
1272
1273   require "bin/mozilla/$form->{script}";
1274
1275   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1276
1277   $currency = $form->{currency};
1278
1279   &order_links;
1280
1281   $form->{currency}     = $currency;
1282   $form->{exchangerate} = "";
1283   $form->{forex}        = "";
1284   $form->{exchangerate} = $exchangerate
1285     if (
1286         $form->{forex} = (
1287                   $exchangerate =
1288                     $form->check_exchangerate(
1289                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1290                     )));
1291
1292   for $i (1 .. $form->{rowcount}) {
1293     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1294                                                      $form->{"${_}_${i}"})
1295             if ($form->{"${_}_${i}"}) }
1296         qw(ship qty sellprice listprice basefactor));
1297   }
1298
1299   &prepare_order;
1300   &display_form;
1301
1302   $lxdebug->leave_sub();
1303 }
1304
1305 sub request_for_quotation {
1306   quotation();
1307 }
1308
1309 sub edit_e_mail {
1310   $lxdebug->enter_sub();
1311   if ($form->{second_run}) {
1312     $form->{print_and_post} = 0;
1313     $form->{resubmit}       = 0;
1314   }
1315
1316   $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
1317
1318   if ($form->{"cp_id"} && !$form->{"email"}) {
1319     CT->get_contact(\%myconfig, $form);
1320     $form->{"email"} = $form->{"cp_email"};
1321   }
1322
1323   $form->{ $form->{vc} } =~ /--/;
1324   $title = $locale->text('E-mail') . " $`";
1325
1326   $form->{oldmedia} = $form->{media};
1327   $form->{media}    = "email";
1328
1329   my $attachment_filename = $form->generate_attachment_filename();
1330
1331   $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
1332   $form->header;
1333
1334   my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
1335   @dont_hide_key_list = qw(action email cc bcc subject message formname sendmode format header override);
1336   @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
1337   @hidden_keys = grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
1338
1339   print $form->parse_html_template('generic/edit_email', 
1340                                   { title           => $title,
1341                                     a_filename      => $attachment_filename,
1342                                     _print_options_ => print_options({ 'inline' => 1 }),
1343                                     HIDDEN          => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
1344                                     SHOW_BCC        => $myconfig{role} eq 'admin' });
1345
1346   $lxdebug->leave_sub();
1347 }
1348
1349 sub send_email {
1350   $lxdebug->enter_sub();
1351
1352   my $callback = $form->{script} . "?action=edit";
1353   map({ $callback .= "\&${_}=" . E($form->{$_}); }
1354       qw(login password type id));
1355
1356   print_form("return");
1357
1358   $form->{callback} = $callback;
1359   $form->redirect();
1360
1361   $lxdebug->leave_sub();
1362 }
1363
1364 # generate the printing options displayed at the bottom of oe and is forms.
1365 # this function will attempt to guess what type of form is displayed, and will generate according options
1366 #
1367 # about the coding: 
1368 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
1369 # the opthash function builds hashrefs which are then pieced together for the template arrays.
1370 # unneeded options are "undef"ed out, and then grepped out. 
1371 #
1372 # the inline options is untested, but intended to be used later in metatemplating
1373 sub print_options {
1374   $lxdebug->enter_sub();
1375
1376   my ($options) = @_;
1377
1378   $options ||= { };
1379
1380   # names 3 parameters and returns a hashref, for use in templates
1381   sub opthash { +{ value => shift, selected => shift, oname => shift } }
1382   (@FORMNAME, @FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
1383
1384   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
1385   $form->{sendmode}   = "attachment";
1386   $form->{format}     = $form->{format} || $myconfig{template_format} || "pdf";
1387   $form->{copies}     = $form->{copies} || $myconfig{copies} || 3;
1388   $form->{media}      = $form->{media} || $myconfig{default_media} || "screen";
1389   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
1390                         defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
1391
1392   $form->{PD}{ $form->{formname} } = "selected";
1393   $form->{DF}{ $form->{format} }   = "selected";
1394   $form->{OP}{ $form->{media} }    = "selected";
1395   $form->{SM}{ $form->{formname} } = "selected";
1396
1397   push @FORMNAME, grep $_,
1398     ($form->{type} eq 'purchase_order') ? (
1399       opthash("purchase_order", $form->{PD}{purchase_order}, $locale->text('Purchase Order')),
1400       opthash("bin_list", $form->{PD}{bin_list}, $locale->text('Bin List')) 
1401     ) : undef,
1402     ($form->{type} eq 'credit_note') ?
1403       opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef,
1404     ($form->{type} eq 'sales_order') ? (
1405       opthash("sales_order", $form->{PD}{sales_order}, $locale->text('Confirmation')),
1406       opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1407       opthash("pick_list", $form->{PD}{pick_list}, $locale->text('Pick List')),
1408       opthash("packing_list", $form->{PD}{packing_list}, $locale->text('Packing List')) 
1409     ) : undef,
1410     ($form->{type} =~ /_quotation$/) ?
1411       opthash("$`_quotation", $form->{PD}{"$`_quotation"}, $locale->text('Quotation')) : undef,
1412     ($form->{type} eq 'invoice') ? (
1413       opthash("invoice", $form->{PD}{invoice}, $locale->text('Invoice')),
1414       opthash("proforma", $form->{PD}{proforma}, $locale->text('Proforma Invoice')),
1415       opthash("packing_list", $form->{PD}{packing_list}, $locale->text('Packing List'))
1416     ) : undef,
1417     ($form->{type} eq 'invoice' && $form->{storno}) ? (
1418       opthash("storno_invoice", $form->{PD}{storno_invoice}, $locale->text('Storno Invoice')),
1419       opthash("storno_packing_list", $form->{PD}{storno_packing_list}, $locale->text('Storno Packing List')) 
1420     ) : undef,
1421     ($form->{type} eq 'credit_note') ?
1422       opthash("credit_note", $form->{PD}{credit_note}, $locale->text('Credit Note')) : undef;
1423
1424   push @SENDMODE, 
1425     opthash("attachment", $form->{SM}{attachment}, $locale->text('Attachment')),
1426     opthash("inline", $form->{SM}{inline}, $locale->text('In-line'))
1427       if ($form->{media} eq 'email');
1428
1429   push @MEDIA, grep $_,
1430       opthash("screen", $form->{OP}{screen}, $locale->text('Screen')),
1431     (scalar @{ $form->{printers} } && $latex_templates) ?
1432       opthash("printer", $form->{OP}{printer}, $locale->text('Printer')) : undef,
1433     ($latex_templates && !$options->{no_queue}) ?
1434       opthash("queue", $form->{OP}{queue}, $locale->text('Queue')) : undef
1435         if ($form->{media} ne 'email');
1436
1437   push @FORMAT, grep $_,
1438     ($opendocument_templates && $openofficeorg_writer_bin && $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)
1439      && !$options->{no_opendocument_pdf}) ?
1440       opthash("opendocument_pdf", $form->{DF}{"opendocument_pdf"}, $locale->text("PDF (OpenDocument/OASIS)")) : undef,
1441     ($latex_templates) ?
1442       opthash("pdf", $form->{DF}{pdf}, $locale->text('PDF')) : undef,
1443     ($latex_templates && !$options->{no_postscript}) ?
1444       opthash("postscript", $form->{DF}{postscript}, $locale->text('Postscript')) : undef,
1445     (!$options->{no_html}) ?
1446       opthash("html", $form->{DF}{html}, "HTML") : undef,
1447     ($opendocument_templates && !$options->{no_opendocument}) ?
1448       opthash("opendocument", $form->{DF}{opendocument}, $locale->text("OpenDocument/OASIS")) : undef;
1449
1450   push @LANGUAGE_ID, 
1451     map { opthash($_->{id}, ($_->{id} eq $form->{language} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
1452       if (ref $form->{languages} eq 'ARRAY');
1453
1454   push @PRINTER_ID, 
1455     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
1456       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
1457
1458   @SELECTS = map { sname => lc $_, DATA => \@$_, show => scalar @$_ }, qw(FORMNAME LANGUAGE_ID FORMAT SENDMODE MEDIA PRINTER_ID);
1459
1460   my %dont_display_groupitems = (
1461     'dunning' => 1,
1462     );
1463
1464   %template_vars = (
1465     display_copies       => scalar @{ $form->{printers} } && $latex_templates && $form->{media} ne 'email',
1466     display_remove_draft => (!$form->{id} && $form->{draft_id}),
1467     display_groupitems   => !$dont_display_groupitems{$form->{type}},
1468     groupitems_checked   => $form->{groupitems} ? "checked" : '',
1469     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
1470   );
1471
1472   my $print_options = $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
1473
1474   if ($options->{inline}) {
1475     $lxdebug->leave_sub() and return $print_options;
1476   } else {
1477     print $print_options; $lxdebug->leave_sub();
1478   }
1479 }
1480
1481 sub print {
1482   $lxdebug->enter_sub();
1483
1484   if ($form->{print_nextsub}) {
1485     call_sub($form->{print_nextsub});
1486     $lxdebug->leave_sub();
1487     return;
1488   }
1489
1490   # if this goes to the printer pass through
1491   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1492     $form->error($locale->text('Select postscript or PDF!'))
1493       if ($form->{format} !~ /(postscript|pdf)/);
1494
1495     $old_form = new Form;
1496     map { $old_form->{$_} = $form->{$_} } keys %$form;
1497   }
1498
1499   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
1500     if ($form->{formname} eq "proforma") {
1501       $form->{proforma} = 1;
1502     }
1503     $form->{print_and_save} = 1;
1504     my $formname = $form->{formname};
1505     &save();
1506     $form->{formname} = $formname;
1507     &edit();
1508     exit;
1509   }
1510
1511   &print_form($old_form);
1512
1513   $lxdebug->leave_sub();
1514 }
1515
1516 sub print_form {
1517   $lxdebug->enter_sub();
1518   my ($old_form) = @_;
1519
1520   $inv       = "inv";
1521   $due       = "due";
1522   $numberfld = "invnumber";
1523
1524   $display_form =
1525     ($form->{display_form}) ? $form->{display_form} : "display_form";
1526
1527   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1528   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1529
1530   if ($form->{formname} eq "invoice") {
1531     $form->{label} = $locale->text('Invoice');
1532   }
1533   if ($form->{formname} eq "packing_list") {
1534
1535     # this is from an invoice
1536     $form->{label} = $locale->text('Packing List');
1537   }
1538   if ($form->{formname} eq 'sales_order') {
1539     $inv                  = "ord";
1540     $due                  = "req";
1541     $form->{"${inv}date"} = $form->{transdate};
1542     $form->{label}        = $locale->text('Sales Order');
1543     $numberfld            = "sonumber";
1544     $order                = 1;
1545   }
1546
1547   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
1548     $inv                  = "inv";
1549     $due                  = "due";
1550     $form->{"${inv}date"} = $form->{invdate};
1551     $form->{label}        = $locale->text('Proforma Invoice');
1552     $numberfld            = "sonumber";
1553     $order                = 0;
1554   }
1555
1556   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
1557     $inv                  = "inv";
1558     $due                  = "due";
1559     $form->{"${inv}date"} = $form->{transdate};
1560     $form->{"invdate"}    = $form->{transdate};
1561     $form->{invnumber}    = $form->{ordnumber};
1562     $form->{label}        = $locale->text('Proforma Invoice');
1563     $numberfld            = "sonumber";
1564     $order                = 1;
1565   }
1566
1567   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1568
1569     # we use the same packing list as from an invoice
1570     $inv = "ord";
1571     $due = "req";
1572     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1573     $form->{label} = $locale->text('Packing List');
1574     $order = 1;
1575     # set invnumber for template packing_list 
1576     $form->{invnumber}   = $form->{ordnumber};
1577   }
1578   if ($form->{formname} eq 'pick_list') {
1579     $inv                  = "ord";
1580     $due                  = "req";
1581     $form->{"${inv}date"} =
1582       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1583     $form->{label} = $locale->text('Pick List');
1584     $order = 1 unless $form->{type} eq 'invoice';
1585   }
1586   if ($form->{formname} eq 'purchase_order') {
1587     $inv                  = "ord";
1588     $due                  = "req";
1589     $form->{"${inv}date"} = $form->{transdate};
1590     $form->{label}        = $locale->text('Purchase Order');
1591     $numberfld            = "ponumber";
1592     $order                = 1;
1593   }
1594   if ($form->{formname} eq 'bin_list') {
1595     $inv                  = "ord";
1596     $due                  = "req";
1597     $form->{"${inv}date"} = $form->{transdate};
1598     $form->{label}        = $locale->text('Bin List');
1599     $order                = 1;
1600   }
1601   if ($form->{formname} eq 'sales_quotation') {
1602     $inv                  = "quo";
1603     $due                  = "req";
1604     $form->{"${inv}date"} = $form->{transdate};
1605     $form->{label}        = $locale->text('Quotation');
1606     $numberfld            = "sqnumber";
1607     $order                = 1;
1608   }
1609
1610   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
1611     $inv                  = "quo";
1612     $due                  = "req";
1613     $form->{"${inv}date"} = $form->{transdate};
1614     $form->{"invdate"}    = $form->{transdate};
1615     $form->{label}        = $locale->text('Proforma Invoice');
1616     $numberfld            = "sqnumber";
1617     $order                = 1;
1618   }
1619
1620   if ($form->{formname} eq 'request_quotation') {
1621     $inv                  = "quo";
1622     $due                  = "req";
1623     $form->{"${inv}date"} = $form->{transdate};
1624     $form->{label}        = $locale->text('Quotation');
1625     $numberfld            = "rfqnumber";
1626     $order                = 1;
1627   }
1628
1629   $form->isblank("email", $locale->text('E-mail address missing!'))
1630     if ($form->{media} eq 'email');
1631   $form->isblank("${inv}date",
1632            $locale->text($form->{label}) 
1633            . ": "
1634            . $locale->text(' Date missing!'));
1635
1636   # $locale->text('Invoice Number missing!')
1637   # $locale->text('Invoice Date missing!')
1638   # $locale->text('Packing List Number missing!')
1639   # $locale->text('Packing List Date missing!')
1640   # $locale->text('Order Number missing!')
1641   # $locale->text('Order Date missing!')
1642   # $locale->text('Quotation Number missing!')
1643   # $locale->text('Quotation Date missing!')
1644
1645   # assign number
1646   $form->{what_done} = $form->{formname};
1647   if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1648     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1649     if ($form->{media} ne 'email') {
1650
1651       # get pricegroups for parts
1652       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1653
1654       # build up html code for prices_$i
1655       set_pricegroup($form->{rowcount});
1656
1657       $form->{rowcount}--;
1658
1659       call_sub($display_form);
1660       # saving the history
1661           if(!exists $form->{addition}) {
1662         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; 
1663             $form->{addition} = "PRINTED";
1664             $form->save_history($form->dbconnect(\%myconfig));
1665       }
1666       # /saving the history
1667       exit;
1668     }
1669   }
1670
1671   &validate_items;
1672
1673   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1674   my ($saved_email, $saved_cc, $saved_bcc) =
1675     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1676
1677   $language_saved = $form->{language_id};
1678   $payment_id_saved = $form->{payment_id};
1679   $salesman_id_saved = $form->{salesman_id};
1680   $cp_id_saved = $form->{cp_id};
1681
1682   call_sub("$form->{vc}_details");
1683
1684   $form->{language_id} = $language_saved;
1685   $form->{payment_id} = $payment_id_saved;
1686
1687   $form->{"email"} = $saved_email if ($saved_email);
1688   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1689   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1690
1691   if (!$cp_id_saved) {
1692     # No contact was selected. Delete all contact variables because
1693     # IS->customer_details() and IR->vendor_details() get the default
1694     # contact anyway.
1695     map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1696   }
1697
1698   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1699   if ($form->{"language_id"}) {
1700     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1701       AM->get_language_details(\%myconfig, $form, $form->{language_id});
1702   } else {
1703     $output_dateformat = $myconfig{"dateformat"};
1704     $output_numberformat = $myconfig{"numberformat"};
1705     $output_longdates = 1;
1706   }
1707
1708   ($form->{employee}) = split /--/, $form->{employee};
1709
1710   # create the form variables
1711   if ($order) {
1712     OE->order_details(\%myconfig, \%$form);
1713   } else {
1714     IS->invoice_details(\%myconfig, \%$form, $locale);
1715   }
1716
1717   $form->get_salesman(\%myconfig, $salesman_id_saved);
1718
1719   if ($form->{shipto_id}) {
1720     $form->get_shipto(\%myconfig);
1721   }
1722
1723   @a = qw(name street zipcode city country);
1724
1725   $shipto = 1;
1726
1727   # if there is no shipto fill it in from billto
1728   foreach $item (@a) {
1729     if ($form->{"shipto$item"}) {
1730       $shipto = 0;
1731       last;
1732     }
1733   }
1734
1735   if ($shipto) {
1736     if (   $form->{formname} eq 'purchase_order'
1737         || $form->{formname} eq 'request_quotation') {
1738       $form->{shiptoname}   = $myconfig{company};
1739       $form->{shiptostreet} = $myconfig{address};
1740     } else {
1741       map { $form->{"shipto$_"} = $form->{$_} } @a;
1742     }
1743   }
1744
1745   $form->{notes} =~ s/^\s+//g;
1746
1747   $form->{templates} = "$myconfig{templates}";
1748
1749   delete $form->{printer_command};
1750
1751   $form->{language} = $form->get_template_language(\%myconfig);
1752
1753   my $printer_code;
1754   if ($form->{media} ne 'email') {
1755     $printer_code = $form->get_printer_code(\%myconfig);
1756     if ($printer_code ne "") {
1757       $printer_code = "_" . $printer_code;
1758     }
1759   }
1760
1761   if ($form->{language} ne "") {
1762     map({ $form->{"unit"}->[$_] =
1763             AM->translate_units($form, $form->{"language"},
1764                                 $form->{"unit"}->[$_], $form->{"qty"}->[$_]); }
1765         (0..scalar(@{$form->{"unit"}}) - 1));
1766     $form->{language} = "_" . $form->{language};
1767   }
1768
1769   # Format dates.
1770   format_dates($output_dateformat, $output_longdates,
1771                qw(invdate orddate quodate pldate duedate reqdate transdate
1772                   shippingdate deliverydate validitydate paymentdate
1773                   datepaid transdate_oe deliverydate_oe
1774                   employee_startdate employee_enddate
1775                   ),
1776                grep({ /^datepaid_\d+$/ ||
1777                         /^transdate_oe_\d+$/ ||
1778                         /^deliverydate_oe_\d+$/ ||
1779                         /^reqdate_\d+$/ ||
1780                         /^deliverydate_\d+$/ ||
1781                         /^transdate_\d+$/
1782                     } keys(%{$form})));
1783
1784   reformat_numbers($output_numberformat, 2,
1785                    qw(invtotal ordtotal quototal subtotal linetotal
1786                       listprice sellprice netprice discount
1787                       tax taxbase total paid),
1788                    grep({ /^linetotal_\d+$/ ||
1789                             /^listprice_\d+$/ ||
1790                             /^sellprice_\d+$/ ||
1791                             /^netprice_\d+$/ ||
1792                             /^taxbase_\d+$/ ||
1793                             /^discount_\d+$/ ||
1794                             /^paid_\d+$/ ||
1795                             /^subtotal_\d+$/ ||
1796                             /^total_\d+$/ ||
1797                             /^tax_\d+$/
1798                         } keys(%{$form})));
1799
1800   reformat_numbers($output_numberformat, undef,
1801                    qw(qty),
1802                    grep({ /^qty_\d+$/
1803                         } keys(%{$form})));
1804
1805   $form->{IN} = "$form->{formname}$form->{language}${printer_code}.html";
1806   if ($form->{format} eq 'postscript') {
1807     $form->{postscript} = 1;
1808     $form->{IN} =~ s/html$/tex/;
1809   } elsif ($form->{"format"} =~ /pdf/) {
1810     $form->{pdf} = 1;
1811     if ($form->{"format"} =~ /opendocument/) {
1812       $form->{IN} =~ s/html$/odt/;
1813     } else {
1814       $form->{IN} =~ s/html$/tex/;
1815     }
1816   } elsif ($form->{"format"} =~ /opendocument/) {
1817     $form->{"opendocument"} = 1;
1818     $form->{"IN"} =~ s/html$/odt/;
1819   }
1820
1821   delete $form->{OUT};
1822
1823   if ($form->{media} eq 'printer') {
1824     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1825     $form->{printed} .= " $form->{formname}";
1826     $form->{printed} =~ s/^ //;
1827   }
1828   $printed = $form->{printed};
1829
1830   if ($form->{media} eq 'email') {
1831     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1832       unless $form->{subject};
1833
1834     $form->{emailed} .= " $form->{formname}";
1835     $form->{emailed} =~ s/^ //;
1836   }
1837   $emailed = $form->{emailed};
1838
1839   if ($form->{media} eq 'queue') {
1840     %queued = map { s|.*/|| } split / /, $form->{queued};
1841
1842     if ($filename = $queued{ $form->{formname} }) {
1843       $form->{queued} =~ s/$form->{formname} $filename//;
1844       unlink "$spool/$filename";
1845       $filename =~ s/\..*$//g;
1846     } else {
1847       $filename = time;
1848       $filename .= $$;
1849     }
1850
1851     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1852     $form->{OUT} = ">$spool/$filename";
1853
1854     # add type
1855     $form->{queued} .= " $form->{formname} $filename";
1856
1857     $form->{queued} =~ s/^ //;
1858   }
1859   $queued = $form->{queued};
1860
1861 # saving the history
1862   if(!exists $form->{addition}) {
1863     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1864     if($form->{media} =~ /printer/) {
1865         $form->{addition} = "PRINTED";
1866     }
1867     elsif($form->{media} =~ /email/) {
1868         $form->{addition} = "MAILED";
1869     }
1870     elsif($form->{media} =~ /queue/) {
1871         $form->{addition} = "QUEUED";
1872     }
1873     elsif($form->{media} =~ /screen/) {
1874         $form->{addition} = "SCREENED";
1875     }
1876     $form->save_history($form->dbconnect(\%myconfig));
1877   }
1878   # /saving the history
1879
1880   $form->parse_template(\%myconfig, $userspath);
1881
1882   $form->{callback} = "";
1883
1884   if ($form->{media} eq 'email') {
1885     $form->{message} = $locale->text('sent') unless $form->{message};
1886   }
1887   $message = $form->{message};
1888
1889   # if we got back here restore the previous form
1890   if ($form->{media} =~ /(printer|email|queue)/) {
1891
1892     $form->update_status(\%myconfig)
1893       if ($form->{media} eq 'queue' && $form->{id});
1894
1895     return $lxdebug->leave_sub() if ($old_form eq "return");
1896
1897     if ($old_form) {
1898
1899       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1900
1901       # restore and display form
1902       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1903
1904       $form->{queued}  = $queued;
1905       $form->{printed} = $printed;
1906       $form->{emailed} = $emailed;
1907       $form->{message} = $message;
1908
1909       $form->{rowcount}--;
1910       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1911         qw(exchangerate creditlimit creditremaining);
1912
1913       for $i (1 .. $form->{paidaccounts}) {
1914         map {
1915           $form->{"${_}_$i"} =
1916             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1917         } qw(paid exchangerate);
1918       }
1919
1920       call_sub($display_form);
1921       exit;
1922     }
1923
1924     $msg =
1925       ($form->{media} eq 'printer')
1926       ? $locale->text('sent to printer')
1927       : $locale->text('emailed to') . " $form->{email}";
1928     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1929   }
1930   if ($form->{printing}) {
1931    call_sub($display_form);
1932    exit; 
1933   }
1934
1935   $lxdebug->leave_sub();
1936 }
1937
1938 sub customer_details {
1939   $lxdebug->enter_sub();
1940   IS->customer_details(\%myconfig, \%$form, @_);
1941   $lxdebug->leave_sub();
1942 }
1943
1944 sub vendor_details {
1945   $lxdebug->enter_sub();
1946
1947   IR->vendor_details(\%myconfig, \%$form, @_);
1948
1949   $lxdebug->leave_sub();
1950 }
1951
1952 sub post_as_new {
1953   $lxdebug->enter_sub();
1954
1955   $form->{postasnew} = 1;
1956   map { delete $form->{$_} } qw(printed emailed queued);
1957
1958   &post;
1959
1960   $lxdebug->leave_sub();
1961 }
1962
1963 sub ship_to {
1964   $lxdebug->enter_sub();
1965   if ($form->{second_run}) {
1966     $form->{print_and_post} = 0;
1967   }
1968
1969   $title = $form->{title};
1970   $form->{title} = $locale->text('Ship to');
1971
1972   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1973     qw(exchangerate creditlimit creditremaining);
1974
1975   my @shipto_vars =
1976     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1977        shiptocontact shiptophone shiptofax shiptoemail
1978        shiptodepartment_1 shiptodepartment_2);
1979
1980   my @addr_vars =
1981     (qw(name department_1 department_2 street zipcode city country
1982         contact email phone fax));
1983
1984   # get details for name
1985   call_sub("$form->{vc}_details", @addr_vars);
1986
1987   $number =
1988     ($form->{vc} eq 'customer')
1989     ? $locale->text('Customer Number')
1990     : $locale->text('Vendor Number');
1991
1992   # get pricegroups for parts
1993   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1994
1995   # build up html code for prices_$i
1996   set_pricegroup($form->{rowcount});
1997
1998   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1999
2000   $form->{rowcount}--;
2001
2002   $form->header;
2003
2004   print qq|
2005 <body>
2006
2007 <form method="post" action="$form->{script}">
2008
2009 <table width="100%">
2010   <tr>
2011     <td>
2012       <table>
2013         <tr class="listheading">
2014           <th class="listheading" colspan="2" width="50%">|
2015     . $locale->text('Billing Address') . qq|</th>
2016           <th class="listheading" width="50%">|
2017     . $locale->text('Shipping Address') . qq|</th>
2018         </tr>
2019         <tr height="5"></tr>
2020         <tr>
2021           <th align="right" nowrap>$number</th>
2022           <td>$form->{"$form->{vc}number"}</td>
2023         </tr>
2024         <tr>
2025           <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
2026           <td>$form->{name}</td>
2027           <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
2028         </tr>
2029         <tr>
2030           <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
2031           <td>$form->{department_1}</td>
2032           <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
2033         </tr>
2034         <tr>
2035           <th align="right" nowrap>&nbsp;</th>
2036           <td>$form->{department_2}</td>
2037           <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
2038         </tr>
2039         <tr>
2040           <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
2041           <td>$form->{street}</td>
2042           <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
2043         </tr>
2044         <tr>
2045           <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
2046           <td>$form->{zipcode}</td>
2047           <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
2048         </tr>
2049         <tr>
2050           <th align="right" nowrap>| . $locale->text('City') . qq|</th>
2051           <td>$form->{city}</td>
2052           <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
2053         </tr>
2054         <tr>
2055           <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
2056           <td>$form->{country}</td>
2057           <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
2058         </tr>
2059         <tr>
2060           <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
2061           <td>$form->{contact}</td>
2062           <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
2063         </tr>
2064         <tr>
2065           <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
2066           <td>$form->{phone}</td>
2067           <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
2068         </tr>
2069         <tr>
2070           <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
2071           <td>$form->{fax}</td>
2072           <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
2073         </tr>
2074         <tr>
2075           <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
2076           <td>$form->{email}</td>
2077           <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
2078         </tr>
2079       </table>
2080     </td>
2081   </tr>
2082 </table>
2083 | . $cgi->hidden("-name" => "nextsub", "-value" => $nextsub);
2084 ;
2085
2086
2087
2088   # delete shipto
2089   map({ delete $form->{$_} } (@shipto_vars, qw(header)));
2090   $form->{title} = $title;
2091
2092   foreach $key (keys %$form) {
2093     $form->{$key} =~ s/\"/&quot;/g;
2094     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
2095   }
2096
2097   print qq|
2098
2099 <hr size="3" noshade>
2100
2101 <br>
2102 <input class="submit" type="submit" name="action" value="|
2103     . $locale->text('Continue') . qq|">
2104 </form>
2105
2106 </body>
2107 </html>
2108 |;
2109
2110   $lxdebug->leave_sub();
2111 }
2112
2113 sub new_license {
2114   $lxdebug->enter_sub();
2115
2116   my $row = shift;
2117
2118   # change callback
2119   $form->{old_callback} = $form->escape($form->{callback}, 1);
2120   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
2121   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
2122
2123   # delete action
2124   delete $form->{action};
2125   $customer = $form->{customer};
2126   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
2127
2128   # save all other form variables in a previousform variable
2129   $form->{row} = $row;
2130   foreach $key (keys %$form) {
2131
2132     # escape ampersands
2133     $form->{$key} =~ s/&/%26/g;
2134     $previousform .= qq|$key=$form->{$key}&|;
2135   }
2136   chop $previousform;
2137   $previousform = $form->escape($previousform, 1);
2138
2139   $form->{script} = "licenses.pl";
2140
2141   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
2142   map { $form->{$_} = $form->escape($form->{$_}, 1) }
2143     qw(partnumber description);
2144   $form->{callback} =
2145     qq|$form->{script}?login=$form->{login}&password=$form->{password}&action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&customer=$customer&partnumber=$form->{partnumber}&description=$form->{description}&previousform="$previousform"&initial=1|;
2146   $form->redirect;
2147
2148   $lxdebug->leave_sub();
2149 }
2150
2151 sub relink_accounts {
2152   $lxdebug->enter_sub();
2153
2154   $form->{"taxaccounts"} =~ s/\s*$//;
2155   $form->{"taxaccounts"} =~ s/^\s*//;
2156   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
2157     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
2158   }
2159   $form->{"taxaccounts"} = "";
2160
2161   for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
2162     if ($form->{"id_$i"}) {
2163       IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
2164     }
2165   }
2166
2167   $lxdebug->leave_sub();
2168 }
2169
2170 sub set_duedate {
2171   $lxdebug->enter_sub();
2172
2173   $form->get_duedate(\%myconfig);
2174
2175   my $q = new CGI;
2176   $result = "$form->{duedate}";
2177   print $q->header();
2178   print $result;
2179   $lxdebug->leave_sub();
2180
2181 }
2182