PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/stock.php

https://bitbucket.org/cpoppema/stockpiler
PHP | 423 lines | 365 code | 41 blank | 17 comment | 54 complexity | d1f9edd06a0c52fff43b4a3fc919993d MD5 | raw file
  1. <?php
  2. const MSG_SUCCESS_DELIVERY = 'Your products have been added to stock successfully!';
  3. const MSG_SUCCESS_PICKUP = 'Your products have been removed from stock successfully!';
  4. const MSG_SUCCESS_EDIT_STOCK = 'Your %s was updated successfully!';
  5. const MSG_UNAUTHORIZED_DELIVERY = 'You are not authorized to make a delivery!';
  6. const MSG_UNAUTHORIZED_PICKUP = 'You are not authorized to do a pickup!';
  7. const MSG_UNAUTHORIZED_EDIT_STOCK = 'You are not authorized to edit deliveries and pickups!';
  8. function stock_history()
  9. {
  10. Security_Authorize();
  11. $stock_changes = R::findAll('stock', 'order by id desc');
  12. $index = R::$adapter->getAffectedRows();
  13. R::preload($stock_changes, array('product'));
  14. foreach($stock_changes as $stock)
  15. {
  16. $css_class = ($stock['type'] == 'delivery') ? 'success' : 'error';
  17. if ($stock['iscanceled'] == 1)
  18. {
  19. $css_class .= ' warning';
  20. }
  21. if ($stock['iscanceled'] == 1 && $_SESSION['CurrentUser_HideInacitve'])
  22. {
  23. $css_class .= ' hide';
  24. }
  25. $body .= "<tr class='" . $css_class . "'>\n";
  26. $body .= "<th>\n";
  27. $body .= $index;
  28. $body .= "</th>\n";
  29. $body .= "<td class='name'>\n";
  30. $body .= $stock['product']['name'];
  31. $body .= "</td>\n";
  32. $body .= "<td class='code'>\n";
  33. $body .= $stock['product']['code'];
  34. $body .= "</td>\n";
  35. $body .= "<td>\n";
  36. $body .= (($stock['type'] == 'delivery') ? '&plus;' : '&minus;') . '&nbsp;' . abs($stock['amount']);
  37. $body .= "</td>\n";
  38. if ($_SESSION["CurrentUser_IsReadOnly"] != "1")
  39. {
  40. $body .= "<td>\n";
  41. $body .= "<a href='" . option('base_uri') . "stock/" . $stock['id'] . "'>Edit</a>\n";
  42. if (isset($stock['order_id']))
  43. {
  44. $body .= "| <a href='" . option('base_uri') . "orders/" . $stock['order_id'] . "'>View order</a>\n";
  45. }
  46. $body .= "</td>\n";
  47. } else {
  48. $body .= "<td>\n";
  49. if (isset($stock['order_id']))
  50. {
  51. $body .= "<a href='" . option('base_uri') . "stock/" . $stock['order_id'] . "'>View order</a>\n";
  52. }
  53. $body .= "</td>\n";
  54. }
  55. $body .= "</tr>\n";
  56. $index--;
  57. }
  58. if (R::$adapter->getAffectedRows() < 1)
  59. {
  60. $body .= "<tr>\n";
  61. $body .= "<td colspan='3'>You're a stock.</td>\n";
  62. $body .= "</tr>\n";
  63. }
  64. // Get typeahead data
  65. $names = array();
  66. $codes = array();
  67. $products = R::findAll('product', 'order by name asc, code asc');
  68. foreach ($products as $product) {
  69. $names[] = $product['name'];
  70. $codes[] = $product['code'];
  71. }
  72. set("names", $names);
  73. set("codes", $codes);
  74. set("title", "Stock History");
  75. set("body", $body);
  76. return html("stock/history.php");
  77. }
  78. function stock_delivery()
  79. {
  80. Security_Authorize();
  81. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  82. {
  83. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_DELIVERY);
  84. exit;
  85. }
  86. // Get typeahead data
  87. $names = array();
  88. $codes = array();
  89. $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
  90. foreach ($products as $product) {
  91. $names[] = array('value' => $product['id'], 'label' => $product['name']); // nested array to allow exact match
  92. $codes[] = $product['code'];
  93. }
  94. set("names", $names);
  95. set("codes", $codes);
  96. set("title", "Stock Delivery");
  97. return html("stock/delivery.php");
  98. }
  99. function stock_delivery_post()
  100. {
  101. Security_Authorize();
  102. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  103. {
  104. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_DELIVERY);
  105. exit;
  106. }
  107. if ($_POST['manualmode'] == '1')
  108. {
  109. if (empty($_POST['id']) || strlen(trim($_POST['id'])) == 0)
  110. {
  111. $_GET['error'] = "You need to provide at least a product!";
  112. }
  113. if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
  114. {
  115. $_GET['error'] = "Care to tell us how much you really want to add to stock?!";
  116. } else {
  117. $product = R::findOne('product', 'id = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['id']));
  118. if (R::$adapter->getAffectedRows() < 1)
  119. {
  120. $_GET['error'] = "The product you entered was not found! Did you scan a previous deleted product perhaps?";
  121. } else {
  122. // Add Delivery
  123. $stock = R::dispense('stock');
  124. $stock->product = $product;
  125. $stock->type = 'delivery';
  126. $stock->amount = $_POST['amount'];
  127. $stock->reason = null;
  128. $stock->iscanceled = false;
  129. $id = R::store($stock);
  130. // Log adding delivery
  131. $entry = R::dispense('log');
  132. $entry->action = 'created';
  133. $entry->object = $stock->getID();
  134. $entry->object_type = $stock->getMeta('type');
  135. $entry->user_id = $_SESSION['CurrentUser_ID'];
  136. $entry->date = R::isoDateTime();
  137. R::store($entry);
  138. // Delivery added, go to list
  139. header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_DELIVERY);
  140. exit;
  141. }
  142. }
  143. } else {
  144. if (empty($_POST['code']) || strlen(trim($_POST['code'])) == 0)
  145. {
  146. $_GET['error'] = "You need to provide at least the product's code!";
  147. }
  148. if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
  149. {
  150. $_GET['error'] = "Care to tell us how much you really want to add to stock?!";
  151. } else {
  152. $product = R::findOne('product', 'code = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['code']));
  153. if (R::$adapter->getAffectedRows() < 1)
  154. {
  155. $_GET['error'] = "The code you entered was not found! Did you scan a previous deleted product perhaps?";
  156. } else {
  157. // Add Delivery
  158. $stock = R::dispense('stock');
  159. $stock->product = $product;
  160. $stock->type = 'delivery';
  161. $stock->amount = $_POST['amount'];
  162. $stock->reason = null;
  163. $stock->iscanceled = false;
  164. $id = R::store($stock);
  165. // Log adding delivery
  166. $entry = R::dispense('log');
  167. $entry->action = 'created';
  168. $entry->object = $stock->getID();
  169. $entry->object_type = $stock->getMeta('type');
  170. $entry->user_id = $_SESSION['CurrentUser_ID'];
  171. $entry->date = R::isoDateTime();
  172. R::store($entry);
  173. // Delivery added, go to list
  174. header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_DELIVERY);
  175. exit;
  176. }
  177. }
  178. }
  179. // Get typeahead data
  180. $names = array();
  181. $codes = array();
  182. $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
  183. foreach ($products as $product) {
  184. $names[] = array('value' => $product['id'], 'label' => $product['name']); // nested array to allow exact match
  185. $codes[] = $product['code'];
  186. }
  187. set("names", $names);
  188. set("codes", $codes);
  189. // Errors, go to form
  190. set("title", "Stock Delivery");
  191. return html("stock/delivery.php");
  192. }
  193. function stock_pickup()
  194. {
  195. Security_Authorize();
  196. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  197. {
  198. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_PICKUP);
  199. exit;
  200. }
  201. // Get typeahead data
  202. $names = array();
  203. $codes = array();
  204. $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
  205. foreach ($products as $product) {
  206. $names[] = array('value' => $product['id'], 'label' => $product['name']); // nested array to allow exact match
  207. $codes[] = $product['code'];
  208. }
  209. set("names", $names);
  210. set("codes", $codes);
  211. set("title", "Stock Pickup");
  212. return html("stock/pickup.php");
  213. }
  214. function stock_pickup_post()
  215. {
  216. Security_Authorize();
  217. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  218. {
  219. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_PICKUP);
  220. exit;
  221. }
  222. if ($_POST['manualmode'] == '1')
  223. {
  224. if (empty($_POST['id']) || strlen(trim($_POST['id'])) == 0)
  225. {
  226. $_GET['error'] = "You need to provide at least a product!";
  227. }
  228. if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
  229. {
  230. $_GET['error'] = "Care to tell us how much you really want to remove from stock?!";
  231. } else {
  232. $product = R::findOne('product', 'id = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['id']));
  233. if (R::$adapter->getAffectedRows() < 1)
  234. {
  235. $_GET['error'] = "The product you entered was not found! Did you scan a previous deleted product perhaps?";
  236. } else {
  237. // Add Pickup
  238. $stock = R::dispense('stock');
  239. $stock->product = $product;
  240. $stock->type = 'pickup';
  241. $stock->amount = $_POST['amount'] * -1;
  242. $stock->reason = null;
  243. $stock->iscanceled = false;
  244. $id = R::store($stock);
  245. // Log adding pickup
  246. $entry = R::dispense('log');
  247. $entry->action = 'created';
  248. $entry->object = $stock->getID();
  249. $entry->object_type = $stock->getMeta('type');
  250. $entry->user_id = $_SESSION['CurrentUser_ID'];
  251. $entry->date = R::isoDateTime();
  252. R::store($entry);
  253. // Delivery added, go to list
  254. header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_PICKUP);
  255. exit;
  256. }
  257. }
  258. } else {
  259. if (empty($_POST['code']) || strlen(trim($_POST['code'])) == 0)
  260. {
  261. $_GET['error'] = "You need to provide at least the code!";
  262. }
  263. if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
  264. {
  265. $_GET['error'] = "Care to tell us how much you really want to remove from stock?!";
  266. } else {
  267. $product = R::findOne('product', 'code = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['code']));
  268. if (R::$adapter->getAffectedRows() < 1)
  269. {
  270. $_GET['error'] = "The code you entered was not found! Did you scan a previous deleted product perhaps?";
  271. } else {
  272. // Add Pickup
  273. $stock = R::dispense('stock');
  274. $stock->product = $product;
  275. $stock->type = 'pickup';
  276. $stock->amount = $_POST['amount'] * -1;
  277. $stock->reason = null;
  278. $stock->iscanceled = false;
  279. $id = R::store($stock);
  280. // Log adding pickup
  281. $entry = R::dispense('log');
  282. $entry->action = 'created';
  283. $entry->object = $stock->getID();
  284. $entry->object_type = $stock->getMeta('type');
  285. $entry->user_id = $_SESSION['CurrentUser_ID'];
  286. $entry->date = R::isoDateTime();
  287. R::store($entry);
  288. // Pickup added, go to list
  289. header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_PICKUP);
  290. exit;
  291. }
  292. }
  293. }
  294. // Get typeahead data
  295. $names = array();
  296. $codes = array();
  297. $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
  298. foreach ($products as $product) {
  299. $names[] = array('value' => $product['id'], 'label' => $product['name']); // nested array to allow exact match
  300. $codes[] = $product['code'];
  301. }
  302. set("names", $names);
  303. set("codes", $codes);
  304. // Errors, go to form
  305. set("title", "Stock Pickup");
  306. return html("stock/pickup.php");
  307. }
  308. function stock_edit()
  309. {
  310. Security_Authorize();
  311. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  312. {
  313. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_EDIT_STOCK);
  314. exit;
  315. }
  316. $stock = R::load('stock', params('id'));
  317. if (!$stock->id)
  318. {
  319. set("title", "Not Found");
  320. set("type", "");
  321. return html("error/notfound.php");
  322. }
  323. R::preload($stock, array('product'));
  324. $stock['amount'] = abs($stock['amount']);
  325. set("title", "Edit " . ucfirst($stock['type']));
  326. set("stock", $stock);
  327. return html("stock/edit.php");
  328. }
  329. function stock_edit_post()
  330. {
  331. Security_Authorize();
  332. if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
  333. {
  334. header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_EDIT_STOCK);
  335. exit;
  336. }
  337. $stock = R::load('stock', params('id'));
  338. if (!$stock->id)
  339. {
  340. set("title", "Not Found");
  341. set("type", "");
  342. return html("error/notfound.php");
  343. }
  344. // Log editing delivery/pickup
  345. $entry = R::dispense('log');
  346. $entry->action = 'modified';
  347. if ($_POST['iscanceled'] == 1)
  348. {
  349. if ($stock->iscanceled != 1)
  350. {
  351. $entry->action = 'canceled';
  352. } else {
  353. $entry->action = 'modified when canceled';
  354. }
  355. }
  356. elseif ($stock->iscanceled == 1)
  357. {
  358. $entry->action = 'uncanceled';
  359. }
  360. // Edit delivery/pickup
  361. $stock->amount = ($stock->type == 'delivery') ? $_POST['amount'] : $_POST['amount'] * -1;
  362. $stock->iscanceled = ((isset($_POST['iscanceled'])) ? true : false);
  363. R::store($stock);
  364. // Continue log entry editing delivery/pickup
  365. $entry->object = $stock->getID();
  366. $entry->object_type = $stock->getMeta('type');
  367. $entry->user_id = $_SESSION['CurrentUser_ID'];
  368. $entry->date = R::isoDateTime();
  369. R::store($entry);
  370. header("Location: " . option('base_uri') . "stock&success=" . sprintf(MSG_SUCCESS_EDIT_STOCK, $stock['type']));
  371. exit;
  372. }