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