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