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