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