Bug 18448: Fix a few db_dependent tests
[koha.git] / t / db_dependent / Acquisition / OrderFromSubscription.t
1 use Modern::Perl;
2
3 use Test::More tests => 12;
4
5 use t::lib::TestBuilder;
6
7 use_ok('C4::Acquisition');
8 use_ok('C4::Biblio');
9 use_ok('C4::Budgets');
10 use_ok('C4::Serials');
11
12 use Koha::Acquisition::Order;
13 use Koha::Database;
14
15 # Start transaction
16 my $schema = Koha::Database->new()->schema();
17 $schema->storage->txn_begin();
18 my $builder = t::lib::TestBuilder->new;
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21
22 my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
23
24 my $bookseller = Koha::Acquisition::Bookseller->new(
25     {
26         name => "my vendor",
27         address1 => "bookseller's address",
28         phone => "0123456",
29         active => 1
30     }
31 )->store;
32
33 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
34 my $budgetid;
35 my $bpid = AddBudgetPeriod({
36     budget_period_startdate   => '01-01-2015',
37     budget_period_enddate     => '12-31-2015',
38     budget_period_description => "budget desc"
39 });
40
41 my $budget_id = AddBudget({
42     budget_code        => "ABCD",
43     budget_amount      => "123.132",
44     budget_name        => "Périodiques",
45     budget_notes       => "This is a note",
46     budget_period_id   => $bpid
47 });
48
49 my $subscriptionid = NewSubscription(
50     undef,      "",     undef, undef, $budget_id, $biblionumber,
51     '01-01-2013',undef, undef, undef,  undef,
52     undef,      undef,  undef, undef, undef, undef,
53     1,          "notes",undef, '01-01-2013', undef, undef,
54     undef,       undef,  0,    "intnotes",  0,
55     undef, undef, 0,          undef,         '31-12-2013', 0
56 );
57 die unless $subscriptionid;
58
59 my ($basket, $basketno);
60 ok($basketno = NewBasket($bookseller->id, 1), "NewBasket(  " . $bookseller->id . ", 1  ) returns $basketno");
61
62 my $cost = 42.00;
63 my $subscription = GetSubscription( $subscriptionid );
64
65 my $order = Koha::Acquisition::Order->new({
66     biblionumber => $subscription->{biblionumber},
67     entrydate => '01-01-2013',
68     quantity => 1,
69     currency => $curcode,
70     listprice => $cost,
71     notes => "This is a note",
72     basketno => $basketno,
73     rrp => $cost,
74     ecost => $cost,
75     tax_rate => 0.0500,
76     orderstatus => 'new',
77     subscriptionid => $subscription->{subscriptionid},
78     budget_id => $budget_id,
79 })->insert;
80 my $ordernumber = $order->{ordernumber};
81
82 my $is_currently_on_order = subscriptionCurrentlyOnOrder( $subscription->{subscriptionid} );
83 is ( $is_currently_on_order, 1, "The subscription is currently on order");
84
85 $order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} );
86 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order not received");
87 ok( $order->{ecost} == $cost, "test cost for the last order not received");
88
89 $dbh->do(q{DELETE FROM aqinvoices});
90 my $invoiceid = AddInvoice(invoicenumber => 'invoice1', booksellerid => $bookseller->id, unknown => "unknown");
91
92 my $invoice = GetInvoice( $invoiceid );
93 $invoice->{datereceived} = '02-01-2013';
94
95 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
96     {
97         biblionumber     => $biblionumber,
98         order            => $order,
99         quantityreceived => 1,
100         budget_id        => $budget_id,
101         invoice          => $invoice,
102     }
103 );
104
105 $order = GetLastOrderReceivedFromSubscriptionid( $subscription->{subscriptionid} );
106 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order received");
107 ok( $order->{ecost} == $cost, "test cost for the last order received");
108
109 $order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} );
110 is ( $order, undef, "test no not received order for a received order");
111
112 my @invoices = GetInvoices();
113 my @invoices_linked_to_subscriptions = grep { $_->{is_linked_to_subscriptions} } @invoices;
114 is(scalar(@invoices_linked_to_subscriptions), 1, 'GetInvoices() can identify invoices that are linked to a subscription');
115
116 # Cleanup
117 $schema->storage->txn_rollback();