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 => 2;
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 'order_line() tests' => sub {
33 # TODO: Split up order_line() to smaller methods in order
34 # to allow better testing
37 $schema->storage->txn_begin;
39 my $biblio = $builder->build_sample_biblio();
40 my $biblioitem = $biblio->biblioitem;
41 $biblioitem->update({ isbn => '979-8572072303' });
42 my $biblioitem_itype = $biblioitem->itemtype;
44 my $item1 = $builder->build_sample_item(
46 biblionumber => $biblio->biblionumber,
47 location => 'PROCESSING',
48 itemcallnumber => '000.101'
51 my $item1_homebranch = $item1->homebranch;
52 my $item1_itype = $item1->effective_itemtype;
53 my $item2 = $builder->build_sample_item(
55 biblionumber => $biblio->biblionumber,
56 location => 'PROCESSING',
57 itemcallnumber => '000.102'
60 my $item2_homebranch = $item2->homebranch;
61 my $item2_itype = $item2->effective_itemtype;
63 my $ean = $builder->build( { source => 'EdifactEan' } );
64 my $dbic_ean = $schema->resultset('EdifactEan')->find($ean->{ee_id});
65 my $order = $builder->build_object(
67 class => 'Koha::Acquisition::Orders',
69 biblionumber => $biblio->biblionumber,
71 line_item_id => 'EDILINEID1',
72 order_vendornote => 'A not so pretty note',
77 my $ordernumber = $order->ordernumber;
78 my $supplier_qualifier = $order->suppliers_reference_qualifier;
79 my $supplier_ordernumber = $order->suppliers_reference_number;
80 my $budgetcode = $order->fund->budget_code;
81 my $deliveryplace = $order->basket->deliveryplace;
82 $order->add_item($item1->itemnumber);
83 $order->add_item($item2->itemnumber);
85 my $vendor = $builder->build(
87 source => 'VendorEdiAccount',
89 vendor_id => $order->basket->bookseller->id,
93 my $dbic_vendor = $schema->resultset('VendorEdiAccount')->find($vendor->{id});
95 my @orders = $schema->resultset('Aqorder')
96 ->search( { basketno => $order->basket->basketno } )->all;
98 my $edi_order = Koha::Edifact::Order->new(
100 orderlines => \@orders,
101 vendor => $dbic_vendor,
106 # FIXME: Add test for an order where the attached biblio has been deleted.
108 $order->basket->create_items('ordering')->store;
109 is( $edi_order->order_line( 1, $orders[0] ),
110 undef, 'order_line run for message formed with effective_create_items = "ordering"' );
112 my $segs = $edi_order->{segs};
113 is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
114 is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
115 is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
116 is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
117 is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
122 . "+$item1_homebranch:LLO"
123 . "+$item1_itype:LST"
127 'GIR segment added for first item and contains item record data'
133 . "+$item2_homebranch:LLO"
134 . "+$item2_itype:LST"
138 'GIR segment added for second item and contains item record data'
140 is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
141 is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
142 is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
143 is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
145 # Reset segments for effective_create_items = 'receiving'
146 $edi_order->{segs} = [];
148 $order->basket->create_items('receiving')->store;
149 is( $edi_order->order_line( 1, $orders[0] ),
150 undef, 'order_line run for message formed with effective_create_items = "receiving"' );
152 $segs = $edi_order->{segs};
153 is( $segs->[0], 'LIN+1++EDILINEID1:EN\'', 'LIN segment added containing order->line_item_id' );
154 is( $segs->[1], 'PIA+5+8572072303:IB\'', 'PIA segment added with example biblioitem->isbn13' );
155 is( $segs->[2], 'IMD+L+009+:::Some boring author\'', 'IMD segment added containing demo data author' );
156 is( $segs->[3], 'IMD+L+050+:::Some boring read\'', 'IMD segment added containing demo data title' );
157 is( $segs->[4], 'QTY+21:2\'', 'QTY segment added containing the number of items expected' );
162 . "+$deliveryplace:LLO"
163 . "+$biblioitem_itype:LST"
165 'GIR segment added for first item and contains biblioitem data'
171 . "+$deliveryplace:LLO"
172 . "+$biblioitem_itype:LST"
174 'GIR segment added for second item and contains biblioitem data'
176 is( $segs->[7], 'FTX+LIN+++A not so pretty note\'', 'FTX segment added containing data from vendor_note' );
177 is( $segs->[8], 'PRI+AAE:1.50:CA\'', 'PRI segment added containing data orderline listprice' );
178 is( $segs->[9], "RFF+LI:$ordernumber'", 'RFF segment added containing koha orderline id' );
179 is( $segs->[10], "RFF+$supplier_qualifier:$supplier_ordernumber'", 'RFF segment added containing supplier orderline id' );
181 $schema->storage->txn_rollback;
184 subtest 'filename() tests' => sub {
187 $schema->storage->txn_begin;
189 my $ean = $builder->build( { source => 'EdifactEan' } );
191 $builder->build_object( { class => 'Koha::Acquisition::Orders' } );
192 my $vendor = $builder->build(
194 source => 'VendorEdiAccount',
195 value => { vendor_id => $order->basket->bookseller->id }
199 my @orders = $schema->resultset('Aqorder')
200 ->search( { basketno => $order->basket->basketno } )->all;
202 my $edi_order = Koha::Edifact::Order->new(
204 orderlines => \@orders,
210 my $expected_filename = 'ordr' . $order->basket->basketno . '.CEP';
211 is( $edi_order->filename, $expected_filename,
212 'Filename is formed from the basket number' );
214 $schema->storage->txn_rollback;