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