WastageController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. //损耗
  3. namespace ghs\controllers;
  4. use common\components\dict;
  5. use Yii;
  6. use bizGhs\order\classes\StockWastageOrderClass;
  7. use common\components\util;
  8. use bizGhs\product\classes\ProductClass;
  9. //已中央化
  10. class WastageController extends BaseController
  11. {
  12. //新增损耗
  13. public function actionCreateOrder()
  14. {
  15. $shopAdmin = $this->shopAdmin;
  16. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  17. util::fail('超管才能报损');
  18. }
  19. $post = Yii::$app->request->post();
  20. $post['shopId'] = $this->shopId;
  21. $post['sjId'] = $this->sjId;
  22. $post['mainId'] = $this->mainId;
  23. $post['shopAdminId'] = $this->shopAdminId;
  24. $shopAdmin = $this->shopAdmin;
  25. $adminName = $shopAdmin['name'] ?? '';
  26. $post['shopAdminName'] = $adminName;
  27. $productJson = $post['product'] ?? '';
  28. if (empty($productJson)) {
  29. util::fail('请选择花材');
  30. }
  31. $productList = json_decode($productJson, true);
  32. if (empty($productList)) {
  33. util::fail('请选择花材');
  34. }
  35. //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
  36. $hasUnitType = array_column($productList, 'unitType');
  37. if (!empty($hasUnitType)) {
  38. $newProductList = [];
  39. foreach ($productList as $key => $val) {
  40. $unitType = $val['unitType'] ?? 0;
  41. $bigNum = 0;
  42. $smallNum = 0;
  43. $num = $val['num'] ?? 0;
  44. $id = $val['productId'] ?? 0;
  45. if ($unitType == dict::getDict('unitType', 'big')) {
  46. $bigNum = $num;
  47. } else {
  48. $smallNum = $num;
  49. }
  50. $newProductList[] = ['productId' => $id, 'bigNum' => $bigNum, 'smallNum' => $smallNum, 'classId' => 0, 'itemId' => 0];
  51. }
  52. $productList = $newProductList;
  53. }
  54. foreach ($productList as $v) {
  55. $bigNum = $v['bigNum'] ?? 0;
  56. $smallNum = $v['smallNum'] ?? 0;
  57. if ($bigNum <= 0 && $smallNum <= 0) {
  58. util::fail('花材数量不能为0');
  59. }
  60. }
  61. $connection = Yii::$app->db;
  62. $transaction = $connection->beginTransaction();
  63. try {
  64. //判断花材有效性
  65. ProductClass::valid($productList, $this->mainId);
  66. $post['product'] = $productList;
  67. $return = StockWastageOrderClass::addOrder($post, $this->shop, true);
  68. $transaction->commit();
  69. util::success($return);
  70. } catch (\Exception $e) {
  71. $transaction->rollBack();
  72. Yii::info("报损失败原因:" . $e->getMessage());
  73. util::fail('报损保存失败');
  74. }
  75. }
  76. //列表
  77. public function actionList()
  78. {
  79. $where = [];
  80. $where['mainId'] = $this->mainId;
  81. $list = StockWastageOrderClass::getOrderList($where);
  82. util::success($list);
  83. }
  84. public function actionDetail()
  85. {
  86. $orderSn = Yii::$app->request->get('orderSn', '');
  87. $orderData = StockWastageOrderClass::getOrderDetail($orderSn, $this->mainId);
  88. util::success($orderData);
  89. }
  90. }