StockOutController.php 10 KB

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