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