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