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 => 73;
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 GetInvoices 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 my $builder = t::lib::TestBuilder->new;
130 # Creating some orders
131 my $bookseller = Koha::Acquisition::Bookseller->new(
134 address1 => "bookseller's address",
140 my $booksellerid = $bookseller->id;
142 my $booksellerinfo = Koha::Acquisition::Booksellers->find( $booksellerid );
143 is( $booksellerinfo->deliverytime,
144 5, 'set deliverytime when creating vendor (Bug 10556)' );
146 my ( $basket, $basketno );
148 $basketno = NewBasket( $booksellerid, 1 ),
149 "NewBasket( $booksellerid , 1 ) returns $basketno"
151 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
153 my $bpid=AddBudgetPeriod({
154 budget_period_startdate => '2008-01-01'
155 , budget_period_enddate => '2008-12-31'
156 , budget_period_active => 1
157 , budget_period_description => "MAPERI"
160 my $budgetid = C4::Budgets::AddBudget(
162 budget_code => "budget_code_test_1",
163 budget_name => "budget_name_test_1",
164 budget_period_id => $bpid,
167 my $budget = C4::Budgets::GetBudget($budgetid);
170 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
171 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
172 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
173 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
174 my ( $biblionumber5, $biblioitemnumber5 ) = AddBiblio( MARC::Record->new, '' );
178 # Prepare 6 orders, and make distinction beween fields to be tested with eq and with ==
179 # Ex : a price of 50.1 will be stored internally as 5.100000
181 my @order_content = (
184 basketno => $basketno,
185 biblionumber => $biblionumber1,
186 budget_id => $budget->{budget_id},
188 order_internalnote => "internal note foo",
189 order_vendornote => "vendor note bar",
194 listprice => 50.121111,
202 basketno => $basketno,
203 biblionumber => $biblionumber2,
204 budget_id => $budget->{budget_id}
206 num => { quantity => 42 }
210 basketno => $basketno,
211 biblionumber => $biblionumber2,
212 budget_id => $budget->{budget_id},
214 order_internalnote => "internal note foo",
215 order_vendornote => "vendor note bar"
229 basketno => $basketno,
230 biblionumber => $biblionumber3,
231 budget_id => $budget->{budget_id},
232 order_internalnote => "internal note",
233 order_vendornote => "vendor note"
248 basketno => $basketno,
249 biblionumber => $biblionumber4,
250 budget_id => $budget->{budget_id},
251 order_internalnote => "internal note bar",
252 order_vendornote => "vendor note foo"
267 basketno => $basketno,
268 biblionumber => $biblionumber5,
269 budget_id => $budget->{budget_id},
270 order_internalnote => "internal note",
271 order_vendornote => "vendor note"
286 # Create 6 orders in database
289 @ocontent{ keys %{ $order_content[$_]->{num} } } =
290 values %{ $order_content[$_]->{num} };
291 @ocontent{ keys %{ $order_content[$_]->{str} } } =
292 values %{ $order_content[$_]->{str} };
293 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->store->ordernumber;
294 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
297 Koha::Acquisition::Orders->find($ordernumbers[3])->cancel;
299 my $invoiceid = AddInvoice(
300 invoicenumber => 'invoice',
301 booksellerid => $booksellerid,
305 my $invoice = GetInvoice( $invoiceid );
307 my $reception_date = output_pref(
309 dt => dt_from_string->add( days => 1 ),
314 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
316 biblionumber => $biblionumber4,
317 order => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
318 quantityreceived => 1,
320 budget_id => $order_content[4]->{str}->{budget_id},
321 datereceived => $reception_date,
328 dt => dt_from_string($datereceived),
334 'ModReceiveOrder sets the passed date'
337 my $search_orders = SearchOrders({
338 booksellerid => $booksellerid,
339 basketno => $basketno
341 isa_ok( $search_orders, 'ARRAY' );
344 ( scalar @$search_orders == 5 )
345 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
347 "SearchOrders only gets non-cancelled orders"
350 $search_orders = SearchOrders({
351 booksellerid => $booksellerid,
352 basketno => $basketno,
357 ( scalar @$search_orders == 4 ) and !grep ( (
358 ( $_->{ordernumber} eq $ordernumbers[3] )
359 or ( $_->{ordernumber} eq $ordernumbers[4] )
363 "SearchOrders with pending params gets only pending orders (bug 10723)"
366 $search_orders = SearchOrders({
367 booksellerid => $booksellerid,
368 basketno => $basketno,
372 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
374 $search_orders = SearchOrders({
375 ordernumber => $ordernumbers[4]
377 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
379 $search_orders = SearchOrders({
380 biblionumber => $biblionumber4
382 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
384 $search_orders = SearchOrders({
385 biblionumber => $biblionumber4,
388 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
391 # Test GetBudgetByOrderNumber
393 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
394 "GetBudgetByOrderNumber returns expected budget" );
396 my $lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 });
397 is( $lateorders->search({ 'me.basketno' => $basketno })->count,
398 0, "GetLateOrders does not get orders from opened baskets" );
399 Koha::Acquisition::Baskets->find($basketno)->close;
400 $lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 });
401 isnt( $lateorders->search({ 'me.basketno' => $basketno })->count,
402 0, "GetLateOrders gets orders from closed baskets" );
403 is( $lateorders->search({ ordernumber => $ordernumbers[3] })->count, 0,
404 "GetLateOrders does not get cancelled orders" );
405 is( $lateorders->search({ ordernumber => $ordernumbers[4] })->count, 0,
406 "GetLateOrders does not get received orders" );
408 $search_orders = SearchOrders({
409 booksellerid => $booksellerid,
410 basketno => $basketno,
414 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)" );
420 my $order = $lateorders->next;
423 output_pref({ str => $order->claimed_date, dateformat => 'iso', dateonly => 1 }),
424 strftime( "%Y-%m-%d", localtime(time) ),
425 "Koha::Acquisition::Order->claim: Check claimed_date"
428 my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed;
429 $order2->{order_internalnote} = "my notes";
430 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
432 biblionumber => $biblionumber2,
434 quantityreceived => 2,
438 $order2 = GetOrder( $ordernumbers[1] );
439 is( $order2->{'quantityreceived'},
440 0, 'Splitting up order did not receive any on original order' );
441 is( $order2->{'quantity'}, 40, '40 items on original order' );
442 is( $order2->{'budget_id'}, $budgetid,
443 'Budget on original order is unchanged' );
444 is( $order2->{order_internalnote}, "my notes",
445 'ModReceiveOrder and GetOrder deal with internal notes' );
446 my $order1 = GetOrder( $ordernumbers[0] );
448 $order1->{order_internalnote},
450 "ModReceiveOrder only changes the supplied orders internal notes"
453 my $neworder = GetOrder($new_ordernumber);
454 is( $neworder->{'quantity'}, 2, '2 items on new order' );
455 is( $neworder->{'quantityreceived'},
456 2, 'Splitting up order received items on new order' );
457 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
459 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
460 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
462 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
463 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
464 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
465 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
466 $orders = GetHistory( ordernumbers => [$ordernumbers[1]] );
467 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumbers returns 1 order' );
468 $orders = GetHistory( ordernumbers => \@ordernumbers );
469 is( scalar( @$orders ), scalar( @ordernumbers ) - 1, 'GetHistory with a list of ordernumbers returns N-1 orders (was has been deleted [3])' );
471 $orders = GetHistory( internalnote => 'internal note foo' );
472 is( scalar( @$orders ), 2, 'GetHistory returns correctly a search for internalnote' );
473 $orders = GetHistory( vendornote => 'vendor note bar' );
474 is( scalar( @$orders ), 2, 'GetHistory returns correctly a search for vendornote' );
475 $orders = GetHistory( internalnote => 'internal note bar' );
476 is( scalar( @$orders ), 1, 'GetHistory returns correctly a search for internalnote' );
477 $orders = GetHistory( vendornote => 'vendor note foo' );
478 is( scalar( @$orders ), 1, 'GetHistory returns correctly a search for vendornote' );
480 my $budget2 = $builder->build_object(
482 class => 'Koha::Acquisition::Funds',
484 budget_code => "budget_code_test_modrecv",
485 budget_name => "budget_name_test_modrecv",
489 my $budgetid2 = $budget2->id;
491 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
492 $order3->{order_internalnote} = "my other notes";
493 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
495 biblionumber => $biblionumber2,
497 quantityreceived => 2,
499 budget_id => $budgetid2,
503 $order3 = GetOrder( $ordernumbers[2] );
504 is( $order3->{'quantityreceived'},
505 0, 'Splitting up order did not receive any on original order' );
506 is( $order3->{'quantity'}, 2, '2 items on original order' );
507 is( $order3->{'budget_id'}, $budgetid,
508 'Budget on original order is unchanged' );
509 is( $order3->{order_internalnote}, "my other notes",
510 'ModReceiveOrder and GetOrder deal with notes' );
512 $neworder = GetOrder($new_ordernumber);
513 is( $neworder->{'quantity'}, 2, '2 items on new order' );
514 is( $neworder->{'quantityreceived'},
515 2, 'Splitting up order received items on new order' );
516 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
518 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
519 $order3->{order_internalnote} = "my third notes";
520 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
522 biblionumber => $biblionumber2,
524 quantityreceived => 2,
526 budget_id => $budgetid2,
530 $order3 = GetOrder( $ordernumbers[2] );
531 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
532 is( $order3->{'quantity'}, 2, '2 items on order' );
533 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
534 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
536 my $nonexistent_order = GetOrder();
537 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
538 $nonexistent_order = GetOrder( 424242424242 );
539 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
541 subtest 'ModOrder' => sub {
543 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
544 my $order = GetOrder( $order1->{ordernumber} );
545 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
549 my $all_count = scalar GetBudgetsReport();
550 ok($all_count >= 1, "GetBudgetReport OK");
552 my $active_count = scalar GetBudgetsReport(1);
553 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
555 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
556 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
558 # "Flavoured" tests (tests that required a run for each marc flavour)
559 # Tests should be added to the run_flavoured_tests sub below
560 my $biblio_module = Test::MockModule->new('C4::Biblio');
561 $biblio_module->mock(
562 'GetMarcSubfieldStructure',
566 my ( $title_field, $title_subfield ) = get_title_field();
567 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
568 my ( $issn_field, $issn_subfield ) = get_issn_field();
569 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
570 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
571 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
574 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
575 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
576 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
577 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
578 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
579 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
584 sub run_flavoured_tests {
585 my $marcflavour = shift;
586 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
589 # Test SearchWithISBNVariations syspref
591 my $marc_record = MARC::Record->new;
592 $marc_record->append_fields( create_isbn_field( '9780136019701', $marcflavour ) );
593 my ( $biblionumber6, $biblioitemnumber6 ) = AddBiblio( $marc_record, '' );
596 my $ordernumber = Koha::Acquisition::Order->new( {
597 basketno => $basketno,
598 biblionumber => $biblionumber6,
599 budget_id => $budget->{budget_id},
600 order_internalnote => "internal note",
601 order_vendornote => "vendor note",
610 } )->store->ordernumber;
612 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
613 $orders = GetHistory( isbn => '0136019706' );
614 is( scalar(@$orders), 0, "GetHistory searches correctly by ISBN" );
616 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
617 $orders = GetHistory( isbn => '0136019706' );
618 is( scalar(@$orders), 1, "GetHistory searches correctly by ISBN" );
620 Koha::Acquisition::Orders->find($ordernumber)->cancel;
622 my $marc_record_issn = MARC::Record->new;
623 $marc_record_issn->append_fields( create_issn_field( '2434561X', $marcflavour ) );
624 my ( $biblionumber6_issn, undef ) = AddBiblio( $marc_record_issn, '' );
626 my $orders_issn = GetHistory( issn => '2434561X' );
627 is( scalar(@$orders_issn), 0, "Precheck that ISSN shouldn't be in database" );
630 my $ordernumber_issn = Koha::Acquisition::Order->new( {
631 basketno => $basketno,
632 biblionumber => $biblionumber6_issn,
633 budget_id => $budget->{budget_id},
634 order_internalnote => "internal note",
635 order_vendornote => "vendor note",
644 } )->store->ordernumber;
646 t::lib::Mocks::mock_preference('SearchWithISSNVariations', 0);
647 $orders_issn = GetHistory( issn => '2434-561X' );
648 is( scalar(@$orders_issn), 0, "GetHistory searches correctly by ISSN" );
650 t::lib::Mocks::mock_preference('SearchWithISSNVariations', 1);
651 $orders_issn = GetHistory( issn => '2434-561X' );
652 is( scalar(@$orders_issn), 1, "GetHistory searches correctly by ISSN" );
654 Koha::Acquisition::Orders->find($ordernumber_issn)->cancel;
657 # Test GetHistory() with and without SearchWithISBNVariations or SearchWithISSNVariations
658 # The ISBN passed as a param is the ISBN-10 version of the 13-digit ISBN in the sample record declared in $marcxml
660 # Do "flavoured" tests
661 subtest 'MARC21' => sub {
663 run_flavoured_tests('MARC21');
666 subtest 'UNIMARC' => sub {
668 run_flavoured_tests('UNIMARC');
671 ### Functions required for "flavoured" tests
672 sub get_title_field {
673 my $marc_flavour = C4::Context->preference('marcflavour');
674 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
678 my $marc_flavour = C4::Context->preference('marcflavour');
679 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
683 my $marc_flavour = C4::Context->preference('marcflavour');
684 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
687 sub get_itemnumber_field {
688 my $marc_flavour = C4::Context->preference('marcflavour');
689 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
692 sub create_isbn_field {
693 my ( $isbn, $marcflavour ) = @_;
695 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
696 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
698 # Add the price subfield
699 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
700 $field->add_subfields( $price_subfield => '$100' );
705 sub create_issn_field {
706 my ( $issn, $marcflavour ) = @_;
708 my ( $issn_field, $issn_subfield ) = get_issn_field();
709 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
714 subtest 'ModReceiveOrder replacementprice tests' => sub {
716 #Let's build an order, we need a couple things though
717 my $order_biblio = $builder->build_sample_biblio;
718 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
719 my $order_invoice = $builder->build({ source => 'Aqinvoice'});
720 my $order_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' } });
721 my $order_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $order_currency->{currency}, invoiceprice => $order_currency->{currency} } });
723 basketno => $order_basket->{basketno},
725 replacementprice => undef,
727 quantityreceived => 0,
728 datereceived => undef,
729 datecancellationprinted => undef,
731 my $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
732 (undef, my $received_ordernumber) = ModReceiveOrder({
733 biblionumber => $order_biblio->biblionumber,
734 order => $receive_order,
735 invoice => $order_invoice,
736 quantityreceived => $receive_order->{quantity},
737 budget_id => $order->{budget_id},
739 my $received_order = GetOrder($received_ordernumber);
740 is ($received_order->{replacementprice},undef,"No price set if none passed in");
741 $orderinfo->{replacementprice} = 16.12;
742 $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
743 (undef, $received_ordernumber) = ModReceiveOrder({
744 biblionumber => $order_biblio->biblionumber,
745 order => $receive_order,
746 invoice => $order_invoice,
747 quantityreceived => $receive_order->{quantity},
748 budget_id => $order->{budget_id},
750 $received_order = GetOrder($received_ordernumber);
751 is ($received_order->{replacementprice},'16.120000',"Replacement price set if none passed in");
754 subtest 'ModReceiveOrder and subscription' => sub {
757 my $first_note = 'first note';
758 my $second_note = 'second note';
759 my $subscription = $builder->build_object( { class => 'Koha::Subscriptions' } );
760 my $order = $builder->build_object(
762 class => 'Koha::Acquisition::Orders',
764 subscriptionid => $subscription->subscriptionid,
765 order_internalnote => $first_note,
767 quantityreceived => 0,
768 ecost_tax_excluded => 42,
769 unitprice_tax_excluded => 42,
773 my $order_info = $order->unblessed;
774 # We do not want the note from the original note to be modified
775 # Keeping it will permit to display it for future receptions
776 $order_info->{order_internalnote} = $second_note;
777 my ( undef, $received_ordernumber ) = ModReceiveOrder(
779 biblionumber => $order->biblionumber,
780 order => $order_info,
781 invoice => $order->{invoiceid},
782 quantityreceived => 1,
783 budget_id => $order->budget_id,
786 my $received_order = Koha::Acquisition::Orders->find($received_ordernumber);
787 is( $received_order->order_internalnote,
788 $second_note, "No price set if none passed in" );
790 is( $order->get_from_storage->order_internalnote, $first_note );
793 subtest 'ModReceiveOrder invoice_unitprice and invoice_currency' => sub {
796 subtest 'partial order' => sub {
799 subtest 'no invoice_unitprice' => sub {
801 my $order = $builder->build_object(
803 class => 'Koha::Acquisition::Orders',
806 quantityreceived => 0,
807 ecost_tax_excluded => 42,
808 unitprice_tax_excluded => 42,
814 %{ $order->unblessed },
815 invoice_unitprice => undef,
816 invoice_currency => undef,
818 my ( undef, $received_ordernumber ) = ModReceiveOrder(
820 biblionumber => $order->biblionumber,
821 order => $order_info,
822 quantityreceived => 1, # We receive only 1
823 budget_id => $order->budget_id,
826 my $active_currency = Koha::Acquisition::Currencies->get_active;
828 Koha::Acquisition::Orders->find($received_ordernumber);
829 is( $received_order->invoice_unitprice,
830 $order->unitprice, 'no price should be stored if none passed' );
831 is( $received_order->invoice_currency,
832 $active_currency->currency, 'no currency should be stored if none passed' );
833 $order = $order->get_from_storage;
834 is( $order->invoice_unitprice, $order->unitprice,
835 'no price should be stored if none passed' );
836 is( $order->invoice_currency, $active_currency->currency,
837 'no currency should be stored if none passed' );
839 subtest 'with invoice_unitprice' => sub {
841 my $order = $builder->build_object(
843 class => 'Koha::Acquisition::Orders',
846 quantityreceived => 0,
847 ecost_tax_excluded => 42,
848 unitprice_tax_excluded => 42,
854 %{ $order->unblessed },
855 invoice_unitprice => 37,
856 invoice_currency => 'GBP',
858 my ( undef, $received_ordernumber ) = ModReceiveOrder(
860 biblionumber => $order->biblionumber,
861 order => $order_info,
862 quantityreceived => 1,
863 budget_id => $order->budget_id,
867 Koha::Acquisition::Orders->find($received_ordernumber);
868 is( $received_order->invoice_unitprice + 0,
869 37, 'price should be stored in new order' );
870 is( $received_order->invoice_currency,
871 'GBP', 'currency should be stored in new order' );
872 $order = $order->get_from_storage;
873 is( $order->invoice_unitprice + 0,
874 37, 'price should be stored in existing order' );
875 is( $order->invoice_currency, 'GBP',
876 'currency should be stored in existing order' );
881 subtest 'full received order' => sub {
884 subtest 'no invoice_unitprice' => sub {
886 my $order = $builder->build_object(
888 class => 'Koha::Acquisition::Orders',
891 quantityreceived => 0,
892 ecost_tax_excluded => 42,
893 unitprice_tax_excluded => 42,
899 %{ $order->unblessed },
900 invoice_unitprice => undef,
901 invoice_currency => undef,
903 my ( undef, $received_ordernumber ) = ModReceiveOrder(
905 biblionumber => $order->biblionumber,
906 order => $order_info,
907 quantityreceived => 5, # We receive them all!
908 budget_id => $order->budget_id,
911 my $active_currency = Koha::Acquisition::Currencies->get_active;
913 Koha::Acquisition::Orders->find($received_ordernumber);
914 is( $received_order->invoice_unitprice,
915 $order->unitprice, 'no price should be stored if none passed' );
916 is( $received_order->invoice_currency,
917 $active_currency->currency, 'no currency should be stored if none passed' );
918 $order = $order->get_from_storage;
919 is( $order->invoice_unitprice, $order->unitprice,
920 'no price should be stored if none passed' );
921 is( $order->invoice_currency, $active_currency->currency,
922 'no currency should be stored if none passed' );
926 subtest 'with invoice_unitprice' => sub {
928 my $order = $builder->build_object(
930 class => 'Koha::Acquisition::Orders',
933 quantityreceived => 0,
934 ecost_tax_excluded => 42,
935 unitprice_tax_excluded => 42,
941 %{ $order->unblessed },
942 invoice_unitprice => 37,
943 invoice_currency => 'GBP',
945 my ( undef, $received_ordernumber ) = ModReceiveOrder(
947 biblionumber => $order->biblionumber,
948 order => $order_info,
949 quantityreceived => 1,
950 budget_id => $order->budget_id,
954 Koha::Acquisition::Orders->find($received_ordernumber);
955 is( $received_order->invoice_unitprice + 0,
956 37, 'price should be stored in new order' );
957 is( $received_order->invoice_currency,
958 'GBP', 'currency should be stored in new order' );
959 $order = $order->get_from_storage;
960 is( $order->invoice_unitprice + 0,
961 37, 'price should be stored in existing order' );
962 is( $order->invoice_currency, 'GBP',
963 'currency should be stored in existing order' );
970 subtest 'GetHistory status search' => sub {
973 my $order_basket = $builder->build( { source => 'Aqbasket', value => { is_standing => 0 } } );
975 basketno => $order_basket->{basketno},
977 replacementprice => undef,
979 quantityreceived => 0,
980 datereceived => undef,
981 orderstatus => 'cancelled',
982 datecancellationprinted => '1990-01-01',
984 my $order = $builder->build( { source => 'Aqorder', value => $orderinfo } );
986 basketno => $order_basket->{basketno},
988 replacementprice => undef,
990 quantityreceived => 0,
991 datereceived => undef,
992 orderstatus => 'new',
993 datecancellationprinted => undef,
995 $order = $builder->build( { source => 'Aqorder', value => $orderinfo2 } );
997 $orders = GetHistory( order_status => "new", basket => $order_basket->{basketno} );
998 is( scalar(@$orders), 1, 'GetHistory with order status "new" returns 1 order' );
1000 my $orders = GetHistory( get_canceled_order => 1, order_status => "any", basket => $order_basket->{basketno} );
1001 is( scalar(@$orders), 2, 'GetHistory with order status "any" returns all (2) orders' );
1003 $orders = GetHistory( order_status => "", basket => $order_basket->{basketno} );
1005 scalar(@$orders), 1,
1006 'GetHistory with order status "all except cancelled" returns only order with status new (1)'
1010 subtest 'GetHistory with additional fields' => sub {
1012 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
1014 basketno => $order_basket->{basketno},
1016 replacementprice => undef,
1018 quantityreceived => 0,
1019 datereceived => undef,
1020 datecancellationprinted => undef,
1022 my $order = $builder->build({ source => 'Aqorder', value => $orderinfo });
1023 my $history = GetHistory(ordernumber => $order->{ordernumber});
1024 is( scalar( @$history ), 1, 'GetHistory returns the one order');
1026 my $additional_field = $builder->build({source => 'AdditionalField', value => {
1027 tablename => 'aqbasket',
1029 authorised_value_category => "",
1032 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
1033 is( scalar ( @$history ), 0, 'GetHistory returns no order for an unused additional field');
1034 my $basket = Koha::Acquisition::Baskets->find({ basketno => $order_basket->{basketno} });
1035 $basket->set_additional_fields([{
1036 id => $additional_field->{id},
1037 value => 'delicious',
1040 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
1041 is( scalar( @$history ), 1, 'GetHistory returns the order when additional field is set');
1044 subtest 'Tests for get_rounding_sql' => sub {
1048 my $value = '3.141592';
1050 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
1051 my $no_rounding_result = C4::Acquisition::get_rounding_sql($value);
1052 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
1053 my $rounding_result = C4::Acquisition::get_rounding_sql($value);
1055 ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
1056 ok( $rounding_result =~ /CAST/, "Value ($value) will be rounded" );
1060 subtest 'Test for get_rounded_price' => sub {
1064 my $exact_price = 3.14;
1065 my $up_price = 3.145592;
1066 my $down_price = 3.141592;
1067 my $round_up_price = sprintf( '%0.2f', $up_price );
1068 my $round_down_price = sprintf( '%0.2f', $down_price );
1070 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
1071 my $not_rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
1072 my $not_rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
1073 my $not_rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
1074 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
1075 my $rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
1076 my $rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
1077 my $rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
1079 is( $not_rounded_result1, $exact_price, "Price ($exact_price) was correctly not rounded ($not_rounded_result1)" );
1080 is( $not_rounded_result2, $up_price, "Price ($up_price) was correctly not rounded ($not_rounded_result2)" );
1081 is( $not_rounded_result3, $down_price, "Price ($down_price) was correctly not rounded ($not_rounded_result3)" );
1082 is( $rounded_result1, $exact_price, "Price ($exact_price) was correctly rounded ($rounded_result1)" );
1083 is( $rounded_result2, $round_up_price, "Price ($up_price) was correctly rounded ($rounded_result2)" );
1084 is( $rounded_result3, $round_down_price, "Price ($down_price) was correctly rounded ($rounded_result3)" );
1088 subtest 'GetHistory - managing library' => sub {
1092 my $orders = GetHistory(managing_library => 'CPL');
1094 my $order_basket1 = $builder->build({ source => 'Aqbasket', value => { branch => 'CPL' } });
1096 basketno => $order_basket1->{basketno},
1098 replacementprice => undef,
1100 quantityreceived => 0,
1101 datereceived => undef,
1102 datecancellationprinted => undef,
1104 my $order1 = $builder->build({ source => 'Aqorder', value => $orderinfo1 });
1106 my $order_basket2 = $builder->build({ source => 'Aqbasket', value => { branch => 'LIB' } });
1108 basketno => $order_basket2->{basketno},
1110 replacementprice => undef,
1112 quantityreceived => 0,
1113 datereceived => undef,
1114 datecancellationprinted => undef,
1116 my $order2 = $builder->build({ source => 'Aqorder', value => $orderinfo2 });
1118 my $history = GetHistory(managing_library => 'CPL');
1119 is( scalar( @$history), scalar ( @$orders ) +1, "GetHistory returns number of orders");
1123 subtest 'GetHistory - is_standing' => sub {
1127 my $orders = GetHistory( is_standing => '1' );
1129 my $order_basket1 = $builder->build( { source => 'Aqbasket', value => { is_standing => 0 } } );
1131 basketno => $order_basket1->{basketno},
1133 replacementprice => undef,
1135 quantityreceived => 0,
1136 datereceived => undef,
1137 datecancellationprinted => undef,
1139 my $order1 = $builder->build( { source => 'Aqorder', value => $orderinfo1 } );
1141 my $order_basket2 = $builder->build( { source => 'Aqbasket', value => { is_standing => 1 } } );
1143 basketno => $order_basket2->{basketno},
1145 replacementprice => undef,
1147 quantityreceived => 0,
1148 datereceived => undef,
1149 datecancellationprinted => undef,
1151 my $order2 = $builder->build( { source => 'Aqorder', value => $orderinfo2 } );
1153 my $history = GetHistory( is_standing => 1 );
1156 scalar(@$orders) + 1,
1157 "GetHistory returns number of standing orders"
1162 subtest 'Acquisition logging' => sub {
1166 t::lib::Mocks::mock_preference('AcquisitionLog', 1);
1168 Koha::ActionLogs->delete;
1169 my $basketno = NewBasket( $booksellerid, 1 );
1170 my @create_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'ADD_BASKET', object => $basketno })->as_list;
1171 is (scalar @create_logs, 1, 'Basket creation is logged');
1173 Koha::ActionLogs->delete;
1174 C4::Acquisition::ReopenBasket($basketno);
1175 my @reopen_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'REOPEN_BASKET', object => $basketno })->as_list;
1176 is (scalar @reopen_logs, 1, 'Basket reopen is logged');
1178 Koha::ActionLogs->delete;
1179 C4::Acquisition::ModBasket({
1180 basketno => $basketno,
1181 booksellerid => $booksellerid
1183 my @mod_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET', object => $basketno })->as_list;
1184 is (scalar @mod_logs, 1, 'Basket modify is logged');
1186 Koha::ActionLogs->delete;
1187 C4::Acquisition::ModBasketHeader($basketno,"Test","","","",$booksellerid);
1188 my @mod_header_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET_HEADER', object => $basketno })->as_list;
1189 is (scalar @mod_header_logs, 1, 'Basket header modify is logged');
1191 Koha::ActionLogs->delete;
1192 C4::Acquisition::ModBasketUsers($basketno,(1));
1193 my @mod_users_logs = Koha::ActionLogs->search({ module =>'ACQUISITIONS', action => 'MODIFY_BASKET_USERS', object => $basketno })->as_list;
1194 is (scalar @mod_users_logs, 1, 'Basket users modify is logged');
1196 t::lib::Mocks::mock_preference('AcquisitionLog', 0);
1199 $schema->storage->txn_rollback();
1201 subtest 'GetInvoices() tests with additional fields' => sub {
1205 $schema->storage->txn_begin;
1207 my $invoice_1 = $builder->build_object(
1209 class => 'Koha::Acquisition::Invoices',
1211 invoicenumber => 'whataretheodds1'
1215 my $invoice_2 = $builder->build_object(
1217 class => 'Koha::Acquisition::Invoices',
1219 invoicenumber => 'whataretheodds2'
1225 my $invoices = [ GetInvoices( invoicenumber => 'whataretheodds' ) ];
1226 is( scalar @{$invoices}, 2, 'Two invoices retrieved' );
1227 is( $invoices->[0]->{invoiceid}, $invoice_1->id );
1228 is( $invoices->[1]->{invoiceid}, $invoice_2->id );
1230 my $additional_field_1 = $builder->build_object(
1231 { class => 'Koha::AdditionalFields',
1233 tablename => 'aqinvoices',
1234 authorised_value_category => "",
1239 my $additional_field_2 = $builder->build_object(
1240 { class => 'Koha::AdditionalFields',
1242 tablename => 'aqinvoices',
1243 authorised_value_category => "",
1248 $invoice_1->set_additional_fields([ { id => $additional_field_1->id, value => 'Ya-Hey' } ]);
1249 $invoice_2->set_additional_fields([ { id => $additional_field_2->id, value => "Hey ho let's go" } ]);
1251 $invoices = [ GetInvoices(
1252 invoicenumber => 'whataretheodds',
1253 additional_fields => [{ id => $additional_field_1->id, value => 'Ya-Hey' }]
1255 is( scalar @{$invoices}, 1, 'One invoice retrieved' );
1256 is( $invoices->[0]->{invoiceid}, $invoice_1->id, 'Ya-Hey' );
1258 $invoices = [ GetInvoices(
1259 invoicenumber => 'whataretheodds',
1260 additional_fields => [{ id => $additional_field_2->id, value => "Hey ho let's go" }]
1262 is( scalar @{$invoices}, 1, 'One invoice retrieved' );
1263 is( $invoices->[0]->{invoiceid}, $invoice_2->id, "Hey ho let's go" );
1265 $schema->storage->txn_rollback;