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