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