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