3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use constant PULL_INTERVAL => 2;
23 use List::MoreUtils qw( uniq );
30 use C4::Items qw( ModItemTransfer );
31 use C4::Reserves qw( ModReserveCancelAll );
35 use DateTime::Duration;
38 my $startdate = $input->param('from');
39 my $enddate = $input->param('to');
40 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
41 my $op = $input->param('op') || '';
42 my $borrowernumber = $input->param('borrowernumber');
43 my $reserve_id = $input->param('reserve_id');
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 template_name => "circ/pendingreserves.tt",
50 flagsrequired => { circulate => "circulate_remaining_permissions" },
56 if ( $op eq 'cancel_reserve' and $reserve_id ) {
57 my $hold = Koha::Holds->find( $reserve_id );
59 my $cancellation_reason = $input->param('cancellation-reason');
60 $hold->cancel({ cancellation_reason => $cancellation_reason });
61 push @messages, { type => 'message', code => 'hold_cancelled' };
63 } elsif ( $op =~ m|^mark_as_lost| ) {
64 my $hold = Koha::Holds->find( $reserve_id );
65 die "wrong reserve_id" unless $hold; # This is a bit rude, but we are not supposed to get a wrong reserve_id
66 my $item = $hold->item;
67 if ( $item and C4::Context->preference('CanMarkHoldsToPullAsLost') =~ m|^allow| ) {
68 my $patron = $hold->borrower;
69 C4::Circulation::LostItem( $item->itemnumber, "pendingreserves" );
70 if ( $op eq 'mark_as_lost_and_notify' and C4::Context->preference('CanMarkHoldsToPullAsLost') eq 'allow_and_notify' ) {
71 my $library = $hold->branch;
72 my $letter = C4::Letters::GetPreparedLetter(
74 letter_code => 'CANCEL_HOLD_ON_LOST',
75 branchcode => $patron->branchcode,
76 lang => $patron->lang,
78 branches => $library->branchcode,
79 borrowers => $patron->borrowernumber,
80 items => $item->itemnumber,
81 biblio => $hold->biblionumber,
82 biblioitems => $hold->biblionumber,
83 reserves => $hold->unblessed,
87 my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
89 C4::Letters::EnqueueLetter(
91 borrowernumber => $patron->borrowernumber,
92 message_transport_type => 'email',
93 from_address => $admin_email_address,
96 unless ( $patron->notice_email_address ) {
97 push @messages, {type => 'alert', code => 'no_email_address', };
99 push @messages, { type => 'message', code => 'letter_enqueued' };
101 push @messages, { type => 'error', code => 'no_template_notice' };
105 if ( $item->homebranch ne $item->holdingbranch ) {
106 C4::Items::ModItemTransfer( $item->itemnumber, $item->holdingbranch, $item->homebranch, 'LostReserve' );
109 if ( my $yaml = C4::Context->preference('UpdateItemWhenLostFromHoldList') ) {
110 $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
112 eval { $assignments = YAML::Load($yaml); };
114 warn "Unable to parse UpdateItemWhenLostFromHoldList syspref : $@" if $@;
118 while ( my ( $f, $v ) = each( %$assignments ) ) {
123 warn "Unable to modify item itemnumber=" . $item->itemnumber . ": $@" if $@;
127 } elsif ( not $item ) {
128 push @messages, { type => 'alert', code => 'hold_placed_at_biblio_level'};
129 } # else the url parameters have been modified and the user is not allowed to continue
133 my $today = dt_from_string;
136 $startdate =~ s/^\s+//;
137 $startdate =~ s/\s+$//;
138 $startdate = eval{dt_from_string( $startdate )};
140 unless ( $startdate ){
141 # changed from delivered range of 10 years-yesterday to 2 days ago-today
142 # Find two days ago for the default shelf pull start date, unless HoldsToPullStartDate sys pref is set.
143 $startdate = $today - DateTime::Duration->new( days => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL );
147 $enddate =~ s/^\s+//;
148 $enddate =~ s/\s+$//;
149 $enddate = eval{dt_from_string( $enddate )};
151 unless ( $enddate ) {
152 #similarly: calculate end date with ConfirmFutureHolds (days)
153 $enddate = $today + DateTime::Duration->new( days => C4::Context->preference('ConfirmFutureHolds') || 0 );
156 # building query parameters
158 'reserve.found' => undef,
159 'reserve.suspend' => 0,
160 'itembib.itemlost' => 0,
161 'itembib.withdrawn' => 0,
162 'itembib.notforloan' => 0,
163 'itembib.itemnumber' => { -not_in => \'SELECT itemnumber FROM branchtransfers WHERE datearrived IS NULL' }
167 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
168 my $startdate_iso = $dtf->format_date($startdate);
169 my $enddate_iso = $dtf->format_date($enddate);
170 if ( $startdate_iso && $enddate_iso ){
171 $where{'reserve.reservedate'} = [ -and => { '>=', $startdate_iso }, { '<=', $enddate_iso } ];
172 } elsif ( $startdate_iso ){
173 $where{'reserve.reservedate'} = { '>=', $startdate_iso };
174 } elsif ( $enddate_iso ){
175 $where{'reserve.reservedate'} = { '<=', $enddate_iso };
179 if ( !C4::Context->preference('AllowHoldsOnDamagedItems') ){
180 $where{'itembib.damaged'} = 0;
183 if ( C4::Context->preference('IndependentBranches') ){
184 $where{'itembib.holdingbranch'} = C4::Context->userenv->{'branch'};
187 # get all distinct unfulfilled reserves
188 my @biblionumbers = Koha::Holds->search(
190 { join => 'itembib', alias => 'reserve', distinct => 1, columns => qw[me.biblionumber] }
191 )->get_column('biblionumber');
193 my @branchtransfers = map { $_->itemnumber } Koha::Item::Transfers->search({ datearrived => undef }, { columns => [ 'itemnumber' ], collapse => 1 });
194 my @waiting_holds = map { $_->itemnumber } Koha::Holds->search({'found' => 'W'}, { columns => [ 'itemnumber' ], collapse => 1 });
196 my @all_items = Koha::Items->search(
198 biblionumber => { in => \@biblionumbers },
203 itemnumber => { -not_in => [ @branchtransfers, @waiting_holds ] },
208 foreach my $item ( @all_items ) {
209 push @{$all_items->{$item->biblionumber}}, $item;
212 # patrons count per biblio
213 my $patrons_count = {
214 map { $_->{biblionumber} => $_->{patrons_count} } @{ Koha::Holds->search(
217 select => [ 'biblionumber', { count => { distinct => 'borrowernumber' } } ],
218 as => [qw( biblionumber patrons_count )],
219 group_by => [qw( biblionumber )]
225 # make final reserves hash and fill with info
227 foreach my $bibnum ( @biblionumbers ){
229 my $items = $all_items->{$bibnum};
231 # get available item types for each biblio
233 if ( C4::Context->preference('item-level_itypes') ){
234 @res_itemtypes = uniq map { defined $_->itype ? $_->itype : () } @$items;
236 @res_itemtypes = Koha::Biblioitems->search(
237 { biblionumber => $bibnum, itemtype => { '!=', undef } },
238 { columns => 'itemtype',
241 )->get_column('itemtype');
243 $reserves->{$bibnum}->{itemtypes} = \@res_itemtypes;
245 # get available locations for each biblio
246 $reserves->{$bibnum}->{locations} = [ uniq map { defined $_->location ? $_->location : () } @$items ];
248 # get available callnumbers for each biblio
249 $reserves->{$bibnum}->{callnumbers} = [ uniq map { defined $_->itemcallnumber ? $_->itemcallnumber : () } @$items ];
251 # get available enumchrons for each biblio
252 $reserves->{$bibnum}->{enumchrons} = [ uniq map { defined $_->enumchron ? $_->enumchron : () } @$items ];
254 # get available copynumbers for each biblio
255 $reserves->{$bibnum}->{copynumbers} = [ uniq map { defined $_->copynumber ? $_->copynumber : () } @$items ];
257 # get available barcodes for each biblio
258 $reserves->{$bibnum}->{barcodes} = [ uniq map { defined $_->barcode ? $_->barcode : () } @$items ];
260 # get available holding branches for each biblio
261 $reserves->{$bibnum}->{holdingbranches} = [ uniq map { defined $_->holdingbranch ? $_->holdingbranch : () } @$items ];
264 my $items_count = scalar @$items;
265 $reserves->{$bibnum}->{items_count} = $items_count;
268 $hold_info->{patrons_count} = $patrons_count->{$bibnum};
270 my $pull_count = $items_count <= $patrons_count->{$bibnum} ? $items_count : $patrons_count->{$bibnum};
271 if ( $pull_count == 0 ) {
272 delete($reserves->{$bibnum});
275 $reserves->{$bibnum}->{pull_count} = $pull_count;
277 # get other relevant information
278 my $res_info = Koha::Holds->search(
279 { 'reserve.biblionumber' => $bibnum, %where },
280 { prefetch => [ 'borrowernumber', 'itembib', 'biblio' ],
281 order_by => 'priority',
284 )->next; # get first item in results
285 $reserves->{$bibnum}->{borrower} = $res_info->borrower;
286 $reserves->{$bibnum}->{item} = $res_info->item;
287 $reserves->{$bibnum}->{biblio} = $res_info->biblio;
288 $reserves->{$bibnum}->{reserve} = $res_info;
292 todaysdate => $today,
295 reserves => $reserves,
296 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
297 HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
298 HoldsToPullEndDate => C4::Context->preference('ConfirmFutureHolds') || 0,
299 messages => \@messages,
302 output_html_with_http_headers $input, $cookie, $template->output;