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