Merge branch 'new/security-release-19.11.09' into 19.11.x
[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::Orders;
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   => '2015-01-01',
37     budget_period_enddate     => '2015-12-31',
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     '2013-01-01',undef, undef, undef,  undef,
52     undef,      undef,  undef, undef, undef, undef,
53     1,          "notes",undef, '2013-01-01', undef, undef,
54     undef,       undef,  0,    "intnotes",  0,
55     undef, undef, 0,          undef,         '2013-12-31', 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 => '2013-01-01',
68     quantity => 1,
69     currency => $curcode,
70     listprice => $cost,
71     basketno => $basketno,
72     rrp => $cost,
73     ecost => $cost,
74     orderstatus => 'new',
75     subscriptionid => $subscription->{subscriptionid},
76     budget_id => $budget_id,
77 })->store;
78 my $ordernumber = $order->ordernumber;
79
80 my $is_currently_on_order = subscriptionCurrentlyOnOrder( $subscription->{subscriptionid} );
81 is ( $is_currently_on_order, 1, "The subscription is currently on order");
82
83 $order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} );
84 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order not received");
85 ok( $order->{ecost} == $cost, "test cost for the last order not received");
86
87 $dbh->do(q{DELETE FROM aqinvoices});
88 my $invoiceid = AddInvoice(invoicenumber => 'invoice1', booksellerid => $bookseller->id, unknown => "unknown");
89
90 my $invoice = GetInvoice( $invoiceid );
91 $invoice->{datereceived} = '2013-01-02';
92
93 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
94     {
95         biblionumber     => $biblionumber,
96         order            => $order,
97         quantityreceived => 1,
98         budget_id        => $budget_id,
99         invoice          => $invoice,
100     }
101 );
102
103 $order = GetLastOrderReceivedFromSubscriptionid( $subscription->{subscriptionid} );
104 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order received");
105 ok( $order->{ecost} == $cost, "test cost for the last order received");
106
107 $order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} );
108 is ( $order, undef, "test no not received order for a received order");
109
110 my @invoices = GetInvoices();
111 my @invoices_linked_to_subscriptions = grep { $_->{is_linked_to_subscriptions} } @invoices;
112 is(scalar(@invoices_linked_to_subscriptions), 1, 'GetInvoices() can identify invoices that are linked to a subscription');
113
114 # Cleanup
115 $schema->storage->txn_rollback();