You are here

public function CommerceInvoiceEntityController::save in Commerce Invoice 7

Saves an invoice.

When saving an invoice, the function will automatically create an invoice number based

Parameters

$invoice: The full invoice object to save.

$transaction: An optional transaction object.

Return value

The saved invoice object.

Overrides DrupalCommerceEntityController::save

File

includes/commerce_invoice.controller.inc, line 49
The controller for the invoice entity containing the CRUD operations.

Class

CommerceInvoiceEntityController
The controller class for invoices contains methods for the order CRUD operations. The load method is inherited from the default controller.

Code

public function save($invoice, DatabaseTransaction $transaction = NULL) {
  $transaction = !is_null($transaction) ? $transaction : db_transaction();
  try {
    $invoice->changed = REQUEST_TIME;

    // Inserting new invoice
    if (empty($invoice->invoice_id)) {
      $invoice->created = REQUEST_TIME;
      $invoice->invoice_number = $this
        ->generate_invoice_id($invoice);
      $this
        ->invoke('presave', $invoice);
      $invoice = $this
        ->_save($invoice, $transaction);
      $this
        ->invoke('insert', $invoice);
    }
    else {
      $this
        ->invoke('presave', $invoice);
      $invoice = $this
        ->_save($invoice, $transaction);
      $this
        ->invoke('save', $invoice);
    }
    return $invoice;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('commerce_invoice', $e);
    throw $e;
  }
}