function commerce_invoice_create_from_order in Commerce Invoice 7.2
Create a new invoice based on a Commerce order.
Parameters
object $order: A Commerce order.
InvoiceNumberPattern|NULL $pattern: An invoice number pattern.
bool $cancel_existing: Cancel existing invoices for the order.
string $status: Override the default invoice status
Return value
Invoice A saved invoice entity.
2 calls to commerce_invoice_create_from_order()
- commerce_invoice_action_create_from_order in ./
commerce_invoice.rules.inc - Action: Create invoice for a given order.
- commerce_invoice_create_from_order_form_submit in ./
commerce_invoice.admin.inc - Form API submit callback for invoicing orders.
File
- ./
commerce_invoice.module, line 237 - The Commerce Invoice module.
Code
function commerce_invoice_create_from_order($order, InvoiceNumberPattern $pattern = NULL, $cancel_existing = TRUE, $status = NULL) {
$invoice = commerce_invoice_object_prepare_from_order($order, $pattern);
if ($cancel_existing) {
$existing = commerce_invoice_load_for_order($order);
}
if ($status) {
$invoice->invoice_status = $status;
}
$invoice
->save();
if ($cancel_existing && !empty($existing)) {
$log = t('A new invoice was created for the same order: number @number, ID @id', [
'@id' => $invoice->invoice_id,
'@number' => $invoice
->getInvoiceNumber()
->__toString(),
]);
foreach ($existing as $previous) {
switch ($previous->invoice_status) {
// Request refund for previously paid invoices.
case Invoice::STATUS_PAID:
$previous->invoice_status = Invoice::STATUS_REFUND_PENDING;
// @todo: Issue a credit note. A rule? A hook? A custom module?
break;
// Cancel pending invoices.
case Invoice::STATUS_PENDING:
$previous->invoice_status = Invoice::STATUS_CANCELED;
break;
}
$previous->log = $log;
$previous
->save();
}
}
return $invoice;
}