Aftersale.php
21.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
namespace app\admin\controller\shopro\order;
use addons\shopro\library\Export;
use addons\shopro\model\OrderAftersaleLog;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\order\Aftersale as OrderAftersale;
use app\common\controller\Backend;
use think\db;
/**
* 订单商品明细
*
* @icon fa fa-circle-o
*/
class Aftersale extends Backend
{
/**
* Aftersale模型对象
* @var \app\admin\model\shopro\order\Aftersale
*/
protected $model = null;
protected $orderModel = null;
public function _initialize()
{
parent::_initialize();
// 手动加载语言包
$this->loadlang('shopro/order/order');
$this->model = new \app\admin\model\shopro\order\Aftersale;
$this->orderModel = new \app\admin\model\shopro\order\Order;
$this->view->assign("dispatchStatusList", $this->model->getDispatchStatusList());
$this->view->assign("aftersaleStatusList", $this->model->getAftersaleStatusList());
$this->view->assign("refundStatusList", $this->model->getRefundStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
// list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
$order = $this->request->get("order", "DESC");
$offset = $this->request->get("offset", 0);
$limit = $this->request->get("limit", 0);
$search = $this->request->get("search", ''); // 关键字
$status = $this->request->get("status", 'all');
$total = $this->buildSearchOrder() // 返回的是 orderModel
->order($sort, $order)
->count();
$list = $this->buildSearchOrder() // 返回的是 orderModel
->with(['aftersale' => function ($query) {
$query->removeOption('soft_delete')->alias('a')->field('a.*, u.nickname as user_nickname, u.mobile as user_mobile')->join('user u', 'u.id = a.user_id', 'LEFT');
}])
->order($sort, $order)
->limit($offset, $limit)
->select();
// foreach ($list as $row) {
// $row->visible(['id','aftersale_sn','user_id','type','order_id','order_item_id','goods_id','goods_sku_price_id','goods_sku_text','goods_title','goods_image','goods_original_price','discount_fee','goods_price','goods_num','dispatch_status','dispatch_fee','aftersale_status','refund_status','refund_fee','createtime','updatetime','deletetime', 'user', 'activity_type']);
// }
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return $this->success('获取成功', null, $result);
}
return $this->view->fetch();
}
/**
* 详情
*/
public function detail($id)
{
if ($this->request->isAjax()) {
$row = $this->model->withTrashed()->with(['user', 'order' => function($query) {
$query->removeOption('soft_delete');
}, 'logs'])->where('id', $id)->find();
if (!$row) {
$this->error(__('No Results were found'));
}
$result = [
'detail' => $row,
'logs' => $row->logs,
'order' => $row->order,
];
return $this->success('获取成功', null, $result);
}
$this->assignconfig('id', $id);
return $this->view->fetch();
}
/**
* 完成售后
*/
public function finish($id)
{
if ($this->request->isAjax()) {
$aftersale = $this->model->withTrashed()->canOper()->where('id', $id)->find();
if (!$aftersale) {
$this->error('售后单不存在或不可完成');
}
$order = Order::withTrashed()->where('id', $aftersale->order_id)->find();
$orderItem = OrderItem::where('id', $aftersale->order_item_id)->find();
if (!$order || !$orderItem) {
$this->error('订单或订单商品不存在');
}
Db::transaction(function () use ($aftersale, $order, $orderItem) {
// 售后单完成前
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_finish_before', $data);
$aftersale->aftersale_status = \app\admin\model\shopro\order\Aftersale::AFTERSALE_STATUS_OK; // 售后完成
$aftersale->save();
// 增加售后单变动记录、
OrderAftersaleLog::operAdd($order, $aftersale, $this->auth->getUserInfo(), 'admin', [
'reason' => '卖家完成售后',
'content' => '售后订单已完成',
'images' => []
]);
$orderItem->aftersale_status = OrderItem::AFTERSALE_STATUS_OK;
$orderItem->save();
\addons\shopro\model\OrderAction::operAdd($order, $orderItem, $this->auth->getUserInfo(), 'admin', '管理员完成售后');
// 售后单完成之后
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_finish_after', $data);
});
return $this->success('操作成功');
}
}
/**
* 拒绝售后
*/
public function refuse($id = 0)
{
if ($this->request->isAjax()) {
$refuse_msg = $this->request->post('refuse_msg', '');
if (!$refuse_msg) {
$this->error('请输入拒绝原因');
}
$aftersale = $this->model->withTrashed()->canOper()->where('id', $id)->find();
if (!$aftersale) {
$this->error('售后单不存在或不可拒绝');
}
$order = Order::withTrashed()->where('id', $aftersale->order_id)->find();
$orderItem = OrderItem::where('id', $aftersale->order_item_id)->find();
if (!$order || !$orderItem) {
$this->error('订单或订单商品不存在');
}
Db::transaction(function () use ($aftersale, $order, $orderItem, $refuse_msg) {
// 售后单拒绝前
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_refuse_before', $data);
$aftersale->aftersale_status = \app\admin\model\shopro\order\Aftersale::AFTERSALE_STATUS_REFUSE; // 售后拒绝
$aftersale->save();
// 增加售后单变动记录
OrderAftersaleLog::operAdd($order, $aftersale, $this->auth->getUserInfo(), 'admin', [
'reason' => '卖家拒绝售后',
'content' => $refuse_msg,
'images' => []
]);
$orderItem->aftersale_status = OrderItem::AFTERSALE_STATUS_REFUSE; // 拒绝售后
$orderItem->save();
\addons\shopro\model\OrderAction::operAdd($order, $orderItem, $this->auth->getUserInfo(), 'admin', '管理员拒绝订单售后:' . $refuse_msg);
// 售后单拒绝后
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_refuse_after', $data);
});
return $this->success('操作成功');
}
}
/**
* 同意退款
*/
public function refund($id = 0)
{
if ($this->request->isAjax()) {
$refund_money = round($this->request->post('refund_money', 0), 2);
if ($refund_money <= 0) {
$this->error('退款金额必须大于 0');
}
$aftersale = $this->model->withTrashed()->canOper()->where('id', $id)->find();
if (!$aftersale) {
$this->error('售后单不存在或不可退款');
}
$order = Order::withTrashed()->with('item')->where('id', $aftersale->order_id)->find();
if (!$order) {
$this->error('订单不存在');
}
$items = $order->item;
$items = array_column($items, null, 'id');
// 当前订单已退款总金额
$refunded_money = array_sum(array_column($items, 'refund_fee'));
// 剩余可退款金额
$refund_surplus_money = $order->pay_fee - $refunded_money;
// 如果退款金额大于订单支付总金额
if ($refund_money > $refund_surplus_money) {
$this->error('退款总金额不能大于实际支付金额');
}
$orderItem = $items[$aftersale['order_item_id']];
if (!$orderItem || in_array($orderItem['refund_status'], [
\app\admin\model\shopro\order\OrderItem::REFUND_STATUS_OK,
\app\admin\model\shopro\order\OrderItem::REFUND_STATUS_FINISH,
])) {
$this->error('订单商品已退款,不能重复退款');
}
Db::transaction(function () use ($aftersale, $order, $orderItem, $refund_money, $refund_surplus_money) {
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_finish_before', $data);
$aftersale->aftersale_status = \app\admin\model\shopro\order\Aftersale::AFTERSALE_STATUS_OK; // 售后同意
$aftersale->refund_status = \app\admin\model\shopro\order\Aftersale::REFUND_STATUS_FINISH; // 售后同意退款
$aftersale->refund_fee = $refund_money; // 退款金额
$aftersale->save();
// 增加售后单变动记录
OrderAftersaleLog::operAdd($order, $aftersale, $this->auth->getUserInfo(), 'admin', [
'reason' => '卖家同意退款',
'content' => '售后订单已退款',
'images' => []
]);
$orderItem->aftersale_status = OrderItem::AFTERSALE_STATUS_OK;
$orderItem->save();
\addons\shopro\model\OrderAction::operAdd($order, $orderItem, $this->auth->getUserInfo(), 'admin', '管理员同意售后退款');
// 退款
\app\admin\model\shopro\order\Order::startRefund($order, $orderItem, $refund_money, $this->auth->getUserInfo(), '管理员同意售后退款');
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
\think\Hook::listen('aftersale_finish_after', $data);
});
return $this->success('操作成功');
}
}
/**
* 留言
*/
public function addLog($id = 0)
{
if ($this->request->isAjax()) {
$reason = $this->request->post('reason', '卖家留言');
$content = $this->request->post('content', '');
$images = $this->request->post('images', []);
if (!$content) {
$this->error('留言内容不能为空');
}
$aftersale = $this->model->withTrashed()->where('id', $id)->find();
if (!$aftersale) {
$this->error('售后单不存在');
}
$order = Order::withTrashed()->with('item')->where('id', $aftersale->order_id)->find();
if (!$order) {
$this->error('订单不存在');
}
Db::transaction(function () use ($order, $aftersale, $reason, $content, $images) {
if ($aftersale['aftersale_status'] == 0) {
$aftersale->aftersale_status = \app\admin\model\shopro\order\Aftersale::AFTERSALE_STATUS_AFTERING; // 售后处理中
$aftersale->save();
}
// 增加售后单变动记录
OrderAftersaleLog::operAdd($order, $aftersale, $this->auth->getUserInfo(), 'admin', [
'reason' => $reason,
'content' => $content,
'images' => $images
]);
});
return $this->success('操作成功');
}
}
private function buildSearchOrder() {
$search = $this->request->get("search", ''); // 关键字
$status = $this->request->get("status", 'all');
$createtime = $this->request->get("createtime/a");
$orders = $this->orderModel->withTrashed();
$orders = $orders->whereExists(function ($query) use ($search, $status, $createtime) {
extract($this->getModelTable());
$aftersales = $query->table($aftersaleName)->where($aftersaleName . '.order_id=' . $orderName . '.id');
$aftersales = $this->aftersaleSearch($aftersales, $search, $status, $createtime);
return $aftersales;
});
return $orders;
}
private function aftersaleSearch($aftersales, $search, $status, $createtime) {
extract($this->getModelTable());
if ($search) {
// 模糊搜索字段
$searcharr = ['goods_title', 'goods_sku_text', 'aftersale_sn'];
foreach ($searcharr as $k => &$v) {
$v = stripos($v, ".") === false ? $aftersaleName . '.' . $v : $v;
}
unset($v);
$aftersales = $aftersales->where(function ($query) use ($searcharr, $search, $aftersaleName) {
$query->where(implode("|", $searcharr), "LIKE", "%{$search}%")
->whereOr(function ($query) use ($search, $aftersaleName) { // 用户
$query->whereExists(function ($query) use ($search, $aftersaleName) {
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
$query->table($userTableName)->where($userTableName . '.id=' . $aftersaleName . '.user_id')
->where(function ($query) use ($search) {
$query->where('nickname', 'like', "%{$search}%")
->whereOr('mobile', 'like', "%{$search}%");
});
});
});
});
}
// 售后单状态
if ($status != 'all') {
if (in_array($status, ['cancel', 'refuse', 'nooper', 'ing', 'finish'])) {
$aftersales = $aftersales->where(OrderAftersale::getScopeWhere($status));
}
}
// 申请时间
if ($createtime && array_filter($createtime)) {
$sym = 'BETWEEN';
if ($createtime[0] === '') {
$sym = '<=';
$createtime = $createtime[1];
} elseif ($createtime[1] === '') {
$sym = '>=';
$createtime = $createtime[0];
}
$aftersales = $aftersales->where('createtime',$sym . ' TIME',$createtime);
}
// 经销商
$dealer = [];
$group_id_arr = array_column($this->auth->getGroups(), 'group_id');
if(!in_array(1,$group_id_arr) && in_array(2,$group_id_arr)){
$dealer = \app\admin\model\dealer\Dealer::where('admin_id',$this->auth->id)->find();
}
if($dealer){
$aftersales = $aftersales->whereExists(function ($query) use ($dealer, $aftersaleName) {
$orderItemName = (new \app\admin\model\shopro\order\OrderItem())->getQuery()->getTable();
$query->table($orderItemName)->where($orderItemName . '.id=' . $aftersaleName . '.order_item_id')
->where('dealer_id', $dealer['id']);
});
}
return $aftersales;
}
private function getModelTable () {
$orderName = $this->orderModel->getQuery()->getTable();
$aftersaleName = $this->model->getQuery()->getTable();
return compact("orderName", "aftersaleName");
}
/**
* 组合搜索 废弃
*/
private function buildSearchOrderOld()
{
$search = $this->request->get("search", ''); // 关键字
$status = $this->request->get("status", 'all');
$name = $this->model->getQuery()->getTable();
$tableName = $name . '.';
$aftersales = $this->model->withTrashed();
if ($search) {
// 模糊搜索字段
$searcharr = ['goods_title', 'goods_sku_text', 'aftersale_sn'];
foreach ($searcharr as $k => &$v) {
$v = stripos($v, ".") === false ? $tableName . $v : $v;
}
unset($v);
$aftersales = $aftersales->where(function ($query) use ($searcharr, $search, $tableName) {
$query->where(implode("|", $searcharr), "LIKE", "%{$search}%")
->whereOr(function ($query) use ($search, $tableName) { // 用户
$query->whereExists(function ($query) use ($search, $tableName) {
$userTableName = (new \app\admin\model\User())->getQuery()->getTable();
$query->table($userTableName)->where($userTableName . '.id=' . $tableName . 'user_id')
->where(function ($query) use ($search) {
$query->where('nickname', 'like', "%{$search}%")
->whereOr('mobile', 'like', "%{$search}%");
});
});
});
});
}
// 售后单状态
if ($status != 'all') {
if (in_array($status, ['cancel', 'refuse', 'nooper', 'ing', 'finish'])) {
$aftersales = $aftersales->{$status}();
}
}
return $aftersales;
}
public function export()
{
$export = new Export();
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
$order = $this->request->get("order", "DESC");
$offset = $this->request->get("offset", 0);
$limit = $this->request->get("limit", 0);
$search = $this->request->get("search", ''); // 关键字
$status = $this->request->get("status", 'all');
$list = $this->buildSearchOrder() // 返回的是 orderModel
->with(['aftersale' => function ($query) {
$query->removeOption('soft_delete')->alias('a')->field('a.*, u.nickname as user_nickname, u.mobile as user_mobile')->join('user u', 'u.id = a.user_id', 'LEFT');
}])
->order($sort, $order)
->select();
// 经销商
$dealer = [];
$group_id_arr = array_column($this->auth->getGroups(), 'group_id');
if(!in_array(1,$group_id_arr) && in_array(2,$group_id_arr)){
$dealer = \app\admin\model\dealer\Dealer::where('admin_id',$this->auth->id)->find();
}
$expCellName = [
'aftersale_id' => 'Id',
'order_sn' => '订单号',
'goods_title' => '商品名称',
'goods_sku_text' => '规格',
'activity_type_text' => '订单来源',
'user_info' => '用户信息',
'aftersale_sn' => '售后单号',
'refund_fee' => '退款金额',
'aftersale_status_text' => '处理状态',
'type_text' => '售后类型',
'createtime' => '申请时间',
];
$newList = [];
foreach ($list as $key => $order) {
foreach ($order['aftersale'] as $k => $after) {
$data = [
'aftersale_id' => $after['id'],
'order_sn' => $order['order_sn'],
'goods_title' => $after['goods_title'],
'goods_sku_text' => $after['goods_sku_text'],
'activity_type_text' => $after['activity_type_text'],
'user_info' => $after['user_nickname'] ? $after['user_nickname'] : '',
'aftersale_sn' => $after['aftersale_sn'],
'refund_fee' => $after['refund_fee'],
'aftersale_status_text' => $after['aftersale_status_text'],
'type_text' => $after['type_text'],
'createtime' => date('Y-m-d H:i:s',$after['createtime']),
];
$newList[] = $data;
}
}
// 增加合计
$newList[] = [
'aftersale_id' => "售后总数:" . count($newList) . ";退款总金额:¥" . array_sum(array_column($newList, 'refund_fee'))
];
$export->exportExcel('售后/退款列表-' . date('Y-m-d H:i:s'), $expCellName, $newList);
}
}