Bug 16609: Catch warning from Koha::Hold in Hold.t
[koha.git] / t / db_dependent / Acquisition.t
index ed352b0..7c80387 100755 (executable)
@@ -1,14 +1,26 @@
 #!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# This Koha test module is a stub!
-# Add more tests here!!!
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use POSIX qw(strftime);
 
-use C4::Bookseller qw( GetBookSellerFromId );
+use POSIX qw(strftime);
 
-use Test::More tests => 71;
+use Test::More tests => 91;
+use Koha::Database;
 
 BEGIN {
     use_ok('C4::Acquisition');
@@ -16,6 +28,8 @@ BEGIN {
     use_ok('C4::Biblio');
     use_ok('C4::Budgets');
     use_ok('C4::Bookseller');
+    use_ok('Koha::Acquisition::Order');
+    use_ok('Koha::Acquisition::Bookseller');
 }
 
 # Sub used for testing C4::Acquisition subs returning order(s):
@@ -25,7 +39,7 @@ BEGIN {
 # params :
 # $exp_fields             : arrayref whose elements are the keys we expect to find
 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
-#                           containing content fields of the order created with NewOrder
+#                           containing content fields of the order created with Koha::Acquisition::Order
 # $order_to_check         : hashref whose keys/values are the content of an order
 #                           returned by the C4::Acquisition sub we are testing
 # returns :
@@ -103,8 +117,11 @@ sub _check_fields_of_orders {
     );
 }
 
+
+my $schema = Koha::Database->new()->schema();
+$schema->storage->txn_begin();
+
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
 # Creating some orders
@@ -118,7 +135,7 @@ my $booksellerid = C4::Bookseller::AddBookseller(
     }
 );
 
-my $booksellerinfo = C4::Bookseller::GetBookSellerFromId($booksellerid);
+my $booksellerinfo = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
 
 is( $booksellerinfo->{deliverytime},
     5, 'set deliverytime when creating vendor (Bug 10556)' );
@@ -130,10 +147,18 @@ ok(
 );
 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
 
+my $bpid=AddBudgetPeriod({
+        budget_period_startdate => '2008-01-01'
+        , budget_period_enddate => '2008-12-31'
+        , budget_period_active  => 1
+        , budget_period_description    => "MAPERI"
+});
+
 my $budgetid = C4::Budgets::AddBudget(
     {
         budget_code => "budget_code_test_getordersbybib",
         budget_name => "budget_name_test_getordersbybib",
+        budget_period_id => $bpid,
     }
 );
 my $budget = C4::Budgets::GetBudget($budgetid);
@@ -144,46 +169,6 @@ my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
 
-#
-# Test NewOrder
-#
-
-my ( $mandatoryparams, $return_error, $basketnum );
-
-# returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing
-eval { ( $basketnum, $ordernumbers[0] ) = C4::Acquisition::NewOrder() };
-$return_error = $@;
-ok(
-    ( !( defined $basketnum || defined $ordernumbers[0] ) )
-      && ( defined $return_error ),
-    "NewOrder with no params returns undef and croaks"
-);
-
-$mandatoryparams = {
-    basketno     => $basketno,
-    quantity     => 24,
-    biblionumber => $biblionumber1,
-    budget_id    => $budget->{budget_id},
-};
-my @mandatoryparams_keys = keys %$mandatoryparams;
-foreach my $mandatoryparams_key (@mandatoryparams_keys) {
-    my %test_missing_mandatoryparams = %$mandatoryparams;
-    delete $test_missing_mandatoryparams{$mandatoryparams_key};
-    eval {
-        ( $basketnum, $ordernumbers[0] ) =
-          C4::Acquisition::NewOrder( \%test_missing_mandatoryparams );
-    };
-    $return_error = $@;
-    my $expected_error = "Mandatory parameter $mandatoryparams_key missing";
-    ok(
-        ( !( defined $basketnum || defined $ordernumbers[0] ) )
-          && ( index( $return_error, $expected_error ) >= 0 ),
-"NewOrder with no $mandatoryparams_key returns undef and croaks with expected error message"
-    );
-}
-
-# FIXME to do : test the other features of NewOrder
-
 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
 # Ex : a price of 50.1 will be stored internally as 5.100000
 
