Bug 6934: New report Cash Register Statistics
[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 strict;
18 use warnings;
19 use C4::Auth;
20 use CGI;
21 use C4::Context;
22 use C4::Reports;
23 use C4::Output;
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Dates qw/format_date format_date_in_iso/;
27 use C4::Budgets qw/GetCurrency GetCurrencies/;
28 #use Data::Dumper;
29 #use Smart::Comments;
30
31 my $input            = new CGI;
32 my $dbh              = C4::Context->dbh;
33 my $fullreportname   = "reports/cash_register_stats.tt";
34
35 my ($template, $borrowernumber, $cookie) = get_template_and_user({
36     template_name => $fullreportname,
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 $branchcode       = $input->param("branch") || C4::Context->userenv->{'branch'};
49 our $sep = ",";
50
51 $template->param(
52     do_it => $do_it,
53     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
54 );
55
56 #Initialize date pickers to today
57 my $today = C4::Dates->today('iso');
58 my $fromDate = $today;
59 my $toDate   = $today;
60
61 ### fromdate today: $fromDate
62
63 my $query_manualinv = "SELECT id, authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'";
64 my $sth_manualinv = $dbh->prepare($query_manualinv) or die "Unable to prepare query" . $dbh->errstr;
65 $sth_manualinv->execute() or die "Unable to execute query " . $sth_manualinv->errstr;
66 my $manualinv_types = $sth_manualinv->fetchall_arrayref({});
67
68 ### $manualinv_types
69
70 if ($do_it) {
71
72     $fromDate = format_date_in_iso($input->param("filter_date_begin"));
73     $toDate   = format_date_in_iso($input->param("filter_date_end"));
74
75     my $whereTType = '';
76
77     if ($transaction_type eq 'ALL') { #All Transactons
78         $whereTType = '';
79     } elsif ($transaction_type eq 'ACT') { #Active
80         $whereTType = " accounttype NOT IN ('F', 'FU', 'FOR', 'M', 'L') AND ";
81     } else { #Single transac type
82         if ($transaction_type eq 'FORW') {
83             $whereTType = " accounttype = 'FOR' OR accounttype = 'W' AND ";
84         } else {
85             $whereTType = " accounttype = '$transaction_type' AND ";
86         }
87     }
88
89     my $whereBranchCode = '';
90     if ($branchcode ne 'ALL') {
91         $whereBranchCode = "AND bo.branchcode = '$branchcode'";
92     }
93
94     ### $transaction_type;
95
96     my $query = "
97     SELECT round(amount,2) AS amount, description,
98         bo.surname AS bsurname, bo.firstname AS bfirstname, m.surname AS msurname, m.firstname AS mfirstname,
99         bo.cardnumber, br.branchname, bo.borrowernumber,
100         al.borrowernumber, DATE(al.date) as date, al.accounttype, al.amountoutstanding,
101         bi.title, bi.biblionumber, i.barcode, i.itype
102         FROM accountlines al
103         LEFT JOIN borrowers bo ON (al.borrowernumber = bo.borrowernumber)
104         LEFT JOIN borrowers m ON (al.manager_id = m.borrowernumber)
105         LEFT JOIN branches br ON (br.branchcode = m.branchcode )
106         LEFT JOIN items i ON (i.itemnumber = al.itemnumber)
107         LEFT JOIN biblio bi ON (bi.biblionumber = i.biblionumber)
108         WHERE $whereTType
109         CAST(al.date AS DATE) BETWEEN ? AND ?
110         $whereBranchCode
111         ORDER BY al.date
112     ";
113     my $sth_stats = $dbh->prepare($query) or die "Unable to prepare query" . $dbh->errstr;
114     $sth_stats->execute($fromDate, $toDate) or die "Unable to execute query " . $sth_stats->errstr;
115
116     my @loopresult;
117     my $grantotal = 0;
118     while ( my $row = $sth_stats->fetchrow_hashref()) {
119         $row->{amountoutstanding} = 0 if (!$row->{amountoutstanding});
120         #if ((abs($row->{amount}) - $row->{amountoutstanding}) > 0) {
121             $row->{amount} = sprintf("%.2f", abs ($row->{amount}));
122             $row->{date} = format_date($row->{date});
123             ### date : $row->{date}
124
125             push (@loopresult, $row);
126             $grantotal += abs($row->{amount});
127         #}
128     }
129
130     my @currency = GetCurrency();
131     $grantotal = sprintf("%.2f", $grantotal);
132
133     if($output eq 'screen'){
134         $template->param(
135             loopresult => \@loopresult,
136             total => $grantotal,
137         );
138     } else{
139         binmode STDOUT, ':encoding(UTF-8)';
140         print $input->header(
141             -type => 'application/vnd.sun.xml.calc',
142             -encoding => 'utf-8',
143             -name => "$basename.csv",
144             -attachment => "$basename.csv"
145         );
146
147         print "Manager name".$sep;
148         print "Borrower cardnumber".$sep;
149         print "Borrower name".$sep;
150         print "Branch".$sep;
151         print "Transaction date".$sep;
152         print "Transaction type".$sep;
153         print "Amount".$sep;
154         print "Biblio title".$sep;
155         print "Barcode".$sep;
156         print "Document type"."\n";
157
158         foreach my $item (@loopresult){
159             print $item->{mfirstname}. ' ' . $item->{msurname} . $sep;
160             print $item->{cardnumber}.$sep;
161             print $item->{bfirstname}. ' ' . $item->{bsurname} . $sep;
162             print $item->{branchname}.$sep;
163             print $item->{date}.$sep;
164             print $item->{accounttype}.$sep;
165             print $item->{amount}.$sep;
166             print $item->{title}.$sep;
167             print $item->{barcode}.$sep;
168             print $item->{itype}."\n";
169         }
170
171         print $sep x 6;
172         print $grantotal."\n";
173         exit(1);
174     }
175
176 }
177
178 ### fromdate final: $fromDate
179 ### toDate final: $toDate
180 $template->param(
181     beginDate        => format_date($fromDate),
182     endDate          => format_date($toDate),
183     transaction_type => $transaction_type,
184     branchloop       => C4::Branch::GetBranchesLoop($branchcode),
185     manualinv_types  => $manualinv_types,
186 );
187 output_html_with_http_headers $input, $cookie, $template->output;
188
189 1;