3 # This file is part of Koha.
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.
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.
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>.
20 use POSIX qw(strftime);
22 use Test::More tests => 92;
26 use_ok('C4::Acquisition');
27 use_ok('C4::Bookseller');
29 use_ok('C4::Budgets');
30 use_ok('C4::Bookseller');
31 use_ok('Koha::Acquisition::Order');
32 use_ok('Koha::Acquisition::Bookseller');
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);
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
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
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} );
64 foreach my $field ( keys %$order_to_check ) {
65 push @test_extra_fields, $field
66 unless grep ( /^$field$/, @$exp_fields );
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} );
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} );
81 \@test_missing_fields, \@test_extra_fields,
82 \@test_different_fields, $test_nbr_fields
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];
100 $t_missing_fields, $t_extra_fields,
101 $t_different_fields, $t_nbr_fields
103 = _check_fields_of_order( $exp_fields, $original_order_content,
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;
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 } };
115 \@test_missing_fields, \@test_extra_fields,
116 \@test_different_fields, \@test_nbr_fields
121 my $schema = Koha::Database->new()->schema();
122 $schema->storage->txn_begin();
124 my $dbh = C4::Context->dbh;
125 $dbh->{RaiseError} = 1;
127 # Creating some orders
128 my $booksellerid = C4::Bookseller::AddBookseller(
131 address1 => "bookseller's address",
138 my $booksellerinfo = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
140 is( $booksellerinfo->{deliverytime},
141 5, 'set deliverytime when creating vendor (Bug 10556)' );
143 my ( $basket, $basketno );
145 $basketno = NewBasket( $booksellerid, 1 ),
146 "NewBasket( $booksellerid , 1 ) returns $basketno"
148 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
150 my $bpid=AddBudgetPeriod({
151 budget_period_startdate => '2008-01-01'
152 , budget_period_enddate => '2008-12-31'
153 , budget_period_active => 1
154 , budget_period_description => "MAPERI"
157 my $budgetid = C4::Budgets::AddBudget(
159 budget_code => "budget_code_test_getordersbybib",
160 budget_name => "budget_name_test_getordersbybib",
161 budget_period_id => $bpid,
164 my $budget = C4::Budgets::GetBudget($budgetid);
167 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
168 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
169 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
170 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
172 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
173 # Ex : a price of 50.1 will be stored internally as 5.100000
175 my @order_content = (
178 basketno => $basketno,
179 biblionumber => $biblionumber1,
180 budget_id => $budget->{budget_id},
182 order_internalnote => "internal note",
183 order_vendornote => "vendor note",
188 listprice => 50.121111,
197 basketno => $basketno,
198 biblionumber => $biblionumber2,
199 budget_id => $budget->{budget_id}
201 num => { quantity => 42 }
205 basketno => $basketno,
206 biblionumber => $biblionumber2,
207 budget_id => $budget->{budget_id},
209 order_internalnote => "internal note",
210 order_vendornote => "vendor note"
225 basketno => $basketno,
226 biblionumber => $biblionumber3,
227 budget_id => $budget->{budget_id},
228 order_internalnote => "internal note",
229 order_vendornote => "vendor note"
245 basketno => $basketno,
246 biblionumber => $biblionumber4,
247 budget_id => $budget->{budget_id},
248 order_internalnote => "internal note",
249 order_vendornote => "vendor note"
265 # Create 4 orders in database
268 @ocontent{ keys %{ $order_content[$_]->{num} } } =
269 values %{ $order_content[$_]->{num} };
270 @ocontent{ keys %{ $order_content[$_]->{str} } } =
271 values %{ $order_content[$_]->{str} };
272 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
273 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
276 # Test UT sub _check_fields_of_order
279 $test_missing_fields, $test_extra_fields,
280 $test_different_fields, $test_nbr_fields
282 = _check_fields_of_order(
284 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
285 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
289 ( $test_nbr_fields == 5 )
290 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
291 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
292 and ( join( " ", @$test_different_fields ) eq 'a' )
294 "_check_fields_of_order can check an order (test 1)"
297 $test_missing_fields, $test_extra_fields,
298 $test_different_fields, $test_nbr_fields
300 = _check_fields_of_order(
302 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
303 { a => "bla", b => "105", c => 15 }
307 ( $test_nbr_fields == 3 )
308 and ( scalar @$test_missing_fields == 0 )
309 and ( scalar @$test_extra_fields == 0 )
310 and ( scalar @$test_different_fields == 0 )
312 "_check_fields_of_order can check an order (test 2)"
315 $test_missing_fields, $test_extra_fields,
316 $test_different_fields, $test_nbr_fields
318 = _check_fields_of_order(
320 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
321 { a => "blabla", b => "105", c => 15, d => "error" }
325 ( $test_nbr_fields == 4 )
326 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
327 and ( scalar @$test_extra_fields == 0 )
328 and ( join( " ", @$test_different_fields ) eq 'a c' )
330 "_check_fields_of_order can check an order (test 3)"
337 my @expectedfields = qw(
350 unitprice_tax_included
351 unitprice_tax_excluded
353 datecancellationprinted
381 suppliers_reference_number
382 suppliers_reference_qualifier
399 estimateddeliverydate
407 $test_missing_fields, $test_extra_fields,
408 $test_different_fields, $test_nbr_fields
410 = _check_fields_of_order( \@expectedfields, $order_content[0],
411 GetOrder( $ordernumbers[0] ) );
414 scalar @expectedfields,
415 "GetOrder gets an order with the right number of fields"
417 is( join( " ", @$test_missing_fields ),
418 '', "GetOrder gets an order with no missing fields" );
419 is( join( " ", @$test_extra_fields ),
420 '', "GetOrder gets an order with no unexpected fields" );
421 is( join( " ", @$test_different_fields ),
422 '', "GetOrder gets an order with the right content in every fields" );
428 my @base_expectedfields = qw(
465 unitprice_tax_included
466 unitprice_tax_excluded
473 editionresponsibility
483 suppliers_reference_number
484 suppliers_reference_qualifier
523 datecancellationprinted
527 ( @base_expectedfields,
528 ( 'transferred_from_timestamp', 'transferred_from' ) );
529 is( GetOrders(), undef, "GetOrders with no params returns undef" );
530 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
531 my @get_orders = GetOrders($basketno);
533 $test_missing_fields, $test_extra_fields,
534 $test_different_fields, $test_nbr_fields
536 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
538 $$test_nbr_fields[0],
539 scalar @expectedfields,
540 "GetOrders gets orders with the right number of fields"
542 is( join( " ", @$test_missing_fields ),
543 '', "GetOrders gets orders with no missing fields" );
544 is( join( " ", @$test_extra_fields ),
545 '', "GetOrders gets orders with no unexpected fields" );
546 is( join( " ", @$test_different_fields ),
547 '', "GetOrders gets orders with the right content in every fields" );
550 ( scalar @get_orders == 4 )
551 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
553 "GetOrders only gets non-cancelled orders"
557 # Test GetOrders { cancelled => 1 }
561 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
562 @get_orders = GetOrders($basketno, { cancelled => 1 });
564 $test_missing_fields, $test_extra_fields,
565 $test_different_fields, $test_nbr_fields
567 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
569 $$test_nbr_fields[0],
570 scalar @expectedfields,
571 "GetOrders { cancelled => 1 } gets orders with the right number of fields"
573 is( join( " ", @$test_missing_fields ),
574 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
575 is( join( " ", @$test_extra_fields ),
576 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
577 is( join( " ", @$test_different_fields ),
579 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
582 ( scalar @get_orders == 1 )
583 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
585 "GetOrders { cancelled => 1 } only gets cancelled orders"
592 @expectedfields = qw (
608 suppliers_reference_number
609 suppliers_reference_qualifier
643 unitprice_tax_included
644 unitprice_tax_excluded
654 datecancellationprinted
661 # note that authorisedby was added to the return of SearchOrder by the
662 # patch for bug 11777
664 my $invoiceid = AddInvoice(
665 invoicenumber => 'invoice',
666 booksellerid => $booksellerid,
670 my $invoice = GetInvoice( $invoiceid );
672 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
674 biblionumber => $biblionumber4,
675 order => GetOrder( $ordernumbers[4] ),
676 quantityreceived => 1,
678 budget_id => $order_content[4]->{str}->{budget_id},
682 my $search_orders = SearchOrders({
683 booksellerid => $booksellerid,
684 basketno => $basketno
686 isa_ok( $search_orders, 'ARRAY' );
688 $test_missing_fields, $test_extra_fields,
689 $test_different_fields, $test_nbr_fields
691 = _check_fields_of_orders( \@expectedfields, \@order_content,
694 $$test_nbr_fields[0],
695 scalar @expectedfields,
696 "SearchOrders gets orders with the right number of fields"
698 is( join( " ", @$test_missing_fields ),
699 '', "SearchOrders gets orders with no missing fields" );
700 is( join( " ", @$test_extra_fields ),
701 '', "SearchOrders gets orders with no unexpected fields" );
702 is( join( " ", @$test_different_fields ),
703 '', "SearchOrders gets orders with the right content in every fields" );
706 ( scalar @$search_orders == 4 )
707 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
709 "SearchOrders only gets non-cancelled orders"
712 $search_orders = SearchOrders({
713 booksellerid => $booksellerid,
714 basketno => $basketno,
719 ( scalar @$search_orders == 3 ) and !grep ( (
720 ( $_->{ordernumber} eq $ordernumbers[3] )
721 or ( $_->{ordernumber} eq $ordernumbers[4] )
725 "SearchOrders with pending params gets only pending orders (bug 10723)"
728 $search_orders = SearchOrders({
729 booksellerid => $booksellerid,
730 basketno => $basketno,
734 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
736 $search_orders = SearchOrders({
737 ordernumber => $ordernumbers[4]
739 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
741 $search_orders = SearchOrders({
742 biblionumber => $biblionumber4
744 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
746 $search_orders = SearchOrders({
747 biblionumber => $biblionumber4,
750 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
753 # Test GetBudgetByOrderNumber
755 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
756 "GetBudgetByOrderNumber returns expected budget" );
762 @expectedfields = qw (
775 estimateddeliverydate
786 my @lateorders = GetLateOrders(0);
787 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
788 0, "GetLateOrders does not get orders from opened baskets" );
789 C4::Acquisition::CloseBasket($basketno);
790 @lateorders = GetLateOrders(0);
791 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
792 0, "GetLateOrders gets orders from closed baskets" );
793 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
794 "GetLateOrders does not gets cancelled orders" );
795 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
796 "GetLateOrders does not gets reveived orders" );
798 $test_missing_fields, $test_extra_fields,
799 $test_different_fields, $test_nbr_fields
801 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
803 $$test_nbr_fields[0],
804 scalar @expectedfields,
805 "GetLateOrders gets orders with the right number of fields"
807 is( join( " ", @$test_missing_fields ),
808 '', "GetLateOrders gets orders with no missing fields" );
809 is( join( " ", @$test_extra_fields ),
810 '', "GetLateOrders gets orders with no unexpected fields" );
811 is( join( " ", @$test_different_fields ),
812 '', "GetLateOrders gets orders with the right content in every fields" );
814 $search_orders = SearchOrders({
815 booksellerid => $booksellerid,
816 basketno => $basketno,
820 is( scalar (@$search_orders), 3, "SearchOrders with pending and ordered params gets only pending ordered orders. After closing the basket, orders are marked as 'ordered' (bug 11170)" );
826 my $order = $lateorders[0];
827 AddClaim( $order->{ordernumber} );
828 my $neworder = GetOrder( $order->{ordernumber} );
830 $neworder->{claimed_date},
831 strftime( "%Y-%m-%d", localtime(time) ),
832 "AddClaim : Check claimed_date"
835 my $order2 = GetOrder( $ordernumbers[1] );
836 $order2->{order_internalnote} = "my notes";
837 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
839 biblionumber => $biblionumber2,
841 quantityreceived => 2,
846 $order2 = GetOrder( $ordernumbers[1] );
847 is( $order2->{'quantityreceived'},
848 0, 'Splitting up order did not receive any on original order' );
849 is( $order2->{'quantity'}, 40, '40 items on original order' );
850 is( $order2->{'budget_id'}, $budgetid,
851 'Budget on original order is unchanged' );
852 is( $order2->{order_internalnote}, "my notes",
853 'ModReceiveOrder and GetOrder deal with internal notes' );
855 $neworder = GetOrder($new_ordernumber);
856 is( $neworder->{'quantity'}, 2, '2 items on new order' );
857 is( $neworder->{'quantityreceived'},
858 2, 'Splitting up order received items on new order' );
859 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
861 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
862 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
864 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
865 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
866 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
867 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
869 my $budgetid2 = C4::Budgets::AddBudget(
871 budget_code => "budget_code_test_modrecv",
872 budget_name => "budget_name_test_modrecv",
876 my $order3 = GetOrder( $ordernumbers[2] );
877 $order3->{order_internalnote} = "my other notes";
878 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
880 biblionumber => $biblionumber2,
882 quantityreceived => 2,
884 budget_id => $budgetid2,
888 $order3 = GetOrder( $ordernumbers[2] );
889 is( $order3->{'quantityreceived'},
890 0, 'Splitting up order did not receive any on original order' );
891 is( $order3->{'quantity'}, 2, '2 items on original order' );
892 is( $order3->{'budget_id'}, $budgetid,
893 'Budget on original order is unchanged' );
894 is( $order3->{order_internalnote}, "my other notes",
895 'ModReceiveOrder and GetOrder deal with notes' );
897 $neworder = GetOrder($new_ordernumber);
898 is( $neworder->{'quantity'}, 2, '2 items on new order' );
899 is( $neworder->{'quantityreceived'},
900 2, 'Splitting up order received items on new order' );
901 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
903 $order3 = GetOrder( $ordernumbers[2] );
904 $order3->{order_internalnote} = "my third notes";
905 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
907 biblionumber => $biblionumber2,
909 quantityreceived => 2,
911 budget_id => $budgetid2,
915 $order3 = GetOrder( $ordernumbers[2] );
916 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
917 is( $order3->{'quantity'}, 2, '2 items on order' );
918 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
919 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
921 my $nonexistent_order = GetOrder();
922 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
923 $nonexistent_order = GetOrder( 424242424242 );
924 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
927 my $order1 = GetOrder($ordernumbers[0]);
928 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
929 ok((not defined $error), "DelOrder does not fail");
930 $order1 = GetOrder($order1->{ordernumber});
931 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
932 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
933 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
935 $order2 = GetOrder($ordernumbers[1]);
936 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
937 ok((not defined $error), "DelOrder does not fail");
938 $order2 = GetOrder($order2->{ordernumber});
939 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
940 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
941 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
943 my $order4 = GetOrder($ordernumbers[3]);
944 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
945 ok((not defined $error), "DelOrder does not fail");
946 $order4 = GetOrder($order4->{ordernumber});
947 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
948 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
949 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
950 # End of tests for DelOrder
952 subtest 'ModOrder' => sub {
954 ModOrder( { ordernumber => $order1->{ordernumber}, unitprice => 42 } );
955 my $order = GetOrder( $order1->{ordernumber} );
956 is( int($order->{unitprice}), 42, 'ModOrder should work even if biblionumber if not passed');
960 my $all_count = scalar GetBudgetsReport();
961 ok($all_count >= 1, "GetBudgetReport OK");
963 my $active_count = scalar GetBudgetsReport(1);
964 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
966 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
967 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
969 $schema->storage->txn_rollback();