Bug 21395: Make perlcritic happy
[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
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 $bpid = AddBudgetPeriod({
34     budget_period_startdate   => '2015-01-01',
35     budget_period_enddate     => '2015-12-31',
36     budget_period_description => "budget desc"
37 });
38
39 my $budget_id = AddBudget({
40     budget_code        => "ABCD",
41     budget_amount      => "123.132",
42     budget_name        => "Périodiques",
43     budget_notes       => "This is a note",
44     budget_period_id   => $bpid
45 });
46
47 my $subscriptionid = NewSubscription(
48     undef,      "",     undef, undef, $budget_id, $biblionumber,
49     '2013-01-01',undef, undef, undef,  undef,
50     undef,      undef,  undef, undef, undef, undef,
51     1,          "notes",undef, '2013-01-01', undef, undef,
52     undef,       undef,  0,    "intnotes",  0,
53     undef, undef, 0,          undef,         '2013-12-31', 0
54 );
55 die unless $subscriptionid;
56
57 my $basketno;
58 ok($basketno = NewBasket($bookseller->id, 1), "NewBasket(  " . $bookseller->id . ", 1  ) returns $basketno");
59
60 my $cost = 42.00;
61 my $subscription = GetSubscription( $subscriptionid );
62
63 my $order = Koha::Acquisition::Order->new({
64     biblionumber => $subscription->{biblionumber},
65     entrydate => '2013-01-01',
66     quantity => 1,
67     currency => $curcode,
68     listprice => $cost,
69     basketno => $basketno,
70     rrp => $cost,
71     ecost => $cost,
72     orderstatus => 'new',
73     subscriptionid => $subscription->{subscriptionid},
74     budget_id => $budget_id,
75 })->store;
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 = Koha::Acquisition::Orders->search({ subscriptionid => $subscription->{subscriptionid}, datereceived => undef })->next->unblessed;
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 => $bookseller->id, unknown => "unknown");
87
88 my $invoice = GetInvoice( $invoiceid );
89 $invoice->{datereceived} = '2013-01-02';
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 = Koha::Acquisition::Orders->search(
102     {
103         subscriptionid => $subscriptionid,
104         datereceived   => { '!=' => undef }
105     },
106     {
107         order_by => [ { -desc => 'datereceived' }, { -desc => 'ordernumber' } ]
108     }
109 )->next->unblessed;
110
111 is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order received");
112 ok( $order->{ecost} == $cost, "test cost for the last order received");
113
114 $order = Koha::Acquisition::Orders->search({ subscriptionid => $subscription->{subscriptionid}, datereceived => undef });
115 is ( $order->count, 0, "test no not received order for a received order");
116
117 my @invoices = GetInvoices();
118 my @invoices_linked_to_subscriptions = grep { $_->{is_linked_to_subscriptions} } @invoices;
119 is(scalar(@invoices_linked_to_subscriptions), 1, 'GetInvoices() can identify invoices that are linked to a subscription');
120
121 # Cleanup
122 $schema->storage->txn_rollback();