CheckOrderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. //预订中减少库存
  11. public function actionBookDelStock()
  12. {
  13. $staff = $this->shopAdmin;
  14. if (isset($staff->identity) && $staff->identity == 2) {
  15. util::fail('不能操作');
  16. }
  17. $get = Yii::$app->request->get();
  18. $productId = $get['itemId'] ?? 0;
  19. $bookItemCustomId = $get['bookItemCustomId'] ?? 0;
  20. $num = $get['num'] ?? 0;
  21. if ($num <= 0) {
  22. util::fail('请填写要减的数量');
  23. }
  24. $shop = $this->shop;
  25. $staff = $this->shopAdmin;
  26. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
  27. if (empty($bookItemCustomRes)) {
  28. util::fail('没有预订相关的信息呢');
  29. }
  30. if ($bookItemCustomRes->mainId != $this->mainId) {
  31. util::fail('不是你的预订信息');
  32. }
  33. $connection = Yii::$app->db;
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. CheckOrderClass::bookToDelStock($productId, $num, $shop, $staff);
  37. $transaction->commit();
  38. util::complete('操作成功');
  39. } catch (\Exception $e) {
  40. Yii::info("操作失败原因:" . $e->getMessage());
  41. $transaction->rollBack();
  42. util::fail('操作失败');
  43. }
  44. }
  45. //预订中增加库存
  46. public function actionBookAddStock()
  47. {
  48. $staff = $this->shopAdmin;
  49. if (isset($staff->identity) && $staff->identity == 2) {
  50. util::fail('不能操作');
  51. }
  52. $get = Yii::$app->request->get();
  53. $productId = $get['itemId'] ?? 0;
  54. $bookItemCustomId = $get['bookItemCustomId'] ?? 0;
  55. $num = $get['num'] ?? 0;
  56. if ($num <= 0) {
  57. util::fail('请填写要加的数量');
  58. }
  59. $shop = $this->shop;
  60. $staff = $this->shopAdmin;
  61. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
  62. if (empty($bookItemCustomRes)) {
  63. util::fail('没有预订相关的信息呢...');
  64. }
  65. if ($bookItemCustomRes->mainId != $this->mainId) {
  66. util::fail('不是你的预订信息..');
  67. }
  68. $needCgNum = $bookItemCustomRes->needCgNum ?? 0;
  69. if ($num > $needCgNum) {
  70. util::fail("最多只需要加" . $needCgNum . "份");
  71. }
  72. $connection = Yii::$app->db;
  73. $transaction = $connection->beginTransaction();
  74. try {
  75. CheckOrderClass::bookToAddStock($productId, $num, $shop, $staff, $bookItemCustomRes);
  76. $transaction->commit();
  77. util::complete('操作成功');
  78. } catch (\Exception $e) {
  79. Yii::info("操作失败原因:" . $e->getMessage());
  80. $transaction->rollBack();
  81. util::fail('操作失败');
  82. }
  83. }
  84. //借库存 ssh 20240720
  85. public function actionLoan()
  86. {
  87. $staff = $this->shopAdmin;
  88. if (isset($staff->identity) && $staff->identity == 2) {
  89. util::fail('不能操作');
  90. }
  91. $get = Yii::$app->request->get();
  92. $inId = $get['inId'] ?? 0;
  93. $outId = $get['outId'] ?? 0;
  94. $bookItemCustomId = $get['bookItemCustomId'] ?? 0;
  95. $num = $get['num'] ?? 0;
  96. if ($num <= 0) {
  97. util::fail('请填写要借的数量');
  98. }
  99. $shop = $this->shop;
  100. $staff = $this->shopAdmin;
  101. $bookItemCustomRes = BookItemCustomClass::getById($bookItemCustomId, true);
  102. if (empty($bookItemCustomRes)) {
  103. util::fail('没有预订相关的信息呢');
  104. }
  105. if ($bookItemCustomRes->mainId != $this->mainId) {
  106. util::fail('不是你的预订信息');
  107. }
  108. $needCgNum = $bookItemCustomRes->needCgNum ?? 0;
  109. if ($num > $needCgNum) {
  110. util::fail("最多只需要借" . $needCgNum . "份");
  111. }
  112. $connection = Yii::$app->db;
  113. $transaction = $connection->beginTransaction();
  114. try {
  115. CheckOrderClass::loanStock($inId, $outId, $num, $shop, $staff, $bookItemCustomRes);
  116. $transaction->commit();
  117. util::complete('操作成功');
  118. } catch (\Exception $e) {
  119. Yii::info("操作失败原因:" . $e->getMessage());
  120. $transaction->rollBack();
  121. util::fail('操作失败');
  122. }
  123. }
  124. //订单列表 ssh 2021.1.14
  125. public function actionList()
  126. {
  127. $get = Yii::$app->request->get();
  128. $orderStatus = isset($get['status']) ? $get['status'] : '';
  129. $where = [];
  130. $where['shopId'] = $this->shopId;
  131. if (!empty($orderStatus)) {
  132. $where['status'] = $orderStatus;
  133. }
  134. $list = CheckOrderClass::getOrderList($where);
  135. util::success($list);
  136. }
  137. //订单详情 2021.1.22 lqh
  138. public function actionDetail()
  139. {
  140. $orderSn = Yii::$app->request->get('orderSn', '');
  141. $orderData = CheckOrderClass::getOrderDetail($orderSn, $this->shopId);
  142. util::success($orderData);
  143. }
  144. //分配库存 ssh 20230720
  145. public function actionSendStock()
  146. {
  147. $shopAdmin = $this->shopAdmin;
  148. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  149. util::fail('超管才能盘点');
  150. }
  151. $post = Yii::$app->request->post();
  152. $motherId = $post['motherId'] ?? 0;
  153. $mother = ProductClass::getById($motherId, true);
  154. if (empty($mother) || $mother->mainId != $this->mainId) {
  155. util::fail('无法操作');
  156. }
  157. $motherName = $mother->name ?? '';
  158. $ghsItemInfo = $post['itemInfo'] ?? '';
  159. if (empty($ghsItemInfo)) {
  160. util::fail('请选择花材3');
  161. }
  162. $itemList = json_decode($ghsItemInfo, true);
  163. if (empty($itemList)) {
  164. util::fail('没有花材');
  165. }
  166. $totalNum = 0;
  167. foreach ($itemList as $key => $item) {
  168. $currentNum = $item['bigNum'] ?? 0;
  169. $totalNum = bcadd($totalNum, $currentNum, 2);
  170. }
  171. if ($totalNum > $mother->stock) {
  172. util::fail($motherName . "不足{$totalNum}扎");
  173. }
  174. $motherBigNum = bcsub($mother->stock, $totalNum, 2);
  175. $motherBigNum = floatval($motherBigNum);
  176. $ids = array_column($itemList, 'productId');
  177. $infoList = ProductClass::getByIds($ids, null, 'id');
  178. if (!empty($infoList)) {
  179. foreach ($itemList as $itemKey => $itemVal) {
  180. $productId = $itemVal['productId'] ?? 0;
  181. $bigNum = $itemVal['bigNum'] ?? 0;
  182. $addBigNum = $infoList[$productId]['stock'] ?? 0;
  183. $newBigNum = bcadd($bigNum, $addBigNum, 2);
  184. $itemList[$itemKey]['bigNum'] = $newBigNum;
  185. }
  186. }
  187. $itemList[] = ['productId' => $motherId, 'bigNum' => $motherBigNum, 'smallNum' => 0];
  188. $newJson = json_encode($itemList);
  189. $post['itemInfo'] = $newJson;
  190. $post['sjId'] = $this->sjId;
  191. $post['shopId'] = $this->shopId;
  192. $post['adminId'] = $this->adminId;
  193. $post['mainId'] = $this->mainId;
  194. $order = $this->saveOrder($post, false, false);
  195. util::success($order);
  196. }
  197. //盘点确认 lqh 2021.1.22
  198. public function actionCreateOrder()
  199. {
  200. $shopAdmin = $this->shopAdmin;
  201. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  202. util::fail('超管才能盘点');
  203. }
  204. if (getenv('YII_ENV') == 'production') {
  205. //花大苪只有三个人可以修改库存
  206. if ($this->mainId == 8164) {
  207. if (!in_array($this->adminId, [9273, 9303, 47576])) {
  208. util::fail('不能修改库存哦');
  209. }
  210. }
  211. } else {
  212. if ($this->mainId == 644) {
  213. if (!in_array($this->adminId, [91, 0])) {
  214. util::fail('不能修改库存哦!');
  215. }
  216. }
  217. }
  218. $post = Yii::$app->request->post();
  219. $post['sjId'] = $this->sjId;
  220. $post['shopId'] = $this->shopId;
  221. $post['adminId'] = $this->adminId;
  222. $post['mainId'] = $this->mainId;
  223. $staff = $this->shopAdmin;
  224. $post['staffName'] = $staff->name ?? '';
  225. $connection = Yii::$app->db;
  226. $transaction = $connection->beginTransaction();
  227. try {
  228. $order = $this->saveOrder($post, false, false);
  229. $transaction->commit();
  230. util::success($order);
  231. } catch (\Exception $e) {
  232. Yii::info("操作失败:" . $e->getMessage());
  233. $transaction->rollBack();
  234. util::fail('操作失败');
  235. }
  236. }
  237. //盘点存草稿 lqh 2021.1.22
  238. public function actionCreateDraft()
  239. {
  240. $shopAdmin = $this->shopAdmin;
  241. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  242. util::fail('超管才能操作');
  243. }
  244. $post = Yii::$app->request->post();
  245. $post['sjId'] = $this->sjId;
  246. $post['shopId'] = $this->shopId;
  247. $post['adminId'] = $this->adminId;
  248. //ssh 20210827
  249. util::fail('草稿功能取消');
  250. $order = $this->saveOrder($post, true, false);
  251. util::success($order);
  252. }
  253. //编辑草稿 ,继续保存草稿
  254. public function actionUpdateDraft()
  255. {
  256. $shopAdmin = $this->shopAdmin;
  257. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  258. util::fail('超管才能操作');
  259. }
  260. $post = Yii::$app->request->post();
  261. $post['sjId'] = $this->sjId;
  262. $post['shopId'] = $this->shopId;
  263. $post['adminId'] = $this->adminId;
  264. $staff = $this->shopAdmin;
  265. $post['staffName'] = $staff->name ?? '';
  266. $orderSn = $post['orderSn'] ?? '';
  267. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  268. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  269. util::fail("订单不存在");
  270. }
  271. $order = $this->saveOrder($post, true, true);
  272. util::success($order);
  273. }
  274. //编辑草稿, 确认
  275. public function actionUpdateOrder()
  276. {
  277. $shopAdmin = $this->shopAdmin;
  278. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  279. util::fail('超管才能操作');
  280. }
  281. $post = Yii::$app->request->post();
  282. $post['sjId'] = $this->sjId;
  283. $post['shopId'] = $this->shopId;
  284. $post['mainId'] = $this->mainId;
  285. $post['adminId'] = $this->adminId;
  286. $staff = $this->shopAdmin;
  287. $post['staffName'] = $staff->name ?? '';
  288. $orderSn = $post['orderSn'] ?? '';
  289. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  290. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  291. util::fail("订单不存在");
  292. }
  293. $order = $this->saveOrder($post, false, true);
  294. util::success($order);
  295. }
  296. protected function saveOrder($post, $draft = false, $update = false)
  297. {
  298. $shopAdmin = $this->shopAdmin;
  299. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  300. util::fail('超管才能操作');
  301. }
  302. $ghsItemInfo = $post['itemInfo'] ?? '';
  303. if (empty($ghsItemInfo)) {
  304. util::fail('请选择花材4');
  305. }
  306. //数据格式 [{productId:0,bigNum:1,smallNum:0}]
  307. $ghsItemInfo = json_decode($ghsItemInfo, true);
  308. if (!is_array($ghsItemInfo)) {
  309. util::fail('花材格式不正确');
  310. }
  311. //判断花材格式,是否属于当前门店
  312. ProductClass::valid($ghsItemInfo, $this->mainId);
  313. foreach ($ghsItemInfo as $key => $item) {
  314. //如果盘点数是99999则转为0
  315. if (isset($item['bigNum']) && $item['bigNum'] == 99999) {
  316. $ghsItemInfo[$key]['bigNum'] = 0;
  317. }
  318. }
  319. $post['itemInfo'] = $ghsItemInfo;
  320. if ($update === true) {
  321. $order = CheckOrderClass::opOrder($post, $draft, $update);
  322. } else {
  323. $order = CheckOrderClass::opOrder($post, $draft, $update);
  324. }
  325. return $order;
  326. }
  327. //清空库存 ssh 20231224
  328. public function actionClearStock()
  329. {
  330. ini_set('memory_limit', '2045M');
  331. set_time_limit(0);
  332. $staff = $this->shopAdmin;
  333. $could = $staff->mobile == '15280215347' ? true : false;
  334. $mainId = $this->mainId;
  335. if (getenv('YII_ENV') == 'production') {
  336. //源花汇小周、小吴、小吕可以清花材
  337. if ($mainId == 10536) {
  338. if (in_array($this->adminId, [11648, 11961, 11976])) {
  339. $could = true;
  340. }
  341. }
  342. //花瀚鲜花供应链
  343. if ($mainId == 14499) {
  344. if (in_array($this->adminId, [11961, 11976])) {
  345. $could = true;
  346. }
  347. }
  348. //徐记花卉
  349. if ($mainId == 10866) {
  350. if (in_array($this->adminId, [11964])) {
  351. $could = true;
  352. }
  353. }
  354. } else {
  355. $could = $staff->mobile == '17187822038' ? true : false;
  356. if ($mainId == 812) {
  357. if (in_array($this->adminId, [1091])) {
  358. $could = true;
  359. }
  360. }
  361. $could = $staff->mobile == '15280215347' ? true : false;
  362. }
  363. if ($could == false) {
  364. util::fail('无法清空库存');
  365. }
  366. $mainId = $this->mainId ?? 0;
  367. $adminId = $this->adminId ?? 0;
  368. $shopId = $this->shopId ?? 0;
  369. $sjId = $this->sjId ?? 0;
  370. $list = ProductClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0, 'stock>' => 0,], null, '*', null, true);
  371. $productList = [];
  372. if (!empty($list)) {
  373. foreach ($list as $key => $info) {
  374. $id = $info->id ?? 0;
  375. if ($info->virtualStock == 0) {
  376. $productList[] = ['productId' => $id, 'bigNum' => 0, 'smallNum' => 0];
  377. }
  378. }
  379. }
  380. if (empty($productList)) {
  381. util::fail('没有花材需要清空');
  382. }
  383. $modifyData = [
  384. 'remark' => '清空库存',
  385. 'sjId' => $sjId,
  386. 'shopId' => $shopId,
  387. 'adminId' => $adminId,
  388. 'mainId' => $mainId,
  389. 'itemInfo' => $productList,
  390. ];
  391. $draft = false;
  392. $update = false;
  393. CheckOrderClass::opOrder($modifyData, $draft, $update);
  394. util::complete('清除成功');
  395. }
  396. }