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