Bug 16607: Remove CPL/MPL from two unit tests
[koha.git] / circ / bookcount.pl
blob09dc6fcbf053f9b9de9d6893365e03461b4c3aaa
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::Branch; # GetBranches
33 use C4::Biblio; # GetBiblioItemData
34 use Koha::DateUtils;
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 = lastmove($itm);
50 my $lastdate;
51 my $count;
52 if ( not $lastmove ) {
53 $count = issuessince( $itm, 0 );
54 } else {
55 $lastdate = $lastmove->{'datearrived'};
56 $count = issuessince( $itm, $lastdate );
59 # make the page ...
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63 template_name => "circ/bookcount.tt",
64 query => $input,
65 type => "intranet",
66 authnotrequired => 0,
67 flagsrequired => { circulate => "circulate_remaining_permissions" },
68 debug => 1,
72 my $branchloop = GetBranchesLoop(C4::Context->userenv->{branch});
73 foreach (@$branchloop) {
74 $_->{issues} = issuesat($itm, $_->{value});
75 $_->{seen} = lastseenat( $itm, $_->{value} ) || undef;
78 $template->param(
79 biblionumber => $biblionumber,
80 title => $data->{'title'},
81 author => $data->{'author'},
82 barcode => $idata->{'barcode'},
83 biblioitemnumber => $bi,
84 homebranch => $homebranch,
85 holdingbranch => $holdingbranch,
86 lastdate => $lastdate ? $lastdate : 0,
87 count => $count,
88 branchloop => $branchloop,
91 output_html_with_http_headers $input, $cookie, $template->output;
92 exit;
94 sub itemdatanum {
95 my ($itemnumber) = @_;
96 my $sth = C4::Context->dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
97 $sth->execute($itemnumber);
98 return $sth->fetchrow_hashref;
101 sub lastmove {
102 my ($itemnumber) = @_;
103 my $dbh = C4::Context->dbh;
104 my $sth = $dbh->prepare(
105 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
107 $sth->execute($itemnumber);
108 my ($date) = $sth->fetchrow_array;
109 return 0 unless $date;
110 $sth = $dbh->prepare(
111 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
113 $sth->execute( $itemnumber, $date );
114 my ($data) = $sth->fetchrow_hashref;
115 return 0 unless $data;
116 return $data;
119 sub issuessince {
120 my ( $itemnumber, $date ) = @_;
121 my $dbh = C4::Context->dbh;
122 my $sth =
123 $dbh->prepare("SELECT SUM(count) FROM (
124 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
125 UNION ALL
126 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
127 ) tmp");
128 $sth->execute( $itemnumber, $date, $itemnumber, $date );
129 return $sth->fetchrow_arrayref->[0];
132 sub issuesat {
133 my ( $itemnumber, $brcd ) = @_;
134 my $dbh = C4::Context->dbh;
135 my $sth = $dbh->prepare(
136 "SELECT SUM(count) FROM (
137 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
138 UNION ALL
139 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
140 ) tmp"
142 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
143 return $sth->fetchrow_array;
146 sub lastseenat {
147 my ( $itm, $brc ) = @_;
148 my $dbh = C4::Context->dbh;
149 my $sth = $dbh->prepare(
150 "SELECT MAX(tstamp) FROM (
151 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
152 UNION ALL
153 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
154 ) tmp"
156 $sth->execute( $itm, $brc, $itm, $brc );
157 my ($date1) = $sth->fetchrow_array;
158 $sth = $dbh->prepare(
159 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
160 UNION ALL
161 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
162 ) tmp"
164 $sth->execute( $itm, $brc, $itm, $brc );
165 my ($date2) = $sth->fetchrow_array;
167 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
168 return ($date);