From 77b72decd41bcdcd642cb9d3f32f86611dcf1566 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. Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens Signed-off-by: Kyle M Hall --- 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 8308bac8a9..2bc3030afb 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1563,10 +1563,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 cbcfe387c2..e1ee9f821c 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -380,7 +380,7 @@ subtest 'regression tests' => sub { }; subtest 'loops' => sub { - plan tests => 1; + plan tests => 2; my $code = "TEST"; my $module = "TEST"; @@ -395,6 +395,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.39.5