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