From 71172354cb93c9e314bf1eef4f1ae795342845bc Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 28 Apr 2025 11:47:36 +0200 Subject: [PATCH] Bug 39711: Fix 500 when viewing a basket without contract Can't call method "contractname" on an undefined value at /kohadevbox/koha/acqui/basket.pl line 462. Use Koha::Acq::Basket->contract --- Koha/Acquisition/Basket.pm | 13 ++++++++ acqui/basket.pl | 10 ++----- .../prog/en/modules/acqui/basket.tt | 5 ++-- t/db_dependent/Koha/Acquisition/Basket.t | 30 +++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Koha/Acquisition/Basket.pm b/Koha/Acquisition/Basket.pm index 9ea54b48e43..13b67d66541 100644 --- a/Koha/Acquisition/Basket.pm +++ b/Koha/Acquisition/Basket.pm @@ -95,6 +95,19 @@ sub basket_group { return Koha::Acquisition::BasketGroup->_new_from_dbic($basket_group_rs); } +=head3 contract + +Returns the contract associated to this basket + +=cut + +sub contract { + my ($self) = @_; + my $rs = $self->_result->contractnumber; + return unless $rs; + return Koha::Acquisition::Contract->_new_from_dbic($rs); +} + =head3 orders my $orders = $basket->orders; diff --git a/acqui/basket.pl b/acqui/basket.pl index d2906cf68fc..cbde5c36202 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -31,7 +31,6 @@ use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSugge use Koha::Biblios; use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; -use Koha::Acquisition::Contracts; use Koha::Acquisition::Orders; use Koha::Libraries; use C4::Letters qw( SendAlerts ); @@ -514,8 +513,6 @@ if ( $op eq 'list' ) { push @cancelledorders_loop, $line; } - my $contract = Koha::Acquisition::Contracts->find( $basket->{contractnumber} ); - if ( $basket->{basketgroupid} ) { $basketgroup = GetBasketgroup( $basket->{basketgroupid} ); } @@ -532,14 +529,13 @@ if ( $op eq 'list' ) { my $edi_order = $basket_obj->edi_order; $template->param( - basketno => $basketno, - basket => $basket, + basketno => $basketno, + basket => $basket, # FIXME Remove basket_obj and use the Koha::Acq::Basket object in the template + basket_obj => $basket_obj, basketname => $basket->{'basketname'}, basketbranchcode => $basket->{branch}, basketnote => $basket->{note}, basketbooksellernote => $basket->{booksellernote}, - basketcontractno => $basket->{contractnumber}, - basketcontractname => $contract->contractname, branches_loop => \@branches_loop, creationdate => $basket->{creationdate}, edi_order => $edi_order, diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt index 39e83cc4adc..f62d29eb208 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -489,10 +489,11 @@ [% IF ( basketbooksellernote ) %]
  • Vendor note: [% basketbooksellernote | html %]
  • [% END %] - [% IF ( basketcontractno ) %] + [% SET contract = basket_obj.contract %] + [% IF contract %]
  • Contract name: - [% basketcontractname | html %]
  • [% contract.contractname| html %] [% END %] [% IF deliveryplace %] diff --git a/t/db_dependent/Koha/Acquisition/Basket.t b/t/db_dependent/Koha/Acquisition/Basket.t index efd7c337750..f03c3ab41df 100755 --- a/t/db_dependent/Koha/Acquisition/Basket.t +++ b/t/db_dependent/Koha/Acquisition/Basket.t @@ -125,6 +125,36 @@ subtest 'basket_group' => sub { $schema->storage->txn_rollback; }; +subtest 'contract' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $basket = $builder->build_object( + { + class => 'Koha::Acquisition::Baskets', + value => { contractnumber => undef } + } + ); + is( $basket->contract, undef, '->contract returns undef is no contract for this basket' ); + + my $contract = $builder->build_object( + { + class => 'Koha::Acquisition::Contracts', + } + ); + + $basket->contractnumber( $contract->contractnumber )->store; + + is( + ref( $basket->contract ), 'Koha::Acquisition::Contract', + '->contract should return a Koha::Acquisition::Contract object if linked to a contract' + ); + + $schema->storage->txn_rollback; +}; + subtest 'creator() tests' => sub { plan tests => 4; -- 2.50.1