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 => 87;
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 $budgetid = C4::Budgets::AddBudget(
152 budget_code => "budget_code_test_getordersbybib",
153 budget_name => "budget_name_test_getordersbybib",
156 my $budget = C4::Budgets::GetBudget($budgetid);
159 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
160 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
161 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
162 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
164 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
165 # Ex : a price of 50.1 will be stored internally as 5.100000
167 my @order_content = (
170 basketno => $basketno,
171 biblionumber => $biblionumber1,
172 budget_id => $budget->{budget_id},
174 order_internalnote => "internal note",
175 order_vendornote => "vendor note",
180 listprice => 50.121111,
189 basketno => $basketno,
190 biblionumber => $biblionumber2,
191 budget_id => $budget->{budget_id}
193 num => { quantity => 42 }
197 basketno => $basketno,
198 biblionumber => $biblionumber2,
199 budget_id => $budget->{budget_id},
201 order_internalnote => "internal note",
202 order_vendornote => "vendor note"
217 basketno => $basketno,
218 biblionumber => $biblionumber3,
219 budget_id => $budget->{budget_id},
220 order_internalnote => "internal note",
221 order_vendornote => "vendor note"
237 basketno => $basketno,
238 biblionumber => $biblionumber4,
239 budget_id => $budget->{budget_id},
240 order_internalnote => "internal note",
241 order_vendornote => "vendor note"
257 # Create 4 orders in database
260 @ocontent{ keys %{ $order_content[$_]->{num} } } =
261 values %{ $order_content[$_]->{num} };
262 @ocontent{ keys %{ $order_content[$_]->{str} } } =
263 values %{ $order_content[$_]->{str} };
264 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
265 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
268 # Test UT sub _check_fields_of_order
271 $test_missing_fields, $test_extra_fields,
272 $test_different_fields, $test_nbr_fields
274 = _check_fields_of_order(
276 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
277 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
281 ( $test_nbr_fields == 5 )
282 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
283 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
284 and ( join( " ", @$test_different_fields ) eq 'a' )
286 "_check_fields_of_order can check an order (test 1)"
289 $test_missing_fields, $test_extra_fields,
290 $test_different_fields, $test_nbr_fields
292 = _check_fields_of_order(
294 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
295 { a => "bla", b => "105", c => 15 }
299 ( $test_nbr_fields == 3 )
300 and ( scalar @$test_missing_fields == 0 )
301 and ( scalar @$test_extra_fields == 0 )
302 and ( scalar @$test_different_fields == 0 )
304 "_check_fields_of_order can check an order (test 2)"
307 $test_missing_fields, $test_extra_fields,
308 $test_different_fields, $test_nbr_fields
310 = _check_fields_of_order(
312 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
313 { a => "blabla", b => "105", c => 15, d => "error" }
317 ( $test_nbr_fields == 4 )
318 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
319 and ( scalar @$test_extra_fields == 0 )
320 and ( join( " ", @$test_different_fields ) eq 'a c' )
322 "_check_fields_of_order can check an order (test 3)"
329 my @expectedfields = qw(
343 datecancellationprinted
367 suppliers_reference_number
368 suppliers_reference_qualifier
385 estimateddeliverydate
393 $test_missing_fields, $test_extra_fields,
394 $test_different_fields, $test_nbr_fields
396 = _check_fields_of_order( \@expectedfields, $order_content[0],
397 GetOrder( $ordernumbers[0] ) );
400 scalar @expectedfields,
401 "GetOrder gets an order with the right number of fields"
403 is( join( " ", @$test_missing_fields ),
404 '', "GetOrder gets an order with no missing fields" );
405 is( join( " ", @$test_extra_fields ),
406 '', "GetOrder gets an order with no unexpected fields" );
407 is( join( " ", @$test_different_fields ),
408 '', "GetOrder gets an order with the right content in every fields" );
414 my @base_expectedfields = qw(
456 editionresponsibility
466 suppliers_reference_number
467 suppliers_reference_qualifier
504 datecancellationprinted
508 ( @base_expectedfields,
509 ( 'transferred_from_timestamp', 'transferred_from' ) );
510 is( GetOrders(), undef, "GetOrders with no params returns undef" );
511 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
512 my @get_orders = GetOrders($basketno);
514 $test_missing_fields, $test_extra_fields,
515 $test_different_fields, $test_nbr_fields
517 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
519 $$test_nbr_fields[0],
520 scalar @expectedfields,
521 "GetOrders gets orders with the right number of fields"
523 is( join( " ", @$test_missing_fields ),
524 '', "GetOrders gets orders with no missing fields" );
525 is( join( " ", @$test_extra_fields ),
526 '', "GetOrders gets orders with no unexpected fields" );
527 is( join( " ", @$test_different_fields ),
528 '', "GetOrders gets orders with the right content in every fields" );
531 ( scalar @get_orders == 4 )
532 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
534 "GetOrders only gets non-cancelled orders"
538 # Test GetOrders { cancelled => 1 }
542 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
543 @get_orders = GetOrders($basketno, { cancelled => 1 });
545 $test_missing_fields, $test_extra_fields,
546 $test_different_fields, $test_nbr_fields
548 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
550 $$test_nbr_fields[0],
551 scalar @expectedfields,
552 "GetOrders { cancelled => 1 } gets orders with the right number of fields"
554 is( join( " ", @$test_missing_fields ),
555 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
556 is( join( " ", @$test_extra_fields ),
557 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
558 is( join( " ", @$test_different_fields ),
560 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
563 ( scalar @get_orders == 1 )
564 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
566 "GetOrders { cancelled => 1 } only gets cancelled orders"
573 @expectedfields = qw (
587 suppliers_reference_number
588 suppliers_reference_qualifier
629 datecancellationprinted
636 # note that authorisedby was added to the return of SearchOrder by the
637 # patch for bug 11777
639 my $invoiceid = AddInvoice(
640 invoicenumber => 'invoice',
641 booksellerid => $booksellerid,
645 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
647 biblionumber => $biblionumber4,
648 ordernumber => $ordernumbers[4],
649 quantityreceived => 1,
652 invoiceid => $invoiceid,
654 budget_id => $order_content[4]->{str}->{budget_id},
658 my $search_orders = SearchOrders({
659 booksellerid => $booksellerid,
660 basketno => $basketno
662 isa_ok( $search_orders, 'ARRAY' );
664 $test_missing_fields, $test_extra_fields,
665 $test_different_fields, $test_nbr_fields
667 = _check_fields_of_orders( \@expectedfields, \@order_content,
670 $$test_nbr_fields[0],
671 scalar @expectedfields,
672 "SearchOrders gets orders with the right number of fields"
674 is( join( " ", @$test_missing_fields ),
675 '', "SearchOrders gets orders with no missing fields" );
676 is( join( " ", @$test_extra_fields ),
677 '', "SearchOrders gets orders with no unexpected fields" );
678 is( join( " ", @$test_different_fields ),
679 '', "SearchOrders gets orders with the right content in every fields" );
682 ( scalar @$search_orders == 4 )
683 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
685 "SearchOrders only gets non-cancelled orders"
688 $search_orders = SearchOrders({
689 booksellerid => $booksellerid,
690 basketno => $basketno,
695 ( scalar @$search_orders == 3 ) and !grep ( (
696 ( $_->{ordernumber} eq $ordernumbers[3] )
697 or ( $_->{ordernumber} eq $ordernumbers[4] )
701 "SearchOrders with pending params gets only pending orders (bug 10723)"
704 $search_orders = SearchOrders({
705 booksellerid => $booksellerid,
706 basketno => $basketno,
710 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
712 $search_orders = SearchOrders({
713 ordernumber => $ordernumbers[4]
715 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
717 $search_orders = SearchOrders({
718 biblionumber => $biblionumber4
720 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
722 $search_orders = SearchOrders({
723 biblionumber => $biblionumber4,
726 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
729 # Test GetBudgetByOrderNumber
731 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
732 "GetBudgetByOrderNumber returns expected budget" );
738 @expectedfields = qw (
751 estimateddeliverydate
762 my @lateorders = GetLateOrders(0);
763 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
764 0, "GetLateOrders does not get orders from opened baskets" );
765 C4::Acquisition::CloseBasket($basketno);
766 @lateorders = GetLateOrders(0);
767 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
768 0, "GetLateOrders gets orders from closed baskets" );
769 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
770 "GetLateOrders does not gets cancelled orders" );
771 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
772 "GetLateOrders does not gets reveived orders" );
774 $test_missing_fields, $test_extra_fields,
775 $test_different_fields, $test_nbr_fields
777 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
779 $$test_nbr_fields[0],
780 scalar @expectedfields,
781 "GetLateOrders gets orders with the right number of fields"
783 is( join( " ", @$test_missing_fields ),
784 '', "GetLateOrders gets orders with no missing fields" );
785 is( join( " ", @$test_extra_fields ),
786 '', "GetLateOrders gets orders with no unexpected fields" );
787 is( join( " ", @$test_different_fields ),
788 '', "GetLateOrders gets orders with the right content in every fields" );
790 $search_orders = SearchOrders({
791 booksellerid => $booksellerid,
792 basketno => $basketno,
796 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)" );
802 my $order = $lateorders[0];
803 AddClaim( $order->{ordernumber} );
804 my $neworder = GetOrder( $order->{ordernumber} );
806 $neworder->{claimed_date},
807 strftime( "%Y-%m-%d", localtime(time) ),
808 "AddClaim : Check claimed_date"
811 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
813 biblionumber => $biblionumber2,
814 ordernumber => $ordernumbers[1],
815 quantityreceived => 2,
818 invoiceid => $invoiceid,
820 order_internalnote => "my notes",
821 order_vendornote => "my vendor notes",
824 my $order2 = GetOrder( $ordernumbers[1] );
825 is( $order2->{'quantityreceived'},
826 0, 'Splitting up order did not receive any on original order' );
827 is( $order2->{'quantity'}, 40, '40 items on original order' );
828 is( $order2->{'budget_id'}, $budgetid,
829 'Budget on original order is unchanged' );
830 is( $order2->{order_internalnote}, "my notes",
831 'ModReceiveOrder and GetOrder deal with internal notes' );
832 is( $order2->{order_vendornote}, "my vendor notes",
833 'ModReceiveOrder and GetOrder deal with vendor notes' );
835 $neworder = GetOrder($new_ordernumber);
836 is( $neworder->{'quantity'}, 2, '2 items on new order' );
837 is( $neworder->{'quantityreceived'},
838 2, 'Splitting up order received items on new order' );
839 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
841 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
842 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
844 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
845 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
846 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
847 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
849 my $budgetid2 = C4::Budgets::AddBudget(
851 budget_code => "budget_code_test_modrecv",
852 budget_name => "budget_name_test_modrecv",
856 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
858 biblionumber => $biblionumber2,
859 ordernumber => $ordernumbers[2],
860 quantityreceived => 2,
863 invoiceid => $invoiceid,
865 budget_id => $budgetid2,
866 order_internalnote => "my other notes",
870 my $order3 = GetOrder( $ordernumbers[2] );
871 is( $order3->{'quantityreceived'},
872 0, 'Splitting up order did not receive any on original order' );
873 is( $order3->{'quantity'}, 2, '2 items on original order' );
874 is( $order3->{'budget_id'}, $budgetid,
875 'Budget on original order is unchanged' );
876 is( $order3->{order_internalnote}, "my other notes",
877 'ModReceiveOrder and GetOrder deal with notes' );
879 $neworder = GetOrder($new_ordernumber);
880 is( $neworder->{'quantity'}, 2, '2 items on new order' );
881 is( $neworder->{'quantityreceived'},
882 2, 'Splitting up order received items on new order' );
883 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
885 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
887 biblionumber => $biblionumber2,
888 ordernumber => $ordernumbers[2],
889 quantityreceived => 2,
892 invoiceid => $invoiceid,
894 budget_id => $budgetid2,
895 order_internalnote => "my third notes",
899 $order3 = GetOrder( $ordernumbers[2] );
900 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
901 is( $order3->{'quantity'}, 2, '2 items on order' );
902 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
903 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
905 my $nonexistent_order = GetOrder();
906 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
907 $nonexistent_order = GetOrder( 424242424242 );
908 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
911 my $order1 = GetOrder($ordernumbers[0]);
912 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
913 ok((not defined $error), "DelOrder does not fail");
914 $order1 = GetOrder($order1->{ordernumber});
915 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
916 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
917 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
919 $order2 = GetOrder($ordernumbers[1]);
920 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
921 ok((not defined $error), "DelOrder does not fail");
922 $order2 = GetOrder($order2->{ordernumber});
923 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
924 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
925 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
927 my $order4 = GetOrder($ordernumbers[3]);
928 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
929 ok((not defined $error), "DelOrder does not fail");
930 $order4 = GetOrder($order4->{ordernumber});
931 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
932 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
933 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
934 # End of tests for DelOrder
936 $schema->storage->txn_rollback();