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