CheckOrderController.php 14 KB

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