This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / cp.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 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 # Payment module
25 #
26 #======================================================================
27
28
29 use SL::CP;
30 use SL::IS;
31 use SL::IR;
32
33 require "$form->{path}/arap.pl";
34
35 1;
36 # end of main
37
38
39 sub payment {
40   
41   # setup customer/vendor selection for open invoices
42   CP->get_openvc(\%myconfig, \%$form);
43
44   if ($form->{"all_$form->{vc}"}) {
45     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{"all_$form->{vc}"} };
46   }
47
48   $form->{arap} = ($form->{vc} eq 'customer') ? "AR" : "AP";
49
50   CP->paymentaccounts(\%myconfig, \%$form);
51
52   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR} };
53
54   # currencies
55   @curr = split /:/, $form->{currencies};
56   chomp $curr[0];
57   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} = $curr[0];
58
59   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
60
61   $form->{media} = "screen";
62
63   &form_header;
64   &list_invoices;
65   &form_footer;
66
67 }
68
69
70
71 sub form_header {
72
73   $vclabel = ucfirst $form->{vc};
74   $vclabel = $locale->text($vclabel);
75
76   if ($form->{vc} eq 'customer') {
77     $form->{title} = $locale->text('Receipt');
78     $rclabel = $locale->text('Reference');
79     $form->{type} = 'receipt';
80   } else {
81     $form->{title} = $locale->text('Payment');
82     $rclabel = $locale->text('Check');
83     $form->{type} = 'check';
84   }
85
86 # $locale->text('Customer')
87 # $locale->text('Vendor')
88
89   if ($form->{$form->{vc}} eq "") {
90     map { $form->{"addr$_"} = "" } (1 .. 4);
91   }
92
93   if ($form->{currency} ne $form->{defaultcurrency}) {
94     $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
95     if ($form->{forex}) {
96       $exchangerate = qq|
97               <tr>
98                 <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
99                 <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
100               </tr>
101 |;
102     } else {
103       $exchangerate = qq|
104               <tr>
105                 <th align=right nowrap>|.$locale->text('Exchangerate').qq|</th>
106                 <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
107               </tr>
108 |;
109     }
110   }
111   
112   foreach $item ($form->{vc}, account, currency) {
113     $form->{"select$item"} =~ s/ selected//;
114     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
115   }
116
117   $vc = ($form->{"select$form->{vc}"}) ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>| : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
118
119   $form->header;
120
121   print qq|
122 <body>
123
124 <form method=post action=$form->{script}>
125
126 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
127 <input type=hidden name=closedto value=$form->{closedto}>
128 <input type=hidden name=vc value=$form->{vc}>
129 <input type=hidden name=type value=$form->{type}>
130
131 <table border=0 width=100%>
132   <tr>
133     <th class=listtop>$form->{title}</th>
134   </tr>
135   <tr height="5"></tr>
136   <tr>
137     <td>
138       <table width=100%>
139         <tr valign=top>
140           <td>
141             <table>
142               <tr>
143                 <th align=right>$vclabel</th>
144                 <td>$vc</td>
145                 <input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">
146                 <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
147                 <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
148               </tr>
149               <tr valign=top>
150                 <th align=right nowrap>|.$locale->text('Address').qq|</th>
151                 <td>
152                   <table>
153                     <tr>
154                       <td>$form->{addr1}</td>
155                     </tr>
156                     <tr>
157                       <td>$form->{addr2}</td>
158                     </tr>
159                     <tr>
160                       <td>$form->{addr3}</td>
161                     </tr>
162                     <tr>
163                       <td>$form->{addr4}</td>
164                     </tr>
165                   </table>
166                 </td>
167                 <input type=hidden name=addr1 value="$form->{addr1}">
168                 <input type=hidden name=addr2 value="$form->{addr2}">
169                 <input type=hidden name=addr3 value="$form->{addr3}">
170                 <input type=hidden name=addr4 value="$form->{addr4}">
171               </tr>
172             </table>
173           </td>
174           <td align=right>
175             <table>
176               <tr>
177                 <th align=right nowrap>|.$locale->text('Account').qq|</th>
178                 <td colspan=3><select name=account>$form->{selectaccount}</select>
179                 <input type=hidden name=selectaccount value="$form->{selectaccount}">
180                 </td>
181               </tr>
182               <tr>
183                 <th align=right nowrap>|.$locale->text('Date').qq|</th>
184                 <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
185               </tr>
186               <tr>
187                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
188                 <td><select name=currency>$form->{selectcurrency}</select></td>
189                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
190                 <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
191               </tr>
192               $exchangerate
193               <tr>
194                 <th align=right nowrap>$rclabel</th>
195                 <td colspan=3><input name=source value="$form->{source}" size=10></td>
196               </tr>
197               <tr>
198                 <th align=right nowrap>|.$locale->text('Amount').qq|</th>
199                 <td colspan=3><input name=amount size=10 value=|.$form->format_amount(\%myconfig, $form->{amount}, 2).qq|></td>
200               </tr>
201               <tr>
202                 <th align=right nowrap>|.$locale->text('From').qq|</th>
203                 <td><input name=transdatefrom size=11 title="$myconfig{dateformat}" value=$form->{transdatefrom}></td>
204                 <th align=right nowrap>|.$locale->text('to').qq|</th>
205                 <td><input name=transdateto size=11 title="$myconfig{dateformat}" value=$form->{transdateto}></td>
206                 <input type=hidden name=oldtransdatefrom value=$form->{oldtransdatefrom}>
207                 <input type=hidden name=oldtransdateto value=$form->{oldtransdateto}>
208               </tr>
209             </table>
210           </td>
211         </tr>
212       </table>
213     </td>
214   </tr>
215 |;
216
217 }
218
219
220 sub list_invoices {
221
222   @column_index = qw(invnumber transdate amount due paid selectpaid);
223   
224   $colspan = $#column_index + 1;
225   
226   print qq|
227   <input type=hidden name=column_index value="id @column_index">
228   <tr>
229     <td>
230       <table width=100%>
231         <tr>
232           <th class=listheading colspan=$colspan>|.$locale->text('Invoices').qq|</th>
233         </tr>
234 |;
235
236   $column_data{invnumber} = qq|<th nowrap>|.$locale->text('Invoice')."</th>";
237   $column_data{transdate} = qq|<th nowrap>|.$locale->text('Date')."</th>";
238   $column_data{amount} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
239   $column_data{due} = qq|<th nowrap>|.$locale->text('Due')."</th>";
240   $column_data{paid} = qq|<th nowrap>|.$locale->text('Applied')."</th>";
241   $column_data{selectpaid} = qq|<th nowrap>|.$locale->text('Paid in full')."</th>";
242   
243   print qq|
244         <tr>
245 |;
246   map { print "$column_data{$_}\n" } @column_index;
247   print qq|
248         </tr>
249 |;
250
251   for $i (1 .. $form->{rowcount}) {
252
253     $form->{"selectpaid_$i"} = "checked" if $form->{"selectpaid_$i"};
254     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
255     
256     $totalamount += $form->{"amount_$i"};
257     $totaldue += $form->{"due_$i"};
258     $totalpaid += $form->{"paid_$i"};
259
260     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
261
262     $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
263       <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
264       <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
265     $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
266       <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
267     $column_data{amount} = qq|<td align=right width=15%>$form->{"amount_$i"}</td>
268       <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
269     $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
270       <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
271
272     $column_data{paid} = qq|<td align=right width=15%>|;
273     if ($form->{"selectpaid_$i"}) {
274       $column_data{paid} .= qq|<input type=hidden name="paid_$i" value=$form->{"paid_$i"}>$form->{"paid_$i"}</td>|;
275     } else {
276       $column_data{paid} .= qq|<input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
277     }
278     $column_data{selectpaid} = qq|<td align=center width=10%><input name="selectpaid_$i" type=checkbox class=checkbox $form->{"selectpaid_$i"}></td>|;
279
280     $j++; $j %= 2;
281     print qq|
282         <tr class=listrow$j>
283 |;
284     map { print "$column_data{$_}\n" } @column_index;
285     print qq|
286         </tr>
287 |;
288   }
289
290   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
291
292   $column_data{amount} = qq|<th align=right>|.$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;").qq|</th>|;
293   $column_data{due} = qq|<th align=right>|.$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;").qq|</th>|;
294   $column_data{paid} = qq|<th align=right>|.$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;").qq|</th>|;
295
296   print qq|
297         <tr class=listtotal>
298 |;
299   map { print "$column_data{$_}\n" } @column_index;
300   print qq|
301         </tr>
302       </table>
303     </td>
304   </tr>
305 |;
306
307 }
308
309
310 sub form_footer {
311
312   $form->{OP}{$form->{media}} = "checked";
313   
314   print qq|
315   <tr>
316     <td><hr size=3 noshade></td>
317   </tr>
318 </table>
319 <input type=hidden name=rowcount value=$form->{rowcount}>
320
321 <input type=hidden name=path value=$form->{path}>
322 <input type=hidden name=login value=$form->{login}>
323 <input type=hidden name=password value=$form->{password}>
324
325 <br>
326 <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
327 <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
328
329   if ($latex) {
330     print qq|
331 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
332 <input class=radio type=radio name=media value=screen $form->{OP}{screen}> |.$locale->text('Screen');
333
334     if ($myconfig{printer}) {
335       print qq|
336 <input class=radio type=radio name=media value=printer $form->{OP}{printer}> |.$locale->text('Printer');
337     }
338   }
339
340   print qq|
341
342 </form>
343
344 </body>
345 </html>
346 |;
347
348 }
349
350
351 sub update {
352   my ($new_name_selected) = @_;
353
354   # get customer and invoices
355   $updated = &check_name($form->{vc});
356
357   $updated = 1 if (($form->{oldtransdatefrom} ne $form->{transdatefrom}) || ($form->{oldtransdateto} ne $form->{transdateto}));
358   $form->{oldtransdatefrom} = $form->{transdatefrom};
359   $form->{oldtransdateto} = $form->{transdateto};
360   
361   if ($new_name_selected || $updated) {
362     CP->get_openinvoices(\%myconfig, \%$form);
363     $updated = 1;
364   }
365
366   if ($form->{currency} ne $form->{oldcurrency}) {
367     $form->{oldcurrency} = $form->{currency};
368     if (!$updated) {
369       CP->get_openinvoices(\%myconfig, \%$form);
370       $updated = 1;
371     }
372   }
373   
374   # check currency
375   $buysell = ($form->{vc} eq 'customer') ? "buy" : "sell";
376   
377   $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, $buysell)));
378
379   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
380   
381   if ($updated) {
382     $form->{rowcount} = 0;
383     
384     $i = 0;
385     foreach $ref (@{ $form->{PR} }) {
386       $i++;
387       $form->{"id_$i"} = $ref->{id};
388       $form->{"invnumber_$i"} = $ref->{invnumber};
389       $form->{"transdate_$i"} = $ref->{transdate};
390       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
391       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
392       $form->{"due_$i"} = $form->round_amount(($ref->{amount} - $ref->{paid}) / $ref->{exchangerate}, 2);
393       $amount = $form->round_amount($amount - $form->{"due_$i"}, 2);
394       $form->{"selectpaid_$i"} = 1 if $amount > 0;
395
396       map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
397
398     }
399     $form->{rowcount} = $i;
400   }
401
402   # recalculate
403   $amount = $form->{amount};
404   for $i (1 .. $form->{rowcount}) {
405
406     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(amount due paid);
407
408     if ($form->{"selectpaid_$i"}) {
409       $amount -= $form->{"due_$i"};
410       
411       if ($amount < 0) {
412         $form->{"selectpaid_$i"} = 0;
413       } else {
414         $form->{"paid_$i"} = $form->{"due_$i"};
415       }
416     }
417
418     map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(amount due paid);
419
420   }
421
422   &form_header;
423   &list_invoices;
424   &form_footer;
425   
426 }
427
428
429 sub post {
430   
431   &check_form;
432
433   $form->redirect($locale->text('Payment posted!')) if (CP->process_payment(\%myconfig, \%$form));
434   $form->error($locale->text('Cannot post payment!'));
435
436 }
437
438
439 sub print {
440  
441   &check_form;
442
443   ($whole, $form->{decimal}) = split /\./, $form->{amount};
444   
445   $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
446   $m = "*" x (24 - length $form->{amount});
447   $form->{amount} = $locale->text($form->{currency})."$m$form->{amount}";
448   
449   $form->{decimal} .= "00";
450   $form->{decimal} = substr($form->{decimal}, 0, 2);
451
452   $check = new CP $myconfig{countrycode};
453   $check->init;
454   $form->{text_amount} = $check->num2text($whole);
455
456   &{ "$form->{vc}_details" };
457
458   $form->{format} = ($form->{media} eq 'screen') ? "pdf" : "postscript";
459   $form->{templates} = "$myconfig{templates}";
460   $form->{IN} = "$form->{type}.tex";
461   $form->{OUT} = "| $myconfig{printer}" if ($form->{media} eq 'printer');
462
463   $form->{company} = $myconfig{company};
464   $form->{address} = $myconfig{address};
465  
466   @a = qw(name invnumber company address text_amount addr1 addr2 addr3 addr4);
467   $form->format_string(@a);
468
469   $form->parse_template(\%myconfig, $userspath);
470
471   $form->{callback} = "";
472
473   $label = uc $form->{type};
474
475 # $locale->text('Check printed!')
476 # $locale->text('Check printing failed!')
477 # $locale->text('Receipt printed!')
478 # $locale->text('Receipt printing failed!')
479
480   $form->redirect($locale->text("$label printed!"));
481   $form->error($locale->text("$label printing failed!"));
482   
483 }
484
485
486 sub customer_details { IS->customer_details(\%myconfig, \%$form) };
487 sub vendor_details { IR->vendor_details(\%myconfig, \%$form) };
488   
489
490 sub check_form {
491   
492   # construct callback
493   $form->{callback} = "$form->{script}?action=payment&vc=$form->{vc}&path=$form->{path}&login=$form->{login}&password=$form->{password}";
494
495   $form->redirect unless $form->{rowcount};
496
497   if ($form->{currency} ne $form->{oldcurrency}) {
498     &update;
499     exit;
500   }
501   
502   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
503   $form->error($locale->text('Amount missing!')) unless $form->{amount};
504
505   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
506   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
507   
508   $form->error($locale->text('Cannot process payment for a closed period!')) if ($datepaid <= $closedto);
509
510   $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
511   for $i (1 .. $form->{rowcount}) {
512     $totalpaid += $form->parse_amount(\%myconfig, $form->{"paid_$i"});
513     if ($form->{"paid_$i"}) {
514       push(@{ $form->{paid} }, $form->{"paid_$i"});
515       push(@{ $form->{due} }, $form->{"due_$i"});
516       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
517       push(@{ $form->{invdate} }, $form->{"transdate_$i"});
518     }
519   }
520
521   $totalpaid = $form->round_amount($totalpaid, 2);
522
523   $form->error($locale->text('Nothing applied!')) unless $totalpaid;
524   $form->error($locale->text('Amount does not equal applied!')) if ($form->{amount} != $totalpaid);
525
526 }
527
528