StockOutController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. $post = Yii::$app->request->post();
  17. $orderSn = $post['orderSn'] ?? '';
  18. $in = StockInOrderClass::getByCondition(['orderSn' => $orderSn], true);
  19. if (empty($in)) {
  20. util::fail('没有找到入库单');
  21. }
  22. if ($in->inMainId != $this->mainId) {
  23. util::fail('不是你的入库单');
  24. }
  25. $inShopId = $post['inShopId'] ?? 0;
  26. if ($inShopId == $this->shopId) {
  27. util::fail('出库和入库门店一样');
  28. }
  29. if (empty($inShopId)) {
  30. //如果没有传门店,表示要回调原店,注意!!!!!!!!!!!!!!!!!
  31. $inShopId = $in->outShopId;
  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. $changeData[$productId] = ['actPutNum' => $actPutNum];
  46. }
  47. }
  48. }
  49. $inItemList = StockInOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  50. if (empty($inItemList)) {
  51. util::fail('没有找到入库单的花材');
  52. }
  53. $itemInfo = [];
  54. foreach ($inItemList as $key => $info) {
  55. $productId = $info['productId'] ?? 0;
  56. $itemNum = $info['itemNum'] ? floatval($info['itemNum']) : 0;
  57. if (!empty($changeData)) {
  58. if (isset($changeData[$productId])) {
  59. $changeResult = $changeData[$productId];
  60. $itemNum = $changeResult['actPutNum'];
  61. }
  62. }
  63. $itemInfo[] = ['productId' => $productId, 'bigNum' => $itemNum, 'smallNum' => 0];
  64. }
  65. $arr = [
  66. 'confirmIn' => 1,
  67. 'date' => '',
  68. 'outTime' => '',
  69. 'remark' => '一键再次出库',
  70. 'inShopId' => $inShopId,
  71. 'inMainId' => $inShop->mainId,
  72. 'itemInfo' => $itemInfo,
  73. 'action' => 'confirm',
  74. 'sjId' => $this->sjId,
  75. 'outShopId' => $this->shopId,
  76. 'outMainId' => $this->mainId,
  77. 'adminId' => $this->adminId,
  78. ];
  79. $connection = Yii::$app->db;
  80. $transaction = $connection->beginTransaction();
  81. try {
  82. StockOutOrderClass::addOrder($arr);
  83. //回调次数+1
  84. $in->callbackTime += 1;
  85. $in->save();
  86. $transaction->commit();
  87. util::complete('操作成功');
  88. } catch (\Exception $exception) {
  89. $transaction->rollBack();
  90. util::fail('操作失败');
  91. }
  92. }
  93. //结清账单 ssh 20221113
  94. public function actionClear()
  95. {
  96. $shopAdmin = $this->shopAdmin;
  97. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  98. util::fail('超管才能修改');
  99. }
  100. $get = Yii::$app->request->get();
  101. $orderSn = $get['orderSn'] ?? '';
  102. $out = StockOutOrderClass::getByCondition(['orderSn' => $orderSn], true);
  103. if (empty($out)) {
  104. util::fail('没有找到出库单');
  105. }
  106. if ($out->outMainId != $this->mainId) {
  107. util::fail('没有权限');
  108. }
  109. if ($out->status != 2) {
  110. util::fail('已出库才能结账单');
  111. }
  112. if ($out->debt == 0) {
  113. util::fail('已经结过了');
  114. }
  115. $inOrderSn = $out->inOrderSn ?? '';
  116. $in = StockInOrderClass::getByCondition(['orderSn' => $inOrderSn], true);
  117. if (empty($in)) {
  118. util::fail('没有找到入库单');
  119. }
  120. if ($in->status != 2) {
  121. util::fail('已入库才能结账单');
  122. }
  123. if ($in->debt == 0) {
  124. util::fail('已经结过了');
  125. }
  126. $out->debt = 0;
  127. $out->save();
  128. $in->debt = 0;
  129. $in->save();
  130. util::complete();
  131. }
  132. //全部出库 ssh 20220906
  133. public function actionAllOut()
  134. {
  135. $get = Yii::$app->request->get();
  136. $inShopId = $get['shopId'] ?? 0;
  137. $confirmIn = $get['confirmIn'] ?? 0;
  138. $inShop = ShopClass::getById($inShopId, true);
  139. if (empty($inShop)) {
  140. util::fail('入库门店没有找到');
  141. }
  142. $inMainId = $inShop->mainId ?? 0;
  143. $shop = $this->shop;
  144. if ($shop->default == 1) {
  145. util::fail('默认门店不能全部出库');
  146. }
  147. $connection = Yii::$app->db;
  148. $transaction = $connection->beginTransaction();
  149. try {
  150. $shopId = $this->shopId;
  151. $mainId = $this->mainId;
  152. $sjId = $shop->sjId ?? 0;
  153. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  154. if (empty($list)) {
  155. util::fail('没有花材');
  156. }
  157. if ($inShopId == $shopId) {
  158. util::fail('不能出给自己');
  159. }
  160. $itemInfo = [];
  161. foreach ($list as $item) {
  162. if ($item->delStatus == 1) {
  163. continue;
  164. }
  165. $stock = $item->stock ?? 0;
  166. $ratio = $item->ratio ?? 0;
  167. if ($stock <= 0) {
  168. continue;
  169. }
  170. $bigNum = intval($stock);
  171. $small = bcsub($stock, $bigNum, 2);
  172. $smallNum = bcmul($small, $ratio);
  173. $currentId = $item->id;
  174. $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  175. }
  176. if (empty($itemInfo)) {
  177. util::fail('没有库存');
  178. }
  179. $data = [
  180. 'remark' => '全部出库',
  181. 'itemInfo' => $itemInfo,
  182. 'inShopId' => $inShopId,
  183. 'inMainId' => $inMainId,
  184. 'outMainId' => $mainId,
  185. 'outShopId' => $shopId,
  186. 'sjId' => $sjId,
  187. 'action' => StockOutOrderClass::ACTION_CONFIRM,
  188. 'outTime' => date('Y-m-d H:i:s'),
  189. 'adminId' => $this->adminId,
  190. 'confirmIn' => $confirmIn,
  191. ];
  192. StockOutOrderClass::addOrder($data);
  193. $transaction->commit();
  194. util::complete();
  195. } catch (\Exception $exception) {
  196. $transaction->rollBack();
  197. util::fail('出库失败');
  198. }
  199. }
  200. //出库列表 lqh 2021.1.23
  201. public function actionList()
  202. {
  203. $get = Yii::$app->request->get();
  204. $orderStatus = isset($get['status']) ? $get['status'] : '';
  205. $where = [];
  206. $where['outShopId'] = $this->shopId;
  207. if (!empty($orderStatus)) {
  208. $where['status'] = $orderStatus;
  209. }
  210. $list = StockOutOrderClass::getOrderList($where);
  211. util::success($list);
  212. }
  213. //新增出库 lqh 2021.1.23
  214. public function actionCreateOrder()
  215. {
  216. $post = Yii::$app->request->post();
  217. $post['sjId'] = $this->sjId;
  218. $post['outShopId'] = $this->shopId;
  219. $post['outMainId'] = $this->mainId;
  220. $post['adminId'] = $this->adminId;
  221. $inShopId = $post['inShopId'] ?? 0;
  222. $now = date('Y-m-d H:i:s');
  223. $date = $post['date'] ?? $now;
  224. $post['outTime'] = $date;
  225. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  226. $post['action'] = $action;
  227. $post['inShopId'] = $inShopId;
  228. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  229. if ($check) {
  230. util::fail('还有待出库的订单');
  231. }
  232. $ghsItemInfo = $post['itemInfo'] ?? '';
  233. if (empty($ghsItemInfo)) {
  234. util::fail('请选择花材');
  235. }
  236. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  237. if (!is_array($ghsItemInfo)) {
  238. util::fail('花材格式不正确');
  239. }
  240. $connection = Yii::$app->db;
  241. $transaction = $connection->beginTransaction();
  242. try {
  243. //入库门店有效性判断
  244. if ($this->shopId == $inShopId) {
  245. util::fail('不能出库给当前门店');
  246. }
  247. $inShop = ShopClass::getById($inShopId);
  248. if (empty($inShop)) {
  249. util::fail('没有找到门店68');
  250. }
  251. ShopClass::valid($inShop, $this->sjId);
  252. $post['inMainId'] = $inShop['mainId'] ?? 0;
  253. ProductClass::valid($ghsItemInfo, $this->mainId);
  254. //判断花材里的信息
  255. foreach ($ghsItemInfo as $v) {
  256. $bigNum = $v['bigNum'] ?? 0;
  257. $smallNum = $v['smallNum'] ?? 0;
  258. $name = $v['name'] ?? '';
  259. if ($smallNum > 0) {
  260. util::fail($name . '不能调拨小单位');
  261. }
  262. if ($bigNum <= 0) {
  263. util::fail('花材数量不能为0');
  264. }
  265. }
  266. $post['itemInfo'] = $ghsItemInfo;
  267. $order = StockOutOrderClass::addOrder($post);
  268. $transaction->commit();
  269. util::success($order);
  270. } catch (\Exception $exception) {
  271. $transaction->rollBack();
  272. util::fail('出库失败');
  273. }
  274. }
  275. //出库详情 lqh 2021.1.23
  276. public function actionDetail()
  277. {
  278. $orderSn = Yii::$app->request->get('orderSn', '');
  279. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  280. util::success($orderData);
  281. }
  282. //确认出库 lqh 2021.1.23
  283. public function actionConfirmOrder()
  284. {
  285. $get = Yii::$app->request->get();
  286. $orderSn = $get['orderSn'];
  287. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  288. util::complete("出库成功");
  289. }
  290. //取消出库 lqh 2021.1.23
  291. public function actionCancelOrder()
  292. {
  293. $get = Yii::$app->request->get();
  294. $orderSn = $get['orderSn'];
  295. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  296. util::complete("取消成功");
  297. }
  298. //打印出库单 ssh 20231026
  299. public function actionPrint()
  300. {
  301. $get = Yii::$app->request->get();
  302. $id = $get['id'];
  303. $out = StockOutOrderClass::getById($id, true);
  304. if (empty($out)) {
  305. util::fail('没有找到出库记录');
  306. }
  307. if ($out->outMainId != $this->mainId) {
  308. util::fail('不是你的出库单');
  309. }
  310. StockOutOrderClass::printOrder($out, $this->shop);
  311. util::complete();
  312. }
  313. }