From ce31a674f5c453c592a1d0994e4530ee562a145c 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 --- 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 9e3b59c..6715213 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1557,10 +1557,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 64df56c..d6023c5 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -327,7 +327,7 @@ subtest 'regression tests' => sub { }; subtest 'loops' => sub { - plan tests => 1; + plan tests => 2; my $code = "TEST"; my $module = "TEST"; @@ -342,6 +342,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.9.3