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