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