StockOutController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\merchant\classes\ShopClass;
  4. use bizGhs\order\classes\StockOutOrderClass;
  5. use bizGhs\product\classes\ProductClass;
  6. use common\components\util;
  7. use Yii;
  8. //出库 lqh 2021.1.23
  9. class StockOutController extends BaseController
  10. {
  11. //全部出库 ssh 20220906
  12. public function actionAllOut()
  13. {
  14. $get = Yii::$app->request->get();
  15. $inShopId = $get['shopId'] ?? 0;
  16. $inShop = ShopClass::getById($inShopId, true);
  17. if (empty($inShop)) {
  18. util::fail('入库门店没有找到');
  19. }
  20. $inMainId = $inShop->mainId ?? 0;
  21. $shop = $this->shop;
  22. if ($shop->default == 1) {
  23. util::fail('首店库存不能全部出库');
  24. }
  25. $connection = Yii::$app->db;
  26. $transaction = $connection->beginTransaction();
  27. try {
  28. $shopId = $this->shopId;
  29. $mainId = $this->mainId;
  30. $sjId = $shop->sjId ?? 0;
  31. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  32. if (empty($list)) {
  33. util::fail('没有花材');
  34. }
  35. $itemInfo = [];
  36. foreach ($list as $item) {
  37. if ($item->delStatus == 1) {
  38. continue;
  39. }
  40. $stock = $item->stock ?? 0;
  41. $ratio = $item->ratio ?? 0;
  42. if ($stock <= 0) {
  43. continue;
  44. }
  45. $bigNum = intval($stock);
  46. $small = bcsub($stock, $bigNum, 2);
  47. $smallNum = bcmul($small, $ratio);
  48. $currentId = $item->id;
  49. $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  50. }
  51. if (empty($itemInfo)) {
  52. util::fail('没有库存');
  53. }
  54. $data = [
  55. 'remark' => '全部出库',
  56. 'itemInfo' => $itemInfo,
  57. 'inShopId' => $inShopId,
  58. 'inMainId' => $inMainId,
  59. 'outMainId' => $mainId,
  60. 'outShopId' => $shopId,
  61. 'sjId' => $sjId,
  62. ];
  63. StockOutOrderClass::addOrder($data);
  64. $transaction->commit();
  65. util::complete();
  66. } catch (\Exception $exception) {
  67. $transaction->rollBack();
  68. util::fail('出库失败');
  69. }
  70. }
  71. //出库列表 lqh 2021.1.23
  72. public function actionList()
  73. {
  74. $get = Yii::$app->request->get();
  75. $orderStatus = isset($get['status']) ? $get['status'] : '';
  76. $where = [];
  77. $where['outShopId'] = $this->shopId;
  78. if (!empty($orderStatus)) {
  79. $where['status'] = $orderStatus;
  80. }
  81. $list = StockOutOrderClass::getOrderList($where);
  82. util::success($list);
  83. }
  84. //新增出库 lqh 2021.1.23
  85. public function actionCreateOrder()
  86. {
  87. $post = Yii::$app->request->post();
  88. $post['sjId'] = $this->sjId;
  89. $post['outShopId'] = $this->shopId;
  90. $post['outMainId'] = $this->mainId;
  91. $post['adminId'] = $this->adminId;
  92. $inShopId = $post['inShopId'] ?? 0;
  93. $now = date('Y-m-d H:i:s');
  94. $date = $post['date'] ?? $now;
  95. $post['outTime'] = $date;
  96. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  97. $post['action'] = $action;
  98. $post['inShopId'] = $inShopId;
  99. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  100. if ($check) {
  101. util::fail('还有待出库的订单');
  102. }
  103. $ghsItemInfo = $post['itemInfo'] ?? '';
  104. if (empty($ghsItemInfo)) {
  105. util::fail('请选择花材');
  106. }
  107. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  108. if (!is_array($ghsItemInfo)) {
  109. util::fail('花材格式不正确');
  110. }
  111. $connection = Yii::$app->db;
  112. $transaction = $connection->beginTransaction();
  113. try {
  114. //入库门店有效性判断
  115. if ($this->shopId == $inShopId) {
  116. util::fail('不能出库给当前门店');
  117. }
  118. $inShop = ShopClass::getById($inShopId);
  119. if (empty($inShop)) {
  120. util::fail('没有找到门店');
  121. }
  122. ShopClass::valid($inShop, $this->sjId);
  123. $post['inMainId'] = $inShop['mainId'] ?? 0;
  124. ProductClass::valid($ghsItemInfo, $this->mainId);
  125. //判断花材里的信息
  126. foreach ($ghsItemInfo as $v) {
  127. $bigNum = $v['bigNum'] ?? 0;
  128. $smallNum = $v['smallNum'] ?? 0;
  129. if ($bigNum <= 0 && $smallNum <= 0) {
  130. util::fail('花材数量不能为0');
  131. }
  132. }
  133. $post['itemInfo'] = $ghsItemInfo;
  134. $order = StockOutOrderClass::addOrder($post);
  135. if ($order) {
  136. $transaction->commit();
  137. util::success($order);
  138. }
  139. util::fail('出库失败');
  140. } catch (\Exception $exception) {
  141. $transaction->rollBack();
  142. util::fail('出库失败');
  143. }
  144. }
  145. //出库详情 lqh 2021.1.23
  146. public function actionDetail()
  147. {
  148. $orderSn = Yii::$app->request->get('orderSn', '');
  149. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  150. util::success($orderData);
  151. }
  152. //确认出库 lqh 2021.1.23
  153. public function actionConfirmOrder()
  154. {
  155. $get = Yii::$app->request->get();
  156. $orderSn = $get['orderSn'];
  157. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  158. util::complete("出库成功");
  159. }
  160. //取消出库 lqh 2021.1.23
  161. public function actionCancelOrder()
  162. {
  163. $get = Yii::$app->request->get();
  164. $orderSn = $get['orderSn'];
  165. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  166. util::complete("取消成功");
  167. }
  168. }