diff --git a/app/Admin/Actions/Show/AfterSaleFinanceShipping.php b/app/Admin/Actions/Show/AfterSaleFinanceShipping.php new file mode 100644 index 00000000..a2e38b35 --- /dev/null +++ b/app/Admin/Actions/Show/AfterSaleFinanceShipping.php @@ -0,0 +1,82 @@ + 确认发货'; + + /** + * 按钮样式定义,默认 btn btn-white waves-effect + * + * @var string + */ + protected $style = 'btn btn-sm btn-danger'; + + /** + * 权限判断,如不需要可以删除此方法 + * + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.after_sales.finance'); + } + + /** + * 处理请求,如果不需要接口处理,请直接删除这个方法 + * + * @param Request $request + * + * @return Response + */ + public function handle(Request $request) + { + // 获取主键 + $key = $this->getKey(); + + $afterSaleService = new AfterSaleService(); + try { + DB::beginTransaction(); + $afterSale = AfterSale::where('state', AfterSale::STATE_FINANCE)->findOrFail($key); + $afterSaleService->finance($afterSale); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + return $this->response()->error('操作失败:'.$th->getMessage()); + } + + return $this->response() + ->success(__('admin.update_succeeded')) + ->refresh(); + } + + public function html() + { + return parent::html().'  '; + } + + /** + * 确认弹窗信息,如不需要可以删除此方法 + * + * @return string|array|void + */ + public function confirm() + { + return ['是否确认发货?', '该操作不可逆,确认后将产生新的订单以及发货包裹单。']; + } +} diff --git a/app/Admin/Controllers/AfterSaleController.php b/app/Admin/Controllers/AfterSaleController.php index 725016de..22e57e22 100644 --- a/app/Admin/Controllers/AfterSaleController.php +++ b/app/Admin/Controllers/AfterSaleController.php @@ -3,6 +3,7 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Show\AfterSaleFinance; +use App\Admin\Actions\Show\AfterSaleFinanceShipping; use App\Admin\Actions\Show\AfterSaleShipping; use App\Admin\Actions\Show\AfterSaleVerify; use App\Admin\Repositories\AfterSale; @@ -31,7 +32,12 @@ class AfterSaleController extends AdminController $grid->column('sn'); $grid->column('orderProduct.name'); $grid->column('num'); - $grid->column('amount'); + $grid->column('amount')->display(function ($value) { + if (in_array($this->type, [AfterSaleModel::TYPE_CHANGE, AfterSaleModel::TYPE_FILL])) { + return '0.00'; + } + return bcdiv($value, 100, 2); + })->prepend('¥'); $grid->column('type')->using([ AfterSaleModel::TYPE_REFUND_AND_RETURN => '退款退货', AfterSaleModel::TYPE_REFUND => '退款', @@ -100,6 +106,11 @@ class AfterSaleController extends AdminController $show->field('order.sn'); $show->field('order_product.name'); $show->field('num'); + if (in_array($show->model()->type, [AfterSaleModel::TYPE_REFUND_AND_RETURN, AfterSaleModel::TYPE_REFUND])) { + $show->field('amount')->as(function ($amount) { + return '¥'.bcdiv($amount, 100, 2); + }); + } $show->field('type')->using([ AfterSaleModel::TYPE_REFUND_AND_RETURN => '退款退货', AfterSaleModel::TYPE_REFUND => '退款', @@ -136,7 +147,11 @@ class AfterSaleController extends AdminController $tools->append(new AfterSaleShipping()); } if ($show->model()->state == AfterSaleModel::STATE_FINANCE) { - $tools->append(new AfterSaleFinance()); + if (in_array($show->model()->type, [AfterSaleModel::TYPE_REFUND_AND_RETURN, AfterSaleModel::TYPE_REFUND])) { + $tools->append(new AfterSaleFinance()); + } elseif (in_array($show->model()->type, [AfterSaleModel::TYPE_CHANGE])) { + $tools->append(new AfterSaleFinanceShipping()); + } } }); })); diff --git a/app/Admin/Forms/AfterSaleVerify.php b/app/Admin/Forms/AfterSaleVerify.php index adc9db74..4f4740ac 100644 --- a/app/Admin/Forms/AfterSaleVerify.php +++ b/app/Admin/Forms/AfterSaleVerify.php @@ -40,7 +40,7 @@ class AfterSaleVerify extends Form implements LazyRenderable DB::beginTransaction(); $afterSale = AfterSale::where('state', AfterSale::STATE_VERIFY)->findOrFail($this->payload['id']); if ($input['state'] == 3) {//审核通过 - $afterSaleService->verify($afterSale, $input['remarks3']); + $afterSaleService->verify($afterSale, $input['remarks3'], (int) $input['amount']); } elseif ($input['state'] == 1) {//需要补充资料 $afterSaleService->backApply($afterSale, $input['remarks1']); } @@ -63,8 +63,12 @@ class AfterSaleVerify extends Form implements LazyRenderable { $id = $this->payload['id'] ?? 0; $afterSale = AfterSale::findOrFail($id); - if (!in_array($afterSale->type, [3, 4])) { - $this->currency('amount')->symbol('¥')->value($afterSale->amount); + if (in_array($afterSale->type, [1, 2])) { + $this->currency('amount')->customFormat(function () use ($afterSale) { + return bcdiv($afterSale->amount, 100, 2); + })->saving(function ($amount) { + return bcmul($amount, 100); + })->symbol('¥'); } $this->radio('state') @@ -72,13 +76,13 @@ class AfterSaleVerify extends Form implements LazyRenderable $defaultRemarks = ''; switch ($afterSale->type) { case 1: - $defaultRemarks = '同意退款退货';//需要用户确认并填入回寄单号 + $defaultRemarks = '同意退款退货, 请确认退款金额,并填入回寄单号';//需要用户确认并填入回寄单号 break; case 2: - $defaultRemarks = '同意退款,等待财务打款审核。'; + $defaultRemarks = '同意退款,请确认退款金额。'; break; case 3: - $defaultRemarks = '同意换货';//需要用户确认并填入回寄单号 + $defaultRemarks = '同意换货,请填入回寄单号';//需要用户确认并填入回寄单号 break; case 4: $defaultRemarks = '同意补货, 新的订单号:';//需要用户收到补货后确认,可后台关闭 diff --git a/app/Endpoint/Api/Http/Controllers/AfterSaleController.php b/app/Endpoint/Api/Http/Controllers/AfterSaleController.php index 6d684236..34770ceb 100644 --- a/app/Endpoint/Api/Http/Controllers/AfterSaleController.php +++ b/app/Endpoint/Api/Http/Controllers/AfterSaleController.php @@ -64,6 +64,9 @@ class AfterSaleController extends Controller $afterSale = $afterSaleService->create($request->user(), $orderProduct, $input['type'], $input['num'], $input); DB::commit(); + } catch (BizException $th) { + DB::rollBack(); + throw new BizException($th->getMessage()); } catch (Throwable $th) { DB::rollBack(); report($th); diff --git a/app/Models/AfterSale.php b/app/Models/AfterSale.php index 6fdaf0ea..62df1830 100644 --- a/app/Models/AfterSale.php +++ b/app/Models/AfterSale.php @@ -29,7 +29,7 @@ class AfterSale extends Model protected $casts = [ 'images' => JsonArray::class, - 'amount' => Price::class, + // 'amount' => Price::class, ]; protected $fillable = [ diff --git a/app/Services/AfterSaleService.php b/app/Services/AfterSaleService.php index b91075ad..9eea528d 100644 --- a/app/Services/AfterSaleService.php +++ b/app/Services/AfterSaleService.php @@ -28,7 +28,7 @@ class AfterSaleService // } //校验该订单商品不能有已完成、处理中的售后记录 - if (AfterSale::where('order_product_id', $orderProduct->id)->where('state', '<', AfterSale::STATE_FINISH)->exists()) { + if (AfterSale::where('order_product_id', $orderProduct->id)->where('state', '<', AfterSale::STATE_CANCEL)->exists()) { throw new BizException('该订单商品已申请售后'); } //校验申请数量不能超过订单商品数量 @@ -36,18 +36,8 @@ class AfterSaleService throw new BizException('申请售后的数量不能大于订单商品数量'); } - // switch ($type) { - // case 3: - // $amount = 0; - // break; - // case 4: - // $amount = 0; - // break; - // default: - // dd(bcmul(bcdiv(10, 3, 2), 3, 2));如果数量相等,则直接取总额 - $amount = ($num == $orderProduct->quantity) ? $orderProduct->total_amount : bcmul(bcdiv($orderProduct->total_amount, $orderProduct->quantity, 2), $num, 2); - // break; - // } + $amount = ($num == $orderProduct->quantity) ? $orderProduct->total_amount : bcmul(bcdiv($orderProduct->total_amount, $orderProduct->quantity), $num); + $afterSale = AfterSale::create(array_merge($params, [ 'user_id' => $user->id, 'order_id' => $orderProduct->order_id, @@ -145,12 +135,13 @@ class AfterSaleService * @param string $remarks * @return void */ - public function verify(AfterSale $afterSale, string $remarks) + public function verify(AfterSale $afterSale, string $remarks, int $amount) { if ($this->isWaitVerify($afterSale)) { switch ($afterSale->type) { case AfterSale::TYPE_REFUND_AND_RETURN: $afterSale->update([ + 'amount' => $amount, 'state' => $afterSale::STATE_AGREE, 'remarks' => $remarks, ]); @@ -162,6 +153,7 @@ class AfterSaleService break; case AfterSale::TYPE_REFUND: $afterSale->update([ + 'amount' => $amount, 'state' => $afterSale::STATE_AGREE, 'remarks' => $remarks, ]); @@ -242,10 +234,9 @@ class AfterSaleService break; case AfterSale::TYPE_CHANGE: $afterSale->update([ - 'remarks' => $remarks.',已为您生成新订单,请注意查收。', - 'state' => AfterSale::STATE_FINISH, + 'remarks' => $remarks, + 'state' => AfterSale::STATE_SHIPPING, ]); - //todo - 生成新订单以及发货单; break; case AfterSale::TYPE_FILL: $afterSale->update([ @@ -297,8 +288,11 @@ class AfterSaleService public function finance(AfterSale $afterSale, $remarks ='退款成功') { if ($this->isWaitFinance($afterSale)) { - - //todo-执行实际退款操作; + if (in_array($afterSale->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND])) { + //todo-执行实际退款操作; + } elseif (in_array($afterSale->type, [AfterSale::TYPE_CHANGE])) {//换货流程 + //todo-执行生成新的订单,发货单操作; + } AfterSaleLog::create([ 'after_sale_id' => $afterSale->id,