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