Bug 20156: Display a markup for the current language in header menu
[koha.git] / t / db_dependent / Acquisition.t
blob62a9a76b5a9946dc3d82ed293d3239ef2a41163d
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use POSIX qw(strftime);
22 use Test::More tests => 65;
23 use Koha::Database;
25 BEGIN {
26 use_ok('C4::Acquisition');
27 use_ok('C4::Biblio');
28 use_ok('C4::Budgets');
29 use_ok('Koha::Acquisition::Orders');
30 use_ok('Koha::Acquisition::Booksellers');
33 # Sub used for testing C4::Acquisition subs returning order(s):
34 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
35 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
36 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
37 # params :
38 # $exp_fields : arrayref whose elements are the keys we expect to find
39 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
40 # containing content fields of the order created with Koha::Acquisition::Order
41 # $order_to_check : hashref whose keys/values are the content of an order
42 # returned by the C4::Acquisition sub we are testing
43 # returns :
44 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
45 # fields missing in $order_to_check
46 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
47 # fields unexpected in $order_to_check
48 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
49 # fields which value is not the same in between $order_to_check and
50 # $test_nbr_fields : contains the number of fields of $order_to_check
52 sub _check_fields_of_order {
53 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
54 my @test_missing_fields = ();
55 my @test_extra_fields = ();
56 my @test_different_fields = ();
57 my $test_nbr_fields = scalar( keys %$order_to_check );
58 foreach my $field (@$exp_fields) {
59 push @test_missing_fields, $field
60 unless exists( $order_to_check->{$field} );
62 foreach my $field ( keys %$order_to_check ) {
63 push @test_extra_fields, $field
64 unless grep ( /^$field$/, @$exp_fields );
66 foreach my $field ( keys %{ $original_order_content->{str} } ) {
67 push @test_different_fields, $field
68 unless ( !exists $order_to_check->{$field} )
69 or ( $original_order_content->{str}->{$field} eq
70 $order_to_check->{$field} );
72 foreach my $field ( keys %{ $original_order_content->{num} } ) {
73 push @test_different_fields, $field
74 unless ( !exists $order_to_check->{$field} )
75 or ( $original_order_content->{num}->{$field} ==
76 $order_to_check->{$field} );
78 return (
79 \@test_missing_fields, \@test_extra_fields,
80 \@test_different_fields, $test_nbr_fields
84 # Sub used for testing C4::Acquisition subs returning several orders
85 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
86 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
87 sub _check_fields_of_orders {
88 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
89 my @test_missing_fields = ();
90 my @test_extra_fields = ();
91 my @test_different_fields = ();
92 my @test_nbr_fields = ();
93 foreach my $order_to_check (@$orders_to_check) {
94 my $original_order_content =
95 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
96 @$original_orders_content )[0];
97 my (
98 $t_missing_fields, $t_extra_fields,
99 $t_different_fields, $t_nbr_fields
101 = _check_fields_of_order( $exp_fields, $original_order_content,
102 $order_to_check );
103 push @test_missing_fields, @$t_missing_fields;
104 push @test_extra_fields, @$t_extra_fields;
105 push @test_different_fields, @$t_different_fields;
106 push @test_nbr_fields, $t_nbr_fields;
108 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
109 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
110 @test_different_fields =
111 keys %{ { map { $_ => 1 } @test_different_fields } };
112 return (
113 \@test_missing_fields, \@test_extra_fields,
114 \@test_different_fields, \@test_nbr_fields
119 my $schema = Koha::Database->new()->schema();
120 $schema->storage->txn_begin();
122 my $dbh = C4::Context->dbh;
123 $dbh->{RaiseError} = 1;
125 # Creating some orders
126 my $bookseller = Koha::Acquisition::Bookseller->new(
128 name => "my vendor",
129 address1 => "bookseller's address",
130 phone => "0123456",
131 active => 1,
132 deliverytime => 5,
134 )->store;
135 my $booksellerid = $bookseller->id;
137 my $booksellerinfo = Koha::Acquisition::Booksellers->find( $booksellerid );
138 is( $booksellerinfo->deliverytime,
139 5, 'set deliverytime when creating vendor (Bug 10556)' );
141 my ( $basket, $basketno );
143 $basketno = NewBasket( $booksellerid, 1 ),
144 "NewBasket( $booksellerid , 1 ) returns $basketno"
146 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
148 my $bpid=AddBudgetPeriod({
149 budget_period_startdate => '2008-01-01'
150 , budget_period_enddate => '2008-12-31'
151 , budget_period_active => 1
152 , budget_period_description => "MAPERI"
155 my $budgetid = C4::Budgets::AddBudget(
157 budget_code => "budget_code_test_getordersbybib",
158 budget_name => "budget_name_test_getordersbybib",
159 budget_period_id => $bpid,
162 my $budget = C4::Budgets::GetBudget($budgetid);
164 my @ordernumbers;
165 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
166 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
167 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
168 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
169 my ( $biblionumber5, $biblioitemnumber5 ) = AddBiblio( MARC::Record->new, '' );
171 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
172 # Ex : a price of 50.1 will be stored internally as 5.100000
174 my @order_content = (
176 str => {
177 basketno => $basketno,
178 biblionumber => $biblionumber1,
179 budget_id => $budget->{budget_id},
180 uncertainprice => 0,
181 order_internalnote => "internal note",
182 order_vendornote => "vendor note",
183 ordernumber => '',
185 num => {
186 quantity => 24,
187 listprice => 50.121111,
188 ecost => 38.15,
189 rrp => 40.15,
190 discount => 5.1111,
194 str => {
195 basketno => $basketno,
196 biblionumber => $biblionumber2,
197 budget_id => $budget->{budget_id}
199 num => { quantity => 42 }
202 str => {
203 basketno => $basketno,
204 biblionumber => $biblionumber2,
205 budget_id => $budget->{budget_id},
206 uncertainprice => 0,
207 order_internalnote => "internal note",
208 order_vendornote => "vendor note"
210 num => {
211 quantity => 4,
212 ecost => 42.1,
213 rrp => 42.1,
214 listprice => 10.1,
215 ecost => 38.1,
216 rrp => 11.0,
217 discount => 5.1,
221 str => {
222 basketno => $basketno,
223 biblionumber => $biblionumber3,
224 budget_id => $budget->{budget_id},
225 order_internalnote => "internal note",
226 order_vendornote => "vendor note"
228 num => {
229 quantity => 4,
230 ecost => 40,
231 rrp => 42,
232 listprice => 10,
233 ecost => 38.15,
234 rrp => 11.00,
235 discount => 0,
236 uncertainprice => 0,
240 str => {
241 basketno => $basketno,
242 biblionumber => $biblionumber4,
243 budget_id => $budget->{budget_id},
244 order_internalnote => "internal note",
245 order_vendornote => "vendor note"
247 num => {
248 quantity => 1,
249 ecost => 10,
250 rrp => 10,
251 listprice => 10,
252 ecost => 10,
253 rrp => 10,
254 discount => 0,
255 uncertainprice => 0,
259 str => {
260 basketno => $basketno,
261 biblionumber => $biblionumber5,
262 budget_id => $budget->{budget_id},
263 order_internalnote => "internal note",
264 order_vendornote => "vendor note"
266 num => {
267 quantity => 1,
268 ecost => 10,
269 rrp => 10,
270 listprice => 10,
271 ecost => 10,
272 rrp => 10,
273 discount => 0,
274 uncertainprice => 0,
279 # Create 5 orders in database
280 for ( 0 .. 5 ) {
281 my %ocontent;
282 @ocontent{ keys %{ $order_content[$_]->{num} } } =
283 values %{ $order_content[$_]->{num} };
284 @ocontent{ keys %{ $order_content[$_]->{str} } } =
285 values %{ $order_content[$_]->{str} };
286 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->store->ordernumber;
287 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
290 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
292 my $invoiceid = AddInvoice(
293 invoicenumber => 'invoice',
294 booksellerid => $booksellerid,
295 unknown => "unknown"
298 my $invoice = GetInvoice( $invoiceid );
300 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
302 biblionumber => $biblionumber4,
303 order => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
304 quantityreceived => 1,
305 invoice => $invoice,
306 budget_id => $order_content[4]->{str}->{budget_id},
310 my $search_orders = SearchOrders({
311 booksellerid => $booksellerid,
312 basketno => $basketno
314 isa_ok( $search_orders, 'ARRAY' );
317 ( scalar @$search_orders == 5 )
318 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
320 "SearchOrders only gets non-cancelled orders"
323 $search_orders = SearchOrders({
324 booksellerid => $booksellerid,
325 basketno => $basketno,
326 pending => 1
330 ( scalar @$search_orders == 4 ) and !grep ( (
331 ( $_->{ordernumber} eq $ordernumbers[3] )
332 or ( $_->{ordernumber} eq $ordernumbers[4] )
334 @$search_orders )
336 "SearchOrders with pending params gets only pending orders (bug 10723)"
339 $search_orders = SearchOrders({
340 booksellerid => $booksellerid,
341 basketno => $basketno,
342 pending => 1,
343 ordered => 1,
345 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
347 $search_orders = SearchOrders({
348 ordernumber => $ordernumbers[4]
350 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
352 $search_orders = SearchOrders({
353 biblionumber => $biblionumber4
355 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
357 $search_orders = SearchOrders({
358 biblionumber => $biblionumber4,
359 pending => 1
361 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
364 # Test GetBudgetByOrderNumber
366 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
367 "GetBudgetByOrderNumber returns expected budget" );
369 my @lateorders = GetLateOrders(0);
370 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
371 0, "GetLateOrders does not get orders from opened baskets" );
372 C4::Acquisition::CloseBasket($basketno);
373 @lateorders = GetLateOrders(0);
374 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
375 0, "GetLateOrders gets orders from closed baskets" );
376 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
377 "GetLateOrders does not get cancelled orders" );
378 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
379 "GetLateOrders does not get received orders" );
381 $search_orders = SearchOrders({
382 booksellerid => $booksellerid,
383 basketno => $basketno,
384 pending => 1,
385 ordered => 1,
387 is( scalar (@$search_orders), 4, "SearchOrders with pending and ordered params gets only pending ordered orders. After closing the basket, orders are marked as 'ordered' (bug 11170)" );
390 # Test AddClaim
393 my $order = $lateorders[0];
394 AddClaim( $order->{ordernumber} );
395 my $neworder = GetOrder( $order->{ordernumber} );
397 $neworder->{claimed_date},
398 strftime( "%Y-%m-%d", localtime(time) ),
399 "AddClaim : Check claimed_date"
402 my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed;
403 $order2->{order_internalnote} = "my notes";
404 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
406 biblionumber => $biblionumber2,
407 order => $order2,
408 quantityreceived => 2,
409 invoice => $invoice,
413 $order2 = GetOrder( $ordernumbers[1] );
414 is( $order2->{'quantityreceived'},
415 0, 'Splitting up order did not receive any on original order' );
416 is( $order2->{'quantity'}, 40, '40 items on original order' );
417 is( $order2->{'budget_id'}, $budgetid,
418 'Budget on original order is unchanged' );
419 is( $order2->{order_internalnote}, "my notes",
420 'ModReceiveOrder and GetOrder deal with internal notes' );
422 $neworder = GetOrder($new_ordernumber);
423 is( $neworder->{'quantity'}, 2, '2 items on new order' );
424 is( $neworder->{'quantityreceived'},
425 2, 'Splitting up order received items on new order' );
426 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
428 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
429 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
431 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
432 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
433 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
434 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
436 my $budgetid2 = C4::Budgets::AddBudget(
438 budget_code => "budget_code_test_modrecv",
439 budget_name => "budget_name_test_modrecv",
443 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
444 $order3->{order_internalnote} = "my other notes";
445 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
447 biblionumber => $biblionumber2,
448 order => $order3,
449 quantityreceived => 2,
450 invoice => $invoice,
451 budget_id => $budgetid2,
455 $order3 = GetOrder( $ordernumbers[2] );
456 is( $order3->{'quantityreceived'},
457 0, 'Splitting up order did not receive any on original order' );
458 is( $order3->{'quantity'}, 2, '2 items on original order' );
459 is( $order3->{'budget_id'}, $budgetid,
460 'Budget on original order is unchanged' );
461 is( $order3->{order_internalnote}, "my other notes",
462 'ModReceiveOrder and GetOrder deal with notes' );
464 $neworder = GetOrder($new_ordernumber);
465 is( $neworder->{'quantity'}, 2, '2 items on new order' );
466 is( $neworder->{'quantityreceived'},
467 2, 'Splitting up order received items on new order' );
468 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
470 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
471 $order3->{order_internalnote} = "my third notes";
472 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
474 biblionumber => $biblionumber2,
475 order => $order3,
476 quantityreceived => 2,
477 invoice => $invoice,
478 budget_id => $budgetid2,
482 $order3 = GetOrder( $ordernumbers[2] );
483 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
484 is( $order3->{'quantity'}, 2, '2 items on order' );
485 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
486 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
488 my $nonexistent_order = GetOrder();
489 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
490 $nonexistent_order = GetOrder( 424242424242 );
491 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
493 # Tests for DelOrder
494 my $order1 = GetOrder($ordernumbers[0]);
495 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
496 ok((not defined $error), "DelOrder does not fail");
497 $order1 = GetOrder($order1->{ordernumber});
498 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
499 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
500 ok((defined Koha::Biblios->find( $order1->{biblionumber} )), "biblio still exists");
502 $order2 = GetOrder($ordernumbers[1]);
503 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
504 ok((not defined $error), "DelOrder does not fail");
505 $order2 = GetOrder($order2->{ordernumber});
506 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
507 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
508 ok((not defined Koha::Biblios->find( $order2->{biblionumber} )), "biblio does not exist anymore");
510 my $order4 = GetOrder($ordernumbers[3]);
511 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
512 ok((not defined $error), "DelOrder does not fail");
513 $order4 = GetOrder($order4->{ordernumber});
514 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
515 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
516 ok((not defined Koha::Biblios->find( $order4->{biblionumber} )), "biblio does not exist anymore");
518 my $order5 = GetOrder($ordernumbers[4]);
519 C4::Items::AddItem( { barcode => '0102030405' }, $order5->{biblionumber} );
520 $error = DelOrder($order5->{biblionumber}, $order5->{ordernumber}, 1);
521 $order5 = GetOrder($order5->{ordernumber});
522 ok((defined $order5->{datecancellationprinted}), "order is cancelled");
523 ok((defined Koha::Biblios->find( $order5->{biblionumber} )), "biblio still exists");
525 # End of tests for DelOrder
527 subtest 'ModOrder' => sub {
528 plan tests => 1;
529 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
530 my $order = GetOrder( $order1->{ordernumber} );
531 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
534 # Budget reports
535 my $all_count = scalar GetBudgetsReport();
536 ok($all_count >= 1, "GetBudgetReport OK");
538 my $active_count = scalar GetBudgetsReport(1);
539 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
541 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
542 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
544 $schema->storage->txn_rollback();