Bug 13321: Rename variables
[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 $invoice1 = GetInvoice( $invoiceid1 );
95 my $invoice2 = GetInvoice( $invoiceid2 );
96
97 my ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
98     {
99         biblionumber     => $biblionumber1,
100         order            => $order1,
101         quantityreceived => 2,
102         invoice          => $invoice1,
103     }
104 );
105
106 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
107     {
108         biblionumber     => $biblionumber2,
109         order            => $order2,
110         quantityreceived => 1,
111         invoice          => $invoice2,
112         rrp              => 42
113     }
114 );
115
116 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
117     {
118         biblionumber     => $biblionumber3,
119         order            => $order3,
120         quantityreceived => 1,
121         invoice          => $invoice2,
122     }
123 );
124
125 $invoice1 = GetInvoiceDetails($invoiceid1);
126 $invoice2 = GetInvoiceDetails($invoiceid2);
127
128 is(scalar @{$invoice1->{'orders'}}, 1, 'Invoice1 has only one order');
129 is(scalar @{$invoice2->{'orders'}}, 2, 'Invoice2 has only two orders');
130
131 my $orders = $invoice1->{orders};
132 ok( exists( @$orders[0]->{basketgroupid} ), "GetInvoiceDetails: The basketgroupid key exists" );
133 ok( exists( @$orders[0]->{basketgroupname} ), "GetInvoiceDetails: The basketgroupname key exists" );
134
135 my @invoices = GetInvoices();
136 cmp_ok(scalar @invoices, '>=', 2, 'GetInvoices returns at least two invoices');
137
138 @invoices = GetInvoices(invoicenumber => 'invoice2');
139 cmp_ok(scalar @invoices, '>=', 1, 'GetInvoices returns at least one invoice when a specific invoice is requested');
140
141 @invoices = GetInvoices(shipmentdateto => '2012-12-24', shipmentdatefrom => '2012-12-24');
142 is($invoices[0]->{invoicenumber}, 'invoice2', 'GetInvoices() to search by shipmentdate works (bug 8854)');
143 @invoices = GetInvoices(title => 'Bug');
144 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by title works (bug 8854)');
145 @invoices = GetInvoices(author => 'Billy');
146 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by author works (bug 8854)');
147 @invoices = GetInvoices(publisher => 'Scholastic');
148 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publisher works (bug 8854)');
149 @invoices = GetInvoices(publicationyear => '2012');
150 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publication/copyright year works (bug 8854)');
151 @invoices = GetInvoices(isbneanissn => '1234567890');
152 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by ISBN works (bug 8854)');
153 @invoices = GetInvoices(isbneanissn => '123456789');
154 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by partial ISBN works (bug 8854)');
155
156 my $invoicesummary1 = GetInvoice($invoiceid1);
157 is($invoicesummary1->{'invoicenumber'}, 'invoice1', 'GetInvoice retrieves correct invoice');
158 is($invoicesummary1->{'invoicenumber'}, $invoice1->{'invoicenumber'}, 'GetInvoice and GetInvoiceDetails retrieve same information');
159
160 ModInvoice(invoiceid => $invoiceid1, invoicenumber => 'invoice11');
161 $invoice1 = GetInvoiceDetails($invoiceid1);
162 is($invoice1->{'invoicenumber'}, 'invoice11', 'ModInvoice changed invoice number');
163
164 is($invoice1->{'closedate'}, undef, 'Invoice is not closed before CloseInvoice call');
165 CloseInvoice($invoiceid1);
166 $invoice1 = GetInvoiceDetails($invoiceid1);
167 isnt($invoice1->{'closedate'}, undef, 'Invoice is closed after CloseInvoice call');
168 ReopenInvoice($invoiceid1);
169 $invoice1 = GetInvoiceDetails($invoiceid1);
170 is($invoice1->{'closedate'}, undef, 'Invoice is open after ReopenInvoice call');
171
172
173 MergeInvoices($invoiceid1, [ $invoiceid2 ]);
174
175 my $mergedinvoice = GetInvoiceDetails($invoiceid1);
176 is(scalar @{$mergedinvoice->{'orders'}}, 3, 'Merged invoice has three orders');
177
178 my $invoiceid3 = AddInvoice(invoicenumber => 'invoice3', booksellerid => $booksellerid, unknown => "unknown");
179 my $invoicecount = GetInvoices();
180 DelInvoice($invoiceid3);
181 @invoices = GetInvoices();
182 is(scalar @invoices, $invoicecount - 1, 'DelInvoice deletes invoice');
183 is(GetInvoice($invoiceid3), undef, 'DelInvoice deleted correct invoice');
184
185 my @invoices_linked_to_subscriptions = map{
186     $_->{is_linked_to_subscriptions}
187     ? $_
188     : ()
189 } @invoices;
190 is_deeply( \@invoices_linked_to_subscriptions, [], "GetInvoices return linked_to_subscriptions: there is no invoices linked to subscriptions yet" );
191
192 END {
193     $dbh and $schema->storage->txn_rollback();
194 }