Bug 22884: Show separator between 440 and 490 fields
[koha.git] / t / db_dependent / Acquisition.t
blobb6fe38d3e9155edc4864428ddf937bf590cbd640
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 => 77;
23 use t::lib::Mocks;
24 use Koha::Database;
25 use Koha::Acquisition::Basket;
27 use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' );
29 BEGIN {
30 use_ok('C4::Acquisition');
31 use_ok('C4::Biblio');
32 use_ok('C4::Budgets');
33 use_ok('Koha::Acquisition::Orders');
34 use_ok('Koha::Acquisition::Booksellers');
35 use_ok('t::lib::TestBuilder');
38 # Sub used for testing C4::Acquisition subs returning order(s):
39 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
40 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
41 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
42 # params :
43 # $exp_fields : arrayref whose elements are the keys we expect to find
44 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
45 # containing content fields of the order created with Koha::Acquisition::Order
46 # $order_to_check : hashref whose keys/values are the content of an order
47 # returned by the C4::Acquisition sub we are testing
48 # returns :
49 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
50 # fields missing in $order_to_check
51 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
52 # fields unexpected in $order_to_check
53 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
54 # fields which value is not the same in between $order_to_check and
55 # $test_nbr_fields : contains the number of fields of $order_to_check
57 sub _check_fields_of_order {
58 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
59 my @test_missing_fields = ();
60 my @test_extra_fields = ();
61 my @test_different_fields = ();
62 my $test_nbr_fields = scalar( keys %$order_to_check );
63 foreach my $field (@$exp_fields) {
64 push @test_missing_fields, $field
65 unless exists( $order_to_check->{$field} );
67 foreach my $field ( keys %$order_to_check ) {
68 push @test_extra_fields, $field
69 unless grep ( /^$field$/, @$exp_fields );
71 foreach my $field ( keys %{ $original_order_content->{str} } ) {
72 push @test_different_fields, $field
73 unless ( !exists $order_to_check->{$field} )
74 or ( $original_order_content->{str}->{$field} eq
75 $order_to_check->{$field} );
77 foreach my $field ( keys %{ $original_order_content->{num} } ) {
78 push @test_different_fields, $field
79 unless ( !exists $order_to_check->{$field} )
80 or ( $original_order_content->{num}->{$field} ==
81 $order_to_check->{$field} );
83 return (
84 \@test_missing_fields, \@test_extra_fields,
85 \@test_different_fields, $test_nbr_fields
89 # Sub used for testing C4::Acquisition subs returning several orders
90 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
91 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
92 sub _check_fields_of_orders {
93 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
94 my @test_missing_fields = ();
95 my @test_extra_fields = ();
96 my @test_different_fields = ();
97 my @test_nbr_fields = ();
98 foreach my $order_to_check (@$orders_to_check) {
99 my $original_order_content =
100 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
101 @$original_orders_content )[0];
102 my (
103 $t_missing_fields, $t_extra_fields,
104 $t_different_fields, $t_nbr_fields
106 = _check_fields_of_order( $exp_fields, $original_order_content,
107 $order_to_check );
108 push @test_missing_fields, @$t_missing_fields;
109 push @test_extra_fields, @$t_extra_fields;
110 push @test_different_fields, @$t_different_fields;
111 push @test_nbr_fields, $t_nbr_fields;
113 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
114 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
115 @test_different_fields =
116 keys %{ { map { $_ => 1 } @test_different_fields } };
117 return (
118 \@test_missing_fields, \@test_extra_fields,
119 \@test_different_fields, \@test_nbr_fields
124 my $schema = Koha::Database->new()->schema();
125 $schema->storage->txn_begin();
127 my $dbh = C4::Context->dbh;
128 $dbh->{RaiseError} = 1;
130 # Creating some orders
131 my $bookseller = Koha::Acquisition::Bookseller->new(
133 name => "my vendor",
134 address1 => "bookseller's address",
135 phone => "0123456",
136 active => 1,
137 deliverytime => 5,
139 )->store;
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);
169 my @ordernumbers;
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 5 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 = (
183 str => {
184 basketno => $basketno,
185 biblionumber => $biblionumber1,
186 budget_id => $budget->{budget_id},
187 uncertainprice => 0,
188 order_internalnote => "internal note",
189 order_vendornote => "vendor note",
190 ordernumber => '',
192 num => {
193 quantity => 24,
194 listprice => 50.121111,
195 ecost => 38.15,
196 rrp => 40.15,
197 discount => 5.1111,
201 str => {
202 basketno => $basketno,
203 biblionumber => $biblionumber2,
204 budget_id => $budget->{budget_id}
206 num => { quantity => 42 }
209 str => {
210 basketno => $basketno,
211 biblionumber => $biblionumber2,
212 budget_id => $budget->{budget_id},
213 uncertainprice => 0,
214 order_internalnote => "internal note",
215 order_vendornote => "vendor note"
217 num => {
218 quantity => 4,
219 ecost => 42.1,
220 rrp => 42.1,
221 listprice => 10.1,
222 ecost => 38.1,
223 rrp => 11.0,
224 discount => 5.1,
228 str => {
229 basketno => $basketno,
230 biblionumber => $biblionumber3,
231 budget_id => $budget->{budget_id},
232 order_internalnote => "internal note",
233 order_vendornote => "vendor note"
235 num => {
236 quantity => 4,
237 ecost => 40,
238 rrp => 42,
239 listprice => 10,
240 ecost => 38.15,
241 rrp => 11.00,
242 discount => 0,
243 uncertainprice => 0,
247 str => {
248 basketno => $basketno,
249 biblionumber => $biblionumber4,
250 budget_id => $budget->{budget_id},
251 order_internalnote => "internal note",
252 order_vendornote => "vendor note"
254 num => {
255 quantity => 1,
256 ecost => 10,
257 rrp => 10,
258 listprice => 10,
259 ecost => 10,
260 rrp => 10,
261 discount => 0,
262 uncertainprice => 0,
266 str => {
267 basketno => $basketno,
268 biblionumber => $biblionumber5,
269 budget_id => $budget->{budget_id},
270 order_internalnote => "internal note",
271 order_vendornote => "vendor note"
273 num => {
274 quantity => 1,
275 ecost => 10,
276 rrp => 10,
277 listprice => 10,
278 ecost => 10,
279 rrp => 10,
280 discount => 0,
281 uncertainprice => 0,
286 # Create 5 orders in database
287 for ( 0 .. 5 ) {
288 my %ocontent;
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 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
299 my $invoiceid = AddInvoice(
300 invoicenumber => 'invoice',
301 booksellerid => $booksellerid,
302 unknown => "unknown"
305 my $invoice = GetInvoice( $invoiceid );
307 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
309 biblionumber => $biblionumber4,
310 order => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
311 quantityreceived => 1,
312 invoice => $invoice,
313 budget_id => $order_content[4]->{str}->{budget_id},
317 my $search_orders = SearchOrders({
318 booksellerid => $booksellerid,
319 basketno => $basketno
321 isa_ok( $search_orders, 'ARRAY' );
324 ( scalar @$search_orders == 5 )
325 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
327 "SearchOrders only gets non-cancelled orders"
330 $search_orders = SearchOrders({
331 booksellerid => $booksellerid,
332 basketno => $basketno,
333 pending => 1
337 ( scalar @$search_orders == 4 ) and !grep ( (
338 ( $_->{ordernumber} eq $ordernumbers[3] )
339 or ( $_->{ordernumber} eq $ordernumbers[4] )
341 @$search_orders )
343 "SearchOrders with pending params gets only pending orders (bug 10723)"
346 $search_orders = SearchOrders({
347 booksellerid => $booksellerid,
348 basketno => $basketno,
349 pending => 1,
350 ordered => 1,
352 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
354 $search_orders = SearchOrders({
355 ordernumber => $ordernumbers[4]
357 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
359 $search_orders = SearchOrders({
360 biblionumber => $biblionumber4
362 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
364 $search_orders = SearchOrders({
365 biblionumber => $biblionumber4,
366 pending => 1
368 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
371 # Test GetBudgetByOrderNumber
373 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
374 "GetBudgetByOrderNumber returns expected budget" );
376 my @lateorders = GetLateOrders(0);
377 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
378 0, "GetLateOrders does not get orders from opened baskets" );
379 C4::Acquisition::CloseBasket($basketno);
380 @lateorders = GetLateOrders(0);
381 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
382 0, "GetLateOrders gets orders from closed baskets" );
383 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
384 "GetLateOrders does not get cancelled orders" );
385 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
386 "GetLateOrders does not get received orders" );
388 $search_orders = SearchOrders({
389 booksellerid => $booksellerid,
390 basketno => $basketno,
391 pending => 1,
392 ordered => 1,
394 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)" );
397 # Test AddClaim
400 my $order = $lateorders[0];
401 AddClaim( $order->{ordernumber} );
402 my $neworder = GetOrder( $order->{ordernumber} );
404 $neworder->{claimed_date},
405 strftime( "%Y-%m-%d", localtime(time) ),
406 "AddClaim : Check claimed_date"
409 my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed;
410 $order2->{order_internalnote} = "my notes";
411 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
413 biblionumber => $biblionumber2,
414 order => $order2,
415 quantityreceived => 2,
416 invoice => $invoice,
419 $order2 = GetOrder( $ordernumbers[1] );
420 is( $order2->{'quantityreceived'},
421 0, 'Splitting up order did not receive any on original order' );
422 is( $order2->{'quantity'}, 40, '40 items on original order' );
423 is( $order2->{'budget_id'}, $budgetid,
424 'Budget on original order is unchanged' );
425 is( $order2->{order_internalnote}, "my notes",
426 'ModReceiveOrder and GetOrder deal with internal notes' );
427 my $order1 = GetOrder( $ordernumbers[0] );
429 $order1->{order_internalnote},
430 "internal note",
431 "ModReceiveOrder only changes the supplied orders internal notes"
434 $neworder = GetOrder($new_ordernumber);
435 is( $neworder->{'quantity'}, 2, '2 items on new order' );
436 is( $neworder->{'quantityreceived'},
437 2, 'Splitting up order received items on new order' );
438 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
440 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
441 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
443 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
444 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
445 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
446 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
447 $orders = GetHistory( ordernumbers => [$ordernumbers[1]] );
448 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumbers returns 1 order' );
449 $orders = GetHistory( ordernumbers => \@ordernumbers );
450 is( scalar( @$orders ), scalar( @ordernumbers ) - 1, 'GetHistory with a list of ordernumbers returns N-1 orders (was has been deleted [3])' );
453 # Test GetHistory() with and without SearchWithISBNVariations
454 # The ISBN passed as a param is the ISBN-10 version of the 13-digit ISBN in the sample record declared in $marcxml
456 my $budgetid2 = C4::Budgets::AddBudget(
458 budget_code => "budget_code_test_modrecv",
459 budget_name => "budget_name_test_modrecv",
463 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
464 $order3->{order_internalnote} = "my other notes";
465 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
467 biblionumber => $biblionumber2,
468 order => $order3,
469 quantityreceived => 2,
470 invoice => $invoice,
471 budget_id => $budgetid2,
475 $order3 = GetOrder( $ordernumbers[2] );
476 is( $order3->{'quantityreceived'},
477 0, 'Splitting up order did not receive any on original order' );
478 is( $order3->{'quantity'}, 2, '2 items on original order' );
479 is( $order3->{'budget_id'}, $budgetid,
480 'Budget on original order is unchanged' );
481 is( $order3->{order_internalnote}, "my other notes",
482 'ModReceiveOrder and GetOrder deal with notes' );
484 $neworder = GetOrder($new_ordernumber);
485 is( $neworder->{'quantity'}, 2, '2 items on new order' );
486 is( $neworder->{'quantityreceived'},
487 2, 'Splitting up order received items on new order' );
488 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
490 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
491 $order3->{order_internalnote} = "my third notes";
492 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
494 biblionumber => $biblionumber2,
495 order => $order3,
496 quantityreceived => 2,
497 invoice => $invoice,
498 budget_id => $budgetid2,
502 $order3 = GetOrder( $ordernumbers[2] );
503 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
504 is( $order3->{'quantity'}, 2, '2 items on order' );
505 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
506 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
508 my $nonexistent_order = GetOrder();
509 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
510 $nonexistent_order = GetOrder( 424242424242 );
511 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
513 # Tests for DelOrder
514 $order1 = GetOrder($ordernumbers[0]);
515 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
516 ok((not defined $error), "DelOrder does not fail");
517 $order1 = GetOrder($order1->{ordernumber});
518 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
519 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
520 ok((defined Koha::Biblios->find( $order1->{biblionumber} )), "biblio still exists");
522 $order2 = GetOrder($ordernumbers[1]);
523 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
524 ok((not defined $error), "DelOrder does not fail");
525 $order2 = GetOrder($order2->{ordernumber});
526 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
527 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
528 ok((not defined Koha::Biblios->find( $order2->{biblionumber} )), "biblio does not exist anymore");
530 my $order4 = GetOrder($ordernumbers[3]);
531 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
532 ok((not defined $error), "DelOrder does not fail");
533 $order4 = GetOrder($order4->{ordernumber});
534 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
535 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
536 ok((not defined Koha::Biblios->find( $order4->{biblionumber} )), "biblio does not exist anymore");
538 my $order5 = GetOrder($ordernumbers[4]);
539 C4::Items::AddItem( { barcode => '0102030405' }, $order5->{biblionumber} );
540 $error = DelOrder($order5->{biblionumber}, $order5->{ordernumber}, 1);
541 $order5 = GetOrder($order5->{ordernumber});
542 ok((defined $order5->{datecancellationprinted}), "order is cancelled");
543 ok((defined Koha::Biblios->find( $order5->{biblionumber} )), "biblio still exists");
545 # End of tests for DelOrder
547 subtest 'ModOrder' => sub {
548 plan tests => 1;
549 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
550 my $order = GetOrder( $order1->{ordernumber} );
551 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
554 # Budget reports
555 my $all_count = scalar GetBudgetsReport();
556 ok($all_count >= 1, "GetBudgetReport OK");
558 my $active_count = scalar GetBudgetsReport(1);
559 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
561 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
562 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
564 # "Flavoured" tests (tests that required a run for each marc flavour)
565 # Tests should be added to the run_flavoured_tests sub below
566 my $biblio_module = new Test::MockModule('C4::Biblio');
567 $biblio_module->mock(
568 'GetMarcSubfieldStructure',
569 sub {
570 my ($self) = shift;
572 my ( $title_field, $title_subfield ) = get_title_field();
573 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
574 my ( $issn_field, $issn_subfield ) = get_issn_field();
575 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
576 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
577 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
579 return {
580 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
581 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
582 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
583 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
584 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
585 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
590 sub run_flavoured_tests {
591 my $marcflavour = shift;
592 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
595 # Test SearchWithISBNVariations syspref
597 my $marc_record = MARC::Record->new;
598 $marc_record->append_fields( create_isbn_field( '9780136019701', $marcflavour ) );
599 my ( $biblionumber6, $biblioitemnumber6 ) = AddBiblio( $marc_record, '' );
601 # Create order
602 my $ordernumber = Koha::Acquisition::Order->new( {
603 basketno => $basketno,
604 biblionumber => $biblionumber6,
605 budget_id => $budget->{budget_id},
606 order_internalnote => "internal note",
607 order_vendornote => "vendor note",
608 quantity => 1,
609 ecost => 10,
610 rrp => 10,
611 listprice => 10,
612 ecost => 10,
613 rrp => 10,
614 discount => 0,
615 uncertainprice => 0,
616 } )->store->ordernumber;
618 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
619 $orders = GetHistory( isbn => '0136019706' );
620 is( scalar(@$orders), 0, "GetHistory searches correctly by ISBN" );
622 t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
623 $orders = GetHistory( isbn => '0136019706' );
624 is( scalar(@$orders), 1, "GetHistory searches correctly by ISBN" );
626 my $order = GetOrder($ordernumber);
627 DelOrder($order->{biblionumber}, $order->{ordernumber}, 1);
630 # Do "flavoured" tests
631 subtest 'MARC21' => sub {
632 plan tests => 2;
633 run_flavoured_tests('MARC21');
636 subtest 'UNIMARC' => sub {
637 plan tests => 2;
638 run_flavoured_tests('UNIMARC');
641 subtest 'NORMARC' => sub {
642 plan tests => 2;
643 run_flavoured_tests('NORMARC');
646 ### Functions required for "flavoured" tests
647 sub get_title_field {
648 my $marc_flavour = C4::Context->preference('marcflavour');
649 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
652 sub get_isbn_field {
653 my $marc_flavour = C4::Context->preference('marcflavour');
654 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
657 sub get_issn_field {
658 my $marc_flavour = C4::Context->preference('marcflavour');
659 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
662 sub get_itemnumber_field {
663 my $marc_flavour = C4::Context->preference('marcflavour');
664 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
667 sub create_isbn_field {
668 my ( $isbn, $marcflavour ) = @_;
670 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
671 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
673 # Add the price subfield
674 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
675 $field->add_subfields( $price_subfield => '$100' );
677 return $field;
680 subtest 'ModReceiveOrder replacementprice tests' => sub {
681 plan tests => 2;
682 #Let's build an order, we need a couple things though
683 my $builder = t::lib::TestBuilder->new;
684 my $order_biblio = $builder->build({ source => 'Biblio' });
685 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
686 my $order_invoice = $builder->build({ source => 'Aqinvoice'});
687 my $order_currency = $builder->build({ source => 'Currency', value => { active => 1, archived => 0, symbol => 'F', rate => 2, isocode => undef, currency => 'FOO' } });
688 my $order_vendor = $builder->build({ source => 'Aqbookseller',value => { listincgst => 0, listprice => $order_currency->{currency}, invoiceprice => $order_currency->{currency} } });
689 my $orderinfo ={
690 basketno => $order_basket->{basketno},
691 booksellerid => $order_vendor->{id},
692 rrp => 19.99,
693 replacementprice => undef,
694 quantity => 1,
695 quantityreceived => 0,
696 datereceived => undef,
697 datecancellationprinted => undef,
699 my $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
700 (undef, my $received_ordernumber) = ModReceiveOrder({
701 biblionumber => $order_biblio->{biblionumber},
702 order => $receive_order,
703 invoice => $order_invoice,
704 quantityreceived => $receive_order->{quantity},
705 budget_id => $order->{budget_id},
707 my $received_order = GetOrder($received_ordernumber);
708 is ($received_order->{replacementprice},undef,"No price set if none passed in");
709 $orderinfo->{replacementprice} = 16.12;
710 $receive_order = $builder->build({ source => 'Aqorder', value => $orderinfo });
711 (undef, $received_ordernumber) = ModReceiveOrder({
712 biblionumber => $order_biblio->{biblionumber},
713 order => $receive_order,
714 invoice => $order_invoice,
715 quantityreceived => $receive_order->{quantity},
716 budget_id => $order->{budget_id},
718 $received_order = GetOrder($received_ordernumber);
719 is ($received_order->{replacementprice},'16.120000',"Replacement price set if none passed in");
722 subtest 'ModReceiveOrder and subscription' => sub {
723 plan tests => 2;
725 my $builder = t::lib::TestBuilder->new;
726 my $first_note = 'first note';
727 my $second_note = 'second note';
728 my $subscription = $builder->build_object( { class => 'Koha::Subscriptions' } );
729 my $order = $builder->build_object(
731 class => 'Koha::Acquisition::Orders',
732 value => {
733 subscriptionid => $subscription->subscriptionid,
734 order_internalnote => $first_note,
735 quantity => 5,
736 quantityreceived => 0,
737 ecost_tax_excluded => 42,
738 unitprice_tax_excluded => 42,
742 my $order_info = $order->unblessed;
743 # We do not want the note from the original note to be modified
744 # Keeping it will permit to display it for future receptions
745 $order_info->{order_internalnote} = $second_note;
746 my ( undef, $received_ordernumber ) = ModReceiveOrder(
748 biblionumber => $order->biblionumber,
749 order => $order_info,
750 invoice => $order->{invoiceid},
751 quantityreceived => 1,
752 budget_id => $order->budget_id,
755 my $received_order = Koha::Acquisition::Orders->find($received_ordernumber);
756 is( $received_order->order_internalnote,
757 $second_note, "No price set if none passed in" );
759 $order->get_from_storage;
760 is( $order->get_from_storage->order_internalnote, $first_note );
763 subtest 'GetHistory with additional fields' => sub {
764 plan tests => 3;
765 my $builder = t::lib::TestBuilder->new;
766 my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
767 my $orderinfo ={
768 basketno => $order_basket->{basketno},
769 rrp => 19.99,
770 replacementprice => undef,
771 quantity => 1,
772 quantityreceived => 0,
773 datereceived => undef,
774 datecancellationprinted => undef,
776 my $order = $builder->build({ source => 'Aqorder', value => $orderinfo });
777 my $history = GetHistory(ordernumber => $order->{ordernumber});
778 is( scalar( @$history ), 1, 'GetHistory returns the one order');
780 my $additional_field = $builder->build({source => 'AdditionalField', value => {
781 tablename => 'aqbasket',
782 name => 'snakeoil',
783 authorised_value_category => "",
786 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
787 is( scalar ( @$history ), 0, 'GetHistory returns no order for an unused additional field');
788 my $basket = Koha::Acquisition::Baskets->find({ basketno => $order_basket->{basketno} });
789 $basket->set_additional_fields([{
790 id => $additional_field->{id},
791 value => 'delicious',
792 }]);
794 $history = GetHistory( ordernumber => $order->{ordernumber}, additional_fields => [{ id => $additional_field->{id}, value=>'delicious'}]);
795 is( scalar( @$history ), 1, 'GetHistory returns the order when additional field is set');
798 subtest 'Tests for get_rounding_sql' => sub {
800 plan tests => 2;
802 my $value = '3.141592';
804 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
805 my $no_rounding_result = C4::Acquisition::get_rounding_sql($value);
806 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
807 my $rounding_result = C4::Acquisition::get_rounding_sql($value);
809 ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
810 ok( $rounding_result =~ /CAST/, "Value ($value) will be rounded" );
814 subtest 'Test for get_rounded_price' => sub {
816 plan tests => 6;
818 my $exact_price = 3.14;
819 my $up_price = 3.145592;
820 my $down_price = 3.141592;
821 my $round_up_price = sprintf( '%0.2f', $up_price );
822 my $round_down_price = sprintf( '%0.2f', $down_price );
824 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{} );
825 my $not_rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
826 my $not_rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
827 my $not_rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
828 t::lib::Mocks::mock_preference( 'OrderPriceRounding', q{nearest_cent} );
829 my $rounded_result1 = C4::Acquisition::get_rounded_price($exact_price);
830 my $rounded_result2 = C4::Acquisition::get_rounded_price($up_price);
831 my $rounded_result3 = C4::Acquisition::get_rounded_price($down_price);
833 is( $not_rounded_result1, $exact_price, "Price ($exact_price) was correctly not rounded ($not_rounded_result1)" );
834 is( $not_rounded_result2, $up_price, "Price ($up_price) was correctly not rounded ($not_rounded_result2)" );
835 is( $not_rounded_result3, $down_price, "Price ($down_price) was correctly not rounded ($not_rounded_result3)" );
836 is( $rounded_result1, $exact_price, "Price ($exact_price) was correctly rounded ($rounded_result1)" );
837 is( $rounded_result2, $round_up_price, "Price ($up_price) was correctly rounded ($rounded_result2)" );
838 is( $rounded_result3, $round_down_price, "Price ($down_price) was correctly rounded ($rounded_result3)" );
841 $schema->storage->txn_rollback();