StockOutController.php 11 KB

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