CheckOrderController.php 15 KB

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