AfterSaleLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. switch($afterSale->refund_method) {
  66. // 仅退款
  67. case AfterSaleEnum::METHOD_ONLY_REFUND:
  68. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
  69. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  70. break;
  71. // 退货退款
  72. case AfterSaleEnum::METHOD_REFUND_GOODS:
  73. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_BUYER_RETURN;
  74. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意售后,等待买家退货', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  75. break;
  76. }
  77. $afterSale->admin_id = $params['admin_id'];
  78. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  79. $afterSale->save();
  80. Db::commit();
  81. return true;
  82. } catch(\Exception $e) {
  83. Db::rollback();
  84. self::setError($e->getMessage());
  85. return false;
  86. }
  87. }
  88. /**
  89. * @notes 卖家拒绝售后
  90. * @param $params
  91. * @return bool
  92. * @author Tab
  93. * @date 2021/8/2 20:01
  94. */
  95. public static function refuse($params)
  96. {
  97. Db::startTrans();
  98. try{
  99. $afterSale = AfterSale::findOrEmpty($params['id']);
  100. if($afterSale->isEmpty()) {
  101. throw new \think\Exception('售后订单不存在');
  102. }
  103. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  104. throw new \think\Exception('不在售后中状态,不能进行拒绝售后操作');
  105. }
  106. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE) {
  107. throw new \think\Exception('不是等待卖家同意状态,不能进行拒绝售后操作');
  108. }
  109. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  110. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_AFTER_SALE;
  111. $afterSale->admin_id = $params['admin_id'];
  112. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  113. $afterSale->save();
  114. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝售后', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  115. // 消息通知
  116. event('Notice', [
  117. 'scene_id' => NoticeEnum::REFUND_REFUSE_NOTICE,
  118. 'params' => [
  119. 'user_id' => $afterSale->user_id,
  120. 'after_sale_sn' => $afterSale->sn
  121. ]
  122. ]);
  123. Db::commit();
  124. return true;
  125. } catch(\Exception $e) {
  126. Db::rollback();
  127. self::setError($e->getMessage());
  128. return false;
  129. }
  130. }
  131. /**
  132. * @notes 卖家拒绝收货
  133. * @param $params
  134. * @return bool
  135. * @author Tab
  136. * @date 2021/8/3 12:02
  137. */
  138. public static function refuseGoods($params)
  139. {
  140. Db::startTrans();
  141. try {
  142. $afterSale = AfterSale::findOrEmpty($params['id']);
  143. if($afterSale->isEmpty()) {
  144. throw new \think\Exception('售后单不存在');
  145. }
  146. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  147. throw new \think\Exception('不是售后中状态,不能进行拒绝收货操作');
  148. }
  149. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
  150. throw new \think\Exception('不是等待卖家收货状态,不允许进行拒绝收货操作');
  151. }
  152. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  153. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_RECEIPT;
  154. $afterSale->admin_remark = $params['admin_remark'] ?? '';
  155. $afterSale->save();
  156. // 记录日志
  157. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝收货,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  158. Db::commit();
  159. return true;
  160. } catch(\Exception $e) {
  161. Db::rollback();
  162. self::setError($e->getMessage());
  163. return false;
  164. }
  165. }
  166. /**
  167. * @notes 卖家确认收货
  168. * @param $params
  169. * @return bool
  170. * @author Tab
  171. * @date 2021/8/3 14:04
  172. */
  173. public static function confirmGoods($params)
  174. {
  175. Db::startTrans();
  176. try {
  177. $afterSale = AfterSale::findOrEmpty($params['id']);
  178. if($afterSale->isEmpty()) {
  179. throw new \think\Exception('售后单不存在');
  180. }
  181. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  182. throw new \think\Exception('不是售后中状态,不能进行确认收货操作');
  183. }
  184. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
  185. throw new \think\Exception('不是等待卖家收货状态,不允许进行确认收货操作');
  186. }
  187. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE;
  188. $afterSale->save();
  189. // 记录日志
  190. AfterSaleService::createAfterLog($afterSale->id, '卖家确认收货,等待卖家处理', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  191. Db::commit();
  192. return true;
  193. } catch(\Exception $e) {
  194. Db::rollback();
  195. self::setError($e->getMessage());
  196. return false;
  197. }
  198. }
  199. /**
  200. * @notes 卖家同意退款
  201. * @param $params
  202. * @return bool
  203. * @author Tab
  204. * @date 2021/8/3 14:24
  205. */
  206. public static function agreeRefund($params)
  207. {
  208. Db::startTrans();
  209. try {
  210. $afterSale = AfterSale::findOrEmpty($params['id']);
  211. if($afterSale->isEmpty()) {
  212. throw new \think\Exception('售后单不存在');
  213. }
  214. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  215. throw new \think\Exception('不是售后中状态,不能进行同意退款操作');
  216. }
  217. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
  218. throw new \think\Exception('不是等待卖家处理状态,不允许进行同意退款操作');
  219. }
  220. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
  221. $afterSale->save();
  222. // 记录日志
  223. AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  224. Db::commit();
  225. return true;
  226. } catch(\Exception $e) {
  227. Db::rollback();
  228. self::setError($e->getMessage());
  229. return false;
  230. }
  231. }
  232. /** 卖家拒绝退款
  233. * @notes
  234. * @param $params
  235. * @return bool
  236. * @author Tab
  237. * @date 2021/8/3 14:33
  238. */
  239. public static function refuseRefund($params)
  240. {
  241. Db::startTrans();
  242. try {
  243. $afterSale = AfterSale::findOrEmpty($params['id']);
  244. if($afterSale->isEmpty()) {
  245. throw new \think\Exception('售后单不存在');
  246. }
  247. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  248. throw new \think\Exception('不是售后中状态,不能进行拒绝退款操作');
  249. }
  250. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
  251. throw new \think\Exception('不是等待卖家处理状态,不允许进行拒绝退款操作');
  252. }
  253. $afterSale->status = AfterSaleEnum::STATUS_FAIL;
  254. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_REFUND;
  255. $afterSale->save();
  256. // 记录日志
  257. AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝退款,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  258. Db::commit();
  259. return true;
  260. } catch(\Exception $e) {
  261. Db::rollback();
  262. self::setError($e->getMessage());
  263. return false;
  264. }
  265. }
  266. /**
  267. * @notes 卖家确认退款
  268. * @param $params
  269. * @return bool
  270. * @author Tab
  271. * @date 2021/8/3 10:59
  272. */
  273. public static function confirmRefund($params)
  274. {
  275. Db::startTrans();
  276. try {
  277. $afterSale = AfterSale::findOrEmpty($params['id']);
  278. if($afterSale->isEmpty()) {
  279. throw new \think\Exception('售后单不存在');
  280. }
  281. if($afterSale->status != AfterSaleEnum::STATUS_ING) {
  282. throw new \think\Exception('不是售后中状态,不能进行确认退款操作');
  283. }
  284. if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND) {
  285. throw new \think\Exception('不是等待卖家退款状态,不允许进行确认退款操作');
  286. }
  287. if($afterSale->refund_total_amount < $params['refund_total_amount']) {
  288. throw new \think\Exception('退款金额不能大于订单实付金额');
  289. }
  290. $afterSale->refund_total_amount = $params['refund_total_amount'];
  291. $afterSale->refund_way = $params['refund_way'];
  292. $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_ING;
  293. $afterSale->save();
  294. // 更新售后商品记录中的退款金额
  295. AfterSaleGoods::where('after_sale_id', $afterSale->id)->update(['refund_amount' => $params['refund_total_amount']]);
  296. // 记录日志
  297. AfterSaleService::createAfterLog($afterSale->id, '卖家已确认退款,售后退款中', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
  298. // 退款
  299. $order = Order::findOrEmpty($afterSale->order_id)->toArray();
  300. RefundLogic::refund($afterSale->refund_way, $order, $afterSale->id, $afterSale->refund_total_amount);
  301. //更新订单状态
  302. RefundLogic::afterSaleRefundUpdate($afterSale->order_id);
  303. Db::commit();
  304. return true;
  305. } catch (\Exception $e) {
  306. Db::rollback();
  307. self::setError($e->getMessage());
  308. return false;
  309. }
  310. }
  311. /**
  312. * @notes 查看售后详情
  313. * @param $params
  314. * @return string[]
  315. * @author Tab
  316. * @date 2021/8/9 18:55
  317. */
  318. public static function detail($params)
  319. {
  320. // 售后信息
  321. $afterSale = self::afterSaleInfo($params);
  322. // 退货信息
  323. $returnGoodsInfo = self::returnGoodsInfo($afterSale);
  324. // 订单信息
  325. $orderInfo = self::orderInfo($afterSale);
  326. // 商品信息
  327. $goodsInfo = self::goodsInfo($afterSale);
  328. // 售后日志
  329. $afterSaleLog= self::afterSaleLog($afterSale);
  330. // 退款按钮
  331. $btns = self::btns($afterSale);
  332. return [
  333. 'after_sale' => $afterSale,
  334. 'return_goods_info' => $returnGoodsInfo,
  335. 'order_info' => $orderInfo,
  336. 'goods_info' => $goodsInfo,
  337. 'after_sale_log' => $afterSaleLog,
  338. 'btns' => $btns
  339. ];
  340. }
  341. /**
  342. * @notes 售后信息
  343. * @param $params
  344. * @return array
  345. * @author Tab
  346. * @date 2021/8/9 20:36
  347. */
  348. public static function afterSaleInfo($params)
  349. {
  350. $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';
  351. $afterSale = AfterSale::field($field)->findOrEmpty($params['id'])->toArray();
  352. return $afterSale;
  353. }
  354. /**
  355. * @notes 退货信息
  356. * @param $afterSale
  357. * @return array
  358. * @author Tab
  359. * @date 2021/8/9 20:36
  360. */
  361. public static function returnGoodsInfo($afterSale)
  362. {
  363. $user = User::field('sn,nickname,mobile')->findOrEmpty($afterSale['user_id'])->toArray();
  364. return [
  365. 'user_sn' => $user['sn'],
  366. 'user_nickname' => $user['nickname'],
  367. 'user_mobile' => $user['mobile']
  368. ];
  369. }
  370. /**
  371. * @notes 订单信息
  372. * @param $afterSale
  373. * @return array
  374. * @author Tab
  375. * @date 2021/8/9 20:36
  376. */
  377. public static function orderInfo($afterSale)
  378. {
  379. $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';
  380. return Order::field($field)->append(['express_status_desc'])->findOrEmpty($afterSale['order_id'])->toArray();
  381. }
  382. /**
  383. * @notes 商品信息
  384. * @param $afterSale
  385. * @return array
  386. * @author Tab
  387. * @date 2021/8/9 20:39
  388. */
  389. public static function goodsInfo($afterSale)
  390. {
  391. // 商品信息
  392. $field = 'gi.goods_id,gi.image as item_image,gi.spec_value_str';
  393. $field .= ',g.name as goods_name,g.image as goods_image';
  394. $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';
  395. $field .= ',asg.refund_amount';
  396. $orderGoods = AfterSaleGoods::alias('asg')
  397. ->leftJoin('goods g', 'g.id = asg.goods_id')
  398. ->leftJoin('goods_item gi', 'gi.id = asg.item_id')
  399. ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
  400. ->field($field)
  401. ->where('asg.after_sale_id', $afterSale['id'])
  402. ->select()
  403. ->toArray();
  404. $goodsIds = array_column($orderGoods,'goods_id');
  405. $goodsSupplier = GoodsSupplier::alias('GS')
  406. ->join('goods G','G.supplier_id = GS.id')
  407. ->where(['G.id'=>$goodsIds])
  408. ->column('GS.name','G.id');
  409. // 商品合计信息
  410. $orderGoodsSum['sum_goods_num'] = $orderGoodsSum['sum_total_price'] = $orderGoodsSum['sum_discount_price'] = $orderGoodsSum['sum_total_pay_price'] = $orderGoodsSum['sum_refund_amount'] = 0;
  411. foreach($orderGoods as &$item) {
  412. $orderGoodsSum['sum_goods_num'] += $item['goods_num'];
  413. $orderGoodsSum['sum_total_price'] += $item['total_price'];
  414. $orderGoodsSum['sum_discount_price'] += $item['discount_price'];
  415. $orderGoodsSum['sum_total_pay_price'] += $item['total_pay_price'];
  416. $orderGoodsSum['sum_refund_amount'] += $item['refund_amount'];
  417. $item['goods_image'] = get_image([$item['item_image'], $item['goods_image']]);
  418. $item['supplier_name'] = $goodsSupplier[$item['goods_id']] ?? '';
  419. $item['member_discount'] = 0;
  420. if($item['member_price'] > 0){
  421. $item['member_discount'] = round(($item['original_price'] - $item['member_price']) * $item['goods_num'],2);
  422. }
  423. $item['coupon_discount'] = $item['discount_price'];
  424. $item['integral_discount'] = $item['integral_price'];
  425. $item['total_discount'] = round( $item['member_discount']+$item['coupon_discount']+$item['integral_discount'],2);
  426. $item['total_amount'] = round($item['original_price'] * $item['goods_num'],2);
  427. }
  428. return [
  429. 'order_goods' => $orderGoods,
  430. 'order_goods_sum' => $orderGoodsSum,
  431. ];
  432. }
  433. /**
  434. * @notes 售后日志
  435. * @param $afterSale
  436. * @return array
  437. * @throws \think\db\exception\DataNotFoundException
  438. * @throws \think\db\exception\DbException
  439. * @throws \think\db\exception\ModelNotFoundException
  440. * @author Tab
  441. * @date 2021/8/9 20:42
  442. */
  443. public static function afterSaleLog($afterSale)
  444. {
  445. $field = 'operator_role,operator_id,content,create_time';
  446. $afterSaleLog = AfterSaleLog::field($field)
  447. ->where('after_sale_id', $afterSale['id'])
  448. ->order('id', 'desc')
  449. ->select()
  450. ->toArray();
  451. foreach($afterSaleLog as &$item) {
  452. $item['operator_name'] = AfterSaleLogEnum::getOpertorName($item['operator_id'], $item['operator_role']);
  453. }
  454. return $afterSaleLog;
  455. }
  456. /**
  457. * @notes 退款按钮
  458. * @param $afterSale
  459. * @return mixed
  460. * @author Tab
  461. * @date 2021/8/10 9:40
  462. */
  463. public static function btns($afterSale)
  464. {
  465. return AfterSaleEnum::getBtns($afterSale);
  466. }
  467. }