Bug 16426: Add tests for ModMember - do not update userid
[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 => 91;
23 use Koha::Database;
24
25 BEGIN {
26     use_ok('C4::Acquisition');
27     use_ok('C4::Bookseller');
28     use_ok('C4::Biblio');
29     use_ok('C4::Budgets');
30     use_ok('C4::Bookseller');
31     use_ok('Koha::Acquisition::Order');
32     use_ok('Koha::Acquisition::Bookseller');
33 }
34
35 # Sub used for testing C4::Acquisition subs returning order(s):
36 #    GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
37 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
38 #  _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
39 # params :
40 # $exp_fields             : arrayref whose elements are the keys we expect to find
41 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
42 #                           containing content fields of the order created with Koha::Acquisition::Order
43 # $order_to_check         : hashref whose keys/values are the content of an order
44 #                           returned by the C4::Acquisition sub we are testing
45 # returns :
46 # \@test_missing_fields   : arrayref void if ok ; otherwise contains the list of
47 #                           fields missing in $order_to_check
48 # \@test_extra_fields     : arrayref void if ok ; otherwise contains the list of
49 #                           fields unexpected in $order_to_check
50 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
51 #                           fields which value is not the same in between $order_to_check and
52 # $test_nbr_fields        : contains the number of fields of $order_to_check
53
54 sub _check_fields_of_order {
55     my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
56     my @test_missing_fields   = ();
57     my @test_extra_fields     = ();
58     my @test_different_fields = ();
59     my $test_nbr_fields       = scalar( keys %$order_to_check );
60     foreach my $field (@$exp_fields) {
61         push @test_missing_fields, $field
62           unless exists( $order_to_check->{$field} );
63     }
64     foreach my $field ( keys %$order_to_check ) {
65         push @test_extra_fields, $field
66           unless grep ( /^$field$/, @$exp_fields );
67     }
68     foreach my $field ( keys %{ $original_order_content->{str} } ) {
69         push @test_different_fields, $field
70           unless ( !exists $order_to_check->{$field} )
71           or ( $original_order_content->{str}->{$field} eq
72             $order_to_check->{$field} );
73     }
74     foreach my $field ( keys %{ $original_order_content->{num} } ) {
75         push @test_different_fields, $field
76           unless ( !exists $order_to_check->{$field} )
77           or ( $original_order_content->{num}->{$field} ==
78             $order_to_check->{$field} );
79     }
80     return (
81         \@test_missing_fields,   \@test_extra_fields,
82         \@test_different_fields, $test_nbr_fields
83     );
84 }
85
86 # Sub used for testing C4::Acquisition subs returning several orders
87 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
88 #   _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
89 sub _check_fields_of_orders {
90     my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
91     my @test_missing_fields   = ();
92     my @test_extra_fields     = ();
93     my @test_different_fields = ();
94     my @test_nbr_fields       = ();
95     foreach my $order_to_check (@$orders_to_check) {
96         my $original_order_content =
97           ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
98               @$original_orders_content )[0];
99         my (
100             $t_missing_fields,   $t_extra_fields,
101             $t_different_fields, $t_nbr_fields
102           )
103           = _check_fields_of_order( $exp_fields, $original_order_content,
104             $order_to_check );
105         push @test_missing_fields,   @$t_missing_fields;
106         push @test_extra_fields,     @$t_extra_fields;
107         push @test_different_fields, @$t_different_fields;
108         push @test_nbr_fields,       $t_nbr_fields;
109     }
110     @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
111     @test_extra_fields   = keys %{ { map { $_ => 1 } @test_extra_fields } };
112     @test_different_fields =
113       keys %{ { map { $_ => 1 } @test_different_fields } };
114     return (
115         \@test_missing_fields,   \@test_extra_fields,
116         \@test_different_fields, \@test_nbr_fields
117     );
118 }
119
120
121 my $schema = Koha::Database->new()->schema();
122 $schema->storage->txn_begin();
123
124 my $dbh = C4::Context->dbh;
125 $dbh->{RaiseError} = 1;
126
127 # Creating some orders
128 my $booksellerid = C4::Bookseller::AddBookseller(
129     {
130         name         => "my vendor",
131         address1     => "bookseller's address",
132         phone        => "0123456",
133         active       => 1,
134         deliverytime => 5,
135     }
136 );
137
138 my $booksellerinfo = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
139
140 is( $booksellerinfo->{deliverytime},
141     5, 'set deliverytime when creating vendor (Bug 10556)' );
142
143 my ( $basket, $basketno );
144 ok(
145     $basketno = NewBasket( $booksellerid, 1 ),
146     "NewBasket(  $booksellerid , 1  ) returns $basketno"
147 );
148 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
149
150 my $bpid=AddBudgetPeriod({
151         budget_period_startdate => '2008-01-01'
152         , budget_period_enddate => '2008-12-31'
153         , budget_period_active  => 1
154         , budget_period_description    => "MAPERI"
155 });
156
157 my $budgetid = C4::Budgets::AddBudget(
158     {
159         budget_code => "budget_code_test_getordersbybib",
160         budget_name => "budget_name_test_getordersbybib",
161         budget_period_id => $bpid,
162     }
163 );
164 my $budget = C4::Budgets::GetBudget($budgetid);
165
166 my @ordernumbers;
167 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
168 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
169 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
170 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
171
172 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
173 # Ex : a price of 50.1 will be stored internally as 5.100000
174
175 my @order_content = (
176     {
177         str => {
178             basketno       => $basketno,
179             biblionumber   => $biblionumber1,
180             budget_id      => $budget->{budget_id},
181             uncertainprice => 0,
182             order_internalnote => "internal note",
183             order_vendornote   => "vendor note",
184             ordernumber => '',
185         },
186         num => {
187             quantity  => 24,
188             listprice => 50.121111,
189             ecost     => 38.15,
190             rrp       => 40.15,
191             discount  => 5.1111,
192             gstrate   => 0.0515
193         }
194     },
195     {
196         str => {
197             basketno     => $basketno,
198             biblionumber => $biblionumber2,
199             budget_id    => $budget->{budget_id}
200         },
201         num => { quantity => 42 }
202     },
203     {
204         str => {
205             basketno       => $basketno,
206             biblionumber   => $biblionumber2,
207             budget_id      => $budget->{budget_id},
208             uncertainprice => 0,
209             order_internalnote => "internal note",
210             order_vendornote   => "vendor note"
211         },
212         num => {
213             quantity  => 4,
214             ecost     => 42.1,
215             rrp       => 42.1,
216             listprice => 10.1,
217             ecost     => 38.1,
218             rrp       => 11.0,
219             discount  => 5.1,
220             gstrate   => 0.1
221         }
222     },
223     {
224         str => {
225             basketno     => $basketno,
226             biblionumber => $biblionumber3,
227             budget_id    => $budget->{budget_id},
228             order_internalnote => "internal note",
229             order_vendornote   => "vendor note"
230         },
231         num => {
232             quantity       => 4,
233             ecost          => 40,
234             rrp            => 42,
235             listprice      => 10,
236             ecost          => 38.15,
237             rrp            => 11.00,
238             discount       => 0,
239             uncertainprice => 0,
240             gstrate        => 0
241         }
242     },
243     {
244         str => {
245             basketno     => $basketno,
246             biblionumber => $biblionumber4,
247             budget_id    => $budget->{budget_id},
248             order_internalnote => "internal note",
249             order_vendornote   => "vendor note"
250         },
251         num => {
252             quantity       => 1,
253             ecost          => 10,
254             rrp            => 10,
255             listprice      => 10,
256             ecost          => 10,
257             rrp            => 10,
258             discount       => 0,
259             uncertainprice => 0,
260             gstrate        => 0
261         }
262     }
263 );
264
265 # Create 4 orders in database
266 for ( 0 .. 4 ) {
267     my %ocontent;
268     @ocontent{ keys %{ $order_content[$_]->{num} } } =
269       values %{ $order_content[$_]->{num} };
270     @ocontent{ keys %{ $order_content[$_]->{str} } } =
271       values %{ $order_content[$_]->{str} };
272     $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
273     $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
274 }
275
276 # Test UT sub _check_fields_of_order
277
278 my (
279     $test_missing_fields,   $test_extra_fields,
280     $test_different_fields, $test_nbr_fields
281   )
282   = _check_fields_of_order(
283     [qw /a b c d e/],
284     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
285     { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
286   );
287 ok(
288     (
289               ( $test_nbr_fields == 5 )
290           and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
291           and ( join( " ", sort @$test_extra_fields )   eq 'f g' )
292           and ( join( " ", @$test_different_fields )    eq 'a' )
293     ),
294     "_check_fields_of_order can check an order (test 1)"
295 );
296 (
297     $test_missing_fields,   $test_extra_fields,
298     $test_different_fields, $test_nbr_fields
299   )
300   = _check_fields_of_order(
301     [qw /a b c /],
302     { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
303     { a => "bla", b => "105", c => 15 }
304   );
305 ok(
306     (
307               ( $test_nbr_fields == 3 )
308           and ( scalar @$test_missing_fields == 0 )
309           and ( scalar @$test_extra_fields == 0 )
310           and ( scalar @$test_different_fields == 0 )
311     ),
312     "_check_fields_of_order can check an order (test 2)"
313 );
314 (
315     $test_missing_fields,   $test_extra_fields,
316     $test_different_fields, $test_nbr_fields
317   )
318   = _check_fields_of_order(
319     [qw /a b c d e/],
320     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
321     { a => "blabla", b => "105", c => 15, d => "error" }
322   );
323 ok(
324     (
325               ( $test_nbr_fields == 4 )
326           and ( join( " ", sort @$test_missing_fields ) eq 'e' )
327           and ( scalar @$test_extra_fields == 0 )
328           and ( join( " ", @$test_different_fields ) eq 'a c' )
329     ),
330     "_check_fields_of_order can check an order (test 3)"
331 );
332
333 #
334 # test GetOrder
335 #
336
337 my @expectedfields = qw(
338   order_internalnote
339   order_vendornote
340   ordernumber
341   biblionumber
342   entrydate
343   quantity
344   currency
345   listprice
346   datereceived
347   invoiceid
348   freight
349   unitprice
350   quantityreceived
351   datecancellationprinted
352   purchaseordernumber
353   basketno
354   timestamp
355   rrp
356   ecost
357   unitpricesupplier
358   unitpricelib
359   gstrate
360   discount
361   budget_id
362   budgetgroup_id
363   budgetdate
364   sort1
365   sort2
366   sort1_authcat
367   sort2_authcat
368   uncertainprice
369   claims_count
370   claimed_date
371   subscriptionid
372   parent_ordernumber
373   orderstatus
374   line_item_id
375   suppliers_reference_number
376   suppliers_reference_qualifier
377   suppliers_report
378   title
379   author
380   basketname
381   branchcode
382   publicationyear
383   copyrightdate
384   editionstatement
385   isbn
386   ean
387   seriestitle
388   publishercode
389   publisher
390   budget
391   supplier
392   supplierid
393   estimateddeliverydate
394   orderdate
395   quantity_to_receive
396   subtotal
397   latesince
398   cancellationreason
399 );
400 (
401     $test_missing_fields,   $test_extra_fields,
402     $test_different_fields, $test_nbr_fields
403   )
404   = _check_fields_of_order( \@expectedfields, $order_content[0],
405     GetOrder( $ordernumbers[0] ) );
406 is(
407     $test_nbr_fields,
408     scalar @expectedfields,
409     "GetOrder gets an order with the right number of fields"
410 );
411 is( join( " ", @$test_missing_fields ),
412     '', "GetOrder gets an order with no missing fields" );
413 is( join( " ", @$test_extra_fields ),
414     '', "GetOrder gets an order with no unexpected fields" );
415 is( join( " ", @$test_different_fields ),
416     '', "GetOrder gets an order with the right content in every fields" );
417
418 #
419 # Test GetOrders
420 #
421
422 my @base_expectedfields = qw(
423   order_internalnote
424   order_vendornote
425   notes
426   ordernumber
427   ecost
428   uncertainprice
429   marc
430   url
431   isbn
432   copyrightdate
433   serial
434   cn_suffix
435   cn_item
436   marcxml
437   freight
438   cn_class
439   title
440   pages
441   budget_encumb
442   budget_name
443   number
444   itemtype
445   totalissues
446   author
447   budget_permission
448   parent_ordernumber
449   size
450   claims_count
451   currency
452   seriestitle
453   timestamp
454   editionstatement
455   budget_parent_id
456   publishercode
457   unitprice
458   collectionvolume
459   budget_amount
460   budget_owner_id
461   datecreated
462   claimed_date
463   subscriptionid
464   editionresponsibility
465   sort2
466   volumedate
467   budget_id
468   illus
469   ean
470   biblioitemnumber
471   datereceived
472   orderstatus
473   line_item_id
474   suppliers_reference_number
475   suppliers_reference_qualifier
476   suppliers_report
477   agerestriction
478   budget_branchcode
479   gstrate
480   listprice
481   budget_code
482   budgetdate
483   basketno
484   discount
485   abstract
486   collectionissn
487   publicationyear
488   collectiontitle
489   invoiceid
490   budgetgroup_id
491   place
492   issn
493   quantityreceived
494   entrydate
495   cn_source
496   sort1_authcat
497   budget_notes
498   biblionumber
499   unititle
500   sort2_authcat
501   budget_expend
502   rrp
503   cn_sort
504   lccn
505   sort1
506   volume
507   purchaseordernumber
508   quantity
509   budget_period_id
510   frameworkcode
511   volumedesc
512   datecancellationprinted
513   cancellationreason
514 );
515 @expectedfields =
516   ( @base_expectedfields,
517     ( 'transferred_from_timestamp', 'transferred_from' ) );
518 is( GetOrders(), undef, "GetOrders with no params returns undef" );
519 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
520 my @get_orders = GetOrders($basketno);
521 (
522     $test_missing_fields,   $test_extra_fields,
523     $test_different_fields, $test_nbr_fields
524   )
525   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
526 is(
527     $$test_nbr_fields[0],
528     scalar @expectedfields,
529     "GetOrders gets orders with the right number of fields"
530 );
531 is( join( " ", @$test_missing_fields ),
532     '', "GetOrders gets orders with no missing fields" );
533 is( join( " ", @$test_extra_fields ),
534     '', "GetOrders gets orders with no unexpected fields" );
535 is( join( " ", @$test_different_fields ),
536     '', "GetOrders gets orders with the right content in every fields" );
537 ok(
538     (
539         ( scalar @get_orders == 4 )
540           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
541     ),
542     "GetOrders only gets non-cancelled orders"
543 );
544
545 #
546 # Test GetOrders { cancelled => 1 }
547 #
548
549 @expectedfields =
550   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
551 @get_orders = GetOrders($basketno, { cancelled => 1 });
552 (
553     $test_missing_fields,   $test_extra_fields,
554     $test_different_fields, $test_nbr_fields
555   )
556   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
557 is(
558     $$test_nbr_fields[0],
559     scalar @expectedfields,
560     "GetOrders { cancelled => 1 } gets orders with the right number of fields"
561 );
562 is( join( " ", @$test_missing_fields ),
563     '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
564 is( join( " ", @$test_extra_fields ),
565     '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
566 is( join( " ", @$test_different_fields ),
567     '',
568     "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
569 ok(
570     (
571         ( scalar @get_orders == 1 )
572           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
573     ),
574     "GetOrders { cancelled => 1 } only gets cancelled orders"
575 );
576
577 #
578 # Test SearchOrders
579 #
580
581 @expectedfields = qw (
582   order_internalnote
583   order_vendornote
584   notes
585   basketgroupid
586   basketgroupname
587   firstname
588   biblioitemnumber
589   ecost
590   uncertainprice
591   creationdate
592   datereceived
593   orderstatus
594   line_item_id
595   suppliers_reference_number
596   suppliers_reference_qualifier
597   suppliers_report
598   isbn
599   copyrightdate
600   gstrate
601   serial
602   listprice
603   budgetdate
604   basketno
605   discount
606   surname
607   freight
608   abstract
609   title
610   closedate
611   basketname
612   budgetgroup_id
613   invoiceid
614   author
615   parent_ordernumber
616   claims_count
617   entrydate
618   currency
619   quantityreceived
620   seriestitle
621   sort1_authcat
622   timestamp
623   biblionumber
624   unititle
625   sort2_authcat
626   rrp
627   unitprice
628   sort1
629   ordernumber
630   datecreated
631   purchaseordernumber
632   quantity
633   claimed_date
634   subscriptionid
635   frameworkcode
636   sort2
637   datecancellationprinted
638   budget_id
639   authorisedby
640   booksellerid
641   cancellationreason
642 );
643
644 # note that authorisedby was added to the return of SearchOrder by the
645 # patch for bug 11777
646
647 my $invoiceid = AddInvoice(
648     invoicenumber => 'invoice',
649     booksellerid  => $booksellerid,
650     unknown       => "unknown"
651 );
652
653 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
654     {
655         biblionumber      => $biblionumber4,
656         ordernumber       => $ordernumbers[4],
657         quantityreceived  => 1,
658         cost              => 10,
659         ecost             => 10,
660         invoiceid         => $invoiceid,
661         rrp               => 10,
662         budget_id          => $order_content[4]->{str}->{budget_id},
663     }
664 );
665
666 my $search_orders = SearchOrders({
667     booksellerid => $booksellerid,
668     basketno     => $basketno
669 });
670 isa_ok( $search_orders, 'ARRAY' );
671 (
672     $test_missing_fields,   $test_extra_fields,
673     $test_different_fields, $test_nbr_fields
674   )
675   = _check_fields_of_orders( \@expectedfields, \@order_content,
676     $search_orders );
677 is(
678     $$test_nbr_fields[0],
679     scalar @expectedfields,
680     "SearchOrders gets orders with the right number of fields"
681 );
682 is( join( " ", @$test_missing_fields ),
683     '', "SearchOrders gets orders with no missing fields" );
684 is( join( " ", @$test_extra_fields ),
685     '', "SearchOrders gets orders with no unexpected fields" );
686 is( join( " ", @$test_different_fields ),
687     '', "SearchOrders gets orders with the right content in every fields" );
688 ok(
689     (
690         ( scalar @$search_orders == 4 )
691           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
692     ),
693     "SearchOrders only gets non-cancelled orders"
694 );
695
696 $search_orders = SearchOrders({
697     booksellerid => $booksellerid,
698     basketno     => $basketno,
699     pending      => 1
700 });
701 ok(
702     (
703         ( scalar @$search_orders == 3 ) and !grep ( (
704                      ( $_->{ordernumber} eq $ordernumbers[3] )
705                   or ( $_->{ordernumber} eq $ordernumbers[4] )
706             ),
707             @$search_orders )
708     ),
709     "SearchOrders with pending params gets only pending orders (bug 10723)"
710 );
711
712 $search_orders = SearchOrders({
713     booksellerid => $booksellerid,
714     basketno     => $basketno,
715     pending      => 1,
716     ordered      => 1,
717 });
718 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
719
720 $search_orders = SearchOrders({
721     ordernumber => $ordernumbers[4]
722 });
723 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
724
725 $search_orders = SearchOrders({
726     biblionumber => $biblionumber4
727 });
728 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
729
730 $search_orders = SearchOrders({
731     biblionumber => $biblionumber4,
732     pending      => 1
733 });
734 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
735
736 #
737 # Test GetBudgetByOrderNumber
738 #
739 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
740     "GetBudgetByOrderNumber returns expected budget" );
741
742 #
743 # Test GetLateOrders
744 #
745
746 @expectedfields = qw (
747   orderdate
748   author
749   budget
750   supplierid
751   claims_count
752   supplier
753   publisher
754   ordernumber
755   quantity
756   basketno
757   claimed_date
758   branch
759   estimateddeliverydate
760   title
761   publicationyear
762   unitpricelib
763   unitpricesupplier
764   subtotal
765   latesince
766   basketname
767   basketgroupid
768   basketgroupname
769 );
770 my @lateorders = GetLateOrders(0);
771 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
772     0, "GetLateOrders does not get orders from opened baskets" );
773 C4::Acquisition::CloseBasket($basketno);
774 @lateorders = GetLateOrders(0);
775 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
776     0, "GetLateOrders gets orders from closed baskets" );
777 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
778     "GetLateOrders does not gets cancelled orders" );
779 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
780     "GetLateOrders does not gets reveived orders" );
781 (
782     $test_missing_fields,   $test_extra_fields,
783     $test_different_fields, $test_nbr_fields
784   )
785   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
786 is(
787     $$test_nbr_fields[0],
788     scalar @expectedfields,
789     "GetLateOrders gets orders with the right number of fields"
790 );
791 is( join( " ", @$test_missing_fields ),
792     '', "GetLateOrders gets orders with no missing fields" );
793 is( join( " ", @$test_extra_fields ),
794     '', "GetLateOrders gets orders with no unexpected fields" );
795 is( join( " ", @$test_different_fields ),
796     '', "GetLateOrders gets orders with the right content in every fields" );
797
798 $search_orders = SearchOrders({
799     booksellerid => $booksellerid,
800     basketno     => $basketno,
801     pending      => 1,
802     ordered      => 1,
803 });
804 is( scalar (@$search_orders), 3, "SearchOrders with pending and ordered params gets only pending ordered orders. After closing the basket, orders are marked as 'ordered' (bug 11170)" );
805
806 #
807 # Test AddClaim
808 #
809
810 my $order = $lateorders[0];
811 AddClaim( $order->{ordernumber} );
812 my $neworder = GetOrder( $order->{ordernumber} );
813 is(
814     $neworder->{claimed_date},
815     strftime( "%Y-%m-%d", localtime(time) ),
816     "AddClaim : Check claimed_date"
817 );
818
819 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
820     {
821         biblionumber     => $biblionumber2,
822         ordernumber      => $ordernumbers[1],
823         quantityreceived => 2,
824         cost             => 12,
825         ecost            => 12,
826         invoiceid        => $invoiceid,
827         rrp              => 42,
828         order_internalnote => "my notes",
829         order_vendornote   => "my vendor notes",
830     }
831 );
832 my $order2 = GetOrder( $ordernumbers[1] );
833 is( $order2->{'quantityreceived'},
834     0, 'Splitting up order did not receive any on original order' );
835 is( $order2->{'quantity'}, 40, '40 items on original order' );
836 is( $order2->{'budget_id'}, $budgetid,
837     'Budget on original order is unchanged' );
838 is( $order2->{order_internalnote}, "my notes",
839     'ModReceiveOrder and GetOrder deal with internal notes' );
840 is( $order2->{order_vendornote}, "my vendor notes",
841     'ModReceiveOrder and GetOrder deal with vendor notes' );
842
843 $neworder = GetOrder($new_ordernumber);
844 is( $neworder->{'quantity'}, 2, '2 items on new order' );
845 is( $neworder->{'quantityreceived'},
846     2, 'Splitting up order received items on new order' );
847 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
848
849 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
850 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
851
852 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
853 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
854 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
855 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
856
857 my $budgetid2 = C4::Budgets::AddBudget(
858     {
859         budget_code => "budget_code_test_modrecv",
860         budget_name => "budget_name_test_modrecv",
861     }
862 );
863
864 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
865     {
866         biblionumber     => $biblionumber2,
867         ordernumber      => $ordernumbers[2],
868         quantityreceived => 2,
869         cost             => 12,
870         ecost            => 12,
871         invoiceid        => $invoiceid,
872         rrp              => 42,
873         budget_id        => $budgetid2,
874         order_internalnote => "my other notes",
875     }
876 );
877
878 my $order3 = GetOrder( $ordernumbers[2] );
879 is( $order3->{'quantityreceived'},
880     0, 'Splitting up order did not receive any on original order' );
881 is( $order3->{'quantity'}, 2, '2 items on original order' );
882 is( $order3->{'budget_id'}, $budgetid,
883     'Budget on original order is unchanged' );
884 is( $order3->{order_internalnote}, "my other notes",
885     'ModReceiveOrder and GetOrder deal with notes' );
886
887 $neworder = GetOrder($new_ordernumber);
888 is( $neworder->{'quantity'}, 2, '2 items on new order' );
889 is( $neworder->{'quantityreceived'},
890     2, 'Splitting up order received items on new order' );
891 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
892
893 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
894     {
895         biblionumber     => $biblionumber2,
896         ordernumber      => $ordernumbers[2],
897         quantityreceived => 2,
898         cost             => 12,
899         ecost            => 12,
900         invoiceid        => $invoiceid,
901         rrp              => 42,
902         budget_id        => $budgetid2,
903         order_internalnote => "my third notes",
904     }
905 );
906
907 $order3 = GetOrder( $ordernumbers[2] );
908 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
909 is( $order3->{'quantity'},         2,          '2 items on order' );
910 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
911 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
912
913 my $nonexistent_order = GetOrder();
914 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
915 $nonexistent_order = GetOrder( 424242424242 );
916 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
917
918 # Tests for DelOrder
919 my $order1 = GetOrder($ordernumbers[0]);
920 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
921 ok((not defined $error), "DelOrder does not fail");
922 $order1 = GetOrder($order1->{ordernumber});
923 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
924 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
925 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
926
927 $order2 = GetOrder($ordernumbers[1]);
928 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
929 ok((not defined $error), "DelOrder does not fail");
930 $order2 = GetOrder($order2->{ordernumber});
931 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
932 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
933 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
934
935 my $order4 = GetOrder($ordernumbers[3]);
936 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
937 ok((not defined $error), "DelOrder does not fail");
938 $order4 = GetOrder($order4->{ordernumber});
939 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
940 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
941 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
942 # End of tests for DelOrder
943
944 # Budget reports
945 my $all_count = scalar GetBudgetsReport();
946 ok($all_count >= 1, "GetBudgetReport OK");
947
948 my $active_count = scalar GetBudgetsReport(1);
949 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
950
951 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
952 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
953
954 $schema->storage->txn_rollback();