Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[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 # any custom scripts for this one
37 if (-f "$form->{path}/custom_io.pl") {
38   eval { require "$form->{path}/custom_io.pl"; };
39 }
40 if (-f "$form->{path}/$form->{login}_io.pl") {
41   eval { require "$form->{path}/$form->{login}_io.pl"; };
42 }
43
44 1;
45
46 # end of main
47
48 # this is for our long dates
49 # $locale->text('January')
50 # $locale->text('February')
51 # $locale->text('March')
52 # $locale->text('April')
53 # $locale->text('May ')
54 # $locale->text('June')
55 # $locale->text('July')
56 # $locale->text('August')
57 # $locale->text('September')
58 # $locale->text('October')
59 # $locale->text('November')
60 # $locale->text('December')
61
62 # this is for our short month
63 # $locale->text('Jan')
64 # $locale->text('Feb')
65 # $locale->text('Mar')
66 # $locale->text('Apr')
67 # $locale->text('May')
68 # $locale->text('Jun')
69 # $locale->text('Jul')
70 # $locale->text('Aug')
71 # $locale->text('Sep')
72 # $locale->text('Oct')
73 # $locale->text('Nov')
74 # $locale->text('Dec')
75 use Data::Dumper;
76 ########################################
77 # Eintrag fuer Version 2.2.0 geaendert #
78 # neue Optik im Rechnungsformular      #
79 ########################################
80 sub display_row {
81   $lxdebug->enter_sub();
82   my $numrows = shift;
83
84   if ($lizenzen && $form->{vc} eq "customer") {
85     if ($form->{type} =~ /sales_order/) {
86       @column_index = (runningnumber, partnumber, description, ship, qty);
87     } elsif ($form->{type} =~ /sales_quotation/) {
88       @column_index = (runningnumber, partnumber, description, qty);
89     } else {
90       @column_index = (runningnumber, partnumber, description, qty);
91     }
92   } else {
93     if (   ($form->{type} =~ /purchase_order/)
94         || ($form->{type} =~ /sales_order/)) {
95       @column_index = (runningnumber, partnumber, description, ship, qty);
96         } else {
97       @column_index = (runningnumber, partnumber, description, qty);
98     }
99   }
100 ############## ENDE Neueintrag ##################
101
102   push @column_index, qw(unit sellprice);
103
104   if ($form->{vc} eq 'customer') {
105     push @column_index, qw(discount);
106   }
107
108   push @column_index, "linetotal";
109
110   my $colspan = $#column_index + 1;
111
112   $form->{invsubtotal} = 0;
113   map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
114
115 ########################################
116   # Eintrag fuer Version 2.2.0 geaendert #
117   # neue Optik im Rechnungsformular      #
118 ########################################
119   $column_data{runningnumber} =
120       qq|<th align=left nowrap width=5 class=listheading>|
121     . $locale->text('No.')
122     . qq|</th>|;
123   $column_data{partnumber} =
124       qq|<th align=left nowrap width=12 class=listheading>|
125     . $locale->text('Number')
126     . qq|</th>|;
127   $column_data{description} =
128       qq|<th align=left nowrap width=30 class=listheading>|
129     . $locale->text('Part Description')
130     . qq|</th>|;
131   $column_data{ship} =
132       qq|<th align=left nowrap width=5 class=listheading>|
133     . $locale->text('Ship')
134     . qq|</th>|;
135   $column_data{qty} =
136       qq|<th align=left nowrap width=5 class=listheading>|
137     . $locale->text('Qty')
138     . qq|</th>|;
139   $column_data{unit} =
140       qq|<th align=left nowrap width=5 class=listheading>|
141     . $locale->text('Unit')
142     . qq|</th>|;
143   $column_data{license} =
144       qq|<th align=left nowrap width=10 class=listheading>|
145     . $locale->text('License')
146     . qq|</th>|;
147   $column_data{serialnr} =
148       qq|<th align=left nowrap width=10 class=listheading>|
149     . $locale->text('Serial No.')
150     . qq|</th>|;
151   $column_data{projectnr} =
152       qq|<th align=left nowrap width=10 class=listheading>|
153     . $locale->text('Project')
154     . qq|</th>|;
155   $column_data{sellprice} =
156       qq|<th align=left nowrap width=10 class=listheading>|
157     . $locale->text('Price')
158     . qq|</th>|;
159   $column_data{discount} =
160       qq|<th align=left class=listheading>|
161     . $locale->text('Discount')
162     . qq|</th>|;
163   $column_data{linetotal} =
164       qq|<th align=left nowrap width=10 class=listheading>|
165     . $locale->text('Extended')
166     . qq|</th>|;
167   $column_data{bin} =
168       qq|<th align=left nowrap width=10 class=listheading>|
169     . $locale->text('Bin')
170     . qq|</th>|;
171 ############## ENDE Neueintrag ##################
172
173   print qq|
174   <tr>
175     <td>
176       <table width=100%>
177         <tr class=listheading>|;
178
179   map { print "\n$column_data{$_}" } @column_index;
180
181   print qq|
182         </tr>
183 |;
184
185   $runningnumber = $locale->text('No.');
186   $deliverydate  = $locale->text('Delivery Date');
187   $serialnumber  = $locale->text('Serial No.');
188   $projectnumber = $locale->text('Project');
189   $partsgroup    = $locale->text('Group');
190
191   $delvar = 'deliverydate';
192
193   if ($form->{type} =~ /_order$/ || $form->{type} =~ /_quotation$/) {
194     $deliverydate = $locale->text('Required by');
195     $delvar       = 'reqdate';
196   }
197
198   for $i (1 .. $numrows) {
199
200     # undo formatting
201     map {
202       $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
203     } qw(qty ship discount sellprice);
204
205     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
206     $dec           = length $dec;
207     $decimalplaces = ($dec > 2) ? $dec : 2;
208
209     $discount =
210       $form->round_amount(
211                         $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
212                         $decimalplaces);
213     $linetotal =
214       $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
215     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
216
217     # convert " to &quot;
218     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
219       qw(partnumber description unit);
220
221 ########################################
222     # Eintrag fuer Version 2.2.0 geaendert #
223     # neue Optik im Rechnungsformular      #
224 ########################################
225     $column_data{runningnumber} =
226       qq|<td><input name="runningnumber_$i" size=5 value=$i></td>|;    # HuT
227 ############## ENDE Neueintrag ##################
228
229     $column_data{partnumber} =
230       qq|<td><input name="partnumber_$i" size=12 value="$form->{"partnumber_$i"}"></td>|;
231
232     if (($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) {
233       $column_data{description} =
234         qq|<td><textarea name="description_$i" rows=$rows cols=30 wrap=soft>$form->{"description_$i"}</textarea></td>|;
235     } else {
236       $column_data{description} =
237         qq|<td><input name="description_$i" size=30 value="$form->{"description_$i"}"></td>|;
238     }
239
240     $column_data{qty} =
241         qq|<td align=right><input name="qty_$i" size=5 value=|
242       . $form->format_amount(\%myconfig, $form->{"qty_$i"})
243       . qq|></td>|;
244     $column_data{ship} =
245         qq|<td align=right><input name="ship_$i" size=5 value=|
246       . $form->format_amount(\%myconfig, $form->{"ship_$i"})
247       . qq|></td>|;
248     $column_data{unit} =
249       qq|<td><input name="unit_$i" size=5 value="$form->{"unit_$i"}"></td>|;
250     $column_data{sellprice} =
251       qq|<td align=right><input name="sellprice_$i" size=9 value=|
252       . $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
253                              $decimalplaces)
254       . qq|></td>|;
255     $column_data{discount} =
256         qq|<td align=right><input name="discount_$i" size=3 value=|
257       . $form->format_amount(\%myconfig, $form->{"discount_$i"})
258       . qq|></td>|;
259     $column_data{linetotal} =
260         qq|<td align=right>|
261       . $form->format_amount(\%myconfig, $linetotal, 2)
262       . qq|</td>|;
263     $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
264
265 ########################################
266     # Eintrag fuer Version 2.2.0 geaendert #
267     # neue Optik im Rechnungsformular      #
268 ########################################
269     #     if ($lizenzen &&  $form->{type} eq "invoice" &&  $form->{vc} eq "customer") {
270     #     $column_data{license} = qq|<td><select name="licensenumber_$i">$form->{"lizenzen_$i"}></select></td>|;
271     #     }
272     #
273     #     if ($form->{type} !~ /_quotation/) {
274     #     $column_data{serialnr} = qq|<td><input name="serialnumber_$i" size=10 value="$form->{"serialnumber_$i"}"></td>|;
275     #     }
276     #
277     #     $column_data{projectnr} = qq|<td><input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}"></td>|;
278 ############## ENDE Neueintrag ##################
279
280     print qq|
281         <tr valign=top>|;
282
283     map { print "\n$column_data{$_}" } @column_index;
284
285     print qq|
286         </tr>
287
288 <input type=hidden name="orderitems_id_$i" value=$form->{"orderitems_id_$i"}>
289 <input type=hidden name="bo_$i" value=$form->{"bo_$i"}>
290
291 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
292 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
293 <input type=hidden name="bin_$i" value="$form->{"bin_$i"}">
294 <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">
295 <input type=hidden name="partnotes_$i" value="$form->{"partnotes_$i"}">
296 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
297 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
298 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
299 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
300 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
301
302 |;
303
304 ########################################
305     # Eintrag fuer Version 2.2.0 geaendert #
306     # neue Optik im Rechnungsformular      #
307 ########################################
308     # print second row
309     print qq|
310         <tr>
311           <td colspan=$colspan>
312 |;
313     if ($lizenzen && $form->{type} eq "invoice" && $form->{vc} eq "customer") {
314       my $selected = $form->{"licensenumber_$i"};
315       my $lizenzen_quoted;
316       $form->{"lizenzen_$i"} =~ s/ selected//g;
317       $form->{"lizenzen_$i"} =~
318         s/value="${selected}"\>/value="${selected}" selected\>/;
319       $lizenzen_quoted = $form->{"lizenzen_$i"};
320       $lizenzen_quoted =~ s/\"/&quot;/g;
321       print qq|
322         <b>Lizenz\#</b>&nbsp;<select name="licensenumber_$i" size=1>
323         $form->{"lizenzen_$i"}
324         </select>
325         <input type=hidden name="lizenzen_$i" value="${lizenzen_quoted}">
326 |;
327     }
328     if ($form->{type} !~ /_quotation/) {
329       print qq|
330           <b>$serialnumber</b>&nbsp;<input name="serialnumber_$i" size=15 value="$form->{"serialnumber_$i"}">|;
331     }
332
333     print qq|
334           <b>$projectnumber</b>&nbsp;<input name="projectnumber_$i" size=10 value="$form->{"projectnumber_$i"}">
335                   <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}">
336                   <input type=hidden name="project_id_$i" value="$form->{"project_id_$i"}">
337           </td>
338         </tr>
339
340 |;
341
342 ############## ENDE Neueintrag ##################
343
344     map { $form->{"${_}_base"} += $linetotal }
345       (split / /, $form->{"taxaccounts_$i"});
346
347     $form->{invsubtotal} += $linetotal;
348   }
349
350   print qq|
351       </table>
352     </td>
353   </tr>
354 |;
355
356   $lxdebug->leave_sub();
357 }
358
359 sub select_item {
360   $lxdebug->enter_sub();
361
362   @column_index = qw(ndx partnumber description onhand sellprice);
363
364   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
365   $column_data{partnumber} =
366     qq|<th class=listheading>| . $locale->text('Number') . qq|</th>|;
367   $column_data{description} =
368     qq|<th class=listheading>| . $locale->text('Part Description') . qq|</th>|;
369   $column_data{sellprice} =
370     qq|<th class=listheading>| . $locale->text('Price') . qq|</th>|;
371   $column_data{onhand} =
372     qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
373
374   # list items with radio button on a form
375   $form->header;
376
377   $title   = $locale->text('Select from one of the items below');
378   $colspan = $#column_index + 1;
379
380   print qq|
381 <body>
382
383 <form method=post action=$form->{script}>
384
385 <table width=100%>
386   <tr>
387     <th class=listtop colspan=$colspan>$title</th>
388   </tr>
389   <tr height="5"></tr>
390   <tr class=listheading>|;
391
392   map { print "\n$column_data{$_}" } @column_index;
393
394   print qq|</tr>|;
395
396   my $i = 0;
397   foreach $ref (@{ $form->{item_list} }) {
398     $checked = ($i++) ? "" : "checked";
399
400     if ($lizenzen) {
401       if ($ref->{inventory_accno} > 0) {
402         $ref->{"lizenzen"} = qq|<option></option>|;
403         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
404           $ref->{"lizenzen"} .=
405             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
406         }
407         $ref->{"lizenzen"} .= qq|<option value=-1>Neue Lizenz</option>|;
408         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
409       }
410     }
411
412     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
413
414     $ref->{sellprice} =
415       $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), 2);
416
417     $column_data{ndx} =
418       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
419     $column_data{partnumber} =
420       qq|<td><input name="new_partnumber_$i" type=hidden value="$ref->{partnumber}">$ref->{partnumber}</td>|;
421     $column_data{description} =
422       qq|<td><input name="new_description_$i" type=hidden value="$ref->{description}">$ref->{description}</td>|;
423     $column_data{sellprice} =
424       qq|<td align=right><input name="new_sellprice_$i" type=hidden value=$ref->{sellprice}>|
425       . $form->format_amount(\%myconfig, $ref->{sellprice}, 2, "&nbsp;")
426       . qq|</td>|;
427     $column_data{onhand} =
428       qq|<td align=right><input name="new_onhand_$i" type=hidden value=$ref->{onhand}>|
429       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
430       . qq|</td>|;
431
432     $j++;
433     $j %= 2;
434     print qq|
435 <tr class=listrow$j>|;
436
437     map { print "\n$column_data{$_}" } @column_index;
438
439     print qq|
440 </tr>
441
442 <input name="new_bin_$i" type=hidden value="$ref->{bin}">
443 <input name="new_listprice_$i" type=hidden value=$ref->{listprice}>
444 <input name="new_inventory_accno_$i" type=hidden value=$ref->{inventory_accno}>
445 <input name="new_income_accno_$i" type=hidden value=$ref->{income_accno}>
446 <input name="new_expense_accno_$i" type=hidden value=$ref->{expense_accno}>
447 <input name="new_unit_$i" type=hidden value="$ref->{unit}">
448 <input name="new_weight_$i" type=hidden value="$ref->{weight}">
449 <input name="new_assembly_$i" type=hidden value="$ref->{assembly}">
450 <input name="new_taxaccounts_$i" type=hidden value="$ref->{taxaccounts}">
451 <input name="new_partsgroup_$i" type=hidden value="$ref->{partsgroup}">
452
453 <input name="new_id_$i" type=hidden value=$ref->{id}>
454
455 |;
456     if ($lizenzen) {
457       print qq|
458 <input name="new_lizenzen_$i" type=hidden value="$ref->{lizenzen}">
459 |;
460     }
461
462   }
463
464   print qq|
465 <tr><td colspan=8><hr size=3 noshade></td></tr>
466 </table>
467
468 <input name=lastndx type=hidden value=$i>
469
470 |;
471
472   # delete action variable
473   map { delete $form->{$_} } qw(action item_list header);
474
475   # save all other form variables
476   foreach $key (keys %${form}) {
477     $form->{$key} =~ s/\"/&quot;/g;
478     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
479   }
480
481   print qq|
482 <input type=hidden name=nextsub value=item_selected>
483
484 <br>
485 <input class=submit type=submit name=action value="|
486     . $locale->text('Continue') . qq|">
487 </form>
488
489 </body>
490 </html>
491 |;
492
493   $lxdebug->leave_sub();
494 }
495
496 sub item_selected {
497   $lxdebug->enter_sub();
498
499   # replace the last row with the checked row
500   $i = $form->{rowcount};
501   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
502
503   # index for new item
504   $j = $form->{ndx};
505
506   # if there was a price entered, override it
507   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
508
509   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} }
510     qw(id partnumber description sellprice listprice inventory_accno income_accno expense_accno bin unit weight assembly taxaccounts partsgroup);
511
512   if ($lizenzen) {
513     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
514   }
515
516   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
517   $dec           = length $dec;
518   $decimalplaces = ($dec > 2) ? $dec : 2;
519
520   if ($sellprice) {
521     $form->{"sellprice_$i"} = $sellprice;
522   } else {
523
524     # if there is an exchange rate adjust sellprice
525     if (($form->{exchangerate} * 1) != 0) {
526       $form->{"sellprice_$i"} /= $form->{exchangerate};
527       $form->{"sellprice_$i"} =
528         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
529     }
530   }
531
532   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
533     qw(sellprice listprice weight);
534
535   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
536   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
537
538   $amount =
539     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
540     $form->{"qty_$i"};
541   map { $form->{"${_}_base"} += $amount }
542     (split / /, $form->{"taxaccounts_$i"});
543   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
544     $form->{"taxaccounts_$i"}
545     if !$form->{taxincluded};
546
547   $form->{creditremaining} -= $amount;
548
549   $form->{"runningnumber_$i"} = $i;
550
551   # delete all the new_ variables
552   for $i (1 .. $form->{lastndx}) {
553     map { delete $form->{"new_${_}_$i"} }
554       qw(partnumber description sellprice bin listprice inventory_accno income_accno expense_accno unit assembly taxaccounts id);
555   }
556
557   map { delete $form->{$_} } qw(ndx lastndx nextsub);
558
559   # format amounts
560   map {
561     $form->{"${_}_$i"} =
562       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
563   } qw(sellprice listprice) if $form->{item} ne 'assembly';
564
565   &display_form;
566
567   $lxdebug->leave_sub();
568 }
569
570 sub new_item {
571   $lxdebug->enter_sub();
572
573   # change callback
574   $form->{old_callback} = $form->escape($form->{callback}, 1);
575   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
576
577   # delete action
578   delete $form->{action};
579
580   # save all other form variables in a previousform variable
581   foreach $key (keys %$form) {
582
583     # escape ampersands
584     $form->{$key} =~ s/&/%26/g;
585     $previousform .= qq|$key=$form->{$key}&|;
586   }
587   chop $previousform;
588   $previousform = $form->escape($previousform, 1);
589
590   $i = $form->{rowcount};
591   map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description);
592
593   $form->header;
594
595   print qq|
596 <body>
597
598 <h4 class=error>| . $locale->text('Item not on file!') . qq|
599
600 <p>
601 | . $locale->text('What type of item is this?') . qq|</h4>
602
603 <form method=post action=ic.pl>
604
605 <p>
606
607   <input class=radio type=radio name=item value=part checked>&nbsp;|
608     . $locale->text('Part') . qq|<br>
609   <input class=radio type=radio name=item value=service>&nbsp;|
610     . $locale->text('Service')
611
612     . qq|
613 <input type=hidden name=previousform value="$previousform">
614 <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
615 <input type=hidden name=description value="$form->{"description_$i"}">
616 <input type=hidden name=rowcount value=$form->{rowcount}>
617 <input type=hidden name=taxaccount2 value=$form->{taxaccounts}>
618 <input type=hidden name=vc value=$form->{vc}>
619
620 <input type=hidden name=path value=$form->{path}>
621 <input type=hidden name=login value=$form->{login}>
622 <input type=hidden name=password value=$form->{password}>
623
624 <input type=hidden name=nextsub value=add>
625
626 <p>
627 <input class=submit type=submit name=action value="|
628     . $locale->text('Continue') . qq|">
629 </form>
630
631 </body>
632 </html>
633 |;
634
635   $lxdebug->leave_sub();
636 }
637
638 sub display_form {
639   $lxdebug->enter_sub();
640
641   # if we have a display_form
642   if ($form->{display_form}) {
643     &{"$form->{display_form}"};
644     exit;
645   }
646
647   &form_header;
648
649   $numrows    = ++$form->{rowcount};
650   $subroutine = "display_row";
651
652   if ($form->{item} eq 'part') {
653     $numrows    = ++$form->{makemodel_rows};
654     $subroutine = "makemodel_row";
655   }
656   if ($form->{item} eq 'assembly') {
657     $numrows    = ++$form->{makemodel_rows};
658     $subroutine = "makemodel_row";
659
660     # create makemodel rows
661     &{$subroutine}($numrows);
662
663     $numrows    = ++$form->{assembly_rows};
664     $subroutine = "assembly_row";
665   }
666   if ($form->{item} eq 'service') {
667     $numrows = 0;
668   }
669
670   # create rows
671   &{$subroutine}($numrows) if $numrows;
672
673   &form_footer;
674
675   $lxdebug->leave_sub();
676 }
677
678 sub check_form {
679   $lxdebug->enter_sub();
680
681   my @a     = ();
682   my $count = 0;
683   my @flds = (
684     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)
685   );
686
687   # remove any makes or model rows
688   if ($form->{item} eq 'part') {
689     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
690       qw(listprice sellprice lastcost weight rop);
691
692     @flds = (make, model);
693     for my $i (1 .. ($form->{makemodel_rows})) {
694       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
695         push @a, {};
696         my $j = $#a;
697
698         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
699         $count++;
700       }
701     }
702
703     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
704     $form->{makemodel_rows} = $count;
705
706   } elsif ($form->{item} eq 'assembly') {
707
708     $form->{sellprice} = 0;
709     $form->{weight}    = 0;
710     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
711       qw(listprice rop stock);
712
713     @flds =
714       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
715
716     for my $i (1 .. ($form->{assembly_rows} - 1)) {
717       if ($form->{"qty_$i"}) {
718         push @a, {};
719         my $j = $#a;
720
721         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
722
723         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
724
725         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
726         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
727         $count++;
728       }
729     }
730
731     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
732
733     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
734     $form->{assembly_rows} = $count;
735
736     $count = 0;
737     @flds  = qw(make model);
738     @a     = ();
739
740     for my $i (1 .. ($form->{makemodel_rows})) {
741       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
742         push @a, {};
743         my $j = $#a;
744
745         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
746         $count++;
747       }
748     }
749
750     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
751     $form->{makemodel_rows} = $count;
752
753   } else {
754
755     # this section applies to invoices and orders
756     # remove any empty numbers
757     if ($form->{rowcount}) {
758       for my $i (1 .. $form->{rowcount} - 1) {
759         if ($form->{"partnumber_$i"}) {
760           push @a, {};
761           my $j = $#a;
762
763           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
764           $count++;
765           if ($lizenzen) {
766             if ($form->{"licensenumber_$i"} == -1) {
767               &new_license($i);
768               exit;
769             }
770           }
771         }
772       }
773
774       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
775       $form->{rowcount} = $count;
776
777       $form->{creditremaining} -= &invoicetotal;
778
779     }
780   }
781
782   &display_form;
783
784   $lxdebug->leave_sub();
785 }
786
787 sub invoicetotal {
788   $lxdebug->enter_sub();
789
790   $form->{oldinvtotal} = 0;
791
792   # add all parts and deduct paid
793   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
794
795   my ($amount, $sellprice, $discount, $qty);
796
797   for my $i (1 .. $form->{rowcount}) {
798     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
799     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
800     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
801
802     $amount = $sellprice * (1 - $discount / 100) * $qty;
803     map { $form->{"${_}_base"} += $amount }
804       (split / /, $form->{"taxaccounts_$i"});
805     $form->{oldinvtotal} += $amount;
806   }
807
808   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
809     split / /, $form->{taxaccounts}
810     if !$form->{taxincluded};
811
812   $form->{oldtotalpaid} = 0;
813   for $i (1 .. $form->{paidaccounts}) {
814     $form->{oldtotalpaid} += $form->{"paid_$i"};
815   }
816
817   $lxdebug->leave_sub();
818
819   # return total
820   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
821 }
822
823 sub validate_items {
824   $lxdebug->enter_sub();
825
826   # check if items are valid
827   if ($form->{rowcount} == 1) {
828     &update;
829     exit;
830   }
831
832   for $i (1 .. $form->{rowcount} - 1) {
833     $form->isblank("partnumber_$i",
834                    $locale->text('Number missing in Row') . " $i");
835   }
836
837   $lxdebug->leave_sub();
838 }
839
840 sub order {
841   $lxdebug->enter_sub();
842
843   $form->{ordnumber} = $form->{invnumber};
844
845   map { delete $form->{$_} } qw(id printed emailed queued);
846
847   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
848     $form->{title} = $locale->text('Add Purchase Order');
849     $form->{vc}    = 'vendor';
850     $form->{type}  = 'purchase_order';
851     $buysell       = 'sell';
852   }
853   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
854     $form->{title} = $locale->text('Add Sales Order');
855     $form->{vc}    = 'customer';
856     $form->{type}  = 'sales_order';
857     $buysell       = 'buy';
858   }
859   $form->{script} = 'oe.pl';
860
861   $form->{shipto} = 1;
862
863   $form->{rowcount}--;
864
865   ($null, $form->{cp_id}) = split /--/, $form->{contact};
866   $form->{cp_id} *= 1;
867
868   require "$form->{path}/$form->{script}";
869
870   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
871
872   $currency = $form->{currency};
873
874   &order_links;
875
876   $form->{currency}     = $currency;
877   $form->{exchangerate} = "";
878   $form->{forex}        = "";
879   $form->{exchangerate} = $exchangerate
880     if (
881         $form->{forex} = (
882                   $exchangerate =
883                     $form->check_exchangerate(
884                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
885                     )));
886
887   &prepare_order;
888   &display_form;
889
890   $lxdebug->leave_sub();
891 }
892
893 sub quotation {
894   $lxdebug->enter_sub();
895
896   map { delete $form->{$_} } qw(id printed emailed queued);
897
898   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
899     $form->{title} = $locale->text('Add Request for Quotation');
900     $form->{vc}    = 'vendor';
901     $form->{type}  = 'request_quotation';
902     $buysell       = 'sell';
903   }
904   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
905     $form->{title} = $locale->text('Add Quotation');
906     $form->{vc}    = 'customer';
907     $form->{type}  = 'sales_quotation';
908     $buysell       = 'buy';
909   }
910
911   ($null, $form->{cp_id}) = split /--/, $form->{contact};
912   $form->{cp_id} *= 1;
913
914   $form->{script} = 'oe.pl';
915
916   $form->{shipto} = 1;
917
918   $form->{rowcount}--;
919
920   require "$form->{path}/$form->{script}";
921
922   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
923
924   $currency = $form->{currency};
925
926   &order_links;
927
928   $form->{currency}     = $currency;
929   $form->{exchangerate} = "";
930   $form->{forex}        = "";
931   $form->{exchangerate} = $exchangerate
932     if (
933         $form->{forex} = (
934                   $exchangerate =
935                     $form->check_exchangerate(
936                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
937                     )));
938
939   &prepare_order;
940   &display_form;
941
942   $lxdebug->leave_sub();
943 }
944
945 sub e_mail {
946   $lxdebug->enter_sub();
947
948   if ($myconfig{role} eq 'admin') {
949     $bcc = qq|
950           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
951           <td><input name=bcc size=30 value="$form->{bcc}"></td>
952 |;
953   }
954
955   if ($form->{formname} =~ /(pick|packing|bin)_list/) {
956     $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
957   }
958
959   $name = $form->{ $form->{vc} };
960   $name =~ s/--.*//g;
961   $title = $locale->text('E-mail') . " $name";
962
963   $form->{oldmedia} = $form->{media};
964   $form->{media}    = "email";
965
966   $form->header;
967
968   print qq|
969 <body>
970
971 <form method=post action=$form->{script}>
972
973 <table width=100%>
974   <tr class=listtop>
975     <th class=listtop>$title</th>
976   </tr>
977   <tr height="5"></tr>
978   <tr>
979     <td>
980       <table width=100%>
981         <tr>
982           <th align=right nowrap>| . $locale->text('To') . qq|</th>
983           <td><input name=email size=30 value="$form->{email}"></td>
984           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
985           <td><input name=cc size=30 value="$form->{cc}"></td>
986         </tr>
987         <tr>
988           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
989           <td><input name=subject size=30 value="$form->{subject}"></td>
990           $bcc
991         </tr>
992       </table>
993     </td>
994   </tr>
995   <tr>
996     <td>
997       <table width=100%>
998         <tr>
999           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1000         </tr>
1001         <tr>
1002           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1003         </tr>
1004       </table>
1005     </td>
1006   </tr>
1007   <tr>
1008     <td>
1009 |;
1010
1011   &print_options;
1012
1013   map { delete $form->{$_} }
1014     qw(action email cc bcc subject message formname sendmode format header override);
1015
1016   # save all other variables
1017   foreach $key (keys %$form) {
1018     $form->{$key} =~ s/\"/&quot;/g;
1019     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1020   }
1021
1022   print qq|
1023     </td>
1024   </tr>
1025   <tr>
1026     <td><hr size=3 noshade></td>
1027   </tr>
1028 </table>
1029
1030 <input type=hidden name=nextsub value=send_email>
1031
1032 <br>
1033 <input name=action class=submit type=submit value="|
1034     . $locale->text('Continue') . qq|">
1035 </form>
1036
1037 </body>
1038 </html>
1039 |;
1040
1041   $lxdebug->leave_sub();
1042 }
1043
1044 sub send_email {
1045   $lxdebug->enter_sub();
1046
1047   $old_form = new Form;
1048
1049   map { $old_form->{$_} = $form->{$_} } keys %$form;
1050   $old_form->{media} = $form->{oldmedia};
1051
1052   &print_form($old_form);
1053
1054   $lxdebug->leave_sub();
1055 }
1056
1057 sub print_options {
1058   $lxdebug->enter_sub();
1059
1060   $form->{sendmode} = "attachment";
1061   $form->{copies}   = 3 unless $form->{copies};
1062
1063   $form->{PD}{ $form->{formname} } = "selected";
1064   $form->{DF}{ $form->{format} }   = "";
1065   $form->{OP}{ $form->{media} }    = "selected";
1066   $form->{SM}{ $form->{sendmode} } = "selected";
1067
1068   if ($form->{type} eq 'purchase_order') {
1069     $type = qq|<select name=formname>
1070             <option value=purchase_order $form->{PD}{purchase_order}>|
1071       . $locale->text('Purchase Order') . qq|
1072             <option value=bin_list $form->{PD}{bin_list}>|
1073       . $locale->text('Bin List');
1074   }
1075
1076   if ($form->{type} eq 'sales_order') {
1077     $type = qq|<select name=formname>
1078             <option value=sales_order $form->{PD}{sales_order}>|
1079       . $locale->text('Confirmation') . qq|
1080             <option value=pick_list $form->{PD}{pick_list}>|
1081       . $locale->text('Pick List') . qq|
1082             <option value=packing_list $form->{PD}{packing_list}>|
1083       . $locale->text('Packing List');
1084   }
1085
1086   if ($form->{type} =~ /_quotation$/) {
1087     $type = qq|<select name=formname>
1088             <option value="$`_quotation" $form->{PD}{"$`_quotation"}>|
1089       . $locale->text('Quotation') . qq|
1090 |;
1091   }
1092
1093   if ($form->{type} eq 'invoice') {
1094     $type = qq|<select name=formname>
1095             <option value=invoice $form->{PD}{invoice}>| . $locale->text('Invoice');
1096   }
1097
1098   if ($form->{type} eq 'ship_order') {
1099     $type = qq|<select name=formname>
1100             <option value=pick_list $form->{PD}{pick_list}>|
1101       . $locale->text('Pick List') . qq|
1102             <option value=packing_list $form->{PD}{packing_list}>|
1103       . $locale->text('Packing List');
1104   }
1105
1106   if ($form->{type} eq 'receive_order') {
1107     $type = qq|<select name=formname>
1108             <option value=bin_list $form->{PD}{bin_list}>|
1109       . $locale->text('Bin List');
1110   }
1111
1112   if ($form->{media} eq 'email') {
1113     $media = qq|<select name=sendmode>
1114             <option value=attachment $form->{SM}{attachment}>|
1115       . $locale->text('Attachment') . qq|
1116             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1117   } else {
1118     $media = qq|<select name=media>
1119             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1120     if ($myconfig{printer} && $latex) {
1121       $media .= qq|
1122             <option value=printer $form->{OP}{printer}>|
1123         . $locale->text('Printer');
1124     }
1125     if ($latex) {
1126       $media .= qq|
1127             <option value=queue $form->{OP}{queue}>| . $locale->text('Queue');
1128     }
1129   }
1130
1131   $format = qq|<select name=format>
1132             <option value=html $form->{DF}{html}>html|;
1133
1134   if ($latex) {
1135     $format = qq|<select name=format>
1136             <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF') . qq|
1137             <option value=html $form->{DF}{html}>html
1138             <option value=postscript $form->{DF}{postscript}>|
1139       . $locale->text('Postscript');
1140   }
1141
1142   $language = qq|<select name=language>
1143                  <option value=""></option>|;
1144   %lang = (de => "deutsch", en => "englisch", fr => "französisch");
1145   foreach $item (keys %lang) {
1146     if ($form->{language} eq $item) {
1147       $language .= qq|<option value="$item" selected>$lang{$item}</option>|;
1148     } else {
1149       $language .= qq|<option value="$item">$lang{$item}</option>|;
1150     }
1151   }
1152
1153   print qq|
1154 <table width=100% cellspacing=0 cellpadding=0>
1155   <tr>
1156     <td>
1157       <table>
1158         <tr>
1159           <td>$type</select></td>
1160           <td>$language</select</td>
1161           <td>$format</select></td>
1162           <td>$media</select></td>
1163 |;
1164
1165   if ($myconfig{printer} && $latex && $form->{media} ne 'email') {
1166     print qq|
1167           <td>| . $locale->text('Copies') . qq|
1168           <input name=copies size=2 value=$form->{copies}></td>
1169 |;
1170   }
1171
1172   $form->{groupitems} = "checked" if $form->{groupitems};
1173
1174   print qq|
1175           <td>| . $locale->text('Group Items') . qq|</td>
1176           <td><input name=groupitems type=checkbox class=checkbox $form->{groupitems}></td>
1177         </tr>
1178       </table>
1179     </td>
1180     <td align=right>
1181       <table>
1182         <tr>
1183 |;
1184
1185   if ($form->{printed} =~ /$form->{formname}/) {
1186     print qq|
1187           <th>\|| . $locale->text('Printed') . qq|\|</th>
1188 |;
1189   }
1190
1191   if ($form->{emailed} =~ /$form->{formname}/) {
1192     print qq|
1193           <th>\|| . $locale->text('E-mailed') . qq|\|</th>
1194 |;
1195   }
1196
1197   if ($form->{queued} =~ /$form->{formname}/) {
1198     print qq|
1199           <th>\|| . $locale->text('Queued') . qq|\|</th>
1200 |;
1201   }
1202
1203   print qq|
1204         </tr>
1205       </table>
1206     </td>
1207   </tr>
1208 </table>
1209 |;
1210
1211   $lxdebug->leave_sub();
1212 }
1213
1214 sub print {
1215   $lxdebug->enter_sub();
1216
1217   # if this goes to the printer pass through
1218   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
1219     $form->error($locale->text('Select postscript or PDF!'))
1220       if ($form->{format} !~ /(postscript|pdf)/);
1221
1222     $old_form = new Form;
1223     map { $old_form->{$_} = $form->{$_} } keys %$form;
1224   }
1225
1226   &print_form($old_form);
1227
1228   $lxdebug->leave_sub();
1229 }
1230
1231 sub print_form {
1232   $lxdebug->enter_sub();
1233   my ($old_form) = @_;
1234
1235   $inv = "inv";
1236   $due = "due";
1237
1238   $numberfld = "invnumber";
1239
1240   $display_form =
1241     ($form->{display_form}) ? $form->{display_form} : "display_form";
1242
1243   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1244   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1245
1246   if ($form->{formname} eq "invoice") {
1247     $form->{label} = $locale->text('Invoice');
1248   }
1249   if ($form->{formname} eq "packing_list") {
1250
1251     # this is from an invoice
1252     $form->{label} = $locale->text('Packing List');
1253   }
1254   if ($form->{formname} eq 'sales_order') {
1255     $inv                  = "ord";
1256     $due                  = "req";
1257     $form->{"${inv}date"} = $form->{transdate};
1258     $form->{label}        = $locale->text('Sales Order');
1259     $numberfld            = "sonumber";
1260     $order                = 1;
1261   }
1262   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
1263
1264     # we use the same packing list as from an invoice
1265     $inv = "ord";
1266     $due = "req";
1267     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
1268     $form->{label} = $locale->text('Packing List');
1269     $order = 1;
1270   }
1271   if ($form->{formname} eq 'pick_list') {
1272     $inv                  = "ord";
1273     $due                  = "req";
1274     $form->{"${inv}date"} =
1275       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
1276     $form->{label} = $locale->text('Pick List');
1277     $order = 1 unless $form->{type} eq 'invoice';
1278   }
1279   if ($form->{formname} eq 'purchase_order') {
1280     $inv                  = "ord";
1281     $due                  = "req";
1282     $form->{"${inv}date"} = $form->{transdate};
1283     $form->{label}        = $locale->text('Purchase Order');
1284     $numberfld            = "ponumber";
1285     $order                = 1;
1286   }
1287   if ($form->{formname} eq 'bin_list') {
1288     $inv                  = "ord";
1289     $due                  = "req";
1290     $form->{"${inv}date"} = $form->{transdate};
1291     $form->{label}        = $locale->text('Bin List');
1292     $order                = 1;
1293   }
1294   if ($form->{formname} eq 'sales_quotation') {
1295     $inv                  = "quo";
1296     $due                  = "req";
1297     $form->{"${inv}date"} = $form->{transdate};
1298     $form->{label}        = $locale->text('Quotation');
1299     $numberfld            = "sqnumber";
1300     $order                = 1;
1301   }
1302   if ($form->{formname} eq 'request_quotation') {
1303     $inv                  = "quo";
1304     $due                  = "req";
1305     $form->{"${inv}date"} = $form->{transdate};
1306     $form->{label}        = $locale->text('Quotation');
1307     $numberfld            = "rfqnumber";
1308     $order                = 1;
1309   }
1310
1311   $form->isblank("email", $locale->text('E-mail address missing!'))
1312     if ($form->{media} eq 'email');
1313   $form->isblank("${inv}date",
1314                  $locale->text($form->{label} . ' Date missing!'));
1315
1316   # $locale->text('Invoice Number missing!')
1317   # $locale->text('Invoice Date missing!')
1318   # $locale->text('Packing List Number missing!')
1319   # $locale->text('Packing List Date missing!')
1320   # $locale->text('Order Number missing!')
1321   # $locale->text('Order Date missing!')
1322   # $locale->text('Quotation Number missing!')
1323   # $locale->text('Quotation Date missing!')
1324
1325   # assign number
1326   if (!$form->{"${inv}number"}) {
1327     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1328     if ($form->{media} ne 'email') {
1329       $form->{rowcount}--;
1330       &{"$display_form"};
1331       exit;
1332     }
1333   }
1334
1335   &validate_items;
1336
1337   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1338   my ($saved_email, $saved_cc, $saved_bcc) =
1339     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1340
1341   $language = $form->{language};
1342   &{"$form->{vc}_details"};
1343   $form->{language} = $language;
1344
1345   $form->{"email"} = $saved_email if ($saved_email);
1346   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1347   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1348
1349   @a = ();
1350   foreach $i (1 .. $form->{rowcount}) {
1351     push @a,
1352       ("partnumber_$i", "description_$i",
1353        "partsgroup_$i", "serialnumber_$i",
1354        "bin_$i",        "unit_$i");
1355   }
1356   map { push @a, "${_}_description" } split / /, $form->{taxaccounts};
1357
1358   $ARAP = ($form->{vc} eq 'customer') ? "AR" : "AP";
1359   push @a, $ARAP;
1360
1361   # format payment dates
1362   for $i (1 .. $form->{paidaccounts} - 1) {
1363     $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"});
1364     push @a, "${ARAP}_paid_$i", "source_$i", "memo_$i";
1365   }
1366
1367   $form->format_string(@a);
1368
1369   ($form->{employee}) = split /--/, $form->{employee};
1370   ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
1371
1372   # create the form variables
1373   if ($order) {
1374     OE->order_details(\%myconfig, \%$form);
1375   } else {
1376     IS->invoice_details(\%myconfig, \%$form, $locale);
1377   }
1378
1379   map { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, 1) }
1380     ("${inv}date", "${due}date", "shippingdate");
1381
1382   @a = qw(name street zipcode city country);
1383
1384   $shipto = 1;
1385
1386   # if there is no shipto fill it in from billto
1387   foreach $item (@a) {
1388     if ($form->{"shipto$item"}) {
1389       $shipto = 0;
1390       last;
1391     }
1392   }
1393
1394   if ($shipto) {
1395     if (   $form->{formname} eq 'purchase_order'
1396         || $form->{formname} eq 'request_quotation') {
1397       $form->{shiptoname}   = $myconfig{company};
1398       $form->{shiptostreet} = $myconfig{address};
1399         } else {
1400       map { $form->{"shipto$_"} = $form->{$_} } @a;
1401     }
1402   }
1403
1404   $form->{notes} =~ s/^\s+//g;
1405
1406   # some of the stuff could have umlauts so we translate them
1407   push @a,
1408     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptoemail shippingpoint shipvia company address signature employee contact);
1409
1410   push @a, ("${inv}date", "${due}date", email, cc, bcc);
1411
1412   $form->format_string(@a);
1413
1414   $form->{templates} = "$myconfig{templates}";
1415   if ($form->{language} ne "") {
1416     $form->{language} = "_" . $form->{language};
1417   }
1418
1419   $form->{IN} = "$form->{formname}$form->{language}.html";
1420   if ($form->{format} eq 'postscript') {
1421     $form->{postscript} = 1;
1422     $form->{IN} =~ s/html$/tex/;
1423   }
1424   if ($form->{format} eq 'pdf') {
1425     $form->{pdf} = 1;
1426     $form->{IN} =~ s/html$/tex/;
1427   }
1428
1429   if ($form->{media} eq 'printer') {
1430     $form->{OUT} = "| $myconfig{printer}";
1431     $form->{printed} .= " $form->{formname}";
1432     $form->{printed} =~ s/^ //;
1433   }
1434   $printed = $form->{printed};
1435
1436   if ($form->{media} eq 'email') {
1437     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1438       unless $form->{subject};
1439
1440     $form->{OUT} = "$sendmail";
1441
1442     $form->{emailed} .= " $form->{formname}";
1443     $form->{emailed} =~ s/^ //;
1444   }
1445   $emailed = $form->{emailed};
1446
1447   if ($form->{media} eq 'queue') {
1448     %queued = split / /, $form->{queued};
1449
1450     if ($filename = $queued{ $form->{formname} }) {
1451       $form->{queued} =~ s/$form->{formname} $filename//;
1452       unlink "$spool/$filename";
1453       $filename =~ s/\..*$//g;
1454     } else {
1455       $filename = time;
1456       $filename .= $$;
1457     }
1458
1459     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1460     $form->{OUT} = ">$spool/$filename";
1461
1462     # add type
1463     $form->{queued} .= " $form->{formname} $filename";
1464
1465     $form->{queued} =~ s/^ //;
1466   }
1467   $queued = $form->{queued};
1468
1469   $form->parse_template(\%myconfig, $userspath);
1470
1471   $form->{callback} = "";
1472
1473   if ($form->{media} eq 'email') {
1474     $form->{message} = $locale->text('sent') unless $form->{message};
1475   }
1476   $message = $form->{message};
1477
1478   # if we got back here restore the previous form
1479   if ($form->{media} =~ /(printer|email|queue)/) {
1480
1481     $form->update_status(\%myconfig)
1482       if ($form->{media} eq 'queue' && $form->{id});
1483
1484     if ($old_form) {
1485
1486       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1487
1488       # restore and display form
1489       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1490
1491       $form->{queued}  = $queued;
1492       $form->{printed} = $printed;
1493       $form->{emailed} = $emailed;
1494       $form->{message} = $message;
1495
1496       $form->{rowcount}--;
1497       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1498         qw(exchangerate creditlimit creditremaining);
1499
1500       for $i (1 .. $form->{paidaccounts}) {
1501         map {
1502           $form->{"${_}_$i"} =
1503             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1504         } qw(paid exchangerate);
1505       }
1506
1507       &{"$display_form"};
1508       exit;
1509     }
1510
1511     $msg =
1512       ($form->{media} eq 'printer')
1513       ? $locale->text('sent to printer')
1514       : $locale->text('emailed to') . " $form->{email}";
1515     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1516   }
1517
1518   $lxdebug->leave_sub();
1519 }
1520
1521 sub customer_details {
1522   $lxdebug->enter_sub();
1523
1524   IS->customer_details(\%myconfig, \%$form);
1525   $lxdebug->leave_sub();
1526 }
1527
1528 sub vendor_details {
1529   $lxdebug->enter_sub();
1530
1531   IR->vendor_details(\%myconfig, \%$form);
1532
1533   $lxdebug->leave_sub();
1534 }
1535
1536 sub post_as_new {
1537   $lxdebug->enter_sub();
1538
1539   $form->{postasnew} = 1;
1540   map { delete $form->{$_} } qw(printed emailed queued);
1541
1542   &post;
1543
1544   $lxdebug->leave_sub();
1545 }
1546
1547 sub ship_to {
1548   $lxdebug->enter_sub();
1549
1550   $title = $form->{title};
1551   $form->{title} = $locale->text('Ship to');
1552
1553   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1554     qw(exchangerate creditlimit creditremaining);
1555
1556   # get details for name
1557   &{"$form->{vc}_details"};
1558
1559   $number =
1560     ($form->{vc} eq 'customer')
1561     ? $locale->text('Customer Number')
1562     : $locale->text('Vendor Number');
1563
1564   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1565
1566   $form->{rowcount}--;
1567
1568   $form->header;
1569
1570   print qq|
1571 <body>
1572
1573 <form method=post action=$form->{script}>
1574
1575 <table width=100%>
1576   <tr>
1577     <td>
1578       <table>
1579         <tr class=listheading>
1580           <th class=listheading colspan=2 width=50%>|
1581     . $locale->text('Billing Address')
1582     . qq|</th>
1583           <th class=listheading width=50%>|
1584     . $locale->text('Shipping Address')
1585     . qq|</th>
1586         </tr>
1587         <tr height="5"></tr>
1588         <tr>
1589           <th align=right nowrap>$number</th>
1590           <td>$form->{"$form->{vc}number"}</td>
1591         </tr>
1592         <tr>
1593           <th align=right nowrap>| . $locale->text('Company Name') . qq|</th>
1594           <td>$form->{name}</td>
1595           <td><input name=shiptoname size=35 value="$form->{shiptoname}"></td>
1596         </tr>
1597         <tr>
1598           <th align=right nowrap>| . $locale->text('Street') . qq|</th>
1599           <td>$form->{street}</td>
1600           <td><input name=shiptostreet size=35 value="$form->{shiptostreet}"></td>
1601         </tr>
1602         <tr>
1603           <th align=right nowrap>| . $locale->text('Zipcode') . qq|</th>
1604           <td>$form->{zipcode}</td>
1605           <td><input name=shiptozipcode size=35 value="$form->{shiptozipcode}"></td>
1606         </tr>
1607         <tr>
1608           <th align=right nowrap>| . $locale->text('City') . qq|</th>
1609           <td>$form->{city}</td>
1610           <td><input name=shiptocity size=35 value="$form->{shiptocity}"></td>
1611         </tr>
1612         <tr>
1613           <th align=right nowrap>| . $locale->text('Country') . qq|</th>
1614           <td>$form->{country}</td>
1615           <td><input name=shiptocountry size=35 value="$form->{shiptocountry}"></td>
1616         </tr>
1617         <tr>
1618           <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
1619           <td>$form->{contact}</td>
1620           <td><input name=shiptocontact size=35 value="$form->{shiptocontact}"></td>
1621         </tr>
1622         <tr>
1623           <th align=right nowrap>| . $locale->text('Phone') . qq|</th>
1624           <td>$form->{"$form->{vc}phone"}</td>
1625           <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
1626         </tr>
1627         <tr>
1628           <th align=right nowrap>| . $locale->text('Fax') . qq|</th>
1629           <td>$form->{"$form->{vc}fax"}</td>
1630           <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
1631         </tr>
1632         <tr>
1633           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
1634           <td>$form->{email}</td>
1635           <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
1636         </tr>
1637       </table>
1638     </td>
1639   </tr>
1640 </table>
1641
1642 <input type=hidden name=nextsub value=$nextsub>
1643 |;
1644
1645   # delete shipto
1646   map { delete $form->{$_} }
1647     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail header);
1648   $form->{title} = $title;
1649
1650   foreach $key (keys %$form) {
1651     $form->{$key} =~ s/\"/&quot;/g;
1652     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1653   }
1654
1655   print qq|
1656
1657 <hr size=3 noshade>
1658
1659 <br>
1660 <input class=submit type=submit name=action value="|
1661     . $locale->text('Continue') . qq|">
1662 </form>
1663
1664 </body>
1665 </html>
1666 |;
1667
1668   $lxdebug->leave_sub();
1669 }
1670
1671 sub new_license {
1672   $lxdebug->enter_sub();
1673
1674   my $row = shift;
1675
1676   # change callback
1677   $form->{old_callback} = $form->escape($form->{callback}, 1);
1678   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
1679   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
1680
1681   # delete action
1682   delete $form->{action};
1683   $customer = $form->{customer};
1684   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
1685
1686   # save all other form variables in a previousform variable
1687   $form->{row} = $row;
1688   foreach $key (keys %$form) {
1689
1690     # escape ampersands
1691     $form->{$key} =~ s/&/%26/g;
1692     $previousform .= qq|$key=$form->{$key}&|;
1693   }
1694   chop $previousform;
1695   $previousform = $form->escape($previousform, 1);
1696
1697   $form->{script} = "licenses.pl";
1698
1699   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
1700   map { $form->{$_} = $form->escape($form->{$_}, 1) }
1701     qw(partnumber description);
1702   $form->{callback} =
1703     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|;
1704   $form->redirect;
1705
1706   $lxdebug->leave_sub();
1707 }
1708