Bug 12830: Move the order-related code into Koha::Acquisition::Order
[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 => 6;
6 use C4::Acquisition;
7 use C4::Biblio qw( AddBiblio DelBiblio );
8 use C4::Bookseller;
9 use C4::Budgets;
10 use C4::Context;
11
12 use Koha::Acquisition::Order;
13
14 # Start transaction
15 my $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
18
19 $dbh->do(q{
20     DELETE FROM aqorders;
21 });
22
23 my $booksellerid = C4::Bookseller::AddBookseller(
24     {
25         name => "my vendor",
26         address1 => "bookseller's address",
27         phone => "0123456",
28         active => 1
29     }
30 );
31
32 my $basketno = C4::Acquisition::NewBasket(
33     $booksellerid
34 );
35
36 my $budgetid = C4::Budgets::AddBudget(
37     {
38         budget_code => "budget_code_test_close_reopen",
39         budget_name => "budget_name_test_close_reopen",
40     }
41 );
42
43 my $budget = C4::Budgets::GetBudget( $budgetid );
44
45 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
46 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
47
48 my $order1 = Koha::Acquisition::Order->new(
49     {
50         basketno => $basketno,
51         quantity => 24,
52         biblionumber => $biblionumber1,
53         budget_id => $budget->{budget_id},
54     }
55 )->insert;
56 my $ordernumber1 = $order1->{ordernumber};
57
58 my $order2 = Koha::Acquisition::Order->new(
59     {
60         basketno => $basketno,
61         quantity => 42,
62         biblionumber => $biblionumber2,
63         budget_id => $budget->{budget_id},
64     }
65 )->insert;
66 my $ordernumber2 = $order2->{ordernumber};
67
68 my $nb_biblio = C4::Acquisition::GetBiblioCountByBasketno( $basketno );
69 is ( $nb_biblio, 2, "There are 2 biblio for this basket" );
70 my @orders = C4::Acquisition::GetOrders( $basketno );
71 is( scalar(@orders), 2, "2 orders are created" );
72 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new before closing the basket" );
73
74 C4::Acquisition::CloseBasket( $basketno );
75 @orders = C4::Acquisition::GetOrders( $basketno );
76 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 2, "2 orders are ordered, the basket is closed" );
77
78 C4::Acquisition::ReopenBasket( $basketno );
79 @orders = C4::Acquisition::GetOrders( $basketno );
80 is ( scalar( map { $_->{orderstatus} eq 'ordered' ? 1 : () } @orders ), 0, "No order are ordered, the basket is reopen" );
81 is ( scalar( map { $_->{orderstatus} eq 'new' ? 1 : () } @orders ), 2, "2 orders are new, the basket is reopen" );
82
83 $dbh->rollback;