Bug 35492: Open holds tab by default on opac-user.pl after suspending a hold
[koha.git] / circ / bookcount.pl
1 #!/usr/bin/perl
2
3 #written 7/3/2002 by Finlay
4 #script to display reports
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
19 #
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>.
22
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output qw( output_and_exit output_html_with_http_headers );
27 use C4::Auth qw( get_template_and_user );
28 use Koha::Biblios;
29 use Koha::Libraries;
30
31 my $input        = CGI->new;
32 my $itm          = $input->param('itm');
33 my $biblionumber = $input->param('biblionumber');
34
35 my $biblio = Koha::Biblios->find( $biblionumber );
36 my $item   = Koha::Items->find( $itm );
37
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "circ/bookcount.tt",
42         query           => $input,
43         type            => "intranet",
44         flagsrequired   => { circulate => "circulate_remaining_permissions" },
45     }
46 );
47
48 output_and_exit( $input, $cookie, $template, 'unknown_biblio')
49     unless $biblio;
50 output_and_exit( $input, $cookie, $template, 'unknown_item')
51     unless $item;
52
53 my $lastdate;
54 my $count;
55 my $lastmove = lastmove($itm);
56 if ( not $lastmove ) {
57     $count = issuessince( $itm, 0 );
58 } else {
59     $lastdate = $lastmove->{'datearrived'};
60     $count = issuessince( $itm, $lastdate );
61 }
62
63 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
64 for my $library ( @$libraries ) {
65     $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
66     $library->{issues}     = issuesat($itm, $library->{branchcode});
67     $library->{seen}       = lastseenat( $itm, $library->{branchcode} ) || undef;
68 }
69
70 $template->param(
71     biblio                  => $biblio,
72     biblionumber            => $biblionumber,
73     title                   => $biblio->title,
74     author                  => $biblio->author,
75     barcode                 => $item->barcode,
76     homebranch              => $item->homebranch,
77     holdingbranch           => $item->holdingbranch,
78     lastdate                => $lastdate ? $lastdate : 0,
79     count                   => $count,
80     libraries               => $libraries,
81 );
82
83 output_html_with_http_headers $input, $cookie, $template->output;
84 exit;
85
86 sub lastmove {
87     my ($itemnumber) = @_;
88     my $dbh = C4::Context->dbh;
89     my $sth = $dbh->prepare(
90 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
91     );
92     $sth->execute($itemnumber);
93     my ($date) = $sth->fetchrow_array;
94     return 0 unless $date;
95     $sth = $dbh->prepare(
96 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
97     );
98     $sth->execute( $itemnumber, $date );
99     my ($data) = $sth->fetchrow_hashref;
100     return 0 unless $data;
101     return $data;
102 }
103
104 sub issuessince {
105     my ( $itemnumber, $date ) = @_;
106     my $dbh = C4::Context->dbh;
107     my $sth =
108       $dbh->prepare("SELECT SUM(count) FROM (
109                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
110                         UNION ALL
111                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
112                      ) tmp");
113     $sth->execute( $itemnumber, $date, $itemnumber, $date );
114     return $sth->fetchrow_arrayref->[0];
115 }
116
117 sub issuesat {
118     my ( $itemnumber, $brcd ) = @_;
119     my $dbh = C4::Context->dbh;
120     my $sth = $dbh->prepare(
121     "SELECT SUM(count) FROM (
122         SELECT COUNT(*) AS count FROM     issues WHERE itemnumber = ? AND branchcode = ?
123         UNION ALL
124         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
125      ) tmp"
126     );
127     $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
128     return $sth->fetchrow_array;
129 }
130
131 sub lastseenat {
132     my ( $itm, $brc ) = @_;
133     my $dbh = C4::Context->dbh;
134     my $sth = $dbh->prepare(
135     "SELECT MAX(tstamp) FROM (
136         SELECT MAX(timestamp) AS tstamp FROM     issues WHERE itemnumber = ? AND branchcode = ?
137         UNION ALL
138         SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
139      ) tmp"
140     );
141     $sth->execute( $itm, $brc, $itm, $brc );
142     my ($date1) = $sth->fetchrow_array;
143     $sth = $dbh->prepare(
144     "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
145      UNION ALL
146      SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
147     ) tmp"
148     );
149     $sth->execute( $itm, $brc, $itm, $brc );
150     my ($date2) = $sth->fetchrow_array;
151
152     my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
153     return ($date);
154 }