db6a934dbb2beaeed009de634ac64ecd665f6384
[kivitendo-erp.git] / bin / mozilla / oe.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 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Order entry module
31 # Quotation module
32 #======================================================================
33
34 use POSIX qw(strftime);
35
36 use SL::OE;
37 use SL::IR;
38 use SL::IS;
39 use SL::PE;
40 use SL::ReportGenerator;
41
42 require "bin/mozilla/io.pl";
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/reportgenerator.pl";
45
46 1;
47
48 # end of main
49
50 # For locales.pl:
51 # $locale->text('Edit the purchase_order');
52 # $locale->text('Edit the sales_order');
53 # $locale->text('Edit the request_quotation');
54 # $locale->text('Edit the sales_quotation');
55
56 # $locale->text('Workflow purchase_order');
57 # $locale->text('Workflow sales_order');
58 # $locale->text('Workflow request_quotation');
59 # $locale->text('Workflow sales_quotation');
60
61 sub set_headings {
62   $lxdebug->enter_sub();
63
64   my ($action) = @_;
65
66   if ($form->{type} eq 'purchase_order') {
67     $form->{title}   = $action eq "edit" ?
68       $locale->text('Edit Purchase Order') :
69       $locale->text('Add Purchase Order');
70     $form->{heading} = $locale->text('Purchase Order');
71     $form->{vc}      = 'vendor';
72   }
73   if ($form->{type} eq 'sales_order') {
74     $form->{title}   = $action eq "edit" ?
75       $locale->text('Edit Sales Order') :
76       $locale->text('Add Sales Order');
77     $form->{heading} = $locale->text('Sales Order');
78     $form->{vc}      = 'customer';
79   }
80   if ($form->{type} eq 'request_quotation') {
81     $form->{title}   = $action eq "edit" ?
82       $locale->text('Edit Request for Quotation') :
83       $locale->text('Add Request for Quotation');
84     $form->{heading} = $locale->text('Request for Quotation');
85     $form->{vc}      = 'vendor';
86   }
87   if ($form->{type} eq 'sales_quotation') {
88     $form->{title}   = $action eq "edit" ?
89       $locale->text('Edit Quotation') :
90       $locale->text('Add Quotation');
91     $form->{heading} = $locale->text('Quotation');
92     $form->{vc}      = 'customer';
93   }
94
95   $lxdebug->leave_sub();
96 }
97
98 sub add {
99   $lxdebug->enter_sub();
100
101   set_headings("add");
102
103   $form->{callback} =
104     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}"
105     unless $form->{callback};
106
107   &order_links;
108   &prepare_order;
109   &display_form;
110
111   $lxdebug->leave_sub();
112 }
113
114 sub edit {
115   $lxdebug->enter_sub();
116   # show history button
117   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
118   #/show hhistory button
119
120   $form->{simple_save} = 0;
121
122   set_headings("edit");
123
124   # editing without stuff to edit? try adding it first
125   if ($form->{rowcount} && !$form->{print_and_save}) {
126     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
127     if (!$id) {
128
129       # reset rowcount
130       undef $form->{rowcount};
131       &add;
132       $lxdebug->leave_sub();
133       return;
134     }
135   } elsif (!$form->{id}) {
136     &add;
137     $lxdebug->leave_sub();
138     return;
139   }
140
141   if ($form->{print_and_save}) {
142     $form->{action}   = "print";
143     $form->{resubmit} = 1;
144     $language_id = $form->{language_id};
145     $printer_id = $form->{printer_id};
146   }
147
148   set_headings("edit");
149
150   &order_links;
151   &prepare_order;
152   if ($form->{print_and_save}) {
153     $form->{language_id} = $language_id;
154     $form->{printer_id} = $printer_id;
155   }
156   &display_form;
157
158   $lxdebug->leave_sub();
159 }
160
161 sub order_links {
162   $lxdebug->enter_sub();
163   # get customer/vendor
164   $form->all_vc(\%myconfig, $form->{vc},
165                 ($form->{vc} eq 'customer') ? "AR" : "AP");
166
167   # retrieve order/quotation
168   $form->{webdav}   = $webdav;
169   $form->{jsscript} = 1;
170
171   my $editing = $form->{id};
172
173   OE->retrieve(\%myconfig, \%$form);
174
175   if ($form->{payment_id}) {
176     $payment_id = $form->{payment_id};
177   }
178   if ($form->{language_id}) {
179     $language_id = $form->{language_id};
180   }
181   if ($form->{taxzone_id}) {
182     $taxzone_id = $form->{taxzone_id};
183   }
184
185   $salesman_id = $form->{salesman_id} if ($editing);
186
187
188   # if multiple rowcounts (== collective order) then check if the
189   # there were more than one customer (in that case OE::retrieve removes
190   # the content from the field)
191   if (   $form->{rowcount}
192       && $form->{type} eq 'sales_order'
193       && defined $form->{customer}
194       && $form->{customer} eq '') {
195
196     #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
197     $form->error(
198                  $locale->text(
199                    'Collective Orders only work for orders from one customer!')
200     );
201   }
202
203   $taxincluded = $form->{taxincluded};
204   $form->{shipto} = 1 if $form->{id};
205
206   if ($form->{"all_$form->{vc}"}) {
207     unless ($form->{"$form->{vc}_id"}) {
208       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
209     }
210   }
211
212   $cp_id    = $form->{cp_id};
213   $intnotes = $form->{intnotes};
214
215   # get customer / vendor
216   if ($form->{type} =~ /(purchase_order|request_quotation)/) {
217     IR->get_vendor(\%myconfig, \%$form);
218
219     #quote all_vendor Bug 133
220     foreach $ref (@{ $form->{all_vendor} }) {
221       $ref->{name} = $form->quote($ref->{name});
222     }
223
224   }
225   if ($form->{type} =~ /sales_(order|quotation)/) {
226     IS->get_customer(\%myconfig, \%$form);
227
228     #quote all_vendor Bug 133
229     foreach $ref (@{ $form->{all_customer} }) {
230       $ref->{name} = $form->quote($ref->{name});
231     }
232
233   }
234   $form->{cp_id} = $cp_id;
235
236   if ($payment_id) {
237     $form->{payment_id} = $payment_id;
238   }
239   if ($language_id) {
240     $form->{language_id} = $language_id;
241   }
242   if ($taxzone_id) {
243     $form->{taxzone_id} = $taxzone_id;
244   }
245   $form->{intnotes} = $intnotes if $intnotes;
246   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
247   $form->{"old$form->{vc}"} =
248     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
249
250   # build the popup menus
251   if (@{ $form->{"all_$form->{vc}"} }) {
252     $form->{ $form->{vc} } =
253       qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
254     map { $form->{"select$form->{vc}"} .=
255 "<option>$_->{name}--$_->{id}</option>\n" }
256       (@{ $form->{"all_$form->{vc}"} });
257   }
258
259   $form->{taxincluded} = $taxincluded if ($form->{id});
260
261   # departments
262   if (@{ $form->{all_departments} }) {
263     $form->{selectdepartment} = "<option>\n";
264     $form->{department}       = "$form->{department}--$form->{department_id}";
265
266     map {
267       $form->{selectdepartment} .=
268         "<option>$_->{description}--$_->{id}</option>\n"
269     } (@{ $form->{all_departments} });
270   }
271
272   $form->{employee} = "$form->{employee}--$form->{employee_id}";
273
274   # forex
275   $form->{forex} = $form->{exchangerate};
276
277   $form->{salesman_id} = $salesman_id if ($editing);
278
279   $lxdebug->leave_sub();
280 }
281
282 sub prepare_order {
283   $lxdebug->enter_sub();
284   $form->{formname} = $form->{type} unless $form->{formname};
285
286   my $i = 0;
287   foreach $ref (@{ $form->{form_details} }) {
288     $form->{rowcount} = ++$i;
289
290     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
291   }
292   for my $i (1 .. $form->{rowcount}) {
293     if ($form->{id}) {
294       $form->{"discount_$i"} =
295         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
296     } else {
297       $form->{"discount_$i"} =
298         $form->format_amount(\%myconfig, $form->{"discount_$i"});
299     }
300     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
301     $dec           = length $dec;
302     $decimalplaces = ($dec > 2) ? $dec : 2;
303
304     # copy reqdate from deliverydate for invoice -> order conversion
305     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
306       unless $form->{"reqdate_$i"};
307
308     $form->{"sellprice_$i"} =
309       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
310                            $decimalplaces);
311
312     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
313     $dec_qty = length $dec_qty;
314     $form->{"qty_$i"} =
315       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
316
317     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
318       qw(partnumber description unit);
319   }
320
321   $lxdebug->leave_sub();
322 }
323
324 sub form_header {
325   $lxdebug->enter_sub();
326
327   my $checkedclosed = $form->{"closed"} ? "checked" : "";
328   my $checkeddelivered = $form->{"delivered"} ? "checked" : "";
329
330   if ($form->{old_employee_id}) {
331     $form->{employee_id} = $form->{old_employee_id};
332   }
333   if ($form->{old_salesman_id}) {
334     $form->{salesman_id} = $form->{old_salesman_id};
335   }
336
337   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
338
339
340   if ($form->{old_employee_id}) {
341     $form->{employee_id} = $form->{old_employee_id};
342   }
343   if ($form->{old_salesman_id}) {
344     $form->{salesman_id} = $form->{old_salesman_id};
345   }
346
347   map { $form->{$_} =~ s/\"/&quot;/g }
348     qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
349        shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
350        shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
351
352   # use JavaScript Calendar or not
353   $form->{jsscript} = 1;
354   $jsscript = "";
355
356   $button1 = qq|
357      <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
358       <input type=button name=transdate id="trigger1" value=|
359     . $locale->text('button') . qq|></td>
360     |;
361   $button2 = qq|
362      <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
363       <input type=button name=reqdate name=reqdate id="trigger2" value=|
364     . $locale->text('button') . qq|></td>
365    |;
366
367   #write Trigger
368   $jsscript =
369     Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
370                         "reqdate", "BL", "trigger2");
371
372   my @tmp;
373
374   if (($form->{"type"} eq "sales_order") ||
375       ($form->{"type"} eq "purchase_order")) {
376     push(@tmp, qq|
377           <input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" $checkeddelivered>
378           <label for="delivered">| . $locale->text('Delivered') . qq|</label>|);
379   }
380
381   if ($form->{id}) {
382     push(@tmp, qq|
383           <input name="closed" id="closed" type="checkbox" class="checkbox" value="1" $checkedclosed>
384           <label for="closed">| . $locale->text('Closed') . qq|</label>|);
385   }
386
387   if (@tmp) {
388     $openclosed .= qq|
389       <tr>
390         <td colspan=| . (2 * scalar(@tmp)) . qq| align=center>| . join("\n", @tmp) . qq|
391         </td>
392       </tr>
393 |;
394   }
395
396   # set option selected
397   foreach $item ($form->{vc}, currency, department, ($form->{vc} eq "customer" ? customer : vendor)) {
398     $form->{"select$item"} =~ s/ selected//;
399     $form->{"select$item"} =~
400       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
401   }
402
403   #quote select[customer|vendor] Bug 133
404   $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
405
406   #substitute \n and \r to \s (bug 543)
407   $form->{"select$form->{vc}"} =~ s/[\n\r]/&nbsp;/g;
408   
409   my @old_project_ids = ($form->{"globalproject_id"});
410   map({ push(@old_project_ids, $form->{"project_id_$_"})
411           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
412
413   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
414   $form->get_lists("contacts"   => "ALL_CONTACTS",
415                    "shipto"     => "ALL_SHIPTO",
416                    "projects"   => {
417                      "key"      => "ALL_PROJECTS",
418                      "all"      => 0,
419                      "old_id"   => \@old_project_ids
420                    },
421                    "employees"  => "ALL_EMPLOYEES",
422                    "salesmen"   => "ALL_SALESMEN",
423                    "taxzones"   => "ALL_TAXZONES",
424                    "payments"   => "ALL_PAYMENTS",
425                    "currencies" => "ALL_CURRENCIES",
426                    $vc          => "ALL_" . uc($vc));
427
428   my %labels;
429   my @values = (undef);
430   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
431     push(@values, $item->{"cp_id"});
432     $labels{$item->{"cp_id"}} = $item->{"cp_name"} .
433       ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
434   }
435
436   my $contact;
437   if (scalar @values > 1) {
438     $contact = qq|
439     <tr>
440       <th align="right">| . $locale->text('Contact Person') . qq|</th>
441       <td>| .
442       NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values, '-style' => 'width: 250px',
443                            '-labels' => \%labels, '-default' => $form->{"cp_id"}))
444       . qq|
445       </td>
446     </tr>|;
447   }
448
449   %labels = ();
450   @values = ();
451
452   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
453     push(@values, $item->{"name"}.qq|--|.$item->{"id"});
454     $labels{$item->{"name"}.qq|--|.$item->{"id"}} = $item->{name};
455   }
456
457   $vc = qq|
458       <input type="hidden" name="$form->{vc}_id" value="| . H($form->{"$form->{vc}_id"}) . qq|">
459       <input type="hidden" name="old$form->{vc}" value="| . H($form->{"old$form->{vc}"}) . qq|">
460       <th align="right">| . $locale->text(ucfirst($form->{vc})) . qq|</th>
461       <td>| . 
462         (($myconfig{vclimit} <=  scalar(@values)) 
463               ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">| 
464               : (NTI($cgi->popup_menu('-name' => "$form->{vc}", '-default' => $form->{"old$form->{vc}"}, 
465                              '-onChange' => 'document.getElementById(\'update_button\').click();',
466                              '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')))) . qq|
467         <input type="button" value="?" onclick="show_vc_details('$form->{vc}')">
468       </td><input type=hidden name="select$form->{vc}" value="| .
469     Q($form->{"select$form->{vc}"}) . qq|">|;
470
471   %labels = ();
472   @values = ("");
473   foreach my $item (@{ $form->{"ALL_PAYMENTS"} }) {
474     push(@values, $item->{"id"});
475     $labels{$item->{"id"}} = $item->{"description"};
476   }
477   
478   $payments = qq|
479     <th align="right">| . $locale->text('Payment Terms') . qq|</th>
480     <td>| .
481     NTI($cgi->popup_menu('-name' => 'payment_id', '-values' => \@values, '-style' => 'width: 250px',
482                          '-labels' => \%labels, '-default' => $form->{payment_id}))
483     . qq|</td>|;
484
485   %labels = ();
486   @values = ("");
487   foreach my $item (@{ $form->{"ALL_SHIPTO"} }) {
488     push(@values, $item->{"shipto_id"});
489     $labels{$item->{"shipto_id"}} = join "; ", grep { $_ } map { $item->{"shipto${_}" } } qw(name department_1 street city);
490   }
491
492   my $shipto;
493   if (scalar @values > 1) {
494     $shipto = qq|
495     <tr>
496       <th align="right">| . $locale->text('Shipping Address') . qq|</th>
497       <td>| .
498       NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values, '-style' => 'width: 250px',
499                            '-labels' => \%labels, '-default' => $form->{"shipto_id"}))
500     . qq|</td>|;
501   }
502
503   %labels = ();
504   @values = ("");
505   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
506     push(@values, $item->{"id"});
507     $labels{$item->{"id"}} = $item->{"projectnumber"};
508   }
509   my $globalprojectnumber =
510     NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
511                          '-labels' => \%labels,
512                          '-default' => $form->{"globalproject_id"}));
513   
514   my $salesmen = "";
515   %labels = ();
516   @values = ();
517   if ($form->{type} =~ /^sales_/) {
518     foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
519       push(@values, $item->{"id"});
520       $labels{$item->{"id"}} = ($item->{"name"} ne "" ? $item->{"name"} : $item->{"login"});
521     }
522     $salesmen =
523       qq|<tr>
524           <th align="right">| . $locale->text('Salesman') . qq|</th>
525           <td>| .
526       NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{"salesman_id"} ? $form->{"salesman_id"} : $form->{"employee_id"},
527                            '-values' => \@values, '-labels' => \%labels))
528       . qq|</td>
529          </tr>|;
530   }
531
532   %labels = ();
533   @values = ();
534   foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
535     push(@values, $item->{"id"});
536     $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
537   }
538
539   my $employee = qq|
540     <tr>
541       <th align="right">| . $locale->text('Employee') . qq|</th>
542       <td>| .
543         NTI($cgi->popup_menu('-name' => 'employee_id', '-default' => $form->{"employee_id"},
544                              '-values' => \@values, '-labels' => \%labels)) . qq|
545       </td>
546     </tr>|;
547
548   %labels = ();
549   @values = ();
550   foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
551     push(@values, $item->{"id"});
552     $labels{$item->{"id"}} = $item->{"description"};
553   }
554
555   $taxzone = qq|
556     <tr>
557       <th align="right">| . $locale->text('Steuersatz') . qq|</th>
558       <td>| .
559         NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
560                              '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')) . qq|
561       </td>
562     </tr>|;
563
564   %labels = ();
565   @values = ();
566   my $i = 0;
567   foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
568     push(@values, $item);
569     $labels{$item} = $item;
570   }
571
572   $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
573   my $currencies;
574   if (scalar @values) {
575     $currencies = qq|
576     <tr>
577       <th align="right">| . $locale->text('Currency') . qq|</th>
578       <td>| .
579         NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
580                              '-values' => \@values, '-labels' => \%labels)) . qq|
581       </td>
582     </tr>|;
583   }
584
585   $form->{exchangerate} =
586     $form->format_amount(\%myconfig, $form->{exchangerate});
587
588   if (!$form->{exchangerate}) {
589     $form->{exchangerate} = "";
590   }
591
592   if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
593     $creditwarning = 1;
594   } else {
595     $creditwarning = 0;
596   }
597
598   $form->{creditlimit} =
599     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
600   $form->{creditremaining} =
601     $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
602
603   $exchangerate = qq|
604 <input type=hidden name=forex value=$form->{forex}>
605 |;
606
607   if ($form->{currency} ne $form->{defaultcurrency}) {
608     if ($form->{forex}) {
609       $exchangerate .=
610           qq|<th align=right>|
611         . $locale->text('Exchangerate')
612         . qq|</th><td>$form->{exchangerate}</td>
613       <input type=hidden name=exchangerate value=$form->{exchangerate}>
614 |;
615     } else {
616       $exchangerate .=
617           qq|<th align=right>|
618         . $locale->text('Exchangerate')
619         . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
620     }
621   }
622
623   if ($form->{business}) {
624     $business = qq|
625               <tr>
626           <th align="right">| . ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type')) . qq|</th>
627           <td>$form->{business}; | . $locale->text('Trade Discount') . qq| |
628       . $form->format_amount(\%myconfig, $form->{tradediscount} * 100)
629       . qq| %</td>
630         </tr>
631 |;
632   }
633
634   if ($form->{max_dunning_level}) {
635     $dunning = qq|
636       <tr>
637         <th align="right">| . $locale->text('Max. Dunning Level') . qq|:</th>
638         <td>
639           <b>$form->{max_dunning_level}</b>;
640           | . $locale->text('Dunning Amount') . qq|: <b>|
641         . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
642         . qq|</b>
643         </td>
644       </tr>
645 |;
646   }
647
648   if ($form->{type} !~ /_quotation$/) {
649     $ordnumber = qq|
650               <tr>
651                 <th width=70% align=right nowrap>| . $locale->text('Order Number') . qq|</th>
652                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
653               </tr>
654               <tr>
655                 <th width=70% align=right nowrap>|
656       . $locale->text('Quotation Number') . qq|</th>
657                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
658               </tr>
659               <tr>
660                 <th width=70% align=right nowrap>|
661       . $locale->text('Customer Order Number') . qq|</th>
662                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
663               </tr>
664               <tr>
665                 <th align=right nowrap>| . $locale->text('Order Date') . qq|</th>
666                 $button1
667
668               </tr>
669               <tr>
670                 <th align=right nowrap=true>| . $locale->text('Required by') . qq|</th>
671                 $button2
672               </tr>
673 |;
674
675     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
676
677     $creditremaining = qq|
678         $shipto
679         <tr>
680           <td align="right">| . $locale->text('Credit Limit') . qq|</td>
681           <td>$form->{creditlimit}; | . $locale->text('Remaining') . qq| <span class="plus$n">$form->{creditremaining}</span></td>
682         </tr>
683               </tr>
684 |;
685   } else {
686     $reqlabel =
687       ($form->{type} eq 'sales_quotation')
688       ? $locale->text('Valid until')
689       : $locale->text('Required by');
690     if ($form->{type} eq 'sales_quotation') {
691       $ordnumber = qq|
692               <tr>
693                 <th width=70% align=right nowrap>|
694         . $locale->text('Quotation Number') . qq|</th>
695                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
696                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
697               </tr>
698 |;
699     } else {
700       $ordnumber = qq|
701               <tr>
702                 <th width=70% align=right nowrap>| . $locale->text('RFQ Number') . qq|</th>
703                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
704                 <input type=hidden name=ordnumber value="$form->{ordnumber}">
705               </tr>
706 |;
707
708     }
709
710     $ordnumber .= qq|
711               <tr>
712                 <th align=right nowrap>| . $locale->text('Quotation Date') . qq|</th>
713                 $button1
714               </tr>
715               <tr>
716                 <th align=right nowrap=true>$reqlabel</th>
717                 $button2
718               </tr>
719 |;
720     $creditremaining = qq| <tr>
721                             <td colspan=4></td>
722                             $shipto
723                           </tr>|;
724   }
725
726   $department = qq|
727               <tr>
728                 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
729                 <td colspan=3><select name=department style="width: 250px">$form->{selectdepartment}</select>
730                 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
731                 </td>
732               </tr> | if $form->{selectdepartment};
733
734   if ($form->{type} eq 'sales_order') {
735     if ($form->{selectemployee}) {
736       $employee .= qq|
737         <input type="hidden" name="customer_klass" value="$form->{customer_klass}">|;
738     }
739   } else {
740     $employee .= qq|
741       <input type="hidden" name="customer_klass" value="$form->{customer_klass}">|;
742   }
743   if ($form->{resubmit} && ($form->{format} eq "html")) {
744     $onload =
745       qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
746   } elsif ($form->{resubmit}) {
747     $onload = qq|document.oe.submit()|;
748   } else {
749     $onload = "focus()";
750   }
751
752   $credittext = $locale->text('Credit Limit exceeded!!!');
753   if ($creditwarning) {
754     $onload = qq|alert('$credittext')|;
755   }
756   
757   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
758   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
759   
760   $form->{"javascript"} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
761   # show history button js
762   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
763   #/show history button js
764   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
765
766   $form->header;
767
768   print qq|
769 <body onLoad="$onload">
770
771 <form method=post name=oe action=$form->{script}>
772
773  <script type="text/javascript" src="js/common.js"></script>
774  <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
775  <script type="text/javascript" src="js/vendor_selection.js"></script>
776  <script type="text/javascript" src="js/calculate_qty.js"></script>
777 |;
778
779   $form->hide_form(qw(id action type vc formname media format proforma queued printed emailed
780                       title discount creditlimit creditremaining tradediscount business
781                       max_dunning_level dunning_amount));
782
783   print qq|
784
785 <table width=100%>
786   <tr class=listtop>
787     <th class=listtop>$form->{title}</th>
788   </tr>
789   <tr height="5"></tr>
790   <tr>
791     <td>
792       <table width="100%">
793         <tr valign=top>
794           <td>
795             <table width=100%>
796               <tr>
797         $vc
798         $contact
799               $creditremaining
800               $business
801               $dunning
802               $taxzone
803               $department
804               <tr>
805                 $currencies
806                 $exchangerate
807               </tr>
808               <tr>
809                 <th align=right>| . $locale->text('Shipping Point') . qq|</th>
810                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
811               </tr>
812               <tr>
813                 <th align=right>| . $locale->text('Ship via') . qq|</th>
814                 <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
815               </tr>
816               <tr>
817                 <th align="right">| . $locale->text('Transaction description') . qq|</th>
818                 <td colspan="3"><input name="transaction_description" size="35" value="| . H($form->{transaction_description}) . qq|"></td>
819               </tr>|;
820 #              <tr>
821 #                 <td colspan=4>
822 #                   <table>
823 #                     <tr>
824 #                       <td colspan=2>
825 #                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
826 #                       </td>
827 #                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
828 #                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
829 #                     </tr>
830 #                     <tr>
831 #                       <td colspan=2>
832 #                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
833 #                       </td>
834 #                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
835 #                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
836 #                     </tr>
837 #                   </table>
838 #                 </td>
839 #               </tr>
840 print qq|           </table>
841           </td>
842           <td align=right>
843             <table>
844               $openclosed
845               $employee
846         $salesmen
847               $ordnumber
848               <tr>
849           <th width="70%" align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
850           <td>$globalprojectnumber</td>
851               </tr>
852             </table>
853           </td>
854         </tr>
855       </table>
856     </td>
857   </tr>
858
859 $jsscript
860
861 <!-- shipto are in hidden variables -->
862
863 <input type=hidden name=shiptoname value="$form->{shiptoname}">
864 <input type=hidden name=shiptostreet value="$form->{shiptostreet}">
865 <input type=hidden name=shiptozipcode value="$form->{shiptozipcode}">
866 <input type=hidden name=shiptocity value="$form->{shiptocity}">
867 <input type=hidden name=shiptocountry value="$form->{shiptocountry}">
868 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
869 <input type=hidden name=shiptophone value="$form->{shiptophone}">
870 <input type=hidden name=shiptofax value="$form->{shiptofax}">
871 <input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
872 <input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
873 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
874
875 <!-- email variables -->
876 <input type=hidden name=message value="$form->{message}">
877 <input type=hidden name=email value="$form->{email}">
878 <input type=hidden name=subject value="$form->{subject}">
879 <input type=hidden name=cc value="$form->{cc}">
880 <input type=hidden name=bcc value="$form->{bcc}">
881
882 <input type=hidden name=taxpart value="$form->{taxpart}">
883 <input type=hidden name=taxservice value="$form->{taxservice}">
884
885 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
886 |;
887
888   foreach $item (split / /, $form->{taxaccounts}) {
889     print qq|
890 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
891 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
892 |;
893   }
894   $lxdebug->leave_sub();
895 }
896
897 sub form_footer {
898   $lxdebug->enter_sub();
899
900   $form->{invtotal} = $form->{invsubtotal};
901
902   if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
903     $rows = 2;
904   }
905   if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
906     $introws = 2;
907   }
908   $rows = ($rows > $introws) ? $rows : $introws;
909   $notes =
910     qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
911   $intnotes =
912     qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
913
914   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
915
916   $taxincluded = "";
917   if ($form->{taxaccounts}) {
918     $taxincluded = qq|
919               <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
920       . $locale->text('Tax Included') . qq|</b><br><br>
921 |;
922   }
923
924   if (!$form->{taxincluded}) {
925
926     foreach $item (split / /, $form->{taxaccounts}) {
927       if ($form->{"${item}_base"}) {
928         $form->{invtotal} += $form->{"${item}_total"} =
929           $form->round_amount(
930                              $form->{"${item}_base"} * $form->{"${item}_rate"},
931                              2);
932         $form->{"${item}_total"} =
933           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
934
935         $tax .= qq|
936               <tr>
937                 <th align=right>$form->{"${item}_description"}&nbsp;|
938                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
939                 <td align=right>$form->{"${item}_total"}</td>
940               </tr>
941 |;
942       }
943     }
944
945     $form->{invsubtotal} =
946       $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
947
948     $subtotal = qq|
949               <tr>
950                 <th align=right>| . $locale->text('Subtotal') . qq|</th>
951                 <td align=right>$form->{invsubtotal}</td>
952               </tr>
953 |;
954
955   }
956
957   if ($form->{taxincluded}) {
958     foreach $item (split / /, $form->{taxaccounts}) {
959       if ($form->{"${item}_base"}) {
960         $form->{"${item}_total"} =
961           $form->round_amount(
962                            ($form->{"${item}_base"} * $form->{"${item}_rate"} /
963                               (1 + $form->{"${item}_rate"})
964                            ),
965                            2);
966         $form->{"${item}_netto"} =
967           $form->round_amount(
968                           ($form->{"${item}_base"} - $form->{"${item}_total"}),
969                           2);
970         $form->{"${item}_total"} =
971           $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
972         $form->{"${item}_netto"} =
973           $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
974
975         $tax .= qq|
976               <tr>
977                 <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
978                                     . $form->{"${item}_rate"} * 100 .qq|%</th>
979                 <td align=right>$form->{"${item}_total"}</td>
980               </tr>
981               <tr>
982                 <th align=right>Nettobetrag</th>
983                 <td align=right>$form->{"${item}_netto"}</td>
984               </tr>
985 |;
986       }
987     }
988
989   }
990
991   $form->{oldinvtotal} = $form->{invtotal};
992   $form->{invtotal}    =
993     $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
994
995   print qq|
996   <tr>
997     <td>
998       <table width=100%>
999         <tr valign=bottom>
1000           <td>
1001             <table>
1002               <tr>
1003                 <th align=left>| . $locale->text('Notes') . qq|</th>
1004                 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
1005               </tr>
1006               <tr valign=top>
1007                 <td>$notes</td>
1008                 <td>$intnotes</td>
1009               </tr>
1010           <tr>
1011     $payments
1012             </tr>
1013       </table>
1014           </td>
1015           <td>
1016             <table>
1017 |;
1018
1019   if ($form->{type} =~ /^sales_/) {
1020     print qq|
1021             <tr>
1022               <th  align=left>| . $locale->text('Ertrag') . qq|</th>
1023               <td>| .  $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
1024             </tr>
1025             <tr>
1026               <th  align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
1027               <td>| .  $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
1028             </tr>
1029 |;
1030   }
1031
1032   print qq|
1033             <input type=hidden name="marge_total" value="$form->{"marge_total"}">
1034             <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
1035             </table>
1036           </td>
1037           <td align=right>
1038             $taxincluded
1039             <table>
1040               $subtotal
1041               $tax
1042               <tr>
1043                 <th align=right>| . $locale->text('Total') . qq|</th>
1044                 <td align=right>$form->{invtotal}</td>
1045               </tr>
1046             </table>
1047           </td>
1048         </tr>
1049       </table>
1050     </td>
1051   </tr>
1052 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
1053 <input type=hidden name=oldtotalpaid value=$totalpaid>
1054   <tr>
1055     <td><hr size=3 noshade></td>
1056   </tr>
1057 |;
1058
1059   if ($webdav) {
1060     $webdav_list = qq|
1061
1062   <tr>
1063     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
1064   </tr>
1065     <table width=100%>
1066       <td align=left width=30%><b>Dateiname</b></td>
1067       <td align=left width=70%><b>Webdavlink</b></td>
1068 |;
1069     foreach $file (@{ $form->{WEBDAV} }) {
1070       $webdav_list .= qq|
1071       <tr>
1072         <td align="left">$file->{name}</td>
1073         <td align="left"><a href="$file->{link}">$file->{type}</a></td>
1074       </tr>
1075 |;
1076     }
1077     $webdav_list .= qq|
1078     </table>
1079   </tr>
1080   <tr>
1081     <td><hr size=3 noshade></td>
1082   </tr>
1083 |;
1084
1085     print $webdav_list;
1086   }
1087
1088   print qq|
1089   <tr>
1090     <td>
1091 |;
1092   print_options();
1093
1094   print qq|
1095     </td>
1096   </tr>
1097 </table>
1098
1099 | . $locale->text("Edit the $form->{type}") . qq|<br>
1100 <input class=submit type=submit name=action id=update_button value="|
1101     . $locale->text('Update') . qq|">
1102 <input class=submit type=submit name=action value="|
1103     . $locale->text('Ship to') . qq|">
1104 <input class=submit type=submit name=action value="|
1105     . $locale->text('Print') . qq|">
1106 <input class=submit type=submit name=action value="|
1107     . $locale->text('E-mail') . qq|">
1108 <input class=submit type=submit name=action value="|
1109     . $locale->text('Save') . qq|">
1110 <input class=submit type=submit name=action value="|
1111     . $locale->text('Save and Close') . qq|">
1112 |;
1113
1114   if (($form->{id})) {
1115     print qq|
1116         <input type="button" class="submit" onclick="set_history_window(|
1117         . Q($form->{id})
1118         . qq|);" name="history" id="history" value="|
1119         . $locale->text('history')
1120         . qq|">
1121
1122 <br>| . $locale->text("Workflow $form->{type}") . qq|<br>
1123 <input class=submit type=submit name=action value="|
1124       . $locale->text('Save as new') . qq|">
1125 <input class=submit type=submit name=action value="|
1126       . $locale->text('Delete') . qq|">|;
1127     if (($form->{type} =~ /sales_quotation$/)) {
1128       print qq|
1129 <input class=submit type=submit name=action value="|
1130         . $locale->text('Sales Order') . qq|">|;
1131     }
1132     if ($form->{type} =~ /request_quotation$/) {
1133       print qq|
1134 <input class=submit type=submit name=action value="|
1135         . $locale->text('Purchase Order') . qq|">|;
1136     }
1137     print qq|
1138 <input class=submit type=submit name=action value="|
1139       . $locale->text('Invoice') . qq|">
1140 |;
1141
1142     if ($form->{type} =~ /sales_order$/) {
1143       print qq|
1144 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1145 <input class=submit type=submit name=action value="|
1146         . $locale->text('Purchase Order') . qq|">
1147 <input class=submit type=submit name=action value="|
1148         . $locale->text('Quotation') . qq|">
1149 |;
1150
1151     } elsif ($form->{type} =~ /purchase_order$/) {
1152       print qq|
1153 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1154 <input class=submit type=submit name=action value="|
1155         . $locale->text('Sales Order') . qq|">
1156 <input class=submit type=submit name=action value="|
1157         . $locale->text('Request for Quotation') . qq|">
1158 |;
1159
1160     } else {
1161       print qq|
1162 <br>$form->{heading} als neue Vorlage verwenden f&uuml;r<br>
1163 <input class=submit type=submit name=action value="|
1164         . $locale->text('Order') . qq|">
1165 |;
1166     }
1167   }
1168
1169   $form->hide_form("saved_xyznumber");
1170
1171   print qq|
1172
1173 <input type=hidden name=rowcount value=$form->{rowcount}>
1174
1175 <input name=callback type=hidden value="$form->{callback}">
1176
1177 <input type=hidden name=login value=$form->{login}>
1178 <input type=hidden name=password value=$form->{password}>
1179
1180 </form>
1181
1182 </body>
1183 </html>
1184 |;
1185   $lxdebug->leave_sub();
1186 }
1187
1188 sub update {
1189   $lxdebug->enter_sub();
1190
1191   set_headings($form->{"id"} ? "edit" : "add");
1192
1193   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1194     qw(exchangerate creditlimit creditremaining);
1195   $form->{update} = 1;
1196       
1197   if($form->{payment_id}) { 
1198     $payment_id = $form->{payment_id};
1199   }
1200   
1201   &check_name($form->{vc});
1202   
1203   if($form->{payment_id} eq "") { 
1204     $form->{payment_id} = $payment_id;
1205   }
1206   
1207   $buysell              = 'buy';
1208   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
1209   $form->{exchangerate} = $exchangerate
1210     if (
1211         $form->{forex} = (
1212                   $exchangerate =
1213                     $form->check_exchangerate(
1214                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
1215                     )));
1216
1217   # for pricegroups
1218   $i = $form->{rowcount};
1219
1220   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
1221
1222   if (   ($form->{"partnumber_$i"} eq "")
1223       && ($form->{"description_$i"} eq "")
1224       && ($form->{"partsgroup_$i"}  eq "")) {
1225
1226     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
1227     &check_form;
1228
1229   } else {
1230
1231     if (   $form->{type} eq 'purchase_order'
1232         || $form->{type} eq 'request_quotation') {
1233       IR->retrieve_item(\%myconfig, \%$form);
1234     }
1235     if ($form->{type} eq 'sales_order' || $form->{type} eq 'sales_quotation') {
1236       IS->retrieve_item(\%myconfig, \%$form);
1237     }
1238
1239     my $rows = scalar @{ $form->{item_list} };
1240
1241     $form->{"discount_$i"} =
1242       $form->format_amount(\%myconfig, $form->{discount} * 100);
1243
1244     if ($rows) {
1245       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
1246
1247       if ($rows > 1) {
1248
1249         &select_item;
1250         exit;
1251
1252       } else {
1253
1254         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
1255         if ($form->{"not_discountable_$i"}) {
1256           $form->{"discount_$i"} = 0;
1257         }
1258         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
1259           qw(partnumber description unit);
1260         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
1261           keys %{ $form->{item_list}[0] };
1262         if ($form->{"part_payment_id_$i"} ne "") {
1263           $form->{payment_id} = $form->{"part_payment_id_$i"};
1264         }
1265
1266         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
1267
1268         ($dec) = ($s =~ /\.(\d+)/);
1269         $dec           = length $dec;
1270         $decimalplaces = ($dec > 2) ? $dec : 2;
1271
1272         if ($sellprice) {
1273           $form->{"sellprice_$i"} = $sellprice;
1274         } else {
1275
1276           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
1277
1278           # if there is an exchange rate adjust sellprice
1279           $form->{"sellprice_$i"} /= $exchangerate;
1280         }
1281
1282         $amount =
1283           $form->{"sellprice_$i"} * $form->{"qty_$i"} *
1284           (1 - $form->{"discount_$i"} / 100);
1285         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
1286         map { $form->{"${_}_base"} += $amount }
1287           (split / /, $form->{"taxaccounts_$i"});
1288         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
1289           split / /, $form->{taxaccounts}
1290           if !$form->{taxincluded};
1291
1292         $form->{creditremaining} -= $amount;
1293
1294         $form->{"sellprice_$i"} =
1295           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1296                                $decimalplaces);
1297         $form->{"qty_$i"} =
1298           $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1299
1300         # get pricegroups for parts
1301         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
1302
1303         # build up html code for prices_$i
1304         &set_pricegroup($i);
1305       }
1306
1307       &display_form;
1308
1309     } else {
1310
1311       # ok, so this is a new part
1312       # ask if it is a part or service item
1313
1314       if (   $form->{"partsgroup_$i"}
1315           && ($form->{"partsnumber_$i"} eq "")
1316           && ($form->{"description_$i"} eq "")) {
1317         $form->{rowcount}--;
1318         $form->{"discount_$i"} = "";
1319         display_form();
1320
1321       } else {
1322         $form->{"id_$i"}   = 0;
1323         new_item();
1324       }
1325     }
1326   }
1327
1328   $lxdebug->leave_sub();
1329 }
1330
1331 sub search {
1332   $lxdebug->enter_sub();
1333
1334   if ($form->{type} eq 'purchase_order') {
1335     $form->{title} = $locale->text('Purchase Orders');
1336     $form->{vc}    = 'vendor';
1337     $ordlabel      = $locale->text('Order Number');
1338     $ordnumber     = 'ordnumber';
1339     $employee      = $locale->text('Employee');
1340   }
1341
1342   if ($form->{type} eq 'request_quotation') {
1343     $form->{title} = $locale->text('Request for Quotations');
1344     $form->{vc}    = 'vendor';
1345     $ordlabel      = $locale->text('RFQ Number');
1346     $ordnumber     = 'quonumber';
1347     $employee      = $locale->text('Employee');
1348   }
1349
1350   if ($form->{type} eq 'sales_order') {
1351     $form->{title} = $locale->text('Sales Orders');
1352     $form->{vc}    = 'customer';
1353     $ordlabel      = $locale->text('Order Number');
1354     $ordnumber     = 'ordnumber';
1355     $employee      = $locale->text('Employee');
1356   }
1357
1358   if ($form->{type} eq 'sales_quotation') {
1359     $form->{title} = $locale->text('Quotations');
1360     $form->{vc}    = 'customer';
1361     $ordlabel      = $locale->text('Quotation Number');
1362     $ordnumber     = 'quonumber';
1363     $employee      = $locale->text('Employee');
1364   }
1365
1366   # setup vendor / customer selection
1367   $form->all_vc(\%myconfig, $form->{vc},
1368                 ($form->{vc} eq 'customer') ? "AR" : "AP");
1369
1370   # departments
1371   if (@{ $form->{all_departments} }) {
1372     $form->{selectdepartment} = "<option>\n";
1373
1374     map {
1375       $form->{selectdepartment} .=
1376         "<option>$_->{description}--$_->{id}</option>\n"
1377     } (@{ $form->{all_departments} });
1378   }
1379
1380   $department = qq|
1381         <tr>
1382           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
1383           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
1384         </tr>
1385 | if $form->{selectdepartment};
1386
1387   my $delivered;
1388   if (($form->{"type"} eq "sales_order") ||
1389       ($form->{"type"} eq "purchase_order")) {
1390     $delivered = qq|
1391         <tr>
1392           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
1393             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
1394           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
1395             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
1396         </tr>
1397 |;
1398   }
1399
1400   # use JavaScript Calendar or not
1401   $form->{jsscript} = 1;
1402   $jsscript = "";
1403
1404   $button1 = qq|
1405      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1406      <input type=button name=transdatefrom id="trigger3" value=|
1407     . $locale->text('button') . qq|></td>
1408     |;
1409   $button2 = qq|
1410      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
1411      <input type=button name=transdateto name=transdateto id="trigger4" value=|
1412     . $locale->text('button') . qq|></td>
1413    |;
1414
1415   #write Trigger
1416   $jsscript =
1417     Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
1418                         "transdateto", "BL", "trigger4");
1419
1420   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
1421
1422   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
1423                                    "all" => 1 },
1424                    "employees" => "ALL_EMPLOYEES",
1425                    $vc => "ALL_" . uc($vc));
1426
1427   my %labels = ();
1428   my @values = ("");
1429   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
1430     push(@values, $item->{"id"});
1431     $labels{$item->{"id"}} = $item->{"projectnumber"};
1432   }
1433   my $projectnumber =
1434     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
1435                          '-labels' => \%labels));
1436
1437   #employees
1438   %labels = ();
1439   @values = ("");
1440   foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
1441     push(@values, $item->{"id"});
1442     $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
1443   }
1444
1445   my $employee_block = qq|
1446     <tr>
1447       <th align="right">| . $locale->text('Employee') . qq|</th>
1448       <td>| .
1449         NTI($cgi->popup_menu('-name'   => 'employee_id',
1450                              '-values' => \@values,
1451                              '-labels' => \%labels)) . qq|
1452       </td>
1453     </tr>|;
1454
1455   %labels = ();
1456   @values = ("");
1457
1458   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
1459     push(@values, $item->{name}.qq|--|.$item->{"id"});
1460     $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
1461   }
1462
1463   my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
1464   $vc =
1465     $myconfig{vclimit} <=  scalar(@values)
1466     ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">| 
1467     : NTI($cgi->popup_menu('-name' => "$form->{vc}",
1468                            '-default' => $form->{"old$form->{vc}"},
1469                            '-onChange' => 'document.getElementById(\'update_button\').click();',
1470                            '-values' => \@values,
1471                            '-labels' => \%labels));
1472   $form->header;
1473
1474   print qq|
1475 <body>
1476
1477 <form method=post action=$form->{script}>
1478
1479 <table width=100%>
1480   <tr>
1481     <th class=listtop>$form->{title}</th>
1482   </tr>
1483   <tr height="5"></tr>
1484   <tr>
1485     <td>
1486       <table>
1487         <tr>
1488           <th align=right>$vc_label</th>
1489           <td colspan=3>$vc</td>
1490         </tr>
1491         $department
1492         <tr>
1493           <th align=right>$ordlabel</th>
1494           <td colspan=3><input name="$ordnumber" size=20></td>
1495         </tr>
1496   $employee_block
1497         <tr>
1498           <th align="right">| . $locale->text('Transaction description') . qq|</th>
1499           <td colspan="3"><input name="transaction_description" size=20></td>
1500         </tr>
1501         <tr>
1502           <th align="right">| . $locale->text("Project Number") . qq|</th>
1503           <td colspan="3">$projectnumber</td>
1504         </tr>
1505         <tr>
1506           <th align=right>| . $locale->text('From') . qq|</th>
1507           $button1
1508           <th align=right>| . $locale->text('Bis') . qq|</th>
1509           $button2
1510         </tr>
1511         <input type=hidden name=sort value=transdate>
1512         <tr>
1513           <th align=right>| . $locale->text('Include in Report') . qq|</th>
1514           <td colspan=5>
1515             <table>
1516         <tr>
1517           <td><input type="checkbox" name="open" value="1" id="open" checked>
1518             <label for="open">| . $locale->text("Open") . qq|</td>
1519           <td><input type="checkbox" name="closed" value="1" id="closed">
1520             <label for="closed">| . $locale->text("Closed") . qq|</td>
1521         </tr>
1522         $delivered
1523               <tr>
1524                 <td><input name="l_id" class=checkbox type=checkbox value=Y>
1525                 | . $locale->text('ID') . qq|</td>
1526                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
1527                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
1528     . $locale->text('Date') . qq|</td>
1529                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
1530     . $locale->text('Required by') . qq|</td>
1531               </tr>
1532               <tr>
1533                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
1534                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
1535                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
1536     . $locale->text('Ship via') . qq|</td>
1537               </tr>
1538               <tr>
1539                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
1540     . $locale->text('Amount') . qq|</td>
1541                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
1542     . $locale->text('Tax') . qq|</td>
1543                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
1544     . $locale->text('Total') . qq|</td>
1545               </tr>
1546               <tr>
1547                 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> |
1548     .             $locale->text('Ertrag') . qq|</td>
1549                 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> |
1550     .             $locale->text('Ertrag prozentual') . qq|</td>
1551               </tr>
1552               <tr>
1553           <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> |
1554           . $locale->text('Project Number') . qq|</td>
1555           <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> |
1556           . $locale->text('Transaction description') . qq|</td>
1557               </tr>
1558               <tr>
1559                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
1560     . $locale->text('Subtotal') . qq|</td>
1561               </tr>
1562             </table>
1563           </td>
1564         </tr>
1565       </table>
1566     </td>
1567   </tr>
1568   <tr><td colspan=4><hr size=3 noshade></td></tr>
1569 </table>
1570
1571 $jsscript
1572
1573 <br>
1574 <input type=hidden name=nextsub value=orders>
1575 <input type=hidden name=login value=$form->{login}>
1576 <input type=hidden name=password value=$form->{password}>
1577 <input type=hidden name=vc value=$form->{vc}>
1578 <input type=hidden name=type value=$form->{type}>
1579
1580 <input class=submit type=submit name=action value="|
1581     . $locale->text('Continue') . qq|">
1582 </form>
1583
1584 </body>
1585 </html>
1586 |;
1587
1588   $lxdebug->leave_sub();
1589 }
1590
1591 sub create_subtotal_row {
1592   $lxdebug->enter_sub();
1593
1594   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1595
1596   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1597
1598   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1599
1600   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1601
1602   map { $totals->{$_} = 0 } @{ $subtotal_columns };
1603
1604   $lxdebug->leave_sub();
1605
1606   return $row;
1607 }
1608
1609 sub orders {
1610   $lxdebug->enter_sub();
1611
1612   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
1613
1614   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
1615
1616   $form->{sort} ||= 'transdate';
1617
1618   OE->transactions(\%myconfig, \%$form);
1619
1620   $form->{rowcount} = scalar @{ $form->{OE} };
1621
1622   my @columns = (
1623     "transdate",               "reqdate",
1624     "id",                      $ordnumber,
1625     "name",                    "netamount",
1626     "tax",                     "amount",
1627     "curr",                    "employee",
1628     "shipvia",                 "globalprojectnumber",
1629     "transaction_description", "open",
1630     "delivered", "marge_total", "marge_percent"
1631   );
1632
1633   # only show checkboxes if gotten here via sales_order form.
1634   my $allow_multiple_orders = $form->{type} eq 'sales_order';
1635   if ($allow_multiple_orders) {
1636     unshift @columns, "ids";
1637   }
1638
1639   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
1640   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
1641
1642   my $attachment_basename;
1643   if ($form->{vc} eq 'vendor') {
1644     if ($form->{type} eq 'purchase_order') {
1645       $form->{title}       = $locale->text('Purchase Orders');
1646       $attachment_basename = $locale->text('purchase_order_list');
1647     } else {
1648       $form->{title}       = $locale->text('Request for Quotations');
1649       $attachment_basename = $locale->text('rfq_list');
1650     }
1651
1652   } else {
1653     if ($form->{type} eq 'sales_order') {
1654       $form->{title}       = $locale->text('Sales Orders');
1655       $attachment_basename = $locale->text('sales_order_list');
1656     } else {
1657       $form->{title}       = $locale->text('Quotations');
1658       $attachment_basename = $locale->text('quotation_list');
1659     }
1660   }
1661
1662   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1663
1664   my @hidden_variables = map { "l_${_}" } @columns;
1665   push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
1666                                                         transaction_description transdatefrom transdateto type vc employee_id);
1667
1668   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1669
1670   my %column_defs = (
1671     'ids'                     => { 'text' => '', },
1672     'transdate'               => { 'text' => $locale->text('Date'), },
1673     'reqdate'                 => { 'text' => $locale->text('Required by'), },
1674     'id'                      => { 'text' => $locale->text('ID'), },
1675     'ordnumber'               => { 'text' => $locale->text('Order'), },
1676     'quonumber'               => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
1677     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1678     'netamount'               => { 'text' => $locale->text('Amount'), },
1679     'tax'                     => { 'text' => $locale->text('Tax'), },
1680     'amount'                  => { 'text' => $locale->text('Total'), },
1681     'curr'                    => { 'text' => $locale->text('Curr'), },
1682     'employee'                => { 'text' => $locale->text('Salesperson'), },
1683     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
1684     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
1685     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1686     'open'                    => { 'text' => $locale->text('Open'), },
1687     'delivered'               => { 'text' => $locale->text('Delivered'), },
1688     'marge_total'                   => { 'text' => $locale->text('Ertrag'), },
1689     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), }
1690   );
1691
1692   foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee shipvia)) {
1693     $column_defs{$name}->{link} = $href . "&sort=$name";
1694   }
1695
1696   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
1697
1698   $form->{"l_type"} = "Y";
1699   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1700   $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
1701
1702   $report->set_columns(%column_defs);
1703   $report->set_column_order(@columns);
1704
1705   $report->set_export_options('orders', @hidden_variables);
1706
1707   $report->set_sort_indicator($form->{sort}, 1);
1708
1709   my @options;
1710   if ($form->{customer}) {
1711     push @options, $locale->text('Customer') . " : $form->{customer}";
1712   }
1713   if ($form->{vendor}) {
1714     push @options, $locale->text('Vendor') . " : $form->{vendor}";
1715   }
1716   if ($form->{department}) {
1717     ($department) = split /--/, $form->{department};
1718     push @options, $locale->text('Department') . " : $department";
1719   }
1720   if ($form->{ordnumber}) {
1721     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1722   }
1723   if ($form->{notes}) {
1724     push @options, $locale->text('Notes') . " : $form->{notes}";
1725   }
1726   if ($form->{transaction_description}) {
1727     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1728   }
1729   if ($form->{transdatefrom}) {
1730     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1731   }
1732   if ($form->{transdateto}) {
1733     push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
1734   }
1735   if ($form->{open}) {
1736     push @options, $locale->text('Open');
1737   }
1738   if ($form->{closed}) {
1739     push @options, $locale->text('Closed');
1740   }
1741   if ($form->{delivered}) {
1742     push @options, $locale->text('Delivered');
1743   }
1744   if ($form->{notdelivered}) {
1745     push @options, $locale->text('Not delivered');
1746   }
1747
1748   $report->set_options('top_info_text'        => join("\n", @options),
1749                        'raw_top_info_text'    => $form->parse_html_template('oe/orders_top'),
1750                        'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1751                        'output_format'        => 'HTML',
1752                        'title'                => $form->{title},
1753                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1754     );
1755   $report->set_options_from_form();
1756
1757   # add sort and escape callback, this one we use for the add sub
1758   $form->{callback} = $href .= "&sort=$form->{sort}";
1759
1760   # escape callback for href
1761   $callback = $form->escape($href);
1762
1763   my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1764
1765   my %totals    = map { $_ => 0 } @subtotal_columns;
1766   my %subtotals = map { $_ => 0 } @subtotal_columns;
1767
1768   my $idx = 0;
1769
1770   my $edit_url = build_std_url('action=edit', 'type', 'vc');
1771
1772   foreach $oe (@{ $form->{OE} }) {
1773     map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1774
1775     $oe->{tax}       = $oe->{amount} - $oe->{netamount};
1776     $oe->{open}      = $oe->{closed}    ? $locale->text('No')  : $locale->text('Yes');
1777     $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1778
1779     map { $subtotals{$_} += $oe->{$_};
1780           $totals{$_}    += $oe->{$_} } @subtotal_columns;
1781
1782     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1783     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
1784
1785     map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1786
1787     my $row = { };
1788
1789     foreach my $column (@columns) {
1790       next if ($column eq 'ids');
1791       $row->{$column} = {
1792         'data'  => $oe->{$column},
1793         'align' => $column_alignment{$column},
1794       };
1795     }
1796
1797     $row->{ids} = {
1798       'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1799                     . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1800       'valign'   => 'center',
1801       'align'    => 'center',
1802     };
1803
1804     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1805
1806     my $row_set = [ $row ];
1807
1808     if (($form->{l_subtotal} eq 'Y')
1809         && (($idx == (scalar @{ $form->{OE} } - 1))
1810             || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1811       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1812     }
1813
1814     $report->add_data($row_set);
1815
1816     $idx++;
1817   }
1818
1819   $report->add_separator();
1820   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1821
1822   $report->generate_with_headers();
1823
1824   $lxdebug->leave_sub();
1825 }
1826
1827 sub check_delivered_flag {
1828   $lxdebug->enter_sub();
1829
1830   if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1831     return $lxdebug->leave_sub();
1832   }
1833
1834   my $all_delivered = 0;
1835
1836   foreach my $i (1 .. $form->{rowcount}) {
1837     next if (!$form->{"id_$i"});
1838
1839     if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1840       $all_delivered = 1;
1841       next;
1842     }
1843
1844     $all_delivered = 0;
1845     last;
1846   }
1847
1848   $form->{delivered} = 1 if $all_delivered;
1849
1850   $lxdebug->leave_sub();
1851 }
1852
1853 sub save_and_close {
1854   $lxdebug->enter_sub();
1855
1856   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1857
1858   if ($form->{type} =~ /_order$/) {
1859     $form->isblank("transdate", $locale->text('Order Date missing!'));
1860   } else {
1861     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1862   }
1863
1864   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1865   $form->{$idx} =~ s/^\s*//g;
1866   $form->{$idx} =~ s/\s*$//g;
1867
1868   $msg = ucfirst $form->{vc};
1869   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1870
1871   # $locale->text('Customer missing!');
1872   # $locale->text('Vendor missing!');
1873
1874   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1875     if ($form->{currency} ne $form->{defaultcurrency});
1876
1877   &validate_items;
1878   
1879   if($form->{payment_id}) { 
1880     $payment_id = $form->{payment_id};
1881   }
1882   
1883   # if the name changed get new values
1884   if (&check_name($form->{vc})) {
1885     if($form->{payment_id} eq "") { 
1886       $form->{payment_id} = $payment_id;
1887     }
1888     &update;
1889     exit;
1890   }
1891
1892   $form->{id} = 0 if $form->{saveasnew};
1893
1894   # this is for the internal notes section for the [email] Subject
1895   if ($form->{type} =~ /_order$/) {
1896     if ($form->{type} eq 'sales_order') {
1897       $form->{label} = $locale->text('Sales Order');
1898
1899       $numberfld = "sonumber";
1900       $ordnumber = "ordnumber";
1901     } else {
1902       $form->{label} = $locale->text('Purchase Order');
1903
1904       $numberfld = "ponumber";
1905       $ordnumber = "ordnumber";
1906     }
1907
1908     $err = $locale->text('Cannot save order!');
1909
1910     check_delivered_flag();
1911
1912   } else {
1913     if ($form->{type} eq 'sales_quotation') {
1914       $form->{label} = $locale->text('Quotation');
1915
1916       $numberfld = "sqnumber";
1917       $ordnumber = "quonumber";
1918     } else {
1919       $form->{label} = $locale->text('Request for Quotation');
1920
1921       $numberfld = "rfqnumber";
1922       $ordnumber = "quonumber";
1923     }
1924
1925     $err = $locale->text('Cannot save quotation!');
1926
1927   }
1928
1929   # get new number in sequence if no number is given or if saveasnew was requested
1930   if (!$form->{$ordnumber} || $form->{saveasnew}) {
1931     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1932   }
1933
1934   relink_accounts();
1935
1936   $form->error($err) if (!OE->save(\%myconfig, \%$form));
1937
1938   # saving the history
1939   if(!exists $form->{addition}) {
1940     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1941         $form->{addition} = "SAVED";
1942         $form->save_history($form->dbconnect(\%myconfig));
1943   }
1944   # /saving the history
1945
1946   $form->redirect($form->{label} . " $form->{$ordnumber} " .
1947                   $locale->text('saved!'));
1948
1949   $lxdebug->leave_sub();
1950 }
1951
1952 sub save {
1953   $lxdebug->enter_sub();
1954
1955   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1956
1957
1958   if ($form->{type} =~ /_order$/) {
1959     $form->isblank("transdate", $locale->text('Order Date missing!'));
1960   } else {
1961     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1962   }
1963
1964   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1965   $form->{$idx} =~ s/^\s*//g;
1966   $form->{$idx} =~ s/\s*$//g;
1967
1968   $msg = ucfirst $form->{vc};
1969   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1970
1971   # $locale->text('Customer missing!');
1972   # $locale->text('Vendor missing!');
1973
1974   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1975     if ($form->{currency} ne $form->{defaultcurrency});
1976
1977   &validate_items;
1978   
1979   if($form->{payment_id}) { 
1980     $payment_id = $form->{payment_id};
1981   }
1982   
1983   # if the name changed get new values
1984   if (&check_name($form->{vc})) {
1985     if($form->{payment_id} eq "") { 
1986       $form->{payment_id} = $payment_id;
1987     }
1988     &update;
1989     exit;
1990   }
1991
1992   $form->{id} = 0 if $form->{saveasnew};
1993
1994   # this is for the internal notes section for the [email] Subject
1995   if ($form->{type} =~ /_order$/) {
1996     if ($form->{type} eq 'sales_order') {
1997       $form->{label} = $locale->text('Sales Order');
1998
1999       $numberfld = "sonumber";
2000       $ordnumber = "ordnumber";
2001     } else {
2002       $form->{label} = $locale->text('Purchase Order');
2003
2004       $numberfld = "ponumber";
2005       $ordnumber = "ordnumber";
2006     }
2007
2008     $err = $locale->text('Cannot save order!');
2009
2010     check_delivered_flag();
2011
2012   } else {
2013     if ($form->{type} eq 'sales_quotation') {
2014       $form->{label} = $locale->text('Quotation');
2015
2016       $numberfld = "sqnumber";
2017       $ordnumber = "quonumber";
2018     } else {
2019       $form->{label} = $locale->text('Request for Quotation');
2020
2021       $numberfld = "rfqnumber";
2022       $ordnumber = "quonumber";
2023     }
2024
2025     $err = $locale->text('Cannot save quotation!');
2026
2027   }
2028
2029   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
2030     unless $form->{$ordnumber};
2031
2032   relink_accounts();
2033
2034   OE->save(\%myconfig, \%$form);
2035
2036   # saving the history
2037   if(!exists $form->{addition}) {
2038     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2039         $form->{addition} = "SAVED";
2040         $form->save_history($form->dbconnect(\%myconfig));
2041   }
2042   # /saving the history 
2043
2044   $form->{simple_save} = 1;
2045   if(!$form->{print_and_save}) {
2046     set_headings("edit");
2047     &update;
2048     exit;
2049   }
2050   $lxdebug->leave_sub();
2051 }
2052
2053 sub delete {
2054   $lxdebug->enter_sub();
2055
2056   $form->header;
2057
2058   if ($form->{type} =~ /_order$/) {
2059     $msg       = $locale->text('Are you sure you want to delete Order Number');
2060     $ordnumber = 'ordnumber';
2061   } else {
2062     $msg = $locale->text('Are you sure you want to delete Quotation Number');
2063     $ordnumber = 'quonumber';
2064   }
2065
2066   print qq|
2067 <body>
2068
2069 <form method=post action=$form->{script}>
2070 |;
2071
2072   # delete action variable
2073   map { delete $form->{$_} } qw(action header);
2074
2075   foreach $key (keys %$form) {
2076     $form->{$key} =~ s/\"/&quot;/g;
2077     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2078   }
2079
2080   print qq|
2081 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
2082
2083 <h4>$msg $form->{$ordnumber}</h4>
2084 <p>
2085 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
2086 <input name=action class=submit type=submit value="|
2087     . $locale->text('Yes') . qq|">
2088 <button class=submit type=button onclick="history.back()">|
2089     . $locale->text('No') . qq|</button>
2090 </form>
2091
2092 </body>
2093 </html>
2094 |;
2095
2096   $lxdebug->leave_sub();
2097 }
2098
2099 sub delete_order_quotation {
2100   $lxdebug->enter_sub();
2101
2102   if ($form->{type} =~ /_order$/) {
2103     $msg = $locale->text('Order deleted!');
2104     $err = $locale->text('Cannot delete order!');
2105   } else {
2106     $msg = $locale->text('Quotation deleted!');
2107     $err = $locale->text('Cannot delete quotation!');
2108   }
2109   if (OE->delete(\%myconfig, \%$form, $spool)){
2110     # saving the history
2111     if(!exists $form->{addition}) {
2112       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2113           $form->{addition} = "DELETED";
2114           $form->save_history($form->dbconnect(\%myconfig));
2115     }
2116     # /saving the history 
2117     $form->info($msg);
2118     exit();
2119   }
2120   $form->error($err);
2121
2122   $lxdebug->leave_sub();
2123 }
2124
2125 sub invoice {
2126   $lxdebug->enter_sub();
2127
2128   $form->{old_employee_id} = $form->{employee_id};
2129   $form->{old_salesman_id} = $form->{salesman_id};
2130
2131   if ($form->{type} =~ /_order$/) {
2132
2133     # these checks only apply if the items don't bring their own ordnumbers/transdates.
2134     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
2135     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
2136       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2137     $form->isblank("transdate", $locale->text('Order Date missing!'))
2138       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
2139
2140     # also copy deliverydate from the order
2141     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
2142     $form->{orddate}      = $form->{transdate};
2143   } else {
2144     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
2145     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
2146     $form->{ordnumber}    = "";
2147     $form->{quodate}      = $form->{transdate};
2148   }
2149   
2150   $payment_id = $form->{payment_id} if $form->{payment_id};
2151   
2152   # if the name changed get new values
2153   if (&check_name($form->{vc})) {
2154     $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
2155     &update;
2156     exit;
2157   }
2158
2159   $form->{cp_id} *= 1;
2160
2161   for $i (1 .. $form->{rowcount}) {
2162     for (qw(ship qty sellprice listprice basefactor)) {
2163       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
2164     }
2165   }
2166
2167   if (   $form->{type} =~ /_order/
2168       && $form->{currency} ne $form->{defaultcurrency}) {
2169
2170     # check if we need a new exchangerate
2171     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
2172
2173     $orddate      = $form->current_date(\%myconfig);
2174     $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
2175
2176     if (!$exchangerate) {
2177       &backorder_exchangerate($orddate, $buysell);
2178       exit;
2179     }
2180   }
2181
2182   # close orders/quotations
2183   $form->{closed} = 1;
2184
2185   # save order if one ordnumber has been given
2186   # if not it's most likely a collective order, which can't be saved back
2187   # so they just have to be closed
2188   if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
2189     OE->close_order(\%myconfig, \%$form) if ($form->{id});
2190   } else {
2191     OE->close_orders(\%myconfig, \%$form);
2192   }
2193
2194   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
2195   $form->{duedate}   = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
2196
2197   $form->{id}     = '';
2198   $form->{closed} = 0;
2199   $form->{rowcount}--;
2200   $form->{shipto} = 1;
2201
2202   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
2203
2204   if ($form->{type} =~ /_order$/) {
2205     $form->{exchangerate} = $exchangerate;
2206     &create_backorder;
2207   }
2208
2209   if (   $form->{type} eq 'purchase_order'
2210       || $form->{type} eq 'request_quotation') {
2211     $form->{title}  = $locale->text('Add Vendor Invoice');
2212     $form->{script} = 'ir.pl';
2213     $script         = "ir";
2214     $buysell        = 'sell';
2215   }
2216
2217   if (   $form->{type} eq 'sales_order' 
2218       || $form->{type} eq 'sales_quotation') {
2219     $form->{title}  = $locale->text('Add Sales Invoice');
2220     $form->{script} = 'is.pl';
2221     $script         = "is";
2222     $buysell        = 'buy';
2223   }
2224
2225   # bo creates the id, reset it
2226   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
2227   $form->{ $form->{vc} } =~ s/--.*//g;
2228   $form->{type} = "invoice";
2229
2230   # locale messages
2231   $locale = new Locale "$myconfig{countrycode}", "$script";
2232
2233   require "bin/mozilla/$form->{script}";
2234
2235   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
2236
2237   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
2238     qw(creditlimit creditremaining);
2239
2240   $currency = $form->{currency};
2241   &invoice_links;
2242
2243   $form->{currency}     = $currency;
2244   $form->{exchangerate} = "";
2245   $form->{forex}        = "";
2246   $form->{exchangerate} = $exchangerate
2247     if (
2248         $form->{forex} = (
2249                     $exchangerate =
2250                       $form->check_exchangerate(
2251                       \%myconfig, $form->{currency}, $form->{invdate}, $buysell
2252                       )));
2253
2254   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
2255
2256   &prepare_invoice;
2257
2258   # format amounts
2259   for $i (1 .. $form->{rowcount}) {
2260     $form->{"discount_$i"} =
2261       $form->format_amount(\%myconfig, $form->{"discount_$i"});
2262
2263     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
2264     $dec           = length $dec;
2265     $decimalplaces = ($dec > 2) ? $dec : 2;
2266
2267     # copy delivery date from reqdate for order -> invoice conversion
2268     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
2269       unless $form->{"deliverydate_$i"};
2270
2271     $form->{"sellprice_$i"} =
2272       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
2273                            $decimalplaces);
2274
2275     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
2276     $dec_qty = length $dec_qty;
2277     $form->{"qty_$i"} =
2278       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
2279
2280     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2281       qw(partnumber description unit);
2282
2283   }
2284
2285   &display_form;
2286
2287   $lxdebug->leave_sub();
2288 }
2289
2290 sub backorder_exchangerate {
2291   $lxdebug->enter_sub();
2292   my ($orddate, $buysell) = @_;
2293
2294   $form->header;
2295
2296   print qq|
2297 <body>
2298
2299 <form method=post action=$form->{script}>
2300 |;
2301
2302   # delete action variable
2303   map { delete $form->{$_} } qw(action header exchangerate);
2304
2305   foreach $key (keys %$form) {
2306     $form->{$key} =~ s/\"/&quot;/g;
2307     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
2308   }
2309
2310   $form->{title} = $locale->text('Add Exchangerate');
2311
2312   print qq|
2313
2314 <input type=hidden name=login value=$form->{login}>
2315 <input type=hidden name=password value=$form->{password}>
2316
2317 <input type=hidden name=exchangeratedate value=$orddate>
2318 <input type=hidden name=buysell value=$buysell>
2319
2320 <table width=100%>
2321   <tr><th class=listtop>$form->{title}</th></tr>
2322   <tr height="5"></tr>
2323   <tr>
2324     <td>
2325       <table>
2326         <tr>
2327           <th align=right>| . $locale->text('Currency') . qq|</th>
2328           <td>$form->{currency}</td>
2329         </tr>
2330         <tr>
2331           <th align=right>| . $locale->text('Date') . qq|</th>
2332           <td>$orddate</td>
2333         </tr>
2334         <tr>
2335           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
2336           <td><input name=exchangerate size=11></td>
2337         </tr>
2338       </table>
2339     </td>
2340   </tr>
2341 </table>
2342
2343 <hr size=3 noshade>
2344
2345 <br>
2346 <input type=hidden name=nextsub value=save_exchangerate>
2347
2348 <input name=action class=submit type=submit value="|
2349     . $locale->text('Continue') . qq|">
2350
2351 </form>
2352
2353 </body>
2354 </html>
2355 |;
2356
2357   $lxdebug->leave_sub();
2358 }
2359
2360 sub save_exchangerate {
2361   $lxdebug->enter_sub();
2362
2363   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
2364   $form->{exchangerate} =
2365     $form->parse_amount(\%myconfig, $form->{exchangerate});
2366   $form->save_exchangerate(\%myconfig, $form->{currency},
2367                            $form->{exchangeratedate},
2368                            $form->{exchangerate}, $form->{buysell});
2369
2370   &invoice;
2371
2372   $lxdebug->leave_sub();
2373 }
2374
2375 sub create_backorder {
2376   $lxdebug->enter_sub();
2377
2378   $form->{shipped} = 1;
2379
2380   # figure out if we need to create a backorder
2381   # items aren't saved if qty != 0
2382
2383   for $i (1 .. $form->{rowcount}) {
2384     $totalqty  += $qty  = $form->{"qty_$i"};
2385     $totalship += $ship = $form->{"ship_$i"};
2386
2387     $form->{"qty_$i"} = $qty - $ship;
2388   }
2389
2390   if ($totalship == 0) {
2391     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
2392     $form->{ordtotal} = 0;
2393     $form->{shipped}  = 0;
2394     return;
2395   }
2396
2397   if ($totalqty == $totalship) {
2398     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
2399     $form->{ordtotal} = 0;
2400     return;
2401   }
2402
2403   @flds = (
2404     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
2405   );
2406
2407   for $i (1 .. $form->{rowcount}) {
2408     map {
2409       $form->{"${_}_$i"} =
2410         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
2411     } qw(sellprice discount);
2412   }
2413
2414   relink_accounts();
2415
2416   OE->save(\%myconfig, \%$form);
2417
2418   # rebuild rows for invoice
2419   @a     = ();
2420   $count = 0;
2421
2422   for $i (1 .. $form->{rowcount}) {
2423     $form->{"qty_$i"} = $form->{"ship_$i"};
2424
2425     if ($form->{"qty_$i"}) {
2426       push @a, {};
2427       $j = $#a;
2428       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
2429       $count++;
2430     }
2431   }
2432
2433   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
2434   $form->{rowcount} = $count;
2435
2436   $lxdebug->leave_sub();
2437 }
2438
2439 sub save_as_new {
2440   $lxdebug->enter_sub();
2441
2442   $form->{saveasnew} = 1;
2443   $form->{closed}    = 0;
2444   map { delete $form->{$_} } qw(printed emailed queued);
2445
2446   # Let Lx-Office assign a new order number if the user hasn't changed the
2447   # previous one. If it has been changed manually then use it as-is.
2448   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
2449   $form->{$idx} =~ s/^\s*//g;
2450   $form->{$idx} =~ s/\s*$//g;
2451   if ($form->{saved_xyznumber} &&
2452       ($form->{saved_xyznumber} eq $form->{$idx})) {
2453     delete($form->{$idx});
2454   }
2455
2456   &save;
2457
2458   $lxdebug->leave_sub();
2459 }
2460
2461 sub check_for_direct_delivery_yes {
2462   $lxdebug->enter_sub();
2463
2464   $form->{direct_delivery_checked} = 1;
2465   delete @{$form}{grep /^shipto/, keys %{ $form }};
2466   map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
2467   $form->{shipto} = 1;
2468   purchase_order();
2469   $lxdebug->leave_sub();
2470 }
2471
2472 sub check_for_direct_delivery_no {
2473   $lxdebug->enter_sub();
2474
2475   $form->{direct_delivery_checked} = 1;
2476   delete @{$form}{grep /^shipto/, keys %{ $form }};
2477   purchase_order();
2478
2479   $lxdebug->leave_sub();
2480 }
2481
2482 sub check_for_direct_delivery {
2483   $lxdebug->enter_sub();
2484
2485   if ($form->{direct_delivery_checked}
2486       || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
2487     $lxdebug->leave_sub();
2488     return;
2489   }
2490
2491   if ($form->{shipto_id}) {
2492     Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
2493
2494   } else {
2495     map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
2496   }
2497
2498   delete $form->{action};
2499   $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ref $_ eq "" } keys %{ $form } ];
2500
2501   $form->header();
2502   print $form->parse_html_template("oe/check_for_direct_delivery");
2503
2504   $lxdebug->leave_sub();
2505
2506   exit 0;
2507 }
2508
2509 sub purchase_order {
2510   $lxdebug->enter_sub();
2511
2512   if ($form->{type} eq 'sales_order') {
2513     check_for_direct_delivery();
2514   }
2515
2516   if (   $form->{type} eq 'sales_quotation'
2517       || $form->{type} eq 'request_quotation') {
2518     OE->close_order(\%myconfig, \%$form);
2519   }
2520
2521   if ($form->{type} =~ /^sales_/) {
2522     delete($form->{ordnumber});
2523   }
2524
2525   $form->{cp_id} *= 1;
2526
2527   $form->{title} = $locale->text('Add Purchase Order');
2528   $form->{vc}    = "vendor";
2529   $form->{type}  = "purchase_order";
2530
2531   &poso;
2532
2533   $lxdebug->leave_sub();
2534 }
2535
2536 sub sales_order {
2537   $lxdebug->enter_sub();
2538
2539   if (   $form->{type} eq 'sales_quotation'
2540       || $form->{type} eq 'request_quotation') {
2541     OE->close_order(\%myconfig, $form);
2542   }
2543
2544   if ($form->{type} eq "purchase_order") {
2545     delete($form->{ordnumber});
2546   }
2547
2548   $form->{cp_id} *= 1;
2549
2550   $form->{title} = $locale->text('Add Sales Order');
2551   $form->{vc}    = "customer";
2552   $form->{type}  = "sales_order";
2553
2554   &poso;
2555
2556   $lxdebug->leave_sub();
2557 }
2558
2559 sub poso {
2560   $lxdebug->enter_sub();
2561
2562   $form->{transdate} = $form->current_date(\%myconfig);
2563   delete $form->{duedate};
2564
2565   $form->{closed} = 0;
2566
2567   $form->{old_employee_id} = $form->{employee_id};
2568   $form->{old_salesman_id} = $form->{salesman_id};
2569
2570   # reset
2571   map { delete $form->{$_} }
2572     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
2573
2574   for $i (1 .. $form->{rowcount}) {
2575     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
2576                                                      $form->{"${_}_${i}"})
2577             if ($form->{"${_}_${i}"}) }
2578         qw(ship qty sellprice listprice basefactor));
2579   }
2580
2581   &order_links;
2582
2583   &prepare_order;
2584
2585   # format amounts
2586   for $i (1 .. $form->{rowcount} - 1) {
2587     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
2588       qw(partnumber description unit);
2589   }
2590
2591   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
2592     qw(creditlimit creditremaining);
2593
2594   &update;
2595
2596   $lxdebug->leave_sub();
2597 }
2598
2599 sub e_mail {
2600   $lxdebug->enter_sub();
2601
2602   $form->{print_and_save} = 1;
2603
2604   $print_post = 1;
2605
2606   my $saved_form = save_form();
2607
2608   save();
2609
2610   my %saved_vars;
2611   map({ $saved_vars{$_} = $form->{$_}; } qw(id ordnumber quonumber));
2612   restore_form($saved_form);
2613   map({ $form->{$_} = $saved_vars{$_}; } qw(id ordnumber quonumber));
2614
2615   edit_e_mail();
2616
2617   $lxdebug->leave_sub();
2618 }
2619
2620 sub yes {
2621   call_sub($form->{yes_nextsub});
2622 }
2623
2624 sub no {
2625   call_sub($form->{no_nextsub});
2626 }