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