3 # Copyright 2024 Martin Renvoize <martin.renvoize@ptfs-europe.com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use FindBin qw( $Bin );
23 use Test::More tests => 1;
27 use t::lib::TestBuilder;
29 use Koha::EDI qw(process_quote);
30 use Koha::Edifact::Transport;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 subtest 'process_quote' => sub {
38 $schema->storage->txn_begin;
40 # Add our test quote file to the database for testing against
41 my $account = $builder->build(
43 source => 'VendorEdiAccount',
45 description => 'test vendor', transport => 'FILE',
49 my $dirname = ( $Bin =~ /^(.*\/t\/)/ ? $1 . 'edi_testfiles/' : q{} );
50 my $filename = 'QUOTES_SMALL.CEQ';
51 ok( -e $dirname . $filename, 'File QUOTES_SMALL.CEQ found' );
53 my $trans = Koha::Edifact::Transport->new( $account->{id} );
54 $trans->working_directory($dirname);
56 my $mhash = $trans->message_hash();
57 $mhash->{message_type} = 'QUOTE';
58 $trans->ingest( $mhash, $filename );
60 my $quote = $schema->resultset('EdifactMessage')->find( { filename => $filename } );
62 # Test quote expects REF to be a valid and active fund code
63 my $active_period = $builder->build(
65 source => 'Aqbudgetperiod',
66 value => { budget_period_active => 1 }
69 my $fund = $builder->build(
74 budget_period_id => $active_period->{budget_period_id}
79 # The quote expects a ROT1 stock rotation roata to exist
80 my $rota = $builder->build_object(
82 class => 'Koha::StockRotationRotas',
83 value => { title => 'ROT1' }
87 source => 'Stockrotationstage',
88 value => { rota_id => $rota->rota_id },
91 # Process the test quote file
92 process_quote($quote);
94 # Test for expected warnings for the passed quote file
96 # Test for quote status change
97 $quote->get_from_storage;
98 is( $quote->status, 'received', 'Quote status was set to received' );
100 # Tests for generated basket for passed quote file
101 my $baskets = Koha::Acquisition::Baskets->search( { booksellerid => $account->{vendor_id} } );
102 is( $baskets->count, 1, "1 basket created for a single quote file" );
103 my $basket = $baskets->next;
105 my $orders = $basket->orders;
106 is( $orders->count, 1, "1 order line attached to basket when only 1 order is in the edi message" );
107 my $order = $orders->next;
109 my $biblio = $order->biblio;
111 my $items = $order->items;
112 is( $items->count, 1, "1 item added when AcqCreateItem eq ordering and 1 item is in the EDI quote" );
113 my $item = $items->next;
115 # Test that item is added to rota appropriately
116 my $on_rota = Koha::StockRotationItems->search({ itemnumber_id => $item->itemnumber });
117 is($on_rota->count, 1, "Item added to stockrotation rota");
119 my $rota_item = $on_rota->next;
120 is($rota_item->stage->rota->id, $rota->id, "Item is on correct rota");
122 $schema->storage->txn_rollback;