Bug 20728: Replace the calls by their Koha::Acq::Orders->search equivalent
[srvgit] / 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
21 my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
22
23 my $bookseller = Koha::Acquisition::Bookseller->new(
24     {
25         name => "my vendor",
26         address1 => "bookseller's address",
27         phone => "0123456",
28         active => 1
29     }
30 )->store;
31
32 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
33 my $budgetid;
34 my $bpid = AddBudgetPeriod({
35     budget_period_startdate   => '2015-01-01',
36     budget_period_enddate     => '2015-12-31',
37     budget_period_description => "budget desc"
38 });
39
40 my $budget_id = AddBudget({
41     budget_code        => "ABCD",
42     budget_amount      => "123.132",
43     budget_name        => "Périodiques",
44     budget_notes       => "This is a note",
45     budget_period_id   => $bpid
46 });
47
48 my $subscriptionid = NewSubscription(
49     undef,      "",     undef, undef, $budget_id, $biblionumber,
50     '2013-01-01',undef, undef, undef,  undef,
51     undef,      undef,  undef, undef, undef, undef,
52     1,          "notes",undef, '2013-01-01', undef, undef,
53     undef,       undef,  0,    "intnotes",  0,
54     undef, undef, 0,          undef,         '2013-12-31', 0
55 );
56 die unless $subscriptionid;
57
58 my ($basket, $basketno);
59 ok($basketno = NewBasket($bookseller->id, 1), "NewBasket(  " . $bookseller->id . ", 1  ) returns $basketno");
60
61 my $cost = 42.00;
62 my $subscription = GetSubscription( $subscriptionid );
63
64 my $order = Koha::Acquisition::Order->new({
65     biblionumber => $subscription->{biblionumber},
66     entrydate => '2013-01-01',
67     quantity => 1,
68     currency => $curcode,
69     listprice => $cost,
70     basketno => $basketno,
71     rrp => $cost,
72     ecost => $cost,
73     orderstatus => 'new',
74     subscriptionid => $subscription->{subscriptionid},
75     budget_id => $budget_id,
76 })->store;
77 my $ordernumber = $order->ordernumber;
78
79 my $is_currently_on_order = subscriptionCurrentlyOnOrder( $subscription->{subscriptionid} );
80 is ( $is_currently_on_order, 1, "The subscription is currently on order");
81
82 $order = Koha::Acquisition::Orders->search({ subscriptionid => $subscription->{subscriptionid}, datereceived => undef })->next->unblessed;
83 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order not received");
84 ok( $order->{ecost} == $cost, "test cost for the last order not received");
85
86 $dbh->do(q{DELETE FROM aqinvoices});
87 my $invoiceid = AddInvoice(invoicenumber => 'invoice1', booksellerid => $bookseller->id, unknown => "unknown");
88
89 my $invoice = GetInvoice( $invoiceid );
90 $invoice->{datereceived} = '2013-01-02';
91
92 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
93     {
94         biblionumber     => $biblionumber,
95         order            => $order,
96         quantityreceived => 1,
97         budget_id        => $budget_id,
98         invoice          => $invoice,
99     }
100 );
101
102 $order = Koha::Acquisition::Orders->search(
103     {
104         subscriptionid => $subscriptionid,
105         datereceived   => { '!=' => undef }
106     },
107     {
108         order_by => [ { -desc => 'datereceived' }, { -desc => 'ordernumber' } ]
109     }
110 )->next->unblessed;
111
112 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order received");
113 ok( $order->{ecost} == $cost, "test cost for the last order received");
114
115 $order = Koha::Acquisition::Orders->search({ subscriptionid => $subscription->{subscriptionid}, datereceived => undef });
116 is ( $order->count, 0, "test no not received order for a received order");
117
118 my @invoices = GetInvoices();
119 my @invoices_linked_to_subscriptions = grep { $_->{is_linked_to_subscriptions} } @invoices;
120 is(scalar(@invoices_linked_to_subscriptions), 1, 'GetInvoices() can identify invoices that are linked to a subscription');
121
122 # Cleanup
123 $schema->storage->txn_rollback();