From 999014d3e84d1489049be21193e55609362c6200 Mon Sep 17 00:00:00 2001 From: Petro Vashchuk Date: Mon, 18 Jul 2022 12:47:21 +0300 Subject: [PATCH] Bug 31306: Add search_ordered sub to order items with extra conditions Commit message from JD: On bug 27272 we are going to replace C4::Items::GetItemsInfo with Koha::Items methods, but we need to keep the existing behaviour that is: Order by publisheddate, enumchron for searisl, or by homebranch.branchname, enumchron, LPAD( copynumber, 8, '0' ), desc dateaccessioned Test plan: Confirm that the code from the new method makes sense Run t/db_dependent/Koha/Items.t Signed-off-by: Owen Leonard Signed-off-by: Tomas Cohen Arazi --- Koha/Items.pm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Koha/Items.pm b/Koha/Items.pm index 90b178a1ce..04db9654aa 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 -- 2.39.5