Bug 14324: Display "Add Child" for Organisations on circ/circulation.pl
[koha.git] / circ / reserveratios.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use Date::Calc qw/Today Add_Delta_YM/;
26
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Dates qw/format_date format_date_in_iso/;
31 use C4::Debug;
32 use C4::Biblio qw/GetMarcBiblio GetRecordValue GetFrameworkCode/;
33 use C4::Acquisition qw/GetOrdersByBiblionumber/;
34
35 my $input = new CGI;
36 my $startdate       = $input->param('from');
37 my $enddate         = $input->param('to');
38 my $ratio           = $input->param('ratio');
39 my $include_ordered = $input->param('include_ordered');
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "circ/reserveratios.tt",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { circulate => "circulate_remaining_permissions" },
48         debug           => 1,
49     }
50 );
51
52 my $booksellerid = $input->param('booksellerid') // '';
53 my $basketno = $input->param('basketno') // '';
54 if ($booksellerid && $basketno) {
55      $template->param( booksellerid => $booksellerid, basketno => $basketno );
56 }
57
58 my ( $year, $month, $day ) = Today();
59 my $todaysdate     = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
60 # Find yesterday for the default shelf pull start and end dates
61 #    A default of the prior years's holds is a reasonable way to pull holds 
62 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
63
64 #               Predefine the start and end dates if they are not already defined
65 #               Check if null, should string match, if so set start and end date to yesterday
66 if (!defined($startdate) or $startdate !~ s/^\s*(\S+)\s*$/$1/) {   # strip spaces, remove Taint
67         $startdate = format_date($datelastyear);
68 }
69 if (!defined($enddate)   or $enddate   !~ s/^\s*(\S+)\s*$/$1/) {   # strip spaces, remove Taint
70         $enddate   = format_date($todaysdate);
71 }
72 if (!defined($ratio)) {
73         $ratio = 3;
74 }
75 # Force to be a number
76 $ratio += 0;
77 if ($ratio <= 0) {
78     $ratio = 1; # prevent division by zero
79 }
80
81 my $dbh    = C4::Context->dbh;
82 my $sqldatewhere = "";
83 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
84 my @query_params = ();
85 if ($startdate) {
86     $sqldatewhere .= " AND reservedate >= ?";
87     push @query_params, format_date_in_iso($startdate);
88 }
89 if ($enddate) {
90     $sqldatewhere .= " AND reservedate <= ?";
91     push @query_params, format_date_in_iso($enddate);
92 }
93
94 my $nfl_comparison = $include_ordered ? '<=' : '=';
95 my $strsth =
96 "SELECT reservedate,
97         reserves.borrowernumber as borrowernumber,
98         reserves.biblionumber,
99         reserves.branchcode as branch,
100         items.holdingbranch,
101         items.itemcallnumber,
102         items.itemnumber,
103         GROUP_CONCAT(DISTINCT items.itemcallnumber 
104                         ORDER BY items.itemnumber SEPARATOR '<br/>') as listcall,
105         GROUP_CONCAT(DISTINCT homebranch
106             ORDER BY items.itemnumber SEPARATOR '<br/>') as homebranch_list,
107         GROUP_CONCAT(DISTINCT holdingbranch 
108             ORDER BY items.itemnumber SEPARATOR '<br/>') as holdingbranch_list,
109         GROUP_CONCAT(DISTINCT items.location 
110                         ORDER BY items.itemnumber SEPARATOR '<br/>') as l_location,
111         GROUP_CONCAT(DISTINCT items.itype 
112                         ORDER BY items.itemnumber SEPARATOR '<br/>') as l_itype,
113
114         reserves.found,
115         biblio.title,
116         biblio.author,
117         count(DISTINCT reserves.borrowernumber) as reservecount, 
118         count(DISTINCT items.itemnumber) as itemcount 
119  FROM  reserves
120  LEFT JOIN items ON items.biblionumber=reserves.biblionumber 
121  LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
122  WHERE
123  notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
124  $sqldatewhere
125 ";
126
127 if (C4::Context->preference('IndependentBranches')){
128     $strsth .= " AND items.holdingbranch=? ";
129     push @query_params, C4::Context->userenv->{'branch'};
130 }
131
132 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
133
134 $template->param(sql => $strsth);
135 my $sth = $dbh->prepare($strsth);
136 $sth->execute(@query_params);
137
138 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
139 my @reservedata;
140 while ( my $data = $sth->fetchrow_hashref ) {
141     my $thisratio = $data->{reservecount} / $data->{itemcount};
142     my $ratiocalc = ($thisratio / $ratio);
143     ($thisratio / $ratio) >= 1 or next;  # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
144     my $record = GetMarcBiblio($data->{biblionumber});
145     $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
146     push(
147         @reservedata,
148         {
149             reservedate        => format_date( $data->{reservedate} ),
150             priority           => $data->{priority},
151             name               => $data->{borrower},
152             title              => $data->{title},
153             subtitle           => $data->{subtitle},
154             author             => $data->{author},
155             itemnum            => $data->{itemnumber},
156             biblionumber       => $data->{biblionumber},
157             holdingbranch      => $data->{holdingbranch},
158             homebranch_list    => $data->{homebranch_list},
159             holdingbranch_list => $data->{holdingbranch_list},
160             branch             => $data->{branch},
161             itemcallnumber     => $data->{itemcallnumber},
162             location           => $data->{l_location},
163             itype              => $data->{l_itype},
164             reservecount       => $data->{reservecount},
165             itemcount          => $data->{itemcount},
166             ratiocalc          => sprintf( "%.0d", $ratio_atleast1 ? ( $thisratio / $ratio ) : $thisratio ),
167             thisratio => sprintf( "%.2f", $thisratio ),
168             thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
169             listcall => $data->{listcall}
170         }
171     );
172 }
173
174 for my $rd ( @reservedata ) {
175     next unless $rd->{biblionumber};
176     $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
177 }
178
179 $template->param(
180     ratio_atleast1  => $ratio_atleast1,
181     todaysdate      => format_date($todaysdate),
182     from            => $startdate,
183     to              => $enddate,
184     ratio           => $ratio,
185     include_ordered => $include_ordered,
186     reserveloop     => \@reservedata,
187 );
188
189 output_html_with_http_headers $input, $cookie, $template->output;
190
191 sub CountPendingOrdersByBiblionumber {
192     my $biblionumber = shift;
193     my @orders = GetOrdersByBiblionumber( $biblionumber );
194     my $cnt = 0;
195     if (scalar(@orders)) {
196         for my $order ( @orders ) {
197             next if $order->{datecancellationprinted};
198             my $onum = $order->{quantity} // 0;
199             my $rnum = $order->{quantityreceived} // 0;
200             next if $rnum >= $onum;
201             $cnt += ($onum - $rnum);
202         }
203     }
204     return $cnt;
205 }