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>.
20 use POSIX qw(strftime);
22 use Test::More tests => 70;
25 use Koha::DateUtils qw(dt_from_string output_pref);
26 use Koha::Acquisition::Basket;
28 use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' );
31 use_ok('C4::Acquisition', qw( NewBasket GetBasket AddInvoice GetInvoice ModReceiveOrder SearchOrders GetOrder GetHistory ModOrder get_rounding_sql get_rounded_price ReopenBasket ModBasket ModBasketHeader ModBasketUsers ));
32 use_ok('C4::Biblio', qw( AddBiblio GetMarcSubfieldStructure ));
33 use_ok('C4::Budgets', qw( AddBudgetPeriod AddBudget GetBudget GetBudgetByOrderNumber GetBudgetsReport GetBudgets GetBudgetReport ));
34 use_ok('Koha::Acquisition::Orders');
35 use_ok('Koha::Acquisition::Booksellers');
36 use_ok('t::lib::TestBuilder');
39 # Sub used for testing C4::Acquisition subs returning order(s):
40 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
41 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
42 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
44 # $exp_fields : arrayref whose elements are the keys we expect to find
45 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
46 # containing content fields of the order created with Koha::Acquisition::Order
47 # $order_to_check : hashref whose keys/values are the content of an order
48 # returned by the C4::Acquisition sub we are testing
50 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
51 # fields missing in $order_to_check
52 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
53 # fields unexpected in $order_to_check
54 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
55 # fields which value is not the same in between $order_to_check and
56 # $test_nbr_fields : contains the number of fields of $order_to_check
58 sub _check_fields_of_order {
59 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
60 my @test_missing_fields = ();
61 my @test_extra_fields = ();
62 my @test_different_fields = ();
63 my $test_nbr_fields = scalar( keys %$order_to_check );
64 foreach my $field (@$exp_fields) {
65 push @test_missing_fields, $field
66 unless exists( $order_to_check->{$field} );
68 foreach my $field ( keys %$order_to_check ) {
69 push @test_extra_fields, $field
70 unless grep ( /^$field$/, @$exp_fields );
72 foreach my $field ( keys %{ $original_order_content->{str} } ) {
73 push @test_different_fields, $field
74 unless ( !exists $order_to_check->{$field} )
75 or ( $original_order_content->{str}->{$field} eq
76 $order_to_check->{$field} );
78 foreach my $field ( keys %{ $original_order_content->{num} } ) {
79 push @test_different_fields, $field
80 unless ( !exists $order_to_check->{$field} )
81 or ( $original_order_content->{num}->{$field} ==
82 $order_to_check->{$field} );
85 \@test_missing_fields, \@test_extra_fields,
86 \@test_different_fields, $test_nbr_fields
90 # Sub used for testing C4::Acquisition subs returning several orders
91 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
92 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
93 sub _check_fields_of_orders {
94 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
95 my @test_missing_fields = ();
96 my @test_extra_fields = ();
97 my @test_different_fields = ();
98 my @test_nbr_fields = ();
99 foreach my $order_to_check (@$orders_to_check) {
100 my $original_order_content =
101 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
102 @$original_orders_content )[0];
104 $t_missing_fields, $t_extra_fields,
105 $t_different_fields, $t_nbr_fields
107 = _check_fields_of_order( $exp_fields, $original_order_content,
109 push @test_missing_fields, @$t_missing_fields;
110 push @test_extra_fields, @$t_extra_fields;
111 push @test_different_fields, @$t_different_fields;
112 push @test_nbr_fields, $t_nbr_fields;
114 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
115 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
116 @test_different_fields =
117 keys %{ { map { $_ => 1 } @test_different_fields } };
119 \@test_missing_fields, \@test_extra_fields,
120 \@test_different_fields, \@test_nbr_fields
125 my $schema = Koha::Database->new()->schema();
126 $schema->storage->txn_begin();
128 # Creating some orders
129 my $bookseller = Koha::Acquisition::Bookseller->new(
132 address1 => "bookseller's address",
138 my $booksellerid = $bookseller->id;
140 my $booksellerinfo = Koha::Acquisition::Booksellers->find( $booksellerid );
141 is( $booksellerinfo->deliverytime,
142 5, 'set deliverytime when creating vendor (Bug 10556)' );
144 my ( $basket, $basketno );
146 $basketno = NewBasket( $booksellerid, 1 ),
147 "NewBasket( $booksellerid , 1 ) returns $basketno"
149 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
151 my $bpid=AddBudgetPeriod({
152 budget_period_startdate => '2008-01-01'
153 , budget_period_enddate => '2008-12-31'
154 , budget_period_active => 1
155 , budget_period_description => "MAPERI"
158 my $budgetid = C4::Budgets::AddBudget(
160 budget_code => "budget_code_test_1",
161 budget_name => "budget_name_test_1",
162 budget_period_id => $bpid,
165 my $budget = C4::Budgets::GetBudget($budgetid);
168 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
169 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
170 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
171 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
172 my ( $biblionumber5, $biblioitemnumber5 ) = AddBiblio( MARC::Record->new, '' );
176 # Prepare 6 orders, and make distinction beween fields to be tested with eq and with ==
177 # Ex : a price of 50.1 will be stored internally as 5.100000
179 my @order_content = (
182 basketno => $basketno,
183 biblionumber => $biblionumber1,
184 budget_id => $budget->{budget_id},
186 order_internalnote => "internal note foo",
187 order_vendornote => "vendor note bar",
192 listprice => 50.121111,
200 basketno => $basketno,
201 biblionumber => $biblionumber2,
202 budget_id => $budget->{budget_id}
204 num => { quantity => 42 }
208 basketno => $basketno,
209 biblionumber => $biblionumber2,
210 budget_id => $budget->{budget_id},
212 order_internalnote => "internal note foo",
213 order_vendornote => "vendor note bar"
227 basketno => $basketno,
228 biblionumber => $biblionumber3,
229 budget_id => $budget->{budget_id},
230 order_internalnote => "internal note",
231 order_vendornote => "vendor note"
246 basketno => $basketno,
247 biblionumber => $biblionumber4,
248 budget_id => $budget->{budget_id},
249 order_internalnote => "internal note bar",
250 order_vendornote => "vendor note foo"
265 basketno => $basketno,
266 biblionumber => $biblionumber5,
267 budget_id => $budget->{budget_id},
268 order_internalnote => "internal note",
269 order_vendornote => "vendor note"
284 # Create 6 orders in database
287 @ocontent{ keys %{ $order_content[$_]->{num} } } =
288 values %{ $order_content[$_]->{num} };
289 @ocontent{ keys %{ $order_content[$_]->{str} } } =
290 values %{ $order_content[$_]->{str} };
291 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->store->ordernumber;
292 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
295 Koha::Acquisition::Orders->find($ordernumbers[3])->cancel;
297 my $invoiceid = AddInvoice(
298 invoicenumber => 'invoice',
299 booksellerid => $booksellerid,
303 my $invoice = GetInvoice( $invoiceid );
305 my $reception_date = output_pref(
307 dt => dt_from_string->add( days => 1 ),
312 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
314 biblionumber => $biblionumber4,
315 order => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
316 quantityreceived => 1,
318 budget_id => $order_content[4]->{str}->{budget_id},
319 datereceived => $reception_date,
326 dt => dt_from_string($datereceived),
332 'ModReceiveOrder sets the passed date'
335 my $search_orders = SearchOrders({
336 booksellerid => $booksellerid,
337 basketno => $basketno
339 isa_ok( $search_orders, 'ARRAY' );
342 ( scalar @$search_orders == 5 )
343 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
345 "SearchOrders only gets non-cancelled orders"
348 $search_orders = SearchOrders({
349 booksellerid => $booksellerid,
350 basketno => $basketno,
355 ( scalar @$search_orders == 4 ) and !grep ( (
356 ( $_->{ordernumber} eq $ordernumbers[3] )
357 or ( $_->{ordernumber} eq $ordernumbers[4] )
361 "SearchOrders with pending params gets only pending orders (bug 10723)"
364 $search_orders = SearchOrders({
365 booksellerid => $booksellerid,
366 basketno => $basketno,
370 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
372 $search_orders = SearchOrders({
373 ordernumber => $ordernumbers[4]
375 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
377 $search_orders = SearchOrders({
378 biblionumber => $biblionumber4
380 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
382 $search_orders = SearchOrders({
383 biblionumber => $biblionumber4,
386 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
389 # Test GetBudgetByOrderNumber
391 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
392 "GetBudgetByOrderNumber returns expected budget" );
394 my $lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 });
395 is( $lateorders->search({ 'me.basketno' => $basketno })->count,
396 0, "GetLateOrders does not get orders from opened baskets" );
397 Koha::Acquisition::Baskets->find($basketno)->close;
398 $lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 });
399 isnt( $lateorders->search({ 'me.basketno' => $basketno })->count,
400 0, "GetLateOrders gets orders from closed baskets" );
401 is( $lateorders->search({ ordernumber => $ordernumbers[3] })->count, 0,
402 "GetLateOrders does not get cancelled orders" );
403 is( $lateorders->search({ ordernumber => $ordernumbers[4] })->count, 0,
404 "GetLateOrders does not get received orders" );
406 $search_orders = SearchOrders({
407 booksellerid => $booksellerid,
408 basketno => $basketno,
412 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)" );
418 my $order = $lateorders->next;
421 output_pref({ str => $order->claimed_date, dateformat => 'iso', dateonly => 1 }),
422 strftime( "%Y-%m-%d", localtime(time) ),
423 "Koha::Acquisition::Order->claim: Check claimed_date"
426 my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed;
427 $order2->{order_internalnote} = "my notes";
428 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
430 biblionumber => $biblionumber2,
432 quantityreceived => 2,
436 $order2 = GetOrder( $ordernumbers[1] );
437 is( $order2->{'quantityreceived'},
438 0, 'Splitting up order did not receive any on original order' );
439 is( $order2->{'quantity'}, 40, '40 items on original order' );
440 is( $order2->{'budget_id'}, $budgetid,
441 'Budget on original order is unchanged' );
442 is( $order2->{order_internalnote}, "my notes",
443 'ModReceiveOrder and GetOrder deal with internal notes' );
444 my $order1 = GetOrder( $ordernumbers[0] );
446 $order1->{order_internalnote},
448 "ModReceiveOrder only changes the supplied orders internal notes"
451 my $neworder = GetOrder($new_ordernumber);
452 is( $neworder->{'quantity'}, 2, '2 items on new order' );
453 is( $neworder->{'quantityreceived'},
454 2, 'Splitting up order received items on new order' );
455 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
457 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
458 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
460 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
461 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
462 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
463 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
464 $orders = GetHistory( ordernumbers => [$ordernumbers[1]] );
465 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumbers returns 1 order' );
466 $orders = GetHistory( ordernumbers => \@ordernumbers );
467 is( scalar( @$orders ), scalar( @ordernumbers ) - 1, 'GetHistory with a list of ordernumbers returns N-1 orders (was has been deleted [3])' );
469 $orders = GetHistory( internalnote => 'internal note foo' );
470 is( scalar( @$orders ), 2, 'GetHistory returns correctly a search for internalnote' );
471 $orders = GetHistory( vendornote => 'vendor note bar' );
472 is( scalar( @$orders ), 2, 'GetHistory returns correctly a search for vendornote' );
473 $orders = GetHistory( internalnote => 'internal note bar' );
474 is( scalar( @$orders ), 1, 'GetHistory returns correctly a search for internalnote' );
475 $orders = GetHistory( vendornote => 'vendor note foo' );
476 is( scalar( @$orders ), 1, 'GetHistory returns correctly a search for vendornote' );
479 # Test GetHistory() with and without SearchWithISBNVariations
480 # The ISBN passed as a param is the ISBN-10 version of the 13-digit ISBN in the sample record declared in $marcxml
482 my $budgetid2 = C4::Budgets::AddBudget(
484 budget_code => "budget_code_test_modrecv",
485 budget_name => "budget_name_test_modrecv",
489 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
490 $order3->{order_internalnote} = "my other notes";
491 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
493 biblionumber => $biblionumber2,
495 quantityreceived => 2,
497 budget_id => $budgetid2,
501 $order3 = GetOrder( $ordernumbers[2] );
502 is( $order3->{'quantityreceived'},
503 0, 'Splitting up order did not receive any on original order' );
504 is( $order3->{'quantity'}, 2, '2 items on original order' );
505 is( $order3->{'budget_id'}, $budgetid,
506 'Budget on original order is unchanged' );
507 is( $order3->{order_internalnote}, "my other notes",
508 'ModReceiveOrder and GetOrder deal with notes' );
510 $neworder = GetOrder($new_ordernumber);
511 is( $neworder->{'quantity'}, 2, '2 items on new order' );
512 is( $neworder->{'quantityreceived'},
513 2, 'Splitting up order received items on new order' );
514 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
516 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
517 $order3->{order_internalnote} = "my third notes";
518 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
520 biblionumber => $biblionumber2,
522 quantityreceived => 2,
524 budget_id => $budgetid2,
528 $order3 = GetOrder( $ordernumbers[2] );
529 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
530 is( $order3->{'quantity'}, 2, '2 items on order' );
531 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
532 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
534 my $nonexistent_order = GetOrder();
535 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
536 $nonexistent_order = GetOrder( 424242424242 );
537 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
539 subtest 'ModOrder' => sub {
541 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
542 my $order = GetOrder( $order1->{ordernumber} );
543 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
547 my $all_count = scalar GetBudgetsReport();
548 ok($all_count >= 1, "GetBudgetReport OK");
550 my $active_count = scalar GetBudgetsReport(1);
551 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
553 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
554 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
556 # "Flavoured" tests (tests that required a run for each marc flavour)
557 # Tests should be added to the run_flavoured_tests sub below
558 my $biblio_module = Test::MockModule->new('C4::Biblio');
559 $biblio_module->mock(
560 'GetMarcSubfieldStructure',
564 my ( $title_field, $title_subfield ) = get_title_field();
565 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
566 my ( $issn_field, $issn_subfield ) = get_issn_field();
567 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
568 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
569 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
572 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
573 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
574 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
575 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
576 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
577 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
582 sub run_flavoured_tests {
583 my $marcflavour = shift;
584 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
587 # Test SearchWithISBNVariations syspref
589 my $marc_record = MARC::Record->new;
590 $marc_record->append_fields( create_isbn_field( '9780136019701', $marcflavour ) );
591 my ( $biblionumber6, $biblioitemnumber6 ) = AddBiblio( $marc_record, '' );
594 my $ordernumber = Koha::Acquisition::Order->new( {
595 basketno => $basketno,
596 biblionumber => $biblionumber6,
597 budget_id => $budget->{budget_id},
598 order_internalnote => "internal note",
599 order_vendornote => "vendor note",
608 } )->store->ordernumber;
610 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
611 $orders = GetHistory( isbn => '0136019706' );
612 is( scalar(@$orders), 0, "GetHistory searches correctly by ISBN" );
614 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
615 $orders = GetHistory( isbn => '0136019706' );
616 is( scalar(@$orders), 1, "GetHistory searches correctly by ISBN" );
618 Koha::Acquisition::Orders->find($ordernumber)->cancel;
621 # Do "flavoured" tests
622 subtest 'MARC21' => sub {
624 run_flavoured_tests('MARC21');
627 subtest 'UNIMARC' => sub {
629 run_flavoured_tests('UNIMARC');
632 ### Functions required for "flavoured" tests
633 sub get_title_field {
634 my $marc_flavour = C4::Context->preference('marcflavour');
635 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
639 my $marc_flavour = C4::Context->preference('marcflavour');
640 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
644 my $marc_flavour = C4::Context->preference('marcflavour');
645 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
648 sub get_itemnumber_field {
649 my $marc_flavour = C4::Context->preference('marcflavour');
650 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
653 sub create_isbn_field {
654 my ( $isbn, $marcflavour ) = @_;
656 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
657 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
659 # Add the price subfield
660 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
661 $field->add_subfields( $price_subfield => '$100' );
666 subtest 'ModReceiveOrder replacementprice tests' => sub {
668 #Let's build an order, we need a couple things though
669 my $builder = t::lib::TestBuilder->new;
670 my $order_biblio = $builder->build_sample_biblio;
671 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
672 my $order_invoice = $builder->build({ source => 'Aqinvoice'});
673 my $order_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' } });
674 my $order_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $order_currency->{currency}, invoiceprice => $order_currency->{currency} } });
676 basketno => $order_basket->{basketno},
677 booksellerid => $order_vendor->{id},
679 replacementprice => undef,
681 quantityreceived => 0,
682 datereceived => undef,
683 datecancellationprinted => undef,
685 my $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
686 (undef, my $received_ordernumber) = ModReceiveOrder({
687 biblionumber => $order_biblio->biblionumber,
688 order => $receive_order,
689 invoice => $order_invoice,
690 quantityreceived => $receive_order->{quantity},
691 budget_id => $order->{budget_id},
693 my $received_order = GetOrder($received_ordernumber);
694 is ($received_order->{replacementprice},undef,"No price set if none passed in");
695 $orderinfo->{replacementprice} = 16.12;
696 $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
697 (undef, $received_ordernumber) = ModReceiveOrder({
698 biblionumber => $order_biblio->biblionumber,
699 order => $receive_order,
700 invoice => $order_invoice,
701 quantityreceived => $receive_order->{quantity},
702 budget_id => $order->{budget_id},
704 $received_order = GetOrder($received_ordernumber);
705 is ($received_order->{replacementprice},'16.120000',"Replacement price set if none passed in");
708 subtest 'ModReceiveOrder and subscription' => sub {
711 my $builder = t::lib::TestBuilder->new;
712 my $first_note = 'first note';
713 my $second_note = 'second note';
714 my $subscription = $builder->build_object( { class => 'Koha::Subscriptions' } );
715 my $order = $builder->build_object(
717 class => 'Koha::Acquisition::Orders',
719 subscriptionid => $subscription->subscriptionid,
720 order_internalnote => $first_note,
722 quantityreceived => 0,
723 ecost_tax_excluded => 42,
724 unitprice_tax_excluded => 42,
728 my $order_info = $order->unblessed;
729 # We do not want the note from the original note to be modified
730 # Keeping it will permit to display it for future receptions
731 $order_info->{order_internalnote} = $second_note;
732 my ( undef, $received_ordernumber ) = ModReceiveOrder(
734 biblionumber => $order->biblionumber,
735 order => $order_info,
736 invoice => $order->{invoiceid},
737 quantityreceived => 1,
738 budget_id => $order->budget_id,
741 my $received_order = Koha::Acquisition::Orders->find($received_ordernumber);
742 is( $received_order->order_internalnote,
743 $second_note, "No price set if none passed in" );
745 $order->get_from_storage;
746 is( $order->get_from_storage->order_internalnote, $first_note );
749 subtest 'GetHistory with additional fields' => sub {
751 my $builder = t::lib::TestBuilder->new;
752 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
754 basketno => $order_basket->{basketno},
756 replacementprice => undef,
758 quantityreceived => 0,
759 datereceived => undef,
760 datecancellationprinted => undef,
762 my $order = $builder->build({ source => 'Aqorder', value => $orderinfo });
763 my $history = GetHistory(ordernumber => $order->{ordernumber});
764 is( scalar( @$history ), 1, 'GetHistory returns the one order');
766 my $additional_field = $builder->build({source => 'AdditionalField', value => {
767 tablename => 'aqbasket',
769 authorised_value_category => "",
772 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
773 is( scalar ( @$history ), 0, 'GetHistory returns no order for an unused additional field');
774 my $basket = Koha::Acquisition::Baskets->find({ basketno => $order_basket->{basketno} });
775 $basket->set_additional_fields([{
776 id => $additional_field->{id},
777 value => 'delicious',
780 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
781 is( scalar( @$history ), 1, 'GetHistory returns the order when additional field is set');
784 subtest 'Tests for get_rounding_sql' => sub {
788 my $value = '3.141592';
790 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
791 my $no_rounding_result = C4::Acquisition::get_rounding_sql($value);
792 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
793 my $rounding_result = C4::Acquisition::get_rounding_sql($value);
795 ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
796 ok( $rounding_result =~ /CAST/, "Value ($value) will be rounded" );
800 subtest 'Test for get_rounded_price' => sub {
804 my $exact_price = 3.14;
805 my $up_price = 3.145592;
806 my $down_price = 3.141592;
807 my $round_up_price = sprintf( '%0.2f', $up_price );
808 my $round_down_price = sprintf( '%0.2f', $down_price );
810 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
811 my $not_rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
812 my $not_rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
813 my $not_rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
814 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
815 my $rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
816 my $rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
817 my $rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
819 is( $not_rounded_result1, $exact_price, "Price ($exact_price) was correctly not rounded ($not_rounded_result1)" );
820 is( $not_rounded_result2, $up_price, "Price ($up_price) was correctly not rounded ($not_rounded_result2)" );
821 is( $not_rounded_result3, $down_price, "Price ($down_price) was correctly not rounded ($not_rounded_result3)" );
822 is( $rounded_result1, $exact_price, "Price ($exact_price) was correctly rounded ($rounded_result1)" );
823 is( $rounded_result2, $round_up_price, "Price ($up_price) was correctly rounded ($rounded_result2)" );
824 is( $rounded_result3, $round_down_price, "Price ($down_price) was correctly rounded ($rounded_result3)" );
828 subtest 'GetHistory - managing library' => sub {
832 my $orders = GetHistory(managing_library => 'CPL');
834 my $builder = t::lib::TestBuilder->new;
836 my $order_basket1 = $builder->build({ source => 'Aqbasket', value => { branch => 'CPL' } });
838 basketno => $order_basket1->{basketno},
840 replacementprice => undef,
842 quantityreceived => 0,
843 datereceived => undef,
844 datecancellationprinted => undef,
846 my $order1 = $builder->build({ source => 'Aqorder', value => $orderinfo1 });
848 my $order_basket2 = $builder->build({ source => 'Aqbasket', value => { branch => 'LIB' } });
850 basketno => $order_basket2->{basketno},
852 replacementprice => undef,
854 quantityreceived => 0,
855 datereceived => undef,
856 datecancellationprinted => undef,
858 my $order2 = $builder->build({ source => 'Aqorder', value => $orderinfo2 });
860 my $history = GetHistory(managing_library => 'CPL');
861 is( scalar( @$history), scalar ( @$orders ) +1, "GetHistory returns number of orders");
865 subtest 'GetHistory - is_standing' => sub {
869 my $orders = GetHistory( is_standing => '1' );
871 my $builder = t::lib::TestBuilder->new;
873 my $order_basket1 = $builder->build( { source => 'Aqbasket', value => { is_standing => 0 } } );
875 basketno => $order_basket1->{basketno},
877 replacementprice => undef,
879 quantityreceived => 0,
880 datereceived => undef,
881 datecancellationprinted => undef,
883 my $order1 = $builder->build( { source => 'Aqorder', value => $orderinfo1 } );
885 my $order_basket2 = $builder->build( { source => 'Aqbasket', value => { is_standing => 1 } } );
887 basketno => $order_basket2->{basketno},
889 replacementprice => undef,
891 quantityreceived => 0,
892 datereceived => undef,
893 datecancellationprinted => undef,
895 my $order2 = $builder->build( { source => 'Aqorder', value => $orderinfo2 } );
897 my $history = GetHistory( is_standing => 1 );
900 scalar(@$orders) + 1,
901 "GetHistory returns number of standing orders"
906 subtest 'Acquisition logging' => sub {
910 t::lib::Mocks::mock_preference('AcquisitionLog', 1);
912 Koha::ActionLogs->delete;
913 my $basketno = NewBasket( $booksellerid, 1 );
914 my @create_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'ADD_BASKET', object => $basketno })->as_list;
915 is (scalar @create_logs, 1, 'Basket creation is logged');
917 Koha::ActionLogs->delete;
918 C4::Acquisition::ReopenBasket($basketno);
919 my @reopen_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'REOPEN_BASKET', object => $basketno })->as_list;
920 is (scalar @reopen_logs, 1, 'Basket reopen is logged');
922 Koha::ActionLogs->delete;
923 C4::Acquisition::ModBasket({
924 basketno => $basketno,
925 booksellerid => $booksellerid
927 my @mod_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET', object => $basketno })->as_list;
928 is (scalar @mod_logs, 1, 'Basket modify is logged');
930 Koha::ActionLogs->delete;
931 C4::Acquisition::ModBasketHeader($basketno,"Test","","","",$booksellerid);
932 my @mod_header_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET_HEADER', object => $basketno })->as_list;
933 is (scalar @mod_header_logs, 1, 'Basket header modify is logged');
935 Koha::ActionLogs->delete;
936 C4::Acquisition::ModBasketUsers($basketno,(1));
937 my @mod_users_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET_USERS', object => $basketno })->as_list;
938 is (scalar @mod_users_logs, 1, 'Basket users modify is logged');
940 t::lib::Mocks::mock_preference('AcquisitionLog', 0);
943 $schema->storage->txn_rollback();