4 use Test::More tests => 40;
7 use C4::Acquisition qw( NewBasket GetBasketsInfosByBookseller );
8 use C4::Biblio qw( AddBiblio );
9 use C4::Budgets qw( AddBudget );
12 use Koha::Acquisition::Orders;
14 my $schema = Koha::Database->new()->schema();
15 $schema->storage->txn_begin();
17 my $dbh = C4::Context->dbh;
18 $dbh->{RaiseError} = 1;
20 my $supplier = Koha::Acquisition::Bookseller->new(
23 address1 => 'bookseller\'s address',
29 my $supplierid = $supplier->id;
32 ok($basketno = NewBasket($supplierid, 1), 'NewBasket( $supplierid , 1 ) returns $basketno');
34 my $budgetid = C4::Budgets::AddBudget(
36 budget_code => 'budget_code_test_1',
37 budget_name => 'budget_name_test_1',
40 my $budget = C4::Budgets::GetBudget( $budgetid );
42 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
43 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
44 my ($biblionumber3, $biblioitemnumber3) = AddBiblio(MARC::Record->new, '');
46 my $order1 = Koha::Acquisition::Order->new(
48 basketno => $basketno,
50 biblionumber => $biblionumber1,
51 budget_id => $budget->{budget_id},
54 my $ordernumber1 = $order1->ordernumber;
56 my $order2 = Koha::Acquisition::Order->new(
58 basketno => $basketno,
60 biblionumber => $biblionumber2,
61 budget_id => $budget->{budget_id},
64 my $ordernumber2 = $order2->ordernumber;
66 my $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
67 is( scalar(@$baskets), 1, 'Start: 1 basket' );
68 my $basket = $baskets->[0];
69 is( $basket->{total_items}, 6, 'Start with 6 items' );
70 is( $basket->{total_biblios}, 2, 'Start with 2 biblios' );
71 is( $basket->{total_items_cancelled}, 0, 'Start with 0 item cancelled' );
72 is( $basket->{expected_items}, 6, 'Start with 6 items expected' );
73 is( $basket->{total_biblios_cancelled}, 0, 'Start with 0 biblio cancelled' );
75 C4::Acquisition::DelOrder( $biblionumber2, $ordernumber2 );
76 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
77 is( scalar(@$baskets), 1, 'Order2 deleted, still 1 basket' );
78 $basket = $baskets->[0];
79 is( $basket->{total_items}, 6, 'Order2 deleted, still 6 items' );
80 is( $basket->{total_biblios}, 2, 'Order2 deleted, still 2 biblios' );
81 is( $basket->{total_items_cancelled}, 4, 'Order2 deleted, 4 items cancelled' );
82 is( $basket->{expected_items}, 2, 'Order2 deleted, now 2 items are expected' );
83 is( $basket->{total_biblios_cancelled}, 1, 'Order2 deleted, 1 biblios cancelled' );
85 C4::Acquisition::DelOrder( $biblionumber1, $ordernumber1 );
86 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
87 is( scalar(@$baskets), 1, 'Both orders deleted, still 1 basket' );
88 $basket = $baskets->[0];
89 is( $basket->{total_items}, 6, 'Both orders deleted, still 6 items' );
90 is( $basket->{total_biblios}, 2, 'Both orders deleted, still 2 biblios' );
91 is( $basket->{total_items_cancelled}, 6, 'Both orders deleted, 6 items cancelled' );
92 is( $basket->{expected_items}, 0, 'Both orders delete, now 0 items are expected' );
93 is( $basket->{total_biblios_cancelled}, 2, 'Both orders deleted, 2 biblios cancelled' );
95 C4::Acquisition::CloseBasket( $basketno );
96 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
97 is( scalar(@$baskets), 0, 'Basket is closed, 0 basket opened' );
98 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid, 1 );
99 is( scalar(@$baskets), 1, 'Basket is closed, test allbasket parameter');
101 C4::Acquisition::ReopenBasket( $basketno );
102 my $order3 = Koha::Acquisition::Order->new(
104 basketno => $basketno,
106 biblionumber => $biblionumber3,
107 budget_id => $budget->{budget_id},
110 my $ordernumber3 = $order3->ordernumber;
112 C4::Acquisition::CloseBasket( $basketno );
113 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
114 is( scalar(@$baskets), 1, 'Basket is closed and has items to receive' );
115 $basket = $baskets->[0];
116 is( $basket->{total_items}, 10, '2 orders deleted, 1 order pending, 10 items total' );
117 is( $basket->{total_biblios}, 3, '2 orders deleted, 1 order pending, 3 biblios total' );
118 is( $basket->{total_items_cancelled}, 6, '2 orders deleted, 1 order pending, still 6 items cancelled' );
119 is( $basket->{expected_items}, 4, '2 orders delete, 1 order pending, 4 items are expected' );
120 is( $basket->{total_biblios_cancelled}, 2, '2 orders deleted, 1 order pending, still 2 biblios cancelled' );
122 my $invoiceid1 = C4::Acquisition::AddInvoice(invoicenumber => 'invoice1', booksellerid => $supplierid, unknown => "unknown");
123 my $invoice1 = C4::Acquisition::GetInvoice( $invoiceid1 );
124 my ( $date_received, $ordernumber4 ) = C4::Acquisition::ModReceiveOrder(
126 biblionumber => $biblionumber3,
127 order => $order3->unblessed,
128 quantityreceived => 2,
129 budget_id => $budget->{budget_id},
130 invoice => $invoice1,
133 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
134 is( scalar(@$baskets), 1, 'Basket is closed and still has items to receive' );
135 $basket = $baskets->[0];
136 is( $basket->{total_items}, 10, '2 orders deleted, 1 order pending, 10 items total' );
137 is( $basket->{total_biblios}, 3, '2 orders deleted, 1 order pending, 3 biblios total' );
138 is( $basket->{total_items_cancelled}, 6, '2 orders deleted, 1 order pending, still 6 items cancelled' );
139 is( $basket->{expected_items}, 2, '2 orders delete, 1 order pending, 2 items are expected' );
140 is( $basket->{total_biblios_cancelled}, 2, '2 orders deleted, 1 order pending, still 2 biblios cancelled' );
142 my $invoiceid2 = C4::Acquisition::AddInvoice(invoicenumber => 'invoice1', booksellerid => $supplierid, unknown => "unknown");
143 my $invoice2 = C4::Acquisition::GetInvoice( $invoiceid2 );
144 my ( $date_received2, $ordernumber5 ) = C4::Acquisition::ModReceiveOrder(
146 biblionumber => $biblionumber3,
147 order => $order3->get_from_storage->unblessed,
148 quantityreceived => 2,
149 budget_id => $budget->{budget_id},
150 invoice => $invoice2,
153 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid, 1 );
154 is( scalar(@$baskets), 1, 'Basket is closed and has no items to receive, test allbasket parameter');
155 $basket = $baskets->[0];
156 is( $basket->{total_items}, 10, 'Basket is closed and has no items to receive, still 10 items total' );
157 is( $basket->{total_biblios}, 3, 'Basket is closed and has no items to receive, still 3 biblios total' );
158 is( $basket->{total_items_cancelled}, 6, 'Basket is closed and has no items to receive, still 6 items cancelled' );
159 is( $basket->{expected_items}, 0, 'Basket is closed and has no items to receive, 0 items are expected' );
160 is( $basket->{total_biblios_cancelled}, 2, 'Basket is closed and has no items to receive, still 2 biblios cancelled' );
162 $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid );
163 is( scalar(@$baskets), 0, 'Basket is closed and has no items to receive' );
165 $schema->storage->txn_rollback();