OrderItemController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\book\classes\BookCustomClass;
  4. use bizGhs\book\classes\BookItemClass;
  5. use bizGhs\book\classes\BookItemCustomClass;
  6. use bizGhs\order\classes\OrderClass;
  7. use bizGhs\order\classes\OrderItemClass;
  8. use bizGhs\order\services\OrderItemService;
  9. use bizGhs\order\services\OrderService;
  10. use bizGhs\product\classes\ProductClass;
  11. use common\components\imgUtil;
  12. use common\components\util;
  13. use Yii;
  14. class OrderItemController extends BaseController
  15. {
  16. public $guestAccess = [];
  17. //过滤名称 ssh 20240430
  18. public function actionFilterName()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? 0;
  22. $info = OrderItemClass::getById($id, true);
  23. $name = $info->name ?? '';
  24. $num = $info->xhNum ?? 0;
  25. $name = strtolower($name);
  26. $name = preg_replace('/\d/', '', $name);
  27. $arr = ['cm', '箱', '个', 'a', 'b', 'c', 'd', 'e', 'o', '梦金鹏', '拍市', '七彩', '市场', '(', ')', '(', ')', '-', '级', '红', '黄', '绿', '橙', '蓝', '基地', '支', '枝', '*', '张良', '配色', '混色', '浅粉', '粉', '玫红', '青铜', '深紫', '浅紫', '紫', '色', '重瓣', ' ', ' '];
  28. foreach ($arr as $it) {
  29. $name = str_replace($it, '', $name);
  30. }
  31. util::success(['name' => $name, 'num' => $num]);
  32. }
  33. //修改预订单花材的数量和价格
  34. public function actionModifyBookItem()
  35. {
  36. $post = Yii::$app->request->post();
  37. $id = $post['id'] ?? 0;
  38. $data = $post['data'] ?? [];
  39. $packCost = $post['packCost'] ?? 0;
  40. $sendCost = $post['sendCost'] ?? 0;
  41. if (empty($data)) {
  42. util::fail('没有花材数据');
  43. }
  44. $order = OrderClass::getById($id, true);
  45. if (empty($order)) {
  46. util::fail('没有订单');
  47. }
  48. if ($order->mainId != $this->mainId) {
  49. util::fail('不是你的订单哦');
  50. }
  51. if ($order->stockChange == 1) {
  52. util::fail('已经入库,不能修改');
  53. }
  54. if ($order->status != 2) {
  55. util::fail('待发货订单才能修改');
  56. }
  57. $connection = Yii::$app->db;
  58. $transaction = $connection->beginTransaction();
  59. try {
  60. $addData = [];
  61. $delData = [];
  62. foreach ($data as $item) {
  63. $id = $item['id'] ?? 0;
  64. $xhNum = $item['xhNum'] ?? 0;
  65. $xhUnitPrice = $item['xhUnitPrice'] ?? 0;
  66. $info = OrderItemClass::getById($id, true);
  67. if (empty($info)) {
  68. continue;
  69. }
  70. if ($info->orderSn != $order->orderSn) {
  71. continue;
  72. }
  73. $productId = $info->productId ?? 0;
  74. $preNum = $info->xhNum ?? 0;
  75. //新增预订花材
  76. if ($xhNum > $preNum) {
  77. $diffNum = bcsub($xhNum, $preNum);
  78. $addData[] = [
  79. 'productId' => $productId,
  80. 'num' => $diffNum,
  81. 'lastNum' => $xhNum,
  82. ];
  83. }
  84. //删除预订花材
  85. if ($preNum > $xhNum) {
  86. $diffNum = bcsub($preNum, $xhNum);
  87. $delData[] = [
  88. 'productId' => $productId,
  89. 'num' => $diffNum,
  90. 'lastNum' => $xhNum,
  91. ];
  92. }
  93. $info->xhNum = $xhNum;
  94. $info->xhPreNum = $xhNum;
  95. $info->xhUnitPrice = $xhUnitPrice;
  96. $info->xhPreUnitPrice = $xhUnitPrice;
  97. $info->save();
  98. }
  99. $order->packCost = $packCost;
  100. $order->sendCost = $sendCost;
  101. $order->save();
  102. $staff = $this->shopAdmin;
  103. $staffId = $staff->id ?? 0;
  104. $staffName = $staff->name ?? '';
  105. $params = ['staff' => $staff, 'staffId' => $staffId, 'staffName' => $staffName,];
  106. if (!empty($addData)) {
  107. BookItemClass::addBatchItem($addData, $order, $params);
  108. }
  109. if (!empty($delData)) {
  110. BookItemClass::delBathItem($delData, $order, $params);
  111. }
  112. $transaction->commit();
  113. util::complete('保存成功');
  114. } catch (\Exception $e) {
  115. $transaction->rollBack();
  116. Yii::info("保存出错了:" . $e->getMessage());
  117. util::fail('保存出错了');
  118. }
  119. }
  120. //更换花材 ssh 20240430
  121. public function actionChangeItem()
  122. {
  123. $post = Yii::$app->request->post();
  124. $delId = $post['delId'] ?? 0;
  125. $orderSn = $post['orderSn'] ?? '';
  126. $num = $post['num'] ?? 0;
  127. $price = $post['price'] ?? 0;
  128. $productId = $post['productId'] ?? 0;
  129. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  130. if (empty($order)) {
  131. util::fail('订单没有找到哦');
  132. }
  133. if ($order->mainId != $this->mainId) {
  134. util::fail('不是你的订单');
  135. }
  136. $delInfo = OrderItemClass::getById($delId, true);
  137. if (empty($delInfo)) {
  138. util::fail('没有找到要删除的花材');
  139. }
  140. if ($delInfo->orderSn != $orderSn) {
  141. util::fail('这个花材你不能删除');
  142. }
  143. $connection = Yii::$app->db;
  144. $transaction = $connection->beginTransaction();
  145. try {
  146. OrderItemClass::delBookItem($order, $delId);
  147. $new = [];
  148. $new[$productId] = ['num' => $num, 'productId' => $productId, 'price' => $price];
  149. OrderItemClass::addBookItemFn($order, $new);
  150. $transaction->commit();
  151. util::complete('更换成功');
  152. } catch (\Exception $e) {
  153. $transaction->rollBack();
  154. Yii::info("出错了:" . $e->getMessage());
  155. util::fail('出错了');
  156. }
  157. }
  158. //删除花材 ssh 20240123
  159. public function actionDelItem()
  160. {
  161. $post = Yii::$app->request->post();
  162. $id = $post['id'] ?? 0;
  163. $smallId = $post['smallId'] ?? 0;
  164. $order = OrderClass::getById($id, true);
  165. if (empty($order)) {
  166. util::fail('没有订单');
  167. }
  168. if ($order->status != 2) {
  169. util::fail('待发货才能添加');
  170. }
  171. if ($order->book == 0) {
  172. util::fail('不是预订单');
  173. }
  174. if ($order->mainId != $this->mainId) {
  175. util::fail('不是你的订单');
  176. }
  177. if (floatval($order->actPrice) != 0) {
  178. util::fail('订单金额不是0');
  179. }
  180. $connection = Yii::$app->db;
  181. $transaction = $connection->beginTransaction();
  182. try {
  183. $staff = $this->shopAdmin;
  184. $staffId = $staff->id ?? 0;
  185. $staffName = $staff->name ?? '';
  186. $params = ['staff' => $staff, 'staffId' => $staffId, 'staffName' => $staffName,];
  187. OrderItemClass::delBookItem($order, $smallId, $params);
  188. $transaction->commit();
  189. util::complete('删除成功');
  190. } catch (\Exception $e) {
  191. $transaction->rollBack();
  192. Yii::info("删除出错了:" . $e->getMessage());
  193. util::fail('删除出错了');
  194. }
  195. }
  196. //添加花材
  197. public function actionAddItem()
  198. {
  199. $post = Yii::$app->request->post();
  200. $id = $post['id'] ?? 0;
  201. $data = $post['data'] ?? 0;
  202. $arr = json_decode($data, true);
  203. $order = OrderClass::getById($id, true);
  204. if (empty($order)) {
  205. util::fail('没有订单');
  206. }
  207. if ($order->status != 2) {
  208. util::fail('待发货才能添加');
  209. }
  210. if ($order->book == 0) {
  211. util::fail('不是预订单');
  212. }
  213. if (floatval($order->actPrice) != 0) {
  214. util::fail('订单金额不是0');
  215. }
  216. if (empty($arr)) {
  217. util::fail('请选花材');
  218. }
  219. $new = [];
  220. foreach ($arr as $val) {
  221. $id = $val['id'] ?? 0;
  222. $num = $val['bigCount'] ?? 0;
  223. $name = $val['name'] ?? '';
  224. $price = $val['price'] ?? 0;
  225. if ($num <= 0) {
  226. util::fail($name . ' 数量要大于0');
  227. }
  228. $new[$id] = ['num' => $num, 'productId' => $id, 'price' => $price];
  229. }
  230. $connection = Yii::$app->db;
  231. $transaction = $connection->beginTransaction();
  232. try {
  233. $staff = $this->shopAdmin;
  234. $staffId = $staff->id ?? 0;
  235. $staffName = $staff->name ?? '';
  236. $params = ['staff' => $staff, 'staffId' => $staffId, 'staffName' => $staffName,];
  237. OrderItemClass::addBookItemFn($order, $new, $params);
  238. $transaction->commit();
  239. util::complete('添加成功');
  240. } catch (\Exception $e) {
  241. $transaction->rollBack();
  242. Yii::info("添加出错了:" . $e->getMessage());
  243. util::fail('添加出错了');
  244. }
  245. }
  246. //赠送花材 ssh 20230411
  247. public function actionGiveItem()
  248. {
  249. $post = Yii::$app->request->post();
  250. $id = $post['id'] ?? 0;
  251. $num = $post['num'] ?? 0;
  252. if (is_numeric($num) == false || $num <= 0) {
  253. util::fail('请填写赠送数量');
  254. }
  255. $orderItem = OrderItemClass::getById($id, true);
  256. if (empty($orderItem)) {
  257. util::fail('没有找到花材');
  258. }
  259. if ($orderItem->mainId != $this->mainId) {
  260. util::fail('无法操作');
  261. }
  262. if (!in_array($this->mainId, [1496])) {
  263. util::fail('开发中');
  264. }
  265. $connection = Yii::$app->db;
  266. $transaction = $connection->beginTransaction();
  267. try {
  268. $staff = $this->shopAdmin;
  269. $staffName = $staff->name ?? '';
  270. $staffId = $staff->id ?? 0;
  271. $data = ['shopId' => $this->shopId, 'sjId' => $this->sjId, 'staffName' => $staffName, 'staffId' => $staffId];
  272. $respond = OrderItemService::giveProduct($data, $orderItem, $num);
  273. $transaction->commit();
  274. util::success(['info' => $respond], '赠送成功');
  275. } catch (\Exception $e) {
  276. $transaction->rollBack();
  277. Yii::info("赠送出错了:" . $e->getMessage());
  278. util::fail('出错了');
  279. }
  280. }
  281. //重选花材 ssh 2021.3.2
  282. public function actionReset()
  283. {
  284. $post = Yii::$app->request->post();
  285. $id = $post['id'] ?? 0;
  286. $product = $post['product'] ?? '[]';
  287. $product = json_decode($product, true);
  288. //检查花材是否都是同家门店的
  289. ProductClass::valid($product, $this->mainId);
  290. $info = OrderService::getOrderInfo($id);
  291. OrderClass::valid($info, $this->shopId);
  292. //添加修改花材
  293. $orderSn = $info['orderSn'] ?? '';
  294. OrderItemClass::replaceItem($orderSn, $product);
  295. util::complete();
  296. }
  297. //修改花材时获取订单花材和花材基本信息的组合 ssh 20210809
  298. public function actionGetOrderProduct()
  299. {
  300. $id = Yii::$app->request->get('id', 0);
  301. $order = OrderClass::getById($id, true);
  302. OrderClass::valid($order, $this->shopId);
  303. $orderSn = $order->orderSn ?? '';
  304. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', 'productId');
  305. $ids = array_column($itemList, 'productId');
  306. if (empty($ids)) {
  307. util::fail('没有找到花材');
  308. }
  309. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  310. if (empty($productList)) {
  311. util::fail('没有找到花材信息');
  312. }
  313. foreach ($productList as $key => $product) {
  314. $productId = $product['id'] ?? 0;
  315. $currentItem = $itemList[$productId] ?? [];
  316. $bigCount = $currentItem['bigNum'] ?? 0;
  317. $smallCount = $currentItem['smallNum'] ?? 0;
  318. $userPrice = $currentItem['userPrice'] ?? 0;
  319. $productList[$key]['bigCount'] = $bigCount;
  320. $productList[$key]['smallCount'] = $smallCount;
  321. $productList[$key]['userPrice'] = $userPrice;
  322. $cover = imgUtil::groupImg($product['cover']);
  323. $productList[$key]['cover'] = $cover;
  324. $bigPrice = $product['unitPrice'] ?? 0;
  325. $smallPrice = $product['smallUnitPrice'] ?? 0;
  326. $productList[$key]['bigPrice'] = $bigPrice;
  327. $productList[$key]['smallPrice'] = $smallPrice;
  328. $productList[$key]['cover'] = $cover;
  329. }
  330. util::success(['productList' => array_values($productList)]);
  331. }
  332. //根据花材id取订单列表 ssh 20211011
  333. public function actionGetOrderInfoList()
  334. {
  335. $get = Yii::$app->request->get();
  336. $productId = $get['productId'] ?? 0;
  337. $product = ProductClass::getById($productId, true);
  338. ProductClass::check($product, $this->mainId);
  339. $where = ['productId' => $productId];
  340. $respond = OrderItemClass::getOrderInfoList($where);
  341. util::success($respond);
  342. }
  343. //修改花材的备注 ssh 20240514
  344. public function actionUpdateItemRemark()
  345. {
  346. $get = Yii::$app->request->get();
  347. $id = $get['id'] ?? 0;
  348. $remark = $get['remark'] ?? '';
  349. $info = OrderItemClass::getById($id, true);
  350. if (empty($info)) {
  351. util::fail('没有找到');
  352. }
  353. if ($info->mainId != $this->mainId) {
  354. util::fail('不是你的花材');
  355. }
  356. $info->remark = $remark;
  357. $info->save();
  358. $orderSn = $info->orderSn ?? '';
  359. $productId = $info->productId ?? 0;
  360. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  361. if (empty($order)) {
  362. util::fail('没有找订单');
  363. }
  364. $customId = $order->customId ?? 0;
  365. $shop = $this->shop;
  366. $bookSn = $shop->bookSn ?? 0;
  367. if (!empty($bookSn) && !empty($remark)) {
  368. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
  369. if (!empty($bookItemRes)) {
  370. $bookItemRes->hasRemark = 1;
  371. $bookItemRes->save();
  372. }
  373. $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
  374. if (!empty($bookCustomRes)) {
  375. $bookCustomRes->hasRemark = 1;
  376. $bookCustomRes->save();
  377. }
  378. $bookItemCustomRes = BookItemCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId, 'itemId' => $productId], true);
  379. if (!empty($bookItemCustomRes)) {
  380. $bookItemCustomRes->hasRemark = 1;
  381. $bookItemCustomRes->remark = $remark;
  382. $bookItemCustomRes->save();
  383. }
  384. }
  385. util::complete();
  386. }
  387. //下载excel ssh 20241127
  388. public function actionGetDownExcel()
  389. {
  390. $get = Yii::$app->request->get();
  391. $id = $get['id'] ?? 0;
  392. $order = OrderClass::getById($id, true);
  393. if (empty($order)) {
  394. util::fail('没有找到订单');
  395. }
  396. if ($order->mainId != $this->mainId) {
  397. util::fail('不是你的订单');
  398. }
  399. $orderSn = $order->orderSn ?? '';
  400. $list = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  401. OrderItemClass::exportExcel($order, $list, $this->mainId);
  402. }
  403. }