You are here

function uc_product_actions_stock_action_executor in Ubercart Product Actions 6

Same name and namespace in other branches
  1. 7 uc_product_actions.module \uc_product_actions_stock_action_executor()

Execute the action (for stock actions).

3 calls to uc_product_actions_stock_action_executor()
uc_product_actions_update_active_action in ./uc_product_actions.module
uc_product_actions_update_stock_action in ./uc_product_actions.module
uc_product_actions_update_threshold_action in ./uc_product_actions.module

File

./uc_product_actions.module, line 409

Code

function uc_product_actions_stock_action_executor($action, $node, $context) {
  $current_value = db_result(db_query("SELECT %s FROM {uc_product_stock} WHERE nid = %d AND sku = '%s'", db_escape_string($action), $node->nid, $node->model));
  if (!db_affected_rows()) {
    db_query("INSERT INTO {uc_product_stock} (sku, nid, active, stock, threshold) VALUES ('%s', %d, %d, %d, %d)", $node->model, $node->nid, 0, 0, 0);
    $current_value = 0;
  }
  if ($action == 'active') {
    $new_value = $context[$action . '_change'][0];
  }
  else {
    switch ($context[$action . '_method']) {
      case 'percentage':
        if ($current_value == 0) {
          $new_value = 0;
        }
        else {
          $change = $current_value * $context[$action . '_change'] / 100;
          $current_value > 0 ? $new_value = $current_value + $change : ($new_value = $current_value - $change);
        }
        break;
      case 'difference':
        $new_value = $current_value + $context[$action . '_change'];
        break;
      case 'absolute':
        $new_value = $context[$action . '_change'];
        break;
    }
  }
  db_query("UPDATE {uc_product_stock} SET %s = %d WHERE nid = %d AND sku = '%s'", $action, round($new_value), $node->nid, $node->model);
}