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