uc_stock.module 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @file
  4. * Allow ubercart products to have stock levels associated with their SKU
  5. *
  6. * uc_stock enables ubercart to manage stock for products. Store admins can set
  7. * the stock levels on a product edit page and a threshold for each SKU value
  8. * When that threshold is reached admins can be optionally notified about the
  9. * current stock level. Store admins can view all stock levels in the reports
  10. * section of Ubercart.
  11. */
  12. /**
  13. * Implements hook_help().
  14. */
  15. function uc_stock_help($path, $arg) {
  16. switch ($path) {
  17. case 'node/%/edit/stock':
  18. return '<p>' . t('To keep track of stock for a particular product SKU, make sure it is marked as active and enter a stock value. When the stock level drops below the threshold value, you can be notified based on your stock settings.') . '</p>';
  19. case 'admin/store/reports/stock':
  20. case 'admin/store/reports/stock/threshold':
  21. return '<p>' . t('This is the list of product SKUs that are currently active. Stock levels below their threshold have highlighted rows. Toggle the checkbox below to alter which stock levels are shown.') . '</p>';
  22. }
  23. }
  24. /**
  25. * Implements hook_menu().
  26. */
  27. function uc_stock_menu() {
  28. $items = array();
  29. $items['admin/store/settings/stock'] = array(
  30. 'title' => 'Stock notifications',
  31. 'description' => 'Enable or disable stock level notifications.',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('uc_stock_settings_form'),
  34. 'access arguments' => array('administer product stock'),
  35. 'file' => 'uc_stock.admin.inc',
  36. );
  37. if (module_exists('uc_reports')) {
  38. $items['admin/store/reports/stock'] = array(
  39. 'title' => 'Stock reports',
  40. 'description' => 'View reports for product stock.',
  41. 'page callback' => 'uc_stock_report',
  42. 'access arguments' => array('view reports'),
  43. 'file' => 'uc_stock.admin.inc',
  44. );
  45. }
  46. $items['node/%node/edit/stock'] = array(
  47. 'title' => 'Stock',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('uc_stock_edit_form', 1),
  50. 'access callback' => 'uc_stock_product_access',
  51. 'access arguments' => array(1),
  52. 'weight' => 10,
  53. 'type' => MENU_LOCAL_TASK,
  54. 'file' => 'uc_stock.admin.inc',
  55. );
  56. return $items;
  57. }
  58. /**
  59. * Implements hook_admin_paths().
  60. */
  61. function uc_stock_admin_paths() {
  62. $paths = array(
  63. 'node/*/edit/stock' => TRUE,
  64. );
  65. return $paths;
  66. }
  67. /**
  68. * Access callback for node/%node/edit/stock.
  69. */
  70. function uc_stock_product_access($node) {
  71. if ($node->type == 'product_kit') {
  72. return FALSE;
  73. }
  74. return uc_product_is_product($node) && node_access('update', $node) && user_access('administer product stock');
  75. }
  76. /**
  77. * Implements hook_permission().
  78. */
  79. function uc_stock_permission() {
  80. return array(
  81. 'administer product stock' => array(
  82. 'title' => t('Administer product stock'),
  83. ),
  84. );
  85. }
  86. /**
  87. * Implements hook_theme().
  88. */
  89. function uc_stock_theme() {
  90. return array(
  91. 'uc_stock_edit_form' => array(
  92. 'render element' => 'form',
  93. 'file' => 'uc_stock.admin.inc',
  94. ),
  95. );
  96. }
  97. /**
  98. * Implements hook_mail().
  99. */
  100. function uc_stock_mail($key, &$message, $params) {
  101. switch ($key) {
  102. case 'threshold':
  103. $message['subject'] = $params['subject'];
  104. $message['body'][] = $params['body'];
  105. break;
  106. }
  107. }
  108. /**
  109. * Implements hook_uc_message().
  110. */
  111. function uc_stock_uc_message() {
  112. $messages['uc_stock_threshold_notification_subject'] = t('[store:name]: Stock threshold limit reached');
  113. $messages['uc_stock_threshold_notification_message'] = t('This message has been sent to let you know that the stock level for "[node:title]" with SKU [uc_stock:model] has reached [uc_stock:level]. There may not be enough units in stock to fulfill order #[uc_order:link].');
  114. return $messages;
  115. }
  116. /**
  117. * Adjusts the product stock level by a set amount.
  118. *
  119. * @param $sku
  120. * The product SKU of the stock level to adjust.
  121. * @param $qty
  122. * The amount to add to or subtract from the stock level.
  123. * @param $check_active
  124. * If FALSE, don't check if stock tracking is active for this SKU.
  125. */
  126. function uc_stock_adjust($sku, $qty, $check_active = TRUE) {
  127. $stock = db_query("SELECT active, stock FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchObject();
  128. if ($check_active) {
  129. if (!$stock || !$stock->active) {
  130. return;
  131. }
  132. }
  133. db_update('uc_product_stock')
  134. ->expression('stock', 'stock + :qty', array(':qty' => $qty))
  135. ->condition('sku', $sku)
  136. ->execute();
  137. module_invoke_all('uc_stock_adjusted', $sku, $stock->stock, $qty);
  138. }
  139. /**
  140. * Sets the product stock level.
  141. *
  142. * @param $sku
  143. * The product SKU of the stock level to set.
  144. * @param $qty
  145. * The number of items in stock.
  146. */
  147. function uc_stock_set($sku, $qty) {
  148. db_update('uc_product_stock')
  149. ->fields(array('stock' => $qty))
  150. ->condition('sku', $sku)
  151. ->execute();
  152. }
  153. /**
  154. * Gets the stock level of a particular product SKU.
  155. *
  156. * @param $sku
  157. * The Ubercart product SKU of the stock level to return.
  158. *
  159. * @return
  160. * The SKU's stock level, or FALSE if not active.
  161. */
  162. function uc_stock_level($sku) {
  163. $stock = db_query("SELECT active, stock FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchObject();
  164. if ($stock && $stock->active) {
  165. return $stock->stock;
  166. }
  167. return FALSE;
  168. }
  169. /**
  170. * Checks if a SKU has an active stock record.
  171. *
  172. * @param $sku
  173. * The Ubercart product SKU to check
  174. *
  175. * @return
  176. * Boolean indicating whether or not the sku has an active stock record.
  177. */
  178. function uc_stock_is_active($sku) {
  179. return (bool) db_query("SELECT active FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchField();
  180. }
  181. /**
  182. * Emails administrator regarding any stock level thresholds hit.
  183. *
  184. * @param $order
  185. * The order object that tripped the threshold limit.
  186. * @param $node
  187. * The node object that is associated with the SKU/model.
  188. * @param $stock
  189. * The stock level object that contains the stock level and SKU.
  190. *
  191. * @return
  192. * The result of drupal_mail().
  193. */
  194. function _uc_stock_send_mail($order, $node, $stock) {
  195. $account = user_load($order->uid);
  196. $token_filters = array('uc_order' => $order, 'user' => $account, 'uc_stock' => $stock, 'node' => $node);
  197. $to = variable_get('uc_stock_threshold_notification_recipients', uc_store_email());
  198. $to = explode(',', $to);
  199. $from = uc_store_email_from();
  200. $subject = variable_get('uc_stock_threshold_notification_subject', uc_get_message('uc_stock_threshold_notification_subject'));
  201. $subject = token_replace($subject, $token_filters);
  202. $body = variable_get('uc_stock_threshold_notification_message', uc_get_message('uc_stock_threshold_notification_message'));
  203. $body = token_replace($body, $token_filters);
  204. // Send to each recipient.
  205. foreach ($to as $email) {
  206. $sent = drupal_mail('uc_stock', 'threshold', $email, uc_store_mail_recipient_language($email), array('body' => $body, 'subject' => $subject, 'order' => $order, 'stock' => $stock), $from);
  207. if (!$sent['result']) {
  208. watchdog('uc_stock', 'Attempt to e-mail @email concerning stock level on sku @sku failed.', array('@email' => $email, '@sku' => $stock->sku), WATCHDOG_ERROR);
  209. }
  210. }
  211. }
  212. /**
  213. * Implements hook_views_api().
  214. */
  215. function uc_stock_views_api() {
  216. return array(
  217. 'api' => '2.0',
  218. 'path' => drupal_get_path('module', 'uc_stock') . '/views',
  219. );
  220. }
  221. /**
  222. * Decrement a product's stock.
  223. *
  224. * @param $product
  225. * The product whose stock is being adjusted.
  226. * @param $key
  227. * Internal, so this function can be used as a callback of array_walk().
  228. * @param $order
  229. * Order object.
  230. */
  231. function uc_stock_adjust_product_stock($product, $key, $order) {
  232. // Product has an active stock?
  233. if (!uc_stock_is_active($product->model)) {
  234. return;
  235. }
  236. // Do nothing if decrement quantity is 0.
  237. if ($product->qty == 0) {
  238. return;
  239. }
  240. // Adjust the product's stock.
  241. uc_stock_adjust($product->model, -$product->qty);
  242. // Load the new stock record.
  243. $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $product->model))->fetchObject();
  244. // Should we notify?
  245. if (variable_get('uc_stock_threshold_notification', FALSE) && $stock->stock <= $stock->threshold) {
  246. $node = node_load($product->nid);
  247. _uc_stock_send_mail($order, $node, $stock);
  248. }
  249. // Save a comment about the stock level.
  250. uc_order_comment_save($order->order_id, 0, t('The stock level for %model_name has been !action to !qty.', array('%model_name' => $product->model, '!qty' => $stock->stock, '!action' => (-$product->qty <= 0) ? t('decreased') : t('increased') )));
  251. }
  252. /**
  253. * Increment a product's stock.
  254. */
  255. function uc_stock_increment_product_stock($product, $key, $order) {
  256. $product->qty = -$product->qty;
  257. return uc_stock_adjust_product_stock($product, $key, $order);
  258. }