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