StockOutController.php 11 KB

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