quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
 
namespace app\common\model\order;
 
use app\common\model\BaseModel;
use app\common\model\order\Order as OrderModel;
use app\shop\model\user\User as UserModel;
use app\common\service\order\OrderRefundService;
use app\common\service\message\MessageService;
use app\common\enum\order\OrderPayTypeEnum;
 
/**
 * 售后管理模型
 */
class OrderRefund extends BaseModel
{
    protected $name = 'order_refund';
    protected $pk = 'order_refund_id';
 
    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User');
    }
 
    /**
     * 关联订单主表
     */
    public function orderMaster()
    {
        return $this->belongsTo('app\\common\\model\\order\\Order');
    }
 
    /**
     * 关联订单商品表
     */
    public function orderproduct()
    {
        return $this->belongsTo('app\\common\\model\\order\\OrderProduct', 'order_product_id', 'order_product_id');
    }
 
    /**
     * 关联图片记录表
     */
    public function image()
    {
        return $this->hasMany('app\\common\\model\\order\\OrderRefundImage');
    }
 
    /**
     * 关联物流公司表
     */
    public function express()
    {
        return $this->belongsTo('app\\api\\model\\settings\\Express');
    }
 
    /**
     * 关联物流公司表
     */
    public function sendexpress()
    {
        return $this->belongsTo('app\\api\\model\\settings\\Express', 'send_express_id', 'express_id');
    }
 
    /**
     * 关联用户表
     */
    public function address()
    {
        return $this->hasOne('app\\api\\model\\order\\OrderRefundAddress');
    }
 
    /**
     * 关联供应商表
     */
    public function supplier()
    {
        return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
    }
 
    /**
     * 售后类型
     */
    public function getTypeAttr($value)
    {
        $status = [10 => '退货退款', 20 => '换货', 30 => '仅退款'];
        return ['text' => $status[$value], 'value' => $value];
    }
 
    /**
     * 售后类型
     */
    public function getPlateStatusAttr($value)
    {
        $status = [0 => '未申请', 10 => '待审核', 20 => '已同意', 30 => '已拒绝'];
        return ['text' => $status[$value], 'value' => $value];
    }
 
    /**
     * 商家是否同意售后
     */
    public function getIsAgreeAttr($value)
    {
        $status = [0 => '待审核', 10 => '已同意', 20 => '已拒绝'];
        return ['text' => $status[$value], 'value' => $value];
    }
 
    /**
     * 售后单状态
     */
    public function getStatusAttr($value)
    {
        $status = [0 => '进行中', 10 => '已拒绝', 20 => '已完成', 30 => '已取消'];
        return ['text' => $status[$value], 'value' => $value];
    }
 
    /**
     * 售后类型
     */
    public function getDeliverTimeAttr($value)
    {
 
        return isset($value) && $value > 0 ? date('Y-m-d H:i:s', $value) : '';
    }
 
    /**
     * 售后单详情
     */
    public static function detail($where)
    {
        is_array($where) ? $filter = $where : $filter['order_refund_id'] = (int)$where;
        return (new static())->with(['order_master.advance', 'image.file', 'orderproduct.image', 'express', 'address', 'user', 'sendexpress'])->where($filter)->find();
    }
 
    /**
     * 获取退款订单总数 (可指定某天)
     * 已同意的退款
     */
    public function getOrderRefundData($startDate = null, $endDate = null, $type, $shop_supplier_id)
    {
        $model = $this;
        $model = $model->where('create_time', '>=', strtotime($startDate));
        if (is_null($endDate)) {
            $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('create_time', '<', strtotime($endDate) + 86400);
        }
 
        if ($shop_supplier_id > 0) {
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
 
        $model = $model->where('is_agree', '=', 10);
 
        if ($type == 'order_refund_money') {
            // 退款金额
            return $model->sum('refund_money');
        } else if ($type == 'order_refund_total') {
            // 退款数量
            return $model->count();
        }
        return 0;
    }
 
    /**
     * 商家审核
     */
    public function audit($data)
    {
        if ($this['is_agree']['value'] != 0) {
            $this->error = '售后已审核';
            return false;
        }
        if ($data['is_agree'] == 20 && empty($data['refuse_desc'])) {
            $this->error = '请输入拒绝原因';
            return false;
        }
        if ($data['is_agree'] == 10 && $this['type']['value'] != 30 && empty($data['address_id'])) {
            $this->error = '请选择退货地址';
            return false;
        }
        $this->startTrans();
        try {
            // 拒绝申请, 标记售后单状态为已拒绝
            $data['is_agree'] == 20 && $data['status'] = 10;
            // 同意换货申请, 标记售后单状态为已完成
            //$data['is_agree'] == 10 && $this['type']['value'] == 20 && $data['status'] = 20;
            // 更新退款单状态
            $this->save($data);
            // 同意售后申请, 记录退货地址
            if ($data['is_agree'] == 10 && $this['type']['value'] != 30) {
                $model = new OrderRefundAddress();
                $model->add($this['order_refund_id'], $data['address_id']);
            }
            // 订单详情
            $order = Order::detail($this['order_id']);
            // 发送模板消息
            (new MessageService)->refund(self::detail($this['order_refund_id']), $order['order_no'], 'audit');
            // 如果是仅退款
            if ($data['is_agree'] == 10 && $this['type']['value'] == 30) {
                $total_refund = $this['orderproduct']['total_pay_price'];
                if ($order['order_source'] == 70 && $order['advance']['money_return'] == 1) {
                    $total_refund = round($total_refund + $order['advance']['pay_price'], 2);
                }
                if ($data['refund_money'] > $total_refund) {
                    $this->error = '退款金额不能大于商品实付款金额';
                    return false;
                }
                $this->refundMoney($order, $data);
            }
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 确认收货并退款
     */
    public function receipt($data)
    {
        // 订单详情
        $order = Order::detail($this['order_id']);
        $total_refund = $this['orderproduct']['total_pay_price'];
        if ($order['order_source'] == 70 && $order['advance']['money_return'] == 1) {
            $total_refund = round($total_refund + $order['advance']['pay_price'], 2);
 
        }
        if ($data['refund_money'] > $total_refund) {
            $this->error = '退款金额不能大于商品实付款金额';
            return false;
        }
        $this->transaction(function () use ($order, $data) {
            $this->refundMoney($order, $data);
        });
        return true;
    }
 
    public function refundMoney($order, $data)
    {
        $advance_money = 0;//预售定金
        if ($order['order_source'] == 70 && $order['advance']['money_return'] == 1) {
            $totalMoney = round($order['pay_price'] + $order['advance']['pay_price'], 2);
            if ($data['refund_money'] > $order['pay_price']) {
                $data['refund_money'] = $order['pay_price'];
                $advance_money = round($totalMoney - $data['refund_money'], 2);
            }
        }
        $update = [
            'is_receipt' => 1,
            'status' => 20
        ];
        if ($this['type']['value'] == 20) {
            $update['send_express_id'] = $data['send_express_id'];
            $update['send_express_no'] = $data['send_express_no'];
            $update['deliver_time'] = time();
            $update['is_plate_send'] = 1;
        }
        $data['refund_money'] > 0 && $update['refund_money'] = $data['refund_money'];
        $out_refund_no = '';
        if ($order['pay_type']['value'] == OrderPayTypeEnum::ALIPAY) {
            $out_refund_no = (new OrderModel)->orderNo();
        }
        $out_refund_no && $update['out_refund_no'] = $out_refund_no;
        // 更新售后单状态
        $this->save($update);
        // 消减用户的实际消费金额
        // 条件:判断订单是否已结算
        if ($order['is_settled'] == true && $data['refund_money'] > 0) {
            (new UserModel)->setDecUserExpend($order['user_id'], $data['refund_money']);
        }
        // 执行原路退款
        $data['refund_money'] > 0 && (new OrderRefundService)->execute($order, $data['refund_money'], $out_refund_no);
        $data['refund_money'] > 0 && $order->save(['refund_money' => $order['refund_money'] + $data['refund_money']]);
        //退预售定金
        $advance_money > 0 && (new OrderRefundService)->execute($order['advance'], $advance_money);
        $advance_money > 0 && $order['advance']->save(['refund_money' => $advance_money]);
        // 发送模板消息
        (new MessageService)->refund(self::detail($this['order_refund_id']), $order['order_no'], 'receipt');
    }
}