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