3 # This Koha test module is a stub!
4 # Add more tests here!!!
9 use C4::Bookseller qw( GetBookSellerFromId );
10 use C4::Biblio qw( AddBiblio );
12 use Test::More tests => 21;
15 use_ok('C4::Acquisition');
18 my $dbh = C4::Context->dbh;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
22 $dbh->do(q{DELETE FROM aqinvoices});
24 my $booksellerid = C4::Bookseller::AddBookseller(
27 address1 => "bookseller's address",
33 my $booksellerinfo = GetBookSellerFromId( $booksellerid );
34 my $basketno = NewBasket($booksellerid, 1);
35 my $basket = GetBasket($basketno);
37 my $budgetid = C4::Budgets::AddBudget(
39 budget_code => "budget_code_test_getordersbybib",
40 budget_name => "budget_name_test_getordersbybib",
43 my $budget = C4::Budgets::GetBudget( $budgetid );
45 my ($ordernumber1, $ordernumber2, $ordernumber3);
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'),
53 my ($biblionumber1, $biblioitemnumber1) = AddBiblio($bibrec1, '');
54 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
55 my ($biblionumber3, $biblioitemnumber3) = AddBiblio(MARC::Record->new, '');
56 ( undef, $ordernumber1 ) = C4::Acquisition::NewOrder(
58 basketno => $basketno,
60 biblionumber => $biblionumber1,
61 budget_id => $budget->{budget_id},
65 ( undef, $ordernumber2 ) = C4::Acquisition::NewOrder(
67 basketno => $basketno,
69 biblionumber => $biblionumber2,
70 budget_id => $budget->{budget_id},
74 ( undef, $ordernumber3 ) = C4::Acquisition::NewOrder(
76 basketno => $basketno,
78 biblionumber => $biblionumber3,
79 budget_id => $budget->{budget_id},
85 my $invoiceid1 = AddInvoice(invoicenumber => 'invoice1', booksellerid => $booksellerid, unknown => "unknown");
86 my $invoiceid2 = AddInvoice(invoicenumber => 'invoice2', booksellerid => $booksellerid, unknown => "unknown",
87 shipmentdate => '2012-12-24',
90 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
101 ($datereceived, $new_ordernumber) = ModReceiveOrder(
112 ($datereceived, $new_ordernumber) = ModReceiveOrder(
124 my $invoice1 = GetInvoiceDetails($invoiceid1);
125 my $invoice2 = GetInvoiceDetails($invoiceid2);
127 is(scalar @{$invoice1->{'orders'}}, 1, 'Invoice1 has only one order');
128 is(scalar @{$invoice2->{'orders'}}, 2, 'Invoice2 has only two orders');
130 my @invoices = GetInvoices();
131 cmp_ok(scalar @invoices, '>=', 2, 'GetInvoices returns at least two invoices');
133 @invoices = GetInvoices(invoicenumber => 'invoice2');
134 cmp_ok(scalar @invoices, '>=', 1, 'GetInvoices returns at least one invoice when a specific invoice is requested');
136 @invoices = GetInvoices(shipmentdateto => '2012-12-24', shipmentdatefrom => '2012-12-24');
137 is($invoices[0]->{invoicenumber}, 'invoice2', 'GetInvoices() to search by shipmentdate works (bug 8854)');
138 @invoices = GetInvoices(title => 'Bug');
139 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by title works (bug 8854)');
140 @invoices = GetInvoices(author => 'Billy');
141 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by author works (bug 8854)');
142 @invoices = GetInvoices(publisher => 'Scholastic');
143 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publisher works (bug 8854)');
144 @invoices = GetInvoices(publicationyear => '2012');
145 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by publication/copyright year works (bug 8854)');
146 @invoices = GetInvoices(isbneanissn => '1234567890');
147 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by ISBN works (bug 8854)');
148 @invoices = GetInvoices(isbneanissn => '123456789');
149 is($invoices[0]->{invoicenumber}, 'invoice1', 'GetInvoices() to search by partial ISBN works (bug 8854)');
151 my $invoicesummary1 = GetInvoice($invoiceid1);
152 is($invoicesummary1->{'invoicenumber'}, 'invoice1', 'GetInvoice retrieves correct invoice');
153 is($invoicesummary1->{'invoicenumber'}, $invoice1->{'invoicenumber'}, 'GetInvoice and GetInvoiceDetails retrieve same information');
155 ModInvoice(invoiceid => $invoiceid1, invoicenumber => 'invoice11');
156 $invoice1 = GetInvoiceDetails($invoiceid1);
157 is($invoice1->{'invoicenumber'}, 'invoice11', 'ModInvoice changed invoice number');
159 is($invoice1->{'closedate'}, undef, 'Invoice is not closed before CloseInvoice call');
160 CloseInvoice($invoiceid1);
161 $invoice1 = GetInvoiceDetails($invoiceid1);
162 isnt($invoice1->{'closedate'}, undef, 'Invoice is closed after CloseInvoice call');
163 ReopenInvoice($invoiceid1);
164 $invoice1 = GetInvoiceDetails($invoiceid1);
165 is($invoice1->{'closedate'}, undef, 'Invoice is open after ReopenInvoice call');
168 MergeInvoices($invoiceid1, [ $invoiceid2 ]);
170 my $mergedinvoice = GetInvoiceDetails($invoiceid1);
171 is(scalar @{$mergedinvoice->{'orders'}}, 3, 'Merged invoice has three orders');
173 my $invoiceid3 = AddInvoice(invoicenumber => 'invoice3', booksellerid => $booksellerid, unknown => "unknown");
174 my $invoicecount = GetInvoices();
175 DelInvoice($invoiceid3);
176 @invoices = GetInvoices();
177 is(scalar @invoices, $invoicecount - 1, 'DelInvoice deletes invoice');
178 is(GetInvoice($invoiceid3), undef, 'DelInvoice deleted correct invoice');
181 $dbh and $dbh->rollback;