| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\after_sale;
- use app\common\enum\AfterSaleEnum;
- use app\common\enum\AfterSaleLogEnum;
- use app\common\enum\DeliveryEnum;
- use app\common\enum\NoticeEnum;
- use app\common\enum\OrderEnum;
- use app\common\enum\YesNoEnum;
- use app\common\logic\BaseLogic;
- use app\common\logic\RefundLogic;
- use app\common\model\AfterSale;
- use app\common\model\AfterSaleGoods;
- use app\common\model\AfterSaleLog;
- use app\common\model\GoodsSupplier;
- use app\common\model\Order;
- use app\common\model\OrderGoods;
- use app\common\model\User;
- use app\common\service\after_sale\AfterSaleService;
- use think\facade\Db;
- /**
- * 售后逻辑层
- * Class AfterSaleLogic
- * @package app\adminapi\logic\after_sale
- */
- class AfterSaleLogic extends BaseLogic
- {
- /**
- * @notes 卖家同意售后
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/2 19:51
- */
- public static function agree($params)
- {
- Db::startTrans();
- try{
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后订单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不在售后中状态,不能进行同意售后操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE) {
- throw new \think\Exception('不是等待卖家同意状态,不能进行同意售后操作');
- }
- switch($afterSale->refund_method) {
- // 仅退款
- case AfterSaleEnum::METHOD_ONLY_REFUND:
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
- AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- break;
- // 退货退款
- case AfterSaleEnum::METHOD_REFUND_GOODS:
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_BUYER_RETURN;
- AfterSaleService::createAfterLog($afterSale->id, '卖家已同意售后,等待买家退货', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- break;
- }
- $afterSale->admin_id = $params['admin_id'];
- $afterSale->admin_remark = $params['admin_remark'] ?? '';
- $afterSale->save();
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 卖家拒绝售后
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/2 20:01
- */
- public static function refuse($params)
- {
- Db::startTrans();
- try{
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后订单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不在售后中状态,不能进行拒绝售后操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_AGREE) {
- throw new \think\Exception('不是等待卖家同意状态,不能进行拒绝售后操作');
- }
- $afterSale->status = AfterSaleEnum::STATUS_FAIL;
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_AFTER_SALE;
- $afterSale->admin_id = $params['admin_id'];
- $afterSale->admin_remark = $params['admin_remark'] ?? '';
- $afterSale->save();
- AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝售后', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- // 消息通知
- event('Notice', [
- 'scene_id' => NoticeEnum::REFUND_REFUSE_NOTICE,
- 'params' => [
- 'user_id' => $afterSale->user_id,
- 'after_sale_sn' => $afterSale->sn
- ]
- ]);
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 卖家拒绝收货
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/3 12:02
- */
- public static function refuseGoods($params)
- {
- Db::startTrans();
- try {
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不是售后中状态,不能进行拒绝收货操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
- throw new \think\Exception('不是等待卖家收货状态,不允许进行拒绝收货操作');
- }
- $afterSale->status = AfterSaleEnum::STATUS_FAIL;
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_RECEIPT;
- $afterSale->save();
- // 记录日志
- AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝收货,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 卖家确认收货
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/3 14:04
- */
- public static function confirmGoods($params)
- {
- Db::startTrans();
- try {
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不是售后中状态,不能进行确认收货操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_RECEIPT) {
- throw new \think\Exception('不是等待卖家收货状态,不允许进行确认收货操作');
- }
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE;
- $afterSale->save();
- // 记录日志
- AfterSaleService::createAfterLog($afterSale->id, '卖家确认收货,等待卖家处理', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 卖家同意退款
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/3 14:24
- */
- public static function agreeRefund($params)
- {
- Db::startTrans();
- try {
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不是售后中状态,不能进行同意退款操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
- throw new \think\Exception('不是等待卖家处理状态,不允许进行同意退款操作');
- }
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND;
- $afterSale->save();
- // 记录日志
- AfterSaleService::createAfterLog($afterSale->id, '卖家已同意,等待退款', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /** 卖家拒绝退款
- * @notes
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/3 14:33
- */
- public static function refuseRefund($params)
- {
- Db::startTrans();
- try {
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不是售后中状态,不能进行拒绝退款操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_HANDLE) {
- throw new \think\Exception('不是等待卖家处理状态,不允许进行拒绝退款操作');
- }
- $afterSale->status = AfterSaleEnum::STATUS_FAIL;
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUSE_REFUND;
- $afterSale->save();
- // 记录日志
- AfterSaleService::createAfterLog($afterSale->id, '卖家拒绝退款,售后失败', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- Db::commit();
- return true;
- } catch(\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 卖家确认退款
- * @param $params
- * @return bool
- * @author Tab
- * @date 2021/8/3 10:59
- */
- public static function confirmRefund($params)
- {
- Db::startTrans();
- try {
- $afterSale = AfterSale::findOrEmpty($params['id']);
- if($afterSale->isEmpty()) {
- throw new \think\Exception('售后单不存在');
- }
- if($afterSale->status != AfterSaleEnum::STATUS_ING) {
- throw new \think\Exception('不是售后中状态,不能进行确认退款操作');
- }
- if($afterSale->sub_status != AfterSaleEnum::SUB_STATUS_WAIT_SELLER_REFUND) {
- throw new \think\Exception('不是等待卖家退款状态,不允许进行确认退款操作');
- }
- if($afterSale->refund_total_amount < $params['refund_total_amount']) {
- throw new \think\Exception('退款金额不能大于订单实付金额');
- }
- $afterSale->refund_total_amount = $params['refund_total_amount'];
- $afterSale->refund_way = $params['refund_way'];
- $afterSale->sub_status = AfterSaleEnum::SUB_STATUS_SELLER_REFUND_ING;
- $afterSale->save();
- // 更新售后商品记录中的退款金额
- AfterSaleGoods::where('after_sale_id', $afterSale->id)->update(['refund_amount' => $params['refund_total_amount']]);
- // 记录日志
- AfterSaleService::createAfterLog($afterSale->id, '卖家已确认退款,售后退款中', $params['admin_id'], AfterSaleLogEnum::ROLE_SELLER);
- // 退款
- $order = Order::findOrEmpty($afterSale->order_id)->toArray();
- RefundLogic::refund($afterSale->refund_way, $order, $afterSale->id, $afterSale->refund_total_amount);
-
- //更新订单状态
- RefundLogic::afterSaleRefundUpdate($afterSale->order_id);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 查看售后详情
- * @param $params
- * @return string[]
- * @author Tab
- * @date 2021/8/9 18:55
- */
- public static function detail($params)
- {
- // 售后信息
- $afterSale = self::afterSaleInfo($params);
- // 退货信息
- $returnGoodsInfo = self::returnGoodsInfo($afterSale);
- // 订单信息
- $orderInfo = self::orderInfo($afterSale);
- // 商品信息
- $goodsInfo = self::goodsInfo($afterSale);
- // 售后日志
- $afterSaleLog= self::afterSaleLog($afterSale);
- // 退款按钮
- $btns = self::btns($afterSale);
- return [
- 'after_sale' => $afterSale,
- 'return_goods_info' => $returnGoodsInfo,
- 'order_info' => $orderInfo,
- 'goods_info' => $goodsInfo,
- 'after_sale_log' => $afterSaleLog,
- 'btns' => $btns
- ];
- }
- /**
- * @notes 售后信息
- * @param $params
- * @return array
- * @author Tab
- * @date 2021/8/9 20:36
- */
- public static function afterSaleInfo($params)
- {
- $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';
- $afterSale = AfterSale::field($field)->findOrEmpty($params['id'])->toArray();
- return $afterSale;
- }
- /**
- * @notes 退货信息
- * @param $afterSale
- * @return array
- * @author Tab
- * @date 2021/8/9 20:36
- */
- public static function returnGoodsInfo($afterSale)
- {
- $user = User::field('sn,nickname,mobile')->findOrEmpty($afterSale['user_id'])->toArray();
- return [
- 'user_sn' => $user['sn'],
- 'user_nickname' => $user['nickname'],
- 'user_mobile' => $user['mobile']
- ];
- }
- /**
- * @notes 订单信息
- * @param $afterSale
- * @return array
- * @author Tab
- * @date 2021/8/9 20:36
- */
- public static function orderInfo($afterSale)
- {
- $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';
- return Order::field($field)->append(['express_status_desc'])->findOrEmpty($afterSale['order_id'])->toArray();
- }
- /**
- * @notes 商品信息
- * @param $afterSale
- * @return array
- * @author Tab
- * @date 2021/8/9 20:39
- */
- public static function goodsInfo($afterSale)
- {
- // 商品信息
- $field = 'gi.goods_id,gi.image as item_image,gi.spec_value_str';
- $field .= ',g.name as goods_name,g.image as goods_image';
- $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';
- $field .= ',asg.refund_amount';
- $orderGoods = AfterSaleGoods::alias('asg')
- ->leftJoin('goods g', 'g.id = asg.goods_id')
- ->leftJoin('goods_item gi', 'gi.id = asg.item_id')
- ->leftJoin('order_goods og', 'og.id = asg.order_goods_id')
- ->field($field)
- ->where('asg.after_sale_id', $afterSale['id'])
- ->select()
- ->toArray();
- $goodsIds = array_column($orderGoods,'goods_id');
- $goodsSupplier = GoodsSupplier::alias('GS')
- ->join('goods G','G.supplier_id = GS.id')
- ->where(['G.id'=>$goodsIds])
- ->column('GS.name','G.id');
- // 商品合计信息
- $orderGoodsSum['sum_goods_num'] = $orderGoodsSum['sum_total_price'] = $orderGoodsSum['sum_discount_price'] = $orderGoodsSum['sum_total_pay_price'] = $orderGoodsSum['sum_refund_amount'] = 0;
- foreach($orderGoods as &$item) {
- $orderGoodsSum['sum_goods_num'] += $item['goods_num'];
- $orderGoodsSum['sum_total_price'] += $item['total_price'];
- $orderGoodsSum['sum_discount_price'] += $item['discount_price'];
- $orderGoodsSum['sum_total_pay_price'] += $item['total_pay_price'];
- $orderGoodsSum['sum_refund_amount'] += $item['refund_amount'];
- $item['goods_image'] = get_image([$item['item_image'], $item['goods_image']]);
- $item['supplier_name'] = $goodsSupplier[$item['goods_id']] ?? '';
- $item['member_discount'] = 0;
- if($item['member_price'] > 0){
- $item['member_discount'] = round(($item['original_price'] - $item['member_price']) * $item['goods_num'],2);
- }
- $item['coupon_discount'] = $item['discount_price'];
- $item['integral_discount'] = $item['integral_price'];
- $item['total_discount'] = round( $item['member_discount']+$item['coupon_discount']+$item['integral_discount'],2);
- $item['total_amount'] = round($item['original_price'] * $item['goods_num'],2);
- }
- return [
- 'order_goods' => $orderGoods,
- 'order_goods_sum' => $orderGoodsSum,
- ];
- }
- /**
- * @notes 售后日志
- * @param $afterSale
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author Tab
- * @date 2021/8/9 20:42
- */
- public static function afterSaleLog($afterSale)
- {
- $field = 'operator_role,operator_id,content,create_time';
- $afterSaleLog = AfterSaleLog::field($field)
- ->where('after_sale_id', $afterSale['id'])
- ->order('id', 'desc')
- ->select()
- ->toArray();
- foreach($afterSaleLog as &$item) {
- $item['operator_name'] = AfterSaleLogEnum::getOpertorName($item['operator_id'], $item['operator_role']);
- }
- return $afterSaleLog;
- }
- /**
- * @notes 退款按钮
- * @param $afterSale
- * @return mixed
- * @author Tab
- * @date 2021/8/10 9:40
- */
- public static function btns($afterSale)
- {
- return AfterSaleEnum::getBtns($afterSale);
- }
- }
|