Bug 19256: Make Koha::Acq::Order using Koha::Object
[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 => 10;
6 use C4::Acquisition;
7 use C4::Biblio qw( AddBiblio DelBiblio );
8 use C4::Budgets;
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 $dbh->{RaiseError} = 1;
20
21 $dbh->do(q{
22     DELETE FROM aqorders;
23 });
24
25 my $bookseller = Koha::Acquisition::Bookseller->new(
26     {
27         name => "my vendor",
28         address1 => "bookseller's address",
29         phone => "0123456",
30         active => 1
31     }
32 )->store;
33
34 my $basketno = C4::Acquisition::NewBasket(
35     $bookseller->id
36 );
37
38 my $budgetid = C4::Budgets::AddBudget(
39     {
40         budget_code => "budget_code_test_close_reopen",
41         budget_name => "budget_name_test_close_reopen",
42     }
43 );
44
45 my $budget = C4::Budgets::GetBudget( $budgetid );
46
47 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
48 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
49
50 my $order1 = Koha::Acquisition::Order->new(
51     {
52         basketno => $basketno,
53         quantity => 24,
54         biblionumber => $biblionumber1,
55         budget_id => $budget->{budget_id},
56     }
57 )->store;
58 my $ordernumber1 = $order1->ordernumber;
59
60 my $order2 = Koha::Acquisition::Order->new(
61     {
62         basketno => $basketno,
63         quantity => 42,
64         biblionumber => $biblionumber2,
65         budget_id => $budget->{budget_id},
66     }
67 )->store;
68 my $ordernumber2 = $order2->ordernumber;
69
70 my $nb_biblio = C4::Acquisition::GetBiblioCountByBasketno( $basketno );
71 is ( $nb_biblio, 2, "There are 2 biblio for this basket" );
72 my @orders = C4::Acquisition::GetOrders( $basketno );
73 is( scalar(@orders), 2, "2 orders are created" );
74 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new before closing the basket" );
75
76 C4::Acquisition::CloseBasket( $basketno );
77 @orders = C4::Acquisition::GetOrders( $basketno );
78 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 2, "2 orders are ordered, the basket is closed" );
79
80 C4::Acquisition::ReopenBasket( $basketno );
81 @orders = C4::Acquisition::GetOrders( $basketno );
82 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 0, "No order is ordered, the basket is reopened" );
83 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new, the basket is reopened" );
84
85 C4::Acquisition::DelOrder( $biblionumber1, $ordernumber1 );
86 my ( $order ) = C4::Acquisition::GetOrders( $basketno, {cancelled => 1} );
87 is( $order->{ordernumber}, $ordernumber1, 'The order returned by GetOrders should have been the right one' );
88 is( $order->{orderstatus}, 'cancelled', 'DelOrder should have set status to cancelled' );
89 C4::Acquisition::CloseBasket( $basketno );
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', 'CloseBasket should not reset the status to ordered for cancelled orders' );
93
94 $schema->storage->txn_rollback();