StockOutController.php 10 KB

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