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