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