StockOutController.php 7.5 KB

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