changing DO_NOT_REMOVE to README.txt
[koha.git] / circ / bookcount.pl
1 #!/usr/bin/perl
2
3
4 #written 7/3/2002 by Finlay
5 #script to display reports
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Circulation;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Auth;
31 use C4::Branch; # GetBranches
32 use C4::Biblio; # GetBiblioItemData
33 use C4::Date;
34
35 my $input        = new CGI;
36 my $itm          = $input->param('itm');
37 my $bi           = $input->param('bi');
38 my $biblionumber = $input->param('biblioitemnumber');
39 my $branches     = GetBranches;
40
41 my $idata = itemdatanum($itm);
42 my $data  = GetBiblioItemData($bi);
43
44 my $homebranch    = $branches->{ $idata->{'homebranch'} }->{'branchname'};
45 my $holdingbranch = $branches->{ $idata->{'holdingbranch'} }->{'branchname'};
46
47 my ( $lastmove, $message ) = lastmove($itm);
48
49 my $lastdate;
50 my $count;
51 if ( not $lastmove ) {
52     $lastdate = $message;
53     $count = issuessince( $itm, 0 );
54 }
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.tmpl",
65         query           => $input,
66         type            => "intranet",
67         authnotrequired => 0,
68         flagsrequired   => { circulate => 1 },
69         debug           => 1,
70     }
71 );
72
73 my @branchloop;
74
75 foreach my $branchcode ( keys %$branches ) {
76     my %linebranch;
77     $linebranch{issues} = issuesat( $itm, $branchcode );
78     my $date = lastseenat( $itm, $branchcode );
79     $linebranch{seen}       = slashdate($date);
80     $linebranch{branchname} = $branches->{$branchcode}->{'branchname'};
81     push( @branchloop, \%linebranch );
82 }
83
84 $template->param(
85     biblionumber            => $biblionumber,
86     title                   => $data->{'title'},
87     author                  => $data->{'author'},
88     barcode                 => $idata->{'barcode'},
89     biblioitemnumber        => $bi,
90     homebranch              => $homebranch,
91     holdingbranch           => $holdingbranch,
92     lastdate                => format_date($lastdate),
93     count                   => $count,
94     branchloop              => \@branchloop,
95     intranetcolorstylesheet =>
96       C4::Context->preference("intranetcolorstylesheet"),
97     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
98     IntranetNav        => C4::Context->preference("IntranetNav"),
99 );
100
101 output_html_with_http_headers $input, $cookie, $template->output;
102
103
104 sub itemdatanum {
105     my ($itemnumber) = @_;
106     my $dbh          = C4::Context->dbh;
107     my $sth          = $dbh->prepare("select * from items where itemnumber=?");
108     $sth->execute($itemnumber);
109     my $data = $sth->fetchrow_hashref;
110     $sth->finish;
111     return ($data);
112 }
113
114 sub lastmove {
115     my ($itemnumber) = @_;
116     my $dbh          = C4::Context->dbh;
117     my $sth          =
118       $dbh->prepare(
119 "select max(branchtransfers.datearrived) from branchtransfers where branchtransfers.itemnumber=?"
120       );
121     $sth->execute($itemnumber);
122     my ($date) = $sth->fetchrow_array;
123     return ( 0, "Item has no branch transfers record" ) if not $date;
124     $sth =
125       $dbh->prepare(
126 "Select * from branchtransfers where branchtransfers.itemnumber=? and branchtransfers.datearrived=?"
127       );
128     $sth->execute( $itemnumber, $date );
129     my ($data) = $sth->fetchrow_hashref;
130     return ( 0, "Item has no branch transfers record" ) if not $data;
131     $sth->finish;
132     return ( $data, "" );
133 }
134
135 sub issuessince {
136     my ( $itemnumber, $date ) = @_;
137     my $dbh = C4::Context->dbh;
138     my $sth =
139       $dbh->prepare(
140 "Select count(*) from issues where issues.itemnumber=? and issues.timestamp > ?"
141       );
142     $sth->execute( $itemnumber, $date );
143     my $count = $sth->fetchrow_hashref;
144     $sth->finish;
145     return ( $count->{'count(*)'} );
146 }
147
148 sub issuesat {
149     my ( $itemnumber, $brcd ) = @_;
150     my $dbh = C4::Context->dbh;
151     my $sth =
152       $dbh->prepare(
153         "Select count(*) from issues where itemnumber=? and branchcode = ?");
154     $sth->execute( $itemnumber, $brcd );
155     my ($count) = $sth->fetchrow_array;
156     $sth->finish;
157     return ($count);
158 }
159
160 sub lastseenat {
161     my ( $itm, $brc ) = @_;
162     my $dbh = C4::Context->dbh;
163     my $sth =
164       $dbh->prepare(
165 "Select max(timestamp) from issues where itemnumber=? and branchcode = ?"
166       );
167     $sth->execute( $itm, $brc );
168     my ($date1) = $sth->fetchrow_array;
169     $sth->finish;
170     $sth =
171       $dbh->prepare(
172 "Select max(datearrived) from branchtransfers where itemnumber=? and tobranch = ?"
173       );
174     $sth->execute( $itm, $brc );
175     my ($date2) = $sth->fetchrow_array;
176     $sth->finish;
177
178     #FIXME: MJR thinks unsafe
179     $date2 =~ s/-//g;
180     $date2 =~ s/://g;
181     $date2 =~ s/ //g;
182     my $date;
183     if ( $date1 < $date2 ) {
184         $date = $date2;
185     }
186     else {
187         $date = $date1;
188     }
189     return ($date);
190 }
191
192 #####################################################
193 # write date....
194 sub slashdate {
195     my ($date) = @_;
196     if ( not $date ) {
197         return "never";
198     }
199     my ( $yr, $mo, $da, $hr, $mi ) = (
200         substr( $date, 0,  4 ),
201         substr( $date, 4,  2 ),
202         substr( $date, 6,  2 ),
203         substr( $date, 8,  2 ),
204         substr( $date, 10, 2 )
205     );
206     return "$hr:$mi  " . format_date("$yr-$mo-$da");
207 }