uc_stock.admin.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * @file
  4. * Stock administration menu items.
  5. */
  6. /**
  7. * Form builder for stock settings form.
  8. *
  9. * @ingroup forms
  10. */
  11. function uc_stock_settings_form($form, &$form_state) {
  12. $form['uc_stock_threshold_notification'] = array(
  13. '#type' => 'checkbox',
  14. '#title' => t('Send email notification when stock level reaches its threshold value'),
  15. '#default_value' => variable_get('uc_stock_threshold_notification', FALSE),
  16. );
  17. $form['uc_stock_threshold_notification_recipients'] = array(
  18. '#type' => 'textfield',
  19. '#title' => t('Notification recipients'),
  20. '#default_value' => variable_get('uc_stock_threshold_notification_recipients', uc_store_email()),
  21. '#description' => t('The list of comma-separated email addresses that will receive the notification.'),
  22. );
  23. $form['uc_stock_threshold_notification_subject'] = array(
  24. '#type' => 'textfield',
  25. '#title' => t('Message subject'),
  26. '#default_value' => variable_get('uc_stock_threshold_notification_subject', uc_get_message('uc_stock_threshold_notification_subject')),
  27. );
  28. $form['uc_stock_threshold_notification_message'] = array(
  29. '#type' => 'textarea',
  30. '#title' => t('Message text'),
  31. '#default_value' => variable_get('uc_stock_threshold_notification_message', uc_get_message('uc_stock_threshold_notification_message')),
  32. '#description' => t('The message the user receives when the stock level reaches its threshold value.'),
  33. '#rows' => 10,
  34. );
  35. if (module_exists('token')) {
  36. $form['token_tree'] = array(
  37. '#markup' => theme('token_tree', array('token_types' => array('uc_order', 'uc_stock', 'node', 'site', 'store'))),
  38. );
  39. }
  40. return system_settings_form($form);
  41. }
  42. /**
  43. * Displays a stock report for products with stock tracking enabled.
  44. */
  45. function uc_stock_report() {
  46. $page_size = (isset($_GET['nopage'])) ? UC_REPORTS_MAX_RECORDS : variable_get('uc_reports_table_size', 30);
  47. $csv_rows = array();
  48. $rows = array();
  49. $header = array(
  50. array('data' => t('SKU'), 'field' => 'sku', 'sort' => 'asc'),
  51. array('data' => t('Product'), 'field' => 'title'),
  52. array('data' => t('Stock'), 'field' => 'stock'),
  53. array('data' => t('Threshold'), 'field' => 'threshold'),
  54. array('data' => t('Operations')),
  55. );
  56. $csv_rows[] = array(t('SKU'), t('Product'), t('Stock'), t('Threshold'));
  57. $query = db_select('uc_product_stock', 's')->extend('PagerDefault')->extend('TableSort')
  58. ->orderByHeader($header)
  59. ->limit($page_size)
  60. ->fields('s', array(
  61. 'nid',
  62. 'sku',
  63. 'stock',
  64. 'threshold',
  65. ));
  66. $query->leftJoin('node', 'n', 's.nid = n.nid');
  67. $query->addField('n', 'title');
  68. $query->condition('active', 1)
  69. ->condition('title', '', '<>');
  70. if (arg(4) == 'threshold') {
  71. $query->where('threshold >= stock');
  72. }
  73. $result = $query->execute();
  74. foreach ($result as $stock) {
  75. $op = array();
  76. if (user_access('administer product stock')) {
  77. $op[] = l(t('edit'), 'node/' . $stock->nid . '/edit/stock', $options = array('query' => array('destination' => 'admin/store/reports/stock')));
  78. }
  79. // Add the data to a table row for display.
  80. $rows[] = array(
  81. 'data' => array(
  82. array('data' => $stock->sku),
  83. array('data' => l($stock->title, 'node/' . $stock->nid)),
  84. array('data' => $stock->stock),
  85. array('data' => $stock->threshold),
  86. array('data' => implode(' ', $op)),
  87. ),
  88. 'class' => array(($stock->threshold >= $stock->stock) ? 'uc-stock-below-threshold' : 'uc-stock-above-threshold'),
  89. );
  90. // Add the data to the CSV contents for export.
  91. $csv_rows[] = array($stock->sku, $stock->title, $stock->stock, $stock->threshold);
  92. }
  93. module_load_include('inc', 'uc_reports', 'uc_reports.admin');
  94. $csv_data = uc_reports_store_csv('uc_stock', $csv_rows);
  95. $build['form'] = drupal_get_form('uc_stock_report_form');
  96. $build['report'] = array(
  97. '#theme' => 'table',
  98. '#header' => $header,
  99. '#rows' => $rows,
  100. '#attributes' => array('width' => '100%', 'class' => array('uc-stock-table')),
  101. );
  102. $build['pager'] = array(
  103. '#theme' => 'pager',
  104. );
  105. $build['links'] = array(
  106. '#prefix' => '<div class="uc-reports-links">',
  107. '#suffix' => '</div>',
  108. );
  109. $build['links']['export_csv'] = array(
  110. '#markup' => l(t('Export to CSV file'), 'admin/store/reports/getcsv/' . $csv_data['report'] . '/' . $csv_data['user']),
  111. '#suffix' => '&nbsp;&nbsp;&nbsp;',
  112. );
  113. if (isset($_GET['nopage'])) {
  114. $build['links']['toggle_pager'] = array(
  115. '#markup' => l(t('Show paged records'), 'admin/store/reports/stock'),
  116. );
  117. }
  118. else {
  119. $build['links']['toggle_pager'] = array(
  120. '#markup' => l(t('Show all records'), 'admin/store/reports/stock', array('query' => array('nopage' => '1'))),
  121. );
  122. }
  123. return $build;
  124. }
  125. /**
  126. * Form builder for stock report threshold filter.
  127. *
  128. * @see uc_stock_report_form_submit()
  129. * @ingroup forms
  130. */
  131. function uc_stock_report_form($form, &$form_state) {
  132. $form['threshold'] = array(
  133. '#type' => 'checkbox',
  134. '#title' => t('Only show SKUs that are below their threshold.'),
  135. '#default_value' => arg(4) == 'threshold' ? TRUE : FALSE,
  136. '#attributes' => array('onchange' => 'this.form.submit();'),
  137. );
  138. $form['actions'] = array('#type' => 'actions');
  139. $form['actions']['submit'] = array(
  140. '#type' => 'submit',
  141. '#value' => t('Update'),
  142. '#attributes' => array('style' => "display:none;"),
  143. );
  144. return $form;
  145. }
  146. /**
  147. * Form submission handler for uc_stock_report_form().
  148. *
  149. * @see uc_stock_report_form()
  150. */
  151. function uc_stock_report_form_submit($form, &$form_state) {
  152. if ($form_state['values']['threshold']) {
  153. drupal_goto('admin/store/reports/stock/threshold');
  154. }
  155. else {
  156. drupal_goto('admin/store/reports/stock');
  157. }
  158. }
  159. /**
  160. * Form builder for product stock edit form.
  161. *
  162. * @see uc_stock_edit_form_submit()
  163. * @see theme_uc_stock_edit_form()
  164. * @ingroup forms
  165. */
  166. function uc_stock_edit_form($form, &$form_state, $node) {
  167. drupal_set_title($node->title);
  168. $form['stock'] = array('#tree' => TRUE);
  169. $skus = uc_product_get_models($node->nid);
  170. // Remove 'Any'.
  171. unset($skus[NULL]);
  172. if (!$skus) {
  173. drupal_set_message(t('No SKU found.'), 'error');
  174. }
  175. else {
  176. foreach (array_values($skus) as $id => $sku) {
  177. $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchAssoc();
  178. $form['stock'][$id]['sku'] = array(
  179. '#type' => 'value',
  180. '#value' => $sku,
  181. );
  182. // Checkbox to mark this as active.
  183. $form['stock'][$id]['active'] = array(
  184. '#type' => 'checkbox',
  185. '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
  186. );
  187. // Sanitized version of the SKU for display.
  188. $form['stock'][$id]['display_sku'] = array(
  189. '#markup' => check_plain($sku),
  190. );
  191. // Textfield for entering the stock level.
  192. $form['stock'][$id]['stock'] = array(
  193. '#type' => 'textfield',
  194. '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0,
  195. '#maxlength' => 9,
  196. '#size' => 9,
  197. );
  198. // Textfield for entering the threshold level.
  199. $form['stock'][$id]['threshold'] = array(
  200. '#type' => 'textfield',
  201. '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0,
  202. '#maxlength' => 9,
  203. '#size' => 9,
  204. );
  205. }
  206. }
  207. $form['nid'] = array(
  208. '#type' => 'value',
  209. '#value' => $node->nid,
  210. );
  211. $form['actions'] = array('#type' => 'actions');
  212. $form['actions']['save'] = array(
  213. '#type' => 'submit',
  214. '#value' => t('Save changes'),
  215. );
  216. return $form;
  217. }
  218. /**
  219. * Form submission handler for uc_stock_edit_form().
  220. *
  221. * @see uc_stock_edit_form()
  222. * @see theme_uc_stock_edit_form()
  223. */
  224. function uc_stock_edit_form_submit($form, &$form_state) {
  225. foreach (element_children($form_state['values']['stock']) as $id) {
  226. $stock = $form_state['values']['stock'][$id];
  227. db_merge('uc_product_stock')
  228. ->key(array('sku' => $stock['sku']))
  229. ->updateFields(array(
  230. 'active' => $stock['active'],
  231. 'stock' => $stock['stock'],
  232. 'threshold' => $stock['threshold'],
  233. ))
  234. ->insertFields(array(
  235. 'sku' => $stock['sku'],
  236. 'active' => $stock['active'],
  237. 'stock' => $stock['stock'],
  238. 'threshold' => $stock['threshold'],
  239. 'nid' => $form_state['values']['nid'],
  240. ))
  241. ->execute();
  242. }
  243. drupal_set_message(t('Stock settings saved.'));
  244. }
  245. /**
  246. * Returns HTML for uc_stock_edit_form().
  247. *
  248. * @param $variables
  249. * An associative array containing:
  250. * - form: A render element representing the form.
  251. *
  252. * @see uc_stock_edit_form()
  253. * @see uc_stock_edit_form_submit()
  254. * @ingroup themeable
  255. */
  256. function theme_uc_stock_edit_form($variables) {
  257. $form = $variables['form'];
  258. drupal_add_js('misc/tableselect.js');
  259. $header = array(
  260. array('data' => '&nbsp;&nbsp;' . t('Active'), 'class' => array('select-all')),
  261. array('data' => t('SKU')),
  262. array('data' => t('Stock')),
  263. array('data' => t('Threshold')),
  264. );
  265. $rows = array();
  266. foreach (element_children($form['stock']) as $id) {
  267. $rows[] = array(
  268. array('data' => drupal_render($form['stock'][$id]['active'])),
  269. array('data' => drupal_render($form['stock'][$id]['display_sku'])),
  270. array('data' => drupal_render($form['stock'][$id]['stock'])),
  271. array('data' => drupal_render($form['stock'][$id]['threshold'])),
  272. );
  273. }
  274. return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($form);
  275. }