From 9af3d13abdfb4ebc69461107663763a5557482ce Mon Sep 17 00:00:00 2001
From: Petro Vashchuk <stalkernoid@gmail.com>
Date: Mon, 18 Jul 2022 12:47:21 +0300
Subject: [PATCH] Bug 27272: Add search_ordered sub to order items with extra
 conditions

---
 Koha/Items.pm           | 28 ++++++++++++++++++++++++++++
 catalogue/detail.pl     | 15 +--------------
 catalogue/moredetail.pl | 15 ++-------------
 3 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/Koha/Items.pm b/Koha/Items.pm
index 90b178a1cef..04db9654aae 100644
--- a/Koha/Items.pm
+++ b/Koha/Items.pm
@@ -398,6 +398,34 @@ sub apply_regex {
     return $value;
 }
 
+=head3 search_ordered
+
+ $items->search_ordered;
+
+Search and sort items in a specific order, depending if serials are present or not
+
+=cut
+
+sub search_ordered {
+    my ($self) = @_;
+
+    if ( $self->search({ select => ["enumchron IS NOT NULL"] }) ) {
+        return $self->search( {}, { order_by => 'enumchron' } );
+    } else {
+        return $self->search(
+            {},
+            {
+                order_by => [
+                    'homebranch.branchname',
+                    'me.enumchron',
+                    \"LPAD( me.copynumber, 8, '0' )",
+                    {-desc => 'me.dateaccessioned'}
+                ],
+                join => ['homebranch']
+            }
+        );
+    }
+}
 
 =head2 Internal methods
 
diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index c042b1d3983..69012f439eb 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -191,20 +191,7 @@ $template->param(
 );
 
 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
-
-my $all_items = $biblio->items(
-    {},
-    {
-        # FIXME A different order is expected if at least one items.serial is true
-        order_by => [
-            'homebranch.branchname',
-            'me.enumchron',
-            \"LDAP( me.copynumber, 8, '0' )",
-            -asc => 'me.dateacessioned'
-        ],
-        join => ['homebranch']
-    }
-);
+my $all_items = $biblio->items->search_ordered;
 my @items;
 my $patron = Koha::Patrons->find( $borrowernumber );
 while ( my $item = $all_items->next ) {
diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl
index d1a2c612a8c..a4ed85e03ad 100755
--- a/catalogue/moredetail.pl
+++ b/catalogue/moredetail.pl
@@ -108,19 +108,8 @@ output_and_exit( $query, $cookie, $template, 'unknown_biblio')
     unless $biblio && $record;
 
 my $fw = GetFrameworkCode($biblionumber);
-my $all_items = $biblio->items(
-    {},
-    {
-        # FIXME A different order is expected if at least one items.serial is true
-        order_by => [
-            'homebranch.branchname',
-            'me.enumchron',
-            \"LDAP( me.copynumber, 8, '0' )",
-            -asc => 'me.dateacessioned'
-        ],
-        join => ['homebranch']
-    }
-);
+my $all_items = $biblio->items->search_ordered;
+
 my @items;
 my $patron = Koha::Patrons->find( $loggedinuser );
 while ( my $item = $all_items->next ) {
-- 
2.25.1