StockOutController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. //全部出库 ssh 20220906
  138. public function actionAllOut()
  139. {
  140. $get = Yii::$app->request->get();
  141. $inShopId = $get['shopId'] ?? 0;
  142. $confirmIn = $get['confirmIn'] ?? 0;
  143. $inShop = ShopClass::getById($inShopId, true);
  144. if (empty($inShop)) {
  145. util::fail('入库门店没有找到');
  146. }
  147. $inMainId = $inShop->mainId ?? 0;
  148. $shop = $this->shop;
  149. if ($shop->default == 1) {
  150. util::fail('默认门店不能全部出库');
  151. }
  152. $connection = Yii::$app->db;
  153. $transaction = $connection->beginTransaction();
  154. try {
  155. $shopId = $this->shopId;
  156. $mainId = $this->mainId;
  157. $sjId = $shop->sjId ?? 0;
  158. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  159. if (empty($list)) {
  160. util::fail('没有花材');
  161. }
  162. if ($inShopId == $shopId) {
  163. util::fail('不能出给自己');
  164. }
  165. $itemInfo = [];
  166. foreach ($list as $item) {
  167. if ($item->delStatus == 1) {
  168. continue;
  169. }
  170. $stock = $item->stock ?? 0;
  171. $ratio = $item->ratio ?? 0;
  172. if ($stock <= 0) {
  173. continue;
  174. }
  175. $bigNum = intval($stock);
  176. $small = bcsub($stock, $bigNum, 2);
  177. $smallNum = bcmul($small, $ratio);
  178. $currentId = $item->id;
  179. $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  180. }
  181. if (empty($itemInfo)) {
  182. util::fail('没有库存');
  183. }
  184. $data = [
  185. 'remark' => '全部出库',
  186. 'itemInfo' => $itemInfo,
  187. 'inShopId' => $inShopId,
  188. 'inMainId' => $inMainId,
  189. 'outMainId' => $mainId,
  190. 'outShopId' => $shopId,
  191. 'sjId' => $sjId,
  192. 'action' => StockOutOrderClass::ACTION_CONFIRM,
  193. 'outTime' => date('Y-m-d H:i:s'),
  194. 'adminId' => $this->adminId,
  195. 'confirmIn' => $confirmIn,
  196. ];
  197. StockOutOrderClass::addOrder($data);
  198. $transaction->commit();
  199. util::complete();
  200. } catch (\Exception $exception) {
  201. $transaction->rollBack();
  202. util::fail('出库失败');
  203. }
  204. }
  205. //出库列表 lqh 2021.1.23
  206. public function actionList()
  207. {
  208. $get = Yii::$app->request->get();
  209. $orderStatus = isset($get['status']) ? $get['status'] : '';
  210. $where = [];
  211. $where['outShopId'] = $this->shopId;
  212. if (!empty($orderStatus)) {
  213. $where['status'] = $orderStatus;
  214. }
  215. $list = StockOutOrderClass::getOrderList($where);
  216. util::success($list);
  217. }
  218. //新增出库 lqh 2021.1.23
  219. public function actionCreateOrder()
  220. {
  221. $post = Yii::$app->request->post();
  222. $post['sjId'] = $this->sjId;
  223. $post['outShopId'] = $this->shopId;
  224. $post['outMainId'] = $this->mainId;
  225. $post['adminId'] = $this->adminId;
  226. $inShopId = $post['inShopId'] ?? 0;
  227. $now = date('Y-m-d H:i:s');
  228. $date = $post['date'] ?? $now;
  229. $post['outTime'] = $date;
  230. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  231. $post['action'] = $action;
  232. $post['inShopId'] = $inShopId;
  233. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  234. if ($check) {
  235. util::fail('还有待出库的订单');
  236. }
  237. $ghsItemInfo = $post['itemInfo'] ?? '';
  238. if (empty($ghsItemInfo)) {
  239. util::fail('请选择花材');
  240. }
  241. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  242. if (!is_array($ghsItemInfo)) {
  243. util::fail('花材格式不正确');
  244. }
  245. $connection = Yii::$app->db;
  246. $transaction = $connection->beginTransaction();
  247. try {
  248. //入库门店有效性判断
  249. if ($this->shopId == $inShopId) {
  250. util::fail('不能出库给当前门店');
  251. }
  252. $inShop = ShopClass::getById($inShopId);
  253. if (empty($inShop)) {
  254. util::fail('没有找到门店68');
  255. }
  256. ShopClass::valid($inShop, $this->sjId);
  257. $post['inMainId'] = $inShop['mainId'] ?? 0;
  258. ProductClass::valid($ghsItemInfo, $this->mainId);
  259. //判断花材里的信息
  260. foreach ($ghsItemInfo as $v) {
  261. $bigNum = $v['bigNum'] ?? 0;
  262. $smallNum = $v['smallNum'] ?? 0;
  263. $name = $v['name'] ?? '';
  264. if ($bigNum <= 0 && $smallNum <= 0) {
  265. util::fail('花材数量不能为0');
  266. }
  267. }
  268. $post['itemInfo'] = $ghsItemInfo;
  269. $order = StockOutOrderClass::addOrder($post);
  270. $transaction->commit();
  271. util::success($order);
  272. } catch (\Exception $exception) {
  273. $transaction->rollBack();
  274. util::fail('出库失败');
  275. }
  276. }
  277. //出库详情 lqh 2021.1.23
  278. public function actionDetail()
  279. {
  280. $orderSn = Yii::$app->request->get('orderSn', '');
  281. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  282. util::success($orderData);
  283. }
  284. //确认出库 lqh 2021.1.23
  285. public function actionConfirmOrder()
  286. {
  287. $get = Yii::$app->request->get();
  288. $orderSn = $get['orderSn'];
  289. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  290. util::complete("出库成功");
  291. }
  292. //取消出库 lqh 2021.1.23
  293. public function actionCancelOrder()
  294. {
  295. $get = Yii::$app->request->get();
  296. $orderSn = $get['orderSn'];
  297. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  298. util::complete("取消成功");
  299. }
  300. //打印出库单 ssh 20231026
  301. public function actionPrint()
  302. {
  303. $get = Yii::$app->request->get();
  304. $id = $get['id'];
  305. $out = StockOutOrderClass::getById($id, true);
  306. if (empty($out)) {
  307. util::fail('没有找到出库记录');
  308. }
  309. if ($out->outMainId != $this->mainId) {
  310. util::fail('不是你的出库单');
  311. }
  312. StockOutOrderClass::printOrder($out, $this->shop);
  313. util::complete();
  314. }
  315. }