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