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