View source
<?php
function commerce_coupon_usage_get_usage($coupon_id, $exclude_cart = FALSE) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_coupon_usage_transaction')
->propertyCondition('coupon_id', $coupon_id);
if ($exclude_cart) {
global $user;
$cart_order_id = commerce_cart_order_id($user->uid);
if ($cart_order_id) {
$query
->propertyCondition('order_id', $cart_order_id, '<>');
}
}
return $query
->count()
->execute();
}
function commerce_coupon_usage_get_max_usage($coupon_id) {
$coupon = commerce_coupon_load($coupon_id);
if (!in_array($coupon->type, array_keys(commerce_coupon_get_types()))) {
return;
}
$coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);
$usage = 0;
if ($coupon_wrapper->commerce_coupon_conditions
->value()) {
$conditions = $coupon_wrapper->commerce_coupon_conditions
->value();
foreach ($conditions as $condition) {
if ($condition['condition_name'] == 'commerce_coupon_usage_evaluate_usage' && !empty($condition['condition_settings']['max_usage'])) {
if (!$usage || $condition['condition_settings']['max_usage'] < $usage) {
$usage = $condition['condition_settings']['max_usage'];
}
}
}
}
return $usage;
}
function commerce_coupon_usage_property_getter($coupon, array $options, $name) {
switch ($name) {
case 'usage':
return commerce_coupon_usage_get_usage($coupon->coupon_id);
case 'max_usage':
return commerce_coupon_usage_get_max_usage($coupon->coupon_id);
}
}
function commerce_coupon_usage_flush_caches() {
_commerce_coupon_install_inline_conditions_field();
}
function commerce_coupon_usage_inline_conditions_info() {
$conditions['commerce_coupon_usage_evaluate_usage'] = array(
'label' => t('Maximum usage'),
'entity type' => 'commerce_coupon',
'callbacks' => array(
'configure' => 'commerce_coupon_usage_evaluate_usage_configure',
'build' => 'commerce_coupon_usage_evaluate_usage_build',
),
);
return $conditions;
}
function commerce_coupon_usage_evaluate_usage_build($coupon, $max_usage) {
$usage = commerce_coupon_usage_get_usage($coupon->coupon_id, TRUE);
if ($usage >= $max_usage) {
$error =& drupal_static('commerce_coupon_error_' . strtolower($coupon->code));
$error = t('Coupon has exceeded maximum number of uses.');
}
else {
return TRUE;
}
}
function commerce_coupon_usage_evaluate_usage_configure($settings) {
if (is_string($settings)) {
$settings = unserialize($settings);
}
$form['max_usage'] = array(
'#type' => 'textfield',
'#title' => t('Maximum usage'),
'#default_value' => !empty($settings['max_usage']) ? $settings['max_usage'] : '',
'#element_validate' => array(
'element_validate_integer_positive',
),
'#required' => TRUE,
);
return $form;
}
function commerce_coupon_usage_commerce_coupon_final_checkout_transaction_rollback($transaction_id) {
commerce_coupon_usage_transaction_delete($transaction_id);
}
function commerce_coupon_usage_commerce_coupon_final_checkout_validate($form, $form_state, EntityDrupalWrapper $order_wrapper) {
if (!commerce_coupon_order_allows_coupons($order_wrapper
->value())) {
return;
}
$transaction_ids = array();
foreach ($order_wrapper->commerce_coupons as $coupon_wrapper) {
$usage_id = commerce_coupon_usage_transaction_write($coupon_wrapper->coupon_id
->value(), $order_wrapper->order_id
->value());
if ($usage_id) {
$transaction_ids[] = $usage_id;
}
else {
form_set_error('', t('Unable to process payment. Please try again.'));
return FALSE;
}
}
return $transaction_ids;
}
function commerce_coupon_usage_entity_info() {
$entity_info['commerce_coupon_usage_transaction'] = array(
'label' => t('Commerce coupon usage transaction'),
'plural label' => t('Commerce coupon usage transactions'),
'views controller class' => 'CommerceCouponUsageTransactionViewsController',
'controller class' => 'CommerceCouponUsageTransactionEntityController',
'base table' => 'commerce_coupon_usage_transaction',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'transaction_id',
'label' => 'transaction_id',
),
'bundles' => array(
'commerce_coupon_usage_transaction' => array(
'label' => t('Commerce coupon usage transaction'),
),
),
'module' => 'commerce_coupon_usage',
);
return $entity_info;
}
function commerce_coupon_usage_transaction_write($coupon_id, $order_id, $uid = NULL, $date = 0) {
$values = array(
'coupon_id' => $coupon_id,
'order_id' => $order_id,
'date' => $date ? $date : REQUEST_TIME,
);
if ($uid) {
$values['uid'] = $uid;
}
else {
$order = commerce_order_load($order_id);
if ($order->uid) {
$values['uid'] = $order->uid;
}
else {
$values['uid'] = 0;
}
}
$transaction = commerce_coupon_usage_transaction_new($values);
commerce_coupon_usage_transaction_save($transaction);
return !empty($transaction->transaction_id) ? $transaction->transaction_id : NULL;
}
function commerce_coupon_usage_transaction_new($values) {
return entity_get_controller('commerce_coupon_usage_transaction')
->create($values);
}
function commerce_coupon_usage_transaction_save($transaction) {
return entity_get_controller('commerce_coupon_usage_transaction')
->save($transaction);
}
function commerce_coupon_usage_transaction_load($transaction_id, $reset = FALSE) {
$transactions = commerce_coupon_usage_transaction_load_multiple(array(
$transaction_id,
), array(), $reset);
return reset($transactions);
}
function commerce_coupon_usage_transaction_load_multiple($transaction_ids = array(), $conditions = array(), $reset = FALSE) {
if (empty($transaction_ids) && empty($conditions)) {
return array();
}
return entity_load('commerce_coupon_usage_transaction', $transaction_ids, $conditions, $reset);
}
function commerce_coupon_usage_transaction_delete($transaction_id) {
return commerce_coupon_usage_transaction_delete_multiple(array(
$transaction_id,
));
}
function commerce_coupon_usage_transaction_delete_multiple($transaction_ids) {
return entity_get_controller('commerce_coupon_usage_transaction')
->delete($transaction_ids);
}
function commerce_coupon_usage_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_coupon_usage') . '/includes/views',
);
}
function commerce_coupon_usage_theme() {
return array(
'commerce_coupon_usage_summary' => array(
'variables' => array(
'coupon_id' => 0,
'details_link_text' => 'Details',
),
),
);
}
function theme_commerce_coupon_usage_summary(array $variables) {
$coupon_id = $variables['coupon_id'];
$link_text = $variables['details_link_text'];
if ($coupon_id) {
$usage = commerce_coupon_usage_get_usage($coupon_id);
$max_usage = commerce_coupon_usage_get_max_usage($coupon_id);
$output = array(
t('Uses: @usage', array(
'@usage' => $usage,
)),
);
if ($max_usage) {
$output[] = t('Maximum uses: @max_usage', array(
'@max_usage' => $max_usage,
));
}
if ($link_text) {
$output[] = l($link_text, 'admin/commerce/coupons/' . $coupon_id . '/usage', array(
'query' => drupal_get_destination(),
));
}
return implode('<br/>', $output);
}
}
function commerce_coupon_usage_record_usage($order) {
if (!commerce_coupon_order_allows_coupons($order)) {
return;
}
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_coupons as $coupon_wrapper) {
if ($coupon_wrapper
->value()) {
commerce_coupon_usage_transaction_write($coupon_wrapper->coupon_id
->value(), $order->order_id, $order->uid, $order->created);
}
}
}
function commerce_coupon_usage_commerce_order_update($order) {
if (!commerce_coupon_order_allows_coupons($order)) {
return;
}
$unchanged = entity_metadata_wrapper('commerce_order', $order->original);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$statuses = commerce_coupon_usage_tracked_order_statuses();
if (in_array($order->status, $statuses)) {
foreach ($order_wrapper->commerce_coupons as $coupon_wrapper) {
$coupon_id = $coupon_wrapper->coupon_id
->value();
if (!$unchanged->commerce_coupons
->value() || !in_array($coupon_id, $unchanged->commerce_coupons
->raw()) || !in_array($unchanged->status
->value(), $statuses)) {
if (commerce_coupon_usage_record_exists($order->order_id, $coupon_id)) {
continue;
}
$transaction_id = commerce_coupon_usage_transaction_write($coupon_id, $order->order_id);
if (!$transaction_id) {
drupal_set_message(t('You have exceeded the maximum usage settings for
coupon %code. Its usage on this order has not been recorded - you must
increase its maximum usage value, remove it from this order and add it again.', array(
'%code' => $coupon_wrapper->code
->value(),
)), 'warning');
}
}
}
foreach ($unchanged->commerce_coupons as $coupon_wrapper) {
$coupon_id = $coupon_wrapper->coupon_id
->value();
if (!$order_wrapper->commerce_coupons
->value() || !in_array($coupon_id, $order_wrapper->commerce_coupons
->raw())) {
$remove_coupon_ids[] = $coupon_id;
}
}
if (isset($remove_coupon_ids)) {
commerce_coupon_reset_usage(array(
$order->order_id,
), $remove_coupon_ids);
}
}
else {
if (in_array($unchanged->status
->value(), $statuses)) {
commerce_coupon_reset_usage(array(
$unchanged->order_id
->value(),
));
}
}
}
function commerce_coupon_usage_commerce_order_insert($order) {
if (!commerce_coupon_order_allows_coupons($order)) {
return;
}
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
if (in_array($order->status, commerce_coupon_usage_tracked_order_statuses())) {
foreach ($order_wrapper->commerce_coupons as $coupon_wrapper) {
$transaction_id = commerce_coupon_usage_transaction_write($coupon_wrapper->coupon_id
->value(), $order->order_id);
if (!$transaction_id) {
drupal_set_message(t('You have exceeded the maximum usage settings for
coupon %code. Its usage on this order cannot be recorded until you
increase its maximum usage settings.', array(
'%code' => $coupon_wrapper->code
->value(),
)), 'warning');
}
}
}
}
function commerce_coupon_usage_commerce_order_delete($order) {
commerce_coupon_reset_usage(array(
$order->order_id,
));
}
function commerce_coupon_usage_tracked_order_statuses() {
$statuses =& drupal_static(__FUNCTION__);
if (empty($statuses)) {
$statuses = array_merge(commerce_order_statuses(array(
'state' => 'completed',
)), commerce_order_statuses(array(
'state' => 'pending',
)));
$statuses = array_keys($statuses);
drupal_alter('commerce_coupon_usage_tracked_order_statuses', $statuses);
}
return $statuses;
}
function commerce_coupon_reset_usage($order_ids = FALSE, $coupon_ids = FALSE) {
$query = db_delete('commerce_coupon_usage_transaction');
if ($order_ids) {
$query
->condition('order_id', $order_ids);
}
if ($coupon_ids) {
$query
->condition('coupon_id', $coupon_ids);
}
if ($order_ids || $coupon_ids) {
$query
->execute();
}
}
function commerce_coupon_usage_record_exists($order_id, $coupon_id) {
$query = new EntityFieldQuery();
$results = $query
->entityCondition('entity_type', 'commerce_coupon_usage_transaction')
->propertyCondition('order_id', $order_id)
->propertyCondition('coupon_id', $coupon_id)
->execute();
return !empty($results['commerce_coupon_usage_transaction']) ? $results['commerce_coupon_usage_transaction'] : array();
}