Bug 23083: Add tests
[koha.git] / circ / reserveratios.pl
blob104928addc3e139235d84062b82e61fe8a8fbf98
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/;
26 use C4::Context;
27 use C4::Output;
28 use C4::Auth;
29 use C4::Debug;
30 use C4::Biblio qw/GetMarcBiblio GetRecordValue GetFrameworkCode/;
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');
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 template_name => "circ/reserveratios.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { circulate => "circulate_remaining_permissions" },
48 debug => 1,
52 my $booksellerid = $input->param('booksellerid') // '';
53 my $basketno = $input->param('basketno') // '';
54 if ($booksellerid && $basketno) {
55 $template->param( booksellerid => $booksellerid, basketno => $basketno );
58 my $effective_create_items = q{};
59 if ( $basketno ){
60 my $basket = Koha::Acquisition::Baskets->find( $basketno );
61 if ($basket){
62 $effective_create_items = $basket->effective_create_items;
63 } else {
64 $effective_create_items = C4::Context->preference('AcqCreateItem');
68 $startdate = eval { dt_from_string( $startdate ) } if $startdate;
69 $enddate = eval { dt_from_string( $enddate ) } if $enddate;
71 my $todaysdate = dt_from_string;
73 # A default of the prior years's holds is a reasonable way to pull holds
74 $enddate = $todaysdate unless $enddate;
75 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
77 if (!defined($ratio)) {
78 $ratio = 3;
80 # Force to be a number
81 $ratio += 0;
82 if ($ratio <= 0) {
83 $ratio = 1; # prevent division by zero
86 my $dbh = C4::Context->dbh;
87 my $sqldatewhere = "";
88 $debug and warn output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 }) . "\n" . output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
89 my @query_params = ();
91 $sqldatewhere .= " AND reservedate >= ?";
92 push @query_params, output_pref({ dt => $startdate, dateformat => 'iso' }) ;
93 $sqldatewhere .= " AND reservedate <= ?";
94 push @query_params, output_pref({ dt => $enddate, dateformat => 'iso' });
96 my $include_aqorders_qty =
97 $effective_create_items eq 'receiving'
98 ? '+ COALESCE(aqorders.quantity, 0) - COALESCE(aqorders.quantityreceived, 0)'
99 : q{};
101 my $include_aqorders_qty_join =
102 $effective_create_items eq 'receiving'
103 ? 'LEFT JOIN aqorders ON reserves.biblionumber=aqorders.biblionumber'
104 : q{};
106 my $nfl_comparison = $include_ordered ? '<=' : '=';
107 my $strsth =
108 "SELECT reservedate,
109 reserves.borrowernumber as borrowernumber,
110 reserves.biblionumber,
111 reserves.branchcode as branch,
112 items.holdingbranch,
113 items.itemcallnumber,
114 items.itemnumber,
115 GROUP_CONCAT(DISTINCT items.itemcallnumber
116 ORDER BY items.itemnumber SEPARATOR '|') as listcall,
117 GROUP_CONCAT(DISTINCT homebranch
118 ORDER BY items.itemnumber SEPARATOR '|') as homebranch_list,
119 GROUP_CONCAT(DISTINCT holdingbranch
120 ORDER BY items.itemnumber SEPARATOR '|') as holdingbranch_list,
121 GROUP_CONCAT(DISTINCT items.location
122 ORDER BY items.itemnumber SEPARATOR '|') as l_location,
123 GROUP_CONCAT(DISTINCT items.itype
124 ORDER BY items.itemnumber SEPARATOR '|') as l_itype,
126 reserves.found,
127 biblio.title,
128 biblio.author,
129 count(DISTINCT reserves.borrowernumber) as reservecount,
130 count(DISTINCT items.itemnumber) $include_aqorders_qty as itemcount
131 FROM reserves
132 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
133 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
134 $include_aqorders_qty_join
135 WHERE
136 notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
137 $sqldatewhere
140 if (C4::Context->preference('IndependentBranches')){
141 $strsth .= " AND items.holdingbranch=? ";
142 push @query_params, C4::Context->userenv->{'branch'};
145 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
147 $template->param(sql => $strsth);
148 my $sth = $dbh->prepare($strsth);
149 $sth->execute(@query_params);
151 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
152 my @reservedata;
153 while ( my $data = $sth->fetchrow_hashref ) {
154 my $thisratio = $data->{reservecount} / $data->{itemcount};
155 my $ratiocalc = ($thisratio / $ratio);
156 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
157 my $record = GetMarcBiblio({ biblionumber => $data->{biblionumber} });
158 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
159 push(
160 @reservedata,
162 reservedate => $data->{reservedate},
163 priority => $data->{priority},
164 name => $data->{borrower},
165 title => $data->{title},
166 subtitle => $data->{subtitle},
167 author => $data->{author},
168 itemnum => $data->{itemnumber},
169 biblionumber => $data->{biblionumber},
170 holdingbranch => $data->{holdingbranch},
171 homebranch_list => [split('\|', $data->{homebranch_list})],
172 holdingbranch_list => [split('\|', $data->{holdingbranch_list})],
173 branch => $data->{branch},
174 itemcallnumber => $data->{itemcallnumber},
175 location => [split('\|', $data->{l_location})],
176 itype => [split('\|', $data->{l_itype})],
177 reservecount => $data->{reservecount},
178 itemcount => $data->{itemcount},
179 ratiocalc => sprintf( "%.0d", $ratio_atleast1 ? ( $thisratio / $ratio ) : $thisratio ),
180 thisratio => sprintf( "%.2f", $thisratio ),
181 thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
182 listcall => [split('\|', $data->{listcall})]
187 for my $rd ( @reservedata ) {
188 next unless $rd->{biblionumber};
189 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
192 $template->param(
193 ratio_atleast1 => $ratio_atleast1,
194 todaysdate => $todaysdate,
195 from => $startdate,
196 to => $enddate,
197 ratio => $ratio,
198 include_ordered => $include_ordered,
199 reserveloop => \@reservedata,
202 output_html_with_http_headers $input, $cookie, $template->output;
204 sub CountPendingOrdersByBiblionumber {
205 my $biblionumber = shift;
206 my @orders = GetOrdersByBiblionumber( $biblionumber );
207 my $cnt = 0;
208 if (scalar(@orders)) {
209 for my $order ( @orders ) {
210 next if $order->{datecancellationprinted};
211 my $onum = $order->{quantity} // 0;
212 my $rnum = $order->{quantityreceived} // 0;
213 next if $rnum >= $onum;
214 $cnt += ($onum - $rnum);
217 return $cnt;