Bug 8636 - add Moose dependency to the dependencies
[koha.git] / circ / bookcount.pl
blob228772efe8e183d20145dda3d8ea604e8a463c6f
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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use CGI;
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::Branch; # GetBranches
33 use C4::Biblio; # GetBiblioItemData
34 use C4::Dates qw/format_date/;
36 my $input = new CGI;
37 my $itm = $input->param('itm');
38 my $bi = $input->param('bi');
39 my $biblionumber = $input->param('biblionumber');
40 my $branches = GetBranches;
42 my $idata = itemdatanum($itm);
43 my $data = GetBiblioItemData($bi);
45 my $homebranch = $branches->{ $idata->{'homebranch'} }->{'branchname'};
46 my $holdingbranch = $branches->{ $idata->{'holdingbranch'} }->{'branchname'};
48 my ( $lastmove, $message ) = lastmove($itm);
50 my $lastdate;
51 my $count;
52 if ( not $lastmove ) {
53 # $lastdate = $message;
54 $count = issuessince( $itm, 0 );
55 } else {
56 $lastdate = $lastmove->{'datearrived'};
57 $count = issuessince( $itm, $lastdate );
60 # make the page ...
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64 template_name => "circ/bookcount.tmpl",
65 query => $input,
66 type => "intranet",
67 authnotrequired => 0,
68 flagsrequired => { circulate => "circulate_remaining_permissions" },
69 debug => 1,
73 my $branchloop = GetBranchesLoop(C4::Context->userenv->{branch});
74 foreach (@$branchloop) {
75 my $date = lastseenat( $itm, $_->{value} );
76 my ($datechunk, $timechunk) = slashdate($date);
77 $_->{issues} = issuesat($itm, $_->{value});
78 $_->{seen} = $datechunk;
79 $_->{seentime} = $timechunk;
82 ### $lastdate
84 $template->param(
85 biblionumber => $biblionumber,
86 title => $data->{'title'},
87 author => $data->{'author'},
88 barcode => $idata->{'barcode'},
89 biblioitemnumber => $bi,
90 homebranch => $homebranch,
91 holdingbranch => $holdingbranch,
92 lastdate => $lastdate ? format_date($lastdate) : $message,
93 count => $count,
94 branchloop => $branchloop,
97 output_html_with_http_headers $input, $cookie, $template->output;
98 exit;
100 sub itemdatanum {
101 my ($itemnumber) = @_;
102 my $sth = C4::Context->dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
103 $sth->execute($itemnumber);
104 return $sth->fetchrow_hashref;
107 sub lastmove {
108 my ($itemnumber) = @_;
109 my $dbh = C4::Context->dbh;
110 my $sth = $dbh->prepare(
111 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
113 $sth->execute($itemnumber);
114 my ($date) = $sth->fetchrow_array;
115 return ( 0, "Item has no branch transfers record" ) if not $date;
116 $sth = $dbh->prepare(
117 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
119 $sth->execute( $itemnumber, $date );
120 my ($data) = $sth->fetchrow_hashref;
121 return ( 0, "Item has no branch transfers record" ) if not $data;
122 return ( $data, "" );
125 sub issuessince {
126 my ( $itemnumber, $date ) = @_;
127 my $dbh = C4::Context->dbh;
128 my $sth =
129 $dbh->prepare("SELECT SUM(count) FROM (
130 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
131 UNION ALL
132 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
133 ) tmp");
134 $sth->execute( $itemnumber, $date, $itemnumber, $date );
135 return $sth->fetchrow_arrayref->[0];
138 sub issuesat {
139 my ( $itemnumber, $brcd ) = @_;
140 my $dbh = C4::Context->dbh;
141 my $sth = $dbh->prepare(
142 "SELECT SUM(count) FROM (
143 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
144 UNION ALL
145 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
146 ) tmp"
148 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
149 return $sth->fetchrow_array;
152 sub lastseenat {
153 my ( $itm, $brc ) = @_;
154 my $dbh = C4::Context->dbh;
155 my $sth = $dbh->prepare(
156 "SELECT MAX(tstamp) FROM (
157 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
158 UNION ALL
159 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
160 ) tmp"
162 $sth->execute( $itm, $brc, $itm, $brc );
163 my ($date1) = $sth->fetchrow_array;
164 $sth = $dbh->prepare(
165 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
166 UNION ALL
167 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
168 ) tmp"
170 $sth->execute( $itm, $brc, $itm, $brc );
171 my ($date2) = $sth->fetchrow_array;
173 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
174 return ($date);
177 #####################################################
178 # return date and time from timestamp
179 sub slashdate {
180 my ($date) = @_;
181 $date or return;
182 # warn "slashdate($date)...";
183 return (
184 format_date($date),
185 substr($date,11,5)