protected function CommerceInvoiceEntityController::generate_invoice_id in Commerce Invoice 7
Generates invoice id
Parameters
object $invoice The invoice to generate an invoice number for.:
Return value
The generated invoice id
1 call to CommerceInvoiceEntityController::generate_invoice_id()
- CommerceInvoiceEntityController::save in includes/
commerce_invoice.controller.inc - Saves an invoice.
File
- includes/
commerce_invoice.controller.inc, line 114 - 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
protected function generate_invoice_id($invoice) {
// TODO: there is probably a better way to do this
// Invoice generation method
$method = variable_get('commerce_invoice_number_method', COMMERCE_INVOICE_METHOD_YEAR);
if ($method == COMMERCE_INVOICE_METHOD_CALLBACK) {
$function_name = variable_get('commerce_invoice_number_callback', '');
if (empty($function_name) || !function_exists($function_name)) {
drupal_set_message(t('No valid callback defined for invoice number generation.'), 'error');
return t('no-invoice-number');
}
else {
return $function_name($invoice);
}
}
// Get last invoice created
$result = db_select('commerce_invoice', 'i')
->fields('i')
->orderBy('created', 'DESC')
->range(0, 1)
->execute();
if ($record = $result
->fetchAssoc()) {
$last_number = $record['invoice_number'];
switch ($method) {
case COMMERCE_INVOICE_METHOD_INFINITE:
$last_id = $record['invoice_id'];
$id = $last_id + 1;
$return = str_replace('[invoice_id]', $id, $method);
break;
case COMMERCE_INVOICE_METHOD_YEAR:
// Are we in the same year as the last invoice
$dash_pos = strpos($last_number, '-');
$last_year = substr($last_number, 0, $dash_pos);
if ($last_year == date('Y')) {
// Get last invoice id
$last_id = strstr($last_number, '-');
$last_id = str_replace('-', '', $last_id);
// Increment invoice id
$id = $last_id + 1;
$return = $last_year . '-' . $id;
}
else {
// Reset invoice id to 1
$return = date('Y') . '-1';
}
break;
case COMMERCE_INVOICE_METHOD_MONTH:
$parts = explode('-', $last_number);
$last_year = $parts[0];
$last_month = $parts[1];
$last_id = $parts[2];
$year = date('Y');
$month = date('m');
if ($last_year == $year) {
if ($last_month == $month) {
$id = $last_id + 1;
}
else {
$id = 1;
}
}
else {
$id = 1;
}
$return = date('Y') . '-' . date('m') . '-' . $id;
break;
}
return $return;
}
else {
// First invoice being generated
$id = 1;
$result = str_replace('[invoice_id]', $id, $method);
return date($result);
}
}