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