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