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 C4::Bookseller qw( GetBookSellerFromId );
24 use Test::More tests => 87;
27 use_ok('C4::Acquisition');
28 use_ok('C4::Bookseller');
30 use_ok('C4::Budgets');
31 use_ok('C4::Bookseller');
32 use_ok('Koha::Acquisition::Order');
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
120 my $dbh = C4::Context->dbh;
121 $dbh->{AutoCommit} = 0;
122 $dbh->{RaiseError} = 1;
124 # Creating some orders
125 my $booksellerid = C4::Bookseller::AddBookseller(
128 address1 => "bookseller's address",
135 my $booksellerinfo = C4::Bookseller::GetBookSellerFromId($booksellerid);
137 is( $booksellerinfo->{deliverytime},
138 5, 'set deliverytime when creating vendor (Bug 10556)' );
140 my ( $basket, $basketno );
142 $basketno = NewBasket( $booksellerid, 1 ),
143 "NewBasket( $booksellerid , 1 ) returns $basketno"
145 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
147 my $budgetid = C4::Budgets::AddBudget(
149 budget_code => "budget_code_test_getordersbybib",
150 budget_name => "budget_name_test_getordersbybib",
153 my $budget = C4::Budgets::GetBudget($budgetid);
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, '' );
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
164 my @order_content = (
167 basketno => $basketno,
168 biblionumber => $biblionumber1,
169 budget_id => $budget->{budget_id},
171 order_internalnote => "internal note",
172 order_vendornote => "vendor note",
177 listprice => 50.121111,
186 basketno => $basketno,
187 biblionumber => $biblionumber2,
188 budget_id => $budget->{budget_id}
190 num => { quantity => 42 }
194 basketno => $basketno,
195 biblionumber => $biblionumber2,
196 budget_id => $budget->{budget_id},
198 order_internalnote => "internal note",
199 order_vendornote => "vendor note"
214 basketno => $basketno,
215 biblionumber => $biblionumber3,
216 budget_id => $budget->{budget_id},
217 order_internalnote => "internal note",
218 order_vendornote => "vendor note"
234 basketno => $basketno,
235 biblionumber => $biblionumber4,
236 budget_id => $budget->{budget_id},
237 order_internalnote => "internal note",
238 order_vendornote => "vendor note"
254 # Create 4 orders in database
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[$_];
265 # Test UT sub _check_fields_of_order
268 $test_missing_fields, $test_extra_fields,
269 $test_different_fields, $test_nbr_fields
271 = _check_fields_of_order(
273 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
274 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
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' )
283 "_check_fields_of_order can check an order (test 1)"
286 $test_missing_fields, $test_extra_fields,
287 $test_different_fields, $test_nbr_fields
289 = _check_fields_of_order(
291 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
292 { a => "bla", b => "105", c => 15 }
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 )
301 "_check_fields_of_order can check an order (test 2)"
304 $test_missing_fields, $test_extra_fields,
305 $test_different_fields, $test_nbr_fields
307 = _check_fields_of_order(
309 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
310 { a => "blabla", b => "105", c => 15, d => "error" }
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' )
319 "_check_fields_of_order can check an order (test 3)"
326 my @expectedfields = qw(
341 datecancellationprinted
380 estimateddeliverydate
388 $test_missing_fields, $test_extra_fields,
389 $test_different_fields, $test_nbr_fields
391 = _check_fields_of_order( \@expectedfields, $order_content[0],
392 GetOrder( $ordernumbers[0] ) );
395 scalar @expectedfields,
396 "GetOrder gets an order with the right number of fields"
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" );
409 my @base_expectedfields = qw(
451 editionresponsibility
497 datecancellationprinted
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);
507 $test_missing_fields, $test_extra_fields,
508 $test_different_fields, $test_nbr_fields
510 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
512 $$test_nbr_fields[0],
513 scalar @expectedfields,
514 "GetOrders gets orders with the right number of fields"
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" );
524 ( scalar @get_orders == 4 )
525 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
527 "GetOrders only gets non-cancelled orders"
531 # Test GetCancelledOrders
535 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
536 is( GetCancelledOrders(), undef,
537 "GetCancelledOrders with no params returns undef" );
538 @get_orders = GetCancelledOrders($basketno);
540 $test_missing_fields, $test_extra_fields,
541 $test_different_fields, $test_nbr_fields
543 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
545 $$test_nbr_fields[0],
546 scalar @expectedfields,
547 "GetCancelledOrders gets orders with the right number of fields"
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 ),
555 "GetCancelledOrders gets orders with the right content in every fields" );
558 ( scalar @get_orders == 1 )
559 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
561 "GetCancelledOrders only gets cancelled orders"
568 @expectedfields = qw (
622 datecancellationprinted
629 # note that authorisedby was added to the return of SearchOrder by the
630 # patch for bug 11777
632 my $invoiceid = AddInvoice(
633 invoicenumber => 'invoice',
634 booksellerid => $booksellerid,
638 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
640 biblionumber => $biblionumber4,
641 ordernumber => $ordernumbers[4],
642 quantityreceived => 1,
645 invoiceid => $invoiceid,
647 budget_id => $order_content[4]->{str}->{budget_id},
651 my $search_orders = SearchOrders({
652 booksellerid => $booksellerid,
653 basketno => $basketno
655 isa_ok( $search_orders, 'ARRAY' );
657 $test_missing_fields, $test_extra_fields,
658 $test_different_fields, $test_nbr_fields
660 = _check_fields_of_orders( \@expectedfields, \@order_content,
663 $$test_nbr_fields[0],
664 scalar @expectedfields,
665 "SearchOrders gets orders with the right number of fields"
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" );
675 ( scalar @$search_orders == 4 )
676 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
678 "SearchOrders only gets non-cancelled orders"
681 $search_orders = SearchOrders({
682 booksellerid => $booksellerid,
683 basketno => $basketno,
688 ( scalar @$search_orders == 3 ) and !grep ( (
689 ( $_->{ordernumber} eq $ordernumbers[3] )
690 or ( $_->{ordernumber} eq $ordernumbers[4] )
694 "SearchOrders with pending params gets only pending orders (bug 10723)"
697 $search_orders = SearchOrders({
698 booksellerid => $booksellerid,
699 basketno => $basketno,
703 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
705 $search_orders = SearchOrders({
706 ordernumber => $ordernumbers[4]
708 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
710 $search_orders = SearchOrders({
711 biblionumber => $biblionumber4
713 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
715 $search_orders = SearchOrders({
716 biblionumber => $biblionumber4,
719 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
722 # Test GetBudgetByOrderNumber
724 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
725 "GetBudgetByOrderNumber returns expected budget" );
731 @expectedfields = qw (
744 estimateddeliverydate
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" );
767 $test_missing_fields, $test_extra_fields,
768 $test_different_fields, $test_nbr_fields
770 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
772 $$test_nbr_fields[0],
773 scalar @expectedfields,
774 "GetLateOrders gets orders with the right number of fields"
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" );
783 $search_orders = SearchOrders({
784 booksellerid => $booksellerid,
785 basketno => $basketno,
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)" );
795 my $order = $lateorders[0];
796 AddClaim( $order->{ordernumber} );
797 my $neworder = GetOrder( $order->{ordernumber} );
799 $neworder->{claimed_date},
800 strftime( "%Y-%m-%d", localtime(time) ),
801 "AddClaim : Check claimed_date"
804 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
806 biblionumber => $biblionumber2,
807 ordernumber => $ordernumbers[1],
808 quantityreceived => 2,
811 invoiceid => $invoiceid,
813 order_internalnote => "my notes",
814 order_vendornote => "my vendor notes",
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' );
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' );
834 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
835 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
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' );
842 my $budgetid2 = C4::Budgets::AddBudget(
844 budget_code => "budget_code_test_modrecv",
845 budget_name => "budget_name_test_modrecv",
849 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
851 biblionumber => $biblionumber2,
852 ordernumber => $ordernumbers[2],
853 quantityreceived => 2,
856 invoiceid => $invoiceid,
858 budget_id => $budgetid2,
859 order_internalnote => "my other notes",
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' );
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' );
878 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
880 biblionumber => $biblionumber2,
881 ordernumber => $ordernumbers[2],
882 quantityreceived => 2,
885 invoiceid => $invoiceid,
887 budget_id => $budgetid2,
888 order_internalnote => "my third notes",
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' );
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' );
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");
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");
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