Kontenabgleich - Bug bei Differenz behoben und Feld read-only
[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($::form->{"nextsub"}); }
59
60 sub get_payments {
61   $::lxdebug->enter_sub;
62   $::auth->assert('cash');
63
64   ($::form->{accno}, $::form->{account}) = split /--/, $::form->{accno};
65
66   RC->payment_transactions(\%::myconfig, $::form);
67
68   display_form();
69
70   $::lxdebug->leave_sub;
71 }
72
73 sub display_form {
74   $::lxdebug->enter_sub;
75   $::auth->assert('cash');
76
77   my @options;
78   push @options, $::locale->text('From') . " " . $::locale->date(\%::myconfig, $::form->{fromdate}, 0) if $::form->{fromdate};
79   push @options, $::locale->text('Until') . " " . $::locale->date(\%::myconfig, $::form->{todate}, 0) if $::form->{todate};
80
81   my $ml = ($::form->{category} eq 'A') ? -1 : 1;
82   my $beginningbalance = $::form->{beginningbalance} * $ml;
83   my $clearedbalance   =
84   my $balance          = $beginningbalance;
85   my $i                = 0;
86   my $last_id          = 0;
87   my ($last_fx, @rows, $cleared, $totaldebits, $totalcredits, $fx);
88
89   for my $ref (@{ $::form->{PR} }) {
90     $balance      += $ref->{amount} * $ml;
91     $cleared      += $ref->{amount} * $ml if $ref->{cleared};
92     $totaldebits  += $ref->{amount} * -1  if $ref->{amount} < 0;
93     $totalcredits += $ref->{amount}       if $ref->{amount} >= 0;
94     $fx           += $ref->{amount} * $ml if $ref->{fx_transaction};
95     $i++                                  if (!$ref->{fx_transaction} && !$last_fx) || $last_id != $ref->{id};
96     $last_fx       = $ref->{fx_transaction};
97     $last_id       = $ref->{id};
98
99     push @rows, { %$ref, balance => $balance, i => $i };
100   }
101
102   my $statementbalance = $::form->parse_amount(\%::myconfig, $::form->{statementbalance});
103   my $difference       = $statementbalance - $clearedbalance - $cleared;
104
105   $::form->header;
106   print $::form->parse_html_template('rc/step2', {
107     is_asset         => $::form->{category} eq 'A',
108     option           => \@options,
109     DATA             => \@rows,
110     total            => {
111       credit => $totalcredits,
112       debit  => $totaldebits,
113     },
114     balance          => {
115       beginning => $beginningbalance,
116       cleared   => $clearedbalance,
117       statement => $statementbalance,
118     },
119     difference       => $difference,
120     rowcount         => $i,
121     fx               => $fx,
122   });
123
124   $::lxdebug->leave_sub;
125 }
126
127 sub update {
128   $::lxdebug->enter_sub;
129   $::auth->assert('cash');
130
131   # reset difference as it doesn't always arrive here empty
132   $::form->{difference} = 0;
133
134   RC->payment_transactions(\%::myconfig, $::form);
135
136   my $i;
137   for my $ref (@{ $::form->{PR} }) {
138     next if $ref->{fx_transaction};
139     $i++;
140     $ref->{cleared} = $::form->{"cleared_$i"};
141   }
142
143   display_form();
144
145   $::lxdebug->leave_sub;
146 }
147
148 sub done {
149   $::lxdebug->enter_sub;
150   $::auth->assert('cash');
151
152   $::form->{callback} = "$::form->{script}?action=reconciliation";
153
154   $::form->error($::locale->text('Out of balance!')) if $::form->{difference} *= 1;
155
156   RC->reconcile(\%::myconfig, $::form);
157   $::form->redirect;
158
159   $::lxdebug->leave_sub;
160 }
161