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