Bug 19983: Switch single-column templates to Bootstrap grid: Authorities
[koha.git] / circ / bookcount.pl
blob438d85126e02267eb4ed9fdc2a2d565d3ac10547
1 #!/usr/bin/perl
3 #written 7/3/2002 by Finlay
4 #script to display reports
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
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.
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.
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>.
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;
35 my $input = new CGI;
36 my $itm = $input->param('itm');
37 my $biblionumber = $input->param('biblionumber');
39 my $biblio = Koha::Biblios->find( $biblionumber );
40 my $item = Koha::Items->find( $itm );
42 if ( !defined $biblio or !defined $item ) {
43 print $input->redirect("/cgi-bin/koha/errors/400.pl");
46 my $lastmove = lastmove($itm);
48 my $lastdate;
49 my $count;
50 if ( not $lastmove ) {
51 $count = issuessince( $itm, 0 );
52 } else {
53 $lastdate = $lastmove->{'datearrived'};
54 $count = issuessince( $itm, $lastdate );
57 # make the page ...
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61 template_name => "circ/bookcount.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { circulate => "circulate_remaining_permissions" },
66 debug => 1,
70 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
71 for my $library ( @$libraries ) {
72 $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
73 $library->{issues} = issuesat($itm, $library->{branchcode});
74 $library->{seen} = lastseenat( $itm, $library->{branchcode} ) || undef;
77 $template->param(
78 biblionumber => $biblionumber,
79 title => $biblio->title,
80 author => $biblio->author,
81 barcode => $item->barcode,
82 homebranch => $item->homebranch,
83 holdingbranch => $item->holdingbranch,
84 lastdate => $lastdate ? $lastdate : 0,
85 count => $count,
86 libraries => $libraries,
89 output_html_with_http_headers $input, $cookie, $template->output;
90 exit;
92 sub lastmove {
93 my ($itemnumber) = @_;
94 my $dbh = C4::Context->dbh;
95 my $sth = $dbh->prepare(
96 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
98 $sth->execute($itemnumber);
99 my ($date) = $sth->fetchrow_array;
100 return 0 unless $date;
101 $sth = $dbh->prepare(
102 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
104 $sth->execute( $itemnumber, $date );
105 my ($data) = $sth->fetchrow_hashref;
106 return 0 unless $data;
107 return $data;
110 sub issuessince {
111 my ( $itemnumber, $date ) = @_;
112 my $dbh = C4::Context->dbh;
113 my $sth =
114 $dbh->prepare("SELECT SUM(count) FROM (
115 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
116 UNION ALL
117 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
118 ) tmp");
119 $sth->execute( $itemnumber, $date, $itemnumber, $date );
120 return $sth->fetchrow_arrayref->[0];
123 sub issuesat {
124 my ( $itemnumber, $brcd ) = @_;
125 my $dbh = C4::Context->dbh;
126 my $sth = $dbh->prepare(
127 "SELECT SUM(count) FROM (
128 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
129 UNION ALL
130 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
131 ) tmp"
133 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
134 return $sth->fetchrow_array;
137 sub lastseenat {
138 my ( $itm, $brc ) = @_;
139 my $dbh = C4::Context->dbh;
140 my $sth = $dbh->prepare(
141 "SELECT MAX(tstamp) FROM (
142 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
143 UNION ALL
144 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
145 ) tmp"
147 $sth->execute( $itm, $brc, $itm, $brc );
148 my ($date1) = $sth->fetchrow_array;
149 $sth = $dbh->prepare(
150 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
151 UNION ALL
152 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
153 ) tmp"
155 $sth->execute( $itm, $brc, $itm, $brc );
156 my ($date2) = $sth->fetchrow_array;
158 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
159 return ($date);