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