AfterSaleLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\after_sale;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\AfterSaleLogEnum;
  22. use app\common\enum\DeliveryEnum;
  23. use app\common\enum\NoticeEnum;
  24. use app\common\enum\OrderEnum;
  25. use app\common\enum\YesNoEnum;
  26. use app\common\logic\BaseLogic;
  27. use app\common\logic\RefundLogic;
  28. use app\common\model\AfterSale;
  29. use app\common\model\AfterSaleGoods;
  30. use app\common\model\AfterSaleLog;
  31. use app\common\model\GoodsSupplier;
  32. use app\common\model\Order;
  33. use app\common\model\OrderGoods;
  34. use app\common\model\User;
  35. use app\common\service\after_sale\AfterSaleService;
  36. use think\facade\Db;
  37. /**
  38. * 售后逻辑层
  39. * Class AfterSaleLogic
  40. * @package app\adminapi\logic\after_sale
  41. */
  42. class AfterSaleLogic extends BaseLogic
  43. {
  44. /**
  45. * @notes 卖家同意售后
  46. * @param $params
  47. * @return bool
  48. * @author Tab
  49. * @date 2021/8/2 19:51
  50. */
  51. public static function agree($params)
  52. {
  53. Db::startTrans();
  54. try{
  55. $afterSale = AfterSale::findOrEmpty($params['id']);
  56. if($afterSale->isEmpty()) {
  57. throw new \think\Exception('售后订单不存在');
  58. }
  59. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  60. throw new \think\Exception('不在售后中状态,不能进行同意售后操作');
  61. }
  62. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE) {
  63. throw new \think\Exception('不是等待卖家同意状态,不能进行同意售后操作');
  64. }
  65. $after_sale_status = 2;
  66. switch($afterSale->refund_method) {
  67. // 仅退款
  68. case AfterSaleEnum::METHOD_ONLY_REFUND:
  69. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
  70. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  71. $after_sale_status = 5;
  72. break;
  73. // 退货退款
  74. case AfterSaleEnum::METHOD_REFUND_GOODS:
  75. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_BUYER_RETURN;
  76. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意售后,等待买家退货', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  77. $after_sale_status = 4;
  78. break;
  79. }
  80. $afterSale->admin_id = $params['admin_id'];
  81. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  82. $afterSale->save();
  83. Order::update([ 'after_sale_status' => $after_sale_status ], [
  84. [ 'id', '=', $afterSale['order_id'] ],
  85. ]);
  86. Db::commit();
  87. return true;
  88. } catch(\Exception $e) {
  89. Db::rollback();
  90. self::setError($e->getMessage());
  91. return false;
  92. }
  93. }
  94. /**
  95. * @notes 卖家拒绝售后
  96. * @param $params
  97. * @return bool
  98. * @author Tab
  99. * @date 2021/8/2 20:01
  100. */
  101. public static function refuse($params)
  102. {
  103. Db::startTrans();
  104. try{
  105. $afterSale = AfterSale::findOrEmpty($params['id']);
  106. if($afterSale->isEmpty()) {
  107. throw new \think\Exception('售后订单不存在');
  108. }
  109. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  110. throw new \think\Exception('不在售后中状态,不能进行拒绝售后操作');
  111. }
  112. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE) {
  113. throw new \think\Exception('不是等待卖家同意状态,不能进行拒绝售后操作');
  114. }
  115. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  116. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_AFTER_SALE;
  117. $afterSale->admin_id = $params['admin_id'];
  118. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  119. $afterSale->save();
  120. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝售后', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  121. // 消息通知
  122. event('Notice', [
  123. 'scene_id' => NoticeEnum::REFUND_REFUSE_NOTICE,
  124. 'params' => [
  125. 'user_id' => $afterSale->user_id,
  126. 'after_sale_sn' => $afterSale->sn
  127. ]
  128. ]);
  129. Db::commit();
  130. return true;
  131. } catch(\Exception $e) {
  132. Db::rollback();
  133. self::setError($e->getMessage());
  134. return false;
  135. }
  136. }
  137. /**
  138. * @notes 卖家拒绝收货
  139. * @param $params
  140. * @return bool
  141. * @author Tab
  142. * @date 2021/8/3 12:02
  143. */
  144. public static function refuseGoods($params)
  145. {
  146. Db::startTrans();
  147. try {
  148. $afterSale = AfterSale::findOrEmpty($params['id']);
  149. if($afterSale->isEmpty()) {
  150. throw new \think\Exception('售后单不存在');
  151. }
  152. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  153. throw new \think\Exception('不是售后中状态,不能进行拒绝收货操作');
  154. }
  155. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
  156. throw new \think\Exception('不是等待卖家收货状态,不允许进行拒绝收货操作');
  157. }
  158. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  159. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_RECEIPT;
  160. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  161. $afterSale->save();
  162. // 记录日志
  163. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝收货,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  164. Db::commit();
  165. return true;
  166. } catch(\Exception $e) {
  167. Db::rollback();
  168. self::setError($e->getMessage());
  169. return false;
  170. }
  171. }
  172. /**
  173. * @notes 卖家确认收货
  174. * @param $params
  175. * @return bool
  176. * @author Tab
  177. * @date 2021/8/3 14:04
  178. */
  179. public static function confirmGoods($params)
  180. {
  181. Db::startTrans();
  182. try {
  183. $afterSale = AfterSale::findOrEmpty($params['id']);
  184. if($afterSale->isEmpty()) {
  185. throw new \think\Exception('售后单不存在');
  186. }
  187. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  188. throw new \think\Exception('不是售后中状态,不能进行确认收货操作');
  189. }
  190. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
  191. throw new \think\Exception('不是等待卖家收货状态,不允许进行确认收货操作');
  192. }
  193. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE;
  194. $afterSale->save();
  195. // 记录日志
  196. AfterSaleService::createAfterLog($afterSale->id, '卖家确认收货,等待卖家处理', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  197. Db::commit();
  198. return true;
  199. } catch(\Exception $e) {
  200. Db::rollback();
  201. self::setError($e->getMessage());
  202. return false;
  203. }
  204. }
  205. /**
  206. * @notes 卖家同意退款
  207. * @param $params
  208. * @return bool
  209. * @author Tab
  210. * @date 2021/8/3 14:24
  211. */
  212. public static function agreeRefund($params)
  213. {
  214. Db::startTrans();
  215. try {
  216. $afterSale = AfterSale::findOrEmpty($params['id']);
  217. if($afterSale->isEmpty()) {
  218. throw new \think\Exception('售后单不存在');
  219. }
  220. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  221. throw new \think\Exception('不是售后中状态,不能进行同意退款操作');
  222. }
  223. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
  224. throw new \think\Exception('不是等待卖家处理状态,不允许进行同意退款操作');
  225. }
  226. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
  227. $afterSale->save();
  228. // 记录日志
  229. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  230. Db::commit();
  231. return true;
  232. } catch(\Exception $e) {
  233. Db::rollback();
  234. self::setError($e->getMessage());
  235. return false;
  236. }
  237. }
  238. /** 卖家拒绝退款
  239. * @notes
  240. * @param $params
  241. * @return bool
  242. * @author Tab
  243. * @date 2021/8/3 14:33
  244. */
  245. public static function refuseRefund($params)
  246. {
  247. Db::startTrans();
  248. try {
  249. $afterSale = AfterSale::findOrEmpty($params['id']);
  250. if($afterSale->isEmpty()) {
  251. throw new \think\Exception('售后单不存在');
  252. }
  253. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  254. throw new \think\Exception('不是售后中状态,不能进行拒绝退款操作');
  255. }
  256. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
  257. throw new \think\Exception('不是等待卖家处理状态,不允许进行拒绝退款操作');
  258. }
  259. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  260. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_REFUND;
  261. $afterSale->save();
  262. // 记录日志
  263. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝退款,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  264. Db::commit();
  265. return true;
  266. } catch(\Exception $e) {
  267. Db::rollback();
  268. self::setError($e->getMessage());
  269. return false;
  270. }
  271. }
  272. /**
  273. * @notes 卖家确认退款
  274. * @param $params
  275. * @return bool
  276. * @author Tab
  277. * @date 2021/8/3 10:59
  278. */
  279. public static function confirmRefund($params)
  280. {
  281. Db::startTrans();
  282. try {
  283. $afterSale = AfterSale::findOrEmpty($params['id']);
  284. if($afterSale->isEmpty()) {
  285. throw new \think\Exception('售后单不存在');
  286. }
  287. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  288. throw new \think\Exception('不是售后中状态,不能进行确认退款操作');
  289. }
  290. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND) {
  291. throw new \think\Exception('不是等待卖家退款状态,不允许进行确认退款操作');
  292. }
  293. if($afterSale->refund_total_amount < $params['refund_total_amount']) {
  294. throw new \think\Exception('退款金额不能大于订单实付金额');
  295. }
  296. $afterSale->refund_total_amount = $params['refund_total_amount'];
  297. $afterSale->refund_way = $params['refund_way'];
  298. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_ING;
  299. $afterSale->save();
  300. // 更新售后商品记录中的退款金额
  301. AfterSaleGoods::where('after_sale_id', $afterSale->id)->update(['refund_amount' => $params['refund_total_amount']]);
  302. // 记录日志
  303. AfterSaleService::createAfterLog($afterSale->id, '卖家已确认退款,售后退款中', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  304. // 退款
  305. $order = Order::findOrEmpty($afterSale->order_id)->toArray();
  306. RefundLogic::refund($afterSale->refund_way, $order, $afterSale->id, $afterSale->refund_total_amount);
  307. //更新订单状态
  308. RefundLogic::afterSaleRefundUpdate($afterSale->order_id);
  309. Db::commit();
  310. return true;
  311. } catch (\Exception $e) {
  312. Db::rollback();
  313. self::setError($e->getMessage());
  314. return false;
  315. }
  316. }
  317. /**
  318. * @notes 查看售后详情
  319. * @param $params
  320. * @return string[]
  321. * @author Tab
  322. * @date 2021/8/9 18:55
  323. */
  324. public static function detail($params)
  325. {
  326. // 售后信息
  327. $afterSale = self::afterSaleInfo($params);
  328. // 退货信息
  329. $returnGoodsInfo = self::returnGoodsInfo($afterSale);
  330. // 订单信息
  331. $orderInfo = self::orderInfo($afterSale);
  332. // 商品信息
  333. $goodsInfo = self::goodsInfo($afterSale);
  334. // 售后日志
  335. $afterSaleLog= self::afterSaleLog($afterSale);
  336. // 退款按钮
  337. $btns = self::btns($afterSale);
  338. return [
  339. 'after_sale' => $afterSale,
  340. 'return_goods_info' => $returnGoodsInfo,
  341. 'order_info' => $orderInfo,
  342. 'goods_info' => $goodsInfo,
  343. 'after_sale_log' => $afterSaleLog,
  344. 'btns' => $btns
  345. ];
  346. }
  347. /**
  348. * @notes 售后信息
  349. * @param $params
  350. * @return array
  351. * @author Tab
  352. * @date 2021/8/9 20:36
  353. */
  354. public static function afterSaleInfo($params)
  355. {
  356. $field = 'id,order_id,user_id,sn,refund_type,refund_type as refund_type_desc,refund_method,refund_method as refund_method_desc,status,status as status_desc,sub_status,refund_reason,refund_remark,refund_image,create_time,express_name,invoice_no,express_remark,express_image,voucher,express_time,admin_remark';
  357. $afterSale = AfterSale::field($field)->findOrEmpty($params['id'])->toArray();
  358. return $afterSale;
  359. }
  360. /**
  361. * @notes 退货信息
  362. * @param $afterSale
  363. * @return array
  364. * @author Tab
  365. * @date 2021/8/9 20:36
  366. */
  367. public static function returnGoodsInfo($afterSale)
  368. {
  369. $user = User::field('sn,nickname,mobile')->findOrEmpty($afterSale['user_id'])->toArray();
  370. return [
  371. 'user_sn' => $user['sn'],
  372. 'user_nickname' => $user['nickname'],
  373. 'user_mobile' => $user['mobile']
  374. ];
  375. }
  376. /**
  377. * @notes 订单信息
  378. * @param $afterSale
  379. * @return array
  380. * @author Tab
  381. * @date 2021/8/9 20:36
  382. */
  383. public static function orderInfo($afterSale)
  384. {
  385. $field = 'order_status,order_status as order_status_desc,sn,order_type,order_type as order_type_desc,order_terminal,order_terminal as order_terminal_desc,create_time,pay_status,pay_status as pay_status_desc,pay_way,pay_way as pay_way_desc,pay_time,confirm_take_time,express_status,delivery_type,verification_status';
  386. return Order::field($field)->append(['express_status_desc'])->findOrEmpty($afterSale['order_id'])->toArray();
  387. }
  388. /**
  389. * @notes 商品信息
  390. * @param $afterSale
  391. * @return array
  392. * @author Tab
  393. * @date 2021/8/9 20:39
  394. */
  395. public static function goodsInfo($afterSale)
  396. {
  397. // 商品信息
  398. $field = 'gi.goods_id,gi.image as item_image,gi.spec_value_str';
  399. $field .= ',g.name as goods_name,g.image as goods_image';
  400. $field .= ',og.goods_price,og.goods_num,og.original_price,og.total_price,og.change_price,og.discount_price,og.member_price,og.integral_price,og.total_pay_price';
  401. $field .= ',asg.refund_amount';
  402. $orderGoods = AfterSaleGoods::alias('asg')
  403. ->leftJoin('goods g', 'g.id = asg.goods_id')
  404. ->leftJoin('goods_item gi', 'gi.id = asg.item_id')
  405. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  406. ->field($field)
  407. ->where('asg.after_sale_id', $afterSale['id'])
  408. ->select()
  409. ->toArray();
  410. $goodsIds = array_column($orderGoods,'goods_id');
  411. $goodsSupplier = GoodsSupplier::alias('GS')
  412. ->join('goods G','G.supplier_id = GS.id')
  413. ->where(['G.id'=>$goodsIds])
  414. ->column('GS.name','G.id');
  415. // 商品合计信息
  416. $orderGoodsSum['sum_goods_num'] = $orderGoodsSum['sum_total_price'] = $orderGoodsSum['sum_discount_price'] = $orderGoodsSum['sum_total_pay_price'] = $orderGoodsSum['sum_refund_amount'] = 0;
  417. foreach($orderGoods as &$item) {
  418. $orderGoodsSum['sum_goods_num'] += $item['goods_num'];
  419. $orderGoodsSum['sum_total_price'] += $item['total_price'];
  420. $orderGoodsSum['sum_discount_price'] += $item['discount_price'];
  421. $orderGoodsSum['sum_total_pay_price'] += $item['total_pay_price'];
  422. $orderGoodsSum['sum_refund_amount'] += $item['refund_amount'];
  423. $item['goods_image'] = get_image([$item['item_image'], $item['goods_image']]);
  424. $item['supplier_name'] = $goodsSupplier[$item['goods_id']] ?? '';
  425. $item['member_discount'] = 0;
  426. if($item['member_price'] > 0){
  427. $item['member_discount'] = round(($item['original_price'] - $item['member_price']) * $item['goods_num'],2);
  428. }
  429. $item['coupon_discount'] = $item['discount_price'];
  430. $item['integral_discount'] = $item['integral_price'];
  431. $item['total_discount'] = round( $item['member_discount']+$item['coupon_discount']+$item['integral_discount'],2);
  432. $item['total_amount'] = round($item['original_price'] * $item['goods_num'],2);
  433. }
  434. return [
  435. 'order_goods' => $orderGoods,
  436. 'order_goods_sum' => $orderGoodsSum,
  437. ];
  438. }
  439. /**
  440. * @notes 售后日志
  441. * @param $afterSale
  442. * @return array
  443. * @throws \think\db\exception\DataNotFoundException
  444. * @throws \think\db\exception\DbException
  445. * @throws \think\db\exception\ModelNotFoundException
  446. * @author Tab
  447. * @date 2021/8/9 20:42
  448. */
  449. public static function afterSaleLog($afterSale)
  450. {
  451. $field = 'operator_role,operator_id,content,create_time';
  452. $afterSaleLog = AfterSaleLog::field($field)
  453. ->where('after_sale_id', $afterSale['id'])
  454. ->order('id', 'desc')
  455. ->select()
  456. ->toArray();
  457. foreach($afterSaleLog as &$item) {
  458. $item['operator_name'] = AfterSaleLogEnum::getOpertorName($item['operator_id'], $item['operator_role']);
  459. }
  460. return $afterSaleLog;
  461. }
  462. /**
  463. * @notes 退款按钮
  464. * @param $afterSale
  465. * @return mixed
  466. * @author Tab
  467. * @date 2021/8/10 9:40
  468. */
  469. public static function btns($afterSale)
  470. {
  471. return AfterSaleEnum::getBtns($afterSale);
  472. }
  473. }