Bug 7162: (follow-up) Add unit tests for DelOrder
[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 => 90;
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   cancellationreason
373 );
374 (
375     $test_missing_fields,   $test_extra_fields,
376     $test_different_fields, $test_nbr_fields
377   )
378   = _check_fields_of_order( \@expectedfields, $order_content[0],
379     GetOrder( $ordernumbers[0] ) );
380 is(
381     $test_nbr_fields,
382     scalar @expectedfields,
383     "GetOrder gets an order with the right number of fields"
384 );
385 is( join( " ", @$test_missing_fields ),
386     '', "GetOrder gets an order with no missing fields" );
387 is( join( " ", @$test_extra_fields ),
388     '', "GetOrder gets an order with no unexpected fields" );
389 is( join( " ", @$test_different_fields ),
390     '', "GetOrder gets an order with the right content in every fields" );
391
392 #
393 # Test GetOrders
394 #
395
396 my @base_expectedfields = qw(
397   order_internalnote
398   order_vendornote
399   notes
400   ordernumber
401   ecost
402   uncertainprice
403   marc
404   url
405   isbn
406   copyrightdate
407   serial
408   cn_suffix
409   cn_item
410   marcxml
411   freight
412   cn_class
413   title
414   pages
415   budget_encumb
416   budget_name
417   number
418   itemtype
419   totalissues
420   author
421   budget_permission
422   parent_ordernumber
423   size
424   claims_count
425   currency
426   seriestitle
427   timestamp
428   editionstatement
429   budget_parent_id
430   publishercode
431   unitprice
432   collectionvolume
433   budget_amount
434   budget_owner_id
435   datecreated
436   claimed_date
437   subscriptionid
438   editionresponsibility
439   sort2
440   volumedate
441   budget_id
442   illus
443   ean
444   biblioitemnumber
445   datereceived
446   orderstatus
447   supplierreference
448   agerestriction
449   budget_branchcode
450   gstrate
451   listprice
452   budget_code
453   budgetdate
454   basketno
455   discount
456   abstract
457   collectionissn
458   publicationyear
459   collectiontitle
460   invoiceid
461   budgetgroup_id
462   place
463   issn
464   quantityreceived
465   entrydate
466   cn_source
467   sort1_authcat
468   budget_notes
469   biblionumber
470   unititle
471   sort2_authcat
472   budget_expend
473   rrp
474   cn_sort
475   totalamount
476   lccn
477   sort1
478   volume
479   purchaseordernumber
480   quantity
481   budget_period_id
482   frameworkcode
483   volumedesc
484   datecancellationprinted
485   cancellationreason
486 );
487 @expectedfields =
488   ( @base_expectedfields,
489     ( 'transferred_from_timestamp', 'transferred_from' ) );
490 is( GetOrders(), undef, "GetOrders with no params returns undef" );
491 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
492 my @get_orders = GetOrders($basketno);
493 (
494     $test_missing_fields,   $test_extra_fields,
495     $test_different_fields, $test_nbr_fields
496   )
497   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
498 is(
499     $$test_nbr_fields[0],
500     scalar @expectedfields,
501     "GetOrders gets orders with the right number of fields"
502 );
503 is( join( " ", @$test_missing_fields ),
504     '', "GetOrders gets orders with no missing fields" );
505 is( join( " ", @$test_extra_fields ),
506     '', "GetOrders gets orders with no unexpected fields" );
507 is( join( " ", @$test_different_fields ),
508     '', "GetOrders gets orders with the right content in every fields" );
509 ok(
510     (
511         ( scalar @get_orders == 4 )
512           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
513     ),
514     "GetOrders only gets non-cancelled orders"
515 );
516
517 #
518 # Test GetCancelledOrders
519 #
520
521 @expectedfields =
522   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
523 is( GetCancelledOrders(), undef,
524     "GetCancelledOrders with no params returns undef" );
525 @get_orders = GetCancelledOrders($basketno);
526 (
527     $test_missing_fields,   $test_extra_fields,
528     $test_different_fields, $test_nbr_fields
529   )
530   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
531 is(
532     $$test_nbr_fields[0],
533     scalar @expectedfields,
534     "GetCancelledOrders gets orders with the right number of fields"
535 );
536 is( join( " ", @$test_missing_fields ),
537     '', "GetCancelledOrders gets orders with no missing fields" );
538 is( join( " ", @$test_extra_fields ),
539     '', "GetCancelledOrders gets orders with no unexpected fields" );
540 is( join( " ", @$test_different_fields ),
541     '',
542     "GetCancelledOrders gets orders with the right content in every fields" );
543 ok(
544     (
545         ( scalar @get_orders == 1 )
546           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
547     ),
548     "GetCancelledOrders only gets cancelled orders"
549 );
550
551 #
552 # Test SearchOrders
553 #
554
555 @expectedfields = qw (
556   order_internalnote
557   order_vendornote
558   notes
559   basketgroupid
560   basketgroupname
561   firstname
562   biblioitemnumber
563   ecost
564   uncertainprice
565   creationdate
566   datereceived
567   orderstatus
568   supplierreference
569   isbn
570   copyrightdate
571   gstrate
572   serial
573   listprice
574   budgetdate
575   basketno
576   discount
577   surname
578   freight
579   abstract
580   title
581   closedate
582   basketname
583   budgetgroup_id
584   invoiceid
585   author
586   parent_ordernumber
587   claims_count
588   entrydate
589   currency
590   quantityreceived
591   seriestitle
592   sort1_authcat
593   timestamp
594   biblionumber
595   unititle
596   sort2_authcat
597   rrp
598   unitprice
599   totalamount
600   sort1
601   ordernumber
602   datecreated
603   purchaseordernumber
604   quantity
605   claimed_date
606   subscriptionid
607   frameworkcode
608   sort2
609   datecancellationprinted
610   budget_id
611   authorisedby
612   booksellerid
613   cancellationreason
614 );
615
616 # note that authorisedby was added to the return of SearchOrder by the
617 # patch for bug 11777
618
619 my $invoiceid = AddInvoice(
620     invoicenumber => 'invoice',
621     booksellerid  => $booksellerid,
622     unknown       => "unknown"
623 );
624
625 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
626     {
627         biblionumber      => $biblionumber4,
628         ordernumber       => $ordernumbers[4],
629         quantityreceived  => 1,
630         cost              => 10,
631         ecost             => 10,
632         invoiceid         => $invoiceid,
633         rrp               => 10,
634         budget_id          => $order_content[4]->{str}->{budget_id},
635     }
636 );
637
638 my $search_orders = SearchOrders({
639     booksellerid => $booksellerid,
640     basketno     => $basketno
641 });
642 isa_ok( $search_orders, 'ARRAY' );
643 (
644     $test_missing_fields,   $test_extra_fields,
645     $test_different_fields, $test_nbr_fields
646   )
647   = _check_fields_of_orders( \@expectedfields, \@order_content,
648     $search_orders );
649 is(
650     $$test_nbr_fields[0],
651     scalar @expectedfields,
652     "SearchOrders gets orders with the right number of fields"
653 );
654 is( join( " ", @$test_missing_fields ),
655     '', "SearchOrders gets orders with no missing fields" );
656 is( join( " ", @$test_extra_fields ),
657     '', "SearchOrders gets orders with no unexpected fields" );
658 is( join( " ", @$test_different_fields ),
659     '', "SearchOrders gets orders with the right content in every fields" );
660 ok(
661     (
662         ( scalar @$search_orders == 4 )
663           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
664     ),
665     "SearchOrders only gets non-cancelled orders"
666 );
667
668 $search_orders = SearchOrders({
669     booksellerid => $booksellerid,
670     basketno     => $basketno,
671     pending      => 1
672 });
673 ok(
674     (
675         ( scalar @$search_orders == 3 ) and !grep ( (
676                      ( $_->{ordernumber} eq $ordernumbers[3] )
677                   or ( $_->{ordernumber} eq $ordernumbers[4] )
678             ),
679             @$search_orders )
680     ),
681     "SearchOrders with pending params gets only pending orders (bug 10723)"
682 );
683
684 $search_orders = SearchOrders({
685     booksellerid => $booksellerid,
686     basketno     => $basketno,
687     pending      => 1,
688     ordered      => 1,
689 });
690 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
691
692 $search_orders = SearchOrders({
693     ordernumber => $ordernumbers[4]
694 });
695 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
696
697 $search_orders = SearchOrders({
698     biblionumber => $biblionumber4
699 });
700 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
701
702 $search_orders = SearchOrders({
703     biblionumber => $biblionumber4,
704     pending      => 1
705 });
706 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
707
708 #
709 # Test GetBudgetByOrderNumber
710 #
711 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
712     "GetBudgetByOrderNumber returns expected budget" );
713
714 #
715 # Test GetLateOrders
716 #
717
718 @expectedfields = qw (
719   orderdate
720   author
721   budget
722   supplierid
723   claims_count
724   supplier
725   publisher
726   ordernumber
727   quantity
728   basketno
729   claimed_date
730   branch
731   estimateddeliverydate
732   title
733   publicationyear
734   unitpricelib
735   unitpricesupplier
736   subtotal
737   latesince
738   basketname
739   basketgroupid
740   basketgroupname
741 );
742 my @lateorders = GetLateOrders(0);
743 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
744     0, "GetLateOrders does not get orders from opened baskets" );
745 C4::Acquisition::CloseBasket($basketno);
746 @lateorders = GetLateOrders(0);
747 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
748     0, "GetLateOrders gets orders from closed baskets" );
749 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
750     "GetLateOrders does not gets cancelled orders" );
751 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
752     "GetLateOrders does not gets reveived orders" );
753 (
754     $test_missing_fields,   $test_extra_fields,
755     $test_different_fields, $test_nbr_fields
756   )
757   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
758 is(
759     $$test_nbr_fields[0],
760     scalar @expectedfields,
761     "GetLateOrders gets orders with the right number of fields"
762 );
763 is( join( " ", @$test_missing_fields ),
764     '', "GetLateOrders gets orders with no missing fields" );
765 is( join( " ", @$test_extra_fields ),
766     '', "GetLateOrders gets orders with no unexpected fields" );
767 is( join( " ", @$test_different_fields ),
768     '', "GetLateOrders gets orders with the right content in every fields" );
769
770 $search_orders = SearchOrders({
771     booksellerid => $booksellerid,
772     basketno     => $basketno,
773     pending      => 1,
774     ordered      => 1,
775 });
776 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)" );
777
778 #
779 # Test AddClaim
780 #
781
782 my $order = $lateorders[0];
783 AddClaim( $order->{ordernumber} );
784 my $neworder = GetOrder( $order->{ordernumber} );
785 is(
786     $neworder->{claimed_date},
787     strftime( "%Y-%m-%d", localtime(time) ),
788     "AddClaim : Check claimed_date"
789 );
790
791 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
792     {
793         biblionumber     => $biblionumber2,
794         ordernumber      => $ordernumbers[1],
795         quantityreceived => 2,
796         cost             => 12,
797         ecost            => 12,
798         invoiceid        => $invoiceid,
799         rrp              => 42,
800         order_internalnote => "my notes",
801         order_vendornote   => "my vendor notes",
802     }
803 );
804 my $order2 = GetOrder( $ordernumbers[1] );
805 is( $order2->{'quantityreceived'},
806     0, 'Splitting up order did not receive any on original order' );
807 is( $order2->{'quantity'}, 40, '40 items on original order' );
808 is( $order2->{'budget_id'}, $budgetid,
809     'Budget on original order is unchanged' );
810 is( $order2->{order_internalnote}, "my notes",
811     'ModReceiveOrder and GetOrder deal with internal notes' );
812 is( $order2->{order_vendornote}, "my vendor notes",
813     'ModReceiveOrder and GetOrder deal with vendor notes' );
814
815 $neworder = GetOrder($new_ordernumber);
816 is( $neworder->{'quantity'}, 2, '2 items on new order' );
817 is( $neworder->{'quantityreceived'},
818     2, 'Splitting up order received items on new order' );
819 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
820
821 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
822 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
823
824 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
825 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
826 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
827 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
828
829 my $budgetid2 = C4::Budgets::AddBudget(
830     {
831         budget_code => "budget_code_test_modrecv",
832         budget_name => "budget_name_test_modrecv",
833     }
834 );
835
836 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
837     {
838         biblionumber     => $biblionumber2,
839         ordernumber      => $ordernumbers[2],
840         quantityreceived => 2,
841         cost             => 12,
842         ecost            => 12,
843         invoiceid        => $invoiceid,
844         rrp              => 42,
845         budget_id        => $budgetid2,
846         order_internalnote => "my other notes",
847     }
848 );
849
850 my $order3 = GetOrder( $ordernumbers[2] );
851 is( $order3->{'quantityreceived'},
852     0, 'Splitting up order did not receive any on original order' );
853 is( $order3->{'quantity'}, 2, '2 items on original order' );
854 is( $order3->{'budget_id'}, $budgetid,
855     'Budget on original order is unchanged' );
856 is( $order3->{order_internalnote}, "my other notes",
857     'ModReceiveOrder and GetOrder deal with notes' );
858
859 $neworder = GetOrder($new_ordernumber);
860 is( $neworder->{'quantity'}, 2, '2 items on new order' );
861 is( $neworder->{'quantityreceived'},
862     2, 'Splitting up order received items on new order' );
863 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
864
865 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
866     {
867         biblionumber     => $biblionumber2,
868         ordernumber      => $ordernumbers[2],
869         quantityreceived => 2,
870         cost             => 12,
871         ecost            => 12,
872         invoiceid        => $invoiceid,
873         rrp              => 42,
874         budget_id        => $budgetid2,
875         order_internalnote => "my third notes",
876     }
877 );
878
879 $order3 = GetOrder( $ordernumbers[2] );
880 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
881 is( $order3->{'quantity'},         2,          '2 items on order' );
882 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
883 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
884
885 my $nonexistent_order = GetOrder();
886 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
887 $nonexistent_order = GetOrder( 424242424242 );
888 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
889
890 # Tests for DelOrder
891 my $order1 = GetOrder($ordernumbers[0]);
892 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
893 ok((not defined $error), "DelOrder does not fail");
894 $order1 = GetOrder($order1->{ordernumber});
895 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
896 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
897 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
898
899 $order2 = GetOrder($ordernumbers[1]);
900 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
901 ok((not defined $error), "DelOrder does not fail");
902 $order2 = GetOrder($order2->{ordernumber});
903 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
904 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
905 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
906
907 my $order4 = GetOrder($ordernumbers[3]);
908 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
909 ok((not defined $error), "DelOrder does not fail");
910 $order4 = GetOrder($order4->{ordernumber});
911 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
912 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
913 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
914 # End of tests for DelOrder
915
916 $dbh->rollback;