From afabb05370217257d7e33d95d82a93a7ebdb0da5 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 | 32 ++++++++++++++++++- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Koha/Acquisition/Basket.pm b/Koha/Acquisition/Basket.pm index b22aa924409..2a77034804f 100644 --- a/Koha/Acquisition/Basket.pm +++ b/Koha/Acquisition/Basket.pm @@ -80,6 +80,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 60757a4c410..d102541af38 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 ); @@ -400,8 +399,6 @@ if ( $op eq 'list' ) { push @cancelledorders_loop, $line; } - my $contract = Koha::Acquisition::Contracts->find( $basket->{contractnumber} ); - if ( $basket->{basketgroupid} ) { $basketgroup = GetBasketgroup( $basket->{basketgroupid} ); } @@ -418,14 +415,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 25592a3b50f..341100ff165 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -318,10 +318,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 9d629c543a9..7356e454e6e 100755 --- a/t/db_dependent/Koha/Acquisition/Basket.t +++ b/t/db_dependent/Koha/Acquisition/Basket.t @@ -20,7 +20,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 14; +use Test::More tests => 15; use Test::Exception; use t::lib::TestBuilder; @@ -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.34.1