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