This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / oe.pl
1 #=====================================================================
2 # SQL-Ledger, Accounting
3 # Copyright (c) 1998-2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # Order entry module
25 #
26 #======================================================================
27
28
29 use SL::OE;
30 use SL::IR;
31 use SL::IS;
32 use SL::PE;
33
34 require "$form->{path}/io.pl";
35 require "$form->{path}/arap.pl";
36
37 1;
38 # end of main
39
40
41 sub add {
42
43   if ($form->{type} eq 'purchase_order') {
44     $form->{title} = $locale->text('Add Purchase Order');
45     $form->{vc} = 'vendor';
46   }
47   if ($form->{type} eq 'sales_order') {
48     $form->{title} = $locale->text('Add Sales Order');
49     $form->{vc} = 'customer';
50   }
51   
52   &order_links;
53   &prepare_order;
54   &display_form;
55
56 }
57
58
59 sub edit {
60
61   if ($form->{type} eq 'purchase_order') {
62     $form->{title} = $locale->text('Edit Purchase Order');
63     $form->{vc} = 'vendor';
64   }
65   if ($form->{type} eq 'sales_order') {
66     $form->{title} = $locale->text('Edit Sales Order');
67     $form->{vc} = 'customer';
68   }
69   
70   &order_links;
71   &prepare_order;
72
73   &display_form;
74
75 }
76
77
78 sub order_links {
79
80   # get vendors / customers
81   $form->all_vc(\%myconfig, $form->{vc});
82
83   # retrieve order
84   OE->retrieve_order(\%myconfig, \%$form);
85   
86   $taxincluded = $form->{taxincluded};
87   $form->{shipto} = 1 if $form->{id};
88
89   # get customer / vendor
90   if ($form->{type} eq 'purchase_order') {
91     IR->get_vendor(\%myconfig, \%$form);
92   }
93   if ($form->{type} eq 'sales_order') {
94     IS->get_customer(\%myconfig, \%$form);
95   }
96
97   ($form->{$form->{vc}}) = split /--/, $form->{$form->{vc}};
98   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
99
100   # build the popup menus
101   if (@{ $form->{"all_$form->{vc}"} }) {
102     $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
103     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{"all_$form->{vc}"} });
104   }
105   
106   # currencies
107   @curr = split /:/, $form->{currencies};
108   chomp $curr[0];
109   $form->{defaultcurrency} = $curr[0];
110   
111   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
112
113   $form->{taxincluded} = $taxincluded if ($form->{id});
114
115   # forex
116   $form->{forex} = $form->{exchangerate};
117   
118 }
119
120
121 sub prepare_order {
122
123   $form->{format} = "html";
124   $form->{media} = "screen";
125   
126   if ($form->{id}) {
127     
128     map { $form->{$_} =~ s/"/&quot;/g } qw(invnumber shippingpoint notes shiptoname shiptoaddr1 shiptoaddr2 shiptoaddr3 shiptoaddr4 shiptocontact);
129     
130     foreach $ref (@{ $form->{order_details} } ) {
131       $i++;
132       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
133       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
134
135       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
136       $dec = length $dec;
137       $decimalplaces = ($dec > 2) ? $dec : 2;
138       
139       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
140       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
141       
142       map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
143       $form->{rowcount} = $i;
144     }
145   }
146
147 }
148
149
150 sub form_header {
151
152   $checkedopen = ($form->{closed}) ? "" : "checked";
153   $checkedclosed = ($form->{closed}) ? "checked" : "";
154
155   if ($form->{id}) {
156     $openclosed = qq|
157       <tr>
158         <td colspan=2 align=center>
159           <table>
160             <tr>
161               <th nowrap><input name=closed type=radio class=radio value=0 $checkedopen> |.$locale->text('Open').qq|</th>
162               <th nowrap><input name=closed type=radio class=radio value=1 $checkedclosed> |.$locale->text('Closed').qq|</th>
163             </tr>
164           </table>
165         </td>
166       </tr>
167 |;
168   }
169
170   # set option selected
171   foreach $item ($form->{vc}, currency) {
172     $form->{"select$item"} =~ s/ selected//;
173     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
174   }
175     
176   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
177
178   $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
179   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
180   
181   $exchangerate = qq|
182 <input type=hidden name=forex value=$form->{forex}>
183 |;
184
185   if ($form->{currency} ne $form->{defaultcurrency}) {
186     if ($form->{forex}) {
187       $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td>$form->{exchangerate}</td>
188       <input type=hidden name=exchangerate value=$form->{exchangerate}>
189 |;
190     } else {
191       $exchangerate .= qq|<th align=right>|.$locale->text('Exchangerate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
192     }
193   }
194
195
196   $vclabel = ucfirst $form->{vc};
197   $vclabel = $locale->text($vclabel);
198
199   if ($form->{type} eq 'sales_order') {
200     
201     $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
202     
203     $creditremaining = qq|
204               <tr>
205                 <td></td>
206                 <td colspan=3>
207                   <table width=100%>
208                     <tr>
209                       <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
210                       <td>$form->{creditlimit}</td>
211                       <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
212                       <td class="plus$n">$form->{creditremaining}</td>
213                     </tr>
214                   </table>
215                 </td>
216               </tr>
217 |;
218   }
219
220   $vc = ($form->{"select$form->{vc}"}) ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">| : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
221
222
223   $form->header;
224   
225   print qq|
226 <body>
227
228 <form method=post action=$form->{script}>
229
230 <input type=hidden name=id value=$form->{id}>
231
232 <input type=hidden name=type value=$form->{type}>
233 <input type=hidden name=media value=$form->{media}>
234 <input type=hidden name=format value=$form->{format}>
235
236 <input type=hidden name=vc value=$form->{vc}>
237 <input type=hidden name=employee value="$form->{employee}">
238
239 <input type=hidden name=title value="$form->{title}">
240
241 <input type=hidden name=discount value=$form->{discount}>
242 <input type=hidden name=creditlimit value=$form->{creditlimit}>
243 <input type=hidden name=creditremaining value=$form->{creditremaining}>
244
245 <table width=100%>
246   <tr>
247     <th class=listtop>$form->{title}</th>
248   </tr>
249   <tr height="5"></tr>
250   <tr>
251     <td>
252       <table width="100%">
253         <tr valign=top>
254           <td>
255             <table>
256               <tr>
257                 <th align=right>$vclabel</th>
258                 <td colspan=3>$vc</td>
259                 <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
260                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
261               </tr>
262               $creditremaining
263               <tr>
264                 <th align=right>|.$locale->text('Currency').qq|</th>
265                 <td><select name=currency>$form->{selectcurrency}</select></td>
266                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
267                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
268                 $exchangerate
269               </tr>
270               <tr>
271                 <th align=right>|.$locale->text('Ship via').qq|</th>
272                 <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
273               </tr>
274             </table>
275           </td>
276           <td align=right>
277             <table width=100%>
278               $openclosed
279               <tr>
280                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
281                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
282               </tr>
283               <tr>
284                 <th align=right nowrap>|.$locale->text('Order Date').qq|</th>
285                 <td><input name=orddate size=11 title="$myconfig{dateformat}" value=$form->{orddate}></td>
286               </tr>
287               <tr>
288                 <th align=right nowrap=true>|.$locale->text('Required by').qq|</th>
289                 <td><input name=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>
290               </tr>
291               <tr>
292                 <th align=right nowrap>|.$locale->text('Terms: Net').qq|</th>
293                 <td nowrap=true><input name=terms size="3" maxlength="3" value=$form->{terms}> |.$locale->text('days').qq|</td>
294               </tr>
295             </table>
296           </td>
297         </tr>
298       </table>
299     </td>
300   </tr>
301
302 <!-- shipto are in hidden variables -->
303
304 <input type=hidden name=shiptoname value="$form->{shiptoname}">
305 <input type=hidden name=shiptoaddr1 value="$form->{shiptoaddr1}">
306 <input type=hidden name=shiptoaddr2 value="$form->{shiptoaddr2}">
307 <input type=hidden name=shiptoaddr3 value="$form->{shiptoaddr3}">
308 <input type=hidden name=shiptoaddr4 value="$form->{shiptoaddr4}">
309 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
310 <input type=hidden name=shiptophone value="$form->{shiptophone}">
311 <input type=hidden name=shiptofax value="$form->{shiptofax}">
312 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
313
314 <!-- email variables -->
315 <input type=hidden name=message value="$form->{message}"> 
316 <input type=hidden name=email value="$form->{email}">
317 <input type=hidden name=subject value="$form->{subject}">
318 <input type=hidden name=cc value="$form->{cc}">
319 <input type=hidden name=bcc value="$form->{bcc}">
320
321 <input type=hidden name=taxpart value="$form->{taxpart}">
322 <input type=hidden name=taxservice value="$form->{taxservice}">
323
324 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
325 |;
326
327   foreach $item (split / /, $form->{taxaccounts}) {
328     print qq|
329 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
330 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
331 |;
332   }
333
334 }
335
336
337 sub form_footer {
338
339   $form->{invtotal} = $form->{invsubtotal};
340
341   if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
342     $rows = 2;
343   }
344   $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
345
346   $taxincluded = "";
347   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
348   if ($form->{taxaccounts}) {
349     $taxincluded = qq|
350                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
351 |;
352   }
353
354   if (!$form->{taxincluded}) {
355     
356     foreach $item (split / /, $form->{taxaccounts}) {
357       if ($form->{"${item}_base"}) {
358         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
359         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
360         
361         $tax .= qq|
362               <tr>
363                 <th align=right>$form->{"${item}_description"}</th>
364                 <td align=right>$form->{"${item}_total"}</td>
365               </tr>
366 |;
367       }
368     }
369
370     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
371     
372     $subtotal = qq|
373               <tr>
374                 <th align=right>|.$locale->text('Subtotal').qq|</th>
375                 <td align=right>$form->{invsubtotal}</td>
376               </tr>
377 |;
378
379   }
380
381   $form->{oldinvtotal} = $form->{invtotal};
382   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
383
384   print qq|
385   <tr>
386     <td>
387       <table width=100%>
388         <tr valign=bottom>
389           <td>
390             <table>
391               <tr>
392                 <th align=left>|.$locale->text('Notes').qq|</th>
393               </tr>
394               <tr>
395                 <td>$notes</td>
396               </tr>
397             </table>
398           </td>
399           <td align=right width=100%>
400             $taxincluded
401             <table width=100%>
402               $subtotal
403               $tax
404               <tr>
405                 <th align=right>|.$locale->text('Total').qq|</th>
406                 <td align=right>$form->{invtotal}</td>
407               </tr>
408             </table>
409           </td>
410         </tr>
411       </table>
412     </td>
413   </tr>
414 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
415 <input type=hidden name=oldtotalpaid value=$totalpaid>
416   <tr>
417     <td>
418 |;
419
420   &print_options;
421
422   print qq|
423     </td>
424   </tr>
425   <tr>
426     <td><hr size=3 noshade></td>
427   </tr>
428 </table>
429
430 <br>
431 <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
432 <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
433 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
434 <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
435 <input class=submit type=submit name=action value="|.$locale->text('Save').qq|">
436 |;
437
438     if ($form->{id}) {
439       print qq|
440 <input class=submit type=submit name=action value="|.$locale->text('Save as new').qq|">
441 <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
442 <input class=submit type=submit name=action value="|.$locale->text('Invoice').qq|">
443 |;
444     }
445
446   print qq| 
447
448 <input type=hidden name=rowcount value=$form->{rowcount}>
449
450 <input name=callback type=hidden value="$form->{callback}">
451
452 <input type=hidden name=path value=$form->{path}>
453 <input type=hidden name=login value=$form->{login}>
454 <input type=hidden name=password value=$form->{password}>
455
456 </form>
457
458 </body>
459 </html>
460 |;
461
462 }
463
464
465 sub update {
466
467   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
468
469   &check_name($form->{vc});
470
471   &check_project;
472
473   $buysell = 'buy';
474   $buysell = 'sell' if ($form->{vc} eq 'vendor');
475   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{orddate}, $buysell)));
476   
477
478   my $i = $form->{rowcount};
479   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
480
481   if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
482
483     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
484     &check_form;
485
486   } else {
487    
488     if ($form->{type} eq 'purchase_order') {
489       IR->retrieve_item(\%myconfig, \%$form);
490     }
491     if ($form->{type} eq 'sales_order') {
492       IS->retrieve_item(\%myconfig, \%$form);
493     }
494
495     my $rows = scalar @{ $form->{item_list} };
496     
497     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
498
499     if ($rows) {
500       $form->{"qty_$i"}                     = 1 unless ($form->{"qty_$i"});
501       
502       if ($rows > 1) {
503         
504         &select_item;
505         exit;
506         
507       } else {
508         
509         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
510         
511         map { $form->{item_list}[$i]{$_} =~ s/"/&quot;/g } qw(partnumber description unit);
512         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
513
514         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
515         
516         ($dec) = ($s =~ /\.(\d+)/);
517         $dec = length $dec;
518         $decimalplaces = ($dec > 2) ? $dec : 2;
519
520         if ($sellprice) {
521           $form->{"sellprice_$i"} = $sellprice;
522         } else {
523           # if there is an exchange rate adjust sellprice
524           $form->{"sellprice_$i"} /= $exchangerate;
525         }
526         
527         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
528         map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
529         map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
530         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
531         
532         $form->{creditremaining} -= $amount;
533         
534         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
535         $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
536       }
537
538       &display_form;
539
540     } else {
541       # ok, so this is a new part
542       # ask if it is a part or service item
543       
544       if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
545         $form->{rowcount}--;
546         $form->{"discount_$i"} = "";
547         &display_form;
548       } else {
549         
550         $form->{"id_$i"}                = 0;
551         $form->{"unit_$i"}      = $locale->text('ea');
552
553         &new_item;
554
555       }
556     }
557   }
558 }
559
560
561
562 sub search {
563   
564   if ($form->{type} eq 'purchase_order') {
565     $form->{title} = $locale->text('Purchase Orders');
566     $form->{vc} = 'vendor';
567   }
568   if ($form->{type} eq 'sales_order') {
569     $form->{title} = $locale->text('Sales Orders');
570     $form->{vc} = 'customer';
571   }
572
573   # setup vendor / customer selection
574   $form->all_vc(\%myconfig, "$form->{vc}");
575
576   map { $vc .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{"all_$form->{vc}"} };
577
578   $vclabel = ucfirst $form->{vc};
579   $vclabel = $locale->text($vclabel);
580   
581 # $locale->text('Vendor')
582 # $locale->text('Customer')
583   
584   $vc = ($vc) ? qq|<select name=$form->{vc}><option>\n$vc</select>| : qq|<input name=$form->{vc} size=35>|;
585
586   $form->header;
587
588   print qq|
589 <body>
590
591 <form method=post action=$form->{script}>
592
593 <table width=100%>
594   <tr>
595     <th class=listtop>$form->{title}</th>
596   </tr>
597   <tr height="5"></tr>
598   <tr>
599     <td>
600       <table>
601         <tr>
602           <th align=right>$vclabel</th>
603           <td colspan=3>$vc</td>
604         </tr>
605         <tr>
606           <th align=right>|.$locale->text('Order Number').qq|</th>
607           <td colspan=3><input name=ordnumber size=20></td>
608         </tr>
609         <tr>
610           <th align=right>|.$locale->text('From').qq|</th>
611           <td><input name=transdatefrom size=11 title="$myconfig{dateformat}"></td>
612           <th align=right>|.$locale->text('to').qq|</th>
613           <td><input name=transdateto size=11 title="$myconfig{dateformat}"></td>
614         </tr>
615         <input type=hidden name=sort value=transdate>
616         <tr>
617           <th align=right>|.$locale->text('Include in Report').qq|</th>
618           <td colspan=3>
619             <table>
620               <tr>
621                 <td><input name="open" class=checkbox type=checkbox value=1 checked> |.$locale->text('Open').qq|</td>
622                 <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |.$locale->text('Closed').qq|</td>
623               </tr>
624               <tr>
625                 <td><input name="l_id" class=checkbox type=checkbox value=Y> |.$locale->text('ID').qq|</td>
626                 <td><input name="l_ordnumber" class=checkbox type=checkbox value=Y checked> |.$locale->text('Order Number').qq|</td>
627                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |.$locale->text('Order Date').qq|</td>
628                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |.$locale->text('Required by').qq|</td>
629               </tr>
630               <tr>
631                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
632                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |.$locale->text('Amount').qq|</td>
633                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> |.$locale->text('Tax').qq|</td>
634                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |.$locale->text('Total').qq|</td>
635               </tr>
636               <tr>
637                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |.$locale->text('Subtotal').qq|</td>
638               </tr>
639             </table>
640           </td>
641         </tr>
642       </table>
643     </td>
644   </tr>
645   <tr><td colspan=4><hr size=3 noshade></td></tr>
646 </table>
647
648 <br>
649 <input type=hidden name=nextsub value=orders>
650 <input type=hidden name=path value=$form->{path}>
651 <input type=hidden name=login value=$form->{login}>
652 <input type=hidden name=password value=$form->{password}>
653 <input type=hidden name=vc value=$form->{vc}>
654 <input type=hidden name=type value=$form->{type}>
655
656 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
657 </form>
658
659 </body>
660 </html>
661 |;
662
663 }
664
665
666 sub orders {
667
668   $name = $form->{$form->{vc}};
669   
670   # split vendor / customer
671   ($form->{$form->{vc}}, $form->{"$form->{vc}_id"}) = split(/--/, $form->{$form->{vc}});
672
673   OE->transactions(\%myconfig, \%$form);
674
675   # construct href
676   $href = "$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=" . $form->escape($form->{ordnumber}) . "&$form->{vc}=" . $form->escape($form->{$form->{vc}});
677
678   # construct callback
679   $name =~ s/&/_/g;
680   $callback = "$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=$form->{ordnumber}&$form->{vc}=$name";
681
682   @columns = $form->sort_columns(qw(transdate reqdate id ordnumber name netamount tax amount curr open closed));
683
684   $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed}) ;
685
686   foreach $item (@columns) {
687     if ($form->{"l_$item"} eq "Y") {
688       push @column_index, $item;
689       
690       # add column to href and callback
691       $callback .= "&l_$item=Y";
692       $href .= "&l_$item=Y";
693     }
694   }
695   
696   if ($form->{l_subtotal} eq 'Y') {
697     $callback .= "&l_subtotal=Y";
698     $href .= "&l_subtotal=Y";
699   }
700  
701  
702   if ($form->{vc} eq 'vendor') {
703     $form->{title} = $locale->text('Purchase Orders');
704     $name = $locale->text('Vendor');
705   }
706   if ($form->{vc} eq 'customer') {
707     $form->{title} = $locale->text('Sales Orders');
708     $name = $locale->text('Customer');
709   }
710  
711   $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>|.$locale->text('ID').qq|</a></th>|;
712   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
713   $column_header{reqdate} = qq|<th><a class=listheading href=$href&sort=reqdate>|.$locale->text('Required by').qq|</a></th>|;
714   $column_header{ordnumber} = qq|<th><a class=listheading href=$href&sort=ordnumber>|.$locale->text('Order').qq|</a></th>|;
715   $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
716   $column_header{netamount} = qq|<th class=listheading>|.$locale->text('Amount').qq|</th>|;
717   $column_header{tax} = qq|<th class=listheading>|.$locale->text('Tax').qq|</th>|;
718   $column_header{amount} = qq|<th class=listheading>|.$locale->text('Total').qq|</th>|;
719   $column_header{curr} = qq|<th class=listheading>|.$locale->text('Curr').qq|</th>|;
720   $column_header{open} = qq|<th class=listheading>|.$locale->text('O').qq|</th>|;
721   $column_header{closed} = qq|<th class=listheading>|.$locale->text('C').qq|</th>|;
722
723   if ($form->{open}) {
724     $option = $locale->text('Open');
725   }
726   if ($form->{closed}) {
727     $option .= " : " if $form->{open};
728     $option .= $locale->text('Closed');
729   }
730   if ($form->{$form->{vc}}) {
731     $option .= "\n<br>";
732     $option .= ucfirst $form->{vc};
733     $option .= " : $form->{$form->{vc}}";
734   }
735   if ($form->{transdatefrom}) {
736     $option .= "\n<br>".$locale->text('From')." ".$locale->date(\%myconfig, $form->{transdatefrom}, 1);
737   }
738   if ($form->{transdateto}) {
739     $option .= "\n<br>".$locale->text('to')." ".$locale->date(\%myconfig, $form->{transdateto}, 1);
740   }
741   
742   $form->header;
743
744   $colspan = $#column_index + 1;
745
746   print qq|
747 <body>
748
749 <table width=100%>
750   <tr><th class=listtop colspan=$colspan>$form->{title}</th></tr>
751   <tr height="5"></tr>
752   <tr>
753     <td colspan=$colspan>$option</td>
754   </tr>
755   <tr class=listheading>|;
756
757 map { print "\n$column_header{$_}" } @column_index;
758
759 print qq|
760   </tr>
761 |;
762
763   # add sort and escape callback
764   $callback = $form->escape($callback . "&sort=$form->{sort}");
765
766   if (@{ $form->{OE} }) {
767     $sameitem = $form->{OE}->[0]->{$form->{sort}};
768   }
769
770   foreach $oe (@{ $form->{OE} }) {
771
772     if ($form->{l_subtotal} eq 'Y') {
773       if ($sameitem ne $oe->{$form->{sort}}) {
774         &subtotal;
775         $sameitem = $oe->{$form->{sort}};
776       }
777     }
778     
779     map { $oe->{$_} *= $oe->{exchangerate} } (qw(netamount amount));
780     
781     $column_data{netamount} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{netamount}, 2, "&nbsp;")."</td>";
782     $column_data{tax} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{amount} - $oe->{netamount}, 2, "&nbsp;")."</td>";
783     $column_data{amount} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{amount}, 2, "&nbsp;")."</td>";
784
785     $totalnetamount += $oe->{netamount};
786     $totalamount += $oe->{amount};
787
788     $subtotalnetamount += $oe->{netamount};
789     $subtotalamount += $oe->{amount};
790
791     $column_data{id} = "<td>$oe->{id}</td>";
792     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
793     $column_data{reqdate} = "<td>$oe->{reqdate}&nbsp;</td>";
794
795     $column_data{ordnumber} = "<td><a href=oe.pl?path=$form->{path}&action=edit&type=$form->{type}&id=$oe->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$oe->{ordnumber}</a></td>";
796     $column_data{name} = "<td>$oe->{name}</td>";
797
798     if ($oe->{closed}) {
799       $column_data{closed} = "<td align=center>X</td>";
800       $column_data{open} = "<td>&nbsp;</td>";
801     } else {
802       $column_data{closed} = "<td>&nbsp;</td>";
803       $column_data{open} = "<td align=center>X</td>";
804     }
805
806     $i++; $i %= 2;
807     print "<tr class=listrow$i>";
808     
809     map { print "\n$column_data{$_}" } @column_index;
810
811     print qq|
812     </tr>
813 |;
814
815   }
816   
817   if ($form->{l_subtotal} eq 'Y') {
818     &subtotal;
819   }
820   
821   # print totals
822   print qq|<tr class=listtotal>|;
823   
824   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
825   
826   $column_data{netamount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;")."</th>";
827   $column_data{tax} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalnetamount, 2, "&nbsp;")."</th>";
828   $column_data{amount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")."</th>";
829
830   map { print "\n$column_data{$_}" } @column_index;
831
832   print qq|
833   </tr>
834   <tr><td colspan=8><hr size=3 noshade></td></tr>
835 </table>
836
837 <br>
838 <form method=post action=$form->{script}>
839
840 <input name=callback type=hidden value="$form->{callback}">
841
842 <input type=hidden name=type value=$form->{type}>
843 <input type=hidden name=vc value=$form->{vc}>
844
845 <input type=hidden name=path value=$form->{path}>
846 <input type=hidden name=login value=$form->{login}>
847 <input type=hidden name=password value=$form->{password}>
848
849 <input class=submit type=submit name=action value=|.$locale->text('Add').qq|
850
851 </form>
852
853 </body>
854 </html>
855 |;
856
857 }
858
859
860
861 sub subtotal {
862
863   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
864   
865   $column_data{netamount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")."</th>";
866   $column_data{tax} = "<td class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount, 2, "&nbsp;")."</th>";
867   $column_data{amount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;")."</th>";
868
869   $subtotalnetamount = 0;
870   $subtotalamount = 0;
871
872   print "<tr class=listsubtotal>";
873   
874   map { print "\n$column_data{$_}" } @column_index;
875
876   print qq|
877   </tr>
878 |;
879
880 }
881
882
883 sub save {
884
885   $form->isblank("ordnumber", $locale->text('Order Number missing!'));
886   $form->isblank("orddate", $locale->text('Order Date missing!'));
887   
888   $msg = ucfirst $form->{vc};
889   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
890
891 # $locale->text('Customer missing!');
892 # $locale->text('Vendor missing!');
893   
894   $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
895   
896   &validate_items;
897
898   # if the name changed get new values
899   if (&check_name($form->{vc})) {
900     &update;
901     exit;
902   }
903
904
905   # this is for the notes section for the [email] Subject
906   $form->{label} = ($form->{type} eq 'sales_order') ? $locale->text('Sales Order') : $locale->text('Purchase Order');
907
908   $form->{id} = 0 if $form->{saveasnew};
909   
910   $form->redirect($locale->text('Order saved!')) if (OE->save_order(\%myconfig, \%$form));
911   $form->error($locale->text('Cannot save order!'));
912   
913 }
914
915
916
917 sub delete {
918
919   $form->header;
920
921   print qq|
922 <body>
923
924 <form method=post action=$form->{script}>
925 |;
926
927   # delete action variable
928   delete $form->{action};
929
930   foreach $key (keys %$form) {
931     $form->{$key} =~ s/"/&quot;/g;
932     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
933   }
934
935   print qq|
936 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
937
938 <h4>|
939   .$locale->text('Are you sure you want to delete Order Number').qq| $form->{ordnumber}
940 </h4>
941 <p>
942 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
943 </form>
944
945 </body>
946 </html>
947 |;
948
949
950 }
951
952
953
954 sub yes {
955
956   $form->redirect($locale->text('Order deleted!')) if (OE->delete_order(\%myconfig, \%$form));
957   $form->error($locale->text('Cannot delete order!'));
958
959 }
960
961
962 sub invoice {
963   
964   $form->isblank("ordnumber", $locale->text('Order Number missing!'));
965   $form->isblank("orddate", $locale->text('Order Date missing!'));
966
967
968   # if the name changed get new values
969   if (&check_name($form->{vc})) {
970     &update;
971     exit;
972   }
973
974   $form->{closed} = 1;
975   OE->save_order(\%myconfig, \%$form);
976   
977   $form->{orddate} = $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
978   $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
979  
980   $form->{id} = '';
981   $form->{closed} = 0;
982   $form->{rowcount}--;
983   $form->{shipto} = 1;
984
985   &create_backorder;
986
987   if ($form->{type} eq 'purchase_order') {
988     $form->{title} = $locale->text('Add Vendor Invoice');
989     $form->{script} = 'ir.pl';
990     $script = "ir";
991     $buysell = 'sell';
992   }
993   if ($form->{type} eq 'sales_order') {
994     $form->{title} = $locale->text('Add Sales Invoice');
995     $form->{script} = 'is.pl';
996     $script = "is";
997     $buysell = 'buy';
998   }
999   
1000  
1001   # bo creates the id, reset it
1002   map { $form->{$_} = "" } qw(id subject message cc bcc);
1003   $form->{$form->{vc}} =~ s/--.*//g;
1004   $form->{type} = "invoice";
1005  
1006   # locale messages
1007   $locale = new Locale "$myconfig{countrycode}", "$script";
1008
1009   require "$form->{path}/$form->{script}";
1010
1011   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1012
1013   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(creditlimit creditremaining);
1014
1015   $currency = $form->{currency};
1016   &invoice_links;
1017
1018   $form->{currency} = $currency;
1019   $form->{exchangerate} = "";
1020   $form->{forex} = "";
1021   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell)));
1022  
1023   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1024
1025   &prepare_invoice;
1026
1027   # format amounts
1028   for $i (1 .. $form->{rowcount}) {
1029     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
1030
1031     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1032     $dec = length $dec;
1033     $decimalplaces = ($dec > 2) ? $dec : 2;
1034
1035     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
1036     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
1037
1038     map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
1039   }
1040
1041   &display_form;
1042
1043 }
1044
1045
1046 sub create_backorder {
1047
1048   # figure out if we need to create a backorder
1049   # items aren't saved if qty != 0
1050
1051   for $i (1 .. $form->{rowcount}) {
1052     $totalqty += $qty = $form->parse_amount($myconfig, $form->{"qty_$i"});
1053     $totalship += $ship = $form->parse_amount($myconfig, $form->{"ship_$i"});
1054     
1055     $form->{"qty_$i"} = $qty - $ship;
1056   }
1057
1058   if ($totalship == 0) {
1059     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1060     $form->{ordtotal} = 0;
1061     return;
1062   }
1063
1064   if ($totalqty == $totalship) {
1065     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1066     $form->{ordtotal} = 0;
1067     return;
1068   }
1069
1070   @flds = (qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts));
1071
1072   OE->save_order(\%myconfig, \%$form);
1073   
1074   # rebuild rows for invoice
1075   @a = ();
1076   $count = 0;
1077
1078   for $i (1 .. $form->{rowcount}) {
1079     
1080     # reformat values if there was a backorder quantity
1081     if ($form->{"qty_$i"}) {
1082       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
1083       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
1084     }
1085
1086     $form->{"qty_$i"} = $form->{"ship_$i"};
1087
1088     if ($form->{"qty_$i"}) {
1089       push @a, {};
1090       $j = $#a;
1091       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1092       $count++;
1093     }
1094   }
1095  
1096   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1097   $form->{rowcount} = $count;
1098   
1099 }
1100
1101
1102
1103 sub save_as_new {
1104
1105   $form->{saveasnew} = 1;
1106   $form->{closed} = 0;
1107   &save;
1108
1109 }
1110
1111