Bug 36750: Fix aria-label for show more/less links
[koha.git] / t / db_dependent / Acquisition.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use POSIX qw(strftime);
21
22 use Test::More tests => 73;
23 use t::lib::Mocks;
24 use Koha::Database;
25 use Koha::DateUtils qw(dt_from_string output_pref);
26 use Koha::Acquisition::Basket;
27
28 use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' );
29
30 BEGIN {
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');
37 }
38
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
57
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} );
67     }
68     foreach my $field ( keys %$order_to_check ) {
69         push @test_extra_fields, $field
70           unless grep ( /^$field$/, @$exp_fields );
71     }
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} );
77     }
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} );
83     }
84     return (
85         \@test_missing_fields,   \@test_extra_fields,
86         \@test_different_fields, $test_nbr_fields
87     );
88 }
89
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
106           )
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;
113     }
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
121     );
122 }
123
124
125 my $schema = Koha::Database->new()->schema();
126 $schema->storage->txn_begin();
127
128 my $builder = t::lib::TestBuilder->new;
129
130 # Creating some orders
131 my $bookseller = Koha::Acquisition::Bookseller->new(
132     {
133         name         => "my vendor",
134         address1     => "bookseller's address",
135         phone        => "0123456",
136         active       => 1,
137         deliverytime => 5,
138     }
139 )->store;
140 my $booksellerid = $bookseller->id;
141
142 my $booksellerinfo = Koha::Acquisition::Booksellers->find( $booksellerid );
143 is( $booksellerinfo->deliverytime,
144     5, 'set deliverytime when creating vendor (Bug 10556)' );
145
146 my ( $basket, $basketno );
147 ok(
148     $basketno = NewBasket( $booksellerid, 1 ),
149     "NewBasket(  $booksellerid , 1  ) returns $basketno"
150 );
151 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
152
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"
158 });
159
160 my $budgetid = C4::Budgets::AddBudget(
161     {
162         budget_code => "budget_code_test_1",
163         budget_name => "budget_name_test_1",
164         budget_period_id => $bpid,
165     }
166 );
167 my $budget = C4::Budgets::GetBudget($budgetid);
168
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, '' );
175
176
177
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
180
181 my @order_content = (
182     {
183         str => {
184             basketno       => $basketno,
185             biblionumber   => $biblionumber1,
186             budget_id      => $budget->{budget_id},
187             uncertainprice => 0,
188             order_internalnote => "internal note foo",
189             order_vendornote   => "vendor note bar",
190             ordernumber => '',
191         },
192         num => {
193             quantity  => 24,
194             listprice => 50.121111,
195             ecost     => 38.15,
196             rrp       => 40.15,
197             discount  => 5.1111,
198         }
199     },
200     {
201         str => {
202             basketno     => $basketno,
203             biblionumber => $biblionumber2,
204             budget_id    => $budget->{budget_id}
205         },
206         num => { quantity => 42 }
207     },
208     {
209         str => {
210             basketno       => $basketno,
211             biblionumber   => $biblionumber2,
212             budget_id      => $budget->{budget_id},
213             uncertainprice => 0,
214             order_internalnote => "internal note foo",
215             order_vendornote   => "vendor note bar"
216         },
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,
225         }
226     },
227     {
228         str => {
229             basketno     => $basketno,
230             biblionumber => $biblionumber3,
231             budget_id    => $budget->{budget_id},
232             order_internalnote => "internal note",
233             order_vendornote   => "vendor note"
234         },
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,
244         }
245     },
246     {
247         str => {
248             basketno     => $basketno,
249             biblionumber => $biblionumber4,
250             budget_id    => $budget->{budget_id},
251             order_internalnote => "internal note bar",
252             order_vendornote   => "vendor note foo"
253         },
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,
263         }
264     },
265     {
266         str => {
267             basketno     => $basketno,
268             biblionumber => $biblionumber5,
269             budget_id    => $budget->{budget_id},
270             order_internalnote => "internal note",
271             order_vendornote   => "vendor note"
272         },
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,
282         }
283     }
284 );
285
286 # Create 6 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[$_];
295 }
296
297 Koha::Acquisition::Orders->find($ordernumbers[3])->cancel;
298
299 my $invoiceid = AddInvoice(
300     invoicenumber => 'invoice',
301     booksellerid  => $booksellerid,
302     unknown       => "unknown"
303 );
304
305 my $invoice = GetInvoice( $invoiceid );
306
307 my $reception_date = output_pref(
308     {
309             dt => dt_from_string->add( days => 1 ),
310             dateformat => 'iso',
311             dateonly => 1,
312     }
313 );
314 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
315     {
316         biblionumber      => $biblionumber4,
317         order             => Koha::Acquisition::Orders->find( $ordernumbers[4] )->unblessed,
318         quantityreceived  => 1,
319         invoice           => $invoice,
320         budget_id         => $order_content[4]->{str}->{budget_id},
321         datereceived      => $reception_date,
322     }
323 );
324
325 is(
326     output_pref(
327         {
328             dt         => dt_from_string($datereceived),
329             dateformat => 'iso',
330             dateonly   => 1
331         }
332     ),
333     $reception_date,
334     'ModReceiveOrder sets the passed date'
335 );
336
337 my $search_orders = SearchOrders({
338     booksellerid => $booksellerid,
339     basketno     => $basketno
340 });
341 isa_ok( $search_orders, 'ARRAY' );
342 ok(
343     (
344         ( scalar @$search_orders == 5 )
345           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
346     ),
347     "SearchOrders only gets non-cancelled orders"
348 );
349
350 $search_orders = SearchOrders({
351     booksellerid => $booksellerid,
352     basketno     => $basketno,
353     pending      => 1
354 });
355 ok(
356     (
357         ( scalar @$search_orders == 4 ) and !grep ( (
358                      ( $_->{ordernumber} eq $ordernumbers[3] )
359                   or ( $_->{ordernumber} eq $ordernumbers[4] )
360             ),
361             @$search_orders )
362     ),
363     "SearchOrders with pending params gets only pending orders (bug 10723)"
364 );
365
366 $search_orders = SearchOrders({
367     booksellerid => $booksellerid,
368     basketno     => $basketno,
369     pending      => 1,
370     ordered      => 1,
371 });
372 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
373
374 $search_orders = SearchOrders({
375     ordernumber => $ordernumbers[4]
376 });
377 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
378
379 $search_orders = SearchOrders({
380     biblionumber => $biblionumber4
381 });
382 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
383
384 $search_orders = SearchOrders({
385     biblionumber => $biblionumber4,
386     pending      => 1
387 });
388 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
389
390 #
391 # Test GetBudgetByOrderNumber
392 #
393 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
394     "GetBudgetByOrderNumber returns expected budget" );
395
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" );
407
408 $search_orders = SearchOrders({
409     booksellerid => $booksellerid,
410     basketno     => $basketno,
411     pending      => 1,
412     ordered      => 1,
413 });
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)" );
415
416 #
417 # Test AddClaim
418 #
419
420 my $order = $lateorders->next;
421 $order->claim();
422 is(
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"
426 );
427
428 my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed;
429 $order2->{order_internalnote} = "my notes";
430 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
431     {
432         biblionumber     => $biblionumber2,
433         order            => $order2,
434         quantityreceived => 2,
435         invoice          => $invoice,
436     }
437 );
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] );
447 is(
448     $order1->{order_internalnote},
449     "internal note foo",
450     "ModReceiveOrder only changes the supplied orders internal notes"
451 );
452
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' );
458
459 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
460 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
461
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])' );
470
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' );
479
480 my $budget2 = $builder->build_object(
481     {
482         class => 'Koha::Acquisition::Funds',
483         value => {
484             budget_code => "budget_code_test_modrecv",
485             budget_name => "budget_name_test_modrecv",
486         }
487     }
488 );
489 my $budgetid2 = $budget2->id;
490
491 my $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
492 $order3->{order_internalnote} = "my other notes";
493 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
494     {
495         biblionumber     => $biblionumber2,
496         order            => $order3,
497         quantityreceived => 2,
498         invoice          => $invoice,
499         budget_id        => $budgetid2,
500     }
501 );
502
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' );
511
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' );
517
518 $order3 = Koha::Acquisition::Orders->find( $ordernumbers[2] )->unblessed;
519 $order3->{order_internalnote} = "my third notes";
520 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
521     {
522         biblionumber     => $biblionumber2,
523         order            => $order3,
524         quantityreceived => 2,
525         invoice          => $invoice,
526         budget_id        => $budgetid2,
527     }
528 );
529
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' );
535
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' );
540
541 subtest 'ModOrder' => sub {
542     plan tests => 1;
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');
546 };
547
548 # Budget reports
549 my $all_count = scalar GetBudgetsReport();
550 ok($all_count >= 1, "GetBudgetReport OK");
551
552 my $active_count = scalar GetBudgetsReport(1);
553 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
554
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.");
557
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',
563     sub {
564         my ($self) = shift;
565
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();
572
573         return {
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 } ],
580         };
581       }
582 );
583
584 sub run_flavoured_tests {
585     my $marcflavour = shift;
586     t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
587
588     #
589     # Test SearchWithISBNVariations syspref
590     #
591     my $marc_record = MARC::Record->new;
592     $marc_record->append_fields( create_isbn_field( '9780136019701', $marcflavour ) );
593     my ( $biblionumber6, $biblioitemnumber6 ) = AddBiblio( $marc_record, '' );
594
595     # Create order
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",
602             quantity       => 1,
603             ecost          => 10,
604             rrp            => 10,
605             listprice      => 10,
606             ecost          => 10,
607             rrp            => 10,
608             discount       => 0,
609             uncertainprice => 0,
610     } )->store->ordernumber;
611
612     t::lib::Mocks::mock_preference('SearchWithISBNVariations', 0);
613     $orders = GetHistory( isbn => '0136019706' );
614     is( scalar(@$orders), 0, "GetHistory searches correctly by ISBN" );
615
616     t::lib::Mocks::mock_preference('SearchWithISBNVariations', 1);
617     $orders = GetHistory( isbn => '0136019706' );
618     is( scalar(@$orders), 1, "GetHistory searches correctly by ISBN" );
619
620     Koha::Acquisition::Orders->find($ordernumber)->cancel;
621
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, '' );
625
626     my $orders_issn = GetHistory( issn => '2434561X' );
627     is( scalar(@$orders_issn), 0, "Precheck that ISSN shouldn't be in database" );
628
629     # Create order
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",
636             quantity       => 1,
637             ecost          => 10,
638             rrp            => 10,
639             listprice      => 10,
640             ecost          => 10,
641             rrp            => 10,
642             discount       => 0,
643             uncertainprice => 0,
644     } )->store->ordernumber;
645
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" );
649
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" );
653
654     Koha::Acquisition::Orders->find($ordernumber_issn)->cancel;
655 }
656
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
659
660 # Do "flavoured" tests
661 subtest 'MARC21' => sub {
662     plan tests => 5;
663     run_flavoured_tests('MARC21');
664 };
665
666 subtest 'UNIMARC' => sub {
667     plan tests => 5;
668     run_flavoured_tests('UNIMARC');
669 };
670
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' );
675 }
676
677 sub get_isbn_field {
678     my $marc_flavour = C4::Context->preference('marcflavour');
679     return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
680 }
681
682 sub get_issn_field {
683     my $marc_flavour = C4::Context->preference('marcflavour');
684     return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
685 }
686
687 sub get_itemnumber_field {
688     my $marc_flavour = C4::Context->preference('marcflavour');
689     return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
690 }
691
692 sub create_isbn_field {
693     my ( $isbn, $marcflavour ) = @_;
694
695     my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
696     my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
697
698     # Add the price subfield
699     my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
700     $field->add_subfields( $price_subfield => '$100' );
701
702     return $field;
703 }
704
705 sub create_issn_field {
706     my ( $issn, $marcflavour ) = @_;
707
708     my ( $issn_field, $issn_subfield ) = get_issn_field();
709     my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
710
711     return $field;
712 }
713
714 subtest 'ModReceiveOrder replacementprice tests' => sub {
715     plan tests => 2;
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} } });
722     my $orderinfo ={
723         basketno => $order_basket->{basketno},
724         rrp => 19.99,
725         replacementprice => undef,
726         quantity => 1,
727         quantityreceived => 0,
728         datereceived => undef,
729         datecancellationprinted => undef,
730     };
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},
738     });
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},
749     });
750     $received_order = GetOrder($received_ordernumber);
751     is ($received_order->{replacementprice},'16.120000',"Replacement price set if none passed in");
752 };
753
754 subtest 'ModReceiveOrder and subscription' => sub {
755     plan tests => 2;
756
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(
761         {
762             class => 'Koha::Acquisition::Orders',
763             value => {
764                 subscriptionid     => $subscription->subscriptionid,
765                 order_internalnote => $first_note,
766                 quantity           => 5,
767                 quantityreceived   => 0,
768                 ecost_tax_excluded => 42,
769                 unitprice_tax_excluded => 42,
770             }
771         }
772     );
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(
778         {
779             biblionumber     => $order->biblionumber,
780             order            => $order_info,
781             invoice          => $order->{invoiceid},
782             quantityreceived => 1,
783             budget_id        => $order->budget_id,
784         }
785     );
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" );
789
790     is( $order->get_from_storage->order_internalnote, $first_note );
791 };
792
793 subtest 'ModReceiveOrder invoice_unitprice and invoice_currency' => sub {
794     plan tests => 2;
795
796     subtest 'partial order' => sub {
797         plan tests => 2;
798
799         subtest 'no invoice_unitprice' => sub {
800             plan tests => 4;
801             my $order = $builder->build_object(
802                 {
803                     class => 'Koha::Acquisition::Orders',
804                     value => {
805                         quantity               => 5,
806                         quantityreceived       => 0,
807                         ecost_tax_excluded     => 42,
808                         unitprice_tax_excluded => 42,
809                         unitprice              => 42,
810                     }
811                 }
812             );
813             my $order_info = {
814                 %{ $order->unblessed },
815                 invoice_unitprice => undef,
816                 invoice_currency  => undef,
817             };
818             my ( undef, $received_ordernumber ) = ModReceiveOrder(
819                 {
820                     biblionumber     => $order->biblionumber,
821                     order            => $order_info,
822                     quantityreceived => 1,                   # We receive only 1
823                     budget_id        => $order->budget_id,
824                 }
825             );
826             my $active_currency = Koha::Acquisition::Currencies->get_active;
827             my $received_order =
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' );
838         };
839         subtest 'with invoice_unitprice' => sub {
840             plan tests => 4;
841             my $order = $builder->build_object(
842                 {
843                     class => 'Koha::Acquisition::Orders',
844                     value => {
845                         quantity               => 5,
846                         quantityreceived       => 0,
847                         ecost_tax_excluded     => 42,
848                         unitprice_tax_excluded => 42,
849                         unitprice              => 42,
850                     }
851                 }
852             );
853             my $order_info = {
854                 %{ $order->unblessed },
855                 invoice_unitprice => 37,
856                 invoice_currency  => 'GBP',
857             };
858             my ( undef, $received_ordernumber ) = ModReceiveOrder(
859                 {
860                     biblionumber     => $order->biblionumber,
861                     order            => $order_info,
862                     quantityreceived => 1,
863                     budget_id        => $order->budget_id,
864                 }
865             );
866             my $received_order =
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' );
877
878         };
879     };
880
881     subtest 'full received order' => sub {
882         plan tests => 2;
883
884         subtest 'no invoice_unitprice' => sub {
885             plan tests => 4;
886             my $order   = $builder->build_object(
887                 {
888                     class => 'Koha::Acquisition::Orders',
889                     value => {
890                         quantity               => 5,
891                         quantityreceived       => 0,
892                         ecost_tax_excluded     => 42,
893                         unitprice_tax_excluded => 42,
894                         unitprice              => 42,
895                     }
896                 }
897             );
898             my $order_info = {
899                 %{ $order->unblessed },
900                 invoice_unitprice => undef,
901                 invoice_currency  => undef,
902             };
903             my ( undef, $received_ordernumber ) = ModReceiveOrder(
904                 {
905                     biblionumber => $order->biblionumber,
906                     order        => $order_info,
907                     quantityreceived => 5,                # We receive them all!
908                     budget_id        => $order->budget_id,
909                 }
910             );
911             my $active_currency = Koha::Acquisition::Currencies->get_active;
912             my $received_order =
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' );
923
924         };
925
926         subtest 'with invoice_unitprice' => sub {
927             plan tests => 4;
928             my $order = $builder->build_object(
929                 {
930                     class => 'Koha::Acquisition::Orders',
931                     value => {
932                         quantity               => 5,
933                         quantityreceived       => 0,
934                         ecost_tax_excluded     => 42,
935                         unitprice_tax_excluded => 42,
936                         unitprice              => 42,
937                     }
938                 }
939             );
940             my $order_info = {
941                 %{ $order->unblessed },
942                 invoice_unitprice => 37,
943                 invoice_currency  => 'GBP',
944             };
945             my ( undef, $received_ordernumber ) = ModReceiveOrder(
946                 {
947                     biblionumber     => $order->biblionumber,
948                     order            => $order_info,
949                     quantityreceived => 1,
950                     budget_id        => $order->budget_id,
951                 }
952             );
953             my $received_order =
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' );
964
965         };
966     };
967
968 };
969
970 subtest 'GetHistory status search' => sub {
971     plan tests => 3;
972
973     my $order_basket = $builder->build( { source => 'Aqbasket', value => { is_standing => 0 } } );
974     my $orderinfo    = {
975         basketno                => $order_basket->{basketno},
976         rrp                     => 19.99,
977         replacementprice        => undef,
978         quantity                => 1,
979         quantityreceived        => 0,
980         datereceived            => undef,
981         orderstatus             => 'cancelled',
982         datecancellationprinted => '1990-01-01',
983     };
984     my $order      = $builder->build( { source => 'Aqorder', value => $orderinfo } );
985     my $orderinfo2 = {
986         basketno                => $order_basket->{basketno},
987         rrp                     => 19.99,
988         replacementprice        => undef,
989         quantity                => 1,
990         quantityreceived        => 0,
991         datereceived            => undef,
992         orderstatus             => 'new',
993         datecancellationprinted => undef,
994     };
995     $order = $builder->build( { source => 'Aqorder', value => $orderinfo2 } );
996
997     $orders = GetHistory( order_status => "new", basket => $order_basket->{basketno} );
998     is( scalar(@$orders), 1, 'GetHistory with order status "new" returns 1 order' );
999
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' );
1002
1003     $orders = GetHistory( order_status => "", basket => $order_basket->{basketno} );
1004     is(
1005         scalar(@$orders), 1,
1006         'GetHistory with order status "all except cancelled" returns only order with status new (1)'
1007     );
1008 };
1009
1010 subtest 'GetHistory with additional fields' => sub {
1011     plan tests => 3;
1012     my $order_basket = $builder->build({ source => 'Aqbasket', value => { is_standing => 0 } });
1013     my $orderinfo ={
1014         basketno => $order_basket->{basketno},
1015         rrp => 19.99,
1016         replacementprice => undef,
1017         quantity => 1,
1018         quantityreceived => 0,
1019         datereceived => undef,
1020         datecancellationprinted => undef,
1021     };
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');
1025
1026     my $additional_field = $builder->build({source => 'AdditionalField', value => {
1027             tablename => 'aqbasket',
1028             name => 'snakeoil',
1029             authorised_value_category => "",
1030         }
1031     });
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',
1038     }]);
1039
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');
1042 };
1043
1044 subtest 'Tests for get_rounding_sql' => sub {
1045
1046     plan tests => 2;
1047
1048     my $value = '3.141592';
1049
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);
1054
1055     ok( $no_rounding_result eq $value, "Value ($value) not to be rounded" );
1056     ok( $rounding_result =~ /CAST/,    "Value ($value) will be rounded" );
1057
1058 };
1059
1060 subtest 'Test for get_rounded_price' => sub {
1061
1062     plan tests => 6;
1063
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 );
1069
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);
1078
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)" );
1085
1086 };
1087
1088 subtest 'GetHistory - managing library' => sub {
1089
1090     plan tests => 1;
1091
1092     my $orders = GetHistory(managing_library => 'CPL');
1093
1094     my $order_basket1 = $builder->build({ source => 'Aqbasket', value => { branch => 'CPL' } });
1095     my $orderinfo1 ={
1096         basketno => $order_basket1->{basketno},
1097         rrp => 19.99,
1098         replacementprice => undef,
1099         quantity => 1,
1100         quantityreceived => 0,
1101         datereceived => undef,
1102         datecancellationprinted => undef,
1103     };
1104     my $order1 = $builder->build({ source => 'Aqorder', value => $orderinfo1 });
1105
1106     my $order_basket2 = $builder->build({ source => 'Aqbasket', value => { branch => 'LIB' } });
1107     my $orderinfo2 ={
1108         basketno => $order_basket2->{basketno},
1109         rrp => 19.99,
1110         replacementprice => undef,
1111         quantity => 1,
1112         quantityreceived => 0,
1113         datereceived => undef,
1114         datecancellationprinted => undef,
1115     };
1116     my $order2 = $builder->build({ source => 'Aqorder', value => $orderinfo2 });
1117
1118     my $history = GetHistory(managing_library => 'CPL');
1119     is( scalar( @$history), scalar ( @$orders ) +1, "GetHistory returns number of orders");
1120
1121 };
1122
1123 subtest 'GetHistory - is_standing' => sub {
1124
1125     plan tests => 1;
1126
1127     my $orders = GetHistory( is_standing => '1' );
1128
1129     my $order_basket1 = $builder->build( { source => 'Aqbasket', value => { is_standing => 0 } } );
1130     my $orderinfo1 = {
1131         basketno                => $order_basket1->{basketno},
1132         rrp                     => 19.99,
1133         replacementprice        => undef,
1134         quantity                => 1,
1135         quantityreceived        => 0,
1136         datereceived            => undef,
1137         datecancellationprinted => undef,
1138     };
1139     my $order1 = $builder->build( { source => 'Aqorder', value => $orderinfo1 } );
1140
1141     my $order_basket2 = $builder->build( { source => 'Aqbasket', value => { is_standing => 1 } } );
1142     my $orderinfo2 = {
1143         basketno                => $order_basket2->{basketno},
1144         rrp                     => 19.99,
1145         replacementprice        => undef,
1146         quantity                => 1,
1147         quantityreceived        => 0,
1148         datereceived            => undef,
1149         datecancellationprinted => undef,
1150     };
1151     my $order2 = $builder->build( { source => 'Aqorder', value => $orderinfo2 } );
1152
1153     my $history = GetHistory( is_standing => 1 );
1154     is(
1155         scalar(@$history),
1156         scalar(@$orders) + 1,
1157         "GetHistory returns number of standing orders"
1158     );
1159
1160 };
1161
1162 subtest 'Acquisition logging' => sub {
1163
1164     plan tests => 5;
1165
1166     t::lib::Mocks::mock_preference('AcquisitionLog', 1);
1167
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');
1172
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');
1177
1178     Koha::ActionLogs->delete;
1179     C4::Acquisition::ModBasket({
1180         basketno => $basketno,
1181         booksellerid => $booksellerid
1182     });
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');
1185
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');
1190
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');
1195
1196     t::lib::Mocks::mock_preference('AcquisitionLog', 0);
1197 };
1198
1199 $schema->storage->txn_rollback();
1200
1201 subtest 'GetInvoices() tests with additional fields' => sub {
1202
1203     plan tests => 7;
1204
1205     $schema->storage->txn_begin;
1206
1207     my $invoice_1 = $builder->build_object(
1208         {
1209              class => 'Koha::Acquisition::Invoices',
1210              value => {
1211                 invoicenumber => 'whataretheodds1'
1212              }
1213         }
1214     );
1215     my $invoice_2 = $builder->build_object(
1216         {
1217              class => 'Koha::Acquisition::Invoices',
1218              value => {
1219                 invoicenumber => 'whataretheodds2'
1220              }
1221         }
1222     );
1223
1224
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 );
1229
1230     my $additional_field_1 = $builder->build_object(
1231         {   class => 'Koha::AdditionalFields',
1232             value => {
1233                 tablename                 => 'aqinvoices',
1234                 authorised_value_category => "",
1235             }
1236         }
1237     );
1238
1239     my $additional_field_2 = $builder->build_object(
1240         {   class => 'Koha::AdditionalFields',
1241             value => {
1242                 tablename                 => 'aqinvoices',
1243                 authorised_value_category => "",
1244             }
1245         }
1246     );
1247
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" } ]);
1250
1251     $invoices = [ GetInvoices(
1252         invoicenumber => 'whataretheodds',
1253         additional_fields => [{ id => $additional_field_1->id, value => 'Ya-Hey' }]
1254     )];
1255     is( scalar @{$invoices}, 1, 'One invoice retrieved' );
1256     is( $invoices->[0]->{invoiceid}, $invoice_1->id, 'Ya-Hey' );
1257
1258     $invoices = [ GetInvoices(
1259         invoicenumber => 'whataretheodds',
1260         additional_fields => [{ id => $additional_field_2->id, value => "Hey ho let's go" }]
1261     )];
1262     is( scalar @{$invoices}, 1, 'One invoice retrieved' );
1263     is( $invoices->[0]->{invoiceid}, $invoice_2->id, "Hey ho let's go" );
1264
1265     $schema->storage->txn_rollback;
1266 };