+my @messages;
+if ( $op eq 'cancel_reserve' and $reserve_id ) {
+ my $hold = Koha::Holds->find( $reserve_id );
+ if ( $hold ) {
+ my $cancellation_reason = $input->param('cancellation-reason');
+ $hold->cancel({ cancellation_reason => $cancellation_reason });
+ push @messages, { type => 'message', code => 'hold_cancelled' };
+ }
+} elsif ( $op =~ m|^mark_as_lost| ) {
+ my $hold = Koha::Holds->find( $reserve_id );
+ die "wrong reserve_id" unless $hold; # This is a bit rude, but we are not supposed to get a wrong reserve_id
+ my $item = $hold->item;
+ if ( $item and C4::Context->preference('CanMarkHoldsToPullAsLost') =~ m|^allow| ) {
+ my $patron = $hold->borrower;
+ C4::Circulation::LostItem( $item->itemnumber, "pendingreserves" );
+ if ( $op eq 'mark_as_lost_and_notify' and C4::Context->preference('CanMarkHoldsToPullAsLost') eq 'allow_and_notify' ) {
+ my $library = $hold->branch;
+ my $letter = C4::Letters::GetPreparedLetter(
+ module => 'reserves',
+ letter_code => 'CANCEL_HOLD_ON_LOST',
+ branchcode => $patron->branchcode,
+ lang => $patron->lang,
+ tables => {
+ branches => $library->branchcode,
+ borrowers => $patron->borrowernumber,
+ items => $item->itemnumber,
+ biblio => $hold->biblionumber,
+ biblioitems => $hold->biblionumber,
+ reserves => $hold->unblessed,
+ },
+ );
+ if ( $letter ) {
+ my $from_address = $library->from_email_address;
+
+ C4::Letters::EnqueueLetter(
+ { letter => $letter,
+ borrowernumber => $patron->borrowernumber,
+ message_transport_type => 'email',
+ from_address => $from_address,
+ }
+ );
+ unless ( $patron->notice_email_address ) {
+ push @messages, {type => 'alert', code => 'no_email_address', };
+ }
+ push @messages, { type => 'message', code => 'letter_enqueued' };
+ } else {
+ push @messages, { type => 'error', code => 'no_template_notice' };
+ }
+ }
+ $hold->cancel;
+ if ( $item->homebranch ne $item->holdingbranch ) {
+ C4::Items::ModItemTransfer( $item->itemnumber, $item->holdingbranch, $item->homebranch, 'LostReserve' );
+ }
+
+ if ( my $yaml = C4::Context->preference('UpdateItemWhenLostFromHoldList') ) {
+ $yaml = "$yaml\n\n"; # YAML is anal on ending \n. Surplus does not hurt
+ my $assignments;
+ eval { $assignments = YAML::XS::Load(Encode::encode_utf8($yaml)); };
+ if ($@) {
+ warn "Unable to parse UpdateItemWhenLostFromHoldList syspref : $@" if $@;
+ }
+ else {
+ eval {
+ while ( my ( $f, $v ) = each( %$assignments ) ) {
+ $item->$f($v);
+ }
+ $item->store;
+ };
+ warn "Unable to modify item itemnumber=" . $item->itemnumber . ": $@" if $@;
+ }
+ }
+
+ } elsif ( not $item ) {
+ push @messages, { type => 'alert', code => 'hold_placed_at_biblio_level'};
+ } # else the url parameters have been modified and the user is not allowed to continue
+}
+
+
+my $today = dt_from_string;
+
+if ( $startdate ) {
+ $startdate =~ s/^\s+//;
+ $startdate =~ s/\s+$//;
+ $startdate = eval{dt_from_string( $startdate )};
+}
+unless ( $startdate ){