AfterSaleLogic.php 19 KB

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