Bug 16889: Add Koha::Objects->columns
[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 => 91;
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 $bpid=AddBudgetPeriod({
151         budget_period_startdate => '2008-01-01'
152         , budget_period_enddate => '2008-12-31'
153         , budget_period_active  => 1
154         , budget_period_description    => "MAPERI"
155 });
156
157 my $budgetid = C4::Budgets::AddBudget(
158     {
159         budget_code => "budget_code_test_getordersbybib",
160         budget_name => "budget_name_test_getordersbybib",
161         budget_period_id => $bpid,
162     }
163 );
164 my $budget = C4::Budgets::GetBudget($budgetid);
165
166 my @ordernumbers;
167 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
168 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
169 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
170 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
171
172 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
173 # Ex : a price of 50.1 will be stored internally as 5.100000
174
175 my @order_content = (
176     {
177         str => {
178             basketno       => $basketno,
179             biblionumber   => $biblionumber1,
180             budget_id      => $budget->{budget_id},
181             uncertainprice => 0,
182             order_internalnote => "internal note",
183             order_vendornote   => "vendor note",
184             ordernumber => '',
185         },
186         num => {
187             quantity  => 24,
188             listprice => 50.121111,
189             ecost     => 38.15,
190             rrp       => 40.15,
191             discount  => 5.1111,
192             gstrate   => 0.0515
193         }
194     },
195     {
196         str => {
197             basketno     => $basketno,
198             biblionumber => $biblionumber2,
199             budget_id    => $budget->{budget_id}
200         },
201         num => { quantity => 42 }
202     },
203     {
204         str => {
205             basketno       => $basketno,
206             biblionumber   => $biblionumber2,
207             budget_id      => $budget->{budget_id},
208             uncertainprice => 0,
209             order_internalnote => "internal note",
210             order_vendornote   => "vendor note"
211         },
212         num => {
213             quantity  => 4,
214             ecost     => 42.1,
215             rrp       => 42.1,
216             listprice => 10.1,
217             ecost     => 38.1,
218             rrp       => 11.0,
219             discount  => 5.1,
220             gstrate   => 0.1
221         }
222     },
223     {
224         str => {
225             basketno     => $basketno,
226             biblionumber => $biblionumber3,
227             budget_id    => $budget->{budget_id},
228             order_internalnote => "internal note",
229             order_vendornote   => "vendor note"
230         },
231         num => {
232             quantity       => 4,
233             ecost          => 40,
234             rrp            => 42,
235             listprice      => 10,
236             ecost          => 38.15,
237             rrp            => 11.00,
238             discount       => 0,
239             uncertainprice => 0,
240             gstrate        => 0
241         }
242     },
243     {
244         str => {
245             basketno     => $basketno,
246             biblionumber => $biblionumber4,
247             budget_id    => $budget->{budget_id},
248             order_internalnote => "internal note",
249             order_vendornote   => "vendor note"
250         },
251         num => {
252             quantity       => 1,
253             ecost          => 10,
254             rrp            => 10,
255             listprice      => 10,
256             ecost          => 10,
257             rrp            => 10,
258             discount       => 0,
259             uncertainprice => 0,
260             gstrate        => 0
261         }
262     }
263 );
264
265 # Create 4 orders in database
266 for ( 0 .. 4 ) {
267     my %ocontent;
268     @ocontent{ keys %{ $order_content[$_]->{num} } } =
269       values %{ $order_content[$_]->{num} };
270     @ocontent{ keys %{ $order_content[$_]->{str} } } =
271       values %{ $order_content[$_]->{str} };
272     $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
273     $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
274 }
275
276 # Test UT sub _check_fields_of_order
277
278 my (
279     $test_missing_fields,   $test_extra_fields,
280     $test_different_fields, $test_nbr_fields
281   )
282   = _check_fields_of_order(
283     [qw /a b c d e/],
284     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
285     { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
286   );
287 ok(
288     (
289               ( $test_nbr_fields == 5 )
290           and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
291           and ( join( " ", sort @$test_extra_fields )   eq 'f g' )
292           and ( join( " ", @$test_different_fields )    eq 'a' )
293     ),
294     "_check_fields_of_order can check an order (test 1)"
295 );
296 (
297     $test_missing_fields,   $test_extra_fields,
298     $test_different_fields, $test_nbr_fields
299   )
300   = _check_fields_of_order(
301     [qw /a b c /],
302     { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
303     { a => "bla", b => "105", c => 15 }
304   );
305 ok(
306     (
307               ( $test_nbr_fields == 3 )
308           and ( scalar @$test_missing_fields == 0 )
309           and ( scalar @$test_extra_fields == 0 )
310           and ( scalar @$test_different_fields == 0 )
311     ),
312     "_check_fields_of_order can check an order (test 2)"
313 );
314 (
315     $test_missing_fields,   $test_extra_fields,
316     $test_different_fields, $test_nbr_fields
317   )
318   = _check_fields_of_order(
319     [qw /a b c d e/],
320     { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
321     { a => "blabla", b => "105", c => 15, d => "error" }
322   );
323 ok(
324     (
325               ( $test_nbr_fields == 4 )
326           and ( join( " ", sort @$test_missing_fields ) eq 'e' )
327           and ( scalar @$test_extra_fields == 0 )
328           and ( join( " ", @$test_different_fields ) eq 'a c' )
329     ),
330     "_check_fields_of_order can check an order (test 3)"
331 );
332
333 #
334 # test GetOrder
335 #
336
337 my @expectedfields = qw(
338   order_internalnote
339   order_vendornote
340   ordernumber
341   biblionumber
342   entrydate
343   quantity
344   currency
345   listprice
346   datereceived
347   invoiceid
348   freight
349   unitprice
350   quantityreceived
351   datecancellationprinted
352   purchaseordernumber
353   basketno
354   timestamp
355   rrp
356   ecost
357   unitpricesupplier
358   unitpricelib
359   gstrate
360   discount
361   budget_id
362   budgetdate
363   sort1
364   sort2
365   sort1_authcat
366   sort2_authcat
367   uncertainprice
368   claims_count
369   claimed_date
370   subscriptionid
371   parent_ordernumber
372   orderstatus
373   line_item_id
374   suppliers_reference_number
375   suppliers_reference_qualifier
376   suppliers_report
377   title
378   author
379   basketname
380   branchcode
381   publicationyear
382   copyrightdate
383   editionstatement
384   isbn
385   ean
386   seriestitle
387   publishercode
388   publisher
389   budget
390   supplier
391   supplierid
392   estimateddeliverydate
393   orderdate
394   quantity_to_receive
395   subtotal
396   latesince
397   cancellationreason
398 );
399 (
400     $test_missing_fields,   $test_extra_fields,
401     $test_different_fields, $test_nbr_fields
402   )
403   = _check_fields_of_order( \@expectedfields, $order_content[0],
404     GetOrder( $ordernumbers[0] ) );
405 is(
406     $test_nbr_fields,
407     scalar @expectedfields,
408     "GetOrder gets an order with the right number of fields"
409 );
410 is( join( " ", @$test_missing_fields ),
411     '', "GetOrder gets an order with no missing fields" );
412 is( join( " ", @$test_extra_fields ),
413     '', "GetOrder gets an order with no unexpected fields" );
414 is( join( " ", @$test_different_fields ),
415     '', "GetOrder gets an order with the right content in every fields" );
416
417 #
418 # Test GetOrders
419 #
420
421 my @base_expectedfields = qw(
422   order_internalnote
423   order_vendornote
424   notes
425   ordernumber
426   ecost
427   uncertainprice
428   marc
429   url
430   isbn
431   copyrightdate
432   serial
433   cn_suffix
434   cn_item
435   marcxml
436   freight
437   cn_class
438   title
439   pages
440   budget_encumb
441   budget_name
442   number
443   itemtype
444   totalissues
445   author
446   budget_permission
447   parent_ordernumber
448   size
449   claims_count
450   currency
451   seriestitle
452   timestamp
453   editionstatement
454   budget_parent_id
455   publishercode
456   unitprice
457   collectionvolume
458   budget_amount
459   budget_owner_id
460   datecreated
461   claimed_date
462   subscriptionid
463   editionresponsibility
464   sort2
465   volumedate
466   budget_id
467   illus
468   ean
469   biblioitemnumber
470   datereceived
471   orderstatus
472   line_item_id
473   suppliers_reference_number
474   suppliers_reference_qualifier
475   suppliers_report
476   agerestriction
477   budget_branchcode
478   gstrate
479   listprice
480   budget_code
481   budgetdate
482   basketno
483   discount
484   abstract
485   collectionissn
486   publicationyear
487   collectiontitle
488   invoiceid
489   place
490   issn
491   quantityreceived
492   entrydate
493   cn_source
494   sort1_authcat
495   budget_notes
496   biblionumber
497   unititle
498   sort2_authcat
499   budget_expend
500   rrp
501   cn_sort
502   lccn
503   sort1
504   volume
505   purchaseordernumber
506   quantity
507   budget_period_id
508   frameworkcode
509   volumedesc
510   datecancellationprinted
511   cancellationreason
512 );
513 @expectedfields =
514   ( @base_expectedfields,
515     ( 'transferred_from_timestamp', 'transferred_from' ) );
516 is( GetOrders(), undef, "GetOrders with no params returns undef" );
517 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
518 my @get_orders = GetOrders($basketno);
519 (
520     $test_missing_fields,   $test_extra_fields,
521     $test_different_fields, $test_nbr_fields
522   )
523   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
524 is(
525     $$test_nbr_fields[0],
526     scalar @expectedfields,
527     "GetOrders gets orders with the right number of fields"
528 );
529 is( join( " ", @$test_missing_fields ),
530     '', "GetOrders gets orders with no missing fields" );
531 is( join( " ", @$test_extra_fields ),
532     '', "GetOrders gets orders with no unexpected fields" );
533 is( join( " ", @$test_different_fields ),
534     '', "GetOrders gets orders with the right content in every fields" );
535 ok(
536     (
537         ( scalar @get_orders == 4 )
538           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
539     ),
540     "GetOrders only gets non-cancelled orders"
541 );
542
543 #
544 # Test GetOrders { cancelled => 1 }
545 #
546
547 @expectedfields =
548   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
549 @get_orders = GetOrders($basketno, { cancelled => 1 });
550 (
551     $test_missing_fields,   $test_extra_fields,
552     $test_different_fields, $test_nbr_fields
553   )
554   = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
555 is(
556     $$test_nbr_fields[0],
557     scalar @expectedfields,
558     "GetOrders { cancelled => 1 } gets orders with the right number of fields"
559 );
560 is( join( " ", @$test_missing_fields ),
561     '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
562 is( join( " ", @$test_extra_fields ),
563     '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
564 is( join( " ", @$test_different_fields ),
565     '',
566     "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
567 ok(
568     (
569         ( scalar @get_orders == 1 )
570           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
571     ),
572     "GetOrders { cancelled => 1 } only gets cancelled orders"
573 );
574
575 #
576 # Test SearchOrders
577 #
578
579 @expectedfields = qw (
580   order_internalnote
581   order_vendornote
582   notes
583   basketgroupid
584   basketgroupname
585   firstname
586   biblioitemnumber
587   ecost
588   uncertainprice
589   creationdate
590   datereceived
591   orderstatus
592   line_item_id
593   suppliers_reference_number
594   suppliers_reference_qualifier
595   suppliers_report
596   isbn
597   copyrightdate
598   gstrate
599   serial
600   listprice
601   budgetdate
602   basketno
603   discount
604   surname
605   freight
606   abstract
607   title
608   closedate
609   basketname
610   invoiceid
611   author
612   parent_ordernumber
613   claims_count
614   entrydate
615   currency
616   quantityreceived
617   seriestitle
618   sort1_authcat
619   timestamp
620   biblionumber
621   unititle
622   sort2_authcat
623   rrp
624   unitprice
625   sort1
626   ordernumber
627   datecreated
628   purchaseordernumber
629   quantity
630   claimed_date
631   subscriptionid
632   frameworkcode
633   sort2
634   datecancellationprinted
635   budget_id
636   authorisedby
637   booksellerid
638   cancellationreason
639 );
640
641 # note that authorisedby was added to the return of SearchOrder by the
642 # patch for bug 11777
643
644 my $invoiceid = AddInvoice(
645     invoicenumber => 'invoice',
646     booksellerid  => $booksellerid,
647     unknown       => "unknown"
648 );
649
650 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
651     {
652         biblionumber      => $biblionumber4,
653         ordernumber       => $ordernumbers[4],
654         quantityreceived  => 1,
655         cost              => 10,
656         ecost             => 10,
657         invoiceid         => $invoiceid,
658         rrp               => 10,
659         budget_id          => $order_content[4]->{str}->{budget_id},
660     }
661 );
662
663 my $search_orders = SearchOrders({
664     booksellerid => $booksellerid,
665     basketno     => $basketno
666 });
667 isa_ok( $search_orders, 'ARRAY' );
668 (
669     $test_missing_fields,   $test_extra_fields,
670     $test_different_fields, $test_nbr_fields
671   )
672   = _check_fields_of_orders( \@expectedfields, \@order_content,
673     $search_orders );
674 is(
675     $$test_nbr_fields[0],
676     scalar @expectedfields,
677     "SearchOrders gets orders with the right number of fields"
678 );
679 is( join( " ", @$test_missing_fields ),
680     '', "SearchOrders gets orders with no missing fields" );
681 is( join( " ", @$test_extra_fields ),
682     '', "SearchOrders gets orders with no unexpected fields" );
683 is( join( " ", @$test_different_fields ),
684     '', "SearchOrders gets orders with the right content in every fields" );
685 ok(
686     (
687         ( scalar @$search_orders == 4 )
688           and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
689     ),
690     "SearchOrders only gets non-cancelled orders"
691 );
692
693 $search_orders = SearchOrders({
694     booksellerid => $booksellerid,
695     basketno     => $basketno,
696     pending      => 1
697 });
698 ok(
699     (
700         ( scalar @$search_orders == 3 ) and !grep ( (
701                      ( $_->{ordernumber} eq $ordernumbers[3] )
702                   or ( $_->{ordernumber} eq $ordernumbers[4] )
703             ),
704             @$search_orders )
705     ),
706     "SearchOrders with pending params gets only pending orders (bug 10723)"
707 );
708
709 $search_orders = SearchOrders({
710     booksellerid => $booksellerid,
711     basketno     => $basketno,
712     pending      => 1,
713     ordered      => 1,
714 });
715 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
716
717 $search_orders = SearchOrders({
718     ordernumber => $ordernumbers[4]
719 });
720 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
721
722 $search_orders = SearchOrders({
723     biblionumber => $biblionumber4
724 });
725 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
726
727 $search_orders = SearchOrders({
728     biblionumber => $biblionumber4,
729     pending      => 1
730 });
731 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
732
733 #
734 # Test GetBudgetByOrderNumber
735 #
736 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
737     "GetBudgetByOrderNumber returns expected budget" );
738
739 #
740 # Test GetLateOrders
741 #
742
743 @expectedfields = qw (
744   orderdate
745   author
746   budget
747   supplierid
748   claims_count
749   supplier
750   publisher
751   ordernumber
752   quantity
753   basketno
754   claimed_date
755   branch
756   estimateddeliverydate
757   title
758   publicationyear
759   unitpricelib
760   unitpricesupplier
761   subtotal
762   latesince
763   basketname
764   basketgroupid
765   basketgroupname
766 );
767 my @lateorders = GetLateOrders(0);
768 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
769     0, "GetLateOrders does not get orders from opened baskets" );
770 C4::Acquisition::CloseBasket($basketno);
771 @lateorders = GetLateOrders(0);
772 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
773     0, "GetLateOrders gets orders from closed baskets" );
774 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
775     "GetLateOrders does not gets cancelled orders" );
776 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
777     "GetLateOrders does not gets reveived orders" );
778 (
779     $test_missing_fields,   $test_extra_fields,
780     $test_different_fields, $test_nbr_fields
781   )
782   = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
783 is(
784     $$test_nbr_fields[0],
785     scalar @expectedfields,
786     "GetLateOrders gets orders with the right number of fields"
787 );
788 is( join( " ", @$test_missing_fields ),
789     '', "GetLateOrders gets orders with no missing fields" );
790 is( join( " ", @$test_extra_fields ),
791     '', "GetLateOrders gets orders with no unexpected fields" );
792 is( join( " ", @$test_different_fields ),
793     '', "GetLateOrders gets orders with the right content in every fields" );
794
795 $search_orders = SearchOrders({
796     booksellerid => $booksellerid,
797     basketno     => $basketno,
798     pending      => 1,
799     ordered      => 1,
800 });
801 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)" );
802
803 #
804 # Test AddClaim
805 #
806
807 my $order = $lateorders[0];
808 AddClaim( $order->{ordernumber} );
809 my $neworder = GetOrder( $order->{ordernumber} );
810 is(
811     $neworder->{claimed_date},
812     strftime( "%Y-%m-%d", localtime(time) ),
813     "AddClaim : Check claimed_date"
814 );
815
816 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
817     {
818         biblionumber     => $biblionumber2,
819         ordernumber      => $ordernumbers[1],
820         quantityreceived => 2,
821         cost             => 12,
822         ecost            => 12,
823         invoiceid        => $invoiceid,
824         rrp              => 42,
825         order_internalnote => "my notes",
826         order_vendornote   => "my vendor notes",
827     }
828 );
829 my $order2 = GetOrder( $ordernumbers[1] );
830 is( $order2->{'quantityreceived'},
831     0, 'Splitting up order did not receive any on original order' );
832 is( $order2->{'quantity'}, 40, '40 items on original order' );
833 is( $order2->{'budget_id'}, $budgetid,
834     'Budget on original order is unchanged' );
835 is( $order2->{order_internalnote}, "my notes",
836     'ModReceiveOrder and GetOrder deal with internal notes' );
837 is( $order2->{order_vendornote}, "my vendor notes",
838     'ModReceiveOrder and GetOrder deal with vendor notes' );
839
840 $neworder = GetOrder($new_ordernumber);
841 is( $neworder->{'quantity'}, 2, '2 items on new order' );
842 is( $neworder->{'quantityreceived'},
843     2, 'Splitting up order received items on new order' );
844 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
845
846 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
847 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
848
849 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
850 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
851 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
852 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
853
854 my $budgetid2 = C4::Budgets::AddBudget(
855     {
856         budget_code => "budget_code_test_modrecv",
857         budget_name => "budget_name_test_modrecv",
858     }
859 );
860
861 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
862     {
863         biblionumber     => $biblionumber2,
864         ordernumber      => $ordernumbers[2],
865         quantityreceived => 2,
866         cost             => 12,
867         ecost            => 12,
868         invoiceid        => $invoiceid,
869         rrp              => 42,
870         budget_id        => $budgetid2,
871         order_internalnote => "my other notes",
872     }
873 );
874
875 my $order3 = GetOrder( $ordernumbers[2] );
876 is( $order3->{'quantityreceived'},
877     0, 'Splitting up order did not receive any on original order' );
878 is( $order3->{'quantity'}, 2, '2 items on original order' );
879 is( $order3->{'budget_id'}, $budgetid,
880     'Budget on original order is unchanged' );
881 is( $order3->{order_internalnote}, "my other notes",
882     'ModReceiveOrder and GetOrder deal with notes' );
883
884 $neworder = GetOrder($new_ordernumber);
885 is( $neworder->{'quantity'}, 2, '2 items on new order' );
886 is( $neworder->{'quantityreceived'},
887     2, 'Splitting up order received items on new order' );
888 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
889
890 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
891     {
892         biblionumber     => $biblionumber2,
893         ordernumber      => $ordernumbers[2],
894         quantityreceived => 2,
895         cost             => 12,
896         ecost            => 12,
897         invoiceid        => $invoiceid,
898         rrp              => 42,
899         budget_id        => $budgetid2,
900         order_internalnote => "my third notes",
901     }
902 );
903
904 $order3 = GetOrder( $ordernumbers[2] );
905 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
906 is( $order3->{'quantity'},         2,          '2 items on order' );
907 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
908 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
909
910 my $nonexistent_order = GetOrder();
911 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
912 $nonexistent_order = GetOrder( 424242424242 );
913 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
914
915 # Tests for DelOrder
916 my $order1 = GetOrder($ordernumbers[0]);
917 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
918 ok((not defined $error), "DelOrder does not fail");
919 $order1 = GetOrder($order1->{ordernumber});
920 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
921 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
922 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
923
924 $order2 = GetOrder($ordernumbers[1]);
925 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
926 ok((not defined $error), "DelOrder does not fail");
927 $order2 = GetOrder($order2->{ordernumber});
928 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
929 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
930 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
931
932 my $order4 = GetOrder($ordernumbers[3]);
933 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
934 ok((not defined $error), "DelOrder does not fail");
935 $order4 = GetOrder($order4->{ordernumber});
936 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
937 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
938 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
939 # End of tests for DelOrder
940
941 # Budget reports
942 my $all_count = scalar GetBudgetsReport();
943 ok($all_count >= 1, "GetBudgetReport OK");
944
945 my $active_count = scalar GetBudgetsReport(1);
946 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
947
948 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
949 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
950
951 $schema->storage->txn_rollback();