Bug 14237: Database updates
[srvgit] / C4 / HoldsQueue.pm
old mode 100755 (executable)
new mode 100644 (file)
index 1015763..3774214
@@ -31,6 +31,7 @@ use C4::Biblio;
 use Koha::DateUtils;
 use Koha::Items;
 use Koha::Patrons;
+use Koha::Libraries;
 
 use List::Util qw(shuffle);
 use List::MoreUtils qw(any);
@@ -61,6 +62,9 @@ Returns Transport Cost Matrix as a hashref <to branch code> => <from branch code
 =cut
 
 sub TransportCostMatrix {
+    my ( $params ) = @_;
+    my $ignore_holds_queue_skip_closed = $params->{ignore_holds_queue_skip_closed};
+
     my $dbh   = C4::Context->dbh;
     my $transport_costs = $dbh->selectall_arrayref("SELECT * FROM transport_cost",{ Slice => {} });
 
@@ -77,7 +81,7 @@ sub TransportCostMatrix {
             disable_transfer => $disabled
         };
 
-        if ( C4::Context->preference("HoldsQueueSkipClosed") ) {
+        if ( !$ignore_holds_queue_skip_closed && C4::Context->preference("HoldsQueueSkipClosed") ) {
             $calendars->{$from} ||= Koha::Calendar->new( branchcode => $from );
             $transport_cost_matrix{$to}{$from}{disable_transfer} ||=
               $calendars->{$from}->is_holiday( $today );
@@ -132,7 +136,12 @@ sub GetHoldsQueueItems {
     my $dbh   = C4::Context->dbh;
 
     my @bind_params = ();
-    my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location, items.enumchron, items.cn_sort, biblioitems.publishercode,biblio.copyrightdate,biblioitems.publicationyear,biblioitems.pages,biblioitems.size,biblioitems.publicationyear,biblioitems.isbn,items.copynumber
+    my $query = q/SELECT tmp_holdsqueue.*, biblio.author, items.ccode, items.itype, biblioitems.itemtype, items.location,
+                         items.enumchron, items.cn_sort, biblioitems.publishercode,
+                         biblio.copyrightdate, biblio.subtitle, biblio.medium,
+                         biblio.part_number, biblio.part_name,
+                         biblioitems.publicationyear, biblioitems.pages, biblioitems.size,
+                         biblioitems.isbn, biblioitems.editionstatement, items.copynumber
                   FROM tmp_holdsqueue
                        JOIN biblio      USING (biblionumber)
                   LEFT JOIN biblioitems USING (biblionumber)
@@ -147,13 +156,6 @@ sub GetHoldsQueueItems {
     $sth->execute(@bind_params);
     my $items = [];
     while ( my $row = $sth->fetchrow_hashref ){
-        my $record = GetMarcBiblio({ biblionumber => $row->{biblionumber} });
-        if ($record){
-            $row->{subtitle} = [ map { $_->{subfield} } @{ GetRecordValue( 'subtitle', $record, '' ) } ];
-            $row->{parts} = GetRecordValue('parts',$record,'')->[0]->{subfield};
-            $row->{numbers} = GetRecordValue('numbers',$record,'')->[0]->{subfield};
-        }
-
         # return the bib-level or item-level itype per syspref
         if (!C4::Context->preference('item-level_itypes')) {
             $row->{itype} = $row->{itemtype};
@@ -278,8 +280,8 @@ sub GetPendingHoldRequestsForBib {
 
     my $dbh = C4::Context->dbh;
 
-    my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserves.branchcode,
-                                reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype
+    my $request_query = "SELECT biblionumber, borrowernumber, itemnumber, priority, reserve_id, reserves.branchcode,
+                                reservedate, reservenotes, borrowers.branchcode AS borrowerbranch, itemtype, item_level_hold
                          FROM reserves
                          JOIN borrowers USING (borrowernumber)
                          WHERE biblionumber = ?
@@ -360,6 +362,52 @@ sub GetItemsAvailableToFillHoldRequestsForBib {
     } @items ];
 }
 
+=head2 _checkHoldPolicy
+
+    _checkHoldPolicy($item, $request)
+
+    check if item agrees with hold policies
+
+=cut
+
+sub _checkHoldPolicy {
+    my ( $item, $request ) = @_;
+
+    return 0 unless $item->{holdallowed} ne 'not_allowed';
+
+    return 0
+      if $item->{holdallowed} eq 'from_home_library'
+      && $item->{homebranch} ne $request->{borrowerbranch};
+
+    return 0
+      if $item->{'holdallowed'} eq 'from_local_hold_group'
+      && !Koha::Libraries->find( $item->{homebranch} )
+              ->validate_hold_sibling( { branchcode => $request->{borrowerbranch} } );
+
+    my $hold_fulfillment_policy = $item->{hold_fulfillment_policy};
+
+    return 0
+      if $hold_fulfillment_policy eq 'holdgroup'
+      && !Koha::Libraries->find( $item->{homebranch} )
+            ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
+
+    return 0
+      if $hold_fulfillment_policy eq 'homebranch'
+      && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
+
+    return 0
+      if $hold_fulfillment_policy eq 'holdingbranch'
+      && $request->{branchcode} ne $item->{$hold_fulfillment_policy};
+
+    return 0
+      if $hold_fulfillment_policy eq 'patrongroup'
+      && !Koha::Libraries->find( $request->{borrowerbranch} )
+              ->validate_hold_sibling( { branchcode => $request->{branchcode} } );
+
+    return 1;
+
+}
+
 =head2 MapItemsToHoldRequests
 
   MapItemsToHoldRequests($hold_requests, $available_items, $branches, $transport_cost_matrix)
@@ -369,7 +417,6 @@ sub GetItemsAvailableToFillHoldRequestsForBib {
 sub MapItemsToHoldRequests {
     my ($hold_requests, $available_items, $branches_to_use, $transport_cost_matrix) = @_;
 
-
     # handle trival cases
     return unless scalar(@$hold_requests) > 0;
     return unless scalar(@$available_items) > 0;
@@ -379,6 +426,10 @@ sub MapItemsToHoldRequests {
                                    grep { defined($_->{itemnumber}) }
                                    @$hold_requests;
 
+    map { $_->{_object} = Koha::Items->find( $_->{itemnumber} ) } @$available_items;
+    my $libraries = {};
+    map { $libraries->{$_->id} = $_ } Koha::Libraries->search();
+
     # group available items by itemnumber
     my %items_by_itemnumber = map { $_->{itemnumber} => $_ } @$available_items;
 
@@ -399,15 +450,19 @@ sub MapItemsToHoldRequests {
           C4::Context->preference('LocalHoldsPriorityItemControl');
 
         foreach my $request (@$hold_requests) {
-            next if (defined($request->{itemnumber})); #skip item level holds in local priority checking
             last if $num_items_remaining == 0;
+            my $patron = Koha::Patrons->find($request->{borrowernumber});
+            next if $patron->category->exclude_from_local_holds_priority;
 
             my $local_hold_match;
             foreach my $item (@$available_items) {
-                next
-                  if ( !$item->{holdallowed} )
-                  || ( $item->{holdallowed} == 1
-                    && $item->{homebranch} ne $request->{borrowerbranch} );
+                next if $item->{_object}->exclude_from_local_holds_priority;
+
+                next unless _checkHoldPolicy($item, $request);
+
+                next if $request->{itemnumber} && $request->{itemnumber} != $item->{itemnumber};
+
+                next unless $item->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
 
                 my $local_holds_priority_item_branchcode =
                   $item->{$LocalHoldsPriorityItemControl};
@@ -434,7 +489,8 @@ sub MapItemsToHoldRequests {
                             holdingbranch  => $item->{holdingbranch},
                             pickup_branch  => $request->{branchcode}
                               || $request->{borrowerbranch},
-                            item_level   => 0,
+                            reserve_id   => $request->{reserve_id},
+                            item_level   => $request->{item_level_hold},
                             reservedate  => $request->{reservedate},
                             reservenotes => $request->{reservenotes},
                         };
@@ -457,12 +513,11 @@ sub MapItemsToHoldRequests {
             if (
                     exists $items_by_itemnumber{ $request->{itemnumber} }
                 and not exists $allocated_items{ $request->{itemnumber} }
-                and ( # Don't fill item level holds that contravene the hold pickup policy at this time
-                    ( $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} eq 'any' )
-                    || ( $request->{branchcode} eq $items_by_itemnumber{ $request->{itemnumber} }->{ $items_by_itemnumber{ $request->{itemnumber} }->{hold_fulfillment_policy} }  )
+                and  _checkHoldPolicy($items_by_itemnumber{ $request->{itemnumber} }, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
                 and ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
                     || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
-                )
+
+                and $items_by_itemnumber{ $request->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } )
 
               )
             {
@@ -472,7 +527,8 @@ sub MapItemsToHoldRequests {
                     biblionumber   => $request->{biblionumber},
                     holdingbranch  => $items_by_itemnumber{ $request->{itemnumber} }->{holdingbranch},
                     pickup_branch  => $request->{branchcode} || $request->{borrowerbranch},
-                    item_level     => 1,
+                    reserve_id     => $request->{reserve_id},
+                    item_level     => $request->{item_level_hold},
                     reservedate    => $request->{reservedate},
                     reservenotes   => $request->{reservenotes},
                 };
@@ -488,7 +544,7 @@ sub MapItemsToHoldRequests {
     # group available items by branch
     my %items_by_branch = ();
     foreach my $item (@$available_items) {
-        next unless $item->{holdallowed};
+        next unless $item->{holdallowed} ne 'not_allowed';
 
         push @{ $items_by_branch{ $item->{holdingbranch} } }, $item
           unless exists $allocated_items{ $item->{itemnumber} };
@@ -510,12 +566,13 @@ sub MapItemsToHoldRequests {
         my $holding_branch_items = $items_by_branch{$pickup_branch};
         if ( $holding_branch_items ) {
             foreach my $item (@$holding_branch_items) {
+                next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
+
                 if (
                     $request->{borrowerbranch} eq $item->{homebranch}
-                    && ( ( $item->{hold_fulfillment_policy} eq 'any' ) # Don't fill item level holds that contravene the hold pickup policy at this time
-                        || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} } )
+                    && _checkHoldPolicy($item, $request) # Don't fill item level holds that contravene the hold pickup policy at this time
                     && ( !$request->{itemtype} # If hold itemtype is set, item's itemtype must match
-                        || $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} )
+                        || ( $request->{itemnumber} && ( $items_by_itemnumber{ $request->{itemnumber} }->{itype} eq $request->{itemtype} ) ) )
                   )
                 {
                     $itemnumber = $item->{itemnumber};
@@ -532,10 +589,10 @@ sub MapItemsToHoldRequests {
                 my $holding_branch_items = $items_by_branch{$holdingbranch};
                 foreach my $item (@$holding_branch_items) {
                     next if $request->{borrowerbranch} ne $item->{homebranch};
+                    next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
 
                     # Don't fill item level holds that contravene the hold pickup policy at this time
-                    next unless $item->{hold_fulfillment_policy} eq 'any'
-                        || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
+                    next unless _checkHoldPolicy($item, $request);
 
                     # If hold itemtype is set, item's itemtype must match
                     next unless ( !$request->{itemtype}
@@ -567,7 +624,8 @@ sub MapItemsToHoldRequests {
                 $holdingbranch ||= $branch;
                 foreach my $item (@$holding_branch_items) {
                     next if $pickup_branch ne $item->{homebranch};
-                    next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
+                    next unless _checkHoldPolicy($item, $request);
+                    next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
 
                     # Don't fill item level holds that contravene the hold pickup policy at this time
                     next unless $item->{hold_fulfillment_policy} eq 'any'
@@ -584,21 +642,18 @@ sub MapItemsToHoldRequests {
             }
 
             # Now try items from the least cost branch based on the transport cost matrix or StaticHoldsQueueWeight
-            unless ( $itemnumber ) {
+            unless ( $itemnumber || !$holdingbranch) {
                 foreach my $current_item ( @{ $items_by_branch{$holdingbranch} } ) {
-                    if ( $holdingbranch && ( $current_item->{holdallowed} == 2 || $request->{borrowerbranch} eq $current_item->{homebranch} ) ) {
+                    next unless _checkHoldPolicy($current_item, $request); # Don't fill item level holds that contravene the hold pickup policy at this time
 
-                        # Don't fill item level holds that contravene the hold pickup policy at this time
-                        next unless $current_item->{hold_fulfillment_policy} eq 'any'
-                            || $request->{branchcode} eq $current_item->{ $current_item->{hold_fulfillment_policy} };
+                    # If hold itemtype is set, item's itemtype must match
+                    next unless ( !$request->{itemtype}
+                        || $current_item->{itype} eq $request->{itemtype} );
 
-                        # If hold itemtype is set, item's itemtype must match
-                        next unless ( !$request->{itemtype}
-                            || $current_item->{itype} eq $request->{itemtype} );
+                    next unless $items_by_itemnumber{ $current_item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
 
-                        $itemnumber = $current_item->{itemnumber};
-                        last; # quit this loop as soon as we have a suitable item
-                    }
+                    $itemnumber = $current_item->{itemnumber};
+                    last; # quit this loop as soon as we have a suitable item
                 }
             }
 
@@ -610,16 +665,15 @@ sub MapItemsToHoldRequests {
                       or next;
 
                     foreach my $item (@$holding_branch_items) {
-                        next if ( $item->{holdallowed} == 1 && $item->{homebranch} ne $request->{borrowerbranch} );
-
                         # Don't fill item level holds that contravene the hold pickup policy at this time
-                        next unless $item->{hold_fulfillment_policy} eq 'any'
-                            || $request->{branchcode} eq $item->{ $item->{hold_fulfillment_policy} };
+                        next unless _checkHoldPolicy($item, $request);
 
                         # If hold itemtype is set, item's itemtype must match
                         next unless ( !$request->{itemtype}
                             || $item->{itype} eq $request->{itemtype} );
 
+                        next unless $items_by_itemnumber{ $item->{itemnumber} }->{_object}->can_be_transferred( { to => $libraries->{ $request->{branchcode} } } );
+
                         $itemnumber = $item->{itemnumber};
                         $holdingbranch = $branch;
                         last PULL_BRANCHES2;
@@ -639,7 +693,8 @@ sub MapItemsToHoldRequests {
                 biblionumber => $request->{biblionumber},
                 holdingbranch => $holdingbranch,
                 pickup_branch => $pickup_branch,
-                item_level => 0,
+                reserve_id => $request->{reserve_id},
+                item_level => $request->{item_level_hold},
                 reservedate => $request->{reservedate},
                 reservenotes => $request->{reservenotes},
             };
@@ -704,15 +759,15 @@ sub AddToHoldTargetMap {
     my $dbh = C4::Context->dbh;
 
     my $insert_sql = q(
-        INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request)
-                               VALUES (?, ?, ?, ?, ?)
+        INSERT INTO hold_fill_targets (borrowernumber, biblionumber, itemnumber, source_branchcode, item_level_request, reserve_id)
+                               VALUES (?, ?, ?, ?, ?, ?)
     );
     my $sth_insert = $dbh->prepare($insert_sql);
 
     foreach my $itemnumber (keys %$item_map) {
         my $mapped_item = $item_map->{$itemnumber};
         $sth_insert->execute($mapped_item->{borrowernumber}, $mapped_item->{biblionumber}, $itemnumber,
-                             $mapped_item->{holdingbranch}, $mapped_item->{item_level});
+                             $mapped_item->{holdingbranch}, $mapped_item->{item_level}, $mapped_item->{reserve_id});
     }
 }