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