CheckOrderController.php 10 KB

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