CheckOrderController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\book\classes\BookItemCustomClass;
  4. use bizGhs\order\classes\CheckOrderClass;
  5. use bizGhs\product\classes\ProductClass;
  6. use Yii;
  7. use common\components\util;
  8. class CheckOrderController extends BaseController
  9. {
  10. //借库存 ssh 20240720
  11. public function actionLoan()
  12. {
  13. $get = Yii::$app->request->get();
  14. $inId = $get['inId'] ?? 0;
  15. $outId = $get['outId'] ?? 0;
  16. $bookItemCustomId = $get['bookItemCustomId']??0;
  17. $num = $get['num'] ?? 0;
  18. if ($num <= 0) {
  19. util::fail('借数量不足');
  20. }
  21. $shop = $this->shop;
  22. $staff = $this->shopAdmin;
  23. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId,true);
  24. if(empty($bookItemCustomRes)){
  25. util::fail('没有预订相关的信息呢');
  26. }
  27. if($bookItemCustomRes->mainId!=$this->mainId){
  28. util::fail('不是你的预订信息');
  29. }
  30. $connection = Yii::$app->db;
  31. $transaction = $connection->beginTransaction();
  32. try {
  33. CheckOrderClass::loanStock($inId, $outId, $num, $shop, $staff,$bookItemCustomRes);
  34. $transaction->commit();
  35. util::complete('操作成功');
  36. } catch (\Exception $e) {
  37. Yii::info("操作失败原因:" . $e->getMessage());
  38. $transaction->rollBack();
  39. util::fail('操作失败');
  40. }
  41. }
  42. //订单列表 ssh 2021.1.14
  43. public function actionList()
  44. {
  45. $get = Yii::$app->request->get();
  46. $orderStatus = isset($get['status']) ? $get['status'] : '';
  47. $where = [];
  48. $where['shopId'] = $this->shopId;
  49. if (!empty($orderStatus)) {
  50. $where['status'] = $orderStatus;
  51. }
  52. $list = CheckOrderClass::getOrderList($where);
  53. util::success($list);
  54. }
  55. //订单详情 2021.1.22 lqh
  56. public function actionDetail()
  57. {
  58. $orderSn = Yii::$app->request->get('orderSn', '');
  59. $orderData = CheckOrderClass::getOrderDetail($orderSn, $this->shopId);
  60. util::success($orderData);
  61. }
  62. //分配库存 ssh 20230720
  63. public function actionSendStock()
  64. {
  65. $shopAdmin = $this->shopAdmin;
  66. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  67. util::fail('超管才能盘点');
  68. }
  69. $post = Yii::$app->request->post();
  70. $motherId = $post['motherId'] ?? 0;
  71. $mother = ProductClass::getById($motherId, true);
  72. if (empty($mother) || $mother->mainId != $this->mainId) {
  73. util::fail('无法操作');
  74. }
  75. $motherName = $mother->name ?? '';
  76. $ghsItemInfo = $post['itemInfo'] ?? '';
  77. if (empty($ghsItemInfo)) {
  78. util::fail('请选择花材');
  79. }
  80. $itemList = json_decode($ghsItemInfo, true);
  81. if (empty($itemList)) {
  82. util::fail('没有花材');
  83. }
  84. $totalNum = 0;
  85. foreach ($itemList as $key => $item) {
  86. $currentNum = $item['bigNum'] ?? 0;
  87. $totalNum = bcadd($totalNum, $currentNum, 2);
  88. }
  89. if ($totalNum > $mother->stock) {
  90. util::fail($motherName . "不足{$totalNum}扎");
  91. }
  92. $motherBigNum = bcsub($mother->stock, $totalNum, 2);
  93. $motherBigNum = floatval($motherBigNum);
  94. $ids = array_column($itemList, 'productId');
  95. $infoList = ProductClass::getByIds($ids, null, 'id');
  96. if (!empty($infoList)) {
  97. foreach ($itemList as $itemKey => $itemVal) {
  98. $productId = $itemVal['productId'] ?? 0;
  99. $bigNum = $itemVal['bigNum'] ?? 0;
  100. $addBigNum = $infoList[$productId]['stock'] ?? 0;
  101. $newBigNum = bcadd($bigNum, $addBigNum, 2);
  102. $itemList[$itemKey]['bigNum'] = $newBigNum;
  103. }
  104. }
  105. $itemList[] = ['productId' => $motherId, 'bigNum' => $motherBigNum, 'smallNum' => 0];
  106. $newJson = json_encode($itemList);
  107. $post['itemInfo'] = $newJson;
  108. $post['sjId'] = $this->sjId;
  109. $post['shopId'] = $this->shopId;
  110. $post['adminId'] = $this->adminId;
  111. $post['mainId'] = $this->mainId;
  112. $order = $this->saveOrder($post, false, false);
  113. util::success($order);
  114. }
  115. //盘点确认 lqh 2021.1.22
  116. public function actionCreateOrder()
  117. {
  118. $shopAdmin = $this->shopAdmin;
  119. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  120. util::fail('超管才能盘点');
  121. }
  122. $post = Yii::$app->request->post();
  123. $post['sjId'] = $this->sjId;
  124. $post['shopId'] = $this->shopId;
  125. $post['adminId'] = $this->adminId;
  126. $post['mainId'] = $this->mainId;
  127. $staff = $this->shopAdmin;
  128. $post['staffName'] = $staff->name ?? '';
  129. $connection = Yii::$app->db;
  130. $transaction = $connection->beginTransaction();
  131. try {
  132. $order = $this->saveOrder($post, false, false);
  133. $transaction->commit();
  134. util::success($order);
  135. } catch (\Exception $e) {
  136. Yii::info("操作失败:" . $e->getMessage());
  137. $transaction->rollBack();
  138. util::fail('操作失败');
  139. }
  140. }
  141. //盘点存草稿 lqh 2021.1.22
  142. public function actionCreateDraft()
  143. {
  144. $shopAdmin = $this->shopAdmin;
  145. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  146. util::fail('超管才能操作');
  147. }
  148. $post = Yii::$app->request->post();
  149. $post['sjId'] = $this->sjId;
  150. $post['shopId'] = $this->shopId;
  151. $post['adminId'] = $this->adminId;
  152. //ssh 20210827
  153. util::fail('草稿功能取消');
  154. $order = $this->saveOrder($post, true, false);
  155. util::success($order);
  156. }
  157. //编辑草稿 ,继续保存草稿
  158. public function actionUpdateDraft()
  159. {
  160. $shopAdmin = $this->shopAdmin;
  161. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  162. util::fail('超管才能操作');
  163. }
  164. $post = Yii::$app->request->post();
  165. $post['sjId'] = $this->sjId;
  166. $post['shopId'] = $this->shopId;
  167. $post['adminId'] = $this->adminId;
  168. $staff = $this->shopAdmin;
  169. $post['staffName'] = $staff->name ?? '';
  170. $orderSn = $post['orderSn'] ?? '';
  171. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  172. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  173. util::fail("订单不存在");
  174. }
  175. $order = $this->saveOrder($post, true, true);
  176. util::success($order);
  177. }
  178. //编辑草稿, 确认
  179. public function actionUpdateOrder()
  180. {
  181. $shopAdmin = $this->shopAdmin;
  182. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  183. util::fail('超管才能操作');
  184. }
  185. $post = Yii::$app->request->post();
  186. $post['sjId'] = $this->sjId;
  187. $post['shopId'] = $this->shopId;
  188. $post['mainId'] = $this->mainId;
  189. $post['adminId'] = $this->adminId;
  190. $staff = $this->shopAdmin;
  191. $post['staffName'] = $staff->name ?? '';
  192. $orderSn = $post['orderSn'] ?? '';
  193. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  194. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  195. util::fail("订单不存在");
  196. }
  197. $order = $this->saveOrder($post, false, true);
  198. util::success($order);
  199. }
  200. protected function saveOrder($post, $draft = false, $update = false)
  201. {
  202. $shopAdmin = $this->shopAdmin;
  203. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  204. util::fail('超管才能操作');
  205. }
  206. $ghsItemInfo = $post['itemInfo'] ?? '';
  207. if (empty($ghsItemInfo)) {
  208. util::fail('请选择花材');
  209. }
  210. //数据格式 [{productId:0,bigNum:1,smallNum:0}]
  211. $ghsItemInfo = json_decode($ghsItemInfo, true);
  212. if (!is_array($ghsItemInfo)) {
  213. util::fail('花材格式不正确');
  214. }
  215. //判断花材格式,是否属于当前门店
  216. ProductClass::valid($ghsItemInfo, $this->mainId);
  217. foreach ($ghsItemInfo as $key => $item) {
  218. //如果盘点数是99999则转为0
  219. if (isset($item['bigNum']) && $item['bigNum'] == 99999) {
  220. $ghsItemInfo[$key]['bigNum'] = 0;
  221. }
  222. }
  223. $post['itemInfo'] = $ghsItemInfo;
  224. if ($update === true) {
  225. $order = CheckOrderClass::opOrder($post, $draft, $update);
  226. } else {
  227. $order = CheckOrderClass::opOrder($post, $draft, $update);
  228. }
  229. return $order;
  230. }
  231. //清空库存 ssh 20231224
  232. public function actionClearStock()
  233. {
  234. ini_set('memory_limit', '2045M');
  235. set_time_limit(0);
  236. $staff = $this->shopAdmin;
  237. $could = $staff->mobile == '15280215347' ? true : false;
  238. $mainId = $this->mainId;
  239. if (getenv('YII_ENV') == 'production') {
  240. //源花汇小周可以清花材
  241. if ($mainId == 10536) {
  242. if (in_array($this->adminId, [11648])) {
  243. $could = true;
  244. }
  245. }
  246. } else {
  247. if ($mainId == 812) {
  248. if (in_array($this->adminId, [1091])) {
  249. $could = true;
  250. }
  251. }
  252. }
  253. if ($could == false) {
  254. util::fail('无法清空库存');
  255. }
  256. $mainId = $this->mainId ?? 0;
  257. $adminId = $this->adminId ?? 0;
  258. $shopId = $this->shopId ?? 0;
  259. $sjId = $this->sjId ?? 0;
  260. $list = ProductClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0, 'stock>' => 0,], null, '*', null, true);
  261. $productList = [];
  262. if (!empty($list)) {
  263. foreach ($list as $key => $info) {
  264. $id = $info->id ?? 0;
  265. if ($info->virtualStock == 0) {
  266. $productList[] = ['productId' => $id, 'bigNum' => 0, 'smallNum' => 0];
  267. }
  268. }
  269. }
  270. if (empty($productList)) {
  271. util::fail('没有花材需要清空');
  272. }
  273. $modifyData = [
  274. 'remark' => '清空库存',
  275. 'sjId' => $sjId,
  276. 'shopId' => $shopId,
  277. 'adminId' => $adminId,
  278. 'mainId' => $mainId,
  279. 'itemInfo' => $productList,
  280. ];
  281. $draft = false;
  282. $update = false;
  283. CheckOrderClass::opOrder($modifyData, $draft, $update);
  284. util::complete('清除成功');
  285. }
  286. }