Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / circ / bookcount.pl
1 #!/usr/bin/perl
2
3 #written 7/3/2002 by Finlay
4 #script to display reports
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Debug;
26 use C4::Context;
27 use C4::Circulation;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Auth;
31 use Koha::Biblios;
32 use Koha::DateUtils;
33 use Koha::Libraries;
34
35 my $input        = CGI->new;
36 my $itm          = $input->param('itm');
37 my $biblionumber = $input->param('biblionumber');
38
39 my $biblio = Koha::Biblios->find( $biblionumber );
40 my $item   = Koha::Items->find( $itm );
41
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "circ/bookcount.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { circulate => "circulate_remaining_permissions" },
49     }
50 );
51
52 output_and_exit( $input, $cookie, $template, 'unknown_biblio')
53     unless $biblio;
54 output_and_exit( $input, $cookie, $template, 'unknown_item')
55     unless $item;
56
57 my $lastdate;
58 my $count;
59 my $lastmove = lastmove($itm);
60 if ( not $lastmove ) {
61     $count = issuessince( $itm, 0 );
62 } else {
63     $lastdate = $lastmove->{'datearrived'};
64     $count = issuessince( $itm, $lastdate );
65 }
66
67 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
68 for my $library ( @$libraries ) {
69     $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
70     $library->{issues}     = issuesat($itm, $library->{branchcode});
71     $library->{seen}       = lastseenat( $itm, $library->{branchcode} ) || undef;
72 }
73
74 $template->param(
75     biblionumber            => $biblionumber,
76     title                   => $biblio->title,
77     author                  => $biblio->author,
78     barcode                 => $item->barcode,
79     homebranch              => $item->homebranch,
80     holdingbranch           => $item->holdingbranch,
81     lastdate                => $lastdate ? $lastdate : 0,
82     count                   => $count,
83     libraries               => $libraries,
84 );
85
86 output_html_with_http_headers $input, $cookie, $template->output;
87 exit;
88
89 sub lastmove {
90     my ($itemnumber) = @_;
91     my $dbh = C4::Context->dbh;
92     my $sth = $dbh->prepare(
93 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
94     );
95     $sth->execute($itemnumber);
96     my ($date) = $sth->fetchrow_array;
97     return 0 unless $date;
98     $sth = $dbh->prepare(
99 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
100     );
101     $sth->execute( $itemnumber, $date );
102     my ($data) = $sth->fetchrow_hashref;
103     return 0 unless $data;
104     return $data;
105 }
106
107 sub issuessince {
108     my ( $itemnumber, $date ) = @_;
109     my $dbh = C4::Context->dbh;
110     my $sth =
111       $dbh->prepare("SELECT SUM(count) FROM (
112                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
113                         UNION ALL
114                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
115                      ) tmp");
116     $sth->execute( $itemnumber, $date, $itemnumber, $date );
117     return $sth->fetchrow_arrayref->[0];
118 }
119
120 sub issuesat {
121     my ( $itemnumber, $brcd ) = @_;
122     my $dbh = C4::Context->dbh;
123     my $sth = $dbh->prepare(
124     "SELECT SUM(count) FROM (
125         SELECT COUNT(*) AS count FROM     issues WHERE itemnumber = ? AND branchcode = ?
126         UNION ALL
127         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
128      ) tmp"
129     );
130     $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
131     return $sth->fetchrow_array;
132 }
133
134 sub lastseenat {
135     my ( $itm, $brc ) = @_;
136     my $dbh = C4::Context->dbh;
137     my $sth = $dbh->prepare(
138     "SELECT MAX(tstamp) FROM (
139         SELECT MAX(timestamp) AS tstamp FROM     issues WHERE itemnumber = ? AND branchcode = ?
140         UNION ALL
141         SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
142      ) tmp"
143     );
144     $sth->execute( $itm, $brc, $itm, $brc );
145     my ($date1) = $sth->fetchrow_array;
146     $sth = $dbh->prepare(
147     "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
148      UNION ALL
149      SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
150     ) tmp"
151     );
152     $sth->execute( $itm, $brc, $itm, $brc );
153     my ($date2) = $sth->fetchrow_array;
154
155     my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
156     return ($date);
157 }