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