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