Bug 23049: Add DBIC Classes
[koha.git] / circ / bookcount.pl
blob0d869464ccd682880ff99e52ff9370a56203b79f
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 );
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "circ/bookcount.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { circulate => "circulate_remaining_permissions" },
50 debug => 1,
54 output_and_exit( $input, $cookie, $template, 'unknown_biblio')
55 unless $biblio;
56 output_and_exit( $input, $cookie, $template, 'unknown_item')
57 unless $item;
59 my $lastdate;
60 my $count;
61 my $lastmove = lastmove($itm);
62 if ( not $lastmove ) {
63 $count = issuessince( $itm, 0 );
64 } else {
65 $lastdate = $lastmove->{'datearrived'};
66 $count = issuessince( $itm, $lastdate );
69 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
70 for my $library ( @$libraries ) {
71 $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
72 $library->{issues} = issuesat($itm, $library->{branchcode});
73 $library->{seen} = lastseenat( $itm, $library->{branchcode} ) || undef;
76 $template->param(
77 biblionumber => $biblionumber,
78 title => $biblio->title,
79 author => $biblio->author,
80 barcode => $item->barcode,
81 homebranch => $item->homebranch,
82 holdingbranch => $item->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 lastmove {
92 my ($itemnumber) = @_;
93 my $dbh = C4::Context->dbh;
94 my $sth = $dbh->prepare(
95 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
97 $sth->execute($itemnumber);
98 my ($date) = $sth->fetchrow_array;
99 return 0 unless $date;
100 $sth = $dbh->prepare(
101 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
103 $sth->execute( $itemnumber, $date );
104 my ($data) = $sth->fetchrow_hashref;
105 return 0 unless $data;
106 return $data;
109 sub issuessince {
110 my ( $itemnumber, $date ) = @_;
111 my $dbh = C4::Context->dbh;
112 my $sth =
113 $dbh->prepare("SELECT SUM(count) FROM (
114 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
115 UNION ALL
116 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
117 ) tmp");
118 $sth->execute( $itemnumber, $date, $itemnumber, $date );
119 return $sth->fetchrow_arrayref->[0];
122 sub issuesat {
123 my ( $itemnumber, $brcd ) = @_;
124 my $dbh = C4::Context->dbh;
125 my $sth = $dbh->prepare(
126 "SELECT SUM(count) FROM (
127 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
128 UNION ALL
129 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
130 ) tmp"
132 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
133 return $sth->fetchrow_array;
136 sub lastseenat {
137 my ( $itm, $brc ) = @_;
138 my $dbh = C4::Context->dbh;
139 my $sth = $dbh->prepare(
140 "SELECT MAX(tstamp) FROM (
141 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
142 UNION ALL
143 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
144 ) tmp"
146 $sth->execute( $itm, $brc, $itm, $brc );
147 my ($date1) = $sth->fetchrow_array;
148 $sth = $dbh->prepare(
149 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
150 UNION ALL
151 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
152 ) tmp"
154 $sth->execute( $itm, $brc, $itm, $brc );
155 my ($date2) = $sth->fetchrow_array;
157 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
158 return ($date);