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