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