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