Bug 21205: Replace C4::Items::GetOrderFromItemnumber calls
[koha.git] / t / db_dependent / Acquisition / CancelReceipt.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 12;
21 use t::lib::TestBuilder;
22
23 use C4::Context;
24 use C4::Acquisition;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Budgets;
28 use t::lib::Mocks;
29
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Acquisition::Booksellers;
33 use Koha::Acquisition::Orders;
34 use MARC::Record;
35
36 my $schema = Koha::Database->new()->schema();
37 $schema->storage->txn_begin();
38 my $dbh = C4::Context->dbh;
39 $dbh->{RaiseError} = 1;
40
41 my $builder = t::lib::TestBuilder->new;
42 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
43
44 my $bookseller = Koha::Acquisition::Bookseller->new(
45     {
46         name => "my vendor 1",
47         address1 => "bookseller's address",
48         phone => "0123456",
49         active => 1
50     }
51 )->store;
52 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
53
54 my $basketno1 = C4::Acquisition::NewBasket(
55     $bookseller->id
56 );
57
58 my $budgetid = C4::Budgets::AddBudget(
59     {
60         budget_code => "budget_code_test_transferorder",
61         budget_name => "budget_name_test_transferorder",
62     }
63 );
64
65 my $budget = C4::Budgets::GetBudget( $budgetid );
66
67 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
68 my $itemnumber = AddItem( { itype => $itemtype }, $biblionumber );
69
70 my $order = Koha::Acquisition::Order->new(
71     {
72         basketno => $basketno1,
73         quantity => 2,
74         biblionumber => $biblionumber,
75         budget_id => $budget->{budget_id},
76     }
77 )->store;
78 my $ordernumber = $order->ordernumber;
79
80 ModReceiveOrder(
81     {
82         biblionumber     => $biblionumber,
83         order            => $order->unblessed,
84         quantityreceived => 2,
85     }
86 );
87
88 $order->add_item( $itemnumber );
89
90 CancelReceipt($ordernumber);
91
92 is($order->items->count, 0, "Create items on receiving: 0 item exist after cancelling a receipt");
93
94 my $itemnumber1 = AddItem( { itype => $itemtype }, $biblionumber );
95 my $itemnumber2 = AddItem( { itype => $itemtype }, $biblionumber );
96
97 t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
98 t::lib::Mocks::mock_preference('AcqItemSetSubfieldsWhenReceiptIsCancelled', '7=9'); # notforloan is mapped with 952$7
99 $order = Koha::Acquisition::Order->new(
100     {
101         basketno => $basketno1,
102         quantity => 2,
103         biblionumber => $biblionumber,
104         budget_id => $budget->{budget_id},
105     }
106 )->store;
107 $ordernumber = $order->ordernumber;
108
109 is( $order->parent_ordernumber, $order->ordernumber,
110     "Insert an order should set parent_order=ordernumber, if no parent_ordernumber given"
111 );
112
113 $order->add_item( $itemnumber1 );
114 $order->add_item( $itemnumber2 );
115
116 is(
117     $order->items->count,
118     2,
119     "Create items on ordering: 2 items should be linked to the order before receiving"
120 );
121
122 my ( undef, $new_ordernumber ) = ModReceiveOrder(
123     {
124         biblionumber     => $biblionumber,
125         order            => $order->unblessed,
126         quantityreceived => 1,
127         received_items   => [ $itemnumber1 ],
128     }
129 );
130
131 my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
132
133 is( $new_order->ordernumber, $new_ordernumber,
134     "ModReceiveOrder should return a correct ordernumber" );
135 isnt( $new_ordernumber, $ordernumber,
136     "ModReceiveOrder should return a different ordernumber" );
137 is( $new_order->parent_ordernumber, $ordernumber,
138     "The new order created by ModReceiveOrder should be linked to the parent order"
139 );
140
141 is(
142     $order->items->count,
143     1,
144     "Create items on ordering: 1 item should still be linked to the original order after receiving"
145 );
146 is(
147     $new_order->items->count,
148     1,
149     "Create items on ordering: 1 item should be linked to new order after receiving"
150 );
151
152 CancelReceipt($new_ordernumber);
153
154 is(
155     $new_order->items->count,
156     0,
157     "Create items on ordering: no item should be linked to the cancelled order"
158 );
159 is(
160     $order->items->count,
161     2,
162     "Create items on ordering: items are not deleted after cancelling a receipt"
163 );
164
165 my $item1 = C4::Items::GetItem( $itemnumber1 );
166 is( $item1->{notforloan}, 9, "The notforloan value has been updated with '9'" );
167
168 my $item2 = C4::Items::GetItem( $itemnumber2 );
169 is( $item2->{notforloan}, 0, "The notforloan value has been updated with '9'" );
170
171 $schema->storage->txn_rollback();