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