import sql-ledger 2.4.4
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / ir.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 # Inventory received module
25 #
26 #======================================================================
27
28
29 use SL::IR;
30 use SL::PE;
31
32 require "$form->{path}/io.pl";
33 require "$form->{path}/arap.pl";
34
35 1;
36 # end of main
37
38
39
40 sub add {
41
42   $form->{title} = $locale->text('Add Vendor Invoice');
43
44   &invoice_links;
45   &prepare_invoice;
46   &display_form;
47   
48 }
49
50
51 sub edit {
52
53   $form->{title} = $locale->text('Edit Vendor Invoice');
54
55   &invoice_links;
56   &prepare_invoice;
57   &display_form;
58   
59 }
60
61
62 sub invoice_links {
63
64   # create links
65   $form->create_links("AP", \%myconfig, "vendor");
66
67   IR->get_vendor(\%myconfig, \%$form);
68   IR->retrieve_invoice(\%myconfig, \%$form);
69
70   # currencies
71   @curr = split /:/, $form->{currencies};
72   chomp $curr[0];
73   $form->{defaultcurrency} = $curr[0];
74
75   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
76
77   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
78
79   # vendors
80   if (@{ $form->{all_vendor} }) {
81     $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
82     map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_vendor} });
83   }
84
85   
86   # forex
87   $form->{forex} = $form->{exchangerate};
88   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
89   
90   foreach $key (keys %{ $form->{AP_links} }) {
91
92     foreach $ref (@{ $form->{AP_links}{$key} }) {
93       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
94     }
95
96     if ($key eq "AP_paid") {
97       for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
98         $form->{"AP_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
99         # reverse paid
100         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount};
101         $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
102         $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
103         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
104         $form->{paidaccounts} = $i;
105       }
106     } else {
107       $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
108     }
109   }
110
111   $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
112
113   $form->{AP} = $form->{AP_1} unless $form->{id};
114
115   $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
116   
117 }
118
119
120
121 sub prepare_invoice {
122
123   if ($form->{id}) {
124         
125     map { $form->{$_} =~ s/"/&quot;/g } qw(invnumber ordnumber);
126
127     foreach $ref (@{ $form->{invoice_details} }) {
128       $i++;
129       map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
130       
131       ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
132       $dec = length $dec;
133       $decimalplaces = ($dec > 2) ? $dec : 2;
134       
135       $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
136       $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
137
138       $form->{rowcount} = $i;
139     }
140   }
141   
142 }
143
144
145
146 sub form_header {
147
148   # set option selected
149   foreach $item (qw(AP vendor currency)) {
150     $form->{"select$item"} =~ s/ selected//;
151     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
152   }
153   
154   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
155
156   $exchangerate = "";
157   if ($form->{currency} ne $form->{defaultcurrency}) {
158     if ($form->{forex}) {
159       $exchangerate .= qq|
160       <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
161       <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>
162 |;
163     } else {
164       $exchangerate .= qq|
165       <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
166       <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
167 |;
168     }
169   }
170   $exchangerate .= qq|
171 <input type=hidden name=forex value=$form->{forex}>
172 |;
173   
174   $vendor = ($form->{selectvendor}) ? qq|<select name=vendor>$form->{selectvendor}</select>\n<input type=hidden name="selectvendor" value="$form->{selectvendor}">| : qq|<input name=vendor value="$form->{vendor}" size=35>|;
175   
176
177   $form->header;
178
179   print qq|
180 <body>
181
182 <form method=post action=$form->{script}>
183
184 <input type=hidden name=id value=$form->{id}>
185 <input type=hidden name=title value="$form->{title}">
186 <input type=hidden name=vc value="vendor">
187
188 <input type=hidden name=closedto value=$form->{closedto}>
189 <input type=hidden name=locked value=$form->{locked}>
190
191
192 <table width=100%>
193   <tr class=listtop>
194     <th>$form->{title}</th>
195   </tr>
196   <tr height="5"></tr>
197   <tr>
198     <td>
199       <table width=100%>
200         <tr valign=top>
201           <td>
202             <table>
203               <tr>
204                 <th align=right>|.$locale->text('Record in').qq|</th>
205                 <td colspan=3><select name=AP>$form->{selectAP}</select></td>
206                 <input type=hidden name=selectAP value="$form->{selectAP}">
207               </tr>
208               <tr>
209                 <th align=right nowrap>|.$locale->text('Vendor').qq|</th>
210                 <td colspan=3>$vendor</td>
211                 
212                 <input type=hidden name=vendor_id value=$form->{vendor_id}>
213                 <input type=hidden name=oldvendor value="$form->{oldvendor}">
214
215               </tr>
216               <tr>
217                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
218                 <td><select name=currency>$form->{selectcurrency}</select></td>
219                 $exchangerate
220               </tr>
221             </table>
222           </td>
223           <td align=right>
224             <table>
225               <tr>
226                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
227                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
228               </tr>
229               <tr>
230                 <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
231                 <td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
232               </tr>
233               <tr>
234                 <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
235                 <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
236               </tr>
237               <tr>
238                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
239                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
240               </tr>
241             </table>
242           </td>
243         </tr>
244       </table>
245     </td>
246   </tr>
247 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
248 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
249 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
250 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
251
252 <input type=hidden name=taxpart value="$form->{taxpart}">
253 <input type=hidden name=taxservice value="$form->{taxservice}">
254
255 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
256 |;
257
258   foreach $item (split / /, $form->{taxaccounts}) {
259     print qq|
260 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
261 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
262 |;
263   }
264
265 }
266
267
268
269 sub form_footer {
270
271   $form->{invtotal} = $form->{invsubtotal};
272   
273   if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
274     $rows = 2;
275   }
276   $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
277
278
279   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
280   if ($form->{taxaccounts}) {
281     $taxincluded = qq|
282                 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
283 |;
284   }
285   
286   if (!$form->{taxincluded}) {
287     
288     foreach $item (split / /, $form->{taxaccounts}) {
289       if ($form->{"${item}_base"}) {
290         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
291         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
292         
293         $tax .= qq|
294                 <tr>
295                   <th align=right>$form->{"${item}_description"}</th>
296                   <td align=right>$form->{"${item}_total"}</td>
297                 </tr>
298 |;
299       }
300     }
301
302     $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
303     
304     $subtotal = qq|
305               <tr>
306                 <th align=right>|.$locale->text('Subtotal').qq|</th>
307                 <td align=right>$form->{invsubtotal}</td>
308               </tr>
309 |;
310
311   }
312
313
314   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
315
316   print qq|
317   <tr>
318     <td colspan=$colspan>
319       <table cellspacing="0">
320         <tr valign=bottom>
321           <td>
322             <table>
323               <tr>
324                 <th align=left>|.$locale->text('Notes').qq|</th>
325               </tr>
326               <tr>
327                 <td>$notes</td>
328               </tr>
329             </table>
330           </td>
331           <td colspan=2 align=right width=100%>
332             $taxincluded
333             <table width=100%>
334               $subtotal
335               $tax
336               <tr>
337                 <th align=right>|.$locale->text('Total').qq|</th>
338                 <td align=right>$form->{invtotal}</td>
339               </tr>
340             </table>
341           </td>
342         </tr>
343       </table>
344     </td>
345   </tr>
346   <tr>
347     <td colspan=$colspan>
348       <table width=100%>
349         <tr>
350           <th colspan=5 class=listheading>|.$locale->text('Payments').qq|</th>
351         </tr>
352 |;
353
354     if ($form->{currency} eq $form->{defaultcurrency}) {
355       @column_index = qw(datepaid source paid AP_paid);
356     } else {
357       @column_index = qw(datepaid source paid exchangerate AP_paid);
358     }
359
360     $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
361     $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
362     $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
363     $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
364     $column_data{source} = "<th>".$locale->text('Source')."</th>";
365
366     print qq|
367         <tr>
368 |;
369     map { print "$column_data{$_}\n" } @column_index;
370     print qq|
371         </tr>
372 |;
373
374     $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
375     for $i (1 .. $form->{paidaccounts}) {
376
377       print qq|
378         <tr>
379 |;
380
381       $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
382       $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
383
384       # format amounts
385       $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
386       $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
387
388       $exchangerate = qq|&nbsp;|;
389       if ($form->{currency} ne $form->{defaultcurrency}) {
390         if ($form->{"forex_$i"}) {
391           $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
392         } else {
393           $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
394         }
395       }
396       $exchangerate .= qq|
397 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
398 |;
399
400       $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
401       $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
402       $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
403       $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
404       $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value=$form->{"source_$i"}></td>|;
405
406       map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
407
408       print qq|
409         </tr>
410 |;
411     }
412     
413     print qq|
414             <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
415             <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
416       </table>
417     </td>
418   </tr>
419   <tr>
420     <td><hr size=3 noshade></td>
421   </tr>
422 </table>
423 <br>
424 |;
425
426   $invdate = $form->datetonum($form->{invdate}, \%myconfig);
427   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
428   
429   if ($form->{id}) {
430     print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
431 |;
432
433     if (!$form->{revtrans}) {
434       if (!$form->{locked}) {
435         print qq|
436         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
437         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
438 |;
439       }
440     }
441
442     if ($invdate > $closedto) {
443       print qq|
444       <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
445       <input class=submit type=submit name=action value="|.$locale->text('Order').qq|">
446 |;
447     }
448
449   } else {
450     if ($invdate > $closedto) {
451       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
452       <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
453     }
454   }
455
456 print qq|
457
458 <input type=hidden name=rowcount value=$form->{rowcount}>
459
460 <input name=callback type=hidden value="$form->{callback}">
461
462 <input type=hidden name=path value=$form->{path}>
463 <input type=hidden name=login value=$form->{login}>
464 <input type=hidden name=password value=$form->{password}>
465
466 </form>
467
468 </body>
469 </html>
470 |;
471
472 }
473
474
475
476 sub update {
477
478   $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
479   
480   &check_name(vendor);
481
482   &check_project;
483
484   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell')));
485   
486   
487   for $i (1 .. $form->{paidaccounts}) {
488     if ($form->{"paid_$i"}) {
489       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
490
491       $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell')));
492     }
493   }
494   
495   $i = $form->{rowcount};
496   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
497
498   if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
499
500     &check_form;
501     
502   } else {
503    
504     IR->retrieve_item(\%myconfig, \%$form);
505
506     my $rows = scalar @{ $form->{item_list} };
507
508     if ($rows) {
509       $form->{"qty_$i"}                     = 1 unless ($form->{"qty_$i"});
510       
511       if ($rows > 1) {
512         
513         &select_item;
514         exit;
515         
516       } else {
517         # override sellprice if there is one entered
518         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
519
520         map { $form->{item_list}[$i]{$_} =~ s/"/&quot;/g } qw(partnumber description unit);
521         
522         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
523
524         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
525         
526         ($dec) = ($s =~ /\.(\d+)/);
527         $dec = length $dec;
528         $decimalplaces = ($dec > 2) ? $dec : 2;
529  
530         if ($sellprice) {
531           $form->{"sellprice_$i"} = $sellprice;
532         } else {
533           # if there is an exchange rate adjust sellprice
534           $form->{"sellprice_$i"} /= $exchangerate;
535         }
536      
537         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
538         $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
539       }
540
541       &display_form;
542
543     } else {
544       # ok, so this is a new part
545       # ask if it is a part or service item
546
547       if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
548         $form->{rowcount}--;
549         $form->{"discount_$i"} = "";
550         &display_form;
551       } else {
552         
553         $form->{"id_$i"}                = 0;
554         $form->{"unit_$i"}      = $locale->text('ea');
555
556         &new_item;
557
558       }
559     }
560   }
561 }
562
563
564
565 sub post {
566
567   $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
568   $form->isblank("invdate", $locale->text('Invoice Date missing!'));
569   $form->isblank("vendor", $locale->text('Vendor missing!'));
570   
571   # if the vendor changed get new values
572   if (&check_name(vendor)) {
573     &update;
574     exit;
575   }
576
577   &validate_items;
578
579   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
580   $invdate = $form->datetonum($form->{invdate}, \%myconfig);
581
582   $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto);
583
584   $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
585   
586   for $i (1 .. $form->{paidaccounts}) {
587     if ($form->{"paid_$i"}) {
588       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
589
590       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
591       
592       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
593       
594       if ($form->{currency} ne $form->{defaultcurrency}) {
595         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
596         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
597       }
598     }
599   }
600   
601   
602   ($form->{AP}) = split /--/, $form->{AP};
603   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
604   
605   $form->{id} = 0 if $form->{postasnew};
606
607   $form->redirect($locale->text('Invoice posted!')) if (IR->post_invoice(\%myconfig, \%$form));
608   $form->error($locale->text('Cannot post invoice!'));
609   
610 }
611
612
613
614 sub delete {
615
616   $form->header;
617
618   print qq|
619 <body>
620
621 <form method=post action=$form->{script}>
622 |;
623
624   # delete action variable
625   delete $form->{action};
626
627   foreach $key (keys %$form) {
628     $form->{$key} =~ s/"/&quot;/g;
629     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
630   }
631
632   print qq|
633 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
634
635 <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
636 <p>
637 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
638 </form>
639 |;
640
641
642 }
643
644
645
646 sub yes {
647
648   $form->redirect($locale->text('Invoice deleted!')) if (IR->delete_invoice(\%myconfig, \%$form));
649   $form->error($locale->text('Cannot delete invoice!'));
650
651 }
652
653