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