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