StockOutController.php 11 KB

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