@@ -194,7 +179,9 @@ my @order_content = (
             biblionumber   => $biblionumber1,
             budget_id      => $budget->{budget_id},
             uncertainprice => 0,
-            notes          => "some notes",
+            order_internalnote => "internal note",
+            order_vendornote   => "vendor note",
+            ordernumber => '',
         },
         num => {
             quantity  => 24,
@@ -219,7 +206,8 @@ my @order_content = (
             biblionumber   => $biblionumber2,
             budget_id      => $budget->{budget_id},
             uncertainprice => 0,
-            notes          => "ordernotes"
+            order_internalnote => "internal note",
+            order_vendornote   => "vendor note"
         },
         num => {
             quantity  => 4,
@@ -237,7 +225,8 @@ my @order_content = (
             basketno     => $basketno,
             biblionumber => $biblionumber3,
             budget_id    => $budget->{budget_id},
-            notes        => "ordernotes"
+            order_internalnote => "internal note",
+            order_vendornote   => "vendor note"
         },
         num => {
             quantity       => 4,
@@ -256,7 +245,8 @@ my @order_content = (
             basketno     => $basketno,
             biblionumber => $biblionumber4,
             budget_id    => $budget->{budget_id},
-            notes        => "ordernotes"
+            order_internalnote => "internal note",
+            order_vendornote   => "vendor note"
         },
         num => {
             quantity       => 1,
@@ -279,7 +269,7 @@ for ( 0 .. 4 ) {
       values %{ $order_content[$_]->{num} };
     @ocontent{ keys %{ $order_content[$_]->{str} } } =
       values %{ $order_content[$_]->{str} };
-    ( undef, $ordernumbers[$_] ) = C4::Acquisition::NewOrder( \%ocontent );
+    $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
     $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
 }
 
@@ -345,22 +335,20 @@ ok(
 #
 
 my @expectedfields = qw(
+  order_internalnote
+  order_vendornote
   ordernumber
   biblionumber
   entrydate
   quantity
   currency
   listprice
-  totalamount
   datereceived
   invoiceid
   freight
   unitprice
   quantityreceived
-  cancelledby
   datecancellationprinted
-  notes
-  supplierreference
   purchaseordernumber
   basketno
   timestamp
@@ -371,7 +359,6 @@ my @expectedfields = qw(
   gstrate
   discount
   budget_id
-  budgetgroup_id
   budgetdate
   sort1
   sort2
@@ -383,6 +370,10 @@ my @expectedfields = qw(
   subscriptionid
   parent_ordernumber
   orderstatus
+  line_item_id
+  suppliers_reference_number
+  suppliers_reference_qualifier
+  suppliers_report
   title
   author
   basketname
@@ -403,6 +394,7 @@ my @expectedfields = qw(
   quantity_to_receive
   subtotal
   latesince
+  cancellationreason
 );
 (
     $test_missing_fields,   $test_extra_fields,
@@ -427,11 +419,13 @@ is( join( " ", @$test_different_fields ),
 #
 
 my @base_expectedfields = qw(
+  order_internalnote
+  order_vendornote
+  notes
   ordernumber
   ecost
   uncertainprice
   marc
-  cancelledby
   url
   isbn
   copyrightdate
@@ -468,7 +462,6 @@ my @base_expectedfields = qw(
   subscriptionid
   editionresponsibility
   sort2
-  notes
   volumedate
   budget_id
   illus
@@ -476,7 +469,10 @@ my @base_expectedfields = qw(
   biblioitemnumber
   datereceived
   orderstatus
-  supplierreference
+  line_item_id
+  suppliers_reference_number
+  suppliers_reference_qualifier
+  suppliers_report
   agerestriction
   budget_branchcode
   gstrate
@@ -490,7 +486,6 @@ my @base_expectedfields = qw(
   publicationyear
   collectiontitle
   invoiceid
-  budgetgroup_id
   place
   issn
   quantityreceived
@@ -504,7 +499,6 @@ my @base_expectedfields = qw(
   budget_expend
   rrp
   cn_sort
-  totalamount
   lccn
   sort1
   volume
@@ -514,6 +508,7 @@ my @base_expectedfields = qw(
   frameworkcode
   volumedesc
   datecancellationprinted
+  cancellationreason
 );
 @expectedfields =
   ( @base_expectedfields,
@@ -546,14 +541,12 @@ ok(
 );
 
 #
-# Test GetCancelledOrders
+# Test GetOrders { cancelled => 1 }
 #
 
 @expectedfields =
   ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
-is( GetCancelledOrders(), undef,
-    "GetCancelledOrders with no params returns undef" );
-@get_orders = GetCancelledOrders($basketno);
+@get_orders = GetOrders($basketno, { cancelled => 1 });
 (
     $test_missing_fields,   $test_extra_fields,
     $test_different_fields, $test_nbr_fields
@@ -562,21 +555,21 @@ is( GetCancelledOrders(), undef,
 is(
     $$test_nbr_fields[0],
     scalar @expectedfields,
-    "GetCancelledOrders gets orders with the right number of fields"
+    "GetOrders { cancelled => 1 } gets orders with the right number of fields"
 );
 is( join( " ", @$test_missing_fields ),
-    '', "GetCancelledOrders gets orders with no missing fields" );
+    '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
 is( join( " ", @$test_extra_fields ),
-    '', "GetCancelledOrders gets orders with no unexpected fields" );
+    '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
 is( join( " ", @$test_different_fields ),
     '',
-    "GetCancelledOrders gets orders with the right content in every fields" );
+    "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
 ok(
     (
         ( scalar @get_orders == 1 )
           and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
     ),
-    "GetCancelledOrders only gets cancelled orders"
+    "GetOrders { cancelled => 1 } only gets cancelled orders"
 );
 
 #
@@ -584,6 +577,9 @@ ok(
 #
 
 @expectedfields = qw (
+  order_internalnote
+  order_vendornote
+  notes
   basketgroupid
   basketgroupname
   firstname
@@ -593,8 +589,10 @@ ok(
   creationdate
   datereceived
   orderstatus
-  supplierreference
-  cancelledby
+  line_item_id
+  suppliers_reference_number
+  suppliers_reference_qualifier
+  suppliers_report
   isbn
   copyrightdate
   gstrate
@@ -609,7 +607,6 @@ ok(
   title
   closedate
   basketname
-  budgetgroup_id
   invoiceid
   author
   parent_ordernumber
@@ -625,7 +622,6 @@ ok(
   sort2_authcat
   rrp
   unitprice
-  totalamount
   sort1
   ordernumber
   datecreated
@@ -635,11 +631,11 @@ ok(
   subscriptionid
   frameworkcode
   sort2
-  notes
   datecancellationprinted
   budget_id
   authorisedby
   booksellerid
+  cancellationreason
 );
 
 # note that authorisedby was added to the return of SearchOrder by the
@@ -651,9 +647,18 @@ my $invoiceid = AddInvoice(
     unknown       => "unknown"
 );
 
-my ( $datereceived, $new_ordernumber ) =
-  ModReceiveOrder( $biblionumber4, $ordernumbers[4], 1, undef, 10, 10,
-    $invoiceid, 10, $order_content[4]->{str}->{budget_id} );
+my ($datereceived, $new_ordernumber) = ModReceiveOrder(
+    {
+        biblionumber      => $biblionumber4,
+        ordernumber       => $ordernumbers[4],
+        quantityreceived  => 1,
+        cost              => 10,
+        ecost             => 10,
+        invoiceid         => $invoiceid,
+        rrp               => 10,
+        budget_id          => $order_content[4]->{str}->{budget_id},
+    }
+);
 
 my $search_orders = SearchOrders({
     booksellerid => $booksellerid,
@@ -709,6 +714,21 @@ $search_orders = SearchOrders({
 });
 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
 
+$search_orders = SearchOrders({
+    ordernumber => $ordernumbers[4]
+});
+is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
+
+$search_orders = SearchOrders({
+    biblionumber => $biblionumber4
+});
+is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
+
+$search_orders = SearchOrders({
+    biblionumber => $biblionumber4,
+    pending      => 1
+});
+is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
 
 #
 # Test GetBudgetByOrderNumber
@@ -740,6 +760,9 @@ ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
   unitpricesupplier
   subtotal
   latesince
+  basketname
+  basketgroupid
+  basketgroupname
 );
 my @lateorders = GetLateOrders(0);
 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
@@ -777,7 +800,6 @@ $search_orders = SearchOrders({
 });
 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)" );
 
-
 #
 # Test AddClaim
 #
@@ -791,17 +813,29 @@ is(
     "AddClaim : Check claimed_date"
 );
 
-( $datereceived, $new_ordernumber ) =
-  ModReceiveOrder( $biblionumber2, $ordernumbers[1], 2, undef, 12, 12,
-    $invoiceid, 42, undef, undef, undef, "my notes");
+( $datereceived, $new_ordernumber ) = ModReceiveOrder(
+    {
+        biblionumber     => $biblionumber2,
+        ordernumber      => $ordernumbers[1],
+        quantityreceived => 2,
+        cost             => 12,
+        ecost            => 12,
+        invoiceid        => $invoiceid,
+        rrp              => 42,
+        order_internalnote => "my notes",
+        order_vendornote   => "my vendor notes",
+    }
+);
 my $order2 = GetOrder( $ordernumbers[1] );
 is( $order2->{'quantityreceived'},
     0, 'Splitting up order did not receive any on original order' );
 is( $order2->{'quantity'}, 40, '40 items on original order' );
 is( $order2->{'budget_id'}, $budgetid,
     'Budget on original order is unchanged' );
-is( $order2->{notes}, "my notes",
-    'ModReceiveOrder and GetOrder deal with notes' );
+is( $order2->{order_internalnote}, "my notes",
+    'ModReceiveOrder and GetOrder deal with internal notes' );
+is( $order2->{order_vendornote}, "my vendor notes",
+    'ModReceiveOrder and GetOrder deal with vendor notes' );
 
 $neworder = GetOrder($new_ordernumber);
 is( $neworder->{'quantity'}, 2, '2 items on new order' );
@@ -809,6 +843,14 @@ is( $neworder->{'quantityreceived'},
     2, 'Splitting up order received items on new order' );
 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
 
+is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
+is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
+
+my $orders = GetHistory( ordernumber => $ordernumbers[1] );
+is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
+$orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
+is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
+
 my $budgetid2 = C4::Budgets::AddBudget(
     {
         budget_code => "budget_code_test_modrecv",
@@ -816,9 +858,19 @@ my $budgetid2 = C4::Budgets::AddBudget(
     }
 );
 
-( $datereceived, $new_ordernumber ) =
-  ModReceiveOrder( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
-    $invoiceid, 42, $budgetid2, undef, undef, "my other notes" );
+( $datereceived, $new_ordernumber ) = ModReceiveOrder(
+    {
+        biblionumber     => $biblionumber2,
+        ordernumber      => $ordernumbers[2],
+        quantityreceived => 2,
+        cost             => 12,
+        ecost            => 12,
+        invoiceid        => $invoiceid,
+        rrp              => 42,
+        budget_id        => $budgetid2,
+        order_internalnote => "my other notes",
+    }
+);
 
 my $order3 = GetOrder( $ordernumbers[2] );
 is( $order3->{'quantityreceived'},
@@ -826,7 +878,7 @@ is( $order3->{'quantityreceived'},
 is( $order3->{'quantity'}, 2, '2 items on original order' );
 is( $order3->{'budget_id'}, $budgetid,
     'Budget on original order is unchanged' );
-is( $order3->{notes}, "my other notes",
+is( $order3->{order_internalnote}, "my other notes",
     'ModReceiveOrder and GetOrder deal with notes' );
 
 $neworder = GetOrder($new_ordernumber);
@@ -835,19 +887,65 @@ is( $neworder->{'quantityreceived'},
     2, 'Splitting up order received items on new order' );
 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
 
-( $datereceived, $new_ordernumber ) =
-  ModReceiveOrder( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
-    $invoiceid, 42, $budgetid2, undef, undef, "my third notes" );
+( $datereceived, $new_ordernumber ) = ModReceiveOrder(
+    {
+        biblionumber     => $biblionumber2,
+        ordernumber      => $ordernumbers[2],
+        quantityreceived => 2,
+        cost             => 12,
+        ecost            => 12,
+        invoiceid        => $invoiceid,
+        rrp              => 42,
+        budget_id        => $budgetid2,
+        order_internalnote => "my third notes",
+    }
+);
 
 $order3 = GetOrder( $ordernumbers[2] );
 is( $order3->{'quantityreceived'}, 2,          'Order not split up' );
 is( $order3->{'quantity'},         2,          '2 items on order' );
 is( $order3->{'budget_id'},        $budgetid2, 'Budget has changed' );
-is( $order3->{notes}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
+is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
 
 my $nonexistent_order = GetOrder();
 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
 $nonexistent_order = GetOrder( 424242424242 );
 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
 
-$dbh->rollback;
+# Tests for DelOrder
+my $order1 = GetOrder($ordernumbers[0]);
+my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
+ok((not defined $error), "DelOrder does not fail");
+$order1 = GetOrder($order1->{ordernumber});
+ok((defined $order1->{datecancellationprinted}), "order is cancelled");
+ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
+ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
+
+$order2 = GetOrder($ordernumbers[1]);
+$error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
+ok((not defined $error), "DelOrder does not fail");
+$order2 = GetOrder($order2->{ordernumber});
+ok((defined $order2->{datecancellationprinted}), "order is cancelled");
+ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
+ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
+
+my $order4 = GetOrder($ordernumbers[3]);
+$error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
+ok((not defined $error), "DelOrder does not fail");
+$order4 = GetOrder($order4->{ordernumber});
+ok((defined $order4->{datecancellationprinted}), "order is cancelled");
+ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
+ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
+# End of tests for DelOrder
+
+# Budget reports
+my $all_count = scalar GetBudgetsReport();
+ok($all_count >= 1, "GetBudgetReport OK");
+
+my $active_count = scalar GetBudgetsReport(1);
+ok($active_count >= 1 , "GetBudgetsReport(1) OK");
+
+is($all_count, scalar GetBudgetsReport(), "GetBudgetReport returns inactive budget period acquisitions.");
+ok($active_count >= scalar GetBudgetsReport(1), "GetBudgetReport doesn't return inactive budget period acquisitions.");
+
+$schema->storage->txn_rollback();