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