f0ef98e732687c5a062f376cd0b60600869abf3a
[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., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # Account reconciliation module
32 #
33 #======================================================================
34
35 use SL::RC;
36 use SL::Locale::String qw(t8);
37
38 require "bin/mozilla/common.pl";
39
40 use strict;
41
42 1;
43
44 # end of main
45
46 sub reconciliation {
47   $::lxdebug->enter_sub;
48   $::auth->assert('cash');
49
50   RC->paymentaccounts(\%::myconfig, $::form);
51
52   setup_rc_reconciliation_action_bar();
53
54   $::form->header;
55   print $::form->parse_html_template('rc/step1', {
56     selection_sub => sub { ("$_[0]{accno}--$_[0]{description}")x2 },
57   });
58
59   $::lxdebug->leave_sub;
60 }
61
62 sub get_payments {
63   $::lxdebug->enter_sub;
64   $::auth->assert('cash');
65
66   ($::form->{accno}, $::form->{account}) = split /--/, $::form->{accno};
67
68   RC->payment_transactions(\%::myconfig, $::form);
69
70   display_form();
71
72   $::lxdebug->leave_sub;
73 }
74
75 sub display_form {
76   $::lxdebug->enter_sub;
77   $::auth->assert('cash');
78
79   my @options;
80   push @options, $::locale->text('From') . " " . $::locale->date(\%::myconfig, $::form->{fromdate}, 0) if $::form->{fromdate};
81   push @options, $::locale->text('Until') . " " . $::locale->date(\%::myconfig, $::form->{todate}, 0) if $::form->{todate};
82
83   my $ml = ($::form->{category} eq 'A') ? -1 : 1;
84   my $beginningbalance = $::form->{beginningbalance} * $ml;
85   my $clearedbalance   =
86   my $balance          = $beginningbalance;
87   my $i                = 0;
88   my $last_id          = 0;
89   my ($last_fx, @rows, $cleared, $totaldebits, $totalcredits, $fx);
90
91   for my $ref (@{ $::form->{PR} }) {
92     $balance      += $ref->{amount} * $ml;
93     $cleared      += $ref->{amount} * $ml if $ref->{cleared};
94     $totaldebits  += $ref->{amount} * -1  if $ref->{amount} < 0;
95     $totalcredits += $ref->{amount}       if $ref->{amount} >= 0;
96     $fx           += $ref->{amount} * $ml if $ref->{fx_transaction};
97     $i++                                  if (!$ref->{fx_transaction} && !$last_fx) || $last_id != $ref->{id};
98     $last_fx       = $ref->{fx_transaction};
99     $last_id       = $ref->{id};
100
101     push @rows, { %$ref, balance => $balance, i => $i };
102   }
103
104   my $statementbalance = $::form->parse_amount(\%::myconfig, $::form->{statementbalance});
105   my $difference       = $statementbalance - $clearedbalance - $cleared;
106
107   setup_rc_display_form_action_bar();
108
109   $::form->header;
110   print $::form->parse_html_template('rc/step2', {
111     is_asset         => $::form->{category} eq 'A',
112     option           => \@options,
113     DATA             => \@rows,
114     total            => {
115       credit => $totalcredits,
116       debit  => $totaldebits,
117     },
118     balance          => {
119       beginning => $beginningbalance,
120       cleared   => $clearedbalance,
121       statement => $statementbalance,
122     },
123     difference       => $difference,
124     rowcount         => $i,
125     fx               => $fx,
126   });
127
128   $::lxdebug->leave_sub;
129 }
130
131 sub update {
132   $::lxdebug->enter_sub;
133   $::auth->assert('cash');
134
135   # reset difference as it doesn't always arrive here empty
136   $::form->{difference} = 0;
137
138   RC->payment_transactions(\%::myconfig, $::form);
139
140   my $i;
141   for my $ref (@{ $::form->{PR} }) {
142     next if $ref->{fx_transaction};
143     $i++;
144     $ref->{cleared} = $::form->{"cleared_$i"};
145   }
146
147   display_form();
148
149   $::lxdebug->leave_sub;
150 }
151
152 sub reconcile {
153   $::lxdebug->enter_sub;
154   $::auth->assert('cash');
155
156   $::form->{callback} = "$::form->{script}?action=reconciliation";
157
158   $::form->error($::locale->text('Out of balance!')) if $::form->{difference} *= 1;
159
160   RC->reconcile(\%::myconfig, $::form);
161   $::form->redirect;
162
163   $::lxdebug->leave_sub;
164 }
165
166 sub setup_rc_reconciliation_action_bar {
167   my %params = @_;
168
169   for my $bar ($::request->layout->get('actionbar')) {
170     $bar->add(
171       action => [
172         t8('Show'),
173         submit    => [ '#form', { action => "get_payments" } ],
174         accesskey => 'enter',
175       ],
176     );
177   }
178 }
179
180 sub setup_rc_display_form_action_bar {
181   my %params = @_;
182
183   for my $bar ($::request->layout->get('actionbar')) {
184     $bar->add(
185       action => [
186         t8('Update'),
187         submit    => [ '#form', { action => "update" } ],
188         accesskey => 'enter',
189       ],
190       action => [
191         t8('Reconcile'),
192         submit => [ '#form', { action => "reconcile" } ],
193       ],
194     );
195   }
196 }