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