Bug 15758: Koha::Libraries - Remove GetBranchesLoop
[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 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;
35 use Koha::Libraries;
36
37 my $input        = new CGI;
38 my $itm          = $input->param('itm');
39 my $bi           = $input->param('bi');
40 my $biblionumber = $input->param('biblionumber');
41 my $branches     = GetBranches;
42
43 my $idata = itemdatanum($itm);
44 my $data  = GetBiblioItemData($bi);
45
46 my $homebranch    = $branches->{ $idata->{'homebranch'}    }->{'branchname'};
47 my $holdingbranch = $branches->{ $idata->{'holdingbranch'} }->{'branchname'};
48
49 my $lastmove = lastmove($itm);
50
51 my $lastdate;
52 my $count;
53 if ( not $lastmove ) {
54     $count = issuessince( $itm, 0 );
55 } else {
56     $lastdate = $lastmove->{'datearrived'};
57     $count = issuessince( $itm, $lastdate );
58 }
59
60 # make the page ...
61
62 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63     {
64         template_name   => "circ/bookcount.tt",
65         query           => $input,
66         type            => "intranet",
67         authnotrequired => 0,
68         flagsrequired   => { circulate => "circulate_remaining_permissions" },
69         debug           => 1,
70     }
71 );
72
73 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
74 for my $library ( @$libraries ) {
75     $library->{selected} = 1 if $library->{branchcode} eq C4::Context->userenv->{branch};
76     $library->{issues}     = issuesat($itm, $library->{branchcode});
77     $library->{seen}       = lastseenat( $itm, $library->{branchcode} ) || undef;
78 }
79
80 $template->param(
81     biblionumber            => $biblionumber,
82     title                   => $data->{'title'},
83     author                  => $data->{'author'},
84     barcode                 => $idata->{'barcode'},
85     biblioitemnumber        => $bi,
86     homebranch              => $homebranch,
87     holdingbranch           => $holdingbranch,
88     lastdate                => $lastdate ? $lastdate : 0,
89     count                   => $count,
90     libraries               => $libraries,
91 );
92
93 output_html_with_http_headers $input, $cookie, $template->output;
94 exit;
95
96 sub itemdatanum {
97     my ($itemnumber) = @_;
98     my $sth = C4::Context->dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
99     $sth->execute($itemnumber);
100     return $sth->fetchrow_hashref;
101 }
102
103 sub lastmove {
104     my ($itemnumber) = @_;
105     my $dbh = C4::Context->dbh;
106     my $sth = $dbh->prepare(
107 "SELECT max(branchtransfers.datearrived) FROM branchtransfers WHERE branchtransfers.itemnumber=?"
108     );
109     $sth->execute($itemnumber);
110     my ($date) = $sth->fetchrow_array;
111     return 0 unless $date;
112     $sth = $dbh->prepare(
113 "SELECT * FROM branchtransfers WHERE branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
114     );
115     $sth->execute( $itemnumber, $date );
116     my ($data) = $sth->fetchrow_hashref;
117     return 0 unless $data;
118     return $data;
119 }
120
121 sub issuessince {
122     my ( $itemnumber, $date ) = @_;
123     my $dbh = C4::Context->dbh;
124     my $sth =
125       $dbh->prepare("SELECT SUM(count) FROM (
126                         SELECT COUNT(*) AS count FROM issues WHERE itemnumber = ? and timestamp > ?
127                         UNION ALL
128                         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? and timestamp > ?
129                      ) tmp");
130     $sth->execute( $itemnumber, $date, $itemnumber, $date );
131     return $sth->fetchrow_arrayref->[0];
132 }
133
134 sub issuesat {
135     my ( $itemnumber, $brcd ) = @_;
136     my $dbh = C4::Context->dbh;
137     my $sth = $dbh->prepare(
138     "SELECT SUM(count) FROM (
139         SELECT COUNT(*) AS count FROM     issues WHERE itemnumber = ? AND branchcode = ?
140         UNION ALL
141         SELECT COUNT(*) AS count FROM old_issues WHERE itemnumber = ? AND branchcode = ?
142      ) tmp"
143     );
144     $sth->execute( $itemnumber, $brcd, $itemnumber, $brcd );
145     return $sth->fetchrow_array;
146 }
147
148 sub lastseenat {
149     my ( $itm, $brc ) = @_;
150     my $dbh = C4::Context->dbh;
151     my $sth = $dbh->prepare(
152     "SELECT MAX(tstamp) FROM (
153         SELECT MAX(timestamp) AS tstamp FROM     issues WHERE itemnumber = ? AND branchcode = ?
154         UNION ALL
155         SELECT MAX(timestamp) AS tstamp FROM old_issues WHERE itemnumber = ? AND branchcode = ?
156      ) tmp"
157     );
158     $sth->execute( $itm, $brc, $itm, $brc );
159     my ($date1) = $sth->fetchrow_array;
160     $sth = $dbh->prepare(
161     "SELECT MAX(transfer) FROM (SELECT max(datearrived) AS transfer FROM branchtransfers WHERE itemnumber=? AND tobranch = ?
162      UNION ALL
163      SELECT max(datesent) AS transfer FROM branchtransfers WHERE itemnumber=? AND frombranch = ?
164     ) tmp"
165     );
166     $sth->execute( $itm, $brc, $itm, $brc );
167     my ($date2) = $sth->fetchrow_array;
168
169     my $date = ( $date1 lt $date2 ) ? $date2 : $date1 ;
170     return ($date);
171 }