This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / sql-ledger / bin / mozilla / rc.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2003
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 # Account reconciliation module
25 #
26 #======================================================================
27
28 use SL::RC;
29
30 1;
31 # end of main
32
33 # this is for our long dates
34 # $locale->text('January')
35 # $locale->text('February')
36 # $locale->text('March')
37 # $locale->text('April')
38 # $locale->text('May ')
39 # $locale->text('June')
40 # $locale->text('July')
41 # $locale->text('August')
42 # $locale->text('September')
43 # $locale->text('October')
44 # $locale->text('November')
45 # $locale->text('December')
46
47 # this is for our short month
48 # $locale->text('Jan')
49 # $locale->text('Feb')
50 # $locale->text('Mar')
51 # $locale->text('Apr')
52 # $locale->text('May')
53 # $locale->text('Jun')
54 # $locale->text('Jul')
55 # $locale->text('Aug')
56 # $locale->text('Sep')
57 # $locale->text('Oct')
58 # $locale->text('Nov')
59 # $locale->text('Dec')
60
61
62 sub reconciliation {
63   
64   RC->paymentaccounts(\%myconfig, \%$form);
65
66   $selection = "";
67   map { $selection .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR} };
68
69   $form->{title} = $locale->text('Reconciliation');
70
71   if ($form->{report}) {
72     $form->{title} = $locale->text('Reconciliation Report');
73     $cleared = qq|
74         <input type=hidden name=report value=1>
75         <tr>
76           <td align=right><input type=checkbox style=checkbox name=outstanding value=1 checked></td>
77           <td>|.$locale->text('Outstanding').qq|</td>
78           <td align=right><input type=checkbox style=checkbox name=cleared value=1></td>
79           <td>|.$locale->text('Cleared').qq|</td>
80         </tr>
81 |;
82
83   }
84
85
86   # accounting years
87   $form->{selectaccountingyear} = "<option>\n";
88   map { $form->{selectaccountingyear} .= qq|<option>$_\n| } @{ $form->{all_years} };
89   $form->{selectaccountingmonth} = "<option>\n";
90   map { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| } sort keys %{ $form->{all_month} };
91
92   $selectfrom = qq|
93         <tr>
94         <th align=right>|.$locale->text('Period').qq|</th>
95         <td colspan=3>
96         <select name=month>$form->{selectaccountingmonth}</select>
97         <select name=year>$form->{selectaccountingyear}</select>
98         <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
99         <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
100         <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
101         <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
102         </td>
103       </tr>
104 |;
105
106
107   $form->header;
108
109   print qq|
110 <body>
111
112 <form method=post action=$form->{script}>
113
114 <table width=100%>
115   <tr>
116     <th class=listtop>$form->{title}</th>
117   </tr>
118   <tr height="5"></tr>
119   <tr>
120     <td>
121       <table>
122         <tr>
123           <th align=right nowrap>|.$locale->text('Account').qq|</th>
124           <td colspan=3><select name=accno>$selection</select>
125           </td>
126         </tr>
127         <tr>
128           <th align=right>|.$locale->text('From').qq|</th>
129           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
130           <th align=right>|.$locale->text('To').qq|</th>
131           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
132         </tr>
133         $selectfrom
134         $cleared
135         <tr>
136           <td align=right><input type=radio style=radio name=summary value=1 checked></td>
137           <td>|.$locale->text('Summary').qq|</td>
138           <td align=right><input type=radio style=radio name=summary value=0></td>
139           <td>|.$locale->text('Detail').qq|</td>
140         </tr>
141         <tr>
142           <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
143           <td colspan=3>|.$locale->text('Include Exchange Rate Difference').qq|</td>
144         </tr>
145       </table>
146     </td>
147   </tr>
148   <tr>
149     <td><hr size=3 noshade></td>
150   </tr>
151 </table>
152
153 <br>
154 <input type=hidden name=nextsub value=get_payments>
155
156 <input type=hidden name=path value=$form->{path}>
157 <input type=hidden name=login value=$form->{login}>
158 <input type=hidden name=sessionid value=$form->{sessionid}>
159
160 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
161
162 </form>
163
164 </body>
165 </html>
166 |;
167
168 }
169
170
171 sub continue { &{ $form->{nextsub} } };
172
173
174 sub get_payments {
175
176   ($form->{accno}, $form->{account}) = split /--/, $form->{accno};
177
178   RC->payment_transactions(\%myconfig, \%$form);
179   
180   $ml = ($form->{category} eq 'A') ? -1 : 1;
181   $form->{statementbalance} = $form->{endingbalance} * $ml;
182   if (! $form->{fx_transaction}) {
183     $form->{statementbalance} = ($form->{endingbalance} - $form->{fx_endingbalance}) * $ml;
184   }
185   
186   $form->{statementbalance} = $form->format_amount(\%myconfig, $form->{statementbalance}, 2, 0);
187   
188   &display_form;
189
190 }
191
192
193 sub display_form {
194   
195   if ($form->{report}) {
196     @column_index = qw(transdate source name cleared credit debit);
197   } else {
198     @column_index = qw(transdate source name cleared credit debit balance);
199   }
200   
201   $column_header{cleared} = qq|<th>|.$locale->text('R').qq|</th>|;
202   $column_header{source} = "<th class=listheading>".$locale->text('Source')."</a></th>";
203   $column_header{name} = "<th class=listheading>".$locale->text('Description')."</a></th>";
204   $column_header{transdate} = "<th class=listheading>".$locale->text('Date')."</a></th>";
205
206   if ($form->{category} eq 'A') {
207     $column_header{debit} = "<th class=listheading>".$locale->text('Deposit')."</a></th>";
208     $column_header{credit} = "<th class=listheading>".$locale->text('Payment')."</a></th>";
209   } else {
210     $column_header{debit} = "<th class=listheading>".$locale->text('Decrease')."</a></th>";
211     $column_header{credit} = "<th class=listheading>".$locale->text('Increase')."</a></th>";
212   }
213
214   $column_header{balance} = "<th class=listheading>".$locale->text('Balance')."</a></th>";
215
216   if ($form->{fromdate}) {
217     $option .= "\n<br>" if ($option);
218     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{fromdate}, 1);
219   }
220   if ($form->{todate}) {
221     $option .= "\n<br>" if ($option);
222     $option .= $locale->text('To')."&nbsp;".$locale->date(\%myconfig, $form->{todate}, 1);
223   }
224
225   $form->{title} = "$form->{accno}--$form->{account}";
226   
227   $form->header;
228
229   print qq|
230 <body>
231
232 <form method=post action=$form->{script}>
233
234 <table width=100%>
235   <tr>
236     <th class=listtop>$form->{title}</th>
237   </tr>
238   <tr height="5"></tr>
239   <tr>
240     <td>$option</td>
241   </tr>
242   <tr>
243     <td>
244       <table width=100%>
245         <tr class=listheading>
246 |;
247
248   map { print "\n$column_header{$_}" } @column_index;
249
250   print qq|
251         </tr>
252 |;
253
254   $ml = ($form->{category} eq 'A') ? -1 : 1;
255   $form->{beginningbalance} *= $ml;
256   $form->{fx_balance} *= $ml;
257   
258   if (! $form->{fx_transaction}) {
259     $form->{beginningbalance} -= $form->{fx_balance};
260   }
261   $balance = $form->{beginningbalance};
262   
263   $i = 0;
264   $j = 0;
265   
266   map { $column_data{$_} = "<td>&nbsp;</td>" } qw(cleared transdate source debit credit);
267
268   if (! $form->{report}) {
269   $column_data{name} = qq|<td>|.$locale->text('Beginning Balance').qq|</td>|;
270   $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
271   print qq|
272         <tr class=listrow$j>
273 |;
274
275   map { print "\n$column_data{$_}" } @column_index;
276
277   print qq|
278         </tr>
279 |;
280   }
281
282
283   foreach $ref (@{ $form->{PR} }) {
284
285     $i++;
286
287     if (! $form->{fx_transaction}) {
288       next if $ref->{fx_transaction};
289     }
290
291     $checked = ($ref->{cleared}) ? "checked" : "";
292     
293     $column_data{name} = "<td>";
294     map { $column_data{name} .= "$_<br>" } @{ $ref->{name} };
295     $column_data{name} .= "</td>";
296     $column_data{source} = qq|<td>$ref->{source}&nbsp;</td>
297     <input type=hidden name="id_$i" value=$ref->{id}>|;
298     
299     $column_data{debit} = "<td>&nbsp;</td>";
300     $column_data{credit} = "<td>&nbsp;</td>";
301     
302     $balance += $ref->{amount} * $ml;
303
304     if ($ref->{amount} < 0) {
305       
306       $totaldebits += $ref->{amount} * -1;
307
308       $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount} * -1, 2, "&nbsp;")."</td>";
309       
310     } else {
311       
312       $totalcredits += $ref->{amount};
313
314       $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount}, 2, "&nbsp;")."</td>";
315       
316     }
317     
318     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
319
320     if ($ref->{fx_transaction}) {
321
322       $column_data{cleared} = ($clearfx) ? qq|<td align=center>*</td>| : qq|<td>&nbsp;</td>|;
323       $cleared += $ref->{amount} * $ml if $clearfx;
324       
325     } else {
326       
327       if ($form->{report}) {
328         
329         if ($ref->{cleared}) {
330           $column_data{cleared} = qq|<td align=center>*</td>|;
331           $clearfx = 1;
332         } else {
333           $column_data{cleared} = qq|<td>&nbsp;</td>|;
334           $clearfx = 0;
335         }
336         
337       } else {
338
339         if ($ref->{oldcleared}) {
340           $cleared += $ref->{amount} * $ml;
341           $clearfx = 1;
342           $column_data{cleared} = qq|<td align=center>*</td>
343           <input type=hidden name="cleared_$i" value=$ref->{cleared}>
344           <input type=hidden name="oldcleared_$i" value=$ref->{oldcleared}>
345           <input type=hidden name="source_$i" value="$ref->{source}">|;
346         } else {
347           $cleared += $ref->{amount} * $ml if $checked;
348           $clearfx = ($checked) ? 1 : 0;
349           $column_data{cleared} = qq|<td align=center><input name="cleared_$i" type=checkbox class=checkbox value=1 $checked>
350           <input type=hidden name="source_$i" value="$ref->{source}"></td>|;
351         }
352         
353       }
354     }
355     
356     $column_data{transdate} = qq|<td>$ref->{transdate}&nbsp;</td>
357     <input type=hidden name="transdate_$i" value=$ref->{transdate}>|;
358
359     $j++; $j %= 2;
360     print qq|
361         <tr class=listrow$j>
362 |;
363
364     map { print "\n$column_data{$_}" } @column_index;
365
366     print qq|
367         </tr>
368 |;
369
370   }
371
372   $form->{rowcount} = $i;
373   
374   # print totals
375   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
376
377   $column_data{debit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebits, 2, "&nbsp;")."</th>";
378   $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredits, 2, "&nbsp;")."</th>";
379    
380   print qq|
381         <tr class=listtotal>
382 |;
383
384   map { print "\n$column_data{$_}" } @column_index;
385  
386   $form->{statementbalance} = $form->parse_amount(\%myconfig, $form->{statementbalance});
387   $difference = $form->format_amount(\%myconfig, $form->{beginningbalance} + $cleared - $form->{statementbalance}, 2, 0);
388   $form->{statementbalance} = $form->format_amount(\%myconfig, $form->{statementbalance}, 2, 0);
389
390   print qq|
391         </tr>
392       </table>
393     </td>
394   </tr>
395 |;
396
397   
398   if ($form->{report}) {
399
400     print qq|
401     </tr>
402   </table>
403 |;
404
405   } else {
406     
407     print qq|
408    
409   <tr>
410     <td>
411       <table width=100%>
412         <tr>
413           <td align=right>
414             <table>
415               <tr>
416                 <th align=right nowrap>|.$locale->text('Statement Balance').qq|</th>
417                 <td width=10%></td>
418                 <td align=right><input name=statementbalance size=11 value=$form->{statementbalance}></td>
419               </tr>
420               <tr>
421                 <th align=right nowrap>|.$locale->text('Difference').qq|</th>
422                 <td width=10%></td>
423                 <td align=right><input name=null size=11 value=$difference></td>
424                 <input type=hidden name=difference value=$difference>
425               </tr>
426             </table>
427           </td>
428         </tr>
429       </table>
430     </td>
431   </tr>
432   <tr>
433     <td><hr size=3 noshade></td>
434   </tr>
435 </table>
436
437 <input type=hidden name=fx_transaction value=$form->{fx_transaction}>
438 <input type=hidden name=summary value=$form->{summary}>
439
440 <input type=hidden name=rowcount value=$form->{rowcount}>
441 <input type=hidden name=accno value="$form->{accno}">
442 <input type=hidden name=account value="$form->{account}">
443
444 <input type=hidden name=fromdate value=$form->{fromdate}>
445 <input type=hidden name=todate value=$form->{todate}>
446
447 <input type=hidden name=path value=$form->{path}>
448 <input type=hidden name=login value=$form->{login}>
449 <input type=hidden name=sessionid value=$form->{sessionid}>
450
451 <br>
452 <input type=submit class=submit name=action value="|.$locale->text('Update').qq|">
453 <input type=submit class=submit name=action value="|.$locale->text('Select all').qq|">
454 <input type=submit class=submit name=action value="|.$locale->text('Done').qq|">|;
455   }
456
457   if ($form->{menubar}) {
458     require "$form->{path}/menu.pl";
459     &menubar;
460   }
461
462   print qq|
463 </form>
464
465 </body>
466 </html>
467 |;
468
469 }
470
471
472 sub update {
473   
474   RC->payment_transactions(\%myconfig, \%$form);
475
476   $i = 0;
477   foreach $ref (@{ $form->{PR} }) {
478     $i++;
479     $ref->{cleared} = ($form->{"cleared_$i"}) ? 1 : 0;
480   }
481
482   &display_form;
483   
484 }
485
486
487 sub select_all {
488   
489   RC->payment_transactions(\%myconfig, \%$form);
490
491   map { $_->{cleared} = 1 } @{ $form->{PR} };
492
493   &display_form;
494   
495 }
496
497
498 sub done {
499
500   $form->{callback} = "$form->{script}?path=$form->{path}&action=reconciliation&login=$form->{login}&sessionid=$form->{sessionid}";
501
502   $form->error($locale->text('Out of balance!')) if ($form->{difference} *= 1);
503
504   RC->reconcile(\%myconfig, \%$form);
505   $form->redirect;
506   
507 }
508
509