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