OrderAftersale.php
11.7 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use addons\shopro\library\traits\model\order\OrderAftersaleScope;
use think\Db;
use traits\model\SoftDelete;
/**
* 订单售后单
*/
class OrderAftersale extends Model
{
use SoftDelete, OrderAftersaleScope;
// 表名,不含前缀
protected $name = 'shopro_order_aftersale';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['deletetime'];
protected $append = [
'type_text',
'dispatch_status_text',
'aftersale_status_text',
'aftersale_status_desc',
'btns',
'refund_status_text'
];
// 发货状态
const DISPATCH_STATUS_NOSEND = 0; // 未发货
const DISPATCH_STATUS_SENDED = 1; // 已发货
const DISPATCH_STATUS_GETED = 2; // 已收货
// 售后状态
const AFTERSALE_STATUS_CANCEL = -2; // 售后取消
const AFTERSALE_STATUS_REFUSE = -1; // 拒绝
const AFTERSALE_STATUS_NOOPER = 0; // 未处理
const AFTERSALE_STATUS_AFTERING = 1; // 处理中
const AFTERSALE_STATUS_OK = 2; // 售后完成
// 退款状态
const REFUND_STATUS_REFUSE = -1; // 拒绝退款(不用了)
const REFUND_STATUS_NOREFUND = 0; // 未退款
const REFUND_STATUS_FINISH = 1; // 同意
public static function getSn($user_id)
{
$rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
$order_sn = date('Yhis') . $rand;
$id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
return 'A' . $order_sn . $id;
}
// 获取售后列表
public static function getList($params) {
$user = User::info();
$type = $params['type'] ?? 'all';
$aftersale = (new self())->where('user_id', $user->id);
if ($type != 'all') {
$aftersale = $aftersale->{$type}();
}
$aftersale = $aftersale->order('id', 'desc')->paginate(10);
return $aftersale;
}
public static function detail($params) {
$user = User::info();
$id = $params['id'] ?? 0;
$aftersale = (new self())->where('user_id', $user->id)->with('logs')->where('id', $id)->find();
if (!$aftersale) {
throw new Exception('售后单不存在');
}
return $aftersale;
}
public static function aftersale($params) {
$user = User::info();
$type = $params['type'];
$order_id = $params['order_id'];
$order_item_id = $params['order_item_id'];
$phone = $params['phone'];
$reason = $params['reason'] ?? '用户申请售后';
$content = $params['content'] ?? '';
$images = $params['images'] ?? [];
$order = Order::canAftersale()->where('user_id', $user->id)->where('id', $order_id)->find();
if (!$order) {
throw new Exception('订单不存在或不可售后');
}
$item = OrderItem::where('user_id', $user->id)->where('id', $order_item_id)->find();
if (!$item) {
throw new Exception('参数错误');
}
if (!in_array($item->aftersale_status, [
OrderItem::AFTERSALE_STATUS_REFUSE,
OrderItem::AFTERSALE_STATUS_NOAFTER
])
) {
throw new Exception('当前订单商品不可申请售后');
}
$orderAftersale = Db::transaction(function () use (
$user,
$type,
$order,
$item,
$order_id,
$order_item_id,
$phone,
$reason,
$content,
$images
) {
$data['aftersale_sn'] = self::getSn($user->id);
$data['user_id'] = $user->id;
$data['type'] = $type;
$data['phone'] = $phone;
$data['order_id'] = $order_id;
$data['order_item_id'] = $order_item_id;
$data['goods_id'] = $item['goods_id'];
$data['goods_sku_price_id'] = $item['goods_sku_price_id'];
$data['goods_sku_text'] = $item['goods_sku_text'];
$data['goods_title'] = $item['goods_title'];
$data['goods_image'] = $item['goods_image'];
$data['goods_original_price'] = $item['goods_original_price'];
$data['discount_fee'] = $item['discount_fee'];
$data['goods_price'] = $item['goods_price'];
$data['goods_num'] = $item['goods_num'];
$data['dispatch_status'] = $item['dispatch_status'];
$data['dispatch_fee'] = $item['dispatch_fee'];
$data['aftersale_status'] = self::AFTERSALE_STATUS_NOOPER;
$data['refund_status'] = self::REFUND_STATUS_NOREFUND; // 未退款
$data['refund_fee'] = 0;
$orderAftersale = new self();
$orderAftersale->allowField(true)->save($data);
// 增加售后单变动记录、
OrderAftersaleLog::operAdd($order, $orderAftersale, $user, 'user', [
'reason' => '您的售后服务单已申请成功,等待售后处理',
'content' => "申请原因:$reason <br>相关描述: $content",
'images' => $images
]);
$ext = $item->ext_arr ? : [];
$ext['aftersale_id'] = $orderAftersale->id;
// 修改订单 item 状态,申请售后
$item->aftersale_status = OrderItem::AFTERSALE_STATUS_AFTERING;
$item->ext = json_encode($ext);
$item->save();
OrderAction::operAdd($order, $item, $user, 'user', '用户申请售后');
return $orderAftersale;
});
return $orderAftersale;
}
// 取消售后单
public static function operCancel($params)
{
$user = User::info();
extract($params);
$aftersale = self::canCancel()->where('user_id', $user->id)->where('id', $id)->find();
if (!$aftersale) {
throw new Exception('售后单不存在或不可取消');
}
$order = Order:: where('user_id', $user->id)->where('id', $aftersale['order_id'])->find();
if (!$order) {
throw new Exception('订单不存在');
}
$orderItem = OrderItem::where('id', $aftersale['order_item_id'])->find();
if (!$orderItem || in_array($orderItem['refund_status'], [OrderItem::REFUND_STATUS_OK, OrderItem::REFUND_STATUS_FINISH])) {
// 不存在, 或者已经退款
throw new Exception('退款商品不存在或已退款');
}
$aftersale = Db::transaction(function () use ($aftersale, $order, $orderItem, $user) {
$aftersale->aftersale_status = self::AFTERSALE_STATUS_CANCEL; // 取消售后单
$aftersale->save();
OrderAftersaleLog::operAdd($order, $aftersale, $user, 'user', [
'reason' => '用户取消申请售后',
'content' => '用户取消申请售后',
'images' => []
]);
// 修改订单 item 为未申请售后
$orderItem->aftersale_status = OrderItem::AFTERSALE_STATUS_NOAFTER;
$orderItem->refund_status = OrderItem::REFUND_STATUS_NOREFUND;
$orderItem->save();
OrderAction::operAdd($order, $orderItem, $user, 'user', '用户取消申请售后');
return $aftersale;
});
return $aftersale;
}
// 删除售后单
public static function operDelete($params)
{
$user = User::info();
extract($params);
$aftersale = self::canDelete()->where('user_id', $user->id)->where('id', $id)->find();
if (!$aftersale) {
throw new Exception('售后单不存在或不可删除');
}
$order = Order::withTrashed()->where('id', $aftersale['order_id'])->find();
$aftersale = Db::transaction(function () use ($aftersale, $order, $user) {
$copyAftersale = $aftersale->toArray();
$aftersale->delete(); // 删除售后单
OrderAftersaleLog::operAdd($order, $copyAftersale, $user, 'user', [
'reason' => '用户删除售后单',
'content' => '用户删除售后单',
'images' => []
]);
return $aftersale;
});
return $aftersale;
}
public function getTypeTextAttr($value, $data)
{
$text = '';
switch ($data['type']) {
case 'refund':
$text = '退款';
break;
case 'return':
$text = '退货';
break;
case 'other':
$text = '其他';
break;
}
return $text;
}
public function getDispatchStatusTextAttr($value, $data)
{
$text = '';
switch ($data['dispatch_status']) {
case self::DISPATCH_STATUS_NOSEND:
$text = '未发货';
break;
case self::DISPATCH_STATUS_SENDED:
$text = '已发货';
break;
case self::DISPATCH_STATUS_GETED:
$text = '已收货';
break;
}
return $text;
}
public function getRefundStatusTextAttr ($value, $data) {
$text = '';
switch ($data['refund_status']) {
case self::REFUND_STATUS_REFUSE:
$text = '拒绝退款';
break;
case self::REFUND_STATUS_NOREFUND:
$text = '未退款';
break;
case self::REFUND_STATUS_FINISH:
$text = '已退款';
break;
}
return $text;
}
public function getAftersaleStatusTextAttr($value, $data)
{
return $this->getStatus($data, 'status_text');
}
public function getAftersaleStatusDescAttr($value, $data)
{
return $this->getStatus($data, 'status_desc');
}
public function getBtnsAttr($value, $data)
{
return $this->getStatus($data, 'btns');
}
public function getStatus($data, $type) {
$text = '';
$desc = '';
$btns = [];
switch ($data['aftersale_status']) {
case self::AFTERSALE_STATUS_CANCEL:
$text = '售后取消';
$desc = '您取消了售后申请';
$btns = ['delete']; // 删除
break;
case self::AFTERSALE_STATUS_REFUSE:
$text = '售后拒绝';
$desc = '您的申请被拒绝,请点击查看详情';
$btns = ['delete']; // 删除
break;
case self::AFTERSALE_STATUS_NOOPER:
$text = '提交申请';
$desc = '您的服务单已申请成功,等待售后处理';
$btns = ['cancel']; // 取消申请
break;
case self::AFTERSALE_STATUS_AFTERING:
$text = '处理中';
$desc = '您的服务单正在处理中,请耐心等待';
$btns = ['cancel']; // 取消申请
break;
case self::AFTERSALE_STATUS_OK:
$text = '售后完成';
$desc = '服务已完成,感谢您的支持';
$btns = ['delete']; // 删除
break;
}
return $type == 'status_text' ? $text : ($type == 'status_desc' ? $desc : $btns);
}
public function logs ()
{
return $this->hasMany(\addons\shopro\model\OrderAftersaleLog::class, 'order_aftersale_id', 'id')->order('id', 'desc');
}
}