3 # Copyright 2021 Joonas Kylmälä <joonas.kylmala@iki.fi>
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>.
22 use Test::More tests => 3;
24 use Koha::Edifact::Order;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest 'beggining_of_message tests' => sub {
35 $schema->storage->txn_begin;
37 my $basketno = sprintf '%011d', '123456';
38 my $edi_vendor = $builder->build(
40 source => 'VendorEdiAccount',
46 my $dbic_edi_vendor = $schema->resultset('VendorEdiAccount')->find($edi_vendor->{id});
48 my $bgm = Koha::Edifact::Order::beginning_of_message($basketno, $dbic_edi_vendor, 1);
49 is( $bgm, qq{BGM+220+$basketno+9'}, "When vendor is set to EDItEUR standard we use 220 in BGM segment");
51 $dbic_edi_vendor->update({ standard => 'BIC'});
52 $bgm = Koha::Edifact::Order::beginning_of_message($basketno, $dbic_edi_vendor, 1);
53 is( $bgm, qq{BGM+220+$basketno+9'}, "When vendor is set to BiC standard we use 22V in BGM segment");
55 $schema->storage->txn_rollback;
58 subtest 'order_line() tests' => sub {
59 # TODO: Split up order_line() to smaller methods in order
60 # to allow better testing
63 $schema->storage->txn_begin;
65 my $biblio = $builder->build_sample_biblio();
66 my $biblioitem = $biblio->biblioitem;
67 $biblioitem->update({ isbn => '979-8572072303' });
68 my $biblioitem_itype = $biblioitem->itemtype;
70 my $item1 = $builder->build_sample_item(
72 biblionumber => $biblio->biblionumber,
73 location => 'PROCESSING',
74 itemcallnumber => '000.101'
77 my $item1_homebranch = $item1->homebranch;
78 my $item1_itype = $item1->effective_itemtype;
79 my $item2 = $builder->build_sample_item(
81 biblionumber => $biblio->biblionumber,
82 location => 'PROCESSING',
83 itemcallnumber => '000.102'
86 my $item2_homebranch = $item2->homebranch;
87 my $item2_itype = $item2->effective_itemtype;
89 my $ean = $builder->build( { source => 'EdifactEan' } );
90 my $dbic_ean = $schema->resultset('EdifactEan')->find($ean->{ee_id});
91 my $order = $builder->build_object(
93 class => 'Koha::Acquisition::Orders',
95 biblionumber => $biblio->biblionumber,
97 line_item_id => 'EDILINEID1',
98 order_vendornote => 'A not so pretty note',
103 my $ordernumber = $order->ordernumber;
104 my $supplier_qualifier = $order->suppliers_reference_qualifier;
105 my $supplier_ordernumber = $order->suppliers_reference_number;
106 my $budgetcode = $order->fund->budget_code;
107 my $deliveryplace = $order->basket->deliveryplace;
108 $order->add_item($item1->itemnumber);
109 $order->add_item($item2->itemnumber);
111 my $vendor = $builder->build(
113 source => 'VendorEdiAccount',
115 vendor_id => $order->basket->bookseller->id,
119 my $dbic_vendor = $schema->resultset('VendorEdiAccount')->find($vendor->{id});
121 my @orders = $schema->resultset('Aqorder')
122 ->search( { basketno => $order->basket->basketno } )->all;
124 my $edi_order = Koha::Edifact::Order->new(
126 orderlines => \@orders,
127 vendor => $dbic_vendor,
132 # FIXME: Add test for an order where the attached biblio has been deleted.
134 $order->basket->create_items('ordering')->store;
135 is( $edi_order->order_line( 1, $orders[0] ),
136 undef, 'order_line run for message formed with effective_create_items = "ordering"' );
138 my $segs = $edi_order->{segs};
139 is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
140 is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
141 is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
142 is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
143 is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
148 . "+$item1_homebranch:LLO"
149 . "+$item1_itype:LST"
153 'GIR segment added for first item and contains item record data'
159 . "+$item2_homebranch:LLO"
160 . "+$item2_itype:LST"
164 'GIR segment added for second item and contains item record data'
166 is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
167 is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
168 is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
169 is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
171 # Reset segments for effective_create_items = 'receiving'
172 $edi_order->{segs} = [];
174 $order->basket->create_items('receiving')->store;
175 is( $edi_order->order_line( 1, $orders[0] ),
176 undef, 'order_line run for message formed with effective_create_items = "receiving"' );
178 $segs = $edi_order->{segs};
179 is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
180 is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
181 is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
182 is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
183 is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
188 . "+$deliveryplace:LLO"
189 . "+$biblioitem_itype:LST"
191 'GIR segment added for first item and contains biblioitem data'
197 . "+$deliveryplace:LLO"
198 . "+$biblioitem_itype:LST"
200 'GIR segment added for second item and contains biblioitem data'
202 is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
203 is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
204 is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
205 is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
207 $schema->storage->txn_rollback;
210 subtest 'filename() tests' => sub {
213 $schema->storage->txn_begin;
215 my $ean = $builder->build( { source => 'EdifactEan' } );
217 $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
218 my $vendor = $builder->build(
220 source => 'VendorEdiAccount',
221 value => { vendor_id => $order->basket->bookseller->id }
225 my @orders = $schema->resultset('Aqorder')
226 ->search( { basketno => $order->basket->basketno } )->all;
228 my $edi_order = Koha::Edifact::Order->new(
230 orderlines => \@orders,
236 my $expected_filename = 'ordr' . $order->basket->basketno . '.CEP';
237 is( $edi_order->filename, $expected_filename,
238 'Filename is formed from the basket number' );
240 $schema->storage->txn_rollback;