Bug 20815: Add ability to choose if lost fee is refunded based on length of time...
[koha.git] / reports / cash_register_stats.pl
blob98390ac52dd2756c79b23b9dbf3e12eb9ef0cd31
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
19 use C4::Auth;
20 use CGI;
21 use C4::Context;
22 use C4::Reports;
23 use C4::Output;
24 use C4::Koha;
25 use C4::Circulation;
26 use DateTime;
27 use Koha::DateUtils;
28 use Text::CSV::Encoded;
29 use List::Util qw/any/;
31 use Koha::Account::CreditTypes;
32 use Koha::Account::DebitTypes;
34 my $input = new CGI;
35 my $dbh = C4::Context->dbh;
37 my ($template, $borrowernumber, $cookie) = get_template_and_user({
38 template_name => "reports/cash_register_stats.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => {reports => '*'},
43 debug => 1,
44 });
46 my $do_it = $input->param('do_it');
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 my $transaction_type = $input->param("transaction_type") || 'ACT';
50 my $manager_branchcode = $input->param("branch") || C4::Context->userenv->{'branch'};
52 $template->param(
53 do_it => $do_it,
54 CGIsepChoice => GetDelimiterChoices,
57 #Initialize date pickers to today
58 my $fromDate = dt_from_string;
59 my $toDate = dt_from_string;
61 my @debit_types =
62 Koha::Account::DebitTypes->search()->as_list;
63 my @credit_types =
64 Koha::Account::CreditTypes->search()->as_list;
66 if ($do_it) {
68 $fromDate = output_pref({ dt => eval { dt_from_string(scalar $input->param("from")) } || dt_from_string,
69 dateformat => 'sql', dateonly => 1 }); #for sql query
70 $toDate = output_pref({ dt => eval { dt_from_string(scalar $input->param("to")) } || dt_from_string,
71 dateformat => 'sql', dateonly => 1 }); #for sql query
73 my $whereTType = q{};
74 my @extra_params; # if we add conditions to the select we need extra params
76 if ($transaction_type eq 'ALL') { #All Transactons
77 $whereTType = q{};
78 } elsif ($transaction_type eq 'ACT') { #Active
79 $whereTType = q{ AND credit_type_code IN ('PAYMENT','CREDIT') };
80 } elsif ($transaction_type eq 'FORW') {
81 $whereTType = q{ AND credit_type_code IN ('FORGIVEN','WRITEOFF') };
82 } else {
83 if ( any { $transaction_type eq $_->code } @debit_types ) {
84 $whereTType = q{ AND debit_type_code = ? };
85 push @extra_params, $transaction_type;
86 } else {
87 $whereTType = q{ AND credit_type_code = ? };
88 push @extra_params, $transaction_type;
92 my $whereBranchCode = q{};
93 if ($manager_branchcode ne 'ALL') {
94 $whereBranchCode = q{ AND m.branchcode = ?};
95 push @extra_params, $manager_branchcode;
99 my $query = "
100 SELECT round(amount,2) AS amount, description,
101 bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
102 bo.cardnumber, br.branchname, bo.borrowernumber,
103 al.borrowernumber, DATE(al.date) as date, al.credit_type_code, al.debit_type_code, al.amountoutstanding, al.note,
104 bi.title, bi.biblionumber, i.barcode, i.itype
105 FROM accountlines al
106 LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
107 LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
108 LEFT JOIN branches br ON (br.branchcode = m.branchcode )
109 LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
110 LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
111 WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
112 $whereTType
113 $whereBranchCode
114 ORDER BY al.date
116 my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
117 $sth_stats->execute($fromDate, $toDate, @extra_params) or die "Unable to execute query " . $sth_stats->errstr;
119 my @loopresult;
120 my $grantotal = 0;
121 while ( my $row = $sth_stats->fetchrow_hashref()) {
122 $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
123 #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
124 $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
125 $row->{date} = dt_from_string($row->{date}, 'sql');
127 push (@loopresult, $row);
128 if($transaction_type eq 'ACT' && ($row->{credit_type_code} !~ /^CREDIT$|^PAYMENT$/)){
129 pop @loopresult;
130 next;
132 if($row->{credit_type_code} =~ /^CREDIT$/){
133 $grantotal -= abs($row->{amount});
134 $row->{amount} = '-' . $row->{amount};
135 }elsif($row->{credit_type_code} eq 'FORGIVEN' || $row->{credit_type_code} eq 'WRITEOFF'){
136 }else{
137 $grantotal += abs($row->{amount});
142 $grantotal = sprintf("%.2f", $grantotal);
144 if($output eq 'screen'){
145 $template->param(
146 loopresult => \@loopresult,
147 total => $grantotal,
149 } else{
150 my $format = 'csv';
151 my $reportname = $input->param('basename');
152 my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
153 my $delimiter = C4::Context->preference('delimiter') || ',';
154 my @rows;
155 foreach my $row (@loopresult) {
156 my @rowValues;
157 push @rowValues, $row->{mfirstname}. ' ' . $row->{msurname},
158 $row->{cardnumber},
159 $row->{bfirstname} . ' ' . $row->{bsurname},
160 $row->{branchname},
161 $row->{date},
162 $row->{credit_type},
163 $row->{debit_type},
164 $row->{note},
165 $row->{amount},
166 $row->{title},
167 $row->{barcode},
168 $row->{itype};
169 push (@rows, \@rowValues) ;
171 my @total;
172 for (1..6){push(@total,"")};
173 push(@total, $grantotal);
174 print $input->header(
175 -type => 'text/csv',
176 -encoding => 'utf-8',
177 -attachment => $reportfilename,
178 -name => $reportfilename
180 my $csvTemplate = C4::Templates::gettemplate('reports/csv/cash_register_stats.tt', 'intranet', $input);
181 $csvTemplate->param(sep => $delimiter, rows => \@rows, total => \@total );
182 print $csvTemplate->output;
183 exit;
188 $template->param(
189 beginDate => $fromDate,
190 endDate => $toDate,
191 transaction_type => $transaction_type,
192 branchloop => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
193 debit_types => \@debit_types,
194 credit_types => \@credit_types,
195 CGIsepChoice => GetDelimiterChoices,
198 output_html_with_http_headers $input, $cookie, $template->output;