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