Bug 35530: Tell UserCSS and UserJS in libraries administration are for OPAC
[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 Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use POSIX qw( ceil );
25
26 use C4::Context;
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Acquisition qw/GetOrdersByBiblionumber/;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Acquisition::Baskets;
32
33 my $input = CGI->new;
34 my $startdate       = $input->param('from');
35 my $enddate         = $input->param('to');
36 my $ratio           = $input->param('ratio');
37 my $include_ordered = $input->param('include_ordered');
38 my $include_suspended = $input->param('include_suspended');
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "circ/reserveratios.tt",
43         query           => $input,
44         type            => "intranet",
45         flagsrequired   => { circulate => "circulate_remaining_permissions" },
46     }
47 );
48
49 my $booksellerid = $input->param('booksellerid') // '';
50 my $basketno = $input->param('basketno') // '';
51 if ($booksellerid && $basketno) {
52      $template->param( booksellerid => $booksellerid, basketno => $basketno );
53 }
54
55 my $effective_create_items = q{};
56 if ( $basketno ){
57     my $basket = Koha::Acquisition::Baskets->find( $basketno );
58     if ($basket){
59         $effective_create_items = $basket->effective_create_items;
60     } else {
61         $effective_create_items = C4::Context->preference('AcqCreateItem');
62     }
63 }
64
65 my $todaysdate = dt_from_string;
66
67 #    A default of the prior years's holds is a reasonable way to pull holds
68 $enddate = $todaysdate unless $enddate;
69 $startdate = $todaysdate->clone->subtract( years => 1 ) unless $startdate;
70
71 if (!defined($ratio)) {
72     $ratio = 3;
73 }
74 # Force to be a number
75 $ratio += 0;
76 if ($ratio <= 0) {
77     $ratio = 1; # prevent division by zero
78 }
79
80 my $dbh    = C4::Context->dbh;
81 my $sqldatewhere = "";
82 my @query_params = ();
83
84 $sqldatewhere .= " AND reservedate >= ?";
85 push @query_params, $startdate;
86 $sqldatewhere .= " AND reservedate <= ?";
87 push @query_params, $enddate;
88
89 my $include_aqorders_qty =
90   $effective_create_items eq 'receiving'
91   ? '+ COALESCE(aqorders.quantity, 0) - COALESCE(aqorders.quantityreceived, 0)'
92   : q{};
93
94 my $include_aqorders_qty_join =
95   $effective_create_items eq 'receiving'
96   ? 'LEFT JOIN aqorders ON reserves.biblionumber=aqorders.biblionumber'
97   : q{};
98
99 my $nfl_comparison = $include_ordered ? '<=' : '=';
100 my $sus_comparison = $include_suspended ? '<=' : '<';
101 my $strsth =
102 "SELECT reservedate,
103         reserves.borrowernumber as borrowernumber,
104         reserves.biblionumber,
105         reserves.branchcode as branch,
106         items.holdingbranch,
107         items.itemcallnumber,
108         items.itemnumber,
109         GROUP_CONCAT(DISTINCT items.itemcallnumber 
110             ORDER BY items.itemnumber SEPARATOR '|') as listcall,
111         GROUP_CONCAT(DISTINCT homebranch
112             ORDER BY items.itemnumber SEPARATOR '|') as homebranch_list,
113         GROUP_CONCAT(DISTINCT holdingbranch 
114             ORDER BY items.itemnumber SEPARATOR '|') as holdingbranch_list,
115         GROUP_CONCAT(DISTINCT items.location 
116             ORDER BY items.itemnumber SEPARATOR '|') as l_location,
117         GROUP_CONCAT(DISTINCT items.itype 
118             ORDER BY items.itemnumber SEPARATOR '|') as l_itype,
119         GROUP_CONCAT(DISTINCT items.ccode
120             ORDER BY items.ccode SEPARATOR '|') as l_ccode,
121
122         reserves.found,
123         biblio.title,
124         biblio.subtitle,
125         biblio.medium,
126         biblio.part_number,
127         biblio.part_name,
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  AND suspend $sus_comparison 1
138  $sqldatewhere
139 ";
140
141 if (C4::Context->preference('IndependentBranches')){
142     $strsth .= " AND items.holdingbranch=? ";
143     push @query_params, C4::Context->userenv->{'branch'};
144 }
145
146 $strsth .= " GROUP BY reserves.biblionumber ORDER BY reservecount DESC";
147
148 $template->param(sql => $strsth);
149 my $sth = $dbh->prepare($strsth);
150 $sth->execute(@query_params);
151
152 my @reservedata;
153 while ( my $data = $sth->fetchrow_hashref ) {
154     my $thisratio     = $data->{reservecount} / $data->{itemcount};
155     my $copies_to_buy = ceil( $data->{reservecount} / $ratio - $data->{itemcount} );
156     $thisratio >= $ratio or next;    # TODO: tighter targeting -- get ratio limit into SQL using HAVING clause
157     push(
158         @reservedata,
159         {
160             reservedate        => $data->{reservedate},
161             priority           => $data->{priority},
162             name               => $data->{borrower},
163             title              => $data->{title},
164             subtitle           => $data->{subtitle},
165             medium             => $data->{medium},
166             part_number        => $data->{part_number},
167             part_name          => $data->{part_name},
168             author             => $data->{author},
169             itemnum            => $data->{itemnumber},
170             biblionumber       => $data->{biblionumber},
171             holdingbranch      => $data->{holdingbranch},
172             homebranch_list    => [ split( '\|', $data->{homebranch_list} ) ],
173             holdingbranch_list => [ split( '\|', $data->{holdingbranch_list} ) ],
174             branch             => $data->{branch},
175             itemcallnumber     => $data->{itemcallnumber},
176             location           => [ split( '\|', $data->{l_location} ) ],
177             itype              => [ split( '\|', $data->{l_itype} ) ],
178             ccode              => [ split( '\|', $data->{l_ccode} ) ],
179             reservecount       => $data->{reservecount},
180             itemcount          => $data->{itemcount},
181             copies_to_buy      => sprintf( "%d", $copies_to_buy ),
182             thisratio          => sprintf( "%.2f", $thisratio ),
183             thisratio_atleast1 => ( $thisratio >= 1 ) ? 1 : 0,
184             listcall           => [ split( '\|', $data->{listcall} ) ]
185         }
186     );
187 }
188
189 for my $rd ( @reservedata ) {
190     next unless $rd->{biblionumber};
191     $rd->{pendingorders} = CountPendingOrdersByBiblionumber( $rd->{biblionumber} );
192 }
193
194 $template->param(
195     todaysdate      => $todaysdate,
196     from            => $startdate,
197     to              => $enddate,
198     ratio           => $ratio,
199     include_ordered => $include_ordered,
200     include_suspended => $include_suspended,
201     reserveloop     => \@reservedata,
202 );
203
204 output_html_with_http_headers $input, $cookie, $template->output;
205
206 sub CountPendingOrdersByBiblionumber {
207     my $biblionumber = shift;
208     my @orders = GetOrdersByBiblionumber( $biblionumber );
209     my $cnt = 0;
210     if (scalar(@orders)) {
211         for my $order ( @orders ) {
212             next if $order->{datecancellationprinted};
213             my $onum = $order->{quantity} // 0;
214             my $rnum = $order->{quantityreceived} // 0;
215             next if $rnum >= $onum;
216             $cnt += ($onum - $rnum);
217         }
218     }
219     return $cnt;
220 }