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