3 # Copyright 2017 Koha Development team
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 => 6;
24 use t::lib::TestBuilder;
29 my $schema = Koha::Database->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest 'basket() tests' => sub {
36 $schema->storage->txn_begin;
38 my $basket = $builder->build_object(
40 class => 'Koha::Acquisition::Baskets'
43 my $order = $builder->build_object(
45 class => 'Koha::Acquisition::Orders',
46 value => { basketno => $basket->basketno }
50 my $retrieved_basket = $order->basket;
51 is( ref($retrieved_basket), 'Koha::Acquisition::Basket',
52 'Type is correct for ->basket' );
53 is_deeply( $retrieved_basket->unblessed,
54 $basket->unblessed, "Correct basket found and updated" );
56 $schema->storage->txn_rollback;
59 subtest 'store' => sub {
62 $schema->storage->txn_begin;
63 my $o = $builder->build_object(
65 class => 'Koha::Acquisition::Orders'
69 subtest 'entrydate' => sub {
74 t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
75 $order = Koha::Acquisition::Order->new(
77 basketno => $o->basketno,
78 biblionumber => $o->biblionumber,
79 budget_id => $o->budget_id,
83 $order->discard_changes;
84 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
86 t::lib::Mocks::mock_preference( 'TimeFormat', '24hr' );
87 $order = Koha::Acquisition::Order->new(
89 basketno => $o->basketno,
90 biblionumber => $o->biblionumber,
91 budget_id => $o->budget_id,
95 $order->discard_changes;
96 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
98 $schema->storage->txn_rollback;
101 subtest 'fund' => sub {
104 $schema->storage->txn_begin;
105 my $o = $builder->build_object(
107 class => 'Koha::Acquisition::Orders',
111 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
112 is( ref( $order->fund ),
113 'Koha::Acquisition::Fund',
114 '->fund should return a Koha::Acquisition::Fund object' );
115 $schema->storage->txn_rollback;
118 subtest 'invoice' => sub {
121 $schema->storage->txn_begin;
122 my $o = $builder->build_object(
124 class => 'Koha::Acquisition::Orders',
125 value => { cancellationreason => 'XXXXXXXX', invoiceid => undef }, # not received yet
129 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
130 is( $order->invoice, undef,
131 '->invoice should return undef if no invoice defined yet');
133 my $invoice = $builder->build_object(
135 class => 'Koha::Acquisition::Invoices',
139 $o->invoiceid( $invoice->invoiceid )->store;
140 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
141 is( ref( $order->invoice ), 'Koha::Acquisition::Invoice',
142 '->invoice should return a Koha::Acquisition::Invoice object if an invoice is defined');
144 $schema->storage->txn_rollback;
147 subtest 'subscription' => sub {
150 $schema->storage->txn_begin;
151 my $o = $builder->build_object(
153 class => 'Koha::Acquisition::Orders',
154 value => { subscriptionid => undef }, # not linked to a subscription
158 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
159 is( $order->subscription, undef,
160 '->subscription should return undef if not created from a subscription');
162 $o = $builder->build_object(
164 class => 'Koha::Acquisition::Orders',
165 # Will be linked to a subscription by TestBuilder
169 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
170 is( ref( $order->subscription ), 'Koha::Subscription',
171 '->subscription should return a Koha::Subscription object if created from a subscription');
173 $schema->storage->txn_rollback;
176 subtest 'duplicate_to | add_item' => sub {
179 $schema->storage->txn_begin;
181 my $item = $builder->build_sample_item;
182 my $order_no_sub = $builder->build_object(
184 class => 'Koha::Acquisition::Orders',
187 biblionumber => $item->biblionumber,
188 subscriptionid => undef, # not linked to a subscription
192 $order_no_sub->basket->create_items(undef)->store; # use syspref
193 $order_no_sub->add_item( $item->itemnumber );
195 $item = $builder->build_sample_item;
196 my $order_from_sub = $builder->build_object(
198 class => 'Koha::Acquisition::Orders',
201 biblionumber => $item->biblionumber,
202 # Will be linked to a subscription by TestBuilder
206 $order_from_sub->basket->create_items(undef)->store; # use syspref
207 $order_from_sub->add_item( $item->itemnumber );
209 my $basket_to = $builder->build_object(
210 { class => 'Koha::Acquisition::Baskets' });
212 subtest 'Create item on receiving' => sub {
215 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
217 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
218 is( $duplicated_order->items->count, 0,
219 'Items should not be copied if the original order did not create items on ordering'
222 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
223 is( $duplicated_order->items->count, 0,
224 'Items should not be copied if the original order is created from a subscription'
228 subtest 'Create item on ordering' => sub {
231 t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
233 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
234 is( $duplicated_order->items->count, 1,
235 'Items should be copied if items are created on ordering'
238 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
239 is( $duplicated_order->items->count, 0,
240 'Items should never be copied if the original order is created from a subscription'
244 $schema->storage->txn_rollback;