From b8b718d6f1160b76225b7af67677f6068ea7f51a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 17 Jan 2017 16:18:54 +0100 Subject: [PATCH] Bug 17971: Add support for objects represented by fk For instance an issue is not fetch from its fk but using the fk itemnumber. We need to support them. --- C4/Letters.pm | 6 ++++-- t/db_dependent/Letters/TemplateToolkit.t | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index cbcdbb1..30b81b9 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1551,10 +1551,12 @@ sub _get_tt_params { my $fk = $config->{$table}->{fk}; if ( $is_a_loop ) { - unless ( ref( $tables->{$table} ) eq 'ARRAY' ) { + my $values = $tables->{$table} || []; + unless ( ref( $values ) eq 'ARRAY' ) { croak "ERROR processing table $table. Wrong API call."; } - my $objects = $module->search( { $pk => { -in => $tables->{$table} } } ); + my $key = $pk ? $pk : $fk; + my $objects = $module->search( { $key => { -in => $values } } ); $params->{ $config->{$table}->{plural} } = $objects; } elsif ( $ref eq q{} || $ref eq 'HASH' ) { diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 5c24b49..4329933 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -283,7 +283,7 @@ $prepared_letter = GetPreparedLetter( is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' ); subtest 'loops' => sub { - plan tests => 1; + plan tests => 2; my $code = "TEST"; my $module = "TEST"; @@ -298,6 +298,20 @@ subtest 'loops' => sub { my $expected_letter = join '', ( $patron_1->{surname}, $patron_2->{surname} ); is( $letter->{content}, $expected_letter, ); }; + + subtest 'foreign key is used' => sub { + plan tests => 1; + my $patron_1 = $builder->build({ source => 'Borrower' }); + my $patron_2 = $builder->build({ source => 'Borrower' }); + my $checkout_1 = $builder->build({ source => 'Issue', value => { borrowernumber => $patron_1->{borrowernumber} } } ); + my $checkout_2 = $builder->build({ source => 'Issue', value => { borrowernumber => $patron_1->{borrowernumber} } } ); + + my $template = q|[% FOREACH checkout IN checkouts %][% checkout.issue_id %][% END %]|; + reset_template( { template => $template, code => $code, module => $module } ); + my $letter = GetPreparedLetter( module => $module, letter_code => $code, loops => { issues => [ $checkout_1->{itemnumber}, $checkout_2->{itemnumber} ] } ); + my $expected_letter = join '', ( $checkout_1->{issue_id}, $checkout_2->{issue_id} ); + is( $letter->{content}, $expected_letter, ); + }; }; sub reset_template { -- 2.1.4