XML basiertes Menue, siehe Bug #771
[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 use CGI;
37 use CGI::Ajax;
38 use List::Util qw(max);
39
40 use SL::Common;
41 use SL::CT;
42 use SL::IC;
43
44 require "bin/mozilla/common.pl";
45
46 # any custom scripts for this one
47 if (-f "bin/mozilla/custom_io.pl") {
48   eval { require "bin/mozilla/custom_io.pl"; };
49 }
50 if (-f "bin/mozilla/$form->{login}_io.pl") {
51   eval { require "bin/mozilla/$form->{login}_io.pl"; };
52 }
53
54 1;
55
56 # end of main
57
58 # this is for our long dates
59 # $locale->text('January')
60 # $locale->text('February')
61 # $locale->text('March')
62 # $locale->text('April')
63 # $locale->text('May ')
64 # $locale->text('June')
65 # $locale->text('July')
66 # $locale->text('August')
67 # $locale->text('September')
68 # $locale->text('October')
69 # $locale->text('November')
70 # $locale->text('December')
71
72 # this is for our short month
73 # $locale->text('Jan')
74 # $locale->text('Feb')
75 # $locale->text('Mar')
76 # $locale->text('Apr')
77 # $locale->text('May')
78 # $locale->text('Jun')
79 # $locale->text('Jul')
80 # $locale->text('Aug')
81 # $locale->text('Sep')
82 # $locale->text('Oct')
83 # $locale->text('Nov')
84 # $locale->text('Dec')
85 use SL::IS;
86 use SL::PE;
87 use SL::AM;
88 use Data::Dumper;
89 ########################################
90 # Eintrag fuer Version 2.2.0 geaendert #
91 # neue Optik im Rechnungsformular      #
92 ########################################
93
94 ##################################################
95 # build html-code for pricegroups in variable $form->{prices_$j}
96
97 sub set_pricegroup {
98   $lxdebug->enter_sub();
99   my $rowcount = shift;
100   for $j (1 .. $rowcount) {
101     next unless $form->{PRICES}{$j};
102     # build drop down list for pricegroups
103     my $option_tmpl = qq|<option value="%s--%s" %s>%s</option>|;
104     $form->{"prices_$j"}  = join '', map { sprintf $option_tmpl, @$_{qw(price pricegroup_id selected pricegroup)} } 
105                                          (+{ pricegroup => $locale->text("none (pricegroup)") }, @{ $form->{PRICES}{$j} });
106
107     foreach $item (@{ $form->{PRICES}{$j} }) {
108       # set new selectedpricegroup_id and prices for "Preis"
109       $form->{"pricegroup_old_$j"} = $item->{pricegroup_id}   if $item->{selected} &&  $item->{pricegroup_id};
110       $form->{"sellprice_$j"}      = $item->{price}           if $item->{selected} &&  $item->{pricegroup_id};
111       $form->{"price_new_$j"}      = $form->{"sellprice_$j"}  if $item->{selected} || !$item->{pricegroup_id};
112     }
113   }
114   $lxdebug->leave_sub();
115 }
116
117 sub select_item {
118   $lxdebug->enter_sub();
119   @column_index = qw(ndx partnumber description onhand unit sellprice);
120
121   $column_data{ndx}        = qq|<th>&nbsp;</th>|;
122   $column_data{partnumber} =
123     qq|<th class="listheading">| . $locale->text('Number') . qq|</th>|;
124   $column_data{description} =
125     qq|<th class="listheading">| . $locale->text('Part Description') . qq|</th>|;
126   $column_data{sellprice} =
127     qq|<th class="listheading">| . $locale->text('Price') . qq|</th>|;
128   $column_data{onhand} =
129     qq|<th class="listheading">| . $locale->text('Qty') . qq|</th>|;
130   $column_data{unit} =
131     qq|<th class="listheading">| . $locale->text('Unit') . qq|</th>|;
132   # list items with radio button on a form
133   $form->header;
134
135   $title   = $locale->text('Select from one of the items below');
136   $colspan = $#column_index + 1;
137
138   print qq|
139   <body>
140
141 <form method="post" action="$form->{script}">
142
143 <table width="100%">
144   <tr>
145     <th class="listtop" colspan="$colspan">$title</th>
146   </tr>
147   <tr height="5"></tr>
148   <tr class="listheading">|;
149
150   map { print "\n$column_data{$_}" } @column_index;
151
152   print qq|</tr>|;
153
154   my $i = 0;
155   foreach $ref (@{ $form->{item_list} }) {
156     $checked = ($i++) ? "" : "checked";
157
158     if ($lizenzen) {
159       if ($ref->{inventory_accno} > 0) {
160         $ref->{"lizenzen"} = qq|<option></option>|;
161         foreach $item (@{ $form->{LIZENZEN}{ $ref->{"id"} } }) {
162           $ref->{"lizenzen"} .=
163             qq|<option value=\"$item->{"id"}\">$item->{"licensenumber"}</option>|;
164         }
165         $ref->{"lizenzen"} .= qq|<option value="-1">Neue Lizenz</option>|;
166         $ref->{"lizenzen"} =~ s/\"/&quot;/g;
167       }
168     }
169
170     map { $ref->{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
171
172     my $display_sellprice  = $ref->{sellprice} * (1 - $form->{tradediscount});
173     $display_sellprice    /= $ref->{price_factor} if ($ref->{price_factor});
174     $display_sellprice     = $form->format_amount(\%myconfig, $display_sellprice, 2);
175
176     $column_data{ndx} =
177       qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
178     $column_data{partnumber} =
179       qq|<td><input name="new_partnumber_$i" type="hidden" value="$ref->{partnumber}">$ref->{partnumber}</td>|;
180     $column_data{description} =
181       qq|<td><input name="new_description_$i" type="hidden" value="$ref->{description}">$ref->{description}</td>|;
182     $column_data{sellprice} =
183       qq|<td align="right"><input name="new_sellprice_$i" type="hidden" value="$ref->{sellprice}">|
184       . $display_sellprice
185       . qq|</td>|;
186     $column_data{onhand} =
187       qq|<td align="right"><input name="new_onhand_$i" type="hidden" value="$ref->{onhand}">|
188       . $form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;")
189       . qq|</td>|;
190     $column_data{unit} =
191       qq|<td>$ref->{unit}</td>|;
192     $j++;
193     $j %= 2;
194     print qq|
195 <tr class=listrow$j>|;
196
197     map { print "\n$column_data{$_}" } @column_index;
198
199     print("</tr>\n");
200
201     my @new_fields =
202       qw(bin listprice inventory_accno income_accno expense_accno unit weight
203          assembly taxaccounts partsgroup formel longdescription not_discountable
204          part_payment_id partnotes id lastcost price_factor_id price_factor);
205     push(@new_fields, "lizenzen") if ($lizenzen);
206
207     print join "\n", map { $cgi->hidden("-name" => "new_${_}_$i", "-value" => $ref->{$_}) } @new_fields;
208     print "\n";
209   }
210
211   print qq|
212 <tr><td colspan="8"><hr size="3" noshade></td></tr>
213 </table>
214
215 <input name="lastndx" type="hidden" value="$i">
216
217 |;
218
219   # delete action variable
220   map { delete $form->{$_} } qw(action item_list header);
221
222   # save all other form variables
223   foreach $key (keys %${form}) {
224     $form->{$key} =~ s/\"/&quot;/g;
225     print qq|<input name="$key" type="hidden" value="$form->{$key}">\n|;
226   }
227
228   print qq|
229 <input type="hidden" name="nextsub" value="item_selected">
230
231 <br>
232 <input class="submit" type="submit" name="action" value="|
233     . $locale->text('Continue') . qq|">
234 </form>
235
236 </body>
237 </html>
238 |;
239
240   $lxdebug->leave_sub();
241 }
242
243 sub item_selected {
244   $lxdebug->enter_sub();
245
246   # replace the last row with the checked row
247   $i = $form->{rowcount};
248   $i = $form->{assembly_rows} if ($form->{item} eq 'assembly');
249
250   # index for new item
251   $j = $form->{ndx};
252
253   #sk
254   #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
255   #$form->{"sellprice_$i"} = $form->{"sellprice_$i"};
256
257   # if there was a price entered, override it
258   $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
259
260   my @new_fields =
261     qw(id partnumber description sellprice listprice inventory_accno
262        income_accno expense_accno bin unit weight assembly taxaccounts
263        partsgroup formel longdescription not_discountable partnotes lastcost
264        price_factor_id price_factor);
265
266   map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } @new_fields;
267
268   $form->{"marge_price_factor_$i"} = $form->{"new_price_factor_$j"};
269
270   if ($form->{"part_payment_id_$i"} ne "") {
271     $form->{payment_id} = $form->{"part_payment_id_$i"};
272   }
273
274   if ($lizenzen) {
275     map { $form->{"${_}_$i"} = $form->{"new_${_}_$j"} } qw(lizenzen);
276   }
277
278   ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
279   $dec           = length $dec;
280   $decimalplaces = ($dec > 2) ? $dec : 2;
281
282   if ($sellprice) {
283     $form->{"sellprice_$i"} = $sellprice;
284   } else {
285
286     # if there is an exchange rate adjust sellprice
287     if (($form->{exchangerate} * 1) != 0) {
288       $form->{"sellprice_$i"} /= $form->{exchangerate};
289       $form->{"sellprice_$i"} =
290         $form->round_amount($form->{"sellprice_$i"}, $decimalplaces);
291     }
292   }
293
294   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
295     qw(sellprice listprice weight);
296
297   $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
298   $form->{weight}    += ($form->{"weight_$i"} * $form->{"qty_$i"});
299   
300   if ($form->{"not_discountable_$i"}) {
301     $form->{"discount_$i"} = 0;
302   }
303
304   $amount =
305     $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) *
306     $form->{"qty_$i"};
307   map { $form->{"${_}_base"} += $amount }
308     (split / /, $form->{"taxaccounts_$i"});
309   map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /,
310     $form->{"taxaccounts_$i"}
311     if !$form->{taxincluded};
312
313   $form->{creditremaining} -= $amount;
314
315   $form->{"runningnumber_$i"} = $i;
316
317   # delete all the new_ variables
318   for $i (1 .. $form->{lastndx}) {
319     map { delete $form->{"new_${_}_$i"} } @new_fields;
320   }
321
322   map { delete $form->{$_} } qw(ndx lastndx nextsub);
323
324   # format amounts
325   map {
326     $form->{"${_}_$i"} =
327       $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces)
328   } qw(sellprice listprice) if $form->{item} ne 'assembly';
329
330   # get pricegroups for parts
331   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
332
333   # build up html code for prices_$i
334   set_pricegroup($form->{rowcount});
335
336   &display_form;
337
338   $lxdebug->leave_sub();
339 }
340
341 sub new_item {
342   $lxdebug->enter_sub();
343
344   # change callback
345   $form->{old_callback} = $form->escape($form->{callback}, 1);
346   $form->{callback}     = $form->escape("$form->{script}?action=display_form", 1);
347
348   # save all form variables except action in a previousform variable
349   my $previousform = join '&', map { $form->{$_} =~ s/&/%26/; "$_=$form->{$_}" } grep { !/action/ } keys %$form;
350
351   map { $form->{"${_}_$form->{rowcount}"} =~ s/\"/&quot;/g } qw(partnumber description);
352
353   $form->header;
354
355   push @HIDDENS, { value => $cgi->hidden("-name" => "previousform", "-value" => $form->escape($previousform, 1)) };
356   push @HIDDENS, map +{ value => $cgi->hidden("-name" => $_, "-value" => $form->{$_}) },                       qw(rowcount vc login password);
357   push @HIDDENS, map +{ value => $cgi->hidden("-name" => $_, "-value" => $form->{"${_}_$form->{rowcount}"}) }, qw(partnumber description unit sellprice);
358   push @HIDDENS, { value => $cgi->hidden("-name" => "taxaccount2", "-value" => $form->{taxaccounts}) };
359
360   print $form->parse_html_template("generic/new_item", { HIDDENS => \@HIDDENS} );
361
362   $lxdebug->leave_sub();
363 }
364
365 sub check_form {
366   $lxdebug->enter_sub();
367   my @a     = ();
368   my $count = 0;
369   my @flds  = (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 payment_id not_discountable shop ve gv buchungsgruppen_id language_values sellprice_pg pricegroup_old price_old price_new unit_old ordnumber transdate longdescription basefactor marge_total marge_percent marge_price_factor lastcost price_factor_id));
370
371   # remove any makes or model rows
372   if ($form->{item} eq 'part') {
373     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
374       qw(listprice sellprice lastcost weight rop);
375
376     @flds = (make, model);
377     for my $i (1 .. ($form->{makemodel_rows})) {
378       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
379         push @a, {};
380         my $j = $#a;
381
382         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
383         $count++;
384       }
385     }
386
387     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
388     $form->{makemodel_rows} = $count;
389
390   } elsif ($form->{item} eq 'assembly') {
391
392     $form->{sellprice} = 0;
393     $form->{weight}    = 0;
394     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
395       qw(listprice rop stock);
396
397     @flds =
398       qw(id qty unit bom partnumber description sellprice weight runningnumber partsgroup);
399
400     for my $i (1 .. ($form->{assembly_rows} - 1)) {
401       if ($form->{"qty_$i"}) {
402         push @a, {};
403         my $j = $#a;
404
405         $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
406
407         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
408
409         #($form->{"sellprice_$i"},$form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
410
411         $form->{sellprice} += ($form->{"qty_$i"} * $form->{"sellprice_$i"});
412         $form->{weight}    += ($form->{"qty_$i"} * $form->{"weight_$i"});
413         $count++;
414       }
415     }
416
417     $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
418
419     $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
420     $form->{assembly_rows} = $count;
421
422     $count = 0;
423     @flds  = qw(make model);
424     @a     = ();
425
426     for my $i (1 .. ($form->{makemodel_rows})) {
427       if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
428         push @a, {};
429         my $j = $#a;
430
431         map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
432         $count++;
433       }
434     }
435
436     $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
437     $form->{makemodel_rows} = $count;
438
439   } elsif ($form->{item} eq 'service') {
440     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(listprice sellprice lastcost);
441
442   } else {
443
444     # this section applies to invoices and orders
445     # remove any empty numbers
446     if ($form->{rowcount}) {
447       for my $i (1 .. $form->{rowcount} - 1) {
448         if ($form->{"partnumber_$i"}) {
449           push @a, {};
450           my $j = $#a;
451
452           map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
453           $count++;
454           if ($lizenzen) {
455             if ($form->{"licensenumber_$i"} == -1) {
456               &new_license($i);
457               exit;
458             }
459           }
460         }
461       }
462
463       $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
464       $form->{rowcount} = $count;
465
466       $form->{creditremaining} -= &invoicetotal;
467
468     }
469   }
470
471   #sk
472   # if pricegroups
473   if (   $form->{type} =~ (/sales_quotation/)
474       or (($form->{level} =~ /Sales/) and ($form->{type} =~ /invoice/))
475       or (($form->{level} eq undef) and ($form->{type} =~ /invoice/))
476       or ($form->{type} =~ /sales_order/)) {
477
478     # get pricegroups for parts
479     IS->get_pricegroups_for_parts(\%myconfig, \%$form);
480
481     # build up html code for prices_$i
482     set_pricegroup($form->{rowcount});
483
484   }
485
486   &display_form;
487
488   $lxdebug->leave_sub();
489 }
490
491 sub invoicetotal {
492   $lxdebug->enter_sub();
493
494   $form->{oldinvtotal} = 0;
495
496   # add all parts and deduct paid
497   map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
498
499   my ($amount, $sellprice, $discount, $qty);
500
501   for my $i (1 .. $form->{rowcount}) {
502     $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
503     $discount  = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
504     $qty       = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
505
506     #($form->{"sellprice_$i"}, $form->{"$pricegroup_old_$i"}) = split /--/, $form->{"sellprice_$i"};
507
508     $amount = $sellprice * (1 - $discount / 100) * $qty;
509     map { $form->{"${_}_base"} += $amount }
510       (split (/ /, $form->{"taxaccounts_$i"}));
511     $form->{oldinvtotal} += $amount;
512   }
513
514   map { $form->{oldinvtotal} += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
515     split(/ /, $form->{taxaccounts})
516     if !$form->{taxincluded};
517
518   $form->{oldtotalpaid} = 0;
519   for $i (1 .. $form->{paidaccounts}) {
520     $form->{oldtotalpaid} += $form->{"paid_$i"};
521   }
522
523   $lxdebug->leave_sub();
524
525   # return total
526   return ($form->{oldinvtotal} - $form->{oldtotalpaid});
527 }
528
529 sub validate_items {
530   $lxdebug->enter_sub();
531
532   # check if items are valid
533   if ($form->{rowcount} == 1) {
534     &update;
535     exit;
536   }
537
538   for $i (1 .. $form->{rowcount} - 1) {
539     $form->isblank("partnumber_$i",
540                    $locale->text('Number missing in Row') . " $i");
541   }
542
543   $lxdebug->leave_sub();
544 }
545
546 sub order {
547   $lxdebug->enter_sub();
548   if ($form->{second_run}) {
549     $form->{print_and_post} = 0;
550   }
551   $form->{ordnumber} = $form->{invnumber};
552
553   $form->{old_employee_id} = $form->{employee_id};
554   $form->{old_salesman_id} = $form->{salesman_id};
555
556   map { delete $form->{$_} } qw(id printed emailed queued);
557   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'request_quotation') {
558     $form->{title} = $locale->text('Add Purchase Order');
559     $form->{vc}    = 'vendor';
560     $form->{type}  = 'purchase_order';
561     $buysell       = 'sell';
562   }
563   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_quotation') {
564     $form->{title} = $locale->text('Add Sales Order');
565     $form->{vc}    = 'customer';
566     $form->{type}  = 'sales_order';
567     $buysell       = 'buy';
568   }
569   $form->{script} = 'oe.pl';
570
571   $form->{shipto} = 1;
572
573   $form->{rowcount}--;
574
575   $form->{cp_id} *= 1;
576
577   require "bin/mozilla/$form->{script}";
578   my $script = $form->{"script"};
579   $script =~ s|.*/||;
580   $script =~ s|.pl$||;
581   $locale = new Locale($language, $script);
582
583   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
584
585   $currency = $form->{currency};
586
587   &order_links;
588
589   $form->{currency}     = $currency;
590   $form->{exchangerate} = "";
591   $form->{forex}        = "";
592   $form->{exchangerate} = $exchangerate
593     if (
594         $form->{forex} = (
595                   $exchangerate =
596                     $form->check_exchangerate(
597                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
598                     )));
599
600   for $i (1 .. $form->{rowcount}) {
601     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
602                                                      $form->{"${_}_${i}"})
603             if ($form->{"${_}_${i}"}) }
604         qw(ship qty sellprice listprice basefactor));
605   }
606
607   &prepare_order;
608   &display_form;
609
610   $lxdebug->leave_sub();
611 }
612
613 sub quotation {
614   $lxdebug->enter_sub();
615   if ($form->{second_run}) {
616     $form->{print_and_post} = 0;
617   }
618   map { delete $form->{$_} } qw(id printed emailed queued);
619
620   if ($form->{script} eq 'ir.pl' || $form->{type} eq 'purchase_order') {
621     $form->{title} = $locale->text('Add Request for Quotation');
622     $form->{vc}    = 'vendor';
623     $form->{type}  = 'request_quotation';
624     $buysell       = 'sell';
625   }
626   if ($form->{script} eq 'is.pl' || $form->{type} eq 'sales_order') {
627     $form->{title} = $locale->text('Add Quotation');
628     $form->{vc}    = 'customer';
629     $form->{type}  = 'sales_quotation';
630     $buysell       = 'buy';
631   }
632
633   $form->{cp_id} *= 1;
634
635   $form->{script} = 'oe.pl';
636
637   $form->{shipto} = 1;
638
639   $form->{rowcount}--;
640
641   require "bin/mozilla/$form->{script}";
642
643   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
644
645   $currency = $form->{currency};
646
647   &order_links;
648
649   $form->{currency}     = $currency;
650   $form->{exchangerate} = "";
651   $form->{forex}        = "";
652   $form->{exchangerate} = $exchangerate
653     if (
654         $form->{forex} = (
655                   $exchangerate =
656                     $form->check_exchangerate(
657                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
658                     )));
659
660   for $i (1 .. $form->{rowcount}) {
661     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
662                                                      $form->{"${_}_${i}"})
663             if ($form->{"${_}_${i}"}) }
664         qw(ship qty sellprice listprice basefactor));
665   }
666
667   &prepare_order;
668   &display_form;
669
670   $lxdebug->leave_sub();
671 }
672
673 sub request_for_quotation {
674   quotation();
675 }
676
677 sub edit_e_mail {
678   $lxdebug->enter_sub();
679   if ($form->{second_run}) {
680     $form->{print_and_post} = 0;
681     $form->{resubmit}       = 0;
682   }
683
684   $form->{email} = $form->{shiptoemail} if $form->{shiptoemail} && $form->{formname} =~ /(pick|packing|bin)_list/;
685
686   if ($form->{"cp_id"} && !$form->{"email"}) {
687     CT->get_contact(\%myconfig, $form);
688     $form->{"email"} = $form->{"cp_email"};
689   }
690
691   $title = $locale->text('E-mail') . " " . $form->get_formname_translation();
692
693   $form->{oldmedia} = $form->{media};
694   $form->{media}    = "email";
695
696   my $attachment_filename = $form->generate_attachment_filename();
697
698   $form->{"fokus"} = $form->{"email"} ? "Form.subject" : "Form.email";
699   $form->header;
700
701   my (@dont_hide_key_list, %dont_hide_key, @hidden_keys);
702   @dont_hide_key_list = qw(action email cc bcc subject message formname sendmode format header override);
703   @dont_hide_key{@dont_hide_key_list} = (1) x @dont_hide_key_list;
704   @hidden_keys = grep { !$dont_hide_key{$_} } grep { !ref $form->{$_} } keys %$form;
705
706   print $form->parse_html_template('generic/edit_email', 
707                                   { title           => $title,
708                                     a_filename      => $attachment_filename,
709                                     _print_options_ => print_options('inline' => 1),
710                                     HIDDEN          => [ map +{ name => $_, value => $form->{$_} }, @hidden_keys ],
711                                     SHOW_BCC        => $myconfig{role} eq 'admin' });
712
713   $lxdebug->leave_sub();
714 }
715
716 sub send_email {
717   $lxdebug->enter_sub();
718
719   my $callback = $form->{script} . "?action=edit";
720   map({ $callback .= "\&${_}=" . E($form->{$_}); }
721       qw(login password type id));
722
723   print_form("return");
724
725   Common->save_email_status(\%myconfig, $form);
726
727   $form->{callback} = $callback;
728   $form->redirect();
729
730   $lxdebug->leave_sub();
731 }
732
733 # generate the printing options displayed at the bottom of oe and is forms.
734 # this function will attempt to guess what type of form is displayed, and will generate according options
735 #
736 # about the coding: 
737 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
738 # the opthash function builds hashrefs which are then pieced together for the template arrays.
739 # unneeded options are "undef"ed out, and then grepped out. 
740 #
741 # the inline options is untested, but intended to be used later in metatemplating
742 sub print_options {
743   $lxdebug->enter_sub();
744
745   my %options = @_;
746
747   # names 3 parameters and returns a hashref, for use in templates
748   sub opthash { +{ value => shift, selected => shift, oname => shift } }
749   (@FORMNAME, @FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
750
751   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
752   $form->{sendmode}   = "attachment";
753   $form->{format}     = $form->{format} || $myconfig{template_format} || "pdf";
754   $form->{copies}     = $form->{copies} || $myconfig{copies}          || 3;
755   $form->{media}      = $form->{media}  || $myconfig{default_media}   || "screen";
756   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
757                         defined $myconfig{default_printer_id} ? $myconfig{default_printer_id} : "";
758
759   $form->{PD}{ $form->{formname} } = "selected";
760   $form->{DF}{ $form->{format} }   = "selected";
761   $form->{OP}{ $form->{media} }    = "selected";
762   $form->{SM}{ $form->{formname} } = "selected";
763
764   push @FORMNAME, grep $_,
765     ($form->{type} eq 'purchase_order') ? (
766       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
767       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List')) 
768     ) : undef,
769     ($form->{type} eq 'credit_note') ?
770       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
771     ($form->{type} eq 'sales_order') ? (
772       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
773       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
774       opthash("pick_list",           $form->{PD}{pick_list},           $locale->text('Pick List')),
775       opthash("packing_list",        $form->{PD}{packing_list},        $locale->text('Packing List')) 
776     ) : undef,
777     ($form->{type} =~ /_quotation$/) ?
778       opthash("$`_quotation",        $form->{PD}{"$`_quotation"},      $locale->text('Quotation')) : undef,
779     ($form->{type} eq 'invoice') ? (
780       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
781       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
782       opthash("packing_list",        $form->{PD}{packing_list},        $locale->text('Packing List'))
783     ) : undef,
784     ($form->{type} eq 'invoice' && $form->{storno}) ? (
785       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
786       opthash("storno_packing_list", $form->{PD}{storno_packing_list}, $locale->text('Storno Packing List')) 
787     ) : undef,
788     ($form->{type} eq 'credit_note') ?
789       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef;
790
791   push @SENDMODE, 
792     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
793     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
794       if ($form->{media} eq 'email');
795
796   push @MEDIA, grep $_,
797       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
798     (scalar @{ $form->{printers} } && $latex_templates) ?
799       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
800     ($latex_templates && !$options->{no_queue}) ?
801       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
802         if ($form->{media} ne 'email');
803
804   push @FORMAT, grep $_,
805     ($opendocument_templates && $openofficeorg_writer_bin && $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)
806      && !$options->{no_opendocument_pdf}) ?
807       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
808     ($latex_templates) ?
809       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
810     ($latex_templates && !$options->{no_postscript}) ?
811       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
812     (!$options->{no_html}) ?
813       opthash("html", $form->{DF}{html}, "HTML") : undef,
814     ($opendocument_templates && !$options->{no_opendocument}) ?
815       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef;
816
817   push @LANGUAGE_ID, 
818     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
819       if (ref $form->{languages} eq 'ARRAY');
820
821   push @PRINTER_ID, 
822     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
823       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
824
825   @SELECTS = map { sname => lc $_, DATA => \@$_, show => !$options{"hide_" . lc($_)} && scalar @$_ }, qw(FORMNAME LANGUAGE_ID FORMAT SENDMODE MEDIA PRINTER_ID);
826
827   my %dont_display_groupitems = (
828     'dunning' => 1,
829     );
830
831   %template_vars = (
832     display_copies       => scalar @{ $form->{printers} } && $latex_templates && $form->{media} ne 'email',
833     display_remove_draft => (!$form->{id} && $form->{draft_id}),
834     display_groupitems   => !$dont_display_groupitems{$form->{type}},
835     groupitems_checked   => $form->{groupitems} ? "checked" : '',
836     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
837   );
838
839   my $print_options = $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
840
841   if ($options{inline}) {
842     $lxdebug->leave_sub() and return $print_options;
843   } else {
844     print $print_options; $lxdebug->leave_sub();
845   }
846 }
847
848 sub print {
849   $lxdebug->enter_sub();
850
851   if ($form->{print_nextsub}) {
852     call_sub($form->{print_nextsub});
853     $lxdebug->leave_sub();
854     return;
855   }
856
857   # if this goes to the printer pass through
858   if ($form->{media} eq 'printer' || $form->{media} eq 'queue') {
859     $form->error($locale->text('Select postscript or PDF!'))
860       if ($form->{format} !~ /(postscript|pdf)/);
861
862     $old_form = new Form;
863     map { $old_form->{$_} = $form->{$_} } keys %$form;
864   }
865
866   if (!$form->{id} || (($form->{formname} eq "proforma") && !$form->{proforma} && (($form->{type} =~ /_order$/) || ($form->{type} =~ /_quotation$/)))) {
867     if ($form->{formname} eq "proforma") {
868       $form->{proforma} = 1;
869     }
870     $form->{print_and_save} = 1;
871     my $formname = $form->{formname};
872     &save();
873     $form->{formname} = $formname;
874     &edit();
875     exit;
876   }
877
878   &print_form($old_form);
879
880   $lxdebug->leave_sub();
881 }
882
883 sub print_form {
884   $lxdebug->enter_sub();
885   my ($old_form) = @_;
886
887   $inv       = "inv";
888   $due       = "due";
889   $numberfld = "invnumber";
890
891   $display_form =
892     ($form->{display_form}) ? $form->{display_form} : "display_form";
893
894   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
895   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
896
897   if ($form->{formname} eq "invoice") {
898     $form->{label} = $locale->text('Invoice');
899   }
900   if ($form->{formname} eq "packing_list") {
901
902     # this is from an invoice
903     $form->{label} = $locale->text('Packing List');
904   }
905   if ($form->{formname} eq 'sales_order') {
906     $inv                  = "ord";
907     $due                  = "req";
908     $form->{"${inv}date"} = $form->{transdate};
909     $form->{label}        = $locale->text('Confirmation');
910     $numberfld            = "sonumber";
911     $order                = 1;
912   }
913
914   if (($form->{type} eq 'invoice') && ($form->{formname} eq 'proforma') ) {
915     $inv                  = "inv";
916     $due                  = "due";
917     $form->{"${inv}date"} = $form->{invdate};
918     $form->{label}        = $locale->text('Proforma Invoice');
919     $numberfld            = "sonumber";
920     $order                = 0;
921   }
922
923   if (($form->{type} eq 'sales_order') && ($form->{formname} eq 'proforma') ) {
924     $inv                  = "inv";
925     $due                  = "due";
926     $form->{"${inv}date"} = $form->{transdate};
927     $form->{"invdate"}    = $form->{transdate};
928     $form->{invnumber}    = $form->{ordnumber};
929     $form->{label}        = $locale->text('Proforma Invoice');
930     $numberfld            = "sonumber";
931     $order                = 1;
932   }
933
934   if ($form->{formname} eq 'packing_list' && $form->{type} ne 'invoice') {
935
936     # we use the same packing list as from an invoice
937     $inv = "ord";
938     $due = "req";
939     $form->{invdate} = $form->{"${inv}date"} = $form->{transdate};
940     $form->{label} = $locale->text('Packing List');
941     $order = 1;
942     # set invnumber for template packing_list 
943     $form->{invnumber}   = $form->{ordnumber};
944   }
945   if ($form->{formname} eq 'pick_list') {
946     $inv                  = "ord";
947     $due                  = "req";
948     $form->{"${inv}date"} =
949       ($form->{transdate}) ? $form->{transdate} : $form->{invdate};
950     $form->{label} = $locale->text('Pick List');
951     $order = 1 unless $form->{type} eq 'invoice';
952   }
953   if ($form->{formname} eq 'purchase_order') {
954     $inv                  = "ord";
955     $due                  = "req";
956     $form->{"${inv}date"} = $form->{transdate};
957     $form->{label}        = $locale->text('Purchase Order');
958     $numberfld            = "ponumber";
959     $order                = 1;
960   }
961   if ($form->{formname} eq 'bin_list') {
962     $inv                  = "ord";
963     $due                  = "req";
964     $form->{"${inv}date"} = $form->{transdate};
965     $form->{label}        = $locale->text('Bin List');
966     $order                = 1;
967   }
968   if ($form->{formname} eq 'sales_quotation') {
969     $inv                  = "quo";
970     $due                  = "req";
971     $form->{"${inv}date"} = $form->{transdate};
972     $form->{label}        = $locale->text('Quotation');
973     $numberfld            = "sqnumber";
974     $order                = 1;
975   }
976
977   if (($form->{type} eq 'sales_quotation') && ($form->{formname} eq 'proforma') ) {
978     $inv                  = "quo";
979     $due                  = "req";
980     $form->{"${inv}date"} = $form->{transdate};
981     $form->{"invdate"}    = $form->{transdate};
982     $form->{label}        = $locale->text('Proforma Invoice');
983     $numberfld            = "sqnumber";
984     $order                = 1;
985   }
986
987   if ($form->{formname} eq 'request_quotation') {
988     $inv                  = "quo";
989     $due                  = "req";
990     $form->{"${inv}date"} = $form->{transdate};
991     $form->{label}        = $locale->text('RFQ');
992     $numberfld            = "rfqnumber";
993     $order                = 1;
994   }
995
996   $form->isblank("email", $locale->text('E-mail address missing!'))
997     if ($form->{media} eq 'email');
998   $form->isblank("${inv}date",
999            $locale->text($form->{label}) 
1000            . ": "
1001            . $locale->text(' Date missing!'));
1002
1003   # $locale->text('Invoice Number missing!')
1004   # $locale->text('Invoice Date missing!')
1005   # $locale->text('Packing List Number missing!')
1006   # $locale->text('Packing List Date missing!')
1007   # $locale->text('Order Number missing!')
1008   # $locale->text('Order Date missing!')
1009   # $locale->text('Quotation Number missing!')
1010   # $locale->text('Quotation Date missing!')
1011
1012   # assign number
1013   $form->{what_done} = $form->{formname};
1014   if (!$form->{"${inv}number"} && !$form->{preview} && !$form->{id}) {
1015     $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
1016     if ($form->{media} ne 'email') {
1017
1018       # get pricegroups for parts
1019       IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1020
1021       # build up html code for prices_$i
1022       set_pricegroup($form->{rowcount});
1023
1024       $form->{rowcount}--;
1025
1026       call_sub($display_form);
1027       # saving the history
1028           if(!exists $form->{addition}) {
1029         $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; 
1030             $form->{addition} = "PRINTED";
1031             $form->save_history($form->dbconnect(\%myconfig));
1032       }
1033       # /saving the history
1034       exit;
1035     }
1036   }
1037
1038   &validate_items;
1039
1040   # Save the email address given in the form because it should override the setting saved for the customer/vendor.
1041   my ($saved_email, $saved_cc, $saved_bcc) =
1042     ($form->{"email"}, $form->{"cc"}, $form->{"bcc"});
1043
1044   $language_saved = $form->{language_id};
1045   $payment_id_saved = $form->{payment_id};
1046   $salesman_id_saved = $form->{salesman_id};
1047   $cp_id_saved = $form->{cp_id};
1048
1049   call_sub("$form->{vc}_details");
1050
1051   $form->{language_id} = $language_saved;
1052   $form->{payment_id} = $payment_id_saved;
1053
1054   $form->{"email"} = $saved_email if ($saved_email);
1055   $form->{"cc"}    = $saved_cc    if ($saved_cc);
1056   $form->{"bcc"}   = $saved_bcc   if ($saved_bcc);
1057
1058   if (!$cp_id_saved) {
1059     # No contact was selected. Delete all contact variables because
1060     # IS->customer_details() and IR->vendor_details() get the default
1061     # contact anyway.
1062     map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1063   }
1064
1065   my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
1066   if ($form->{"language_id"}) {
1067     ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) =
1068       AM->get_language_details(\%myconfig, $form, $form->{language_id});
1069   } else {
1070     $output_dateformat = $myconfig{"dateformat"};
1071     $output_numberformat = $myconfig{"numberformat"};
1072     $output_longdates = 1;
1073   }
1074
1075   ($form->{employee}) = split /--/, $form->{employee};
1076
1077   # create the form variables
1078   if ($order) {
1079     OE->order_details(\%myconfig, \%$form);
1080   } else {
1081     IS->invoice_details(\%myconfig, \%$form, $locale);
1082   }
1083
1084   $form->get_salesman(\%myconfig, $salesman_id_saved);
1085
1086   if ($form->{shipto_id}) {
1087     $form->get_shipto(\%myconfig);
1088   }
1089
1090   @a = qw(name street zipcode city country);
1091
1092   $shipto = 1;
1093
1094   # if there is no shipto fill it in from billto
1095   foreach $item (@a) {
1096     if ($form->{"shipto$item"}) {
1097       $shipto = 0;
1098       last;
1099     }
1100   }
1101
1102   if ($shipto) {
1103     if (   $form->{formname} eq 'purchase_order'
1104         || $form->{formname} eq 'request_quotation') {
1105       $form->{shiptoname}   = $myconfig{company};
1106       $form->{shiptostreet} = $myconfig{address};
1107     } else {
1108       map { $form->{"shipto$_"} = $form->{$_} } @a;
1109     }
1110   }
1111
1112   $form->{notes} =~ s/^\s+//g;
1113
1114   $form->{templates} = "$myconfig{templates}";
1115
1116   delete $form->{printer_command};
1117
1118   $form->{language} = $form->get_template_language(\%myconfig);
1119
1120   my $printer_code;
1121   if ($form->{media} ne 'email') {
1122     $printer_code = $form->get_printer_code(\%myconfig);
1123     if ($printer_code ne "") {
1124       $printer_code = "_" . $printer_code;
1125     }
1126   }
1127
1128   if ($form->{language} ne "") {
1129     map({ $form->{"unit"}->[$_] =
1130             AM->translate_units($form, $form->{"language"},
1131                                 $form->{"unit"}->[$_], $form->{"qty"}->[$_]); }
1132         (0..scalar(@{$form->{"unit"}}) - 1));
1133     $form->{language} = "_" . $form->{language};
1134   }
1135
1136   # Format dates.
1137   format_dates($output_dateformat, $output_longdates,
1138                qw(invdate orddate quodate pldate duedate reqdate transdate
1139                   shippingdate deliverydate validitydate paymentdate
1140                   datepaid transdate_oe deliverydate_oe
1141                   employee_startdate employee_enddate
1142                   ),
1143                grep({ /^datepaid_\d+$/ ||
1144                         /^transdate_oe_\d+$/ ||
1145                         /^deliverydate_oe_\d+$/ ||
1146                         /^reqdate_\d+$/ ||
1147                         /^deliverydate_\d+$/ ||
1148                         /^transdate_\d+$/
1149                     } keys(%{$form})));
1150
1151   reformat_numbers($output_numberformat, 2,
1152                    qw(invtotal ordtotal quototal subtotal linetotal
1153                       listprice sellprice netprice discount
1154                       tax taxbase total paid),
1155                    grep({ /^linetotal_\d+$/ ||
1156                             /^listprice_\d+$/ ||
1157                             /^sellprice_\d+$/ ||
1158                             /^netprice_\d+$/ ||
1159                             /^taxbase_\d+$/ ||
1160                             /^discount_\d+$/ ||
1161                             /^paid_\d+$/ ||
1162                             /^subtotal_\d+$/ ||
1163                             /^total_\d+$/ ||
1164                             /^tax_\d+$/
1165                         } keys(%{$form})));
1166
1167   reformat_numbers($output_numberformat, undef,
1168                    qw(qty price_factor),
1169                    grep({ /^qty_\d+$/
1170                         } keys(%{$form})));
1171
1172   $form->{IN} = "$form->{formname}$form->{language}${printer_code}.html";
1173   if ($form->{format} eq 'postscript') {
1174     $form->{postscript} = 1;
1175     $form->{IN} =~ s/html$/tex/;
1176   } elsif ($form->{"format"} =~ /pdf/) {
1177     $form->{pdf} = 1;
1178     if ($form->{"format"} =~ /opendocument/) {
1179       $form->{IN} =~ s/html$/odt/;
1180     } else {
1181       $form->{IN} =~ s/html$/tex/;
1182     }
1183   } elsif ($form->{"format"} =~ /opendocument/) {
1184     $form->{"opendocument"} = 1;
1185     $form->{"IN"} =~ s/html$/odt/;
1186   }
1187
1188   delete $form->{OUT};
1189
1190   if ($form->{media} eq 'printer') {
1191     $form->{OUT} = "| $form->{printer_command} &>/dev/null";
1192     $form->{printed} .= " $form->{formname}";
1193     $form->{printed} =~ s/^ //;
1194   }
1195   $printed = $form->{printed};
1196
1197   if ($form->{media} eq 'email') {
1198     $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
1199       unless $form->{subject};
1200
1201     $form->{emailed} .= " $form->{formname}";
1202     $form->{emailed} =~ s/^ //;
1203   }
1204   $emailed = $form->{emailed};
1205
1206   if ($form->{media} eq 'queue') {
1207     %queued = map { s|.*/|| } split / /, $form->{queued};
1208
1209     if ($filename = $queued{ $form->{formname} }) {
1210       $form->{queued} =~ s/\Q$form->{formname} $filename\E//;
1211       unlink "$spool/$filename";
1212       $filename =~ s/\..*$//g;
1213     } else {
1214       $filename = time;
1215       $filename .= $$;
1216     }
1217
1218     $filename .= ($form->{postscript}) ? '.ps' : '.pdf';
1219     $form->{OUT} = ">$spool/$filename";
1220
1221     # add type
1222     $form->{queued} .= " $form->{formname} $filename";
1223
1224     $form->{queued} =~ s/^ //;
1225   }
1226   $queued = $form->{queued};
1227
1228 # saving the history
1229   if(!exists $form->{addition}) {
1230     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1231     if($form->{media} =~ /printer/) {
1232         $form->{addition} = "PRINTED";
1233     }
1234     elsif($form->{media} =~ /email/) {
1235         $form->{addition} = "MAILED";
1236     }
1237     elsif($form->{media} =~ /queue/) {
1238         $form->{addition} = "QUEUED";
1239     }
1240     elsif($form->{media} =~ /screen/) {
1241         $form->{addition} = "SCREENED";
1242     }
1243     $form->save_history($form->dbconnect(\%myconfig));
1244   }
1245   # /saving the history
1246
1247   $form->parse_template(\%myconfig, $userspath);
1248
1249   $form->{callback} = "";
1250
1251   if ($form->{media} eq 'email') {
1252     $form->{message} = $locale->text('sent') unless $form->{message};
1253   }
1254   $message = $form->{message};
1255
1256   # if we got back here restore the previous form
1257   if ($form->{media} =~ /(printer|email|queue)/) {
1258
1259     $form->update_status(\%myconfig)
1260       if ($form->{media} eq 'queue' && $form->{id});
1261
1262     return $lxdebug->leave_sub() if ($old_form eq "return");
1263
1264     if ($old_form) {
1265
1266       $old_form->{"${inv}number"} = $form->{"${inv}number"};
1267
1268       # restore and display form
1269       map { $form->{$_} = $old_form->{$_} } keys %$old_form;
1270
1271       $form->{queued}  = $queued;
1272       $form->{printed} = $printed;
1273       $form->{emailed} = $emailed;
1274       $form->{message} = $message;
1275
1276       $form->{rowcount}--;
1277       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1278         qw(exchangerate creditlimit creditremaining);
1279
1280       for $i (1 .. $form->{paidaccounts}) {
1281         map {
1282           $form->{"${_}_$i"} =
1283             $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
1284         } qw(paid exchangerate);
1285       }
1286
1287       call_sub($display_form);
1288       exit;
1289     }
1290
1291     $msg =
1292       ($form->{media} eq 'printer')
1293       ? $locale->text('sent to printer')
1294       : $locale->text('emailed to') . " $form->{email}";
1295     $form->redirect(qq|$form->{label} $form->{"${inv}number"} $msg|);
1296   }
1297   if ($form->{printing}) {
1298    call_sub($display_form);
1299    exit; 
1300   }
1301
1302   $lxdebug->leave_sub();
1303 }
1304
1305 sub customer_details {
1306   $lxdebug->enter_sub();
1307   IS->customer_details(\%myconfig, \%$form, @_);
1308   $lxdebug->leave_sub();
1309 }
1310
1311 sub vendor_details {
1312   $lxdebug->enter_sub();
1313
1314   IR->vendor_details(\%myconfig, \%$form, @_);
1315
1316   $lxdebug->leave_sub();
1317 }
1318
1319 sub post_as_new {
1320   $lxdebug->enter_sub();
1321
1322   $form->{postasnew} = 1;
1323   map { delete $form->{$_} } qw(printed emailed queued);
1324
1325   &post;
1326
1327   $lxdebug->leave_sub();
1328 }
1329
1330 sub ship_to {
1331   $lxdebug->enter_sub();
1332   if ($form->{second_run}) {
1333     $form->{print_and_post} = 0;
1334   }
1335
1336   $title = $form->{title};
1337   $form->{title} = $locale->text('Ship to');
1338
1339   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1340     qw(exchangerate creditlimit creditremaining);
1341
1342   my @shipto_vars =
1343     qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry
1344        shiptocontact shiptophone shiptofax shiptoemail
1345        shiptodepartment_1 shiptodepartment_2);
1346
1347   my @addr_vars =
1348     (qw(name department_1 department_2 street zipcode city country
1349         contact email phone fax));
1350
1351   # get details for name
1352   call_sub("$form->{vc}_details", @addr_vars);
1353
1354   $number =
1355     ($form->{vc} eq 'customer')
1356     ? $locale->text('Customer Number')
1357     : $locale->text('Vendor Number');
1358
1359   # get pricegroups for parts
1360   IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1361
1362   # build up html code for prices_$i
1363   set_pricegroup($form->{rowcount});
1364
1365   $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
1366
1367   $form->{rowcount}--;
1368
1369   $form->header;
1370
1371   print qq|
1372 <body>
1373
1374 <form method="post" action="$form->{script}">
1375
1376 <table width="100%">
1377   <tr>
1378     <td>
1379       <table>
1380         <tr class="listheading">
1381           <th class="listheading" colspan="2" width="50%">|
1382     . $locale->text('Billing Address') . qq|</th>
1383           <th class="listheading" width="50%">|
1384     . $locale->text('Shipping Address') . qq|</th>
1385         </tr>
1386         <tr height="5"></tr>
1387         <tr>
1388           <th align="right" nowrap>$number</th>
1389           <td>$form->{"$form->{vc}number"}</td>
1390         </tr>
1391         <tr>
1392           <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
1393           <td>$form->{name}</td>
1394           <td><input name="shiptoname" size="35" value="$form->{shiptoname}"></td>
1395         </tr>
1396         <tr>
1397           <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
1398           <td>$form->{department_1}</td>
1399           <td><input name="shiptodepartment_1" size="35" value="$form->{shiptodepartment_1}"></td>
1400         </tr>
1401         <tr>
1402           <th align="right" nowrap>&nbsp;</th>
1403           <td>$form->{department_2}</td>
1404           <td><input name="shiptodepartment_2" size="35" value="$form->{shiptodepartment_2}"></td>
1405         </tr>
1406         <tr>
1407           <th align="right" nowrap>| . $locale->text('Street') . qq|</th>
1408           <td>$form->{street}</td>
1409           <td><input name="shiptostreet" size="35" value="$form->{shiptostreet}"></td>
1410         </tr>
1411         <tr>
1412           <th align="right" nowrap>| . $locale->text('Zipcode') . qq|</th>
1413           <td>$form->{zipcode}</td>
1414           <td><input name="shiptozipcode" size="35" value="$form->{shiptozipcode}"></td>
1415         </tr>
1416         <tr>
1417           <th align="right" nowrap>| . $locale->text('City') . qq|</th>
1418           <td>$form->{city}</td>
1419           <td><input name="shiptocity" size="35" value="$form->{shiptocity}"></td>
1420         </tr>
1421         <tr>
1422           <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
1423           <td>$form->{country}</td>
1424           <td><input name="shiptocountry" size="35" value="$form->{shiptocountry}"></td>
1425         </tr>
1426         <tr>
1427           <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
1428           <td>$form->{contact}</td>
1429           <td><input name="shiptocontact" size="35" value="$form->{shiptocontact}"></td>
1430         </tr>
1431         <tr>
1432           <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
1433           <td>$form->{phone}</td>
1434           <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
1435         </tr>
1436         <tr>
1437           <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
1438           <td>$form->{fax}</td>
1439           <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
1440         </tr>
1441         <tr>
1442           <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
1443           <td>$form->{email}</td>
1444           <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
1445         </tr>
1446       </table>
1447     </td>
1448   </tr>
1449 </table>
1450 | . $cgi->hidden("-name" => "nextsub", "-value" => $nextsub);
1451 ;
1452
1453
1454
1455   # delete shipto
1456   map({ delete $form->{$_} } (@shipto_vars, qw(header)));
1457   $form->{title} = $title;
1458
1459   foreach $key (keys %$form) {
1460     $form->{$key} =~ s/\"/&quot;/g;
1461     print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1462   }
1463
1464   print qq|
1465
1466 <hr size="3" noshade>
1467
1468 <br>
1469 <input class="submit" type="submit" name="action" value="|
1470     . $locale->text('Continue') . qq|">
1471 </form>
1472
1473 </body>
1474 </html>
1475 |;
1476
1477   $lxdebug->leave_sub();
1478 }
1479
1480 sub new_license {
1481   $lxdebug->enter_sub();
1482
1483   my $row = shift;
1484
1485   # change callback
1486   $form->{old_callback} = $form->escape($form->{callback}, 1);
1487   $form->{callback} = $form->escape("$form->{script}?action=display_form", 1);
1488   $form->{old_callback} = $form->escape($form->{old_callback}, 1);
1489
1490   # delete action
1491   delete $form->{action};
1492   $customer = $form->{customer};
1493   map { $form->{"old_$_"} = $form->{"${_}_$row"} } qw(partnumber description);
1494
1495   # save all other form variables in a previousform variable
1496   $form->{row} = $row;
1497   foreach $key (keys %$form) {
1498
1499     # escape ampersands
1500     $form->{$key} =~ s/&/%26/g;
1501     $previousform .= qq|$key=$form->{$key}&|;
1502   }
1503   chop $previousform;
1504   $previousform = $form->escape($previousform, 1);
1505
1506   $form->{script} = "licenses.pl";
1507
1508   map { $form->{$_} = $form->{"old_$_"} } qw(partnumber description);
1509   map { $form->{$_} = $form->escape($form->{$_}, 1) }
1510     qw(partnumber description);
1511   $form->{callback} =
1512     qq|$form->{script}?login=$form->{login}&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|;
1513   $form->redirect;
1514
1515   $lxdebug->leave_sub();
1516 }
1517
1518 sub relink_accounts {
1519   $lxdebug->enter_sub();
1520
1521   $form->{"taxaccounts"} =~ s/\s*$//;
1522   $form->{"taxaccounts"} =~ s/^\s*//;
1523   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1524     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1525   }
1526   $form->{"taxaccounts"} = "";
1527
1528   for (my $i = 1; $i <= $form->{"rowcount"}; $i++) {
1529     if ($form->{"id_$i"}) {
1530       IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1);
1531     }
1532   }
1533
1534   $lxdebug->leave_sub();
1535 }
1536
1537 sub set_duedate {
1538   $lxdebug->enter_sub();
1539
1540   $form->get_duedate(\%myconfig);
1541
1542   my $q = new CGI;
1543   $result = "$form->{duedate}";
1544   print $q->header();
1545   print $result;
1546   $lxdebug->leave_sub();
1547
1548 }
1549