Bug 23070: Use Koha::Hold in C4::Reserves::RevertWaitingStatus
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 6 Jun 2019 20:14:28 +0000 (15:14 -0500)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 20 Jul 2020 14:16:37 +0000 (16:16 +0200)
We are using raw SQL statements, we should use Koha::Hold instead.

This patch does not seem optimal, we would like to increment priority in
only 1 statement and without the need to fetch and loop all holds.

== Test plan ==
- apply patch
- place some holds on the same record
- check that the priorities look good
- mark one hold as waiting by doing a check-in
- revert the waiting status
- confirm that the priorities are recalculated correctly

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

C4/Reserves.pm

index 979b3de..a78f2a4 100644 (file)
@@ -1997,31 +1997,21 @@ sub RevertWaitingStatus {
     my $dbh = C4::Context->dbh;
 
     ## Get the waiting reserve we want to revert
-    my $query = "
-        SELECT * FROM reserves
-        WHERE itemnumber = ?
-        AND found IS NOT NULL
-    ";
-    my $sth = $dbh->prepare( $query );
-    $sth->execute( $itemnumber );
-    my $reserve = $sth->fetchrow_hashref();
-
-    my $hold = Koha::Holds->find( $reserve->{reserve_id} ); # TODO Remove the next raw SQL statements and use this instead
+    my $hold = Koha::Holds->search(
+        {
+            itemnumber => $itemnumber,
+            found => { not => undef },
+        }
+    )->next;
 
     ## Increment the priority of all other non-waiting
     ## reserves for this bib record
-    $query = "
-        UPDATE reserves
-        SET
-          priority = priority + 1
-        WHERE
-          biblionumber =  ?
-        AND
-          priority > 0
-    ";
-    $sth = $dbh->prepare( $query );
-    $sth->execute( $reserve->{'biblionumber'} );
+    my $holds = Koha::Holds->search({ biblionumber => $hold->biblionumber, priority => { '>' => 0 } });
+    while ( my $h = $holds->next ) {
+        $h->priority( $h->priority + 1 )->store;
+    }
 
+    ## Fix up the currently waiting reserve
     $hold->set(
         {
             priority    => 1,
@@ -2031,7 +2021,7 @@ sub RevertWaitingStatus {
         }
     )->store();
 
-    _FixPriority( { biblionumber => $reserve->{biblionumber} } );
+    _FixPriority( { biblionumber => $hold->biblionumber } );
 
     return $hold;
 }