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