Bug 17781 - Improper branchcode set during renewal
[koha.git] / t / db_dependent / Acquisition / StandingOrders.t
blobd067747b64a55dfa9a9f2901409a448d95d45389
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Test::More tests => 13;
6 use C4::Context;
7 use C4::Acquisition;
8 use C4::Biblio;
9 use C4::Items;
10 use C4::Bookseller;
11 use C4::Budgets;
12 use Koha::Acquisition::Order;
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
16 my $schema = Koha::Database->schema;
17 $schema->storage->txn_begin;
18 my $builder = t::lib::TestBuilder->new;
20 # Set up configuration data
22 my $branch = $builder->build( { source => 'Branch' } );
23 my $bookseller = $builder->build( { source => 'Aqbookseller' } );
24 my $budget = $builder->build( { source => 'Aqbudget' } );
25 my $staffmember = $builder->build( { source => 'Borrower' } );
27 # Create baskets and orders
29 my $basketno = NewBasket(
30 $bookseller->{id},
31 $staffmember->{borrowernumber},
32 'Standing order basket', # basketname
33 '', # basketnote
34 '', # basketbooksellernote
35 undef, # basketcontractnumber
36 $branch->{branchcode}, # deliveryplace
37 $branch->{branchcode}, # billingplace
38 1 # is_standing
41 my $nonstandingbasketno = NewBasket(
42 $bookseller->{id},
43 $staffmember->{borrowernumber},
44 'Non-standing order basket', # basketname
45 '', # basketnote
46 '', # basketbooksellernote
47 undef, # basketcontractnumber
48 $branch->{branchcode}, # deliveryplace
49 $branch->{branchcode}, # billingplace
50 0 # is_standing
53 my $basket = GetBasket($basketno);
55 is( $basket->{is_standing}, 1, 'basket correctly created as standing order basket' );
57 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( MARC::Record->new, '' );
60 my $ordernumber = Koha::Acquisition::Order->new(
62 basketno => $basketno,
63 biblionumber => $biblionumber,
64 budget_id => $budget->{budget_id},
65 currency => 'USD',
66 quantity => 0,
67 rrp => 42,
68 rrp_tax_included => 42,
69 rrp_tax_excluded => 42,
70 ecost => 22,
71 ecost_tax_included => 22,
72 ecost_tax_excluded => 22,
73 unitprice => 12,
74 unitprice_tax_included => 12,
75 unitprice_tax_excluded => 12,
76 tax_rate => 0,
77 tax_rate_on_ordering => 0,
78 tax_rate_on_receiving => 0,
80 )->insert->{ordernumber};
82 isnt( $ordernumber, undef, 'standing order successfully created' );
84 my $search_orders = SearchOrders( {
85 basketno => $basketno,
86 pending => 1,
87 ordered => 1,
88 } );
90 ok(
91 scalar @$search_orders == 1 && $search_orders->[0]->{ordernumber} == $ordernumber,
92 'standing order counts as a pending/ordered order'
95 my $invoiceid = AddInvoice(
96 invoicenumber => 'invoice',
97 booksellerid => $bookseller->{id},
98 unknown => "unknown"
101 my $order = Koha::Acquisition::Order->fetch( { ordernumber => $ordernumber } );
103 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
105 biblionumber => $biblionumber,
106 order => $order,
107 quantityreceived => 2,
108 invoiceid => $invoiceid,
112 isnt( $ordernumber, $new_ordernumber, "standing order split on receive" );
114 #order has been updated, refetch
115 $order = Koha::Acquisition::Order->fetch( { ordernumber => $ordernumber } );
116 my $neworder = Koha::Acquisition::Order->fetch( { ordernumber => $new_ordernumber } );
118 is( $order->{orderstatus}, 'partial', 'original order set to partially received' );
119 is( $order->{quantity}, 1, 'original order quantity unchanged' );
120 is( $order->{quantityreceived}, 0, 'original order has no received items' );
121 isnt( $order->{unitprice}, 12, 'original order does not get cost' );
122 is( $neworder->{orderstatus}, 'complete', 'new order set to complete' );
123 is( $neworder->{quantityreceived}, 2, 'new order has received items' );
124 cmp_ok( $neworder->{unitprice}, '==', 12, 'new order does get cost' );
126 $search_orders = SearchOrders( {
127 basketno => $basketno,
128 pending => 1,
129 ordered => 1,
130 } );
132 is( scalar @$search_orders, 1, 'only one pending order after receive' );
133 is( $search_orders->[0]->{ordernumber}, $ordernumber, 'original order is only pending order' );
135 $schema->storage->txn_rollback();