Bug 7288: (follow-up) add unit test for is_linked_to_subscriptions flag
[koha.git] / t / db_dependent / Acquisition / Invoices.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8
9 use C4::Bookseller qw( GetBookSellerFromId );
10 use C4::Biblio qw( AddBiblio );
11
12 use Test::More tests => 22;
13
14 BEGIN {
15     use_ok('C4::Acquisition');
16 }
17
18 my $dbh = C4::Context->dbh;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
21
22 $dbh->do(q{DELETE FROM aqinvoices});
23
24 my $booksellerid = C4::Bookseller::AddBookseller(
25     {
26         name => "my vendor",
27         address1 => "bookseller's address",
28         phone => "0123456",
29         active => 1
30     }
31 );
32
33 my $booksellerinfo = GetBookSellerFromId( $booksellerid );
34 my $basketno = NewBasket($booksellerid, 1);
35 my $basket   = GetBasket($basketno);
36
37 my $budgetid = C4::Budgets::AddBudget(
38     {
39         budget_code => "budget_code_test_getordersbybib",
40         budget_name => "budget_name_test_getordersbybib",
41     }
42 );
43 my $budget = C4::Budgets::GetBudget( $budgetid );
44
45 my ($ordernumber1, $ordernumber2, $ordernumber3);
46 my $bibrec1 = MARC::Record->new();
47 $bibrec1->append_fields(
48     MARC::Field->new('020', '', '', 'a' => '1234567890'),
49     MARC::Field->new('100', '', '', 'a' => 'Shakespeare,  Billy'),
50     MARC::Field->new('245', '', '', 'a' => 'Bug 8854'),
51     MARC::Field->new('260', '', '', 'b' => 'Scholastic Publishing', c => 'c2012'),
52 );
53 my ($biblionumber1, $biblioitemnumber1) = AddBiblio($bibrec1, '');
54 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
55 my ($biblionumber3, $biblioitemnumber3) = AddBiblio(MARC::Record->new, '');
56 ( undef, $ordernumber1 ) = C4::Acquisition::NewOrder(
57     {
58         basketno => $basketno,
59         quantity => 2,
60         biblionumber => $biblionumber1,
61         budget_id => $budget->{budget_id},
62     }
63 );
64
65 ( undef, $ordernumber2 ) = C4::Acquisition::NewOrder(
66     {
67         basketno => $basketno,
68         quantity => 1,
69         biblionumber => $biblionumber2,
70         budget_id => $budget->{budget_id},
71     }
72 );
73
74 ( undef, $ordernumber3 ) = C4::Acquisition::NewOrder(
75     {
76         basketno => $basketno,
77         quantity => 1,
78         biblionumber => $biblionumber3,
79         budget_id => $budget->{budget_id},
80         ecost => 42,
81         rrp => 42,
82     }
83 );
84
85 my $invoiceid1 = AddInvoice(invoicenumber => 'invoice1', booksellerid => $booksellerid, unknown => "unknown");
86 my $invoiceid2 = AddInvoice(invoicenumber => 'invoice2', booksellerid => $booksellerid, unknown => "unknown",
87                             shipmentdate => '2012-12-24',
88                            );
89
90 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
91     {
92         biblionumber     => $biblionumber1,
93         ordernumber      => $ordernumber1,
94         quantityreceived => 2,
95         cost             => 12,
96         ecost            => 12,
97         invoiceid        => $invoiceid1,
98         rrp              => 42
99     }
100 );
101
102 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
103     {
104         biblionumber     => $biblionumber2,
105         ordernumber      => $ordernumber2,
106         quantityreceived => 1,
107         cost             => 5,
108         ecost            => 5,
109         invoiceid        => $invoiceid2,
110         rrp              => 42
111     }
112 );
113
114 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
115     {
116         biblionumber     => $biblionumber3,
117         ordernumber      => $ordernumber3,
118         quantityreceived => 1,
119         cost             => 12,
120         ecost            => 12,
121         invoiceid        => $invoiceid2,
122         rrp              => 42
123     }
124 );
125
126 my $invoice1 = GetInvoiceDetails($invoiceid1);
127 my $invoice2 = GetInvoiceDetails($invoiceid2);
128
129 is(scalar @{$invoice1->{'orders'}}, 1, 'Invoice1 has only one order');
130 is(scalar @{$invoice2->{'orders'}}, 2, 'Invoice2 has only two orders');
131
132 my @invoices = GetInvoices();
133 cmp_ok(scalar @invoices, '>=', 2, 'GetInvoices returns at least two invoices');
134
135 @invoices = GetInvoices(invoicenumber => 'invoice2');
136 cmp_ok(scalar @invoices, '>=', 1, 'GetInvoices returns at least one invoice when a specific invoice is requested');
137
138 @invoices = GetInvoices(shipmentdateto => '2012-12-24', shipmentdatefrom => '2012-12-24');
139 is($invoices[0]->{invoicenumber}, 'invoice2', 'GetInvoices() to search by shipmentdate works (bug 8854)');
140 @invoices = GetInvoices(title => 'Bug');
141 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by title works (bug 8854)');
142 @invoices = GetInvoices(author => 'Billy');
143 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by author works (bug 8854)');
144 @invoices = GetInvoices(publisher => 'Scholastic');
145 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publisher works (bug 8854)');
146 @invoices = GetInvoices(publicationyear => '2012');
147 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publication/copyright year works (bug 8854)');
148 @invoices = GetInvoices(isbneanissn => '1234567890');
149 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by ISBN works (bug 8854)');
150 @invoices = GetInvoices(isbneanissn => '123456789');
151 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by partial ISBN works (bug 8854)');
152
153 my $invoicesummary1 = GetInvoice($invoiceid1);
154 is($invoicesummary1->{'invoicenumber'}, 'invoice1', 'GetInvoice retrieves correct invoice');
155 is($invoicesummary1->{'invoicenumber'}, $invoice1->{'invoicenumber'}, 'GetInvoice and GetInvoiceDetails retrieve same information');
156
157 ModInvoice(invoiceid => $invoiceid1, invoicenumber => 'invoice11');
158 $invoice1 = GetInvoiceDetails($invoiceid1);
159 is($invoice1->{'invoicenumber'}, 'invoice11', 'ModInvoice changed invoice number');
160
161 is($invoice1->{'closedate'}, undef, 'Invoice is not closed before CloseInvoice call');
162 CloseInvoice($invoiceid1);
163 $invoice1 = GetInvoiceDetails($invoiceid1);
164 isnt($invoice1->{'closedate'}, undef, 'Invoice is closed after CloseInvoice call');
165 ReopenInvoice($invoiceid1);
166 $invoice1 = GetInvoiceDetails($invoiceid1);
167 is($invoice1->{'closedate'}, undef, 'Invoice is open after ReopenInvoice call');
168
169
170 MergeInvoices($invoiceid1, [ $invoiceid2 ]);
171
172 my $mergedinvoice = GetInvoiceDetails($invoiceid1);
173 is(scalar @{$mergedinvoice->{'orders'}}, 3, 'Merged invoice has three orders');
174
175 my $invoiceid3 = AddInvoice(invoicenumber => 'invoice3', booksellerid => $booksellerid, unknown => "unknown");
176 my $invoicecount = GetInvoices();
177 DelInvoice($invoiceid3);
178 @invoices = GetInvoices();
179 is(scalar @invoices, $invoicecount - 1, 'DelInvoice deletes invoice');
180 is(GetInvoice($invoiceid3), undef, 'DelInvoice deleted correct invoice');
181
182 my @invoices_linked_to_subscriptions = map{
183     $_->{is_linked_to_subscriptions}
184     ? $_
185     : ()
186 } @invoices;
187 is_deeply( \@invoices_linked_to_subscriptions, [], "GetInvoices return linked_to_subscriptions: there is no invoices linked to subscriptions yet" );
188
189 END {
190     $dbh and $dbh->rollback;
191 }