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