Bug 11224: (follow-up) apply tidying
[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 => 64;
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 );
643
644 # note that authorisedby was added to the return of SearchOrder by the
645 # patch for bug 11777
646
647 my $invoiceid = AddInvoice(
648     invoicenumber => 'invoice',
649     booksellerid  => $booksellerid,
650     unknown       => "unknown"
651 );
652
653 my ( $datereceived, $new_ordernumber ) =
654   ModReceiveOrder( $biblionumber4, $ordernumbers[4], 1, undef, 10, 10,
655     $invoiceid, 10, $order_content[4]->{str}->{budget_id} );
656
657 my $search_orders = SearchOrders({
658     booksellerid => $booksellerid,
659     basketno     => $basketno
660 });
661 isa_ok( $search_orders, 'ARRAY' );
662 (
663     $test_missing_fields,   $test_extra_fields,
664     $test_different_fields, $test_nbr_fields
665   )
666   = _check_fields_of_orders( \@expectedfields, \@order_content,
667     $search_orders );
668 is(
669     $$test_nbr_fields[0],
670     scalar @expectedfields,
671     "SearchOrders gets orders with the right number of fields"
672 );
673 is( join( " ", @$test_missing_fields ),
674     '', "SearchOrders gets orders with no missing fields" );
675 is( join( " ", @$test_extra_fields ),
676     '', "SearchOrders gets orders with no unexpected fields" );
677 is( join( " ", @$test_different_fields ),
678     '', "SearchOrders gets orders with the right content in every fields" );
679 ok(
680     (
681         ( scalar @$search_orders == 4 )
682           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
683     ),
684     "SearchOrders only gets non-cancelled orders"
685 );
686
687 $search_orders = SearchOrders({
688     booksellerid => $booksellerid,
689     basketno     => $basketno,
690     pending      => 1
691 });
692 ok(
693     (
694         ( scalar @$search_orders == 3 ) and !grep ( (
695                      ( $_->{ordernumber} eq $ordernumbers[3] )
696                   or ( $_->{ordernumber} eq $ordernumbers[4] )
697             ),
698             @$search_orders )
699     ),
700     "SearchOrders with pending params gets only pending orders"
701 );
702
703 #
704 # Test GetBudgetByOrderNumber
705 #
706
707 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
708     "GetBudgetByOrderNumber returns expected budget" );
709
710 #
711 # Test GetLateOrders
712 #
713
714 @expectedfields = qw (
715   orderdate
716   author
717   budget
718   supplierid
719   claims_count
720   supplier
721   publisher
722   ordernumber
723   quantity
724   basketno
725   claimed_date
726   branch
727   estimateddeliverydate
728   title
729   publicationyear
730   unitpricelib
731   unitpricesupplier
732   subtotal
733   latesince
734 );
735 my @lateorders = GetLateOrders(0);
736 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
737     0, "GetLateOrders does not get orders from opened baskets" );
738 C4::Acquisition::CloseBasket($basketno);
739 @lateorders = GetLateOrders(0);
740 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
741     0, "GetLateOrders gets orders from closed baskets" );
742 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
743     "GetLateOrders does not gets cancelled orders" );
744 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
745     "GetLateOrders does not gets reveived orders" );
746 (
747     $test_missing_fields,   $test_extra_fields,
748     $test_different_fields, $test_nbr_fields
749   )
750   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
751 is(
752     $$test_nbr_fields[0],
753     scalar @expectedfields,
754     "GetLateOrders gets orders with the right number of fields"
755 );
756 is( join( " ", @$test_missing_fields ),
757     '', "GetLateOrders gets orders with no missing fields" );
758 is( join( " ", @$test_extra_fields ),
759     '', "GetLateOrders gets orders with no unexpected fields" );
760 is( join( " ", @$test_different_fields ),
761     '', "GetLateOrders gets orders with the right content in every fields" );
762
763 #
764 # Test AddClaim
765 #
766
767 my $order = $lateorders[0];
768 AddClaim( $order->{ordernumber} );
769 my $neworder = GetOrder( $order->{ordernumber} );
770 is(
771     $neworder->{claimed_date},
772     strftime( "%Y-%m-%d", localtime(time) ),
773     "AddClaim : Check claimed_date"
774 );
775
776 my $firstorder = $search_orders->[0];
777
778 # fake receiving the order
779 ModOrder({
780     ordernumber      => $firstorder->{ordernumber},
781     biblionumber     => $firstorder->{biblionumber},
782     quantityreceived => $firstorder->{quantity},
783 });
784
785 ( $datereceived, $new_ordernumber ) =
786   ModReceiveOrder( $biblionumber2, $ordernumbers[1], 2, undef, 12, 12,
787     $invoiceid, 42, );
788 my $order2 = GetOrder( $ordernumbers[1] );
789 is( $order2->{'quantityreceived'},
790     0, 'Splitting up order did not receive any on original order' );
791 is( $order2->{'quantity'}, 40, '40 items on original order' );
792 is( $order2->{'budget_id'}, $budgetid,
793     'Budget on original order is unchanged' );
794
795 $neworder = GetOrder($new_ordernumber);
796 is( $neworder->{'quantity'}, 2, '2 items on new order' );
797 is( $neworder->{'quantityreceived'},
798     2, 'Splitting up order received items on new order' );
799 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
800
801 my $budgetid2 = C4::Budgets::AddBudget(
802     {
803         budget_code => "budget_code_test_modrecv",
804         budget_name => "budget_name_test_modrecv",
805     }
806 );
807
808 ( $datereceived, $new_ordernumber ) =
809   ModReceiveOrder( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
810     $invoiceid, 42, $budgetid2 );
811
812 my $order3 = GetOrder( $ordernumbers[2] );
813 is( $order3->{'quantityreceived'},
814     0, 'Splitting up order did not receive any on original order' );
815 is( $order3->{'quantity'}, 2, '2 items on original order' );
816 is( $order3->{'budget_id'}, $budgetid,
817     'Budget on original order is unchanged' );
818
819 $neworder = GetOrder($new_ordernumber);
820 is( $neworder->{'quantity'}, 2, '2 items on new order' );
821 is( $neworder->{'quantityreceived'},
822     2, 'Splitting up order received items on new order' );
823 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
824
825 ( $datereceived, $new_ordernumber ) =
826   ModReceiveOrder( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
827     $invoiceid, 42, $budgetid2 );
828
829 $order3 = GetOrder( $ordernumbers[2] );
830 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
831 is( $order3->{'quantity'},         2,          '2 items on order' );
832 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
833
834 $dbh->rollback;