Drupal: Generate unique invoice number for unbercart order

This CodeLet generate a unique invoice number for every order placed with ubercart. The unique invice number is stored in a custom table and a function is provided to fetch the invoice number for respective order number. You can use this function in order template to display the inovice number

CodeLet: 

Module file:
<?php

/**
* @file
*
* Generat unique invoice number for each Ubercart order
*
* @author DrupalD
*/

/**
* Implementation of hook_uc_invoice
*
* @param $op
* @param $order
* @param $arg2
* @author DrupalD
*/
function uc_invoice_uc_order($op, $order, $arg2) {
switch($op) {
case 'new':
$result = db_query("SELECT MAX(invoice_id) last_id FROM {uc_invoice}");
$record = $result->fetchAssoc();
$sn__invoice = ($record['last_id'] == 0 || $record['last_id'] == NULL) ? 1 : $record['last_id'] + 1;
if ($order->order_status != 'abandoned') {
$sn__invoice_id = db_insert('uc_invoice')
->fields(array(
'invoice_id' => $sn__invoice,
'order_id' => $order->order_id,
))
->execute();
drupal_set_message(t('Invoice @invice_id created', array('@invice_id' => $sn__invoice)));
}
break;
case 'delete':
$sn__invoice_id = _get_uc_invoice_number($order->order_id);

//Update other invoices
db_update('uc_invoice')
->expression('invoice_id', 'invoice_id - :invoice_id', array('invoice_id' => 1))
->condition('invoice_id', $sn__invoice_id, '>')
->execute();

//Delete invoice id
$b__order_delete = db_delete('uc_invoice')
->condition('order_id', $order->order_id)
->execute();

break;
}
}

/**
* _get_uc_invoice_number
*
* Retrieve invoice number from order number
*
* @param $sn__order_id
* Id of the order
* @return invoice id
* @author DrupalD
*/
function _get_uc_invoice_number($sn__order_id) {
$record = db_select('uc_invoice', 'ui')
->fields('ui', array('invoice_id'))
->condition('order_id', $sn__order_id, '=')
->execute()
->fetchAssoc();
return $record['invoice_id'];
}

/**
* Implementation of hook_cron
*
* @author DrupalD
*/
function uc_invoice_cron() {
$om__query = db_select('uc_orders', 'uo');
$om__query->leftjoin('uc_invoice', 'ui', 'uo.order_id = ui.order_id');
$om__query->fields('uo', array('order_id'))
->condition('uo.order_status', 'abandoned', '<>')
->isNull('ui.invoice_id');
$result = $om__query->execute();
while ($record = $result->fetchAssoc()) {
$sn_invoice_result = db_query("SELECT MAX(invoice_id) last_id FROM {uc_invoice}");
$sn_invoice_record = $sn_invoice_result->fetchAssoc();
$sn__invoice = ($sn_invoice_record['last_id'] == 0 || $sn_invoice_record['last_id'] == NULL) ? 1 : $sn_invoice_record['last_id'] + 1;
$om__query = db_insert('uc_invoice')
->fields(array(
'invoice_id' => $sn__invoice,
'order_id' => $record['order_id'],
))
->execute();
watchdog('invoice', t('Invoice @invice_id created'), array('@invice_id' => $record['order_id']));
}
}
?>

Install file:
<?php

/**
* @file
*
* Create schema for uc_invoice
*
* @author DrupalD
*/

/**
* Implementation of hook_schema
*
* @author DrupalD
*/
function uc_invoice_schema() {
$schema['uc_invoice'] = array(
'description' => 'Relates Ubercart orders with invoice',
'fields' => array(
'invoice_id' => array(
'description' => 'Unique invoice number',
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),

'order_id' => array(
'description' => 'Unique order number',
'type' => 'int',
'lenght' => 11,
'not null' => TRUE,
),
),
'primary key' => array('invoice_id', 'order_id'),
);

return $schema;
}

/**
* Implmentation of hook_update_N
*
* @author DrupalD
*/
function uc_invoice_update_7001(&$sandbox) {
db_query("ALTER TABLE {uc_invoice} MODIFY COLUMN invoice_id int(11) not null");
db_add_primary_key('uc_invoice', array('invoice_id', 'order_id'));
}
?>

By DrupalD on Wed, 24/04/2013 - 5:11pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback