StockOutController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 'action' => StockOutOrderClass::ACTION_CONFIRM,
  63. ];
  64. StockOutOrderClass::addOrder($data);
  65. $transaction->commit();
  66. util::complete();
  67. } catch (\Exception $exception) {
  68. $transaction->rollBack();
  69. util::fail('出库失败');
  70. }
  71. }
  72. //出库列表 lqh 2021.1.23
  73. public function actionList()
  74. {
  75. $get = Yii::$app->request->get();
  76. $orderStatus = isset($get['status']) ? $get['status'] : '';
  77. $where = [];
  78. $where['outShopId'] = $this->shopId;
  79. if (!empty($orderStatus)) {
  80. $where['status'] = $orderStatus;
  81. }
  82. $list = StockOutOrderClass::getOrderList($where);
  83. util::success($list);
  84. }
  85. //新增出库 lqh 2021.1.23
  86. public function actionCreateOrder()
  87. {
  88. $post = Yii::$app->request->post();
  89. $post['sjId'] = $this->sjId;
  90. $post['outShopId'] = $this->shopId;
  91. $post['outMainId'] = $this->mainId;
  92. $post['adminId'] = $this->adminId;
  93. $inShopId = $post['inShopId'] ?? 0;
  94. $now = date('Y-m-d H:i:s');
  95. $date = $post['date'] ?? $now;
  96. $post['outTime'] = $date;
  97. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  98. $post['action'] = $action;
  99. $post['inShopId'] = $inShopId;
  100. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  101. if ($check) {
  102. util::fail('还有待出库的订单');
  103. }
  104. $ghsItemInfo = $post['itemInfo'] ?? '';
  105. if (empty($ghsItemInfo)) {
  106. util::fail('请选择花材');
  107. }
  108. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  109. if (!is_array($ghsItemInfo)) {
  110. util::fail('花材格式不正确');
  111. }
  112. $connection = Yii::$app->db;
  113. $transaction = $connection->beginTransaction();
  114. try {
  115. //入库门店有效性判断
  116. if ($this->shopId == $inShopId) {
  117. util::fail('不能出库给当前门店');
  118. }
  119. $inShop = ShopClass::getById($inShopId);
  120. if (empty($inShop)) {
  121. util::fail('没有找到门店');
  122. }
  123. ShopClass::valid($inShop, $this->sjId);
  124. $post['inMainId'] = $inShop['mainId'] ?? 0;
  125. ProductClass::valid($ghsItemInfo, $this->mainId);
  126. //判断花材里的信息
  127. foreach ($ghsItemInfo as $v) {
  128. $bigNum = $v['bigNum'] ?? 0;
  129. $smallNum = $v['smallNum'] ?? 0;
  130. if ($bigNum <= 0 && $smallNum <= 0) {
  131. util::fail('花材数量不能为0');
  132. }
  133. }
  134. $post['itemInfo'] = $ghsItemInfo;
  135. $order = StockOutOrderClass::addOrder($post);
  136. if ($order) {
  137. $transaction->commit();
  138. util::success($order);
  139. }
  140. util::fail('出库失败');
  141. } catch (\Exception $exception) {
  142. $transaction->rollBack();
  143. util::fail('出库失败');
  144. }
  145. }
  146. //出库详情 lqh 2021.1.23
  147. public function actionDetail()
  148. {
  149. $orderSn = Yii::$app->request->get('orderSn', '');
  150. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  151. util::success($orderData);
  152. }
  153. //确认出库 lqh 2021.1.23
  154. public function actionConfirmOrder()
  155. {
  156. $get = Yii::$app->request->get();
  157. $orderSn = $get['orderSn'];
  158. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  159. util::complete("出库成功");
  160. }
  161. //取消出库 lqh 2021.1.23
  162. public function actionCancelOrder()
  163. {
  164. $get = Yii::$app->request->get();
  165. $orderSn = $get['orderSn'];
  166. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  167. util::complete("取消成功");
  168. }
  169. }