CheckOrderController.php 13 KB

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