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;
32 my $dbh = C4::Context->dbh;
34 my ($template, $borrowernumber, $cookie) = get_template_and_user({
35 template_name => "reports/cash_register_stats.tt",
39 flagsrequired => {reports => '*'},
43 my $do_it = $input->param('do_it');
44 my $output = $input->param("output");
45 my $basename = $input->param("basename");
46 my $transaction_type = $input->param("transaction_type") || 'ACT';
47 my $manager_branchcode = $input->param("branch") || C4::Context->userenv->{'branch'};
51 CGIsepChoice => GetDelimiterChoices,
54 #Initialize date pickers to today
55 my $fromDate = dt_from_string;
56 my $toDate = dt_from_string;
58 my $query_manualinv = "SELECT id, authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'";
59 my $sth_manualinv = $dbh->prepare($query_manualinv) or die "Unable to prepare query" . $dbh->errstr;
60 $sth_manualinv->execute() or die "Unable to execute query " . $sth_manualinv->errstr;
61 my $manualinv_types = $sth_manualinv->fetchall_arrayref({});
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
73 if ($transaction_type eq 'ALL') { #All Transactons
75 } elsif ($transaction_type eq 'ACT') { #Active
76 $whereTType = " accounttype NOT IN ('F', 'FU', 'FOR', 'M', 'L') AND ";
77 } else { #Single transac type
78 if ($transaction_type eq 'FORW') {
79 $whereTType = " accounttype = 'FOR' OR accounttype = 'W' AND ";
81 $whereTType = " accounttype = '$transaction_type' AND ";
85 my $whereBranchCode = '';
86 if ($manager_branchcode ne 'ALL') {
87 $whereBranchCode = "AND m.branchcode = '$manager_branchcode'";
92 SELECT round(amount,2) AS amount, description,
93 bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
94 bo.cardnumber, br.branchname, bo.borrowernumber,
95 al.borrowernumber, DATE(al.date) as date, al.accounttype, al.amountoutstanding, al.note,
96 bi.title, bi.biblionumber, i.barcode, i.itype
98 LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
99 LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
100 LEFT JOIN branches br ON (br.branchcode = m.branchcode )
101 LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
102 LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
104 CAST(al.date AS DATE) BETWEEN ? AND ?
108 my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query" . $dbh->errstr;
109 $sth_stats->execute($fromDate, $toDate) or die "Unable to execute query " . $sth_stats->errstr;
113 while ( my $row = $sth_stats->fetchrow_hashref()) {
114 $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
115 #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
116 $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
117 $row->{date} = dt_from_string($row->{date}, 'sql');
119 push (@loopresult, $row);
120 if($transaction_type eq 'ACT' && ($row->{accounttype} !~ /^C$|^CR$|^LR$|^Pay$/)){
124 if($row->{accounttype} =~ /^C$|^CR$|^LR$/){
125 $grantotal -= abs($row->{amount});
126 $row->{amount} = '-' . $row->{amount};
127 }elsif($row->{accounttype} eq 'FORW' || $row->{accounttype} eq 'W'){
129 $grantotal += abs($row->{amount});
134 $grantotal = sprintf("%.2f", $grantotal);
136 if($output eq 'screen'){
138 loopresult => \@loopresult,
142 binmode STDOUT, ':encoding(UTF-8)';
145 my $reportname = $input->param('basename');
146 my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
147 #my $reportfilename = "$reportname.html" ;
148 my $delimiter = C4::Context->preference('delimiter') || ',';
149 my ( $type, $content );
150 if ( $format eq 'csv' ) {
151 my $type = 'application/csv';
152 my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
153 $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
155 push @headers, "mfirstname",
165 if ($csv->combine(@headers)) {
166 $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
168 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
170 foreach my $row (@loopresult) {
172 push @rowValues, $row->{mfirstname},
182 if ($csv->combine(@rowValues)) {
183 $content .= Encode::decode('UTF-8',$csv->string()) . "\n";
185 push @$q_errors, { combine => $csv->error_diag() } ;
189 print $input->header(
191 -attachment=> $reportfilename
195 print $delimiter x 6;
196 print $grantotal."\n";
197 foreach my $err (@$q_errors) {
198 print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
199 } # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
206 beginDate => $fromDate,
208 transaction_type => $transaction_type,
209 branchloop => C4::Branch::GetBranchesLoop($manager_branchcode),
210 manualinv_types => $manualinv_types,
211 CGIsepChoice => GetDelimiterChoices,
214 output_html_with_http_headers $input, $cookie, $template->output;