Bug 6934: fixed encoding issue
[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 #use Data::Dumper;
29 #use Smart::Comments;
30
31 my $input            = new CGI;
32 my $dbh              = C4::Context->dbh;
33
34 my ($template, $borrowernumber, $cookie) = get_template_and_user({
35     template_name => "reports/cash_register_stats.tt",
36     query => $input,
37     type => "intranet",
38     authnotrequired => 0,
39     flagsrequired => {reports => '*'},
40     debug => 1,
41 });
42
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'};
48
49 $template->param(
50     do_it => $do_it,
51     CGIsepChoice => GetDelimiterChoices,
52 );
53
54 #Initialize date pickers to today
55 my $fromDate = dt_from_string;
56 my $toDate   = dt_from_string;
57
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({});
62
63
64 if ($do_it) {
65
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
70
71     my $whereTType = '';
72
73     if ($transaction_type eq 'ALL') { #All Transactons
74         $whereTType = '';
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 ";
80         } else {
81             $whereTType = " accounttype = '$transaction_type' AND ";
82         }
83     }
84
85     my $whereBranchCode = '';
86     if ($manager_branchcode ne 'ALL') {
87         $whereBranchCode = "AND m.branchcode = '$manager_branchcode'";
88     }
89
90
91     my $query = "
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
97         FROM accountlines al
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)
103         WHERE $whereTType
104         CAST(al.date AS DATE) BETWEEN ? AND ?
105         $whereBranchCode
106         ORDER BY al.date
107     ";
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;
110
111     my @loopresult;
112     my $grantotal = 0;
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');
118
119             push (@loopresult, $row);
120             if($transaction_type eq 'ACT' && ($row->{accounttype} !~ /^C$|^CR$|^LR$|^Pay$/)){
121                 pop @loopresult;
122                 next;
123             }
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'){
128             }else{
129                 $grantotal += abs($row->{amount});
130             }
131         #}
132     }
133
134     $grantotal = sprintf("%.2f", $grantotal);
135
136     if($output eq 'screen'){
137         $template->param(
138             loopresult => \@loopresult,
139             total => $grantotal,
140         );
141     } else{
142         binmode STDOUT, ':encoding(UTF-8)';
143         my $q_errors;
144         my $format = 'csv';
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();
154             my @headers = ();
155             push @headers, "mfirstname",
156                                     "cardnumber",
157                                     "bfirstname",
158                                     "branchname",
159                                     "date",
160                                     "accounttype",
161                                     "amount",
162                                     "title",
163                                     "barcode",
164                                     "itype";
165             if ($csv->combine(@headers)) {
166                 $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
167             } else {
168                 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
169             }
170             foreach my $row (@loopresult) {
171                                 my @rowValues = ();
172                 push @rowValues, $row->{mfirstname},
173                         $row->{cardnumber},
174                                                 $row->{bfirstname},
175                                                 $row->{branchname},
176                                                 $row->{date},
177                                                 $row->{accounttype},
178                                                 $row->{amount},
179                                                 $row->{title},
180                                                 $row->{barcode};
181                                                 $row->{itype};
182                 if ($csv->combine(@rowValues)) {
183                     $content .= Encode::decode('UTF-8',$csv->string()) . "\n";
184                 } else {
185                     push @$q_errors, { combine => $csv->error_diag() } ;
186                 }
187             }
188         }
189         print $input->header(
190             -type => $type,
191             -attachment=> $reportfilename
192         );
193         print $content;
194
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.
200         exit(1);
201     }
202
203 }
204
205 $template->param(
206     beginDate        => $fromDate,
207     endDate          => $toDate,
208     transaction_type => $transaction_type,
209     branchloop       => C4::Branch::GetBranchesLoop($manager_branchcode),
210     manualinv_types  => $manualinv_types,
211     CGIsepChoice => GetDelimiterChoices,
212 );
213
214 output_html_with_http_headers $input, $cookie, $template->output;
215
216 1;