CheckOrderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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) == false || $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. $order = $this->saveOrder($post, false, false);
  196. util::success($order);
  197. }
  198. //盘点确认 lqh 2021.1.22
  199. public function actionCreateOrder()
  200. {
  201. $shopAdmin = $this->shopAdmin;
  202. if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
  203. util::fail('超管才能盘点');
  204. }
  205. $post = Yii::$app->request->post();
  206. $post['sjId'] = $this->sjId;
  207. $post['shopId'] = $this->shopId;
  208. $post['adminId'] = $this->adminId;
  209. $post['mainId'] = $this->mainId;
  210. $staff = $this->shopAdmin;
  211. $post['staffName'] = $staff->name ?? '';
  212. $connection = Yii::$app->db;
  213. $transaction = $connection->beginTransaction();
  214. try {
  215. $order = $this->saveOrder($post, false, false);
  216. $transaction->commit();
  217. util::success($order);
  218. } catch (\Exception $e) {
  219. Yii::info("操作失败:" . $e->getMessage());
  220. $transaction->rollBack();
  221. util::fail('操作失败');
  222. }
  223. }
  224. //盘点存草稿 lqh 2021.1.22
  225. public function actionCreateDraft()
  226. {
  227. $shopAdmin = $this->shopAdmin;
  228. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  229. util::fail('超管才能操作');
  230. }
  231. $post = Yii::$app->request->post();
  232. $post['sjId'] = $this->sjId;
  233. $post['shopId'] = $this->shopId;
  234. $post['adminId'] = $this->adminId;
  235. //ssh 20210827
  236. util::fail('草稿功能取消');
  237. $order = $this->saveOrder($post, true, false);
  238. util::success($order);
  239. }
  240. //编辑草稿 ,继续保存草稿
  241. public function actionUpdateDraft()
  242. {
  243. $shopAdmin = $this->shopAdmin;
  244. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  245. util::fail('超管才能操作');
  246. }
  247. $post = Yii::$app->request->post();
  248. $post['sjId'] = $this->sjId;
  249. $post['shopId'] = $this->shopId;
  250. $post['adminId'] = $this->adminId;
  251. $staff = $this->shopAdmin;
  252. $post['staffName'] = $staff->name ?? '';
  253. $orderSn = $post['orderSn'] ?? '';
  254. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  255. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  256. util::fail("订单不存在");
  257. }
  258. $order = $this->saveOrder($post, true, true);
  259. util::success($order);
  260. }
  261. //编辑草稿, 确认
  262. public function actionUpdateOrder()
  263. {
  264. $shopAdmin = $this->shopAdmin;
  265. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  266. util::fail('超管才能操作');
  267. }
  268. $post = Yii::$app->request->post();
  269. $post['sjId'] = $this->sjId;
  270. $post['shopId'] = $this->shopId;
  271. $post['mainId'] = $this->mainId;
  272. $post['adminId'] = $this->adminId;
  273. $staff = $this->shopAdmin;
  274. $post['staffName'] = $staff->name ?? '';
  275. $orderSn = $post['orderSn'] ?? '';
  276. $orderData = CheckOrderClass::orderExist($orderSn, $this->shopId);
  277. if ($orderData['status'] != CheckOrderClass::STATUS_DRAFT) {
  278. util::fail("订单不存在");
  279. }
  280. $order = $this->saveOrder($post, false, true);
  281. util::success($order);
  282. }
  283. protected function saveOrder($post, $draft = false, $update = false)
  284. {
  285. $shopAdmin = $this->shopAdmin;
  286. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  287. util::fail('超管才能操作');
  288. }
  289. $ghsItemInfo = $post['itemInfo'] ?? '';
  290. if (empty($ghsItemInfo)) {
  291. util::fail('请选择花材4');
  292. }
  293. //数据格式 [{productId:0,bigNum:1,smallNum:0}]
  294. $ghsItemInfo = json_decode($ghsItemInfo, true);
  295. if (!is_array($ghsItemInfo)) {
  296. util::fail('花材格式不正确');
  297. }
  298. //判断花材格式,是否属于当前门店
  299. ProductClass::valid($ghsItemInfo, $this->mainId);
  300. foreach ($ghsItemInfo as $key => $item) {
  301. //如果盘点数是99999则转为0
  302. if (isset($item['bigNum']) && $item['bigNum'] == 99999) {
  303. $ghsItemInfo[$key]['bigNum'] = 0;
  304. }
  305. }
  306. $post['itemInfo'] = $ghsItemInfo;
  307. if ($update === true) {
  308. $order = CheckOrderClass::opOrder($post, $draft, $update);
  309. } else {
  310. $order = CheckOrderClass::opOrder($post, $draft, $update);
  311. }
  312. return $order;
  313. }
  314. //清空库存 ssh 20231224
  315. public function actionClearStock()
  316. {
  317. ini_set('memory_limit', '2045M');
  318. set_time_limit(0);
  319. $staff = $this->shopAdmin;
  320. $could = $staff->mobile == '15280215347' ? true : false;
  321. $mainId = $this->mainId;
  322. if (getenv('YII_ENV') == 'production') {
  323. //源花汇小周、小吴、小吕可以清花材
  324. if ($mainId == 10536) {
  325. if (in_array($this->adminId, [11648, 11961, 11976])) {
  326. $could = true;
  327. }
  328. }
  329. //花瀚鲜花供应链
  330. if ($mainId == 14499) {
  331. if (in_array($this->adminId, [11961, 11976])) {
  332. $could = true;
  333. }
  334. }
  335. //徐记花卉
  336. if ($mainId == 10866) {
  337. if (in_array($this->adminId, [11964])) {
  338. $could = true;
  339. }
  340. }
  341. //小蔡鲜花苏南店
  342. if ($mainId == 28944) {
  343. if (in_array($this->adminId, [29264])) {
  344. $could = true;
  345. }
  346. }
  347. } else {
  348. $could = $staff->mobile == '17187822038' ? true : false;
  349. if ($mainId == 812) {
  350. if (in_array($this->adminId, [1091])) {
  351. $could = true;
  352. }
  353. }
  354. $could = $staff->mobile == '15280215347' ? true : false;
  355. }
  356. if ($could == false) {
  357. noticeUtil::push("不能清空库存", '15280215347');
  358. util::fail('无法清空库存');
  359. }
  360. noticeUtil::push("可以清空库存", '15280215347');
  361. util::complete('清除成功哈!');
  362. $mainId = $this->mainId ?? 0;
  363. $adminId = $this->adminId ?? 0;
  364. $shopId = $this->shopId ?? 0;
  365. $sjId = $this->sjId ?? 0;
  366. $list = ProductClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0, 'stock>' => 0,], null, '*', null, true);
  367. $productList = [];
  368. if (!empty($list)) {
  369. foreach ($list as $key => $info) {
  370. $id = $info->id ?? 0;
  371. if ($info->virtualStock == 0) {
  372. $productList[] = ['productId' => $id, 'bigNum' => 0, 'smallNum' => 0];
  373. }
  374. }
  375. }
  376. if (empty($productList)) {
  377. util::fail('没有花材需要清空');
  378. }
  379. $modifyData = [
  380. 'remark' => '清空库存',
  381. 'sjId' => $sjId,
  382. 'shopId' => $shopId,
  383. 'adminId' => $adminId,
  384. 'mainId' => $mainId,
  385. 'itemInfo' => $productList,
  386. ];
  387. $draft = false;
  388. $update = false;
  389. CheckOrderClass::opOrder($modifyData, $draft, $update);
  390. util::complete('清除成功');
  391. }
  392. }