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 => 91;
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(
351 datecancellationprinted
375 suppliers_reference_number
376 suppliers_reference_qualifier
393 estimateddeliverydate
401 $test_missing_fields, $test_extra_fields,
402 $test_different_fields, $test_nbr_fields
404 = _check_fields_of_order( \@expectedfields, $order_content[0],
405 GetOrder( $ordernumbers[0] ) );
408 scalar @expectedfields,
409 "GetOrder gets an order with the right number of fields"
411 is( join( " ", @$test_missing_fields ),
412 '', "GetOrder gets an order with no missing fields" );
413 is( join( " ", @$test_extra_fields ),
414 '', "GetOrder gets an order with no unexpected fields" );
415 is( join( " ", @$test_different_fields ),
416 '', "GetOrder gets an order with the right content in every fields" );
422 my @base_expectedfields = qw(
464 editionresponsibility
474 suppliers_reference_number
475 suppliers_reference_qualifier
512 datecancellationprinted
516 ( @base_expectedfields,
517 ( 'transferred_from_timestamp', 'transferred_from' ) );
518 is( GetOrders(), undef, "GetOrders with no params returns undef" );
519 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
520 my @get_orders = GetOrders($basketno);
522 $test_missing_fields, $test_extra_fields,
523 $test_different_fields, $test_nbr_fields
525 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
527 $$test_nbr_fields[0],
528 scalar @expectedfields,
529 "GetOrders gets orders with the right number of fields"
531 is( join( " ", @$test_missing_fields ),
532 '', "GetOrders gets orders with no missing fields" );
533 is( join( " ", @$test_extra_fields ),
534 '', "GetOrders gets orders with no unexpected fields" );
535 is( join( " ", @$test_different_fields ),
536 '', "GetOrders gets orders with the right content in every fields" );
539 ( scalar @get_orders == 4 )
540 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
542 "GetOrders only gets non-cancelled orders"
546 # Test GetOrders { cancelled => 1 }
550 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
551 @get_orders = GetOrders($basketno, { cancelled => 1 });
553 $test_missing_fields, $test_extra_fields,
554 $test_different_fields, $test_nbr_fields
556 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
558 $$test_nbr_fields[0],
559 scalar @expectedfields,
560 "GetOrders { cancelled => 1 } gets orders with the right number of fields"
562 is( join( " ", @$test_missing_fields ),
563 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
564 is( join( " ", @$test_extra_fields ),
565 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
566 is( join( " ", @$test_different_fields ),
568 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
571 ( scalar @get_orders == 1 )
572 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
574 "GetOrders { cancelled => 1 } only gets cancelled orders"
581 @expectedfields = qw (
595 suppliers_reference_number
596 suppliers_reference_qualifier
637 datecancellationprinted
644 # note that authorisedby was added to the return of SearchOrder by the
645 # patch for bug 11777
647 my $invoiceid = AddInvoice(
648 invoicenumber => 'invoice',
649 booksellerid => $booksellerid,
653 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
655 biblionumber => $biblionumber4,
656 ordernumber => $ordernumbers[4],
657 quantityreceived => 1,
660 invoiceid => $invoiceid,
662 budget_id => $order_content[4]->{str}->{budget_id},
666 my $search_orders = SearchOrders({
667 booksellerid => $booksellerid,
668 basketno => $basketno
670 isa_ok( $search_orders, 'ARRAY' );
672 $test_missing_fields, $test_extra_fields,
673 $test_different_fields, $test_nbr_fields
675 = _check_fields_of_orders( \@expectedfields, \@order_content,
678 $$test_nbr_fields[0],
679 scalar @expectedfields,
680 "SearchOrders gets orders with the right number of fields"
682 is( join( " ", @$test_missing_fields ),
683 '', "SearchOrders gets orders with no missing fields" );
684 is( join( " ", @$test_extra_fields ),
685 '', "SearchOrders gets orders with no unexpected fields" );
686 is( join( " ", @$test_different_fields ),
687 '', "SearchOrders gets orders with the right content in every fields" );
690 ( scalar @$search_orders == 4 )
691 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
693 "SearchOrders only gets non-cancelled orders"
696 $search_orders = SearchOrders({
697 booksellerid => $booksellerid,
698 basketno => $basketno,
703 ( scalar @$search_orders == 3 ) and !grep ( (
704 ( $_->{ordernumber} eq $ordernumbers[3] )
705 or ( $_->{ordernumber} eq $ordernumbers[4] )
709 "SearchOrders with pending params gets only pending orders (bug 10723)"
712 $search_orders = SearchOrders({
713 booksellerid => $booksellerid,
714 basketno => $basketno,
718 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
720 $search_orders = SearchOrders({
721 ordernumber => $ordernumbers[4]
723 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
725 $search_orders = SearchOrders({
726 biblionumber => $biblionumber4
728 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
730 $search_orders = SearchOrders({
731 biblionumber => $biblionumber4,
734 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
737 # Test GetBudgetByOrderNumber
739 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
740 "GetBudgetByOrderNumber returns expected budget" );
746 @expectedfields = qw (
759 estimateddeliverydate
770 my @lateorders = GetLateOrders(0);
771 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
772 0, "GetLateOrders does not get orders from opened baskets" );
773 C4::Acquisition::CloseBasket($basketno);
774 @lateorders = GetLateOrders(0);
775 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
776 0, "GetLateOrders gets orders from closed baskets" );
777 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
778 "GetLateOrders does not gets cancelled orders" );
779 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
780 "GetLateOrders does not gets reveived orders" );
782 $test_missing_fields, $test_extra_fields,
783 $test_different_fields, $test_nbr_fields
785 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
787 $$test_nbr_fields[0],
788 scalar @expectedfields,
789 "GetLateOrders gets orders with the right number of fields"
791 is( join( " ", @$test_missing_fields ),
792 '', "GetLateOrders gets orders with no missing fields" );
793 is( join( " ", @$test_extra_fields ),
794 '', "GetLateOrders gets orders with no unexpected fields" );
795 is( join( " ", @$test_different_fields ),
796 '', "GetLateOrders gets orders with the right content in every fields" );
798 $search_orders = SearchOrders({
799 booksellerid => $booksellerid,
800 basketno => $basketno,
804 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)" );
810 my $order = $lateorders[0];
811 AddClaim( $order->{ordernumber} );
812 my $neworder = GetOrder( $order->{ordernumber} );
814 $neworder->{claimed_date},
815 strftime( "%Y-%m-%d", localtime(time) ),
816 "AddClaim : Check claimed_date"
819 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
821 biblionumber => $biblionumber2,
822 ordernumber => $ordernumbers[1],
823 quantityreceived => 2,
826 invoiceid => $invoiceid,
828 order_internalnote => "my notes",
829 order_vendornote => "my vendor notes",
832 my $order2 = GetOrder( $ordernumbers[1] );
833 is( $order2->{'quantityreceived'},
834 0, 'Splitting up order did not receive any on original order' );
835 is( $order2->{'quantity'}, 40, '40 items on original order' );
836 is( $order2->{'budget_id'}, $budgetid,
837 'Budget on original order is unchanged' );
838 is( $order2->{order_internalnote}, "my notes",
839 'ModReceiveOrder and GetOrder deal with internal notes' );
840 is( $order2->{order_vendornote}, "my vendor notes",
841 'ModReceiveOrder and GetOrder deal with vendor notes' );
843 $neworder = GetOrder($new_ordernumber);
844 is( $neworder->{'quantity'}, 2, '2 items on new order' );
845 is( $neworder->{'quantityreceived'},
846 2, 'Splitting up order received items on new order' );
847 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
849 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
850 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
852 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
853 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
854 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
855 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
857 my $budgetid2 = C4::Budgets::AddBudget(
859 budget_code => "budget_code_test_modrecv",
860 budget_name => "budget_name_test_modrecv",
864 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
866 biblionumber => $biblionumber2,
867 ordernumber => $ordernumbers[2],
868 quantityreceived => 2,
871 invoiceid => $invoiceid,
873 budget_id => $budgetid2,
874 order_internalnote => "my other notes",
878 my $order3 = GetOrder( $ordernumbers[2] );
879 is( $order3->{'quantityreceived'},
880 0, 'Splitting up order did not receive any on original order' );
881 is( $order3->{'quantity'}, 2, '2 items on original order' );
882 is( $order3->{'budget_id'}, $budgetid,
883 'Budget on original order is unchanged' );
884 is( $order3->{order_internalnote}, "my other notes",
885 'ModReceiveOrder and GetOrder deal with notes' );
887 $neworder = GetOrder($new_ordernumber);
888 is( $neworder->{'quantity'}, 2, '2 items on new order' );
889 is( $neworder->{'quantityreceived'},
890 2, 'Splitting up order received items on new order' );
891 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
893 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
895 biblionumber => $biblionumber2,
896 ordernumber => $ordernumbers[2],
897 quantityreceived => 2,
900 invoiceid => $invoiceid,
902 budget_id => $budgetid2,
903 order_internalnote => "my third notes",
907 $order3 = GetOrder( $ordernumbers[2] );
908 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
909 is( $order3->{'quantity'}, 2, '2 items on order' );
910 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
911 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
913 my $nonexistent_order = GetOrder();
914 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
915 $nonexistent_order = GetOrder( 424242424242 );
916 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
919 my $order1 = GetOrder($ordernumbers[0]);
920 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
921 ok((not defined $error), "DelOrder does not fail");
922 $order1 = GetOrder($order1->{ordernumber});
923 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
924 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
925 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
927 $order2 = GetOrder($ordernumbers[1]);
928 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
929 ok((not defined $error), "DelOrder does not fail");
930 $order2 = GetOrder($order2->{ordernumber});
931 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
932 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
933 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
935 my $order4 = GetOrder($ordernumbers[3]);
936 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
937 ok((not defined $error), "DelOrder does not fail");
938 $order4 = GetOrder($order4->{ordernumber});
939 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
940 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
941 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
942 # End of tests for DelOrder
945 my $all_count = scalar GetBudgetsReport();
946 ok($all_count >= 1, "GetBudgetReport OK");
948 my $active_count = scalar GetBudgetsReport(1);
949 ok($active_count >= 1 , "GetBudgetsReport(1) OK");
951 is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
952 ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
954 $schema->storage->txn_rollback();