115 lines
4.0 KiB
PHP
115 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Admin\Services\OrderPackageService;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\AfterSale;
|
|
use App\Models\Order;
|
|
use App\Models\OrderPackageProduct;
|
|
use App\Services\AfterSaleService;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class AfterSaleShippingFill extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* 权限判断,如不需要可以删除此方法
|
|
*
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.after_sales.shipping');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$afterSaleService = new AfterSaleService();
|
|
try {
|
|
DB::beginTransaction();
|
|
$afterSale = AfterSale::where('state', AfterSale::STATE_SHIPPING)->findOrFail($this->payload['id']);
|
|
//处理售后
|
|
$afterSaleService->shippingFill($afterSale, $input['shipping_number']);
|
|
/** 处理补发逻辑 **/
|
|
//获取售后商品数量
|
|
$fillQuantity = $afterSale->num;
|
|
$packageProduct = OrderPackageProduct::where('order_product_id', $afterSale->order_product_id)->where('quantity', '>=', $fillQuantity)->first();
|
|
if (!$packageProduct) {
|
|
throw new BizException('补发包裹未找到或者售后补发数量异常');
|
|
}
|
|
//调整已发货包裹商品数量
|
|
$packageProduct->decrement('quantity', $fillQuantity);
|
|
//回滚对应订单商品待发货数量;
|
|
$packageProduct->orderProduct->increment('remain_quantity', $fillQuantity);
|
|
//回滚订单状态
|
|
if ($afterSale->order->isCompleted()) {
|
|
$afterSale->order->update([
|
|
'status'=>Order::STATUS_PAID,
|
|
'shipping_state'=>Order::SHIPPING_STATE_PROCESSING,
|
|
'completed_at'=>null,
|
|
]);
|
|
} elseif ($afterSale->order->isShipped()) {
|
|
$afterSale->order->update([
|
|
'shipping_state'=>Order::SHIPPING_STATE_PROCESSING,
|
|
]);
|
|
}
|
|
//自动帮他生成新的发货单
|
|
$orderPackageService = new OrderPackageService();
|
|
$orderPackageService->createPackage($afterSale->order, [
|
|
'shipping_company'=>$input['shipping_company'],
|
|
'shipping_number'=>$input['shipping_number'],
|
|
'packages'=>[
|
|
[
|
|
'order_product_id'=>$afterSale->order_product_id,
|
|
'quantity' =>$fillQuantity,
|
|
],
|
|
],
|
|
]);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
return $this->response()->error('操作失败:'.$th->getMessage());
|
|
}
|
|
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$this->select('shipping_company', '快递公司')->options([
|
|
'圆通速递'=>'圆通速递',
|
|
'韵达快递'=>'韵达快递',
|
|
'中通快递'=>'中通快递',
|
|
'申通快递'=>'申通快递',
|
|
'百世快递'=>'百世快递',
|
|
'EMS'=>'EMS',
|
|
'顺丰速运'=>'顺丰速运',
|
|
'德邦快递'=>'德邦快递',
|
|
])->default('韵达快递')->required();
|
|
$this->text('shipping_number', '快递单号')->required();
|
|
// $this->text('remarks', '填写补货物流单号')->required();
|
|
$this->confirm('是否确认补货完成', '该操作不可逆,确认后将完成该售后单。');
|
|
}
|
|
}
|