StockOutController.php 7.4 KB

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