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
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.
27 use Text::CSV::Encoded;
28 use List::Util qw/any/;
30 use Koha::Account::DebitTypes;
33 my $dbh = C4::Context->dbh;
35 my ($template, $borrowernumber, $cookie) = get_template_and_user({
36 template_name => "reports/cash_register_stats.tt",
40 flagsrequired => {reports => '*'},
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'};
52 CGIsepChoice => GetDelimiterChoices,
55 #Initialize date pickers to today
56 my $fromDate = dt_from_string;
57 my $toDate = dt_from_string;
60 Koha::Account::DebitTypes->search()->as_list;
62 Koha::Account::CreditTypes->search()->as_list;
66 $fromDate = output_pref({ dt => eval { dt_from_string($input->param("from")) } || dt_from_string,
67 dateformat => 'sql', dateonly => 1 }); #for sql query
68 $toDate = output_pref({ dt => eval { dt_from_string($input->param("to")) } || dt_from_string,
69 dateformat => 'sql', dateonly => 1 }); #for sql query
72 my @extra_params; # if we add conditions to the select we need extra params
74 if ($transaction_type eq 'ALL') { #All Transactons
76 } elsif ($transaction_type eq 'ACT') { #Active
77 $whereTType = q{ AND credit_type_code IN ('PAYMENT','CREDIT') };
78 } elsif ($transaction_type eq 'FORW') {
79 $whereTType = q{ AND credit_type_code IN ('FORGIVEN','WRITEOFF') };
81 if ( any { $transaction_type eq $_->code } @debit_types ) {
82 $whereTType = q{ AND debit_type_code = ? };
83 push @extra_params, $transaction_type;
85 $whereTType = q{ AND credit_type_code = ? };
86 push @extra_params, $transaction_type;
90 my $whereBranchCode = q{};
91 if ($manager_branchcode ne 'ALL') {
92 $whereBranchCode = q{ AND m.branchcode = ?};
93 push @extra_params, $manager_branchcode;
98 SELECT round(amount,2) AS amount, description,
99 bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
100 bo.cardnumber, br.branchname, bo.borrowernumber,
101 al.borrowernumber, DATE(al.date) as date, al.credit_type_code, al.debit_type_code, al.amountoutstanding, al.note,
102 bi.title, bi.biblionumber, i.barcode, i.itype
104 LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
105 LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
106 LEFT JOIN branches br ON (br.branchcode = m.branchcode )
107 LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
108 LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
109 WHERE CAST(al.date AS DATE) BETWEEN ? AND ?
114 my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query " . $dbh->errstr;
115 $sth_stats->execute($fromDate, $toDate, @extra_params) or die "Unable to execute query " . $sth_stats->errstr;
119 while ( my $row = $sth_stats->fetchrow_hashref()) {
120 $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
121 #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
122 $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
123 $row->{date} = dt_from_string($row->{date}, 'sql');
125 push (@loopresult, $row);
126 if($transaction_type eq 'ACT' && ($row->{credit_type_code} !~ /^CREDIT$|^PAYMENT$/)){
130 if($row->{credit_type_code} =~ /^CREDIT$/){
131 $grantotal -= abs($row->{amount});
132 $row->{amount} = '-' . $row->{amount};
133 }elsif($row->{credit_type_code} eq 'FORGIVEN' || $row->{credit_type_code} eq 'WRITEOFF'){
135 $grantotal += abs($row->{amount});
140 $grantotal = sprintf("%.2f", $grantotal);
142 if($output eq 'screen'){
144 loopresult => \@loopresult,
149 my $reportname = $input->param('basename');
150 my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
151 my $delimiter = C4::Context->preference('delimiter') || ',';
153 foreach my $row (@loopresult) {
155 push @rowValues, $row->{mfirstname}. ' ' . $row->{msurname},
157 $row->{bfirstname} . ' ' . $row->{bsurname},
167 push (@rows, \@rowValues) ;
170 for (1..6){push(@total,"")};
171 push(@total, $grantotal);
172 print $input->header(
174 -encoding => 'utf-8',
175 -attachment => $reportfilename,
176 -name => $reportfilename
178 my $csvTemplate = C4::Templates::gettemplate('reports/csv/cash_register_stats.tt', 'intranet', $input);
179 $csvTemplate->param(sep => $delimiter, rows => \@rows, total => \@total );
180 print $csvTemplate->output;
187 beginDate => $fromDate,
189 transaction_type => $transaction_type,
190 branchloop => Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed,
191 debit_types => \@debit_types,
192 credit_types => \@credit_types,
193 CGIsepChoice => GetDelimiterChoices,
196 output_html_with_http_headers $input, $cookie, $template->output;