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