Bug 10306: (QA follow-up) More feedback for admins in koha2marclinks
[koha.git] / circ / bookcount.pl
blob2976a05b669c13143081f8a2a8031ecaba14d328
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 strict;
24 #use warnings; FIXME - Bug 2505
25 use CGI qw ( -utf8 );
26 use C4::Debug;
27 use C4::Context;
28 use C4::Circulation;
29 use C4::Output;
30 use C4::Koha;
31 use C4::Auth;
32 use C4::Biblio; # GetBiblioItemData
33 use Koha::DateUtils;
34 use Koha::Libraries;
36 my $input = new CGI;
37 my $itm = $input->param('itm');
38 my $bi = $input->param('bi');
39 my $biblionumber = $input->param('biblionumber');
41 my $idata = itemdatanum($itm);
42 my $data = GetBiblioItemData($bi);
44 my $lastmove = lastmove($itm);
46 my $lastdate;
47 my $count;
48 if ( not $lastmove ) {
49 $count = issuessince( $itm, 0 );
50 } else {
51 $lastdate = $lastmove->{'datearrived'};
52 $count = issuessince( $itm, $lastdate );
55 # make the page ...
57 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59 template_name => "circ/bookcount.tt",
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => { circulate => "circulate_remaining_permissions" },
64 debug => 1,
68 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
69 for my $library ( @$libraries ) {
70 $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
71 $library->{issues} = issuesat($itm, $library->{branchcode});
72 $library->{seen} = lastseenat( $itm, $library->{branchcode} ) || undef;
75 $template->param(
76 biblionumber => $biblionumber,
77 title => $data->{'title'},
78 author => $data->{'author'},
79 barcode => $idata->{'barcode'},
80 biblioitemnumber => $bi,
81 homebranch => $idata->{homebranch},
82 holdingbranch => $idata->{holdingbranch},
83 lastdate => $lastdate ? $lastdate : 0,
84 count => $count,
85 libraries => $libraries,
88 output_html_with_http_headers $input, $cookie, $template->output;
89 exit;
91 sub itemdatanum {
92 my ($itemnumber) = @_;
93 my $sth = C4::Context->dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
94 $sth->execute($itemnumber);
95 return $sth->fetchrow_hashref;
98 sub lastmove {
99 my ($itemnumber) = @_;
100 my $dbh = C4::Context->dbh;
101 my $sth = $dbh->prepare(
102 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
104 $sth->execute($itemnumber);
105 my ($date) = $sth->fetchrow_array;
106 return 0 unless $date;
107 $sth = $dbh->prepare(
108 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
110 $sth->execute( $itemnumber, $date );
111 my ($data) = $sth->fetchrow_hashref;
112 return 0 unless $data;
113 return $data;
116 sub issuessince {
117 my ( $itemnumber, $date ) = @_;
118 my $dbh = C4::Context->dbh;
119 my $sth =
120 $dbh->prepare("SELECT SUM(count) FROM (
121 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
122 UNION ALL
123 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
124 ) tmp");
125 $sth->execute( $itemnumber, $date, $itemnumber, $date );
126 return $sth->fetchrow_arrayref->[0];
129 sub issuesat {
130 my ( $itemnumber, $brcd ) = @_;
131 my $dbh = C4::Context->dbh;
132 my $sth = $dbh->prepare(
133 "SELECT SUM(count) FROM (
134 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
135 UNION ALL
136 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
137 ) tmp"
139 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
140 return $sth->fetchrow_array;
143 sub lastseenat {
144 my ( $itm, $brc ) = @_;
145 my $dbh = C4::Context->dbh;
146 my $sth = $dbh->prepare(
147 "SELECT MAX(tstamp) FROM (
148 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
149 UNION ALL
150 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
151 ) tmp"
153 $sth->execute( $itm, $brc, $itm, $brc );
154 my ($date1) = $sth->fetchrow_array;
155 $sth = $dbh->prepare(
156 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
157 UNION ALL
158 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
159 ) tmp"
161 $sth->execute( $itm, $brc, $itm, $brc );
162 my ($date2) = $sth->fetchrow_array;
164 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
165 return ($date);