StockOutController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\merchant\classes\ShopClass;
  4. use bizGhs\order\classes\StockInOrderClass;
  5. use bizGhs\order\classes\StockInOrderItemClass;
  6. use bizGhs\order\classes\StockOutOrderClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use common\components\util;
  9. use Yii;
  10. //出库 lqh 2021.1.23
  11. class StockOutController extends BaseController
  12. {
  13. //入库单一键出库 ssh 20231207
  14. public function actionOneKeyStockOut()
  15. {
  16. if (Yii::$app->request->isGet) {
  17. $post = Yii::$app->request->get();
  18. } elseif (Yii::$app->request->isPost) {
  19. $post = Yii::$app->request->post();
  20. }
  21. $orderSn = $post['orderSn'] ?? '';
  22. $in = StockInOrderClass::getByCondition(['orderSn' => $orderSn], true);
  23. if (empty($in)) {
  24. util::fail('没有找到入库单');
  25. }
  26. if ($in->inMainId != $this->mainId) {
  27. util::fail('不是你的入库单');
  28. }
  29. $inShopId = $post['inShopId'] ?? 0;
  30. if ($inShopId == $this->shopId) {
  31. util::fail('出库和入库门店一样');
  32. }
  33. $inShop = ShopClass::getById($inShopId, true);
  34. if (empty($inShop)) {
  35. util::fail('没有找到入库门店');
  36. }
  37. $changeList = $post['changeList'] ?? [];
  38. $changeData = [];
  39. if (!empty($changeList)) {
  40. $changeFormatData = json_decode($changeList, true);
  41. if (!empty($changeFormatData)) {
  42. foreach ($changeFormatData as $k => $v) {
  43. $productId = $v['productId'];
  44. $actPutNum = $v['actPutNum'] ?? 0;
  45. $itemName = $v['itemName'] ?? '';
  46. $changeData[$productId] = ['actPutNum' => $actPutNum, 'itemName' => $itemName];
  47. }
  48. }
  49. }
  50. $inItemList = StockInOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  51. if (empty($inItemList)) {
  52. util::fail('没有找到入库单的花材');
  53. }
  54. $itemInfo = [];
  55. foreach ($inItemList as $key => $info) {
  56. $productId = $info['productId'] ?? 0;
  57. $itemNum = $info['itemNum'] ? floatval($info['itemNum']) : 0;
  58. if (!empty($changeData)) {
  59. if (isset($changeData[$productId])) {
  60. $changeResult = $changeData[$productId];
  61. $itemNum = $changeResult['actPutNum'];
  62. $itemName = $changeResult['itemName'];
  63. if (!is_numeric($itemNum)) {
  64. util::fail('请填写【' . $itemName . '】回调数值');
  65. }
  66. }
  67. }
  68. $itemInfo[] = ['productId' => $productId, 'bigNum' => $itemNum, 'smallNum' => 0];
  69. }
  70. $arr = [
  71. 'confirmIn' => 1,
  72. 'date' => '',
  73. 'outTime' => '',
  74. 'remark' => '一键再次出库',
  75. 'inShopId' => $inShopId,
  76. 'inMainId' => $inShop->mainId,
  77. 'itemInfo' => $itemInfo,
  78. 'action' => 'confirm',
  79. 'sjId' => $this->sjId,
  80. 'outShopId' => $this->shopId,
  81. 'outMainId' => $this->mainId,
  82. 'adminId' => $this->adminId,
  83. ];
  84. $connection = Yii::$app->db;
  85. $transaction = $connection->beginTransaction();
  86. try {
  87. StockOutOrderClass::addOrder($arr);
  88. //回调次数+1
  89. $in->callbackTime += 1;
  90. $in->save();
  91. $transaction->commit();
  92. util::complete('操作成功');
  93. } catch (\Exception $exception) {
  94. $transaction->rollBack();
  95. util::fail('操作失败');
  96. }
  97. }
  98. //结清账单 ssh 20221113
  99. public function actionClear()
  100. {
  101. $shopAdmin = $this->shopAdmin;
  102. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  103. util::fail('超管才能修改');
  104. }
  105. $get = Yii::$app->request->get();
  106. $orderSn = $get['orderSn'] ?? '';
  107. $out = StockOutOrderClass::getByCondition(['orderSn' => $orderSn], true);
  108. if (empty($out)) {
  109. util::fail('没有找到出库单');
  110. }
  111. if ($out->outMainId != $this->mainId) {
  112. util::fail('没有权限');
  113. }
  114. if ($out->status != 2) {
  115. util::fail('已出库才能结账单');
  116. }
  117. if ($out->debt == 0) {
  118. util::fail('已经结过了');
  119. }
  120. $inOrderSn = $out->inOrderSn ?? '';
  121. $in = StockInOrderClass::getByCondition(['orderSn' => $inOrderSn], true);
  122. if (empty($in)) {
  123. util::fail('没有找到入库单');
  124. }
  125. if ($in->status != 2) {
  126. util::fail('已入库才能结账单');
  127. }
  128. if ($in->debt == 0) {
  129. util::fail('已经结过了');
  130. }
  131. $out->debt = 0;
  132. $out->save();
  133. $in->debt = 0;
  134. $in->save();
  135. util::complete();
  136. }
  137. /**
  138. * 全部出库
  139. * 职责:将当前门店下所有有库存且未删除的花材一次性全部出库到指定门店
  140. * 入参:GET 传参 shopId (入库门店ID), confirmIn (是否直接确认入库)
  141. * 返回:JSON 格式的成功提示
  142. * 副作用:扣减出库门店库存,记录出库单及明细,写入库存变动日志
  143. * 关键边界:
  144. * 1. 校验入库门店有效性,默认门店不允许全部出库,不能出库给自己
  145. * 2. 优化:直接在数据库过滤有库存且未删除的商品,且仅查询必要字段(id, stock, ratio),返回纯数组,避免实例化大量 ActiveRecord 对象,极大节省内存和 CPU 耗时
  146. */
  147. public function actionAllOut()
  148. {
  149. $get = Yii::$app->request->get();
  150. $inShopId = $get['shopId'] ?? 0;
  151. $confirmIn = $get['confirmIn'] ?? 0;
  152. $inShop = ShopClass::getById($inShopId, true);
  153. if (empty($inShop)) {
  154. util::fail('入库门店没有找到');
  155. }
  156. $inMainId = $inShop->mainId ?? 0;
  157. $shop = $this->shop;
  158. if ($shop->default == 1) {
  159. util::fail('默认门店不能全部出库');
  160. }
  161. $connection = Yii::$app->db;
  162. $transaction = $connection->beginTransaction();
  163. try {
  164. $shopId = $this->shopId;
  165. $mainId = $this->mainId;
  166. $sjId = $shop->sjId ?? 0;
  167. // 复杂分支/关键逻辑:优化查询。直接在数据库过滤 delStatus=0 且 stock>0 的商品,仅查询 id, stock, ratio 字段,且返回纯数组(不实例化对象)
  168. $list = ProductClass::getAllByCondition([
  169. 'mainId' => $mainId,
  170. 'delStatus' => 0,
  171. 'stock>' => 0,
  172. ], null, 'id, stock, ratio', null, false);
  173. if (empty($list)) {
  174. util::fail('没有花材');
  175. }
  176. if ($inShopId == $shopId) {
  177. util::fail('不能出给自己');
  178. }
  179. $itemInfo = [];
  180. foreach ($list as $item) {
  181. $stock = $item['stock'] ?? 0;
  182. $ratio = $item['ratio'] ?? 0;
  183. $bigNum = intval($stock);
  184. $small = bcsub((string)$stock, (string)$bigNum, 2);
  185. $smallNum = bcmul($small, (string)$ratio);
  186. $currentId = $item['id'];
  187. $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => (float)$smallNum];
  188. }
  189. if (empty($itemInfo)) {
  190. util::fail('没有库存');
  191. }
  192. $data = [
  193. 'remark' => '全部出库',
  194. 'itemInfo' => $itemInfo,
  195. 'inShopId' => $inShopId,
  196. 'inMainId' => $inMainId,
  197. 'outMainId' => $mainId,
  198. 'outShopId' => $shopId,
  199. 'sjId' => $sjId,
  200. 'action' => StockOutOrderClass::ACTION_CONFIRM,
  201. 'outTime' => date('Y-m-d H:i:s'),
  202. 'adminId' => $this->adminId,
  203. 'confirmIn' => $confirmIn,
  204. ];
  205. StockOutOrderClass::addOrder($data);
  206. $transaction->commit();
  207. util::complete();
  208. } catch (\Exception $exception) {
  209. $transaction->rollBack();
  210. util::fail('出库失败' . $exception->getMessage());
  211. }
  212. }
  213. //出库列表 lqh 2021.1.23
  214. public function actionList()
  215. {
  216. $get = Yii::$app->request->get();
  217. $orderStatus = isset($get['status']) ? $get['status'] : '';
  218. $where = [];
  219. $where['outShopId'] = $this->shopId;
  220. if (!empty($orderStatus)) {
  221. $where['status'] = $orderStatus;
  222. }
  223. $list = StockOutOrderClass::getOrderList($where);
  224. util::success($list);
  225. }
  226. //新增出库 lqh 2021.1.23
  227. public function actionCreateOrder()
  228. {
  229. $post = Yii::$app->request->post();
  230. $post['sjId'] = $this->sjId;
  231. $post['outShopId'] = $this->shopId;
  232. $post['outMainId'] = $this->mainId;
  233. $post['adminId'] = $this->adminId;
  234. $inShopId = $post['inShopId'] ?? 0;
  235. $now = date('Y-m-d H:i:s');
  236. $date = $post['date'] ?? $now;
  237. $post['outTime'] = $date;
  238. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  239. $post['action'] = $action;
  240. $post['inShopId'] = $inShopId;
  241. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  242. if ($check) {
  243. util::fail('还有待出库的订单');
  244. }
  245. $ghsItemInfo = $post['itemInfo'] ?? '';
  246. if (empty($ghsItemInfo)) {
  247. util::fail('请选择花材');
  248. }
  249. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  250. if (!is_array($ghsItemInfo)) {
  251. util::fail('花材格式不正确');
  252. }
  253. //避免重复提交
  254. $adminId = $this->adminId;
  255. util::checkRepeatCommit($adminId, 5);
  256. try {
  257. $order = util::runWithDbConcurrencyRetry(function () use ($post, $ghsItemInfo, $inShopId) {
  258. $connection = Yii::$app->db;
  259. $transaction = $connection->beginTransaction();
  260. try {
  261. //入库门店有效性判断
  262. if ($this->shopId == $inShopId) {
  263. util::fail('不能出库给当前门店');
  264. }
  265. $inShop = ShopClass::getById($inShopId);
  266. if (empty($inShop)) {
  267. util::fail('没有找到门店68');
  268. }
  269. ShopClass::valid($inShop, $this->sjId);
  270. $post['inMainId'] = $inShop['mainId'] ?? 0;
  271. $staff = $this->shopAdmin;
  272. $post['staffName'] = $staff->name ?? '';
  273. $post['staffId'] = $staff->id ?? 0;
  274. $post['adminId'] = $this->adminId;
  275. ProductClass::valid($ghsItemInfo, $this->mainId);
  276. //判断花材里的信息
  277. foreach ($ghsItemInfo as $v) {
  278. $bigNum = $v['bigNum'] ?? 0;
  279. $smallNum = $v['smallNum'] ?? 0;
  280. if ($bigNum <= 0 && $smallNum <= 0) {
  281. util::fail('花材数量不能为0');
  282. }
  283. }
  284. $post['itemInfo'] = $ghsItemInfo;
  285. $order = StockOutOrderClass::addOrder($post);
  286. $transaction->commit();
  287. return $order;
  288. } catch (\Exception $exception) {
  289. if ($transaction->isActive) {
  290. $transaction->rollBack();
  291. }
  292. throw $exception;
  293. }
  294. });
  295. util::success($order);
  296. } catch (\Exception $exception) {
  297. if (util::isDbConcurrencyException($exception)) {
  298. util::fail('系统繁忙中,请稍后再试');
  299. } else {
  300. util::fail('出库失败' . $exception->getMessage());
  301. }
  302. }
  303. }
  304. //出库详情 lqh 2021.1.23
  305. public function actionDetail()
  306. {
  307. $orderSn = Yii::$app->request->get('orderSn', '');
  308. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  309. util::success($orderData);
  310. }
  311. //确认出库 lqh 2021.1.23
  312. public function actionConfirmOrder()
  313. {
  314. $get = Yii::$app->request->get();
  315. $orderSn = $get['orderSn'];
  316. try {
  317. util::runWithDbConcurrencyRetry(function () use ($orderSn) {
  318. $connection = Yii::$app->db;
  319. $transaction = $connection->beginTransaction();
  320. try {
  321. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  322. $transaction->commit();
  323. } catch (\Exception $exception) {
  324. if ($transaction->isActive) {
  325. $transaction->rollBack();
  326. }
  327. throw $exception;
  328. }
  329. });
  330. util::complete("出库成功");
  331. } catch (\Exception $exception) {
  332. if (util::isDbConcurrencyException($exception)) {
  333. util::fail('系统繁忙中,请稍后再试');
  334. } else {
  335. util::fail($exception->getMessage());
  336. }
  337. }
  338. }
  339. //取消出库 lqh 2021.1.23
  340. public function actionCancelOrder()
  341. {
  342. $get = Yii::$app->request->get();
  343. $orderSn = $get['orderSn'];
  344. try {
  345. util::runWithDbConcurrencyRetry(function () use ($orderSn) {
  346. $connection = Yii::$app->db;
  347. $transaction = $connection->beginTransaction();
  348. try {
  349. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  350. $transaction->commit();
  351. } catch (\Exception $exception) {
  352. if ($transaction->isActive) {
  353. $transaction->rollBack();
  354. }
  355. throw $exception;
  356. }
  357. });
  358. util::complete("取消成功");
  359. } catch (\Exception $exception) {
  360. if (util::isDbConcurrencyException($exception)) {
  361. util::fail('系统繁忙中,请稍后再试');
  362. } else {
  363. util::fail($exception->getMessage());
  364. }
  365. }
  366. }
  367. //打印出库单 ssh 20231026
  368. public function actionPrint()
  369. {
  370. $get = Yii::$app->request->get();
  371. $id = $get['id'];
  372. $out = StockOutOrderClass::getById($id, true);
  373. if (empty($out)) {
  374. util::fail('没有找到出库记录');
  375. }
  376. if ($out->outMainId != $this->mainId) {
  377. util::fail('不是你的出库单');
  378. }
  379. StockOutOrderClass::printOrder($out, $this->shop);
  380. util::complete();
  381. }
  382. }