+=head3 GetInvoiceDetails
+
+ my $invoice = GetInvoiceDetails($invoiceid)
+
+Return informations about an invoice + the list of related order lines
+
+Orders informations are in $invoice->{orders} (array ref)
+
+=cut
+
+sub GetInvoiceDetails {
+ my ($invoiceid) = @_;
+
+ if ( !defined $invoiceid ) {
+ carp 'GetInvoiceDetails called without an invoiceid';
+ return;
+ }
+
+ my $dbh = C4::Context->dbh;
+ my $query = q{
+ SELECT aqinvoices.*, aqbooksellers.name AS suppliername
+ FROM aqinvoices
+ LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
+ WHERE invoiceid = ?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($invoiceid);
+
+ my $invoice = $sth->fetchrow_hashref;
+
+ $query = q{
+ SELECT aqorders.*, biblio.*, aqbasket.basketname
+ FROM aqorders
+ LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
+ LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
+ WHERE invoiceid = ?
+ };
+ $sth = $dbh->prepare($query);
+ $sth->execute($invoiceid);
+ $invoice->{orders} = $sth->fetchall_arrayref({});
+ $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
+
+ return $invoice;
+}
+
+=head3 AddInvoice
+
+ my $invoiceid = AddInvoice(
+ invoicenumber => $invoicenumber,
+ booksellerid => $booksellerid,
+ shipmentdate => $shipmentdate,
+ billingdate => $billingdate,
+ closedate => $closedate,
+ shipmentcost => $shipmentcost,
+ shipmentcost_budgetid => $shipmentcost_budgetid
+ );
+
+Create a new invoice and return its id or undef if it fails.
+
+=cut
+
+sub AddInvoice {
+ my %invoice = @_;
+
+ return unless(%invoice and $invoice{invoicenumber});
+
+ my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
+ closedate shipmentcost shipmentcost_budgetid);
+
+ my @set_strs;
+ my @set_args;
+ foreach my $key (keys %invoice) {
+ if(0 < grep(/^$key$/, @columns)) {
+ push @set_strs, "$key = ?";
+ push @set_args, ($invoice{$key} || undef);
+ }
+ }
+
+ my $rv;
+ if(@set_args > 0) {
+ my $dbh = C4::Context->dbh;
+ my $query = "INSERT INTO aqinvoices SET ";
+ $query .= join (",", @set_strs);
+ my $sth = $dbh->prepare($query);
+ $rv = $sth->execute(@set_args);
+ if($rv) {
+ $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
+ }
+ }
+ return $rv;
+}
+
+=head3 ModInvoice
+
+ ModInvoice(
+ invoiceid => $invoiceid, # Mandatory
+ invoicenumber => $invoicenumber,
+ booksellerid => $booksellerid,
+ shipmentdate => $shipmentdate,
+ billingdate => $billingdate,
+ closedate => $closedate,
+ shipmentcost => $shipmentcost,
+ shipmentcost_budgetid => $shipmentcost_budgetid
+ );
+
+Modify an invoice, invoiceid is mandatory.
+
+Return undef if it fails.
+
+=cut
+
+sub ModInvoice {
+ my %invoice = @_;
+
+ return unless(%invoice and $invoice{invoiceid});
+
+ my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
+ closedate shipmentcost shipmentcost_budgetid);
+
+ my @set_strs;
+ my @set_args;
+ foreach my $key (keys %invoice) {
+ if(0 < grep(/^$key$/, @columns)) {
+ push @set_strs, "$key = ?";
+ push @set_args, ($invoice{$key} || undef);
+ }
+ }
+
+ my $dbh = C4::Context->dbh;
+ my $query = "UPDATE aqinvoices SET ";
+ $query .= join(",", @set_strs);
+ $query .= " WHERE invoiceid = ?";
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@set_args, $invoice{invoiceid});
+}
+
+=head3 CloseInvoice
+
+ CloseInvoice($invoiceid);
+
+Close an invoice.
+
+Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
+
+=cut
+
+sub CloseInvoice {
+ my ($invoiceid) = @_;
+
+ return unless $invoiceid;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ UPDATE aqinvoices
+ SET closedate = CAST(NOW() AS DATE)
+ WHERE invoiceid = ?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($invoiceid);
+}
+
+=head3 ReopenInvoice
+
+ ReopenInvoice($invoiceid);
+
+Reopen an invoice
+
+Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => C4::Dates->new()->output('iso'))
+
+=cut
+
+sub ReopenInvoice {
+ my ($invoiceid) = @_;
+
+ return unless $invoiceid;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ UPDATE aqinvoices
+ SET closedate = NULL
+ WHERE invoiceid = ?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($invoiceid);
+}
+
+=head3 DelInvoice
+
+ DelInvoice($invoiceid);
+
+Delete an invoice if there are no items attached to it.
+
+=cut
+
+sub DelInvoice {
+ my ($invoiceid) = @_;
+
+ return unless $invoiceid;
+
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ SELECT COUNT(*)
+ FROM aqorders
+ WHERE invoiceid = ?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($invoiceid);
+ my $res = $sth->fetchrow_arrayref;
+ if ( $res && $res->[0] == 0 ) {
+ $query = qq{
+ DELETE FROM aqinvoices
+ WHERE invoiceid = ?
+ };
+ my $sth = $dbh->prepare($query);
+ return ( $sth->execute($invoiceid) > 0 );
+ }
+ return;
+}
+
+=head3 MergeInvoices
+
+ MergeInvoices($invoiceid, \@sourceids);
+
+Merge the invoices identified by the IDs in \@sourceids into
+the invoice identified by $invoiceid.
+
+=cut
+
+sub MergeInvoices {
+ my ($invoiceid, $sourceids) = @_;
+
+ return unless $invoiceid;
+ foreach my $sourceid (@$sourceids) {
+ next if $sourceid == $invoiceid;
+ my $source = GetInvoiceDetails($sourceid);
+ foreach my $order (@{$source->{'orders'}}) {
+ $order->{'invoiceid'} = $invoiceid;
+ ModOrder($order);
+ }
+ DelInvoice($source->{'invoiceid'});
+ }
+ return;
+}
+
+=head3 GetBiblioCountByBasketno
+
+$biblio_count = &GetBiblioCountByBasketno($basketno);
+
+Looks up the biblio's count that has basketno value $basketno
+
+Returns a quantity
+
+=cut
+
+sub GetBiblioCountByBasketno {
+ my ($basketno) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = "
+ SELECT COUNT( DISTINCT( biblionumber ) )
+ FROM aqorders
+ WHERE basketno = ?
+ AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
+ ";
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute($basketno);
+ return $sth->fetchrow;