Bug 8365: Add unit tests and fix QA issues
[koha.git] / circ / bookcount.pl
blob75bf319e1ca421ea1295e474cd05844218c4d288
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 = 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.tmpl",
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 my $date = lastseenat( $itm, $_->{value} );
75 my ($datechunk, $timechunk) = slashdate($date);
76 $_->{issues} = issuesat($itm, $_->{value});
77 $_->{seen} = $datechunk;
78 $_->{seentime} = $timechunk;
81 $template->param(
82 biblionumber => $biblionumber,
83 title => $data->{'title'},
84 author => $data->{'author'},
85 barcode => $idata->{'barcode'},
86 biblioitemnumber => $bi,
87 homebranch => $homebranch,
88 holdingbranch => $holdingbranch,
89 lastdate => $lastdate ? format_date($lastdate) : 0,
90 count => $count,
91 branchloop => $branchloop,
94 output_html_with_http_headers $input, $cookie, $template->output;
95 exit;
97 sub itemdatanum {
98 my ($itemnumber) = @_;
99 my $sth = C4::Context->dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
100 $sth->execute($itemnumber);
101 return $sth->fetchrow_hashref;
104 sub lastmove {
105 my ($itemnumber) = @_;
106 my $dbh = C4::Context->dbh;
107 my $sth = $dbh->prepare(
108 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
110 $sth->execute($itemnumber);
111 my ($date) = $sth->fetchrow_array;
112 return 0 unless $date;
113 $sth = $dbh->prepare(
114 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
116 $sth->execute( $itemnumber, $date );
117 my ($data) = $sth->fetchrow_hashref;
118 return 0 unless $data;
119 return $data;
122 sub issuessince {
123 my ( $itemnumber, $date ) = @_;
124 my $dbh = C4::Context->dbh;
125 my $sth =
126 $dbh->prepare("SELECT SUM(count) FROM (
127 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
128 UNION ALL
129 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
130 ) tmp");
131 $sth->execute( $itemnumber, $date, $itemnumber, $date );
132 return $sth->fetchrow_arrayref->[0];
135 sub issuesat {
136 my ( $itemnumber, $brcd ) = @_;
137 my $dbh = C4::Context->dbh;
138 my $sth = $dbh->prepare(
139 "SELECT SUM(count) FROM (
140 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
141 UNION ALL
142 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
143 ) tmp"
145 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
146 return $sth->fetchrow_array;
149 sub lastseenat {
150 my ( $itm, $brc ) = @_;
151 my $dbh = C4::Context->dbh;
152 my $sth = $dbh->prepare(
153 "SELECT MAX(tstamp) FROM (
154 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
155 UNION ALL
156 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
157 ) tmp"
159 $sth->execute( $itm, $brc, $itm, $brc );
160 my ($date1) = $sth->fetchrow_array;
161 $sth = $dbh->prepare(
162 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
163 UNION ALL
164 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
165 ) tmp"
167 $sth->execute( $itm, $brc, $itm, $brc );
168 my ($date2) = $sth->fetchrow_array;
170 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
171 return ($date);
174 #####################################################
175 # return date and time from timestamp
176 sub slashdate {
177 my ($date) = @_;
178 $date or return;
179 return (
180 format_date($date),
181 substr($date,11,5)