Bug 14237: Database updates
[srvgit] / C4 / HoldsQueue.pm
old mode 100755 (executable)
new mode 100644 (file)
index 70bb0bc..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 );
@@ -134,10 +138,10 @@ sub GetHoldsQueueItems {
     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, biblio.subtitle, biblio.part_number,
-                         biblio.part_name,
+                         biblio.copyrightdate, biblio.subtitle, biblio.medium,
+                         biblio.part_number, biblio.part_name,
                          biblioitems.publicationyear, biblioitems.pages, biblioitems.size,
-                         biblioitems.isbn, items.copynumber
+                         biblioitems.isbn, biblioitems.editionstatement, items.copynumber
                   FROM tmp_holdsqueue
                        JOIN biblio      USING (biblionumber)
                   LEFT JOIN biblioitems USING (biblionumber)
@@ -158,9 +162,6 @@ sub GetHoldsQueueItems {
         }
         delete $row->{itemtype};
 
-        my @subtitles = split(/ \| /, $row->{'subtitle'} // '' );
-        $row->{'subtitle'} = \@subtitles;
-
         push @$items, $row;
     }
     return $items;
@@ -279,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 = ?
@@ -361,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)
@@ -403,15 +450,17 @@ 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} } } );
 
@@ -440,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},
                         };
@@ -463,12 +513,10 @@ 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} } } )
 
               )
@@ -479,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},
                 };
@@ -495,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} };
@@ -521,10 +570,9 @@ sub MapItemsToHoldRequests {
 
                 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};
@@ -544,8 +592,7 @@ sub MapItemsToHoldRequests {
                     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}
@@ -577,7 +624,7 @@ 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
@@ -595,23 +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} } } );
+                    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
                 }
             }
 
@@ -623,11 +665,8 @@ 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}
@@ -654,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},
             };
@@ -719,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});
     }
 }