Bug 23049: (follow-up) VARCHAR(80) + ac -> account
[koha.git] / reports / cash_register_stats.pl
blobb4340c9f62ce66e813164de36a7b7b9cdc7a0adc
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation;
9 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with Koha; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 use Modern::Perl;
18 use C4::Auth;
19 use CGI;
20 use C4::Context;
21 use C4::Reports;
22 use C4::Output;
23 use C4::Koha;
24 use C4::Circulation;
25 use DateTime;
26 use Koha::DateUtils;
27 use Text::CSV::Encoded;
28 use List::Util qw/any/;
30 use Koha::Account::DebitTypes;
32 my $input = new CGI;
33 my $dbh = C4::Context->dbh;
35 my ($template, $borrowernumber, $cookie) = get_template_and_user({
36 template_name => "reports/cash_register_stats.tt",
37 query => $input,
38 type => "intranet",
39 authnotrequired => 0,
40 flagsrequired => {reports => '*'},
41 debug => 1,
42 });
44 my $do_it = $input->param('do_it');
45 my $output = $input->param("output");
46 my $basename = $input->param("basename");
47 my $transaction_type = $input->param("transaction_type") || 'ACT';
48 my $manager_branchcode = $input->param("branch") || C4::Context->userenv->{'branch'};
50 $template->param(
51 do_it => $do_it,
52 CGIsepChoice => GetDelimiterChoices,
55 #Initialize date pickers to today
56 my $fromDate = dt_from_string;
57 my $toDate = dt_from_string;
59 my @debit_types =
60 Koha::Account::DebitTypes->search()->as_list;
62 if ($do_it) {
64 $fromDate = output_pref({ dt => eval { dt_from_string($input->param("from")) } || dt_from_string,
65 dateformat => 'sql', dateonly => 1 }); #for sql query
66 $toDate = output_pref({ dt => eval { dt_from_string($input->param("to")) } || dt_from_string,
67 dateformat => 'sql', dateonly => 1 }); #for sql query
69 my $whereTType = q{};
70 my @extra_params; # if we add conditions to the select we need extra params
72 if ($transaction_type eq 'ALL') { #All Transactons
73 $whereTType = q{};
74 } elsif ($transaction_type eq 'ACT') { #Active
75 $whereTType = q{ AND accounttype IN ('Pay','C') };
76 } elsif ($transaction_type eq 'FORW') {
77 $whereTType = q{ AND accounttype IN ('FOR','W') };
78 } else {
79 if ( any { $transaction_type eq $_->code } @debit_types ) {
80 $whereTType = q{ AND debit_type_code = ? };
81 push @extra_params, $transaction_type;
82 } else {
83 $whereTType = q{ AND accounttype = ? };
84 push @extra_params, $transaction_type;
88 my $whereBranchCode = q{};
89 if ($manager_branchcode ne 'ALL') {
90 $whereBranchCode = q{ AND m.branchcode = ?};
91 push @extra_params, $manager_branchcode;
95 my $query = "
96 SELECT round(amount,2) AS amount, description,
97 bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
98 bo.cardnumber, br.branchname, bo.borrowernumber,
99 al.borrowernumber, DATE(al.date) as date, al.accounttype, al.debit_type_code, al.amountoutstanding, al.note,
100 bi.title, bi.biblionumber, i.barcode, i.itype
101 FROM accountlines al
102 LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
103 LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
104 LEFT JOIN branches br ON (br.branchcode = m.branchcode )
105 LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
106 LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
107 WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
108 $whereTType
109 $whereBranchCode
110 ORDER BY al.date
112 my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
113 $sth_stats->execute($fromDate, $toDate, @extra_params) or die "Unable to execute query " . $sth_stats->errstr;
115 my @loopresult;
116 my $grantotal = 0;
117 while ( my $row = $sth_stats->fetchrow_hashref()) {
118 $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
119 #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
120 $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
121 $row->{date} = dt_from_string($row->{date}, 'sql');
123 push (@loopresult, $row);
124 if($transaction_type eq 'ACT' && ($row->{accounttype} !~ /^C$|^CR$|^Pay$/)){
125 pop @loopresult;
126 next;
128 if($row->{accounttype} =~ /^C$|^CR$/){
129 $grantotal -= abs($row->{amount});
130 $row->{amount} = '-' . $row->{amount};
131 }elsif($row->{accounttype} eq 'FORW' || $row->{accounttype} eq 'W'){
132 }else{
133 $grantotal += abs($row->{amount});
138 $grantotal = sprintf("%.2f", $grantotal);
140 if($output eq 'screen'){
141 $template->param(
142 loopresult => \@loopresult,
143 total => $grantotal,
145 } else{
146 my $format = 'csv';
147 my $reportname = $input->param('basename');
148 my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
149 my $delimiter = C4::Context->preference('delimiter') || ',';
150 my @rows;
151 foreach my $row (@loopresult) {
152 my @rowValues;
153 push @rowValues, $row->{mfirstname}. ' ' . $row->{msurname},
154 $row->{cardnumber},
155 $row->{bfirstname} . ' ' . $row->{bsurname},
156 $row->{branchname},
157 $row->{date},
158 $row->{accounttype},
159 $row->{debit_type},
160 $row->{note},
161 $row->{amount},
162 $row->{title},
163 $row->{barcode},
164 $row->{itype};
165 push (@rows, \@rowValues) ;
167 my @total;
168 for (1..6){push(@total,"")};
169 push(@total, $grantotal);
170 print $input->header(
171 -type => 'text/csv',
172 -encoding => 'utf-8',
173 -attachment => $reportfilename,
174 -name => $reportfilename
176 my $csvTemplate = C4::Templates::gettemplate('reports/csv/cash_register_stats.tt', 'intranet', $input);
177 $csvTemplate->param(sep => $delimiter, rows => \@rows, total => \@total );
178 print $csvTemplate->output;
179 exit;
184 $template->param(
185 beginDate => $fromDate,
186 endDate => $toDate,
187 transaction_type => $transaction_type,
188 branchloop => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
189 debit_types => \@debit_types,
190 CGIsepChoice => GetDelimiterChoices,
193 output_html_with_http_headers $input, $cookie, $template->output;