StockOutController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\merchant\classes\ShopClass;
  4. use bizGhs\order\classes\StockInOrderClass;
  5. use bizGhs\order\classes\StockOutOrderClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use common\components\util;
  8. use Yii;
  9. //出库 lqh 2021.1.23
  10. class StockOutController extends BaseController
  11. {
  12. //结清账单 ssh 20221113
  13. public function actionClear()
  14. {
  15. $shopAdmin = $this->shopAdmin;
  16. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  17. util::fail('超管才能修改');
  18. }
  19. $get = Yii::$app->request->get();
  20. $orderSn = $get['orderSn'] ?? '';
  21. $out = StockOutOrderClass::getByCondition(['orderSn' => $orderSn], true);
  22. if (empty($out)) {
  23. util::fail('没有找到出库单');
  24. }
  25. if ($out->outMainId != $this->mainId) {
  26. util::fail('没有权限');
  27. }
  28. if ($out->status != 2) {
  29. util::fail('已出库才能结账单');
  30. }
  31. if ($out->debt == 0) {
  32. util::fail('已经结过了');
  33. }
  34. $inOrderSn = $out->inOrderSn ?? '';
  35. $in = StockInOrderClass::getByCondition(['orderSn' => $inOrderSn], true);
  36. if (empty($in)) {
  37. util::fail('没有找到入库单');
  38. }
  39. if ($in->status != 2) {
  40. util::fail('已入库才能结账单');
  41. }
  42. if ($in->debt == 0) {
  43. util::fail('已经结过了');
  44. }
  45. $out->debt = 0;
  46. $out->save();
  47. $in->debt = 0;
  48. $in->save();
  49. util::complete();
  50. }
  51. //全部出库 ssh 20220906
  52. public function actionAllOut()
  53. {
  54. $get = Yii::$app->request->get();
  55. $inShopId = $get['shopId'] ?? 0;
  56. $confirmIn = $get['confirmIn'] ?? 0;
  57. $inShop = ShopClass::getById($inShopId, true);
  58. if (empty($inShop)) {
  59. util::fail('入库门店没有找到');
  60. }
  61. $inMainId = $inShop->mainId ?? 0;
  62. $shop = $this->shop;
  63. if ($shop->default == 1) {
  64. util::fail('默认门店不能全部出库');
  65. }
  66. $connection = Yii::$app->db;
  67. $transaction = $connection->beginTransaction();
  68. try {
  69. $shopId = $this->shopId;
  70. $mainId = $this->mainId;
  71. $sjId = $shop->sjId ?? 0;
  72. $list = ProductClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  73. if (empty($list)) {
  74. util::fail('没有花材');
  75. }
  76. if ($inShopId == $shopId) {
  77. util::fail('不能出给自己');
  78. }
  79. $itemInfo = [];
  80. foreach ($list as $item) {
  81. if ($item->delStatus == 1) {
  82. continue;
  83. }
  84. $stock = $item->stock ?? 0;
  85. $ratio = $item->ratio ?? 0;
  86. if ($stock <= 0) {
  87. continue;
  88. }
  89. $bigNum = intval($stock);
  90. $small = bcsub($stock, $bigNum, 2);
  91. $smallNum = bcmul($small, $ratio);
  92. $currentId = $item->id;
  93. $itemInfo[] = ['productId' => $currentId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  94. }
  95. if (empty($itemInfo)) {
  96. util::fail('没有库存');
  97. }
  98. $data = [
  99. 'remark' => '全部出库',
  100. 'itemInfo' => $itemInfo,
  101. 'inShopId' => $inShopId,
  102. 'inMainId' => $inMainId,
  103. 'outMainId' => $mainId,
  104. 'outShopId' => $shopId,
  105. 'sjId' => $sjId,
  106. 'action' => StockOutOrderClass::ACTION_CONFIRM,
  107. 'outTime' => date('Y-m-d H:i:s'),
  108. 'adminId' => $this->adminId,
  109. 'confirmIn' => $confirmIn,
  110. ];
  111. StockOutOrderClass::addOrder($data);
  112. $transaction->commit();
  113. util::complete();
  114. } catch (\Exception $exception) {
  115. $transaction->rollBack();
  116. util::fail('出库失败');
  117. }
  118. }
  119. //出库列表 lqh 2021.1.23
  120. public function actionList()
  121. {
  122. $get = Yii::$app->request->get();
  123. $orderStatus = isset($get['status']) ? $get['status'] : '';
  124. $where = [];
  125. $where['outShopId'] = $this->shopId;
  126. if (!empty($orderStatus)) {
  127. $where['status'] = $orderStatus;
  128. }
  129. $list = StockOutOrderClass::getOrderList($where);
  130. util::success($list);
  131. }
  132. //新增出库 lqh 2021.1.23
  133. public function actionCreateOrder()
  134. {
  135. $post = Yii::$app->request->post();
  136. $post['sjId'] = $this->sjId;
  137. $post['outShopId'] = $this->shopId;
  138. $post['outMainId'] = $this->mainId;
  139. $post['adminId'] = $this->adminId;
  140. $inShopId = $post['inShopId'] ?? 0;
  141. $now = date('Y-m-d H:i:s');
  142. $date = $post['date'] ?? $now;
  143. $post['outTime'] = $date;
  144. $action = $post['action'] ?? 'wait'; //confirm(直接出库) wait(待出库中)
  145. $post['action'] = $action;
  146. $post['inShopId'] = $inShopId;
  147. $check = StockOutOrderClass::getByCondition(['outShopId' => $this->shopId, 'inShopId' => $inShopId, 'status' => StockOutOrderClass::STATUS_WAIT]);
  148. if ($check) {
  149. util::fail('还有待出库的订单');
  150. }
  151. $ghsItemInfo = $post['itemInfo'] ?? '';
  152. if (empty($ghsItemInfo)) {
  153. util::fail('请选择花材');
  154. }
  155. $ghsItemInfo = json_decode($ghsItemInfo, true); // [{productId:0,bigNum:1,smallNum:0}]
  156. if (!is_array($ghsItemInfo)) {
  157. util::fail('花材格式不正确');
  158. }
  159. $connection = Yii::$app->db;
  160. $transaction = $connection->beginTransaction();
  161. try {
  162. //入库门店有效性判断
  163. if ($this->shopId == $inShopId) {
  164. util::fail('不能出库给当前门店');
  165. }
  166. $inShop = ShopClass::getById($inShopId);
  167. if (empty($inShop)) {
  168. util::fail('没有找到门店');
  169. }
  170. ShopClass::valid($inShop, $this->sjId);
  171. $post['inMainId'] = $inShop['mainId'] ?? 0;
  172. ProductClass::valid($ghsItemInfo, $this->mainId);
  173. //判断花材里的信息
  174. foreach ($ghsItemInfo as $v) {
  175. $bigNum = $v['bigNum'] ?? 0;
  176. $smallNum = $v['smallNum'] ?? 0;
  177. if ($bigNum <= 0 && $smallNum <= 0) {
  178. util::fail('花材数量不能为0');
  179. }
  180. }
  181. $post['itemInfo'] = $ghsItemInfo;
  182. $order = StockOutOrderClass::addOrder($post);
  183. $transaction->commit();
  184. util::success($order);
  185. } catch (\Exception $exception) {
  186. $transaction->rollBack();
  187. util::fail('出库失败');
  188. }
  189. }
  190. //出库详情 lqh 2021.1.23
  191. public function actionDetail()
  192. {
  193. $orderSn = Yii::$app->request->get('orderSn', '');
  194. $orderData = StockOutOrderClass::getOrderDetail($orderSn, $this->shopId);
  195. util::success($orderData);
  196. }
  197. //确认出库 lqh 2021.1.23
  198. public function actionConfirmOrder()
  199. {
  200. $get = Yii::$app->request->get();
  201. $orderSn = $get['orderSn'];
  202. StockOutOrderClass::confirmOrder($orderSn, $this->shopId, $this->adminId);
  203. util::complete("出库成功");
  204. }
  205. //取消出库 lqh 2021.1.23
  206. public function actionCancelOrder()
  207. {
  208. $get = Yii::$app->request->get();
  209. $orderSn = $get['orderSn'];
  210. StockOutOrderClass::cancelOrder($orderSn, $this->shopId, $this->adminId);
  211. util::complete("取消成功");
  212. }
  213. }