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