From 77eba5d1b23ba2ddabe8b70ff354bfdce86e6af8 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 19 Oct 2022 17:13:13 -0300 Subject: [PATCH] Bug 31115: Add additional_attributes support to GetInvoices This patch adds support for searching additional_fields when retrieving invoices using C4::Acquisition::Invoices. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Acquisition.t => SUCCESS: Tests pass! 3. Sign off :-D Sponsored-by: The Research University in the Helmholtz Association (KIT) Signed-off-by: Michaela Sieber Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Acquisition.pm | 25 ++++++++++++ t/db_dependent/Acquisition.t | 73 +++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 1edd0520ef..12178420bf 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -30,6 +30,7 @@ use C4::Templates qw(gettemplate); use Koha::DateUtils qw( dt_from_string ); use Koha::Acquisition::Baskets; use Koha::Acquisition::Booksellers; +use Koha::Acquisition::Invoices; use Koha::Acquisition::Orders; use Koha::Biblios; use Koha::Exceptions; @@ -2412,6 +2413,18 @@ asc is the default if omitted sub GetInvoices { my %args = @_; + my $additional_fields = $args{additional_fields} // []; + my $matching_invoice_ids_for_additional_fields = []; + if ( @$additional_fields ) { + my @invoices = Koha::Acquisition::Invoices->filter_by_additional_fields($additional_fields)->as_list; + + return () unless @invoices; + + $matching_invoice_ids_for_additional_fields = [ map { + $_->id + } @invoices ]; + } + my @columns = qw(invoicenumber booksellerid shipmentdate billingdate closedate shipmentcost shipmentcost_budgetid); @@ -2504,6 +2517,18 @@ sub GetInvoices { } $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs; + + # Handle additional fields filtering + if ( @$additional_fields ) { + my $operator = ' WHERE'; + if ( @bind_strs ) { # there's a WHERE already + $operator = ' AND'; + } + $query .= "$operator aqinvoices.invoiceid IN (" + . join( ', ', @$matching_invoice_ids_for_additional_fields ) + . ')'; + } + $query .= " GROUP BY aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id, aqbooksellers.name"; if($args{order_by}) { diff --git a/t/db_dependent/Acquisition.t b/t/db_dependent/Acquisition.t index 0f3839ec07..1a4863943e 100755 --- a/t/db_dependent/Acquisition.t +++ b/t/db_dependent/Acquisition.t @@ -19,7 +19,7 @@ use Modern::Perl; use POSIX qw(strftime); -use Test::More tests => 70; +use Test::More tests => 71; use t::lib::Mocks; use Koha::Database; use Koha::DateUtils qw(dt_from_string output_pref); @@ -28,7 +28,7 @@ use Koha::Acquisition::Basket; use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' ); BEGIN { - use_ok('C4::Acquisition', qw( NewBasket GetBasket AddInvoice GetInvoice ModReceiveOrder SearchOrders GetOrder GetHistory ModOrder get_rounding_sql get_rounded_price ReopenBasket ModBasket ModBasketHeader ModBasketUsers )); + use_ok('C4::Acquisition', qw( NewBasket GetBasket AddInvoice GetInvoice GetInvoices ModReceiveOrder SearchOrders GetOrder GetHistory ModOrder get_rounding_sql get_rounded_price ReopenBasket ModBasket ModBasketHeader ModBasketUsers )); use_ok('C4::Biblio', qw( AddBiblio GetMarcSubfieldStructure )); use_ok('C4::Budgets', qw( AddBudgetPeriod AddBudget GetBudget GetBudgetByOrderNumber GetBudgetsReport GetBudgets GetBudgetReport )); use_ok('Koha::Acquisition::Orders'); @@ -983,3 +983,72 @@ subtest 'Acquisition logging' => sub { }; $schema->storage->txn_rollback(); + +subtest 'GetInvoices() tests with additional fields' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $builder = t::lib::TestBuilder->new; + + my $invoice_1 = $builder->build_object( + { + class => 'Koha::Acquisition::Invoices', + value => { + invoicenumber => 'whataretheodds1' + } + } + ); + my $invoice_2 = $builder->build_object( + { + class => 'Koha::Acquisition::Invoices', + value => { + invoicenumber => 'whataretheodds2' + } + } + ); + + + my $invoices = [ GetInvoices( invoicenumber => 'whataretheodds' ) ]; + is( scalar @{$invoices}, 2, 'Two invoices retrieved' ); + is( $invoices->[0]->{invoiceid}, $invoice_1->id ); + is( $invoices->[1]->{invoiceid}, $invoice_2->id ); + + my $additional_field_1 = $builder->build_object( + { class => 'Koha::AdditionalFields', + value => { + tablename => 'aqinvoices', + authorised_value_category => "", + } + } + ); + + my $additional_field_2 = $builder->build_object( + { class => 'Koha::AdditionalFields', + value => { + tablename => 'aqinvoices', + authorised_value_category => "", + } + } + ); + + $invoice_1->set_additional_fields([ { id => $additional_field_1->id, value => 'Ya-Hey' } ]); + $invoice_2->set_additional_fields([ { id => $additional_field_2->id, value => "Hey ho let's go" } ]); + + $invoices = [ GetInvoices( + invoicenumber => 'whataretheodds', + additional_fields => [{ id => $additional_field_1->id, value => 'Ya-Hey' }] + )]; + is( scalar @{$invoices}, 1, 'One invoice retrieved' ); + is( $invoices->[0]->{invoiceid}, $invoice_1->id, 'Ya-Hey' ); + + $invoices = [ GetInvoices( + invoicenumber => 'whataretheodds', + additional_fields => [{ id => $additional_field_2->id, value => "Hey ho let's go" }] + )]; + is( scalar @{$invoices}, 1, 'One invoice retrieved' ); + is( $invoices->[0]->{invoiceid}, $invoice_2->id, "Hey ho let's go" ); + + $schema->storage->txn_rollback; +}; -- 2.39.5