Bug 12827: NewOrder should not return basketno
[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 => 79;
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 { $ordernumbers[0] = C4::Acquisition::NewOrder() };
155 $return_error = $@;
156 ok(
157     ( ! 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         $ordernumbers[0] =
174           C4::Acquisition::NewOrder( \%test_missing_mandatoryparams );
175     };
176     $return_error = $@;
177     my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is 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             ordernumber => '',
200         },
201         num => {
202             quantity  => 24,
203             listprice => 50.121111,
204             ecost     => 38.15,
205             rrp       => 40.15,
206             discount  => 5.1111,
207             gstrate   => 0.0515
208         }
209     },
210     {
211         str => {
212             basketno     => $basketno,
213             biblionumber => $biblionumber2,
214             budget_id    => $budget->{budget_id}
215         },
216         num => { quantity => 42 }
217     },
218     {
219         str => {
220             basketno       => $basketno,
221             biblionumber   => $biblionumber2,
222             budget_id      => $budget->{budget_id},
223             uncertainprice => 0,
224             order_internalnote => "internal note",
225             order_vendornote   => "vendor note"
226         },
227         num => {
228             quantity  => 4,
229             ecost     => 42.1,
230             rrp       => 42.1,
231             listprice => 10.1,
232             ecost     => 38.1,
233             rrp       => 11.0,
234             discount  => 5.1,
235             gstrate   => 0.1
236         }
237     },
238     {
239         str => {
240             basketno     => $basketno,
241             biblionumber => $biblionumber3,
242             budget_id    => $budget->{budget_id},
243             order_internalnote => "internal note",
244             order_vendornote   => "vendor note"
245         },
246         num => {
247             quantity       => 4,
248             ecost          => 40,
249             rrp            => 42,
250             listprice      => 10,
251             ecost          => 38.15,
252             rrp            => 11.00,
253             discount       => 0,
254             uncertainprice => 0,
255             gstrate        => 0
256         }
257     },
258     {
259         str => {
260             basketno     => $basketno,
261             biblionumber => $biblionumber4,
262             budget_id    => $budget->{budget_id},
263             order_internalnote => "internal note",
264             order_vendornote   => "vendor note"
265         },
266         num => {
267             quantity       => 1,
268             ecost          => 10,
269             rrp            => 10,
270             listprice      => 10,
271             ecost          => 10,
272             rrp            => 10,
273             discount       => 0,
274             uncertainprice => 0,
275             gstrate        => 0
276         }
277     }
278 );
279
280 # Create 4 orders in database
281 for ( 0 .. 4 ) {
282     my %ocontent;
283     @ocontent{ keys %{ $order_content[$_]->{num} } } =
284       values %{ $order_content[$_]->{num} };
285     @ocontent{ keys %{ $order_content[$_]->{str} } } =
286       values %{ $order_content[$_]->{str} };
287     $ordernumbers[$_] = C4::Acquisition::NewOrder( \%ocontent );
288     $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
289 }
290
291 # Test UT sub _check_fields_of_order
292
293 my (
294     $test_missing_fields,   $test_extra_fields,
295     $test_different_fields, $test_nbr_fields
296   )
297   = _check_fields_of_order(
298     [qw /a b c d e/],
299     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
300     { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
301   );
302 ok(
303     (
304               ( $test_nbr_fields == 5 )
305           and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
306           and ( join( " ", sort @$test_extra_fields )   eq 'f g' )
307           and ( join( " ", @$test_different_fields )    eq 'a' )
308     ),
309     "_check_fields_of_order can check an order (test 1)"
310 );
311 (
312     $test_missing_fields,   $test_extra_fields,
313     $test_different_fields, $test_nbr_fields
314   )
315   = _check_fields_of_order(
316     [qw /a b c /],
317     { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
318     { a => "bla", b => "105", c => 15 }
319   );
320 ok(
321     (
322               ( $test_nbr_fields == 3 )
323           and ( scalar @$test_missing_fields == 0 )
324           and ( scalar @$test_extra_fields == 0 )
325           and ( scalar @$test_different_fields == 0 )
326     ),
327     "_check_fields_of_order can check an order (test 2)"
328 );
329 (
330     $test_missing_fields,   $test_extra_fields,
331     $test_different_fields, $test_nbr_fields
332   )
333   = _check_fields_of_order(
334     [qw /a b c d e/],
335     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
336     { a => "blabla", b => "105", c => 15, d => "error" }
337   );
338 ok(
339     (
340               ( $test_nbr_fields == 4 )
341           and ( join( " ", sort @$test_missing_fields ) eq 'e' )
342           and ( scalar @$test_extra_fields == 0 )
343           and ( join( " ", @$test_different_fields ) eq 'a c' )
344     ),
345     "_check_fields_of_order can check an order (test 3)"
346 );
347
348 #
349 # test GetOrder
350 #
351
352 my @expectedfields = qw(
353   order_internalnote
354   order_vendornote
355   ordernumber
356   biblionumber
357   entrydate
358   quantity
359   currency
360   listprice
361   totalamount
362   datereceived
363   invoiceid
364   freight
365   unitprice
366   quantityreceived
367   cancelledby
368   datecancellationprinted
369   supplierreference
370   purchaseordernumber
371   basketno
372   timestamp
373   rrp
374   ecost
375   unitpricesupplier
376   unitpricelib
377   gstrate
378   discount
379   budget_id
380   budgetgroup_id
381   budgetdate
382   sort1
383   sort2
384   sort1_authcat
385   sort2_authcat
386   uncertainprice
387   claims_count
388   claimed_date
389   subscriptionid
390   parent_ordernumber
391   orderstatus
392   title
393   author
394   basketname
395   branchcode
396   publicationyear
397   copyrightdate
398   editionstatement
399   isbn
400   ean
401   seriestitle
402   publishercode
403   publisher
404   budget
405   supplier
406   supplierid
407   estimateddeliverydate
408   orderdate
409   quantity_to_receive
410   subtotal
411   latesince
412 );
413 (
414     $test_missing_fields,   $test_extra_fields,
415     $test_different_fields, $test_nbr_fields
416   )
417   = _check_fields_of_order( \@expectedfields, $order_content[0],
418     GetOrder( $ordernumbers[0] ) );
419 is(
420     $test_nbr_fields,
421     scalar @expectedfields,
422     "GetOrder gets an order with the right number of fields"
423 );
424 is( join( " ", @$test_missing_fields ),
425     '', "GetOrder gets an order with no missing fields" );
426 is( join( " ", @$test_extra_fields ),
427     '', "GetOrder gets an order with no unexpected fields" );
428 is( join( " ", @$test_different_fields ),
429     '', "GetOrder gets an order with the right content in every fields" );
430
431 #
432 # Test GetOrders
433 #
434
435 my @base_expectedfields = qw(
436   order_internalnote
437   order_vendornote
438   notes
439   ordernumber
440   ecost
441   uncertainprice
442   marc
443   cancelledby
444   url
445   isbn
446   copyrightdate
447   serial
448   cn_suffix
449   cn_item
450   marcxml
451   freight
452   cn_class
453   title
454   pages
455   budget_encumb
456   budget_name
457   number
458   itemtype
459   totalissues
460   author
461   budget_permission
462   parent_ordernumber
463   size
464   claims_count
465   currency
466   seriestitle
467   timestamp
468   editionstatement
469   budget_parent_id
470   publishercode
471   unitprice
472   collectionvolume
473   budget_amount
474   budget_owner_id
475   datecreated
476   claimed_date
477   subscriptionid
478   editionresponsibility
479   sort2
480   volumedate
481   budget_id
482   illus
483   ean
484   biblioitemnumber
485   datereceived
486   orderstatus
487   supplierreference
488   agerestriction
489   budget_branchcode
490   gstrate
491   listprice
492   budget_code
493   budgetdate
494   basketno
495   discount
496   abstract
497   collectionissn
498   publicationyear
499   collectiontitle
500   invoiceid
501   budgetgroup_id
502   place
503   issn
504   quantityreceived
505   entrydate
506   cn_source
507   sort1_authcat
508   budget_notes
509   biblionumber
510   unititle
511   sort2_authcat
512   budget_expend
513   rrp
514   cn_sort
515   totalamount
516   lccn
517   sort1
518   volume
519   purchaseordernumber
520   quantity
521   budget_period_id
522   frameworkcode
523   volumedesc
524   datecancellationprinted
525 );
526 @expectedfields =
527   ( @base_expectedfields,
528     ( 'transferred_from_timestamp', 'transferred_from' ) );
529 is( GetOrders(), undef, "GetOrders with no params returns undef" );
530 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
531 my @get_orders = GetOrders($basketno);
532 (
533     $test_missing_fields,   $test_extra_fields,
534     $test_different_fields, $test_nbr_fields
535   )
536   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
537 is(
538     $$test_nbr_fields[0],
539     scalar @expectedfields,
540     "GetOrders gets orders with the right number of fields"
541 );
542 is( join( " ", @$test_missing_fields ),
543     '', "GetOrders gets orders with no missing fields" );
544 is( join( " ", @$test_extra_fields ),
545     '', "GetOrders gets orders with no unexpected fields" );
546 is( join( " ", @$test_different_fields ),
547     '', "GetOrders gets orders with the right content in every fields" );
548 ok(
549     (
550         ( scalar @get_orders == 4 )
551           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
552     ),
553     "GetOrders only gets non-cancelled orders"
554 );
555
556 #
557 # Test GetCancelledOrders
558 #
559
560 @expectedfields =
561   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
562 is( GetCancelledOrders(), undef,
563     "GetCancelledOrders with no params returns undef" );
564 @get_orders = GetCancelledOrders($basketno);
565 (
566     $test_missing_fields,   $test_extra_fields,
567     $test_different_fields, $test_nbr_fields
568   )
569   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
570 is(
571     $$test_nbr_fields[0],
572     scalar @expectedfields,
573     "GetCancelledOrders gets orders with the right number of fields"
574 );
575 is( join( " ", @$test_missing_fields ),
576     '', "GetCancelledOrders gets orders with no missing fields" );
577 is( join( " ", @$test_extra_fields ),
578     '', "GetCancelledOrders gets orders with no unexpected fields" );
579 is( join( " ", @$test_different_fields ),
580     '',
581     "GetCancelledOrders gets orders with the right content in every fields" );
582 ok(
583     (
584         ( scalar @get_orders == 1 )
585           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
586     ),
587     "GetCancelledOrders only gets cancelled orders"
588 );
589
590 #
591 # Test SearchOrders
592 #
593
594 @expectedfields = qw (
595   order_internalnote
596   order_vendornote
597   notes
598   basketgroupid
599   basketgroupname
600   firstname
601   biblioitemnumber
602   ecost
603   uncertainprice
604   creationdate
605   datereceived
606   orderstatus
607   supplierreference
608   cancelledby
609   isbn
610   copyrightdate
611   gstrate
612   serial
613   listprice
614   budgetdate
615   basketno
616   discount
617   surname
618   freight
619   abstract
620   title
621   closedate
622   basketname
623   budgetgroup_id
624   invoiceid
625   author
626   parent_ordernumber
627   claims_count
628   entrydate
629   currency
630   quantityreceived
631   seriestitle
632   sort1_authcat
633   timestamp
634   biblionumber
635   unititle
636   sort2_authcat
637   rrp
638   unitprice
639   totalamount
640   sort1
641   ordernumber
642   datecreated
643   purchaseordernumber
644   quantity
645   claimed_date
646   subscriptionid
647   frameworkcode
648   sort2
649   datecancellationprinted
650   budget_id
651   authorisedby
652   booksellerid
653 );
654
655 # note that authorisedby was added to the return of SearchOrder by the
656 # patch for bug 11777
657
658 my $invoiceid = AddInvoice(
659     invoicenumber => 'invoice',
660     booksellerid  => $booksellerid,
661     unknown       => "unknown"
662 );
663
664 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
665     {
666         biblionumber      => $biblionumber4,
667         ordernumber       => $ordernumbers[4],
668         quantityreceived  => 1,
669         cost              => 10,
670         ecost             => 10,
671         invoiceid         => $invoiceid,
672         rrp               => 10,
673         budget_id          => $order_content[4]->{str}->{budget_id},
674     }
675 );
676
677 my $search_orders = SearchOrders({
678     booksellerid => $booksellerid,
679     basketno     => $basketno
680 });
681 isa_ok( $search_orders, 'ARRAY' );
682 (
683     $test_missing_fields,   $test_extra_fields,
684     $test_different_fields, $test_nbr_fields
685   )
686   = _check_fields_of_orders( \@expectedfields, \@order_content,
687     $search_orders );
688 is(
689     $$test_nbr_fields[0],
690     scalar @expectedfields,
691     "SearchOrders gets orders with the right number of fields"
692 );
693 is( join( " ", @$test_missing_fields ),
694     '', "SearchOrders gets orders with no missing fields" );
695 is( join( " ", @$test_extra_fields ),
696     '', "SearchOrders gets orders with no unexpected fields" );
697 is( join( " ", @$test_different_fields ),
698     '', "SearchOrders gets orders with the right content in every fields" );
699 ok(
700     (
701         ( scalar @$search_orders == 4 )
702           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
703     ),
704     "SearchOrders only gets non-cancelled orders"
705 );
706
707 $search_orders = SearchOrders({
708     booksellerid => $booksellerid,
709     basketno     => $basketno,
710     pending      => 1
711 });
712 ok(
713     (
714         ( scalar @$search_orders == 3 ) and !grep ( (
715                      ( $_->{ordernumber} eq $ordernumbers[3] )
716                   or ( $_->{ordernumber} eq $ordernumbers[4] )
717             ),
718             @$search_orders )
719     ),
720     "SearchOrders with pending params gets only pending orders (bug 10723)"
721 );
722
723 $search_orders = SearchOrders({
724     booksellerid => $booksellerid,
725     basketno     => $basketno,
726     pending      => 1,
727     ordered      => 1,
728 });
729 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
730
731 $search_orders = SearchOrders({
732     ordernumber => $ordernumbers[4]
733 });
734 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
735
736 $search_orders = SearchOrders({
737     biblionumber => $biblionumber4
738 });
739 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
740
741 $search_orders = SearchOrders({
742     biblionumber => $biblionumber4,
743     pending      => 1
744 });
745 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
746
747 #
748 # Test GetBudgetByOrderNumber
749 #
750 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
751     "GetBudgetByOrderNumber returns expected budget" );
752
753 #
754 # Test GetLateOrders
755 #
756
757 @expectedfields = qw (
758   orderdate
759   author
760   budget
761   supplierid
762   claims_count
763   supplier
764   publisher
765   ordernumber
766   quantity
767   basketno
768   claimed_date
769   branch
770   estimateddeliverydate
771   title
772   publicationyear
773   unitpricelib
774   unitpricesupplier
775   subtotal
776   latesince
777   basketname
778   basketgroupid
779   basketgroupname
780 );
781 my @lateorders = GetLateOrders(0);
782 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
783     0, "GetLateOrders does not get orders from opened baskets" );
784 C4::Acquisition::CloseBasket($basketno);
785 @lateorders = GetLateOrders(0);
786 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
787     0, "GetLateOrders gets orders from closed baskets" );
788 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
789     "GetLateOrders does not gets cancelled orders" );
790 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
791     "GetLateOrders does not gets reveived orders" );
792 (
793     $test_missing_fields,   $test_extra_fields,
794     $test_different_fields, $test_nbr_fields
795   )
796   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
797 is(
798     $$test_nbr_fields[0],
799     scalar @expectedfields,
800     "GetLateOrders gets orders with the right number of fields"
801 );
802 is( join( " ", @$test_missing_fields ),
803     '', "GetLateOrders gets orders with no missing fields" );
804 is( join( " ", @$test_extra_fields ),
805     '', "GetLateOrders gets orders with no unexpected fields" );
806 is( join( " ", @$test_different_fields ),
807     '', "GetLateOrders gets orders with the right content in every fields" );
808
809 $search_orders = SearchOrders({
810     booksellerid => $booksellerid,
811     basketno     => $basketno,
812     pending      => 1,
813     ordered      => 1,
814 });
815 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)" );
816
817 #
818 # Test AddClaim
819 #
820
821 my $order = $lateorders[0];
822 AddClaim( $order->{ordernumber} );
823 my $neworder = GetOrder( $order->{ordernumber} );
824 is(
825     $neworder->{claimed_date},
826     strftime( "%Y-%m-%d", localtime(time) ),
827     "AddClaim : Check claimed_date"
828 );
829
830 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
831     {
832         biblionumber     => $biblionumber2,
833         ordernumber      => $ordernumbers[1],
834         quantityreceived => 2,
835         cost             => 12,
836         ecost            => 12,
837         invoiceid        => $invoiceid,
838         rrp              => 42,
839         order_internalnote => "my notes",
840         order_vendornote   => "my vendor notes",
841     }
842 );
843 my $order2 = GetOrder( $ordernumbers[1] );
844 is( $order2->{'quantityreceived'},
845     0, 'Splitting up order did not receive any on original order' );
846 is( $order2->{'quantity'}, 40, '40 items on original order' );
847 is( $order2->{'budget_id'}, $budgetid,
848     'Budget on original order is unchanged' );
849 is( $order2->{order_internalnote}, "my notes",
850     'ModReceiveOrder and GetOrder deal with internal notes' );
851 is( $order2->{order_vendornote}, "my vendor notes",
852     'ModReceiveOrder and GetOrder deal with vendor notes' );
853
854 $neworder = GetOrder($new_ordernumber);
855 is( $neworder->{'quantity'}, 2, '2 items on new order' );
856 is( $neworder->{'quantityreceived'},
857     2, 'Splitting up order received items on new order' );
858 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
859
860 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
861 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
862
863 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
864 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
865 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
866 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
867
868 my $budgetid2 = C4::Budgets::AddBudget(
869     {
870         budget_code => "budget_code_test_modrecv",
871         budget_name => "budget_name_test_modrecv",
872     }
873 );
874
875 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
876     {
877         biblionumber     => $biblionumber2,
878         ordernumber      => $ordernumbers[2],
879         quantityreceived => 2,
880         cost             => 12,
881         ecost            => 12,
882         invoiceid        => $invoiceid,
883         rrp              => 42,
884         budget_id        => $budgetid2,
885         order_internalnote => "my other notes",
886     }
887 );
888
889 my $order3 = GetOrder( $ordernumbers[2] );
890 is( $order3->{'quantityreceived'},
891     0, 'Splitting up order did not receive any on original order' );
892 is( $order3->{'quantity'}, 2, '2 items on original order' );
893 is( $order3->{'budget_id'}, $budgetid,
894     'Budget on original order is unchanged' );
895 is( $order3->{order_internalnote}, "my other notes",
896     'ModReceiveOrder and GetOrder deal with notes' );
897
898 $neworder = GetOrder($new_ordernumber);
899 is( $neworder->{'quantity'}, 2, '2 items on new order' );
900 is( $neworder->{'quantityreceived'},
901     2, 'Splitting up order received items on new order' );
902 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
903
904 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
905     {
906         biblionumber     => $biblionumber2,
907         ordernumber      => $ordernumbers[2],
908         quantityreceived => 2,
909         cost             => 12,
910         ecost            => 12,
911         invoiceid        => $invoiceid,
912         rrp              => 42,
913         budget_id        => $budgetid2,
914         order_internalnote => "my third notes",
915     }
916 );
917
918 $order3 = GetOrder( $ordernumbers[2] );
919 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
920 is( $order3->{'quantity'},         2,          '2 items on order' );
921 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
922 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
923
924 my $nonexistent_order = GetOrder();
925 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
926 $nonexistent_order = GetOrder( 424242424242 );
927 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
928
929 $dbh->rollback;