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