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