OrderItemController.php 13 KB

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