StockOutController.php 7.4 KB

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