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