WastageController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace hd\controllers;
  3. use bizGhs\order\classes\StockWastageOrderClass;
  4. use bizGhs\product\classes\ProductClass;
  5. use bizHd\wastage\classes\WastageClass;
  6. use common\components\dict;
  7. use common\components\util;
  8. use Yii;
  9. class WastageController extends BaseController
  10. {
  11. public $guestAccess = [];
  12. //商品报损 ssh 20220424
  13. public function actionGoodsWastage()
  14. {
  15. $shopAdmin = $this->shopAdmin;
  16. if (isset($shopAdmin->super) == false || $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. $goodsJson = $post['goodsList'] ?? '';
  28. if (empty($goodsJson)) {
  29. util::fail('请选择商品');
  30. }
  31. $goodsList = json_decode($goodsJson, true);
  32. if (empty($goodsList)) {
  33. util::fail('请选择商品');
  34. }
  35. $connection = Yii::$app->db;
  36. $transaction = $connection->beginTransaction();
  37. try {
  38. $post['goodsList'] = $goodsList;
  39. $return = WastageClass::goodsWastage($post);
  40. $transaction->commit();
  41. util::success($return);
  42. } catch (\Exception $e) {
  43. $transaction->rollBack();
  44. Yii::info("失败原因:" . $e->getMessage());
  45. util::fail('报损失败');
  46. }
  47. }
  48. //新增损耗
  49. public function actionCreateOrder()
  50. {
  51. $shopAdmin = $this->shopAdmin;
  52. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  53. util::fail('超管才能报损');
  54. }
  55. $post = Yii::$app->request->post();
  56. $post['shopId'] = $this->shopId;
  57. $post['sjId'] = $this->sjId;
  58. $post['mainId'] = $this->mainId;
  59. $post['shopAdminId'] = $this->shopAdminId;
  60. $shopAdmin = $this->shopAdmin;
  61. $adminName = $shopAdmin['name'] ?? '';
  62. $post['shopAdminName'] = $adminName;
  63. $productJson = $post['product'] ?? '';
  64. if (empty($productJson)) {
  65. util::fail('请选择花材');
  66. }
  67. $productList = json_decode($productJson, true);
  68. if (empty($productList)) {
  69. util::fail('请选择花材');
  70. }
  71. //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
  72. $hasUnitType = array_column($productList, 'unitType');
  73. if (!empty($hasUnitType)) {
  74. $newProductList = [];
  75. foreach ($productList as $key => $val) {
  76. $unitType = $val['unitType'] ?? 0;
  77. $bigNum = 0;
  78. $smallNum = 0;
  79. $num = $val['num'] ?? 0;
  80. $id = $val['productId'] ?? 0;
  81. if ($unitType == dict::getDict('unitType', 'big')) {
  82. $bigNum = $num;
  83. } else {
  84. $smallNum = $num;
  85. }
  86. $newProductList[] = ['productId' => $id, 'bigNum' => $bigNum, 'smallNum' => $smallNum, 'classId' => 0, 'itemId' => 0];
  87. }
  88. $productList = $newProductList;
  89. }
  90. foreach ($productList as $v) {
  91. $bigNum = $v['bigNum'] ?? 0;
  92. $smallNum = $v['smallNum'] ?? 0;
  93. if ($bigNum <= 0 && $smallNum <= 0) {
  94. util::fail('数量不能为0');
  95. }
  96. if (isset($v['property']) && $v['property'] == 0) {
  97. util::fail('花材和商品请分开报损');
  98. }
  99. }
  100. $connection = Yii::$app->db;
  101. $transaction = $connection->beginTransaction();
  102. try {
  103. ProductClass::valid($productList, $this->mainId);
  104. $post['product'] = $productList;
  105. $post['ptStyle'] = dict::getDict('ptStyle', 'hd');
  106. $return = StockWastageOrderClass::addOrder($post);
  107. $transaction->commit();
  108. util::success($return);
  109. } catch (\Exception $e) {
  110. $transaction->rollBack();
  111. Yii::info("报损失败原因:" . $e->getMessage());
  112. util::fail('报损保存失败');
  113. }
  114. }
  115. //列表
  116. public function actionList()
  117. {
  118. $where = [];
  119. $where['mainId'] = $this->mainId;
  120. $list = StockWastageOrderClass::getOrderList($where);
  121. util::success($list);
  122. }
  123. public function actionDetail()
  124. {
  125. $orderSn = Yii::$app->request->get('orderSn', '');
  126. $orderData = StockWastageOrderClass::getOrderDetail($orderSn, $this->mainId);
  127. util::success($orderData);
  128. }
  129. }