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