From 0841c96f3a79c90f20c4fdb5dfe61dc1bbe8f19b Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Mon, 17 Nov 2025 15:09:35 +0000 Subject: [PATCH] Bug 20125: (QA follow-up) move to methods and adds some error messages --- misc/cronjobs/ordersClaim.pl | 77 +++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/misc/cronjobs/ordersClaim.pl b/misc/cronjobs/ordersClaim.pl index d010812576..48bfad4efb 100755 --- a/misc/cronjobs/ordersClaim.pl +++ b/misc/cronjobs/ordersClaim.pl @@ -36,6 +36,7 @@ use Modern::Perl; use Getopt::Long; use Pod::Usage; +use Try::Tiny; use C4::Letters qw( GetPreparedLetter EnqueueLetter ); use Koha::Acquisition::Orders; @@ -92,37 +93,68 @@ GetOptions( pod2usage(1) if $help; -my @lateorders = Koha::Acquisition::Orders->filter_by_lates( { delay => $delay } )->as_list; +sub get_late_orders { + my ($delay) = @_; -# Sort orders by basket -my $baskets_orders; -push @{ $baskets_orders->{ $_->basketno } }, $_->ordernumber for @lateorders; + try { + return Koha::Acquisition::Orders->filter_by_lates( { delay => $delay } )->as_list; + } catch { + Koha::Exception->throw("Error getting late orders: $_"); + return []; + }; +} -while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { - my $schema = Koha::Database->new->schema; +sub get_basket_orders { + my ($late_orders) = @_; + + my $baskets_orders = {}; + foreach my $order (@$late_orders) { + push @{ $baskets_orders->{ $order->basketno } }, $order->ordernumber; + } + return $baskets_orders; +} + +sub process_basket { + my ( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ) = @_; + my $schema = Koha::Database->new->schema; my $basket = Koha::Acquisition::Baskets->find($basketno); + unless ($basket) { + Koha::Exception->throw("Basket not found for basketno: $basketno"); + } my $booksellerid = $basket->booksellerid; my $bookseller = Koha::Acquisition::Booksellers->find($booksellerid); + unless ($bookseller) { + Koha::Exception->throw("Bookseller not found for booksellerid: $booksellerid"); + } my $contact = $schema->resultset('Aqcontact')->find( { booksellerid => $booksellerid, claimacquisition => 1 }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } ); - unless ( defined($delay) ) { - $delay = $bookseller->deliverytime || 0; + unless ($contact) { + Koha::Exception->throw("No contact found for booksellerid: $booksellerid"); } + my $date_threshold = output_pref( + { + dt => dt_from_string->subtract( days => $claimed_for ), + dateformat => 'iso', + dateonly => 1 + } + ); + my $orders = []; my $order_objects = Koha::Acquisition::Orders->search( { ordernumber => { -in => $orderids } } ); + while ( my $order = $order_objects->next ) { my $claims_count = $order->claims_count; - my $last_claim = dt_from_string( $order->claimed_date ); + my $last_claim = $order->claimed_date; next if $claims_count > $max_claims; - next if $last_claim && $last_claim > dt_from_string->subtract( days => $claimed_for ); + next if defined $last_claim && $last_claim > $date_threshold; my $result = { %{ $order->unblessed }, @@ -135,7 +167,9 @@ while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { push @$orders, $result; } - next unless @$orders; + unless (@$orders) { + Koha::Exception->throw("No orders found for basketno: $basketno"); + } my $letter = GetPreparedLetter( module => 'claimacquisition', @@ -147,7 +181,11 @@ while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { 'aqbasket' => $basket->unblessed }, repeat => $orders, - ) or next; + ); + + unless ($letter) { + Koha::Exception->throw("Failed to prepare letter for basketno: $basketno"); + } my $from_address = C4::Context->preference('KohaAdminEmailAddress'); if ( my $library = Koha::Libraries->find( $basket->branch ) ) { @@ -163,7 +201,18 @@ while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { } ); - continue unless $id; + unless ($id) { + Koha::Exception->throw("Failed to enqueue letter for basketno: $basketno"); + } + + foreach my $order (@$orders) { + Koha::Acquisition::Orders->find( $order->{ordernumber} )->claim(); + } +} + +my $late_orders = get_late_orders($delay); +my $baskets_orders = get_basket_orders($late_orders); - Koha::Acquisition::Orders->find( $_->{ordernumber} )->claim() for @$orders; +while ( my ( $basketno, $orderids ) = each %$baskets_orders ) { + process_basket( $basketno, $orderids, $max_claims, $claimed_for, $letter_code ); } -- 2.39.5