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