Bug 17088 - Bad MARC XML can halt export_records.pl
[koha.git] / circ / reserveratios.pl
blob56c84d8f9b3c0fa690033d5816ae1b030d87e503
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 strict;
22 use warnings;
24 use CGI qw ( -utf8 );
25 use Date::Calc qw/Today Add_Delta_YM/;
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Debug;
31 use C4::Biblio qw/GetMarcBiblio GetRecordValue GetFrameworkCode/;
32 use C4::Acquisition qw/GetOrdersByBiblionumber/;
33 use Koha::DateUtils;
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 $startdate = eval { dt_from_string( $startdate ) } if $startdate;
59 $enddate = eval { dt_from_string( $enddate ) } if $enddate;
61 my $todaysdate = dt_from_string;
63 # A default of the prior years's holds is a reasonable way to pull holds
64 $enddate = $todaysdate unless $enddate;
65 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
67 if (!defined($ratio)) {
68 $ratio = 3;
70 # Force to be a number
71 $ratio += 0;
72 if ($ratio <= 0) {
73 $ratio = 1; # prevent division by zero
76 my $dbh = C4::Context->dbh;
77 my $sqldatewhere = "";
78 $debug and warn output_pref({ dt => $startdate, dateformat => 'iso', dateonly => 1 }) . "\n" . output_pref({ dt => $enddate, dateformat => 'iso', dateonly => 1 });
79 my @query_params = ();
81 $sqldatewhere .= " AND reservedate >= ?";
82 push @query_params, output_pref({ dt => $startdate, dateformat => 'iso' }) ;
83 $sqldatewhere .= " AND reservedate <= ?";
84 push @query_params, output_pref({ dt => $enddate, dateformat => 'iso' });
86 my $include_aqorders_qty =
87 C4::Context->preference('AcqCreateItem') eq 'receiving'
88 ? '+ COALESCE(aqorders.quantity, 0) - COALESCE(aqorders.quantityreceived, 0)'
89 : q{};
91 my $include_aqorders_qty_join =
92 C4::Context->preference('AcqCreateItem') eq 'receiving'
93 ? 'LEFT JOIN aqorders ON reserves.biblionumber=aqorders.biblionumber'
94 : q{};
96 my $nfl_comparison = $include_ordered ? '<=' : '=';
97 my $strsth =
98 "SELECT reservedate,
99 reserves.borrowernumber as borrowernumber,
100 reserves.biblionumber,
101 reserves.branchcode as branch,
102 items.holdingbranch,
103 items.itemcallnumber,
104 items.itemnumber,
105 GROUP_CONCAT(DISTINCT items.itemcallnumber
106 ORDER BY items.itemnumber SEPARATOR '|') as listcall,
107 GROUP_CONCAT(DISTINCT homebranch
108 ORDER BY items.itemnumber SEPARATOR '|') as homebranch_list,
109 GROUP_CONCAT(DISTINCT holdingbranch
110 ORDER BY items.itemnumber SEPARATOR '|') as holdingbranch_list,
111 GROUP_CONCAT(DISTINCT items.location
112 ORDER BY items.itemnumber SEPARATOR '|') as l_location,
113 GROUP_CONCAT(DISTINCT items.itype
114 ORDER BY items.itemnumber SEPARATOR '|') as l_itype,
116 reserves.found,
117 biblio.title,
118 biblio.author,
119 count(DISTINCT reserves.borrowernumber) as reservecount,
120 count(DISTINCT items.itemnumber) $include_aqorders_qty as itemcount
121 FROM reserves
122 LEFT JOIN items ON items.biblionumber=reserves.biblionumber
123 LEFT JOIN biblio ON reserves.biblionumber=biblio.biblionumber
124 $include_aqorders_qty_join
125 WHERE
126 notforloan $nfl_comparison 0 AND damaged = 0 AND itemlost = 0 AND withdrawn = 0
127 $sqldatewhere
130 if (C4::Context->preference('IndependentBranches')){
131 $strsth .= " AND items.holdingbranch=? ";
132 push @query_params, C4::Context->userenv->{'branch'};
135 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
137 $template->param(sql => $strsth);
138 my $sth = $dbh->prepare($strsth);
139 $sth->execute(@query_params);
141 my $ratio_atleast1 = ($ratio >= 1) ? 1 : 0;
142 my @reservedata;
143 while ( my $data = $sth->fetchrow_hashref ) {
144 my $thisratio = $data->{reservecount} / $data->{itemcount};
145 my $ratiocalc = ($thisratio / $ratio);
146 ($thisratio / $ratio) >= 1 or next; # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
147 my $record = GetMarcBiblio($data->{biblionumber});
148 $data->{subtitle} = GetRecordValue('subtitle', $record, GetFrameworkCode($data->{biblionumber}));
149 push(
150 @reservedata,
152 reservedate => $data->{reservedate},
153 priority => $data->{priority},
154 name => $data->{borrower},
155 title => $data->{title},
156 subtitle => $data->{subtitle},
157 author => $data->{author},
158 itemnum => $data->{itemnumber},
159 biblionumber => $data->{biblionumber},
160 holdingbranch => $data->{holdingbranch},
161 homebranch_list => [split('\|', $data->{homebranch_list})],
162 holdingbranch_list => [split('\|', $data->{holdingbranch_list})],
163 branch => $data->{branch},
164 itemcallnumber => $data->{itemcallnumber},
165 location => [split('\|', $data->{l_location})],
166 itype => [split('\|', $data->{l_itype})],
167 reservecount => $data->{reservecount},
168 itemcount => $data->{itemcount},
169 ratiocalc => sprintf( "%.0d", $ratio_atleast1 ? ( $thisratio / $ratio ) : $thisratio ),
170 thisratio => sprintf( "%.2f", $thisratio ),
171 thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
172 listcall => [split('\|', $data->{listcall})]
177 for my $rd ( @reservedata ) {
178 next unless $rd->{biblionumber};
179 $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
182 $template->param(
183 ratio_atleast1 => $ratio_atleast1,
184 todaysdate => $todaysdate,
185 from => $startdate,
186 to => $enddate,
187 ratio => $ratio,
188 include_ordered => $include_ordered,
189 reserveloop => \@reservedata,
192 output_html_with_http_headers $input, $cookie, $template->output;
194 sub CountPendingOrdersByBiblionumber {
195 my $biblionumber = shift;
196 my @orders = GetOrdersByBiblionumber( $biblionumber );
197 my $cnt = 0;
198 if (scalar(@orders)) {
199 for my $order ( @orders ) {
200 next if $order->{datecancellationprinted};
201 my $onum = $order->{quantity} // 0;
202 my $rnum = $order->{quantityreceived} // 0;
203 next if $rnum >= $onum;
204 $cnt += ($onum - $rnum);
207 return $cnt;