Bug 26352: Switch from using call() to call_recursive()
[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     biblionumber            => $biblionumber,
72     title                   => $biblio->title,
73     author                  => $biblio->author,
74     barcode                 => $item->barcode,
75     homebranch              => $item->homebranch,
76     holdingbranch           => $item->holdingbranch,
77     lastdate                => $lastdate ? $lastdate : 0,
78     count                   => $count,
79     libraries               => $libraries,
80 );
81
82 output_html_with_http_headers $input, $cookie, $template->output;
83 exit;
84
85 sub lastmove {
86     my ($itemnumber) = @_;
87     my $dbh = C4::Context->dbh;
88     my $sth = $dbh->prepare(
89 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
90     );
91     $sth->execute($itemnumber);
92     my ($date) = $sth->fetchrow_array;
93     return 0 unless $date;
94     $sth = $dbh->prepare(
95 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
96     );
97     $sth->execute( $itemnumber, $date );
98     my ($data) = $sth->fetchrow_hashref;
99     return 0 unless $data;
100     return $data;
101 }
102
103 sub issuessince {
104     my ( $itemnumber, $date ) = @_;
105     my $dbh = C4::Context->dbh;
106     my $sth =
107       $dbh->prepare("SELECT SUM(count) FROM (
108                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
109                         UNION ALL
110                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
111                      ) tmp");
112     $sth->execute( $itemnumber, $date, $itemnumber, $date );
113     return $sth->fetchrow_arrayref->[0];
114 }
115
116 sub issuesat {
117     my ( $itemnumber, $brcd ) = @_;
118     my $dbh = C4::Context->dbh;
119     my $sth = $dbh->prepare(
120     "SELECT SUM(count) FROM (
121         SELECT COUNT(*) AS count FROM     issues WHERE itemnumber = ? AND branchcode = ?
122         UNION ALL
123         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
124      ) tmp"
125     );
126     $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
127     return $sth->fetchrow_array;
128 }
129
130 sub lastseenat {
131     my ( $itm, $brc ) = @_;
132     my $dbh = C4::Context->dbh;
133     my $sth = $dbh->prepare(
134     "SELECT MAX(tstamp) FROM (
135         SELECT MAX(timestamp) AS tstamp FROM     issues WHERE itemnumber = ? AND branchcode = ?
136         UNION ALL
137         SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
138      ) tmp"
139     );
140     $sth->execute( $itm, $brc, $itm, $brc );
141     my ($date1) = $sth->fetchrow_array;
142     $sth = $dbh->prepare(
143     "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
144      UNION ALL
145      SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
146     ) tmp"
147     );
148     $sth->execute( $itm, $brc, $itm, $brc );
149     my ($date2) = $sth->fetchrow_array;
150
151     my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
152     return ($date);
153 }