OrderItemController.php 16 KB

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