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