Bug 6934: QA follow-up
[koha.git] / reports / cash_register_stats.pl
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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;
8 #
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.
12 #
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.
16
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
29 my $input            = new CGI;
30 my $dbh              = C4::Context->dbh;
31
32 my ($template, $borrowernumber, $cookie) = get_template_and_user({
33     template_name => "reports/cash_register_stats.tt",
34     query => $input,
35     type => "intranet",
36     authnotrequired => 0,
37     flagsrequired => {reports => '*'},
38     debug => 1,
39 });
40
41 my $do_it            = $input->param('do_it');
42 my $output           = $input->param("output");
43 my $basename         = $input->param("basename");
44 my $transaction_type = $input->param("transaction_type") || 'ACT';
45 my $manager_branchcode       = $input->param("branch") || C4::Context->userenv->{'branch'};
46
47 $template->param(
48     do_it => $do_it,
49     CGIsepChoice => GetDelimiterChoices,
50 );
51
52 #Initialize date pickers to today
53 my $fromDate = dt_from_string;
54 my $toDate   = dt_from_string;
55
56 my $query_manualinv = "SELECT id, authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'";
57 my $sth_manualinv = $dbh->prepare($query_manualinv) or die "Unable to prepare query" . $dbh->errstr;
58 $sth_manualinv->execute() or die "Unable to execute query " . $sth_manualinv->errstr;
59 my $manualinv_types = $sth_manualinv->fetchall_arrayref({});
60
61
62 if ($do_it) {
63
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
68
69     my $whereTType = '';
70
71     if ($transaction_type eq 'ALL') { #All Transactons
72         $whereTType = '';
73     } elsif ($transaction_type eq 'ACT') { #Active
74         $whereTType = " accounttype NOT IN ('F', 'FU', 'FOR', 'M', 'L') AND ";
75     } else { #Single transac type
76         if ($transaction_type eq 'FORW') {
77             $whereTType = " accounttype = 'FOR' OR accounttype = 'W' AND ";
78         } else {
79             $whereTType = " accounttype = '$transaction_type' AND ";
80         }
81     }
82
83     my $whereBranchCode = '';
84     if ($manager_branchcode ne 'ALL') {
85         $whereBranchCode = "AND m.branchcode = '$manager_branchcode'";
86     }
87
88
89     my $query = "
90     SELECT round(amount,2) AS amount, description,
91         bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
92         bo.cardnumber, br.branchname, bo.borrowernumber,
93         al.borrowernumber, DATE(al.date) as date, al.accounttype, al.amountoutstanding, al.note,
94         bi.title, bi.biblionumber, i.barcode, i.itype
95         FROM accountlines al
96         LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
97         LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
98         LEFT JOIN branches br ON (br.branchcode = m.branchcode )
99         LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
100         LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
101         WHERE $whereTType
102         CAST(al.date AS DATE) BETWEEN ? AND ?
103         $whereBranchCode
104         ORDER BY al.date
105     ";
106     my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query" . $dbh->errstr;
107     $sth_stats->execute($fromDate, $toDate) or die "Unable to execute query " . $sth_stats->errstr;
108
109     my @loopresult;
110     my $grantotal = 0;
111     while ( my $row = $sth_stats->fetchrow_hashref()) {
112         $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
113         #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
114             $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
115             $row->{date} = dt_from_string($row->{date}, 'sql');
116
117             push (@loopresult, $row);
118             if($transaction_type eq 'ACT' && ($row->{accounttype} !~ /^C$|^CR$|^LR$|^Pay$/)){
119                 pop @loopresult;
120                 next;
121             }
122             if($row->{accounttype} =~ /^C$|^CR$|^LR$/){
123                 $grantotal -= abs($row->{amount});
124                 $row->{amount} = '-' . $row->{amount};
125             }elsif($row->{accounttype} eq 'FORW' || $row->{accounttype} eq 'W'){
126             }else{
127                 $grantotal += abs($row->{amount});
128             }
129         #}
130     }
131
132     $grantotal = sprintf("%.2f", $grantotal);
133
134     if($output eq 'screen'){
135         $template->param(
136             loopresult => \@loopresult,
137             total => $grantotal,
138         );
139     } else{
140         binmode STDOUT, ':encoding(UTF-8)';
141         my $q_errors;
142         my $format = 'csv';
143         my $reportname = $input->param('basename');
144         my $reportfilename = $reportname ? "$reportname.$format" : "reportresults.$format" ;
145         #my $reportfilename = "$reportname.html" ;
146         my $delimiter = C4::Context->preference('delimiter') || ',';
147         my ( $type, $content );
148         if ( $format eq 'csv' ) {
149             my $type = 'application/csv';
150             my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
151             $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
152             my @headers = ();
153             push @headers, "mfirstname",
154                         "cardnumber",
155                         "bfirstname",
156                         "branchname",
157                         "date",
158                         "accounttype",
159                         "amount",
160                         "title",
161                         "barcode",
162                         "itype";
163             if ($csv->combine(@headers)) {
164                 $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
165             } else {
166                 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
167             }
168             foreach my $row (@loopresult) {
169                 my @rowValues = ();
170                 push @rowValues, $row->{mfirstname},
171                         $row->{cardnumber},
172                         $row->{bfirstname},
173                         $row->{branchname},
174                         $row->{date},
175                         $row->{accounttype},
176                         $row->{amount},
177                         $row->{title},
178                         $row->{barcode};
179                         $row->{itype};
180                 if ($csv->combine(@rowValues)) {
181                     $content .= Encode::decode('UTF-8',$csv->string()) . "\n";
182                 } else {
183                     push @$q_errors, { combine => $csv->error_diag() } ;
184                 }
185             }
186         }
187         print $input->header(
188             -type => $type,
189             -attachment=> $reportfilename
190         );
191         print $content;
192
193         print $delimiter x 6;
194         print $grantotal."\n";
195         foreach my $err (@$q_errors) {
196             print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
197         }   # here we print all the non-fatal errors at the end.  Not super smooth, but better than nothing.
198         exit(1);
199     }
200
201 }
202
203 $template->param(
204     beginDate        => $fromDate,
205     endDate          => $toDate,
206     transaction_type => $transaction_type,
207     branchloop       => C4::Branch::GetBranchesLoop($manager_branchcode),
208     manualinv_types  => $manualinv_types,
209     CGIsepChoice => GetDelimiterChoices,
210 );
211
212 output_html_with_http_headers $input, $cookie, $template->output;
213
214 1;