Bug 25417: Add unit test
[koha.git] / t / db_dependent / Acquisition.t
blob912a451e25ac49a59a22c3896e55eb8b8560cbf0
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 => 79;
23 use t::lib::Mocks;
24 use Koha::Database;
25 use Koha::DateUtils qw(dt_from_string output_pref);
26 use Koha::Acquisition::Basket;
28 use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' );
30 BEGIN {
31 use_ok('C4::Acquisition');
32 use_ok('C4::Biblio');
33 use_ok('C4::Budgets');
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);
43 # params :
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
49 # returns :
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} );
84 return (
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];
103 my (
104 $t_missing_fields, $t_extra_fields,
105 $t_different_fields, $t_nbr_fields
107 = _check_fields_of_order( $exp_fields, $original_order_content,
108 $order_to_check );
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 } };
118 return (
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(
131 name => "my vendor",
132 address1 => "bookseller's address",
133 phone => "0123456",
134 active => 1,
135 deliverytime => 5,
137 )->store;
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);
167 my @ordernumbers;
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 5 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 = (
181 str => {
182 basketno => $basketno,
183 biblionumber => $biblionumber1,
184 budget_id => $budget->{budget_id},
185 uncertainprice => 0,
186 order_internalnote => "internal note",
187 order_vendornote => "vendor note",
188 ordernumber => '',
190 num => {
191 quantity => 24,
192 listprice => 50.121111,
193 ecost => 38.15,
194 rrp => 40.15,
195 discount => 5.1111,
199 str => {
200 basketno => $basketno,
201 biblionumber => $biblionumber2,
202 budget_id => $budget->{budget_id}
204 num => { quantity => 42 }
207 str => {
208 basketno => $basketno,
209 biblionumber => $biblionumber2,
210 budget_id => $budget->{budget_id},
211 uncertainprice => 0,
212 order_internalnote => "internal note",
213 order_vendornote => "vendor note"
215 num => {
216 quantity => 4,
217 ecost => 42.1,
218 rrp => 42.1,
219 listprice => 10.1,
220 ecost => 38.1,
221 rrp => 11.0,
222 discount => 5.1,
226 str => {
227 basketno => $basketno,
228 biblionumber => $biblionumber3,
229 budget_id => $budget->{budget_id},
230 order_internalnote => "internal note",
231 order_vendornote => "vendor note"
233 num => {
234 quantity => 4,
235 ecost => 40,
236 rrp => 42,
237 listprice => 10,
238 ecost => 38.15,
239 rrp => 11.00,
240 discount => 0,
241 uncertainprice => 0,
245 str => {
246 basketno => $basketno,
247 biblionumber => $biblionumber4,
248 budget_id => $budget->{budget_id},
249 order_internalnote => "internal note",
250 order_vendornote => "vendor note"
252 num => {
253 quantity => 1,
254 ecost => 10,
255 rrp => 10,
256 listprice => 10,
257 ecost => 10,
258 rrp => 10,
259 discount => 0,
260 uncertainprice => 0,
264 str => {
265 basketno => $basketno,
266 biblionumber => $biblionumber5,
267 budget_id => $budget->{budget_id},
268 order_internalnote => "internal note",
269 order_vendornote => "vendor note"
271 num => {
272 quantity => 1,
273 ecost => 10,
274 rrp => 10,
275 listprice => 10,
276 ecost => 10,
277 rrp => 10,
278 discount => 0,
279 uncertainprice => 0,
284 # Create 5 orders in database
285 for ( 0 .. 5 ) {
286 my %ocontent;
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 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
297 my $invoiceid = AddInvoice(
298 invoicenumber => 'invoice',
299 booksellerid => $booksellerid,
300 unknown => "unknown"
303 my $invoice = GetInvoice( $invoiceid );
305 my $reception_date = output_pref(
307 dt => dt_from_string->add( days => 1 ),
308 dateformat => 'iso',
309 dateonly => 1,
312 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
314 biblionumber => $biblionumber4,
315 order => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
316 quantityreceived => 1,
317 invoice => $invoice,
318 budget_id => $order_content[4]->{str}->{budget_id},
319 datereceived => $reception_date,
324 output_pref(
326 dt => dt_from_string($datereceived),
327 dateformat => 'iso',
328 dateonly => 1
331 $reception_date,
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,
351 pending => 1
355 ( scalar @$search_orders == 4 ) and !grep ( (
356 ( $_->{ordernumber} eq $ordernumbers[3] )
357 or ( $_->{ordernumber} eq $ordernumbers[4] )
359 @$search_orders )
361 "SearchOrders with pending params gets only pending orders (bug 10723)"
364 $search_orders = SearchOrders({
365 booksellerid => $booksellerid,
366 basketno => $basketno,
367 pending => 1,
368 ordered => 1,
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,
384 pending => 1
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 C4::Acquisition::CloseBasket($basketno);
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,
409 pending => 1,
410 ordered => 1,
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)" );
415 # Test AddClaim
418 my $order = $lateorders->next;
419 $order->claim();
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,
431 order => $order2,
432 quantityreceived => 2,
433 invoice => $invoice,
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},
447 "internal note",
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])' );
470 # Test GetHistory() with and without SearchWithISBNVariations
471 # The ISBN passed as a param is the ISBN-10 version of the 13-digit ISBN in the sample record declared in $marcxml
473 my $budgetid2 = C4::Budgets::AddBudget(
475 budget_code => "budget_code_test_modrecv",
476 budget_name => "budget_name_test_modrecv",
480 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
481 $order3->{order_internalnote} = "my other notes";
482 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
484 biblionumber => $biblionumber2,
485 order => $order3,
486 quantityreceived => 2,
487 invoice => $invoice,
488 budget_id => $budgetid2,
492 $order3 = GetOrder( $ordernumbers[2] );
493 is( $order3->{'quantityreceived'},
494 0, 'Splitting up order did not receive any on original order' );
495 is( $order3->{'quantity'}, 2, '2 items on original order' );
496 is( $order3->{'budget_id'}, $budgetid,
497 'Budget on original order is unchanged' );
498 is( $order3->{order_internalnote}, "my other notes",
499 'ModReceiveOrder and GetOrder deal with notes' );
501 $neworder = GetOrder($new_ordernumber);
502 is( $neworder->{'quantity'}, 2, '2 items on new order' );
503 is( $neworder->{'quantityreceived'},
504 2, 'Splitting up order received items on new order' );
505 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
507 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
508 $order3->{order_internalnote} = "my third notes";
509 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
511 biblionumber => $biblionumber2,
512 order => $order3,
513 quantityreceived => 2,
514 invoice => $invoice,
515 budget_id => $budgetid2,
519 $order3 = GetOrder( $ordernumbers[2] );
520 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
521 is( $order3->{'quantity'}, 2, '2 items on order' );
522 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
523 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
525 my $nonexistent_order = GetOrder();
526 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
527 $nonexistent_order = GetOrder( 424242424242 );
528 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
530 # Tests for DelOrder
531 $order1 = GetOrder($ordernumbers[0]);
532 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
533 ok((not defined $error), "DelOrder does not fail");
534 $order1 = GetOrder($order1->{ordernumber});
535 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
536 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
537 ok((defined Koha::Biblios->find( $order1->{biblionumber} )), "biblio still exists");
539 $order2 = GetOrder($ordernumbers[1]);
540 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
541 ok((not defined $error), "DelOrder does not fail");
542 $order2 = GetOrder($order2->{ordernumber});
543 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
544 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
545 ok((not defined Koha::Biblios->find( $order2->{biblionumber} )), "biblio does not exist anymore");
547 my $order4 = GetOrder($ordernumbers[3]);
548 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
549 ok((not defined $error), "DelOrder does not fail");
550 $order4 = GetOrder($order4->{ordernumber});
551 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
552 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
553 ok((not defined Koha::Biblios->find( $order4->{biblionumber} )), "biblio does not exist anymore");
555 my $order5 = GetOrder($ordernumbers[4]);
556 Koha::Item->new({ barcode => '0102030405', biblionumber => $order5->{biblionumber} })->store;
557 $error = DelOrder($order5->{biblionumber}, $order5->{ordernumber}, 1);
558 $order5 = GetOrder($order5->{ordernumber});
559 ok((defined $order5->{datecancellationprinted}), "order is cancelled");
560 ok((defined Koha::Biblios->find( $order5->{biblionumber} )), "biblio still exists");
562 # End of tests for DelOrder
564 subtest 'ModOrder' => sub {
565 plan tests => 1;
566 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
567 my $order = GetOrder( $order1->{ordernumber} );
568 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
571 # Budget reports
572 my $all_count = scalar GetBudgetsReport();
573 ok($all_count >= 1, "GetBudgetReport OK");
575 my $active_count = scalar GetBudgetsReport(1);
576 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
578 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
579 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
581 # "Flavoured" tests (tests that required a run for each marc flavour)
582 # Tests should be added to the run_flavoured_tests sub below
583 my $biblio_module = new Test::MockModule('C4::Biblio');
584 $biblio_module->mock(
585 'GetMarcSubfieldStructure',
586 sub {
587 my ($self) = shift;
589 my ( $title_field, $title_subfield ) = get_title_field();
590 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
591 my ( $issn_field, $issn_subfield ) = get_issn_field();
592 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
593 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
594 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
596 return {
597 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
598 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
599 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
600 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
601 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
602 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
607 sub run_flavoured_tests {
608 my $marcflavour = shift;
609 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
612 # Test SearchWithISBNVariations syspref
614 my $marc_record = MARC::Record->new;
615 $marc_record->append_fields( create_isbn_field( '9780136019701', $marcflavour ) );
616 my ( $biblionumber6, $biblioitemnumber6 ) = AddBiblio( $marc_record, '' );
618 # Create order
619 my $ordernumber = Koha::Acquisition::Order->new( {
620 basketno => $basketno,
621 biblionumber => $biblionumber6,
622 budget_id => $budget->{budget_id},
623 order_internalnote => "internal note",
624 order_vendornote => "vendor note",
625 quantity => 1,
626 ecost => 10,
627 rrp => 10,
628 listprice => 10,
629 ecost => 10,
630 rrp => 10,
631 discount => 0,
632 uncertainprice => 0,
633 } )->store->ordernumber;
635 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
636 $orders = GetHistory( isbn => '0136019706' );
637 is( scalar(@$orders), 0, "GetHistory searches correctly by ISBN" );
639 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
640 $orders = GetHistory( isbn => '0136019706' );
641 is( scalar(@$orders), 1, "GetHistory searches correctly by ISBN" );
643 my $order = GetOrder($ordernumber);
644 DelOrder($order->{biblionumber}, $order->{ordernumber}, 1);
647 # Do "flavoured" tests
648 subtest 'MARC21' => sub {
649 plan tests => 2;
650 run_flavoured_tests('MARC21');
653 subtest 'UNIMARC' => sub {
654 plan tests => 2;
655 run_flavoured_tests('UNIMARC');
658 subtest 'NORMARC' => sub {
659 plan tests => 2;
660 run_flavoured_tests('NORMARC');
663 ### Functions required for "flavoured" tests
664 sub get_title_field {
665 my $marc_flavour = C4::Context->preference('marcflavour');
666 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
669 sub get_isbn_field {
670 my $marc_flavour = C4::Context->preference('marcflavour');
671 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
674 sub get_issn_field {
675 my $marc_flavour = C4::Context->preference('marcflavour');
676 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
679 sub get_itemnumber_field {
680 my $marc_flavour = C4::Context->preference('marcflavour');
681 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
684 sub create_isbn_field {
685 my ( $isbn, $marcflavour ) = @_;
687 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
688 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
690 # Add the price subfield
691 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
692 $field->add_subfields( $price_subfield => '$100' );
694 return $field;
697 subtest 'ModReceiveOrder replacementprice tests' => sub {
698 plan tests => 2;
699 #Let's build an order, we need a couple things though
700 my $builder = t::lib::TestBuilder->new;
701 my $order_biblio = $builder->build({ source => 'Biblio' });
702 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
703 my $order_invoice = $builder->build({ source => 'Aqinvoice'});
704 my $order_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' } });
705 my $order_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $order_currency->{currency}, invoiceprice => $order_currency->{currency} } });
706 my $orderinfo ={
707 basketno => $order_basket->{basketno},
708 booksellerid => $order_vendor->{id},
709 rrp => 19.99,
710 replacementprice => undef,
711 quantity => 1,
712 quantityreceived => 0,
713 datereceived => undef,
714 datecancellationprinted => undef,
716 my $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
717 (undef, my $received_ordernumber) = ModReceiveOrder({
718 biblionumber => $order_biblio->{biblionumber},
719 order => $receive_order,
720 invoice => $order_invoice,
721 quantityreceived => $receive_order->{quantity},
722 budget_id => $order->{budget_id},
724 my $received_order = GetOrder($received_ordernumber);
725 is ($received_order->{replacementprice},undef,"No price set if none passed in");
726 $orderinfo->{replacementprice} = 16.12;
727 $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
728 (undef, $received_ordernumber) = ModReceiveOrder({
729 biblionumber => $order_biblio->{biblionumber},
730 order => $receive_order,
731 invoice => $order_invoice,
732 quantityreceived => $receive_order->{quantity},
733 budget_id => $order->{budget_id},
735 $received_order = GetOrder($received_ordernumber);
736 is ($received_order->{replacementprice},'16.120000',"Replacement price set if none passed in");
739 subtest 'ModReceiveOrder and subscription' => sub {
740 plan tests => 2;
742 my $builder = t::lib::TestBuilder->new;
743 my $first_note = 'first note';
744 my $second_note = 'second note';
745 my $subscription = $builder->build_object( { class => 'Koha::Subscriptions' } );
746 my $order = $builder->build_object(
748 class => 'Koha::Acquisition::Orders',
749 value => {
750 subscriptionid => $subscription->subscriptionid,
751 order_internalnote => $first_note,
752 quantity => 5,
753 quantityreceived => 0,
754 ecost_tax_excluded => 42,
755 unitprice_tax_excluded => 42,
759 my $order_info = $order->unblessed;
760 # We do not want the note from the original note to be modified
761 # Keeping it will permit to display it for future receptions
762 $order_info->{order_internalnote} = $second_note;
763 my ( undef, $received_ordernumber ) = ModReceiveOrder(
765 biblionumber => $order->biblionumber,
766 order => $order_info,
767 invoice => $order->{invoiceid},
768 quantityreceived => 1,
769 budget_id => $order->budget_id,
772 my $received_order = Koha::Acquisition::Orders->find($received_ordernumber);
773 is( $received_order->order_internalnote,
774 $second_note, "No price set if none passed in" );
776 $order->get_from_storage;
777 is( $order->get_from_storage->order_internalnote, $first_note );
780 subtest 'GetHistory with additional fields' => sub {
781 plan tests => 3;
782 my $builder = t::lib::TestBuilder->new;
783 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
784 my $orderinfo ={
785 basketno => $order_basket->{basketno},
786 rrp => 19.99,
787 replacementprice => undef,
788 quantity => 1,
789 quantityreceived => 0,
790 datereceived => undef,
791 datecancellationprinted => undef,
793 my $order = $builder->build({ source => 'Aqorder', value => $orderinfo });
794 my $history = GetHistory(ordernumber => $order->{ordernumber});
795 is( scalar( @$history ), 1, 'GetHistory returns the one order');
797 my $additional_field = $builder->build({source => 'AdditionalField', value => {
798 tablename => 'aqbasket',
799 name => 'snakeoil',
800 authorised_value_category => "",
803 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
804 is( scalar ( @$history ), 0, 'GetHistory returns no order for an unused additional field');
805 my $basket = Koha::Acquisition::Baskets->find({ basketno => $order_basket->{basketno} });
806 $basket->set_additional_fields([{
807 id => $additional_field->{id},
808 value => 'delicious',
809 }]);
811 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
812 is( scalar( @$history ), 1, 'GetHistory returns the order when additional field is set');
815 subtest 'Tests for get_rounding_sql' => sub {
817 plan tests => 2;
819 my $value = '3.141592';
821 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
822 my $no_rounding_result = C4::Acquisition::get_rounding_sql($value);
823 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
824 my $rounding_result = C4::Acquisition::get_rounding_sql($value);
826 ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
827 ok( $rounding_result =~ /CAST/, "Value ($value) will be rounded" );
831 subtest 'Test for get_rounded_price' => sub {
833 plan tests => 6;
835 my $exact_price = 3.14;
836 my $up_price = 3.145592;
837 my $down_price = 3.141592;
838 my $round_up_price = sprintf( '%0.2f', $up_price );
839 my $round_down_price = sprintf( '%0.2f', $down_price );
841 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
842 my $not_rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
843 my $not_rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
844 my $not_rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
845 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
846 my $rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
847 my $rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
848 my $rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
850 is( $not_rounded_result1, $exact_price, "Price ($exact_price) was correctly not rounded ($not_rounded_result1)" );
851 is( $not_rounded_result2, $up_price, "Price ($up_price) was correctly not rounded ($not_rounded_result2)" );
852 is( $not_rounded_result3, $down_price, "Price ($down_price) was correctly not rounded ($not_rounded_result3)" );
853 is( $rounded_result1, $exact_price, "Price ($exact_price) was correctly rounded ($rounded_result1)" );
854 is( $rounded_result2, $round_up_price, "Price ($up_price) was correctly rounded ($rounded_result2)" );
855 is( $rounded_result3, $round_down_price, "Price ($down_price) was correctly rounded ($rounded_result3)" );
859 subtest 'GetHistory - managing library' => sub {
861 plan tests => 1;
863 my $orders = GetHistory(managing_library => 'CPL');
865 my $builder = t::lib::TestBuilder->new;
867 my $order_basket1 = $builder->build({ source => 'Aqbasket', value => { branch => 'CPL' } });
868 my $orderinfo1 ={
869 basketno => $order_basket1->{basketno},
870 rrp => 19.99,
871 replacementprice => undef,
872 quantity => 1,
873 quantityreceived => 0,
874 datereceived => undef,
875 datecancellationprinted => undef,
877 my $order1 = $builder->build({ source => 'Aqorder', value => $orderinfo1 });
879 my $order_basket2 = $builder->build({ source => 'Aqbasket', value => { branch => 'LIB' } });
880 my $orderinfo2 ={
881 basketno => $order_basket2->{basketno},
882 rrp => 19.99,
883 replacementprice => undef,
884 quantity => 1,
885 quantityreceived => 0,
886 datereceived => undef,
887 datecancellationprinted => undef,
889 my $order2 = $builder->build({ source => 'Aqorder', value => $orderinfo2 });
891 my $history = GetHistory(managing_library => 'CPL');
892 is( scalar( @$history), scalar ( @$orders ) +1, "GetHistory returns number of orders");
896 $schema->storage->txn_rollback();