Bug 11169: Display acquisition details in the OPAC
[koha.git] / t / db_dependent / Acquisition.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use Modern::Perl;
7 use POSIX qw(strftime);
8
9 use C4::Bookseller qw( GetBookSellerFromId );
10
11 use Test::More tests => 77;
12
13 BEGIN {
14     use_ok('C4::Acquisition');
15     use_ok('C4::Bookseller');
16     use_ok('C4::Biblio');
17     use_ok('C4::Budgets');
18     use_ok('C4::Bookseller');
19 }
20
21 # Sub used for testing C4::Acquisition subs returning order(s):
22 #    GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
23 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
24 #  _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
25 # params :
26 # $exp_fields             : arrayref whose elements are the keys we expect to find
27 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
28 #                           containing content fields of the order created with NewOrder
29 # $order_to_check         : hashref whose keys/values are the content of an order
30 #                           returned by the C4::Acquisition sub we are testing
31 # returns :
32 # \@test_missing_fields   : arrayref void if ok ; otherwise contains the list of
33 #                           fields missing in $order_to_check
34 # \@test_extra_fields     : arrayref void if ok ; otherwise contains the list of
35 #                           fields unexpected in $order_to_check
36 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
37 #                           fields which value is not the same in between $order_to_check and
38 # $test_nbr_fields        : contains the number of fields of $order_to_check
39
40 sub _check_fields_of_order {
41     my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
42     my @test_missing_fields   = ();
43     my @test_extra_fields     = ();
44     my @test_different_fields = ();
45     my $test_nbr_fields       = scalar( keys %$order_to_check );
46     foreach my $field (@$exp_fields) {
47         push @test_missing_fields, $field
48           unless exists( $order_to_check->{$field} );
49     }
50     foreach my $field ( keys %$order_to_check ) {
51         push @test_extra_fields, $field
52           unless grep ( /^$field$/, @$exp_fields );
53     }
54     foreach my $field ( keys %{ $original_order_content->{str} } ) {
55         push @test_different_fields, $field
56           unless ( !exists $order_to_check->{$field} )
57           or ( $original_order_content->{str}->{$field} eq
58             $order_to_check->{$field} );
59     }
60     foreach my $field ( keys %{ $original_order_content->{num} } ) {
61         push @test_different_fields, $field
62           unless ( !exists $order_to_check->{$field} )
63           or ( $original_order_content->{num}->{$field} ==
64             $order_to_check->{$field} );
65     }
66     return (
67         \@test_missing_fields,   \@test_extra_fields,
68         \@test_different_fields, $test_nbr_fields
69     );
70 }
71
72 # Sub used for testing C4::Acquisition subs returning several orders
73 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
74 #   _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
75 sub _check_fields_of_orders {
76     my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
77     my @test_missing_fields   = ();
78     my @test_extra_fields     = ();
79     my @test_different_fields = ();
80     my @test_nbr_fields       = ();
81     foreach my $order_to_check (@$orders_to_check) {
82         my $original_order_content =
83           ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
84               @$original_orders_content )[0];
85         my (
86             $t_missing_fields,   $t_extra_fields,
87             $t_different_fields, $t_nbr_fields
88           )
89           = _check_fields_of_order( $exp_fields, $original_order_content,
90             $order_to_check );
91         push @test_missing_fields,   @$t_missing_fields;
92         push @test_extra_fields,     @$t_extra_fields;
93         push @test_different_fields, @$t_different_fields;
94         push @test_nbr_fields,       $t_nbr_fields;
95     }
96     @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
97     @test_extra_fields   = keys %{ { map { $_ => 1 } @test_extra_fields } };
98     @test_different_fields =
99       keys %{ { map { $_ => 1 } @test_different_fields } };
100     return (
101         \@test_missing_fields,   \@test_extra_fields,
102         \@test_different_fields, \@test_nbr_fields
103     );
104 }
105
106 my $dbh = C4::Context->dbh;
107 $dbh->{AutoCommit} = 0;
108 $dbh->{RaiseError} = 1;
109
110 # Creating some orders
111 my $booksellerid = C4::Bookseller::AddBookseller(
112     {
113         name         => "my vendor",
114         address1     => "bookseller's address",
115         phone        => "0123456",
116         active       => 1,
117         deliverytime => 5,
118     }
119 );
120
121 my $booksellerinfo = C4::Bookseller::GetBookSellerFromId($booksellerid);
122
123 is( $booksellerinfo->{deliverytime},
124     5, 'set deliverytime when creating vendor (Bug 10556)' );
125
126 my ( $basket, $basketno );
127 ok(
128     $basketno = NewBasket( $booksellerid, 1 ),
129     "NewBasket(  $booksellerid , 1  ) returns $basketno"
130 );
131 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
132
133 my $budgetid = C4::Budgets::AddBudget(
134     {
135         budget_code => "budget_code_test_getordersbybib",
136         budget_name => "budget_name_test_getordersbybib",
137     }
138 );
139 my $budget = C4::Budgets::GetBudget($budgetid);
140
141 my @ordernumbers;
142 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
143 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
144 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
145 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
146
147 #
148 # Test NewOrder
149 #
150
151 my ( $mandatoryparams, $return_error, $basketnum );
152
153 # returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing
154 eval { ( $basketnum, $ordernumbers[0] ) = C4::Acquisition::NewOrder() };
155 $return_error = $@;
156 ok(
157     ( !( defined $basketnum || defined $ordernumbers[0] ) )
158       && ( defined $return_error ),
159     "NewOrder with no params returns undef and croaks"
160 );
161
162 $mandatoryparams = {
163     basketno     => $basketno,
164     quantity     => 24,
165     biblionumber => $biblionumber1,
166     budget_id    => $budget->{budget_id},
167 };
168 my @mandatoryparams_keys = keys %$mandatoryparams;
169 foreach my $mandatoryparams_key (@mandatoryparams_keys) {
170     my %test_missing_mandatoryparams = %$mandatoryparams;
171     delete $test_missing_mandatoryparams{$mandatoryparams_key};
172     eval {
173         ( $basketnum, $ordernumbers[0] ) =
174           C4::Acquisition::NewOrder( \%test_missing_mandatoryparams );
175     };
176     $return_error = $@;
177     my $expected_error = "Mandatory parameter $mandatoryparams_key missing";
178     ok(
179         ( !( defined $basketnum || defined $ordernumbers[0] ) )
180           && ( index( $return_error, $expected_error ) >= 0 ),
181 "NewOrder with no $mandatoryparams_key returns undef and croaks with expected error message"
182     );
183 }
184
185 # FIXME to do : test the other features of NewOrder
186
187 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
188 # Ex : a price of 50.1 will be stored internally as 5.100000
189
190 my @order_content = (
191     {
192         str => {
193             basketno       => $basketno,
194             biblionumber   => $biblionumber1,
195             budget_id      => $budget->{budget_id},
196             uncertainprice => 0,
197             order_internalnote => "internal note",
198             order_vendornote   => "vendor note"
199         },
200         num => {
201             quantity  => 24,
202             listprice => 50.121111,
203             ecost     => 38.15,
204             rrp       => 40.15,
205             discount  => 5.1111,
206             gstrate   => 0.0515
207         }
208     },
209     {
210         str => {
211             basketno     => $basketno,
212             biblionumber => $biblionumber2,
213             budget_id    => $budget->{budget_id}
214         },
215         num => { quantity => 42 }
216     },
217     {
218         str => {
219             basketno       => $basketno,
220             biblionumber   => $biblionumber2,
221             budget_id      => $budget->{budget_id},
222             uncertainprice => 0,
223             order_internalnote => "internal note",
224             order_vendornote   => "vendor note"
225         },
226         num => {
227             quantity  => 4,
228             ecost     => 42.1,
229             rrp       => 42.1,
230             listprice => 10.1,
231             ecost     => 38.1,
232             rrp       => 11.0,
233             discount  => 5.1,
234             gstrate   => 0.1
235         }
236     },
237     {
238         str => {
239             basketno     => $basketno,
240             biblionumber => $biblionumber3,
241             budget_id    => $budget->{budget_id},
242             order_internalnote => "internal note",
243             order_vendornote   => "vendor note"
244         },
245         num => {
246             quantity       => 4,
247             ecost          => 40,
248             rrp            => 42,
249             listprice      => 10,
250             ecost          => 38.15,
251             rrp            => 11.00,
252             discount       => 0,
253             uncertainprice => 0,
254             gstrate        => 0
255         }
256     },
257     {
258         str => {
259             basketno     => $basketno,
260             biblionumber => $biblionumber4,
261             budget_id    => $budget->{budget_id},
262             order_internalnote => "internal note",
263             order_vendornote   => "vendor note"
264         },
265         num => {
266             quantity       => 1,
267             ecost          => 10,
268             rrp            => 10,
269             listprice      => 10,
270             ecost          => 10,
271             rrp            => 10,
272             discount       => 0,
273             uncertainprice => 0,
274             gstrate        => 0
275         }
276     }
277 );
278
279 # Create 4 orders in database
280 for ( 0 .. 4 ) {
281     my %ocontent;
282     @ocontent{ keys %{ $order_content[$_]->{num} } } =
283       values %{ $order_content[$_]->{num} };
284     @ocontent{ keys %{ $order_content[$_]->{str} } } =
285       values %{ $order_content[$_]->{str} };
286     ( undef, $ordernumbers[$_] ) = C4::Acquisition::NewOrder( \%ocontent );
287     $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
288 }
289
290 # Test UT sub _check_fields_of_order
291
292 my (
293     $test_missing_fields,   $test_extra_fields,
294     $test_different_fields, $test_nbr_fields
295   )
296   = _check_fields_of_order(
297     [qw /a b c d e/],
298     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
299     { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
300   );
301 ok(
302     (
303               ( $test_nbr_fields == 5 )
304           and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
305           and ( join( " ", sort @$test_extra_fields )   eq 'f g' )
306           and ( join( " ", @$test_different_fields )    eq 'a' )
307     ),
308     "_check_fields_of_order can check an order (test 1)"
309 );
310 (
311     $test_missing_fields,   $test_extra_fields,
312     $test_different_fields, $test_nbr_fields
313   )
314   = _check_fields_of_order(
315     [qw /a b c /],
316     { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
317     { a => "bla", b => "105", c => 15 }
318   );
319 ok(
320     (
321               ( $test_nbr_fields == 3 )
322           and ( scalar @$test_missing_fields == 0 )
323           and ( scalar @$test_extra_fields == 0 )
324           and ( scalar @$test_different_fields == 0 )
325     ),
326     "_check_fields_of_order can check an order (test 2)"
327 );
328 (
329     $test_missing_fields,   $test_extra_fields,
330     $test_different_fields, $test_nbr_fields
331   )
332   = _check_fields_of_order(
333     [qw /a b c d e/],
334     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
335     { a => "blabla", b => "105", c => 15, d => "error" }
336   );
337 ok(
338     (
339               ( $test_nbr_fields == 4 )
340           and ( join( " ", sort @$test_missing_fields ) eq 'e' )
341           and ( scalar @$test_extra_fields == 0 )
342           and ( join( " ", @$test_different_fields ) eq 'a c' )
343     ),
344     "_check_fields_of_order can check an order (test 3)"
345 );
346
347 #
348 # test GetOrder
349 #
350
351 my @expectedfields = qw(
352   order_internalnote
353   order_vendornote
354   ordernumber
355   biblionumber
356   entrydate
357   quantity
358   currency
359   listprice
360   totalamount
361   datereceived
362   invoiceid
363   freight
364   unitprice
365   quantityreceived
366   cancelledby
367   datecancellationprinted
368   supplierreference
369   purchaseordernumber
370   basketno
371   timestamp
372   rrp
373   ecost
374   unitpricesupplier
375   unitpricelib
376   gstrate
377   discount
378   budget_id
379   budgetgroup_id
380   budgetdate
381   sort1
382   sort2
383   sort1_authcat
384   sort2_authcat
385   uncertainprice
386   claims_count
387   claimed_date
388   subscriptionid
389   parent_ordernumber
390   orderstatus
391   title
392   author
393   basketname
394   branchcode
395   publicationyear
396   copyrightdate
397   editionstatement
398   isbn
399   ean
400   seriestitle
401   publishercode
402   publisher
403   budget
404   supplier
405   supplierid
406   estimateddeliverydate
407   orderdate
408   quantity_to_receive
409   subtotal
410   latesince
411 );
412 (
413     $test_missing_fields,   $test_extra_fields,
414     $test_different_fields, $test_nbr_fields
415   )
416   = _check_fields_of_order( \@expectedfields, $order_content[0],
417     GetOrder( $ordernumbers[0] ) );
418 is(
419     $test_nbr_fields,
420     scalar @expectedfields,
421     "GetOrder gets an order with the right number of fields"
422 );
423 is( join( " ", @$test_missing_fields ),
424     '', "GetOrder gets an order with no missing fields" );
425 is( join( " ", @$test_extra_fields ),
426     '', "GetOrder gets an order with no unexpected fields" );
427 is( join( " ", @$test_different_fields ),
428     '', "GetOrder gets an order with the right content in every fields" );
429
430 #
431 # Test GetOrders
432 #
433
434 my @base_expectedfields = qw(
435   order_internalnote
436   order_vendornote
437   notes
438   ordernumber
439   ecost
440   uncertainprice
441   marc
442   cancelledby
443   url
444   isbn
445   copyrightdate
446   serial
447   cn_suffix
448   cn_item
449   marcxml
450   freight
451   cn_class
452   title
453   pages
454   budget_encumb
455   budget_name
456   number
457   itemtype
458   totalissues
459   author
460   budget_permission
461   parent_ordernumber
462   size
463   claims_count
464   currency
465   seriestitle
466   timestamp
467   editionstatement
468   budget_parent_id
469   publishercode
470   unitprice
471   collectionvolume
472   budget_amount
473   budget_owner_id
474   datecreated
475   claimed_date
476   subscriptionid
477   editionresponsibility
478   sort2
479   volumedate
480   budget_id
481   illus
482   ean
483   biblioitemnumber
484   datereceived
485   orderstatus
486   supplierreference
487   agerestriction
488   budget_branchcode
489   gstrate
490   listprice
491   budget_code
492   budgetdate
493   basketno
494   discount
495   abstract
496   collectionissn
497   publicationyear
498   collectiontitle
499   invoiceid
500   budgetgroup_id
501   place
502   issn
503   quantityreceived
504   entrydate
505   cn_source
506   sort1_authcat
507   budget_notes
508   biblionumber
509   unititle
510   sort2_authcat
511   budget_expend
512   rrp
513   cn_sort
514   totalamount
515   lccn
516   sort1
517   volume
518   purchaseordernumber
519   quantity
520   budget_period_id
521   frameworkcode
522   volumedesc
523   datecancellationprinted
524 );
525 @expectedfields =
526   ( @base_expectedfields,
527     ( 'transferred_from_timestamp', 'transferred_from' ) );
528 is( GetOrders(), undef, "GetOrders with no params returns undef" );
529 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
530 my @get_orders = GetOrders($basketno);
531 (
532     $test_missing_fields,   $test_extra_fields,
533     $test_different_fields, $test_nbr_fields
534   )
535   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
536 is(
537     $$test_nbr_fields[0],
538     scalar @expectedfields,
539     "GetOrders gets orders with the right number of fields"
540 );
541 is( join( " ", @$test_missing_fields ),
542     '', "GetOrders gets orders with no missing fields" );
543 is( join( " ", @$test_extra_fields ),
544     '', "GetOrders gets orders with no unexpected fields" );
545 is( join( " ", @$test_different_fields ),
546     '', "GetOrders gets orders with the right content in every fields" );
547 ok(
548     (
549         ( scalar @get_orders == 4 )
550           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
551     ),
552     "GetOrders only gets non-cancelled orders"
553 );
554
555 #
556 # Test GetCancelledOrders
557 #
558
559 @expectedfields =
560   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
561 is( GetCancelledOrders(), undef,
562     "GetCancelledOrders with no params returns undef" );
563 @get_orders = GetCancelledOrders($basketno);
564 (
565     $test_missing_fields,   $test_extra_fields,
566     $test_different_fields, $test_nbr_fields
567   )
568   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
569 is(
570     $$test_nbr_fields[0],
571     scalar @expectedfields,
572     "GetCancelledOrders gets orders with the right number of fields"
573 );
574 is( join( " ", @$test_missing_fields ),
575     '', "GetCancelledOrders gets orders with no missing fields" );
576 is( join( " ", @$test_extra_fields ),
577     '', "GetCancelledOrders gets orders with no unexpected fields" );
578 is( join( " ", @$test_different_fields ),
579     '',
580     "GetCancelledOrders gets orders with the right content in every fields" );
581 ok(
582     (
583         ( scalar @get_orders == 1 )
584           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
585     ),
586     "GetCancelledOrders only gets cancelled orders"
587 );
588
589 #
590 # Test SearchOrders
591 #
592
593 @expectedfields = qw (
594   order_internalnote
595   order_vendornote
596   notes
597   basketgroupid
598   basketgroupname
599   firstname
600   biblioitemnumber
601   ecost
602   uncertainprice
603   creationdate
604   datereceived
605   orderstatus
606   supplierreference
607   cancelledby
608   isbn
609   copyrightdate
610   gstrate
611   serial
612   listprice
613   budgetdate
614   basketno
615   discount
616   surname
617   freight
618   abstract
619   title
620   closedate
621   basketname
622   budgetgroup_id
623   invoiceid
624   author
625   parent_ordernumber
626   claims_count
627   entrydate
628   currency
629   quantityreceived
630   seriestitle
631   sort1_authcat
632   timestamp
633   biblionumber
634   unititle
635   sort2_authcat
636   rrp
637   unitprice
638   totalamount
639   sort1
640   ordernumber
641   datecreated
642   purchaseordernumber
643   quantity
644   claimed_date
645   subscriptionid
646   frameworkcode
647   sort2
648   datecancellationprinted
649   budget_id
650   authorisedby
651   booksellerid
652 );
653
654 # note that authorisedby was added to the return of SearchOrder by the
655 # patch for bug 11777
656
657 my $invoiceid = AddInvoice(
658     invoicenumber => 'invoice',
659     booksellerid  => $booksellerid,
660     unknown       => "unknown"
661 );
662
663 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
664     {
665         biblionumber      => $biblionumber4,
666         ordernumber       => $ordernumbers[4],
667         quantityreceived  => 1,
668         cost              => 10,
669         ecost             => 10,
670         invoiceid         => $invoiceid,
671         rrp               => 10,
672         budget_id          => $order_content[4]->{str}->{budget_id},
673     }
674 );
675
676 my $search_orders = SearchOrders({
677     booksellerid => $booksellerid,
678     basketno     => $basketno
679 });
680 isa_ok( $search_orders, 'ARRAY' );
681 (
682     $test_missing_fields,   $test_extra_fields,
683     $test_different_fields, $test_nbr_fields
684   )
685   = _check_fields_of_orders( \@expectedfields, \@order_content,
686     $search_orders );
687 is(
688     $$test_nbr_fields[0],
689     scalar @expectedfields,
690     "SearchOrders gets orders with the right number of fields"
691 );
692 is( join( " ", @$test_missing_fields ),
693     '', "SearchOrders gets orders with no missing fields" );
694 is( join( " ", @$test_extra_fields ),
695     '', "SearchOrders gets orders with no unexpected fields" );
696 is( join( " ", @$test_different_fields ),
697     '', "SearchOrders gets orders with the right content in every fields" );
698 ok(
699     (
700         ( scalar @$search_orders == 4 )
701           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
702     ),
703     "SearchOrders only gets non-cancelled orders"
704 );
705
706 $search_orders = SearchOrders({
707     booksellerid => $booksellerid,
708     basketno     => $basketno,
709     pending      => 1
710 });
711 ok(
712     (
713         ( scalar @$search_orders == 3 ) and !grep ( (
714                      ( $_->{ordernumber} eq $ordernumbers[3] )
715                   or ( $_->{ordernumber} eq $ordernumbers[4] )
716             ),
717             @$search_orders )
718     ),
719     "SearchOrders with pending params gets only pending orders (bug 10723)"
720 );
721
722 $search_orders = SearchOrders({
723     booksellerid => $booksellerid,
724     basketno     => $basketno,
725     pending      => 1,
726     ordered      => 1,
727 });
728 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
729
730
731 #
732 # Test GetBudgetByOrderNumber
733 #
734 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
735     "GetBudgetByOrderNumber returns expected budget" );
736
737 #
738 # Test GetLateOrders
739 #
740
741 @expectedfields = qw (
742   orderdate
743   author
744   budget
745   supplierid
746   claims_count
747   supplier
748   publisher
749   ordernumber
750   quantity
751   basketno
752   claimed_date
753   branch
754   estimateddeliverydate
755   title
756   publicationyear
757   unitpricelib
758   unitpricesupplier
759   subtotal
760   latesince
761   basketname
762   basketgroupid
763   basketgroupname
764 );
765 my @lateorders = GetLateOrders(0);
766 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
767     0, "GetLateOrders does not get orders from opened baskets" );
768 C4::Acquisition::CloseBasket($basketno);
769 @lateorders = GetLateOrders(0);
770 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
771     0, "GetLateOrders gets orders from closed baskets" );
772 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
773     "GetLateOrders does not gets cancelled orders" );
774 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
775     "GetLateOrders does not gets reveived orders" );
776 (
777     $test_missing_fields,   $test_extra_fields,
778     $test_different_fields, $test_nbr_fields
779   )
780   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
781 is(
782     $$test_nbr_fields[0],
783     scalar @expectedfields,
784     "GetLateOrders gets orders with the right number of fields"
785 );
786 is( join( " ", @$test_missing_fields ),
787     '', "GetLateOrders gets orders with no missing fields" );
788 is( join( " ", @$test_extra_fields ),
789     '', "GetLateOrders gets orders with no unexpected fields" );
790 is( join( " ", @$test_different_fields ),
791     '', "GetLateOrders gets orders with the right content in every fields" );
792
793 $search_orders = SearchOrders({
794     booksellerid => $booksellerid,
795     basketno     => $basketno,
796     pending      => 1,
797     ordered      => 1,
798 });
799 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)" );
800
801 $search_orders = SearchOrders({
802     ordernumber => $ordernumbers[4]
803 });
804 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
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 $dbh->rollback;