Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Last active March 5, 2018 10:49
Show Gist options
  • Save cgi-caesar/2020402f867da0d4c3c19e0cd1e27e8c to your computer and use it in GitHub Desktop.
Save cgi-caesar/2020402f867da0d4c3c19e0cd1e27e8c to your computer and use it in GitHub Desktop.
aMember (site.php): sequential invoice numbers
<?php
function _get_my_number($record)
{
$prefix = $record instanceof InvoicePayment ? 'INV' : 'RFD';
$year = date('Y', amstrtotime($record->dattm));
$table = $record instanceof InvoicePayment ? '?_invoice_payment' : '?_invoice_refund';
$num = Am_Di::getInstance()->db->selectCell(<<<CUT
SELECT COUNT(*) + 1 FROM {$table}
WHERE dattm < ?
AND YEAR(dattm) = ?
CUT
, $record->dattm, $year);
return sprintf("%s-%s-%03d", $prefix, $year, $num);
}
Am_Di::getInstance()->hook->add([
Am_Event::SET_DISPLAY_INVOICE_PAYMENT_ID,
Am_Event::SET_DISPLAY_INVOICE_REFUND_ID], function(Am_Event $e) {
$e->setReturn(_get_my_number($e->getRecord()));
});
//add column with Invoice Number to payments grid in Admin UI
Am_Di::getInstance()->hook->add([
'gridPaymentInitGrid',
'gridRefundInitGrid'], function(Am_Event_Grid $e) {
$e->getGrid()->prependField('display_invoice_id', ___('Invoice Number'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment