Bug 17600: Standardize our EXPORT_OK
[koha.git] / t / db_dependent / Acquisition / close_reopen_basket.t
1 #!/usr/bin/env perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 14;
6 use C4::Acquisition qw( NewBasket GetBiblioCountByBasketno GetOrders GetOrder ReopenBasket );
7 use C4::Biblio qw( AddBiblio );
8 use C4::Budgets qw( AddBudget GetBudget );
9 use C4::Context;
10 use Koha::Database;
11 use Koha::Acquisition::Booksellers;
12 use Koha::Acquisition::Orders;
13
14 # Start transaction
15 my $schema = Koha::Database->new()->schema();
16 $schema->storage->txn_begin();
17
18 my $dbh = C4::Context->dbh;
19
20 $dbh->do(q{
21     DELETE FROM aqorders;
22 });
23
24 my $bookseller = Koha::Acquisition::Bookseller->new(
25     {
26         name => "my vendor",
27         address1 => "bookseller's address",
28         phone => "0123456",
29         active => 1
30     }
31 )->store;
32
33 my $basketno = C4::Acquisition::NewBasket(
34     $bookseller->id
35 );
36
37 my $budgetid = C4::Budgets::AddBudget(
38     {
39         budget_code => "budget_code_test_close_reopen",
40         budget_name => "budget_name_test_close_reopen",
41     }
42 );
43
44 my $budget = C4::Budgets::GetBudget( $budgetid );
45
46 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
47 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
48
49 my $order1 = Koha::Acquisition::Order->new(
50     {
51         basketno => $basketno,
52         quantity => 24,
53         biblionumber => $biblionumber1,
54         budget_id => $budget->{budget_id},
55     }
56 )->store;
57 my $ordernumber1 = $order1->ordernumber;
58
59 my $order2 = Koha::Acquisition::Order->new(
60     {
61         basketno => $basketno,
62         quantity => 42,
63         biblionumber => $biblionumber2,
64         budget_id => $budget->{budget_id},
65     }
66 )->store;
67 my $ordernumber2 = $order2->ordernumber;
68
69 my $nb_biblio = C4::Acquisition::GetBiblioCountByBasketno( $basketno );
70 is ( $nb_biblio, 2, "There are 2 biblio for this basket" );
71 my @orders = C4::Acquisition::GetOrders( $basketno );
72 is( scalar(@orders), 2, "2 orders are created" );
73 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new before closing the basket" );
74
75 Koha::Acquisition::Baskets->find( $basketno )->close;
76 @orders = C4::Acquisition::GetOrders( $basketno );
77 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 2, "2 orders are ordered, the basket is closed" );
78
79 C4::Acquisition::ReopenBasket( $basketno );
80 @orders = C4::Acquisition::GetOrders( $basketno );
81 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 0, "No order is ordered, the basket is reopened" );
82 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new, the basket is reopened" );
83
84 Koha::Acquisition::Orders->find($ordernumber1)->cancel;
85 my ( $order ) = C4::Acquisition::GetOrders( $basketno, {cancelled => 1} );
86 is( $order->{ordernumber}, $ordernumber1, 'The order returned by GetOrders should have been the right one' );
87 is( $order->{orderstatus}, 'cancelled', 'cancelling the order should have set status to cancelled' );
88
89 Koha::Acquisition::Baskets->find( $basketno )->close;
90 ( $order ) = C4::Acquisition::GetOrders( $basketno, {cancelled => 1} );
91 is( $order->{ordernumber}, $ordernumber1, 'The order returned by GetOrders should have been the right one' );
92 is( $order->{orderstatus}, 'cancelled', '$basket->close should not reset the status to ordered for cancelled orders' );
93
94 C4::Acquisition::ReopenBasket( $basketno );
95 ( $order ) = C4::Acquisition::GetOrders( $basketno, {cancelled => 1} );
96 is( $order->{ordernumber}, $ordernumber1, 'The expected order is cancelled, the basket is reopened' );
97 is( $order->{orderstatus}, 'cancelled', 'ReopenBasket should not reset the status for cancelled orders' );
98
99 ( $order ) = C4::Acquisition::GetOrders( $basketno, { cancelled => 0 } );
100 is ( $order->{ordernumber}, $ordernumber2, "The expect order is not cancelled, the basket is reopened" );
101 is ( $order->{orderstatus}, 'new', 'The expected order is new, the basket is reopened' );
102
103 $schema->storage->txn_rollback();