3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 12;
21 use t::lib::TestBuilder;
32 use Koha::Acquisition::Booksellers;
33 use Koha::Acquisition::Orders;
37 my $schema = Koha::Database->new()->schema();
38 $schema->storage->txn_begin();
39 my $dbh = C4::Context->dbh;
40 $dbh->{RaiseError} = 1;
42 my $builder = t::lib::TestBuilder->new;
43 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
45 my $bookseller = Koha::Acquisition::Bookseller->new(
47 name => "my vendor 1",
48 address1 => "bookseller's address",
53 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
55 my $basketno1 = C4::Acquisition::NewBasket(
59 my $budgetid = C4::Budgets::AddBudget(
61 budget_code => "budget_code_test_transferorder",
62 budget_name => "budget_name_test_transferorder",
66 my $budget = C4::Budgets::GetBudget( $budgetid );
68 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
69 my $itemnumber = AddItem( { itype => $itemtype }, $biblionumber );
71 my $order = Koha::Acquisition::Order->new(
73 basketno => $basketno1,
75 biblionumber => $biblionumber,
76 budget_id => $budget->{budget_id},
79 my $ordernumber = $order->ordernumber;
83 biblionumber => $biblionumber,
84 order => $order->unblessed,
85 quantityreceived => 2,
89 $order->add_item( $itemnumber );
91 CancelReceipt($ordernumber);
93 is($order->items->count, 0, "Create items on receiving: 0 item exist after cancelling a receipt");
95 my $itemnumber1 = AddItem( { itype => $itemtype }, $biblionumber );
96 my $itemnumber2 = AddItem( { itype => $itemtype }, $biblionumber );
98 t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
99 t::lib::Mocks::mock_preference('AcqItemSetSubfieldsWhenReceiptIsCancelled', '7=9'); # notforloan is mapped with 952$7
100 $order = Koha::Acquisition::Order->new(
102 basketno => $basketno1,
104 biblionumber => $biblionumber,
105 budget_id => $budget->{budget_id},
108 $ordernumber = $order->ordernumber;
110 is( $order->parent_ordernumber, $order->ordernumber,
111 "Insert an order should set parent_order=ordernumber, if no parent_ordernumber given"
114 $order->add_item( $itemnumber1 );
115 $order->add_item( $itemnumber2 );
118 $order->items->count,
120 "Create items on ordering: 2 items should be linked to the order before receiving"
123 my ( undef, $new_ordernumber ) = ModReceiveOrder(
125 biblionumber => $biblionumber,
126 order => $order->unblessed,
127 quantityreceived => 1,
128 received_items => [ $itemnumber1 ],
132 my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
134 is( $new_order->ordernumber, $new_ordernumber,
135 "ModReceiveOrder should return a correct ordernumber" );
136 isnt( $new_ordernumber, $ordernumber,
137 "ModReceiveOrder should return a different ordernumber" );
138 is( $new_order->parent_ordernumber, $ordernumber,
139 "The new order created by ModReceiveOrder should be linked to the parent order"
143 $order->items->count,
145 "Create items on ordering: 1 item should still be linked to the original order after receiving"
148 $new_order->items->count,
150 "Create items on ordering: 1 item should be linked to new order after receiving"
153 CancelReceipt($new_ordernumber);
156 $new_order->items->count,
158 "Create items on ordering: no item should be linked to the cancelled order"
161 $order->items->count,
163 "Create items on ordering: items are not deleted after cancelling a receipt"
166 my $item1 = Koha::Items->find( $itemnumber1 );
167 is( $item1->notforloan, 9, "The notforloan value has been updated with '9'" );
169 my $item2 = Koha::Items->find( $itemnumber2 );
170 is( $item2->notforloan, 0, "The notforloan value has been updated with '9'" );
172 $schema->storage->txn_rollback();