unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / bin / mozilla / rc.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Account reconciliation module
31 #
32 #======================================================================
33
34
35 use SL::RC;
36
37
38 1;
39 # end of main
40
41
42 sub reconciliation {
43   $lxdebug->enter_sub();
44   
45   RC->paymentaccounts(\%myconfig, \%$form);
46
47   $selection = "";
48   map { $selection .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{PR} };
49
50   $form->{title} = $locale->text('Reconciliation');
51
52   $form->header;
53
54   print qq|
55 <body>
56
57 <form method=post action=$form->{script}>
58
59 <table width=100%>
60   <tr>
61     <th class=listtop>$form->{title}</th>
62   </tr>
63   <tr height="5"></tr>
64   <tr>
65     <td>
66       <table>
67         <tr>
68           <th align=right nowrap>|.$locale->text('Account').qq|</th>
69           <td colspan=3><select name=accno>$selection</select>
70           </td>
71         </tr>
72         <tr>
73           <th align=right>|.$locale->text('From').qq|</th>
74           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
75           <th align=right>|.$locale->text('To').qq|</th>
76           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
77         </tr>
78       </table>
79     </td>
80   </tr>
81   <tr>
82     <td><hr size=3 noshade></td>
83   </tr>
84 </table>
85
86 <br>
87 <input type=hidden name=nextsub value=get_payments>
88
89 <input type=hidden name=path value=$form->{path}>
90 <input type=hidden name=login value=$form->{login}>
91 <input type=hidden name=password value=$form->{password}>
92
93 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
94
95 </form>
96
97 </body>
98 </html>
99 |;
100
101   $lxdebug->leave_sub();
102 }
103
104
105 sub continue { &{ $form->{nextsub} } };
106
107
108 sub get_payments {
109   $lxdebug->enter_sub();
110
111   ($form->{accno}, $form->{account}) = split /--/, $form->{accno};
112
113   RC->payment_transactions(\%myconfig, \%$form);
114  
115   &display_form;
116
117   $lxdebug->leave_sub();
118 }
119
120
121 sub display_form {
122   $lxdebug->enter_sub();
123   
124   @column_index = qw(cleared transdate source name credit debit balance);
125   
126   $column_header{cleared} = "<th>&nbsp;</th>";
127   $column_header{source} = "<th class=listheading>".$locale->text('Source')."</a></th>";
128   $column_header{name} = "<th class=listheading>".$locale->text('Description')."</a></th>";
129   $column_header{transdate} = "<th class=listheading>".$locale->text('Date')."</a></th>";
130
131   if ($form->{category} eq 'A') {
132     $column_header{debit} = "<th class=listheading>".$locale->text('Deposit')."</a></th>";
133     $column_header{credit} = "<th class=listheading>".$locale->text('Payment')."</a></th>";
134   } else {
135     $column_header{debit} = "<th class=listheading>".$locale->text('Decrease')."</a></th>";
136     $column_header{credit} = "<th class=listheading>".$locale->text('Increase')."</a></th>";
137   }
138
139   $column_header{balance} = "<th class=listheading>".$locale->text('Balance')."</a></th>";
140
141   if ($form->{fromdate}) {
142     $option .= "\n<br>" if ($option);
143     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{fromdate}, 1);
144   }
145   if ($form->{todate}) {
146     $option .= "\n<br>" if ($option);
147     $option .= $locale->text('To')."&nbsp;".$locale->date(\%myconfig, $form->{todate}, 1);
148   }
149
150   $form->{title} = "$form->{accno}--$form->{account}";
151   
152   $form->header;
153
154   print qq|
155 <body>
156
157 <form method=post action=$form->{script}>
158
159 <table width=100%>
160   <tr>
161     <th class=listtop>$form->{title}</th>
162   </tr>
163   <tr height="5"></tr>
164   <tr>
165     <td>$option</td>
166   </tr>
167   <tr>
168     <td>
169       <table width=100%>
170         <tr class=listheading>
171 |;
172
173   map { print "\n$column_header{$_}" } @column_index;
174
175   print qq|
176         </tr>
177 |;
178
179   $ml = ($form->{category} eq 'A') ? -1 : 1;
180   $form->{beginningbalance} *= $ml;
181   $clearedbalance = $balance = $form->{beginningbalance};
182   $i = 0;
183   $id = 0;
184   
185   map { $column_data{$_} = "<td>&nbsp;</td>" } qw(cleared transdate source name debit credit);
186   $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
187   $j = 0;
188   print qq|
189         <tr class=listrow$j>
190 |;
191
192   map { print "\n$column_data{$_}" } @column_index;
193
194   print qq|
195         </tr>
196 |;
197
198
199   foreach $ref (@{ $form->{PR} }) {
200
201     $balance += $ref->{amount} * $ml;
202     $cleared += $ref->{amount} * $ml if $ref->{cleared};
203
204     $column_data{name} = "<td>$ref->{name}&nbsp;</td>";
205     $column_data{source} = qq|<td>$ref->{source}&nbsp;</a>
206     </td>|;
207     $column_data{transdate} = "<td>$ref->{transdate}&nbsp;</td>";
208     
209     $column_data{debit} = "<td>&nbsp;</td>";
210     $column_data{credit} = "<td>&nbsp;</td>";
211     
212     if ($ref->{amount} < 0) {
213       $totaldebits += $ref->{amount} * -1;
214       $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount} * -1, 2, "&nbsp;")."</td>";
215     } else {
216       $totalcredits += $ref->{amount};
217       $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount}, 2, "&nbsp;")."</td>";
218     }
219     
220     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $balance, 2, 0)."</td>";
221
222     if ($ref->{fx_transaction}) {
223       $i++ unless $id == $ref->{id};
224       $fx_transaction = 1;
225       $fx += $ref->{amount} * $ml;
226       $column_data{cleared} = qq|<td align=center>&nbsp;
227       <input type=hidden name="fxoid_$i" value=$ref->{oid}>
228       </td>|;
229     } else {
230       $i++ unless ($fx_transaction && $id == $ref->{id});
231       $fx_transaction = 0;
232       $column_data{cleared} = qq|<td>
233       <input name="cleared_$i" type=checkbox class=checkbox value=1 $ref->{cleared}>
234       <input type=hidden name="oid_$i" value=$ref->{oid}>
235       </td>|;
236     }
237     $id = $ref->{id};
238
239     $j++; $j %= 2;
240     print qq|
241         <tr class=listrow$j>
242 |;
243
244     map { print "\n$column_data{$_}" } @column_index;
245
246     print qq|
247         </tr>
248 |;
249
250   }
251
252   # print totals
253   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
254
255   $column_data{debit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebits, 2, "&nbsp;")."</th>";
256   $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredits, 2, "&nbsp;")."</th>";
257    
258   print qq|
259         <tr class=listtotal>
260 |;
261
262   map { print "\n$column_data{$_}" } @column_index;
263
264   $form->{statementbalance} = $form->parse_amount(\%myconfig, $form->{statementbalance});
265   $difference = $form->format_amount(\%myconfig, $form->{statementbalance} - $clearedbalance - $cleared, 2, 0);
266   
267   $form->{statementbalance} = $form->format_amount(\%myconfig, $form->{statementbalance}, 2, 0);
268
269   $clearedbalance = $form->format_amount(\%myconfig, $clearedbalance, 2, 0);
270   
271   if ($fx) {
272     $fx = $form->format_amount(\%myconfig, $fx, 2, 0);
273     $exchdiff = qq|
274                 <th align=right nowrap>|.$locale->text('Exchangerate Difference').qq|</th>
275                 <td width=10%></td>
276                 <td align=right>$fx</td>
277 |;
278   }
279
280   print qq|
281         </tr>
282       </table>
283     </td>
284   </tr>
285   <tr>
286     <td>
287       <table width=100%>
288         <tr valign=top>
289           <td>
290             <table>
291               <tr>
292                 <th align=right nowrap>|.$locale->text('Cleared Balance').qq|</th>
293                 <td width=10%></td>
294                 <td align=right>$clearedbalance</td>
295               </tr>
296               <tr>
297                 $exchdiff
298               </tr>
299             </table>
300           </td>
301           <td align=right>
302             <table>
303               <tr>
304                 <th align=right nowrap>|.$locale->text('Statement Balance').qq|</th>
305                 <td width=10%></td>
306                 <td align=right><input name=statementbalance size=11 value=$form->{statementbalance}></td>
307               </tr>
308               <tr>
309                 <th align=right nowrap>|.$locale->text('Difference').qq|</th>
310                 <td width=10%></td>
311                 <td align=right><input name=null size=11 value=$difference></td>
312                 <input type=hidden name=difference value=$difference>
313               </tr>
314             </table>
315           </td>
316         </tr>
317       </table>
318     </td>
319   </tr>
320   <tr>
321     <td><hr size=3 noshade></td>
322   </tr>
323 </table>
324
325 <input type=hidden name=rowcount value=$i>
326 <input type=hidden name=accno value=$form->{accno}>
327 <input type=hidden name=account value="$form->{account}">
328
329 <input type=hidden name=fromdate value=$form->{fromdate}>
330 <input type=hidden name=todate value=$form->{todate}>
331
332 <input type=hidden name=path value=$form->{path}>
333 <input type=hidden name=login value=$form->{login}>
334 <input type=hidden name=password value=$form->{password}>
335
336 <br>
337 <input type=submit class=submit name=action value="|.$locale->text('Update').qq|">
338 <input type=submit class=submit name=action value="|.$locale->text('Select all').qq|">
339 <input type=submit class=submit name=action value="|.$locale->text('Done').qq|">|;
340
341   if ($form->{menubar}) {
342     require "$form->{path}/menu.pl";
343     &menubar;
344   }
345
346   print qq|
347 </form>
348
349 </body>
350 </html>
351 |;
352
353   $lxdebug->leave_sub();
354 }
355
356
357 sub update {
358   $lxdebug->enter_sub();
359   
360   RC->payment_transactions(\%myconfig, \%$form);
361
362   foreach $ref (@{ $form->{PR} }) {
363     if (!$ref->{fx_transaction}) {
364       $i++;
365       $ref->{cleared} = ($form->{"cleared_$i"}) ? "checked" : "";
366     }
367   }
368
369   &display_form;
370   
371   $lxdebug->leave_sub();
372 }
373
374
375 sub select_all {
376   $lxdebug->enter_sub();
377   
378   RC->payment_transactions(\%myconfig, \%$form);
379
380   map { $_->{cleared} = "checked" unless $_->{fx_transaction} } @{ $form->{PR} };
381
382   &display_form;
383   
384   $lxdebug->leave_sub();
385 }
386
387
388 sub done {
389   $lxdebug->enter_sub();
390
391   $form->{callback} = "$form->{script}?path=$form->{path}&action=reconciliation&login=$form->{login}&password=$form->{password}";
392
393   $form->error($locale->text('Out of balance!')) if ($form->{difference} *= 1);
394
395   RC->reconcile(\%myconfig, \%$form);
396   $form->redirect;
397   
398   $lxdebug->leave_sub();
399 }
400
401