function commerce_gc_giftcard_balance in Commerce GC 7
Compute the balance for a particular giftcard coupon.
Parameters
type $coupon_id:
Return value
type
8 calls to commerce_gc_giftcard_balance()
- CommerceGCTransactionEntityController::save in includes/
commerce_gc.controller.inc - Saves a transaction with a created date if it is new.
- commerce_gc_coupon_properties in ./
commerce_gc.module - commerce_gc_giftcard_minimum_balance in ./
commerce_gc.rules.inc - commerce_gc_handler_area_balance_summary::render in includes/
views/ handlers/ commerce_gc_handler_area_balance_summary.inc - Render the area.
- commerce_gc_handler_field_balance_summary::render in includes/
views/ handlers/ commerce_gc_handler_field_balance_summary.inc - Render the field.
File
- ./
commerce_gc.module, line 1101 - Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.
Code
function commerce_gc_giftcard_balance($coupon_id, $for_update = FALSE) {
$coupon = commerce_coupon_load($coupon_id);
if ($coupon) {
$query = db_select('commerce_gc_transaction', 'c');
$query
->addExpression('SUM(c.amount)', 'balance');
$query
->condition('c.coupon_id', $coupon_id)
->condition('status', commerce_gc_balance_total_statuses());
if ($for_update) {
$query
->forUpdate();
}
$balance = $query
->execute()
->fetchCol();
return $balance ? reset($balance) : 0;
}
}