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>.
28 use Text::CSV::Encoded;
29 use List::Util qw/any/;
31 use Koha::Account::CreditTypes;
32 use Koha::Account::DebitTypes;
35 my $dbh = C4::Context->dbh;
37 my ($template, $borrowernumber, $cookie) = get_template_and_user({
38 template_name => "reports/cash_register_stats.tt",
41 flagsrequired => {reports => '*'},
45 my $do_it = $input->param('do_it');
46 my $output = $input->param("output");
47 my $basename = $input->param("basename");
48 my $transaction_type = $input->param("transaction_type") || 'ACT';
49 my $manager_branchcode = $input->param("branch") || C4::Context->userenv->{'branch'};
53 CGIsepChoice => GetDelimiterChoices,
56 #Initialize date pickers to today
57 my $fromDate = dt_from_string;
58 my $toDate = dt_from_string;
61 Koha::Account::DebitTypes->search()->as_list;
63 Koha::Account::CreditTypes->search()->as_list;
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
74 my @extra_params; # if we add conditions to the select we need extra params
76 if ($transaction_type eq 'ALL') { #All Transactons
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') };
83 if ( any { $transaction_type eq $_->code } @debit_types ) {
84 $whereTType = q{ AND debit_type_code = ? };
85 push @extra_params, $transaction_type;
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;
98 my $whereRegister = q{};
99 $registerid = $input->param("registerid");
101 $whereRegister = q{ AND al.register_id = ?};
102 push @extra_params, $registerid;
106 SELECT round(amount,2) AS amount, description,
107 bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
108 bo.cardnumber, br.branchname, bo.borrowernumber,
109 al.borrowernumber, DATE(al.date) as date, al.credit_type_code, al.debit_type_code, al.amountoutstanding, al.note,
110 bi.title, bi.biblionumber, i.barcode, i.itype
112 LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
113 LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
114 LEFT JOIN branches br ON (br.branchcode = m.branchcode )
115 LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
116 LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
117 WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
123 my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
124 $sth_stats->execute($fromDate, $toDate, @extra_params) or die "Unable to execute query " . $sth_stats->errstr;
128 while ( my $row = $sth_stats->fetchrow_hashref()) {
129 $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
130 #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
131 $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
132 $row->{date} = dt_from_string($row->{date}, 'sql');
134 push (@loopresult, $row);
135 if($transaction_type eq 'ACT' && ($row->{credit_type_code} !~ /^CREDIT$|^PAYMENT$/)){
139 if($row->{credit_type_code} =~ /^CREDIT$/){
140 $grantotal -= abs($row->{amount});
141 $row->{amount} = '-' . $row->{amount};
142 }elsif($row->{credit_type_code} eq 'FORGIVEN' || $row->{credit_type_code} eq 'WRITEOFF'){
144 $grantotal += abs($row->{amount});
149 $grantotal = sprintf("%.2f", $grantotal);
151 if($output eq 'screen'){
153 loopresult => \@loopresult,
158 my $reportname = $input->param('basename');
159 my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
160 my $delimiter = C4::Context->preference('delimiter') || ',';
162 foreach my $row (@loopresult) {
164 push @rowValues, $row->{mfirstname}. ' ' . $row->{msurname},
166 $row->{bfirstname} . ' ' . $row->{bsurname},
169 $row->{credit_type_code},
170 $row->{debit_type_code},
176 push (@rows, \@rowValues) ;
179 for (1..6){push(@total,"")};
180 push(@total, $grantotal);
181 print $input->header(
183 -encoding => 'utf-8',
184 -attachment => $reportfilename,
185 -name => $reportfilename
187 my $csvTemplate = C4::Templates::gettemplate('reports/csv/cash_register_stats.tt', 'intranet', $input);
188 $csvTemplate->param(sep => $delimiter, rows => \@rows, total => \@total );
189 print $csvTemplate->output;
196 beginDate => $fromDate,
198 transaction_type => $transaction_type,
199 branchloop => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
200 debit_types => \@debit_types,
201 credit_types => \@credit_types,
202 registerid => $registerid,
203 CGIsepChoice => GetDelimiterChoices,
206 output_html_with_http_headers $input, $cookie, $template->output;