diff --git a/uc_stock/uc_stock.ca.inc b/uc_stock/uc_stock.ca.inc index d1308ca..f024f10 100644 --- a/uc_stock/uc_stock.ca.inc +++ b/uc_stock/uc_stock.ca.inc @@ -29,6 +29,152 @@ function uc_stock_ca_predicate() { ), ), ); + $predicates['uc_stock_increment_on_cancel'] = array( + '#title' => t('Increment stock on cancelling order'), + '#trigger' => 'uc_order_status_update', + '#conditions' => array( + '#operator' => 'AND', + '#conditions' => array( + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status is Cancelled.'), + '#argument_map' => array( + 'order' => 'updated_order', + ), + '#settings' => array( + 'order_status' => 'canceled', + ), + ), + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status wasn\'t Canceled.'), + '#argument_map' => array( + 'order' => 'order', + ), + '#settings' => array( + 'order_status' => 'canceled', + 'negate' => TRUE, + ), + ), + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status wasn\'t In Checkout.'), + '#argument_map' => array( + 'order' => 'order', + ), + '#settings' => array( + 'order_status' => 'in_checkout', + 'negate' => TRUE, + ), + ), + ), + ), + '#class' => 'uc_stock', + '#status' => 1, + '#actions' => array( + array( + '#name' => 'uc_stock_action_increment_stock', + '#title' => t('Increment stock of products in order'), + '#argument_map' => array( + 'order' => 'order', + ), + ), + ), + ); + $predicates['uc_stock_increment_on_delete'] = array( + '#title' => t('Increment stock on deleting an order'), + '#trigger' => 'uc_stock_order_deleted', + '#conditions' => array( + '#operator' => 'AND', + '#conditions' => array( + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status wasn\'t Canceled.'), + '#argument_map' => array( + 'order' => 'order', + ), + '#settings' => array( + 'order_status' => 'canceled', + 'negate' => TRUE, + ), + ), + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status wasn\'t In Checkout.'), + '#argument_map' => array( + 'order' => 'order', + ), + '#settings' => array( + 'order_status' => 'in_checkout', + 'negate' => TRUE, + ), + ), + ), + ), + '#class' => 'uc_stock', + '#status' => 1, + '#actions' => array( + array( + '#name' => 'uc_stock_action_increment_stock', + '#title' => t('Increment stock of products in order'), + '#argument_map' => array( + 'order' => 'order', + ), + ), + ), + ); + $predicates['uc_stock_decrement_on_uncancel'] = array( + '#title' => t('Decrement stock when order cancellation is being undone'), + '#trigger' => 'uc_order_status_update', + '#conditions' => array( + '#operator' => 'AND', + '#conditions' => array( + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status was Canceled.'), + '#argument_map' => array( + 'order' => 'order', + ), + '#settings' => array( + 'order_status' => 'canceled', + ), + ), + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status isn\'t Canceled.'), + '#argument_map' => array( + 'order' => 'updated_order', + ), + '#settings' => array( + 'order_status' => 'canceled', + 'negate' => TRUE, + ), + ), + array( + '#name' => 'uc_order_status_condition', + '#title' => t('If the order status isn\'t In Checkout.'), + '#argument_map' => array( + 'order' => 'updated_order', + ), + '#settings' => array( + 'order_status' => 'in_checkout', + 'negate' => TRUE, + ), + ), + ), + ), + '#class' => 'uc_stock', + '#status' => 1, + '#actions' => array( + array( + '#name' => 'uc_stock_action_decrement_stock', + '#title' => t('Decrement stock of products in order'), + '#argument_map' => array( + 'order' => 'order', + ), + ), + ), + ); return $predicates; } @@ -45,10 +191,36 @@ function uc_stock_ca_action() { ), '#category' => t('Stock'), ); + $actions['uc_stock_action_increment_stock'] = array( + '#title' => t('Increment stock of products on the order with tracking activated.'), + '#callback' => 'uc_stock_action_increment_stock', + '#arguments' => array( + 'order' => array('#entity' => 'uc_order', '#title' => t('Order')), + ), + '#category' => t('Stock'), + ); return $actions; } +/** + * Implements hook_ca_trigger(). + */ +function uc_stock_ca_trigger() { + $triggers['uc_stock_order_deleted'] = array( + '#title' => t('An order is being deleted'), + '#category' => t('Order'), + '#arguments' => array( + 'order' => array( + '#entity' => 'uc_order', + '#title' => t('Order'), + ), + ), + ); + + return $triggers; +} + /****************************************************************************** * Conditional Action Callbacks and Forms * ******************************************************************************/ @@ -61,3 +233,25 @@ function uc_stock_action_decrement_stock($order, $settings) { array_walk($order->products, 'uc_stock_adjust_product_stock', $order); } } + +/****************************************************************************** +* Helper functions * +******************************************************************************/ + +/** +* Increment a product's stock. +*/ +function uc_stock_increment_product_stock($product, $key, $order) { + //inverse qty to inverse the decrement functionality of uc_stock_adjust_product_stock() + $product->qty = -$product->qty; + uc_stock_adjust_product_stock($product, $key, $order); +} + +/** +* Increase the stock of ordered products. +*/ +function uc_stock_action_increment_stock($order, $settings) { + if (is_array($order->products)) { + array_walk($order->products, 'uc_stock_increment_product_stock', $order); + } +} diff --git a/uc_stock/uc_stock.module b/uc_stock/uc_stock.module index acd0f8e..1ca309f 100644 --- a/uc_stock/uc_stock.module +++ b/uc_stock/uc_stock.module @@ -124,6 +124,15 @@ function uc_stock_mail($key, &$message, $params) { ******************************************************************************/ /** + * Implements hook_order(). + */ +function uc_stock_order($op, $order, $arg2) { + if ($op == 'delete') { + ca_pull_trigger('uc_stock_order_deleted', $order); + } +} + +/** * Implements hook_token_list(). */ function uc_stock_token_list($type = 'all') {