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