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;
25 use_ok('C4::Acquisition');
26 use_ok('C4::Bookseller');
28 use_ok('C4::Budgets');
29 use_ok('C4::Bookseller');
30 use_ok('Koha::Acquisition::Order');
31 use_ok('Koha::Acquisition::Bookseller');
34 # Sub used for testing C4::Acquisition subs returning order(s):
35 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
36 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
37 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
39 # $exp_fields : arrayref whose elements are the keys we expect to find
40 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
41 # containing content fields of the order created with Koha::Acquisition::Order
42 # $order_to_check : hashref whose keys/values are the content of an order
43 # returned by the C4::Acquisition sub we are testing
45 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
46 # fields missing in $order_to_check
47 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
48 # fields unexpected in $order_to_check
49 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
50 # fields which value is not the same in between $order_to_check and
51 # $test_nbr_fields : contains the number of fields of $order_to_check
53 sub _check_fields_of_order {
54 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
55 my @test_missing_fields = ();
56 my @test_extra_fields = ();
57 my @test_different_fields = ();
58 my $test_nbr_fields = scalar( keys %$order_to_check );
59 foreach my $field (@$exp_fields) {
60 push @test_missing_fields, $field
61 unless exists( $order_to_check->{$field} );
63 foreach my $field ( keys %$order_to_check ) {
64 push @test_extra_fields, $field
65 unless grep ( /^$field$/, @$exp_fields );
67 foreach my $field ( keys %{ $original_order_content->{str} } ) {
68 push @test_different_fields, $field
69 unless ( !exists $order_to_check->{$field} )
70 or ( $original_order_content->{str}->{$field} eq
71 $order_to_check->{$field} );
73 foreach my $field ( keys %{ $original_order_content->{num} } ) {
74 push @test_different_fields, $field
75 unless ( !exists $order_to_check->{$field} )
76 or ( $original_order_content->{num}->{$field} ==
77 $order_to_check->{$field} );
80 \@test_missing_fields, \@test_extra_fields,
81 \@test_different_fields, $test_nbr_fields
85 # Sub used for testing C4::Acquisition subs returning several orders
86 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
87 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
88 sub _check_fields_of_orders {
89 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
90 my @test_missing_fields = ();
91 my @test_extra_fields = ();
92 my @test_different_fields = ();
93 my @test_nbr_fields = ();
94 foreach my $order_to_check (@$orders_to_check) {
95 my $original_order_content =
96 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
97 @$original_orders_content )[0];
99 $t_missing_fields, $t_extra_fields,
100 $t_different_fields, $t_nbr_fields
102 = _check_fields_of_order( $exp_fields, $original_order_content,
104 push @test_missing_fields, @$t_missing_fields;
105 push @test_extra_fields, @$t_extra_fields;
106 push @test_different_fields, @$t_different_fields;
107 push @test_nbr_fields, $t_nbr_fields;
109 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
110 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
111 @test_different_fields =
112 keys %{ { map { $_ => 1 } @test_different_fields } };
114 \@test_missing_fields, \@test_extra_fields,
115 \@test_different_fields, \@test_nbr_fields
119 my $dbh = C4::Context->dbh;
120 $dbh->{AutoCommit} = 0;
121 $dbh->{RaiseError} = 1;
123 # Creating some orders
124 my $booksellerid = C4::Bookseller::AddBookseller(
127 address1 => "bookseller's address",
134 my $booksellerinfo = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
136 is( $booksellerinfo->{deliverytime},
137 5, 'set deliverytime when creating vendor (Bug 10556)' );
139 my ( $basket, $basketno );
141 $basketno = NewBasket( $booksellerid, 1 ),
142 "NewBasket( $booksellerid , 1 ) returns $basketno"
144 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
146 my $budgetid = C4::Budgets::AddBudget(
148 budget_code => "budget_code_test_getordersbybib",
149 budget_name => "budget_name_test_getordersbybib",
152 my $budget = C4::Budgets::GetBudget($budgetid);
155 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
156 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
157 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
158 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
160 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
161 # Ex : a price of 50.1 will be stored internally as 5.100000
163 my @order_content = (
166 basketno => $basketno,
167 biblionumber => $biblionumber1,
168 budget_id => $budget->{budget_id},
170 order_internalnote => "internal note",
171 order_vendornote => "vendor note",
176 listprice => 50.121111,
185 basketno => $basketno,
186 biblionumber => $biblionumber2,
187 budget_id => $budget->{budget_id}
189 num => { quantity => 42 }
193 basketno => $basketno,
194 biblionumber => $biblionumber2,
195 budget_id => $budget->{budget_id},
197 order_internalnote => "internal note",
198 order_vendornote => "vendor note"
213 basketno => $basketno,
214 biblionumber => $biblionumber3,
215 budget_id => $budget->{budget_id},
216 order_internalnote => "internal note",
217 order_vendornote => "vendor note"
233 basketno => $basketno,
234 biblionumber => $biblionumber4,
235 budget_id => $budget->{budget_id},
236 order_internalnote => "internal note",
237 order_vendornote => "vendor note"
253 # Create 4 orders in database
256 @ocontent{ keys %{ $order_content[$_]->{num} } } =
257 values %{ $order_content[$_]->{num} };
258 @ocontent{ keys %{ $order_content[$_]->{str} } } =
259 values %{ $order_content[$_]->{str} };
260 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
261 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
264 # Test UT sub _check_fields_of_order
267 $test_missing_fields, $test_extra_fields,
268 $test_different_fields, $test_nbr_fields
270 = _check_fields_of_order(
272 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
273 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
277 ( $test_nbr_fields == 5 )
278 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
279 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
280 and ( join( " ", @$test_different_fields ) eq 'a' )
282 "_check_fields_of_order can check an order (test 1)"
285 $test_missing_fields, $test_extra_fields,
286 $test_different_fields, $test_nbr_fields
288 = _check_fields_of_order(
290 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
291 { a => "bla", b => "105", c => 15 }
295 ( $test_nbr_fields == 3 )
296 and ( scalar @$test_missing_fields == 0 )
297 and ( scalar @$test_extra_fields == 0 )
298 and ( scalar @$test_different_fields == 0 )
300 "_check_fields_of_order can check an order (test 2)"
303 $test_missing_fields, $test_extra_fields,
304 $test_different_fields, $test_nbr_fields
306 = _check_fields_of_order(
308 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
309 { a => "blabla", b => "105", c => 15, d => "error" }
313 ( $test_nbr_fields == 4 )
314 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
315 and ( scalar @$test_extra_fields == 0 )
316 and ( join( " ", @$test_different_fields ) eq 'a c' )
318 "_check_fields_of_order can check an order (test 3)"
325 my @expectedfields = qw(
339 datecancellationprinted
377 estimateddeliverydate
385 $test_missing_fields, $test_extra_fields,
386 $test_different_fields, $test_nbr_fields
388 = _check_fields_of_order( \@expectedfields, $order_content[0],
389 GetOrder( $ordernumbers[0] ) );
392 scalar @expectedfields,
393 "GetOrder gets an order with the right number of fields"
395 is( join( " ", @$test_missing_fields ),
396 '', "GetOrder gets an order with no missing fields" );
397 is( join( " ", @$test_extra_fields ),
398 '', "GetOrder gets an order with no unexpected fields" );
399 is( join( " ", @$test_different_fields ),
400 '', "GetOrder gets an order with the right content in every fields" );
406 my @base_expectedfields = qw(
448 editionresponsibility
492 datecancellationprinted
496 ( @base_expectedfields,
497 ( 'transferred_from_timestamp', 'transferred_from' ) );
498 is( GetOrders(), undef, "GetOrders with no params returns undef" );
499 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
500 my @get_orders = GetOrders($basketno);
502 $test_missing_fields, $test_extra_fields,
503 $test_different_fields, $test_nbr_fields
505 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
507 $$test_nbr_fields[0],
508 scalar @expectedfields,
509 "GetOrders gets orders with the right number of fields"
511 is( join( " ", @$test_missing_fields ),
512 '', "GetOrders gets orders with no missing fields" );
513 is( join( " ", @$test_extra_fields ),
514 '', "GetOrders gets orders with no unexpected fields" );
515 is( join( " ", @$test_different_fields ),
516 '', "GetOrders gets orders with the right content in every fields" );
519 ( scalar @get_orders == 4 )
520 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
522 "GetOrders only gets non-cancelled orders"
526 # Test GetOrders { cancelled => 1 }
530 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
531 @get_orders = GetOrders($basketno, { cancelled => 1 });
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 { cancelled => 1 } gets orders with the right number of fields"
542 is( join( " ", @$test_missing_fields ),
543 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
544 is( join( " ", @$test_extra_fields ),
545 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
546 is( join( " ", @$test_different_fields ),
548 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
551 ( scalar @get_orders == 1 )
552 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
554 "GetOrders { cancelled => 1 } only gets cancelled orders"
561 @expectedfields = qw (
613 datecancellationprinted
620 # note that authorisedby was added to the return of SearchOrder by the
621 # patch for bug 11777
623 my $invoiceid = AddInvoice(
624 invoicenumber => 'invoice',
625 booksellerid => $booksellerid,
629 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
631 biblionumber => $biblionumber4,
632 ordernumber => $ordernumbers[4],
633 quantityreceived => 1,
636 invoiceid => $invoiceid,
638 budget_id => $order_content[4]->{str}->{budget_id},
642 my $search_orders = SearchOrders({
643 booksellerid => $booksellerid,
644 basketno => $basketno
646 isa_ok( $search_orders, 'ARRAY' );
648 $test_missing_fields, $test_extra_fields,
649 $test_different_fields, $test_nbr_fields
651 = _check_fields_of_orders( \@expectedfields, \@order_content,
654 $$test_nbr_fields[0],
655 scalar @expectedfields,
656 "SearchOrders gets orders with the right number of fields"
658 is( join( " ", @$test_missing_fields ),
659 '', "SearchOrders gets orders with no missing fields" );
660 is( join( " ", @$test_extra_fields ),
661 '', "SearchOrders gets orders with no unexpected fields" );
662 is( join( " ", @$test_different_fields ),
663 '', "SearchOrders gets orders with the right content in every fields" );
666 ( scalar @$search_orders == 4 )
667 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
669 "SearchOrders only gets non-cancelled orders"
672 $search_orders = SearchOrders({
673 booksellerid => $booksellerid,
674 basketno => $basketno,
679 ( scalar @$search_orders == 3 ) and !grep ( (
680 ( $_->{ordernumber} eq $ordernumbers[3] )
681 or ( $_->{ordernumber} eq $ordernumbers[4] )
685 "SearchOrders with pending params gets only pending orders (bug 10723)"
688 $search_orders = SearchOrders({
689 booksellerid => $booksellerid,
690 basketno => $basketno,
694 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
696 $search_orders = SearchOrders({
697 ordernumber => $ordernumbers[4]
699 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
701 $search_orders = SearchOrders({
702 biblionumber => $biblionumber4
704 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
706 $search_orders = SearchOrders({
707 biblionumber => $biblionumber4,
710 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
713 # Test GetBudgetByOrderNumber
715 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
716 "GetBudgetByOrderNumber returns expected budget" );
722 @expectedfields = qw (
735 estimateddeliverydate
746 my @lateorders = GetLateOrders(0);
747 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
748 0, "GetLateOrders does not get orders from opened baskets" );
749 C4::Acquisition::CloseBasket($basketno);
750 @lateorders = GetLateOrders(0);
751 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
752 0, "GetLateOrders gets orders from closed baskets" );
753 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
754 "GetLateOrders does not gets cancelled orders" );
755 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
756 "GetLateOrders does not gets reveived orders" );
758 $test_missing_fields, $test_extra_fields,
759 $test_different_fields, $test_nbr_fields
761 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
763 $$test_nbr_fields[0],
764 scalar @expectedfields,
765 "GetLateOrders gets orders with the right number of fields"
767 is( join( " ", @$test_missing_fields ),
768 '', "GetLateOrders gets orders with no missing fields" );
769 is( join( " ", @$test_extra_fields ),
770 '', "GetLateOrders gets orders with no unexpected fields" );
771 is( join( " ", @$test_different_fields ),
772 '', "GetLateOrders gets orders with the right content in every fields" );
774 $search_orders = SearchOrders({
775 booksellerid => $booksellerid,
776 basketno => $basketno,
780 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)" );
786 my $order = $lateorders[0];
787 AddClaim( $order->{ordernumber} );
788 my $neworder = GetOrder( $order->{ordernumber} );
790 $neworder->{claimed_date},
791 strftime( "%Y-%m-%d", localtime(time) ),
792 "AddClaim : Check claimed_date"
795 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
797 biblionumber => $biblionumber2,
798 ordernumber => $ordernumbers[1],
799 quantityreceived => 2,
802 invoiceid => $invoiceid,
804 order_internalnote => "my notes",
805 order_vendornote => "my vendor notes",
808 my $order2 = GetOrder( $ordernumbers[1] );
809 is( $order2->{'quantityreceived'},
810 0, 'Splitting up order did not receive any on original order' );
811 is( $order2->{'quantity'}, 40, '40 items on original order' );
812 is( $order2->{'budget_id'}, $budgetid,
813 'Budget on original order is unchanged' );
814 is( $order2->{order_internalnote}, "my notes",
815 'ModReceiveOrder and GetOrder deal with internal notes' );
816 is( $order2->{order_vendornote}, "my vendor notes",
817 'ModReceiveOrder and GetOrder deal with vendor notes' );
819 $neworder = GetOrder($new_ordernumber);
820 is( $neworder->{'quantity'}, 2, '2 items on new order' );
821 is( $neworder->{'quantityreceived'},
822 2, 'Splitting up order received items on new order' );
823 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
825 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
826 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
828 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
829 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
830 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
831 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
833 my $budgetid2 = C4::Budgets::AddBudget(
835 budget_code => "budget_code_test_modrecv",
836 budget_name => "budget_name_test_modrecv",
840 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
842 biblionumber => $biblionumber2,
843 ordernumber => $ordernumbers[2],
844 quantityreceived => 2,
847 invoiceid => $invoiceid,
849 budget_id => $budgetid2,
850 order_internalnote => "my other notes",
854 my $order3 = GetOrder( $ordernumbers[2] );
855 is( $order3->{'quantityreceived'},
856 0, 'Splitting up order did not receive any on original order' );
857 is( $order3->{'quantity'}, 2, '2 items on original order' );
858 is( $order3->{'budget_id'}, $budgetid,
859 'Budget on original order is unchanged' );
860 is( $order3->{order_internalnote}, "my other notes",
861 'ModReceiveOrder and GetOrder deal with notes' );
863 $neworder = GetOrder($new_ordernumber);
864 is( $neworder->{'quantity'}, 2, '2 items on new order' );
865 is( $neworder->{'quantityreceived'},
866 2, 'Splitting up order received items on new order' );
867 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
869 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
871 biblionumber => $biblionumber2,
872 ordernumber => $ordernumbers[2],
873 quantityreceived => 2,
876 invoiceid => $invoiceid,
878 budget_id => $budgetid2,
879 order_internalnote => "my third notes",
883 $order3 = GetOrder( $ordernumbers[2] );
884 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
885 is( $order3->{'quantity'}, 2, '2 items on order' );
886 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
887 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
889 my $nonexistent_order = GetOrder();
890 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
891 $nonexistent_order = GetOrder( 424242424242 );
892 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
895 my $order1 = GetOrder($ordernumbers[0]);
896 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
897 ok((not defined $error), "DelOrder does not fail");
898 $order1 = GetOrder($order1->{ordernumber});
899 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
900 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
901 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
903 $order2 = GetOrder($ordernumbers[1]);
904 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
905 ok((not defined $error), "DelOrder does not fail");
906 $order2 = GetOrder($order2->{ordernumber});
907 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
908 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
909 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
911 my $order4 = GetOrder($ordernumbers[3]);
912 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
913 ok((not defined $error), "DelOrder does not fail");
914 $order4 = GetOrder($order4->{ordernumber});
915 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
916 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
917 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
918 # End of tests for DelOrder