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
381 estimateddeliverydate
389 $test_missing_fields, $test_extra_fields,
390 $test_different_fields, $test_nbr_fields
392 = _check_fields_of_order( \@expectedfields, $order_content[0],
393 GetOrder( $ordernumbers[0] ) );
396 scalar @expectedfields,
397 "GetOrder gets an order with the right number of fields"
399 is( join( " ", @$test_missing_fields ),
400 '', "GetOrder gets an order with no missing fields" );
401 is( join( " ", @$test_extra_fields ),
402 '', "GetOrder gets an order with no unexpected fields" );
403 is( join( " ", @$test_different_fields ),
404 '', "GetOrder gets an order with the right content in every fields" );
410 my @base_expectedfields = qw(
452 editionresponsibility
496 datecancellationprinted
500 ( @base_expectedfields,
501 ( 'transferred_from_timestamp', 'transferred_from' ) );
502 is( GetOrders(), undef, "GetOrders with no params returns undef" );
503 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
504 my @get_orders = GetOrders($basketno);
506 $test_missing_fields, $test_extra_fields,
507 $test_different_fields, $test_nbr_fields
509 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
511 $$test_nbr_fields[0],
512 scalar @expectedfields,
513 "GetOrders gets orders with the right number of fields"
515 is( join( " ", @$test_missing_fields ),
516 '', "GetOrders gets orders with no missing fields" );
517 is( join( " ", @$test_extra_fields ),
518 '', "GetOrders gets orders with no unexpected fields" );
519 is( join( " ", @$test_different_fields ),
520 '', "GetOrders gets orders with the right content in every fields" );
523 ( scalar @get_orders == 4 )
524 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
526 "GetOrders only gets non-cancelled orders"
530 # Test GetOrders { cancelled => 1 }
534 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
535 @get_orders = GetOrders($basketno, { cancelled => 1 });
537 $test_missing_fields, $test_extra_fields,
538 $test_different_fields, $test_nbr_fields
540 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
542 $$test_nbr_fields[0],
543 scalar @expectedfields,
544 "GetOrders { cancelled => 1 } gets orders with the right number of fields"
546 is( join( " ", @$test_missing_fields ),
547 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
548 is( join( " ", @$test_extra_fields ),
549 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
550 is( join( " ", @$test_different_fields ),
552 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
555 ( scalar @get_orders == 1 )
556 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
558 "GetOrders { cancelled => 1 } only gets cancelled orders"
565 @expectedfields = qw (
617 datecancellationprinted
624 # note that authorisedby was added to the return of SearchOrder by the
625 # patch for bug 11777
627 my $invoiceid = AddInvoice(
628 invoicenumber => 'invoice',
629 booksellerid => $booksellerid,
633 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
635 biblionumber => $biblionumber4,
636 ordernumber => $ordernumbers[4],
637 quantityreceived => 1,
640 invoiceid => $invoiceid,
642 budget_id => $order_content[4]->{str}->{budget_id},
646 my $search_orders = SearchOrders({
647 booksellerid => $booksellerid,
648 basketno => $basketno
650 isa_ok( $search_orders, 'ARRAY' );
652 $test_missing_fields, $test_extra_fields,
653 $test_different_fields, $test_nbr_fields
655 = _check_fields_of_orders( \@expectedfields, \@order_content,
658 $$test_nbr_fields[0],
659 scalar @expectedfields,
660 "SearchOrders gets orders with the right number of fields"
662 is( join( " ", @$test_missing_fields ),
663 '', "SearchOrders gets orders with no missing fields" );
664 is( join( " ", @$test_extra_fields ),
665 '', "SearchOrders gets orders with no unexpected fields" );
666 is( join( " ", @$test_different_fields ),
667 '', "SearchOrders gets orders with the right content in every fields" );
670 ( scalar @$search_orders == 4 )
671 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
673 "SearchOrders only gets non-cancelled orders"
676 $search_orders = SearchOrders({
677 booksellerid => $booksellerid,
678 basketno => $basketno,
683 ( scalar @$search_orders == 3 ) and !grep ( (
684 ( $_->{ordernumber} eq $ordernumbers[3] )
685 or ( $_->{ordernumber} eq $ordernumbers[4] )
689 "SearchOrders with pending params gets only pending orders (bug 10723)"
692 $search_orders = SearchOrders({
693 booksellerid => $booksellerid,
694 basketno => $basketno,
698 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
700 $search_orders = SearchOrders({
701 ordernumber => $ordernumbers[4]
703 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
705 $search_orders = SearchOrders({
706 biblionumber => $biblionumber4
708 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
710 $search_orders = SearchOrders({
711 biblionumber => $biblionumber4,
714 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
717 # Test GetBudgetByOrderNumber
719 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
720 "GetBudgetByOrderNumber returns expected budget" );
726 @expectedfields = qw (
739 estimateddeliverydate
750 my @lateorders = GetLateOrders(0);
751 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
752 0, "GetLateOrders does not get orders from opened baskets" );
753 C4::Acquisition::CloseBasket($basketno);
754 @lateorders = GetLateOrders(0);
755 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
756 0, "GetLateOrders gets orders from closed baskets" );
757 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
758 "GetLateOrders does not gets cancelled orders" );
759 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
760 "GetLateOrders does not gets reveived orders" );
762 $test_missing_fields, $test_extra_fields,
763 $test_different_fields, $test_nbr_fields
765 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
767 $$test_nbr_fields[0],
768 scalar @expectedfields,
769 "GetLateOrders gets orders with the right number of fields"
771 is( join( " ", @$test_missing_fields ),
772 '', "GetLateOrders gets orders with no missing fields" );
773 is( join( " ", @$test_extra_fields ),
774 '', "GetLateOrders gets orders with no unexpected fields" );
775 is( join( " ", @$test_different_fields ),
776 '', "GetLateOrders gets orders with the right content in every fields" );
778 $search_orders = SearchOrders({
779 booksellerid => $booksellerid,
780 basketno => $basketno,
784 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 my $order = $lateorders[0];
791 AddClaim( $order->{ordernumber} );
792 my $neworder = GetOrder( $order->{ordernumber} );
794 $neworder->{claimed_date},
795 strftime( "%Y-%m-%d", localtime(time) ),
796 "AddClaim : Check claimed_date"
799 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
801 biblionumber => $biblionumber2,
802 ordernumber => $ordernumbers[1],
803 quantityreceived => 2,
806 invoiceid => $invoiceid,
808 order_internalnote => "my notes",
809 order_vendornote => "my vendor notes",
812 my $order2 = GetOrder( $ordernumbers[1] );
813 is( $order2->{'quantityreceived'},
814 0, 'Splitting up order did not receive any on original order' );
815 is( $order2->{'quantity'}, 40, '40 items on original order' );
816 is( $order2->{'budget_id'}, $budgetid,
817 'Budget on original order is unchanged' );
818 is( $order2->{order_internalnote}, "my notes",
819 'ModReceiveOrder and GetOrder deal with internal notes' );
820 is( $order2->{order_vendornote}, "my vendor notes",
821 'ModReceiveOrder and GetOrder deal with vendor notes' );
823 $neworder = GetOrder($new_ordernumber);
824 is( $neworder->{'quantity'}, 2, '2 items on new order' );
825 is( $neworder->{'quantityreceived'},
826 2, 'Splitting up order received items on new order' );
827 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
829 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
830 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
832 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
833 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
834 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
835 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
837 my $budgetid2 = C4::Budgets::AddBudget(
839 budget_code => "budget_code_test_modrecv",
840 budget_name => "budget_name_test_modrecv",
844 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
846 biblionumber => $biblionumber2,
847 ordernumber => $ordernumbers[2],
848 quantityreceived => 2,
851 invoiceid => $invoiceid,
853 budget_id => $budgetid2,
854 order_internalnote => "my other notes",
858 my $order3 = GetOrder( $ordernumbers[2] );
859 is( $order3->{'quantityreceived'},
860 0, 'Splitting up order did not receive any on original order' );
861 is( $order3->{'quantity'}, 2, '2 items on original order' );
862 is( $order3->{'budget_id'}, $budgetid,
863 'Budget on original order is unchanged' );
864 is( $order3->{order_internalnote}, "my other notes",
865 'ModReceiveOrder and GetOrder deal with notes' );
867 $neworder = GetOrder($new_ordernumber);
868 is( $neworder->{'quantity'}, 2, '2 items on new order' );
869 is( $neworder->{'quantityreceived'},
870 2, 'Splitting up order received items on new order' );
871 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
873 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
875 biblionumber => $biblionumber2,
876 ordernumber => $ordernumbers[2],
877 quantityreceived => 2,
880 invoiceid => $invoiceid,
882 budget_id => $budgetid2,
883 order_internalnote => "my third notes",
887 $order3 = GetOrder( $ordernumbers[2] );
888 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
889 is( $order3->{'quantity'}, 2, '2 items on order' );
890 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
891 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
893 my $nonexistent_order = GetOrder();
894 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
895 $nonexistent_order = GetOrder( 424242424242 );
896 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
899 my $order1 = GetOrder($ordernumbers[0]);
900 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
901 ok((not defined $error), "DelOrder does not fail");
902 $order1 = GetOrder($order1->{ordernumber});
903 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
904 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
905 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
907 $order2 = GetOrder($ordernumbers[1]);
908 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
909 ok((not defined $error), "DelOrder does not fail");
910 $order2 = GetOrder($order2->{ordernumber});
911 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
912 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
913 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
915 my $order4 = GetOrder($ordernumbers[3]);
916 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
917 ok((not defined $error), "DelOrder does not fail");
918 $order4 = GetOrder($order4->{ordernumber});
919 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
920 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
921 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
922 # End of tests for DelOrder
924 $schema->storage->txn_rollback();