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