| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file contains all the Workflow-NG hooks that are neccesary for Workflow
|
| 7 |
* integeration with the uc_stock module
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
/*******************************************************************************
|
| 12 |
* Workflow-ng Hooks *
|
| 13 |
******************************************************************************/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_configuration().
|
| 17 |
*/
|
| 18 |
function uc_stock_configuration() {
|
| 19 |
$configurations = array();
|
| 20 |
$configurations['uc_stock_decrement_on_order'] = array(
|
| 21 |
'#label' => t('Decrement stock upon order submission'),
|
| 22 |
'#event' => 'checkout_complete',
|
| 23 |
'#module' => 'uc_stock',
|
| 24 |
);
|
| 25 |
$action = workflow_ng_use_action('uc_stock_action_decrement_stock', array(
|
| 26 |
'#label' => t('Decrement stock of products in order'),
|
| 27 |
'#arguments' => array(
|
| 28 |
'order' => array('#entity' => 'order', '#label' => t('Order')),
|
| 29 |
),
|
| 30 |
));
|
| 31 |
$configurations['uc_stock_decrement_on_order'] = workflow_ng_configure($configurations['uc_stock_decrement_on_order'], $action);
|
| 32 |
return $configurations;
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_action_info().
|
| 37 |
*/
|
| 38 |
function uc_stock_action_info() {
|
| 39 |
return array(
|
| 40 |
'uc_stock_action_decrement_stock' => array(
|
| 41 |
'#label' => t('Decrement stock of products on the order with tracking activated.'),
|
| 42 |
'#arguments' => array(
|
| 43 |
'order' => array('#entity' => 'order', '#label' => t('Order')),
|
| 44 |
),
|
| 45 |
'#module' => t('Stock'),
|
| 46 |
),
|
| 47 |
);
|
| 48 |
}
|
| 49 |
|
| 50 |
/*******************************************************************************
|
| 51 |
* Workflow-ng Action Callbacks and Forms *
|
| 52 |
******************************************************************************/
|
| 53 |
|
| 54 |
function uc_stock_action_decrement_stock($order, $settings) {
|
| 55 |
if (is_array($order->products)) {
|
| 56 |
$stock_warnings = array();
|
| 57 |
foreach ($order->products as $product) {
|
| 58 |
if (($stock = uc_stock_level($product->model)) !== FALSE) {
|
| 59 |
$stock_level = db_fetch_object(db_query("SELECT * FROM {uc_product_stock} WHERE sku = '%s'", $product->model));
|
| 60 |
if ((($stock - $product->qty) <= $stock_level->threshold) && !in_array($product->model, array_keys($stock_warnings))) {
|
| 61 |
$stock_level->stock -= $product->qty;
|
| 62 |
$stock_warnings[$product->model] = $stock_level;
|
| 63 |
}
|
| 64 |
uc_stock_adjust($product->model, -$product->qty);
|
| 65 |
uc_order_comment_save($order->order_id, 0, t('The stock level for %model_name has been decreased to !qty.', array('%model_name' => $product->model, '!qty' => ($stock - $product->qty))));
|
| 66 |
}
|
| 67 |
}
|
| 68 |
if (!empty($stock_warnings) && variable_get('uc_stock_threshold_notification', FALSE)) {
|
| 69 |
foreach ($stock_warnings as $model => $stock_level) {
|
| 70 |
_uc_stock_send_mail($order, $stock_level);
|
| 71 |
}
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|