Bug 25642: Fix description of new feature in update_dbix_class_files.pl
[koha.git] / circ / reserveratios.pl
blob77117934d0690f4e868689707980ff7a4e0cab79
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use Date::Calc qw/Today Add_Delta_YM/;
25 use POSIX qw( ceil );
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Debug;
31 use C4::Acquisition qw/GetOrdersByBiblionumber/;
32 use Koha::DateUtils;
33 use Koha::Acquisition::Baskets;
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 my $include_suspended = $input->param('include_suspended');
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 template_name => "circ/reserveratios.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => { circulate => "circulate_remaining_permissions" },
49 debug => 1,
53 my $booksellerid = $input->param('booksellerid') // '';
54 my $basketno = $input->param('basketno') // '';
55 if ($booksellerid && $basketno) {
56 $template->param( booksellerid => $booksellerid, basketno => $basketno );
59 my $effective_create_items = q{};
60 if ( $basketno ){
61 my $basket = Koha::Acquisition::Baskets->find( $basketno );
62 if ($basket){
63 $effective_create_items = $basket->effective_create_items;
64 } else {
65 $effective_create_items = C4::Context->preference('AcqCreateItem');
69 $startdate = eval { dt_from_string( $startdate ) } if $startdate;
70 $enddate = eval { dt_from_string( $enddate ) } if $enddate;
72 my $todaysdate = dt_from_string;
74 # A default of the prior years's holds is a reasonable way to pull holds
75 $enddate = $todaysdate unless $enddate;
76 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
78 if (!defined($ratio)) {
79 $ratio = 3;
81 # Force to be a number
82 $ratio += 0;
83 if ($ratio <= 0) {
84 $ratio = 1; # prevent division by zero
87 my $dbh = C4::Context->dbh;
88 my $sqldatewhere = "";
89 $debug and warn output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 }) . "\n" . output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
90 my @query_params = ();
92 $sqldatewhere .= " AND reservedate >= ?";
93 push @query_params, output_pref({ dt => $startdate, dateformat => 'iso' }) ;
94 $sqldatewhere .= " AND reservedate <= ?";
95 push @query_params, output_pref({ dt => $enddate, dateformat => 'iso' });
97 my $include_aqorders_qty =
98 $effective_create_items eq 'receiving'
99 ? '+ COALESCE(aqorders.quantity, 0) - COALESCE(aqorders.quantityreceived, 0)'
100 : q{};
102 my $include_aqorders_qty_join =
103 $effective_create_items eq 'receiving'
104 ? 'LEFT JOIN aqorders ON reserves.biblionumber=aqorders.biblionumber'
105 : q{};
107 my $nfl_comparison = $include_ordered ? '<=' : '=';
108 my $sus_comparison = $include_suspended ? '<=' : '<';
109 my $strsth =
110 "SELECT reservedate,
111 reserves.borrowernumber as borrowernumber,
112 reserves.biblionumber,
113 reserves.branchcode as branch,
114 items.holdingbranch,
115 items.itemcallnumber,
116 items.itemnumber,
117 GROUP_CONCAT(DISTINCT items.itemcallnumber
118 ORDER BY items.itemnumber SEPARATOR '|') as listcall,
119 GROUP_CONCAT(DISTINCT homebranch
120 ORDER BY items.itemnumber SEPARATOR '|') as homebranch_list,
121 GROUP_CONCAT(DISTINCT holdingbranch
122 ORDER BY items.itemnumber SEPARATOR '|') as holdingbranch_list,
123 GROUP_CONCAT(DISTINCT items.location
124 ORDER BY items.itemnumber SEPARATOR '|') as l_location,
125 GROUP_CONCAT(DISTINCT items.itype
126 ORDER BY items.itemnumber SEPARATOR '|') as l_itype,
128 reserves.found,
129 biblio.title,
130 biblio.subtitle,
131 biblio.medium,
132 biblio.part_number,
133 biblio.part_name,
134 biblio.author,
135 count(DISTINCT reserves.borrowernumber) as reservecount,
136 count(DISTINCT items.itemnumber) $include_aqorders_qty as itemcount
137 FROM reserves
138 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
139 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
140 $include_aqorders_qty_join
141 WHERE
142 notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
143 AND suspend $sus_comparison 1
144 $sqldatewhere
147 if (C4::Context->preference('IndependentBranches')){
148 $strsth .= " AND items.holdingbranch=? ";
149 push @query_params, C4::Context->userenv->{'branch'};
152 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
154 $template->param(sql => $strsth);
155 my $sth = $dbh->prepare($strsth);
156 $sth->execute(@query_params);
158 my @reservedata;
159 while ( my $data = $sth->fetchrow_hashref ) {
160 my $thisratio = $data->{reservecount} / $data->{itemcount};
161 my $ratiocalc = ceil($data->{reservecount}/$ratio - $data->{itemcount});
162 $ratiocalc >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
163 push(
164 @reservedata,
166 reservedate => $data->{reservedate},
167 priority => $data->{priority},
168 name => $data->{borrower},
169 title => $data->{title},
170 subtitle => $data->{subtitle},
171 medium => $data->{medium},
172 part_number => $data->{part_number},
173 part_name => $data->{part_name},
174 author => $data->{author},
175 itemnum => $data->{itemnumber},
176 biblionumber => $data->{biblionumber},
177 holdingbranch => $data->{holdingbranch},
178 homebranch_list => [split('\|', $data->{homebranch_list})],
179 holdingbranch_list => [split('\|', $data->{holdingbranch_list})],
180 branch => $data->{branch},
181 itemcallnumber => $data->{itemcallnumber},
182 location => [split('\|', $data->{l_location})],
183 itype => [split('\|', $data->{l_itype})],
184 reservecount => $data->{reservecount},
185 itemcount => $data->{itemcount},
186 ratiocalc => sprintf( "%.0d", $ratiocalc ),
187 thisratio => sprintf( "%.2f", $thisratio ),
188 thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
189 listcall => [split('\|', $data->{listcall})]
194 for my $rd ( @reservedata ) {
195 next unless $rd->{biblionumber};
196 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
199 $template->param(
200 todaysdate => $todaysdate,
201 from => $startdate,
202 to => $enddate,
203 ratio => $ratio,
204 include_ordered => $include_ordered,
205 include_suspended => $include_suspended,
206 reserveloop => \@reservedata,
209 output_html_with_http_headers $input, $cookie, $template->output;
211 sub CountPendingOrdersByBiblionumber {
212 my $biblionumber = shift;
213 my @orders = GetOrdersByBiblionumber( $biblionumber );
214 my $cnt = 0;
215 if (scalar(@orders)) {
216 for my $order ( @orders ) {
217 next if $order->{datecancellationprinted};
218 my $onum = $order->{quantity} // 0;
219 my $rnum = $order->{quantityreceived} // 0;
220 next if $rnum >= $onum;
221 $cnt += ($onum - $rnum);
224 return $cnt;