import sql-ledger 2.4.4
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / ar.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2001
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 # Accounts Receivables
25 #
26 #======================================================================
27
28
29 use SL::AR;
30 use SL::IS;
31 use SL::PE;
32
33 require "$form->{path}/arap.pl";
34
35 1;
36 # end of main
37
38 # this is for our long dates
39 # $locale->text('January')
40 # $locale->text('February')
41 # $locale->text('March')
42 # $locale->text('April')
43 # $locale->text('May ')
44 # $locale->text('June')
45 # $locale->text('July')
46 # $locale->text('August')
47 # $locale->text('September')
48 # $locale->text('October')
49 # $locale->text('November')
50 # $locale->text('December')
51
52 # this is for our short month
53 # $locale->text('Jan')
54 # $locale->text('Feb')
55 # $locale->text('Mar')
56 # $locale->text('Apr')
57 # $locale->text('May')
58 # $locale->text('Jun')
59 # $locale->text('Jul')
60 # $locale->text('Aug')
61 # $locale->text('Sep')
62 # $locale->text('Oct')
63 # $locale->text('Nov')
64 # $locale->text('Dec')
65
66
67 sub add {
68
69   $form->{title} = "Add";
70   $form->{callback} = "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&password=$form->{password}" unless $form->{callback};
71
72   &create_links;
73   &display_form;
74   
75 }
76
77
78 sub edit {
79
80   $form->{title} = "Edit";
81
82   &create_links;
83   &display_form;
84
85 }
86
87
88 sub display_form {
89
90   &form_header;
91   &form_footer;
92
93 }
94
95
96 sub create_links {
97
98   $form->create_links("AR", \%myconfig, "customer");
99   $duedate = $form->{duedate};
100   
101   $taxincluded = $form->{taxincluded};
102   
103   IS->get_customer(\%myconfig, \%$form);
104
105   $form->{duedate} = $duedate;
106   $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
107
108   # currencies
109   @curr = split /:/, $form->{currencies};
110   chomp $curr[0];
111   $form->{defaultcurrency} = $curr[0];
112
113   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
114
115   # customers
116   if (@{ $form->{all_customer} }) {
117     $form->{customer} = "$form->{customer}--$form->{customer_id}";
118     map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_customer} });
119   }
120   
121   # forex
122   $form->{forex} = $form->{exchangerate};
123   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
124   
125   foreach $key (keys %{ $form->{AR_links} }) {
126        
127     foreach $ref (@{ $form->{AR_links}{$key} }) {
128       if ($key eq 'AR_tax') {
129         $form->{"selectAR_tax_$ref->{accno}"} = "<option>$ref->{accno}--$ref->{description}\n";
130         next;
131       }
132       $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
133     }
134     
135     # if there is a value we have an old entry
136     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
137       if ($key eq "AR_paid") {
138         $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
139         # reverse paid
140         $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
141         $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
142         $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
143         
144         $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
145         
146         $form->{paidaccounts}++;
147       } else {
148      
149         $akey = $key;
150         $akey =~ s/AR_//;
151         
152         if ($key eq "AR_tax") {
153
154           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
155           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i-1]->{amount} / $exchangerate, 2);
156           $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
157         } else {
158           $form->{"${akey}_$i"} = $form->round_amount($form->{acc_trans}{$key}->[$i-1]->{amount} / $exchangerate, 2);
159           if ($akey eq 'amount') {
160             $form->{rowcount}++;
161             $totalamount += $form->{"${akey}_$i"};
162
163             $form->{"oldprojectnumber_$i"} = $form->{"projectnumber_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
164             $form->{"project_id_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
165           }
166           $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
167         }
168       }
169     }
170   }
171
172   $form->{taxincluded} = $taxincluded if ($form->{id});
173   $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
174
175   if ($form->{taxincluded} && $totalamount) {
176     # add tax to amounts and invtotal
177     for $i (1 .. $form->{rowcount}) {
178       $taxamount = $totaltax * $form->{"amount_$i"} / $totalamount;
179       $tax = $form->round_amount($taxamount, 2);
180       $diff += ($taxamount - $tax);
181       $form->{"amount_$i"} += $tax;
182     }
183     $form->{amount_1} += $form->round_amount($diff, 2);
184   }
185
186   $form->{invtotal} = $totalamount + $totaltax;
187   $form->{rowcount}++ if $form->{id};
188   
189   $form->{AR} = $form->{AR_1};
190   
191   $form->{locked} = ($form->datetonum($form->{transdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
192
193 }
194
195
196 sub form_header {
197
198   $title = $form->{title};
199   $form->{title} = $locale->text("$title Accounts Receivables Transaction");
200
201   $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
202   
203 # $locale->text('Add Accounts Receivables Transaction')
204 # $locale->text('Edit Accounts Receivables Transaction')
205
206   # set option selected
207   foreach $item (qw(AR customer currency)) {
208     $form->{"select$item"} =~ s/ selected//;
209     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
210   }
211   
212   # format amounts
213   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
214   
215   $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
216   $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
217
218   $exchangerate = qq|
219 <input type=hidden name=forex value=$form->{forex}>
220 |;
221   if ($form->{currency} ne $form->{defaultcurrency}) {
222     if ($form->{forex}) {
223       $exchangerate .= qq|
224         <th align=right>|.$locale->text('Exchangerate').qq|</th>
225         <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
226 |;
227     } else {
228       $exchangerate .= qq|
229         <th align=right>|.$locale->text('Exchangerate').qq|</th>
230         <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
231 |;
232     }
233   }
234   
235   $taxincluded = "";
236   if ($form->{taxaccounts}) {
237     $taxincluded = qq|
238               <tr>
239                 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
240                 <th align=left nowrap>|.$locale->text('Tax Included').qq|</th>
241               </tr>
242 |;
243   }
244
245  
246   if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
247     $rows = 2;
248   }
249   $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
250   
251
252   $form->header;
253   
254   $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
255
256   $customer = ($form->{selectcustomer}) ? qq|<select name=customer>$form->{selectcustomer}</select>| : qq|<input name=customer value="$form->{customer}" size=35>|;
257  
258   print qq|
259 <body>
260
261 <form method=post action=$form->{script}>
262
263 <input type=hidden name=id value=$form->{id}>
264 <input type=hidden name=sort value=$form->{sort}>
265 <input type=hidden name=closedto value=$form->{closedto}>
266 <input type=hidden name=locked value=$form->{locked}>
267 <input type=hidden name=title value="$title">
268
269 <table width=100%>
270   <tr>
271     <th class=listtop>$form->{title}</th>
272   </tr>
273   <tr height="5"></tr>
274   <tr valign=top>
275     <td>
276       <table width=100%>
277         <tr valign=top>
278           <td>
279             <table>
280               <tr>
281                 <th align="right" nowrap>|.$locale->text('Customer').qq|</th>
282                 <td colspan=3>$customer</td>
283                 <input type=hidden name=selectcustomer value="$form->{selectcustomer}">
284                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
285                 <input type=hidden name=customer_id value="$form->{customer_id}">
286                 <input type=hidden name=terms value=$form->{terms}>
287               </tr>
288               <tr>
289                 <td></td>
290                 <td colspan=3>
291                   <table width=100%>
292                     <tr>
293                       <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
294                       <td>$form->{creditlimit}</td>
295                       <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
296                       <td class="plus$n">$form->{creditremaining}</td>
297                       <input type=hidden name=creditlimit value=$form->{creditlimit}>
298                       <input type=hidden name=creditremaining value=$form->{creditremaining}>
299                     </tr>
300                   </table>
301                 </td>
302               </tr>
303               $taxincluded
304               <tr>
305                 <th align=right>|.$locale->text('Currency').qq|</th>
306                 <td><select name=currency>$form->{selectcurrency}</select></td>
307                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
308                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
309                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
310                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
311                 $exchangerate
312               </tr>
313             </table>
314           </td>
315           <td align=right>
316             <table>
317               <tr>
318                 <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
319                 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
320               </tr>
321               <tr>
322                 <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
323                 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
324               </tr>
325               <tr>
326                 <th align=right nowrap>|.$locale->text('Invoice Date').qq|</th>
327                 <td><input name=transdate size=11 title="($myconfig{'dateformat'})" value=$form->{transdate}></td>
328               </tr>
329               <tr>
330                 <th align=right nowrap>|.$locale->text('Due Date').qq|</th>
331                 <td><input name=duedate size=11 title="$myconfig{'dateformat'}" value=$form->{duedate}></td>
332               </tr>
333             </table>
334           </td>
335         </tr>
336       </table>
337     </td>
338   </tr>
339   <tr>
340     <td>
341       <table width=100%>
342     
343 <input type=hidden name=selectAR_amount value="$form->{selectAR_amount}">
344 <input type=hidden name=rowcount value=$form->{rowcount}>
345 |;
346
347   $amount = $locale->text('Amount');
348   $project = $locale->text('Project');
349   
350   for $i (1 .. $form->{rowcount}) {
351     
352     $form->{"selectAR_amount"} =~ s/ selected//;
353     $form->{"selectAR_amount"} =~ s/option>\Q$form->{"AR_amount_$i"}\E/option selected>$form->{"AR_amount_$i"}/;
354     
355     # format amounts
356     $form->{"amount_$i"} = $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
357
358     print qq|
359         <tr>
360           <th align=right>$amount</th>
361           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
362           <th>$project</th>
363           <td><input name="projectnumber_$i" size=20 value="$form->{"projectnumber_$i"}">
364               <input type=hidden name="project_id_$i" value=$form->{"project_id_$i"}>
365               <input type=hidden name="oldprojectnumber_$i" value="$form->{"oldprojectnumber_$i"}"></td>
366           <td width=50%><select name="AR_amount_$i">$form->{"selectAR_amount"}</select></td>
367         </tr>
368 |;
369     $amount = "";
370     $project = "";
371   }
372
373   $taxlabel = ($form->{taxincluded}) ? $locale->text('Tax Included') : $locale->text('Tax');
374   
375   foreach $item (split / /, $form->{taxaccounts}) {
376
377     $form->{"tax_$item"} = $form->format_amount(\%myconfig, $form->{"tax_$item"}, 2);
378     print qq|
379         <tr>
380           <th align=right nowrap>$taxlabel</th>
381           <td><input name="tax_$item" size=10 value=$form->{"tax_$item"}></td>
382           <td colspan=2></td>
383           <td><select name="AR_tax_$item">$form->{"selectAR_tax_$item"}</select></td>
384         </tr>
385         <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
386         <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
387         <input type=hidden name="selectAR_tax_$item" value="$form->{"selectAR_tax_$item"}">
388 |;
389   
390   }
391
392   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
393
394   print qq|
395         <tr>
396
397           <th align=right>|.$locale->text('Total').qq|</th>
398           <th align=left>$form->{invtotal}</th>
399
400           <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
401           <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
402
403           <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
404
405           <td colspan=2></td>
406           <td><select name=AR>$form->{selectAR}</select></td>
407           <input type=hidden name=selectAR value="$form->{selectAR}">
408   
409         </tr>
410         <tr>
411           <th align=right>|.$locale->text('Notes').qq|</th>
412           <td colspan=4>$notes</td>
413         </tr>
414       </table>
415     </td>
416   </tr>
417   <tr>
418     <td>
419       <table width=100%>
420         <tr>
421           <th class=listheading colspan=5>|.$locale->text('Payments').qq|</th>
422         </tr>
423 |;
424
425
426   if ($form->{currency} eq $form->{defaultcurrency}) {
427     @column_index = qw(datepaid source paid AR_paid);
428   } else {
429     @column_index = qw(datepaid source paid exchangerate AR_paid);
430   }
431
432   $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
433   $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
434   $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
435   $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
436   $column_data{source} = "<th>".$locale->text('Source')."</th>";
437   
438   print "
439         <tr>
440 ";
441   map { print "$column_data{$_}\n" } @column_index;
442   print "
443         </tr>
444 ";
445
446   $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
447   for $i (1 .. $form->{paidaccounts}) {
448     print "
449         <tr>
450 ";
451
452     $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
453     $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
454   
455     # format amounts
456     $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
457     $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
458
459     $exchangerate = qq|&nbsp;|;
460     if ($form->{currency} ne $form->{defaultcurrency}) {
461       if ($form->{"forex_$i"}) {
462         $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
463       } else {
464         $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
465       }
466     }
467     
468     $exchangerate .= qq|
469 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
470 |;
471
472     $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
473     $column_data{"AR_paid_$i"} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
474     $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
475     $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 value=$form->{"datepaid_$i"}></td>|;
476     $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
477
478     map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
479     
480     print "
481         </tr>
482 ";
483   }
484   
485   print qq|
486 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
487 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
488
489       </table>
490     </td>
491   </tr>
492   <tr>
493     <td><hr size=3 noshade></td>
494   </tr>
495 </table>
496 |;
497
498 }
499
500
501 sub form_footer {
502
503   print qq|
504
505 <input name=callback type=hidden value="$form->{callback}">
506
507 <input type=hidden name=path value=$form->{path}>
508 <input type=hidden name=login value=$form->{login}>
509 <input type=hidden name=password value=$form->{password}>
510
511 <br>
512 |;
513
514   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
515   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
516   
517   if ($form->{id}) {
518     
519       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
520 |;
521
522     if (!$form->{revtrans}) {
523       if (!$form->{locked}) {
524         print qq|
525         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
526         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
527 |;
528       }
529     }
530
531     if ($transdate > $closedto) {
532       print qq|
533 <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
534 |;
535     }
536
537   } else {
538     if ($transdate > $closedto) {
539       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
540       <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
541     }
542   }
543
544   print "
545 </form>
546
547 </body>
548 </html>
549 ";
550
551 }
552
553
554 sub update {
555   my $display = shift;
556
557   if ($display) {
558     goto TAXCALC;
559   }
560
561   $form->{invtotal} = 0;
562   
563   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
564
565   @flds = qw(amount AR_amount projectnumber oldprojectnumber project_id);
566   $count = 0;
567   @a = ();
568   for $i (1 .. $form->{rowcount}) {
569     $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
570     if ($form->{"amount_$i"}) {
571       push @a, {};
572       $j = $#a;
573
574       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
575       $count++;
576     }
577   }
578
579   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
580   $form->{rowcount} = $count + 1;
581
582   map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
583
584   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'buy')));
585
586
587   $form->{invdate} = $form->{transdate};
588   &check_name(customer);
589
590   &check_project;
591
592
593 TAXCALC:
594   # recalculate taxes
595   if ($form->{taxincluded}) {
596     $taxrate = 0;
597
598     map { $taxrate += $form->{"${_}_rate"} } split / /, $form->{taxaccounts};
599
600     foreach $item (split / /, $form->{taxaccounts}) {
601       $amount = ($form->{invtotal} * (1 - 1 / (1 + $taxrate)) * $form->{"${item}_rate"} / $taxrate) if $taxrate;
602       $form->{"tax_$item"} = $form->round_amount($amount, 2);
603       $taxdiff += ($amount - $form->{"tax_$item"});
604       if (abs $taxdiff >= 0.005) {
605         $form->{"tax_$item"} += $form->round_amount($taxdiff, 2);
606         $taxdiff = 0;
607       }
608       $form->{"selectAR_tax_$item"} = qq|<option>$item--$form->{"${item}_description"}|;
609       $totaltax += $form->{"tax_$item"};
610     }
611   } else {
612     foreach $item (split / /, $form->{taxaccounts}) {
613       $form->{"tax_$item"} = $form->round_amount($form->{invtotal} * $form->{"${item}_rate"}, 2);
614       $form->{"selectAR_tax_$item"} = qq|<option>$item--$form->{"${item}_description"}|;
615       $totaltax += $form->{"tax_$item"};
616     }
617   }
618
619   $form->{invtotal} = ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
620
621   for $i (1 .. $form->{paidaccounts}) {
622     if ($form->{"paid_$i"}) {
623       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
624
625       $totalpaid += $form->{"paid_$i"};
626
627       $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy')));
628     }
629   }
630
631   $form->{creditremaining} -= ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} - $form->{oldinvtotal});
632   $form->{oldinvtotal} = $form->{invtotal};
633   $form->{oldtotalpaid} = $totalpaid;
634   
635   &display_form;
636   
637 }
638
639
640 sub post {
641
642   # check if there is an invoice number, invoice and due date
643   $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
644   $form->isblank("transdate", $locale->text('Invoice Date missing!'));
645   $form->isblank("duedate", $locale->text('Due Date missing!'));
646   $form->isblank("customer", $locale->text('Customer missing!'));
647
648   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
649   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
650   
651   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
652
653   $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
654   
655   for $i (1 .. $form->{paidaccounts}) {
656     if ($form->{"paid_$i"}) {
657       $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
658       
659       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
660
661       $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
662
663       if ($form->{currency} ne $form->{defaultcurrency}) {
664         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
665         $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
666       }
667     }
668   }
669
670   # if oldcustomer ne customer redo form
671   ($customer) = split /--/, $form->{customer};
672   if ($form->{oldcustomer} ne "$customer--$form->{customer_id}") {
673     &update;
674     exit;
675   }
676
677   $form->{id} = 0 if $form->{postasnew};
678
679   $form->redirect($locale->text('Transaction posted!')) if (AR->post_transaction(\%myconfig, \%$form));
680   $form->error($locale->text('Cannot post transaction!'));
681
682 }
683
684
685 sub post_as_new {
686
687   $form->{postasnew} = 1;
688   &post;
689
690 }
691
692
693 sub delete {
694
695   $form->{title} = $locale->text('Confirm!');
696   
697   $form->header;
698
699   print qq|
700 <body>
701
702 <form method=post action=$form->{script}>
703 |;
704
705   foreach $key (keys %$form) {
706     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
707   }
708
709   print qq|
710 <h2 class=confirm>$form->{title}</h2>
711
712 <h4>|.$locale->text('Are you sure you want to delete Transaction').qq| $form->{invnumber}</h4>
713
714 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
715 </form>
716
717 </body>
718 </html>
719 |;
720
721 }
722
723
724
725 sub yes {
726
727   $form->redirect($locale->text('Transaction deleted!')) if (AR->delete_transaction(\%myconfig, \%$form));
728   $form->error($locale->text('Cannot delete transaction!'));
729
730 }
731
732
733 sub search {
734
735   # setup customer selection
736   $form->all_vc(\%myconfig, "customer");
737
738   if (@{ $form->{all_customer} }) { 
739     map { $customer .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{all_customer} };
740     $customer = qq|<select name=customer><option>\n$customer</select>|;
741   } else {
742     $customer = qq|<input name=customer size=35>|;
743   }
744     
745   $form->{title} = $locale->text('AR Transactions');
746     
747   $form->header;
748   
749   print qq|
750 <body>
751
752 <form method=post action=$form->{script}>
753
754 <table width=100%>
755   <tr><th class=listtop>$form->{title}</th></tr>
756   <tr height="5"></tr>
757   <tr>
758     <td>
759       <table>
760         <tr>
761           <th align=right>|.$locale->text('Customer').qq|</th>
762           <td colspan=3>$customer</td>
763         </tr>
764         <tr>
765           <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
766           <td colspan=3><input name=invnumber size=20></td>
767         </tr>
768         <tr>
769           <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
770           <td colspan=3><input name=ordnumber size=20></td>
771         </tr>
772         <tr>
773           <th align=right nowrap>|.$locale->text('Notes').qq|</th>
774           <td colspan=3><input name=notes size=40></td>
775         </tr>
776         <tr>
777           <th align=right nowrap>|.$locale->text('From').qq|</th>
778           <td><input name=transdatefrom size=11 title="$myconfig{dateformat}"></td>
779           <th align=right>|.$locale->text('to').qq|</th>
780           <td><input name=transdateto size=11 title="$myconfig{dateformat}"></td>
781         </tr>
782         <input type=hidden name=sort value=transdate>
783       </table>
784     </td>
785   </tr>
786   <tr>
787     <td>
788       <table>
789         <tr>
790           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
791           <td>
792             <table width=100%>
793               <tr>
794                 <td align=right><input name=open class=checkbox type=checkbox value=Y checked></td>
795                 <td nowrap>|.$locale->text('Open').qq|</td>
796                 <td align=right><input name=closed class=checkbox type=checkbox value=Y></td>
797                 <td nowrap>|.$locale->text('Closed').qq|</td>
798               </tr>
799               <tr>
800                 <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
801                 <td nowrap>|.$locale->text('ID').qq|</td>
802                 <td align=right><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
803                 <td nowrap>|.$locale->text('Invoice Number').qq|</td>
804                 <td align=right><input name="l_ordnumber" class=checkbox type=checkbox value=Y></td>
805                 <td nowrap>|.$locale->text('Order Number').qq|</td>
806                 <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
807                 <td nowrap>|.$locale->text('Invoice Date').qq|</td>
808               </tr>
809               <tr>
810                 <td align=right><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
811                 <td nowrap>|.$locale->text('Customer').qq|</td>
812                 <td align=right><input name="l_netamount" class=checkbox type=checkbox value=Y></td>
813                 <td nowrap>|.$locale->text('Amount').qq|</td>
814                 <td align=right><input name="l_tax" class=checkbox type=checkbox value=Y></td>
815                 <td nowrap>|.$locale->text('Tax').qq|</td>
816                 <td align=right><input name="l_amount" class=checkbox type=checkbox value=Y checked></td>
817                 <td nowrap>|.$locale->text('Total').qq|</td>
818               </tr>
819               <tr>
820                 <td align=right><input name="l_datepaid" class=checkbox type=checkbox value=Y></td>
821                 <td nowrap>|.$locale->text('Date Paid').qq|</td>
822                 <td align=right><input name="l_paid" class=checkbox type=checkbox value=Y checked></td>
823                 <td nowrap>|.$locale->text('Paid').qq|</td>
824                 <td align=right><input name="l_duedate" class=checkbox type=checkbox value=Y></td>
825                 <td nowrap>|.$locale->text('Due Date').qq|</td>
826                 <td align=right><input name="l_due" class=checkbox type=checkbox value=Y></td>
827                 <td nowrap>|.$locale->text('Amount Due').qq|</td>
828               </tr>
829               <tr>
830                 <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
831                 <td nowrap>|.$locale->text('Notes').qq|</td>
832                 <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
833                 <td nowrap>|.$locale->text('Salesperson').qq|</td>
834                 <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
835                 <td nowrap>|.$locale->text('Ship via').qq|</td>
836               </tr>
837               <tr>
838                 <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
839                 <td nowrap>|.$locale->text('Subtotal').qq|</td>
840               </tr>
841             </table>
842           </td>
843         </tr>
844       </table>
845     </td>
846   </tr>
847   <tr>
848     <td><hr size=3 noshade></td>
849   </tr>
850 </table>
851
852 <input type=hidden name=nextsub value=$form->{nextsub}>
853
854 <input type=hidden name=path value=$form->{path}>
855 <input type=hidden name=login value=$form->{login}>
856 <input type=hidden name=password value=$form->{password}>
857
858 <br>
859 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
860
861 </form>
862
863 </body>
864 </html>
865 |;
866
867 }
868
869
870 sub ar_transactions {
871
872   $form->{customer} = $form->unescape($form->{customer});
873   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
874
875   AR->ar_transactions(\%myconfig, \%$form);
876
877   $callback = "$form->{script}?action=ar_transactions&path=$form->{path}&login=$form->{login}&password=$form->{password}";
878   $href = $callback;
879
880   if ($form->{customer}) {
881     $callback .= "&customer=".$form->escape($form->{customer});
882     $href .= "&customer=".$form->escape($form->{customer});
883     $option = $locale->text('Customer')." : $form->{customer}";
884   }
885   if ($form->{invnumber}) {
886     $callback .= "&invnumber=$form->{invnumber}";
887     $href .= "&invnumber=".$form->escape($form->{invnumber});
888     $option .= "\n<br>" if ($option);
889     $option .= $locale->text('Invoice Number')." : $form->{invnumber}";
890   }
891   if ($form->{ordnumber}) {
892     $callback .= "&ordnumber=$form->{ordnumber}";
893     $href .= "&ordnumber=".$form->escape($form->{ordnumber});
894     $option .= "\n<br>" if ($option);
895     $option .= $locale->text('Order Number')." : $form->{ordnumber}";
896   }
897   if ($form->{notes}) {
898     $callback .= "&notes=$form->{notes}";
899     $href .= "&notes=".$form->escape($form->{notes});
900     $option .= "\n<br>" if $option;
901     $option .= $locale->text('Notes')." : $form->{notes}";
902   }
903   
904   if ($form->{transdatefrom}) {
905     $callback .= "&transdatefrom=$form->{transdatefrom}";
906     $href .= "&transdatefrom=$form->{transdatefrom}";
907     $option .= "\n<br>" if ($option);
908     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{transdatefrom}, 1);
909   }
910   if ($form->{transdateto}) {
911     $callback .= "&transdateto=$form->{transdateto}";
912     $href .= "&transdateto=$form->{transdateto}";
913     $option .= "\n<br>" if ($option);
914     $option .= $locale->text('to')."&nbsp;".$locale->date(\%myconfig, $form->{transdateto}, 1);
915   }
916   if ($form->{open}) {
917     $callback .= "&open=$form->{open}";
918     $href .= "&open=$form->{open}";
919     $option .= "\n<br>" if ($option);
920     $option .= $locale->text('Open');
921   }
922   if ($form->{closed}) {
923     $callback .= "&closed=$form->{closed}";
924     $href .= "&closed=$form->{closed}";
925     $option .= "\n<br>" if ($option);
926     $option .= $locale->text('Closed');
927   }
928
929   @columns = $form->sort_columns(qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee shippingpoint));
930   
931   foreach $item (@columns) {
932     if ($form->{"l_$item"} eq "Y") {
933       push @column_index, $item;
934
935       # add column to href and callback
936       $callback .= "&l_$item=Y";
937       $href .= "&l_$item=Y";
938     }
939   }
940
941   if ($form->{l_subtotal} eq 'Y') {
942     $callback .= "&l_subtotal=Y";
943     $href .= "&l_subtotal=Y";
944   }
945   
946
947   $column_header{id} = "<th><a class=listheading href=$href&sort=id>".$locale->text('ID')."</a></th>";
948   $column_header{transdate} = "<th><a class=listheading href=$href&sort=transdate>".$locale->text('Date')."</a></th>";
949   $column_header{duedate} = "<th><a class=listheading href=$href&sort=duedate>".$locale->text('Due Date')."</a></th>";
950   $column_header{invnumber} = "<th><a class=listheading href=$href&sort=invnumber>".$locale->text('Invoice')."</a></th>";
951   $column_header{ordnumber} = "<th><a class=listheading href=$href&sort=ordnumber>".$locale->text('Order')."</a></th>";
952   $column_header{name} = "<th><a class=listheading href=$href&sort=name>".$locale->text('Customer')."</a></th>";
953   $column_header{netamount} = "<th class=listheading>" . $locale->text('Amount') . "</th>";
954   $column_header{tax} = "<th class=listheading>" . $locale->text('Tax') . "</th>";
955   $column_header{amount} = "<th class=listheading>" . $locale->text('Total') . "</th>";
956   $column_header{paid} = "<th class=listheading>" . $locale->text('Paid') . "</th>";
957   $column_header{datepaid} = "<th><a class=listheading href=$href&sort=datepaid>" . $locale->text('Date Paid') . "</a></th>";
958   $column_header{due} = "<th class=listheading>" . $locale->text('Amount Due') . "</th>";
959   $column_header{notes} = "<th class=listheading>".$locale->text('Notes')."</th>";
960   $column_header{employee} = "<th class=listheading><a class=listheading href=$href&sort=employee>".$locale->text('Salesperson')."</th>";
961
962   $column_header{shippingpoint} = "<th class=listheading><a class=listheading href=$href&sort=shippingpoint>" . $locale->text('Ship via') . "</a></th>";
963   
964
965   $form->{title} = $locale->text('AR Transactions');
966
967   $form->header;
968
969   print qq|
970 <body>
971
972 <table width=100%>
973   <tr>
974     <th class=listtop>$form->{title}</th>
975   </tr>
976   <tr height="5"></tr>
977   <tr>
978     <td>$option</td>
979   </tr>
980   <tr>
981     <td>
982       <table width=100%>
983         <tr class=listheading>
984 |;
985
986   map { print "\n$column_header{$_}" } @column_index;
987
988   print qq|
989         </tr>
990 |;
991
992
993   # add sort and escape callback, this one we use for the add sub
994   $form->{callback} = $callback .= "&sort=$form->{sort}";
995
996   # escape callback for href
997   $callback = $form->escape($callback);
998   
999   if (@{ $form->{AR} }) {
1000     $sameitem = $form->{AR}->[0]->{$form->{sort}};
1001   }
1002   
1003   # sums and tax on reports by Antonio Gallardo
1004   #
1005   foreach $ar (@{ $form->{AR} }) {
1006
1007     if ($form->{l_subtotal} eq 'Y') {
1008       if ($sameitem ne $ar->{$form->{sort}}) {
1009         &ar_subtotal;
1010       }
1011     }
1012     
1013     $column_data{netamount} = "<td align=right>".$form->format_amount(\%myconfig, $ar->{netamount}, 2, "&nbsp;")."</td>";
1014     $column_data{tax} = "<td align=right>".$form->format_amount(\%myconfig, $ar->{amount} - $ar->{netamount}, 2, "&nbsp;")."</td>";
1015     $column_data{amount} = "<td align=right>".$form->format_amount(\%myconfig, $ar->{amount}, 2, "&nbsp;")."</td>";
1016     $column_data{paid} = "<td align=right>".$form->format_amount(\%myconfig, $ar->{paid}, 2, "&nbsp;")."</td>";
1017     $column_data{due} = "<td align=right>".$form->format_amount(\%myconfig, $ar->{amount} - $ar->{paid}, 2, "&nbsp;")."</td>";
1018     
1019     $subtotalnetamount += $ar->{netamount};
1020     $subtotalamount += $ar->{amount};
1021     $subtotalpaid += $ar->{paid};
1022     $subtotaldue += $ar->{amount} - $ar->{paid};
1023     
1024     $totalnetamount += $ar->{netamount};
1025     $totalamount += $ar->{amount};
1026     $totalpaid += $ar->{paid};
1027     $totaldue += ($ar->{amount} - $ar->{paid});
1028     
1029     $column_data{transdate} = "<td>$ar->{transdate}&nbsp;</td>";
1030     $column_data{id} = "<td>$ar->{id}</td>";
1031     $column_data{datepaid} = "<td>$ar->{datepaid}&nbsp;</td>";
1032     $column_data{duedate} = "<td>$ar->{duedate}&nbsp;</td>";
1033
1034     $module = ($ar->{invoice}) ? "is.pl" : $form->{script};
1035
1036     $column_data{invnumber} = "<td><a href=$module?action=edit&id=$ar->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ar->{invnumber}</a></td>";
1037     $column_data{ordnumber} = "<td>$ar->{ordnumber}&nbsp;</td>";
1038     $column_data{name} = "<td>$ar->{name}</td>";
1039     $ar->{notes} =~ s/\r\n/<br>/g;
1040     $column_data{notes} = "<td>$ar->{notes}&nbsp;</td>";
1041     $column_data{shippingpoint} = "<td>$ar->{shippingpoint}&nbsp;</td>";
1042     $column_data{employee} = "<td>$ar->{employee}&nbsp;</td>";
1043
1044     $i++; $i %= 2;
1045     print "
1046         <tr class=listrow$i>
1047 ";
1048
1049     map { print "\n$column_data{$_}" } @column_index;
1050
1051     print qq|
1052         </tr>
1053 |;
1054
1055   
1056   }
1057
1058   if ($form->{l_subtotal} eq 'Y') {
1059     &ar_subtotal;
1060   }
1061
1062   # print totals
1063   print qq|
1064         <tr class=listtotal>
1065 |;
1066
1067   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1068   
1069   $column_data{netamount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;")."</th>";
1070   $column_data{tax} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalnetamount, 2, "&nbsp;")."</th>";
1071   $column_data{amount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")."</th>";
1072   $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")."</th>";
1073   $column_data{due} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;")."</th>";
1074
1075   map { print "\n$column_data{$_}" } @column_index;
1076
1077   print qq|
1078         </tr>
1079       </table>
1080     </td>
1081   </tr>
1082   <tr>
1083     <td><hr size=3 noshade></td>
1084   </tr>
1085 </table>
1086
1087 <br>
1088 <form method=post action=$form->{script}>
1089
1090 <input name=callback type=hidden value="$form->{callback}">
1091
1092 <input type=hidden name=path value=$form->{path}>
1093 <input type=hidden name=login value=$form->{login}>
1094 <input type=hidden name=password value=$form->{password}>
1095
1096 <input class=submit type=submit name=action value="|.$locale->text('AR Transaction').qq|">
1097 <input class=submit type=submit name=action value="|.$locale->text('Sales Invoice').qq|">
1098
1099 </form>
1100
1101 </body>
1102 </html>
1103 |;
1104
1105 }
1106
1107
1108 sub ar_subtotal {
1109
1110   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1111   
1112   $column_data{tax} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount, 2, "&nbsp;")."</th>";
1113   $column_data{amount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;")."</th>";
1114   $column_data{paid} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;")."</th>";
1115   $column_data{due} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotaldue, 2, "&nbsp;")."</th>";
1116   
1117   $subtotalnetamount = 0;
1118   $subtotalamount = 0;
1119   $subtotalpaid = 0;
1120   $subtotaldue = 0;
1121
1122   $sameitem = $ar->{$form->{sort}};
1123   
1124   print "<tr class=listsubtotal>";
1125
1126   map { print "\n$column_data{$_}" } @column_index;
1127
1128 print "
1129 </tr>
1130 ";
1131  
1132 }
1133
1134
1135