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