Bug 22280: Add a unit test
[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 Koha::Items;
35 use MARC::Record;
36
37 my $schema = Koha::Database->new()->schema();
38 $schema->storage->txn_begin();
39 my $dbh = C4::Context->dbh;
40 $dbh->{RaiseError} = 1;
41
42 my $builder = t::lib::TestBuilder->new;
43 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
44
45 my $bookseller = Koha::Acquisition::Bookseller->new(
46     {
47         name => "my vendor 1",
48         address1 => "bookseller's address",
49         phone => "0123456",
50         active => 1
51     }
52 )->store;
53 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
54
55 my $basketno1 = C4::Acquisition::NewBasket(
56     $bookseller->id
57 );
58
59 my $budgetid = C4::Budgets::AddBudget(
60     {
61         budget_code => "budget_code_test_transferorder",
62         budget_name => "budget_name_test_transferorder",
63     }
64 );
65
66 my $budget = C4::Budgets::GetBudget( $budgetid );
67
68 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
69 my $itemnumber = AddItem( { itype => $itemtype }, $biblionumber );
70
71 my $order = Koha::Acquisition::Order->new(
72     {
73         basketno => $basketno1,
74         quantity => 2,
75         biblionumber => $biblionumber,
76         budget_id => $budget->{budget_id},
77     }
78 )->store;
79 my $ordernumber = $order->ordernumber;
80
81 ModReceiveOrder(
82     {
83         biblionumber     => $biblionumber,
84         order            => $order->unblessed,
85         quantityreceived => 2,
86     }
87 );
88
89 $order->add_item( $itemnumber );
90
91 CancelReceipt($ordernumber);
92
93 is($order->items->count, 0, "Create items on receiving: 0 item exist after cancelling a receipt");
94
95 my $itemnumber1 = AddItem( { itype => $itemtype }, $biblionumber );
96 my $itemnumber2 = AddItem( { itype => $itemtype }, $biblionumber );
97
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(
101     {
102         basketno => $basketno1,
103         quantity => 2,
104         biblionumber => $biblionumber,
105         budget_id => $budget->{budget_id},
106     }
107 )->store;
108 $ordernumber = $order->ordernumber;
109
110 is( $order->parent_ordernumber, $order->ordernumber,
111     "Insert an order should set parent_order=ordernumber, if no parent_ordernumber given"
112 );
113
114 $order->add_item( $itemnumber1 );
115 $order->add_item( $itemnumber2 );
116
117 is(
118     $order->items->count,
119     2,
120     "Create items on ordering: 2 items should be linked to the order before receiving"
121 );
122
123 my ( undef, $new_ordernumber ) = ModReceiveOrder(
124     {
125         biblionumber     => $biblionumber,
126         order            => $order->unblessed,
127         quantityreceived => 1,
128         received_items   => [ $itemnumber1 ],
129     }
130 );
131
132 my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
133
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"
140 );
141
142 is(
143     $order->items->count,
144     1,
145     "Create items on ordering: 1 item should still be linked to the original order after receiving"
146 );
147 is(
148     $new_order->items->count,
149     1,
150     "Create items on ordering: 1 item should be linked to new order after receiving"
151 );
152
153 CancelReceipt($new_ordernumber);
154
155 is(
156     $new_order->items->count,
157     0,
158     "Create items on ordering: no item should be linked to the cancelled order"
159 );
160 is(
161     $order->items->count,
162     2,
163     "Create items on ordering: items are not deleted after cancelling a receipt"
164 );
165
166 my $item1 = Koha::Items->find( $itemnumber1 );
167 is( $item1->notforloan, 9, "The notforloan value has been updated with '9'" );
168
169 my $item2 = Koha::Items->find( $itemnumber2 );
170 is( $item2->notforloan, 0, "The notforloan value has been updated with '9'" );
171
172 $schema->storage->txn_rollback();