Bug 6819: Hide individual cancel order links when order line has been received
[koha.git] / circ / bookcount.pl
blob8509991ac43f28da473a0c66d79b2952239b52f7
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 = CGI->new;
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 flagsrequired => { circulate => "circulate_remaining_permissions" },
49 debug => 1,
53 output_and_exit( $input, $cookie, $template, 'unknown_biblio')
54 unless $biblio;
55 output_and_exit( $input, $cookie, $template, 'unknown_item')
56 unless $item;
58 my $lastdate;
59 my $count;
60 my $lastmove = lastmove($itm);
61 if ( not $lastmove ) {
62 $count = issuessince( $itm, 0 );
63 } else {
64 $lastdate = $lastmove->{'datearrived'};
65 $count = issuessince( $itm, $lastdate );
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 => $biblio->title,
78 author => $biblio->author,
79 barcode => $item->barcode,
80 homebranch => $item->homebranch,
81 holdingbranch => $item->holdingbranch,
82 lastdate => $lastdate ? $lastdate : 0,
83 count => $count,
84 libraries => $libraries,
87 output_html_with_http_headers $input, $cookie, $template->output;
88 exit;
90 sub lastmove {
91 my ($itemnumber) = @_;
92 my $dbh = C4::Context->dbh;
93 my $sth = $dbh->prepare(
94 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
96 $sth->execute($itemnumber);
97 my ($date) = $sth->fetchrow_array;
98 return 0 unless $date;
99 $sth = $dbh->prepare(
100 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
102 $sth->execute( $itemnumber, $date );
103 my ($data) = $sth->fetchrow_hashref;
104 return 0 unless $data;
105 return $data;
108 sub issuessince {
109 my ( $itemnumber, $date ) = @_;
110 my $dbh = C4::Context->dbh;
111 my $sth =
112 $dbh->prepare("SELECT SUM(count) FROM (
113 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
114 UNION ALL
115 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
116 ) tmp");
117 $sth->execute( $itemnumber, $date, $itemnumber, $date );
118 return $sth->fetchrow_arrayref->[0];
121 sub issuesat {
122 my ( $itemnumber, $brcd ) = @_;
123 my $dbh = C4::Context->dbh;
124 my $sth = $dbh->prepare(
125 "SELECT SUM(count) FROM (
126 SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? AND branchcode = ?
127 UNION ALL
128 SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
129 ) tmp"
131 $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
132 return $sth->fetchrow_array;
135 sub lastseenat {
136 my ( $itm, $brc ) = @_;
137 my $dbh = C4::Context->dbh;
138 my $sth = $dbh->prepare(
139 "SELECT MAX(tstamp) FROM (
140 SELECT MAX(timestamp) AS tstamp FROM issues WHERE itemnumber = ? AND branchcode = ?
141 UNION ALL
142 SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
143 ) tmp"
145 $sth->execute( $itm, $brc, $itm, $brc );
146 my ($date1) = $sth->fetchrow_array;
147 $sth = $dbh->prepare(
148 "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
149 UNION ALL
150 SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
151 ) tmp"
153 $sth->execute( $itm, $brc, $itm, $brc );
154 my ($date2) = $sth->fetchrow_array;
156 my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
157 return ($date);