can't set $p without $cgi
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / rc.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 # Account reconciliation module
25 #
26 #======================================================================
27
28
29 use SL::RC;
30
31
32 1;
33 # end of main
34
35
36 sub reconciliation {
37   
38   RC->paymentaccounts(\%myconfig, \%$form);
39
40   $selection = "";
41   map { $selection .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR} };
42
43   $form->{title} = $locale->text('Reconciliation');
44
45   $form->header;
46
47   print qq|
48 <body>
49
50 <form method=post action=$form->{script}>
51
52 <table width=100%>
53   <tr>
54     <th class=listtop>$form->{title}</th>
55   </tr>
56   <tr height="5"></tr>
57   <tr>
58     <td>
59       <table>
60         <tr>
61           <th align=right nowrap>|.$locale->text('Account').qq|</th>
62           <td colspan=3><select name=accno>$selection</select>
63           </td>
64         </tr>
65         <tr>
66           <th align=right>|.$locale->text('From').qq|</th>
67           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
68           <th align=right>|.$locale->text('to').qq|</th>
69           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
70         </tr>
71       </table>
72     </td>
73   </tr>
74   <tr>
75     <td><hr size=3 noshade></td>
76   </tr>
77 </table>
78
79 <br>
80 <input type=hidden name=nextsub value=get_payments>
81
82 <input type=hidden name=path value=$form->{path}>
83 <input type=hidden name=login value=$form->{login}>
84 <input type=hidden name=password value=$form->{password}>
85
86 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
87
88 </form>
89
90 </body>
91 </html>
92 |;
93
94 }
95
96
97 sub continue { &{ $form->{nextsub} } };
98
99
100 sub get_payments {
101
102   ($form->{accno}, $form->{account}) = split /--/, $form->{accno};
103
104   RC->payment_transactions(\%myconfig, \%$form);
105  
106   &display_form;
107
108 }
109
110
111 sub display_form {
112   
113   @column_index = qw(cleared transdate source name credit debit balance);
114   
115   $column_header{cleared} = "<th class=listheading>&nbsp;</th>";
116   $column_header{source} = "<th class=listheading>".$locale->text('Source')."</a></th>";
117   $column_header{name} = "<th class=listheading>".$locale->text('Description')."</a></th>";
118   $column_header{transdate} = "<th class=listheading>".$locale->text('Date')."</a></th>";
119
120   $column_header{debit} = "<th class=listheading>".$locale->text('Deposit')."</a></th>";
121   $column_header{credit} = "<th class=listheading>".$locale->text('Payment')."</a></th>";
122
123   $column_header{balance} = "<th class=listheading>".$locale->text('Balance')."</a></th>";
124
125   if ($form->{fromdate}) {
126     $option .= "\n<br>" if ($option);
127     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{fromdate}, 1);
128   }
129   if ($form->{todate}) {
130     $option .= "\n<br>" if ($option);
131     $option .= $locale->text('to')."&nbsp;".$locale->date(\%myconfig, $form->{todate}, 1);
132   }
133
134   $form->{title} = "$form->{accno}--$form->{account}";
135   
136   $form->header;
137
138   print qq|
139 <body>
140
141 <form method=post action=$form->{script}>
142
143 <table width=100%>
144   <tr>
145     <th class=listtop>$form->{title}</th>
146   </tr>
147   <tr height="5"></tr>
148   <tr>
149     <td>$option</td>
150   </tr>
151   <tr>
152     <td>
153       <table width=100%>
154         <tr>
155 |;
156
157   map { print "\n$column_header{$_}" } @column_index;
158
159   print qq|
160         </tr>
161 |;
162
163   $form->{beginningbalance} *= -1;
164   $clearedbalance = $balance = $form->{beginningbalance};
165   $i = 0;
166   $id = 0;
167
168   map { $column_data{$_} = "<td>&nbsp;</td>" } qw(cleared transdate source name debit credit);
169   $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
170   $j = 0;
171   print qq|
172         <tr class=listrow$j>
173 |;
174
175   map { print "\n$column_data{$_}" } @column_index;
176
177   print qq|
178         </tr>
179 |;
180   
181
182   foreach $ref (@{ $form->{PR} }) {
183
184     $balance += $ref->{amount} * -1;
185     $cleared += $ref->{amount} * -1 if $ref->{cleared};
186
187     $column_data{name} = "<td>$ref->{name}&nbsp;</td>";
188     $column_data{source} = qq|<td>$ref->{source}&nbsp;</a>
189     </td>|;
190     $column_data{transdate} = "<td>$ref->{transdate}&nbsp;</td>";
191     
192     $column_data{debit} = "<td>&nbsp;</td>";
193     $column_data{credit} = "<td>&nbsp;</td>";
194     
195     if ($ref->{amount} < 0) {
196       $totaldebits += $ref->{amount} * -1;
197       $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount} * -1, 2, "&nbsp;")."</td>";
198     } else {
199       $totalcredits += $ref->{amount};
200       $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount}, 2, "&nbsp;")."</td>";
201     }
202     
203     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
204
205     if ($ref->{fx_transaction}) {
206       $i++ unless $id == $ref->{id};
207       $fx_transaction = 1;
208       $fx += $ref->{amount} * -1;
209       $column_data{cleared} = qq|<td align=center>&nbsp;
210       <input type=hidden name="fxoid_$i" value=$ref->{oid}>
211       </td>|;
212     } else {
213       $i++ unless ($fx_transaction && $id == $ref->{id});
214       $fx_transaction = 0;
215       $column_data{cleared} = qq|<td>
216       <input name="cleared_$i" type=checkbox class=checkbox value=1 $ref->{cleared}>
217       <input type=hidden name="oid_$i" value=$ref->{oid}>
218       </td>|;
219     }
220     $id = $ref->{id};
221
222     $j++; $j %= 2;
223     print qq|
224         <tr class=listrow$j>
225 |;
226
227     map { print "\n$column_data{$_}" } @column_index;
228
229     print qq|
230         </tr>
231 |;
232
233   }
234
235   # print totals
236   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
237
238   $column_data{debit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebits, 2, "&nbsp;")."</th>";
239   $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredits, 2, "&nbsp;")."</th>";
240    
241   print qq|
242         <tr class=listtotal>
243 |;
244
245   map { print "\n$column_data{$_}" } @column_index;
246
247   $form->{statementbalance} = $form->parse_amount(\%myconfig, $form->{statementbalance});
248   $difference = $form->format_amount(\%myconfig, $form->{statementbalance} - $clearedbalance - $cleared, 2, 0);
249   
250   $form->{statementbalance} = $form->format_amount(\%myconfig, $form->{statementbalance}, 2, 0);
251
252   $clearedbalance = $form->format_amount(\%myconfig, $clearedbalance, 2, 0);
253   
254   if ($fx) {
255     $fx = $form->format_amount(\%myconfig, $fx, 2, 0);
256     $exchdiff = qq|
257                 <th align=right nowrap>|.$locale->text('Exchangerate Difference').qq|</th>
258                 <td width=10%></td>
259                 <td align=right>$fx</td>
260 |;
261   }
262
263   print qq|
264         </tr>
265       </table>
266     </td>
267   </tr>
268   <tr>
269     <td>
270       <table width=100%>
271         <tr valign=top>
272           <td>
273             <table>
274               <tr>
275                 <th align=right nowrap>|.$locale->text('Cleared Balance').qq|</th>
276                 <td width=10%></td>
277                 <td align=right>$clearedbalance</td>
278               </tr>
279               <tr>
280                 $exchdiff
281               </tr>
282             </table>
283           </td>
284           <td align=right>
285             <table>
286               <tr>
287                 <th align=right nowrap>|.$locale->text('Statement Balance').qq|</th>
288                 <td width=10%></td>
289                 <td align=right><input name=statementbalance size=11 value=$form->{statementbalance}></td>
290               </tr>
291               <tr>
292                 <th align=right nowrap>|.$locale->text('Difference').qq|</th>
293                 <td width=10%></td>
294                 <td align=right><input name=none size=11 value=$difference></td>
295                 <input type=hidden name=difference value=$difference>
296               </tr>
297             </table>
298           </td>
299         </tr>
300       </table>
301     </td>
302   </tr>
303   <tr>
304     <td><hr size=3 noshade></td>
305   </tr>
306 </table>
307
308 <input type=hidden name=rowcount value=$i>
309 <input type=hidden name=accno value=$form->{accno}>
310 <input type=hidden name=account value="$form->{account}">
311
312 <input type=hidden name=fromdate value=$form->{fromdate}>
313 <input type=hidden name=todate value=$form->{todate}>
314
315 <input type=hidden name=path value=$form->{path}>
316 <input type=hidden name=login value=$form->{login}>
317 <input type=hidden name=password value=$form->{password}>
318
319 <br>
320 <input type=submit class=submit name=action value="|.$locale->text('Update').qq|">
321 <input type=submit class=submit name=action value="|.$locale->text('Select all').qq|">
322 <input type=submit class=submit name=action value="|.$locale->text('Done').qq|">
323
324 </form>
325
326 </body>
327 </html>
328 |;
329
330 }
331
332
333 sub update {
334   
335   RC->payment_transactions(\%myconfig, \%$form);
336
337   foreach $ref (@{ $form->{PR} }) {
338     if (!$ref->{fx_transaction}) {
339       $i++;
340       $ref->{cleared} = "checked" if $form->{"cleared_$i"};
341     }
342   }
343
344   &display_form;
345   
346 }
347
348
349 sub select_all {
350   
351   RC->payment_transactions(\%myconfig, \%$form);
352
353   map { $_->{cleared} = "checked" unless $_->{fx_transaction} } @{ $form->{PR} };
354
355   &display_form;
356   
357 }
358
359
360 sub done {
361
362   $form->{callback} = "$form->{script}?path=$form->{path}&action=reconciliation&login=$form->{login}&password=$form->{password}";
363
364   $form->error($locale->text('Out of balance!')) if ($form->{difference} *= 1);
365
366   RC->reconcile(\%myconfig, \%$form);
367   $form->redirect;
368   
369 }
370
371