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