提取后台orderservice
parent
95ebc75d5f
commit
1042bfcb23
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace App\Admin\Actions\Grid;
|
||||
|
||||
use App\Services\AdminSendCouponService;
|
||||
use App\Admin\Services\AdminSendCouponService;
|
||||
use Dcat\Admin\Actions\Response;
|
||||
use Dcat\Admin\Grid\RowAction;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Actions\Grid;
|
||||
|
||||
use App\Models\OrderPackage;
|
||||
use Dcat\Admin\Grid\RowAction;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class OrderPackageFailed extends RowAction
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected $title = '<i class="feather icon-eye-off grid-action-icon"></i>';
|
||||
|
||||
public function title()
|
||||
{
|
||||
if ($this->title) {
|
||||
return $this->title.' '.'作废';
|
||||
}
|
||||
|
||||
return '作废';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Model|Authenticatable|HasPermissions|null $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->can('dcat.admin.users.enable');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the action request.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function handle(Request $request)
|
||||
{
|
||||
$package = OrderPackage::findOrFail($this->getKey());
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$user->enable();
|
||||
DB::commit();
|
||||
} catch (Throwable $th) {
|
||||
DB::rollBack();
|
||||
report($th);
|
||||
return $this->response()->error('操作失败,'.$th->getMessage())->refresh();
|
||||
}
|
||||
|
||||
return $this->response()->success('操作成功')->refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array|void
|
||||
*/
|
||||
public function confirm()
|
||||
{
|
||||
return ['确认作废当前货运单?', '该操作不可逆,确认后将作废该货运单,且订单相应恢复。'];
|
||||
}
|
||||
}
|
||||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace App\Admin\Actions\Show;
|
||||
|
||||
use App\Admin\Services\AdminOrderService;
|
||||
use App\Models\Order;
|
||||
use App\Services\OrderService;
|
||||
use Dcat\Admin\Show\AbstractTool;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -48,7 +48,7 @@ class OrderPay extends AbstractTool
|
|||
// 获取主键
|
||||
$key = $this->getKey();
|
||||
|
||||
$orderService = new OrderService();
|
||||
$orderService = new AdminOrderService();
|
||||
$order = Order::where('status', Order::STATUS_PENDING)->findOrFail($key);
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
|
|
|||
|
|
@ -166,13 +166,12 @@ class OrderController extends AdminController
|
|||
return bcdiv($v, 100, 2);
|
||||
})->prepend('- ¥');
|
||||
}
|
||||
|
||||
$show->field('reduced_amount')->as(function ($v) {
|
||||
return bcdiv($v, 100, 2);
|
||||
})->prepend('- ¥');
|
||||
$show->field('shipping_fee')->as(function ($v) {
|
||||
return bcdiv($v, 100, 2);
|
||||
})->prepend('+ ¥');
|
||||
$show->field('reduced_amount')->as(function ($v) {
|
||||
return bcdiv($v, 100, 2);
|
||||
})->prepend('- ¥');
|
||||
$show->divider();
|
||||
$show->field('note');
|
||||
$show->field('remark');
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Admin\Services\AdminOrderService;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\Order;
|
||||
use App\Services\OrderService;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
|
|
@ -38,7 +38,7 @@ class OrderConsigneeInfo extends Form implements LazyRenderable
|
|||
{
|
||||
$orderId = $this->payload['id'] ?? 0;
|
||||
$order = Order::findOrFail($orderId);
|
||||
$orderService = new OrderService();
|
||||
$orderService = new AdminOrderService();
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$orderService->adminEditConsignee($order, $input);
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Admin\Services\AdminOrderService;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\Order;
|
||||
use App\Services\OrderService;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
|
|
@ -41,7 +41,7 @@ class OrderPackage extends Form implements LazyRenderable
|
|||
$order = Order::findOrFail($orderId);
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$orderService = new OrderService();
|
||||
$orderService = new AdminOrderService();
|
||||
$orderService->createPackage($order, $input);
|
||||
DB::commit();
|
||||
} catch (BizException $e) {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Admin\Services\AdminOrderService;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\Order;
|
||||
use App\Services\OrderService;
|
||||
use Dcat\Admin\Admin;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
|
|
@ -47,7 +47,7 @@ class OrderReduce extends Form implements LazyRenderable
|
|||
if ($reduceAmount == $order->total_amount) {
|
||||
throw new BizException('调整价格和订单原价格一致,无需调整');
|
||||
}
|
||||
$orderService = new OrderService();
|
||||
$orderService = new AdminOrderService();
|
||||
|
||||
$reduced = $order->total_amount-$reduceAmount;
|
||||
//判断是否在当前操作人调价权限范围内
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Admin\Services\AdminOrderService;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\Order;
|
||||
use App\Services\OrderService;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
|
|
@ -40,7 +40,7 @@ class OrderRemark extends Form implements LazyRenderable
|
|||
$order = Order::findOrFail($orderId);
|
||||
|
||||
$remark = $input['remark']??'';
|
||||
$orderService = new OrderService();
|
||||
$orderService = new AdminOrderService();
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$orderService->adminRemark($order, $remark);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Services;
|
||||
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderLog;
|
||||
use App\Models\OrderPackage;
|
||||
use App\Models\OrderPackageProduct;
|
||||
use App\Models\OrderProduct;
|
||||
use App\Services\Kuaidi100Service;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class AdminOrderService
|
||||
{
|
||||
/**
|
||||
* 订单调整价格
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function adminReduceOrder(Order $order, int $reduceAmount)
|
||||
{
|
||||
if ($order->isPending()) {
|
||||
$res = $order->where('updated_at', $order->updated_at)->update([
|
||||
'reduced_amount' => $order->total_amount - $reduceAmount + $order->reduced_amount,
|
||||
'total_amount' => $reduceAmount,
|
||||
]);
|
||||
if ($res === 0) {
|
||||
throw new BizException('订单已发生改变');
|
||||
}
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '调整订单支付价格为:¥'.bcdiv($reduceAmount, 100, 2),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台支付订单
|
||||
*
|
||||
* @param Order $order
|
||||
* @return void
|
||||
*/
|
||||
public function adminPay(Order $order)
|
||||
{
|
||||
if ($order->isPending()) {
|
||||
//操作订单状态-需要调整为统一支付方法
|
||||
$order->update([
|
||||
'status' => Order::STATUS_PAID,
|
||||
]);
|
||||
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '改变订单状态为【已支付】',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加后台备注
|
||||
*
|
||||
* @param Order $order
|
||||
* @param string $remark
|
||||
* @return void
|
||||
*/
|
||||
public function adminRemark(Order $order, string $remark)
|
||||
{
|
||||
//操作订单状态-需要调整为统一支付方法
|
||||
$order->update([
|
||||
'remark' => $remark,
|
||||
]);
|
||||
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '修改订单备注:'.$remark,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改订单收货信息
|
||||
*
|
||||
* @param Order $order
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public function adminEditConsignee(Order $order, array $params)
|
||||
{
|
||||
$oldOrderConsignee = $order->consignee_name.','.$order->consignee_telephone.','.$order->consignee_zone.$order->consignee_address;
|
||||
$order->update($params);
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '修改订单收货信息<br\>原收货信息:'.$oldOrderConsignee,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单发货单
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createPackage(Order $order, array $params)
|
||||
{
|
||||
//合并同样商品,并检验是否能发货
|
||||
$packageProducts = [];
|
||||
if (count($params['packages']) <= 0) {
|
||||
throw new BizException('请选择发货商品');
|
||||
}
|
||||
foreach ($params['packages'] as $item) {
|
||||
if ($item['quantity'] < 1) {
|
||||
throw new BizException('发货数量不能小于1');
|
||||
}
|
||||
if (isset($packageProducts[$item['order_product_id']])) {
|
||||
$packageProducts[$item['order_product_id']]['quantity'] += $item['quantity'];
|
||||
} else {
|
||||
$packageProducts[$item['order_product_id']] = [
|
||||
'order_product_id'=> $item['order_product_id'],
|
||||
'quantity' =>$item['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
foreach ($packageProducts as $product) {
|
||||
$_orderProduct = OrderProduct::where('after_sale_state', '<>', 1)
|
||||
->where('remain_quantity', '>=', $product['quantity'])
|
||||
->find($product['order_product_id']);
|
||||
if (is_null($_orderProduct)) {
|
||||
throw new BizException('发货数量不能大于剩余数量');
|
||||
}
|
||||
$_orderProduct->decrement('remain_quantity', $product['quantity']);
|
||||
}
|
||||
|
||||
//创建发货单数据
|
||||
$package = new OrderPackage();
|
||||
$package->order_id = $order->id;
|
||||
$package->consignee_name = $order->consignee_name;
|
||||
$package->consignee_telephone = $order->consignee_telephone;
|
||||
$package->consignee_zone = $order->consignee_zone;
|
||||
$package->consignee_address = $order->consignee_address;
|
||||
|
||||
$package->shipping_company = $params['shipping_company'];
|
||||
$package->shipping_code = Arr::get(Kuaidi100Service::$codeArr, $package->shipping_company, '');
|
||||
$package->shipping_number = $params['shipping_number'];
|
||||
|
||||
//保存发货单
|
||||
$package->save();
|
||||
//保存发货单商品
|
||||
OrderPackageProduct::insert(array_map(function ($value) use ($package) {
|
||||
return array_merge($value, [
|
||||
'order_package_id' => $package->id,
|
||||
'created_at' => $package->created_at,
|
||||
'updated_at' => $package->created_at,
|
||||
]);
|
||||
}, $packageProducts));
|
||||
if (config('settings.kuaidi100_is_use')) {
|
||||
$kuaidi100Service = new Kuaidi100Service();
|
||||
$kuaidi100Service->poll($package->shipping_number, $package->shipping_code, $package->consignee_telephone);
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
if (!OrderProduct::where('order_id', $order->id)->where('remain_quantity', '>', 0)->exists()) {
|
||||
$order->update([
|
||||
'shipping_state'=>Order::SHIPPING_STATE_PROCESSED,
|
||||
]);
|
||||
} elseif ($order->shipping_state == Order::SHIPPING_STATE_PENDING) {
|
||||
$order->update([
|
||||
'shipping_state'=>Order::SHIPPING_STATE_PROCESSING,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Admin\Services;
|
||||
|
||||
use App\Models\CouponSendTask;
|
||||
use App\Models\CouponTaskLog;
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -62,32 +61,13 @@ class OrderPackage extends Model
|
|||
return $this->hasMany(OrderPackageProduct::class, 'order_package_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联的订单商品
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function orderProducts()
|
||||
{
|
||||
return $this->belongsToMany(OrderProduct::class, 'order_package_products', 'order_package_id', 'order_product_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新包裹物流状态
|
||||
*/
|
||||
public function updatePackageStatus(int $status, ?Carbon $time = null)
|
||||
{
|
||||
//如果是签收或者自动签收,填入签收时间; 并填入包裹商品里面的售后有效期;
|
||||
$this->status = $status;
|
||||
|
||||
if ($status == self::STATUS_CHECK || $status == self::STATUS_AUTOCHECK) {
|
||||
$this->checked_at = $time ?? now();
|
||||
$this->orderProducts()->update([
|
||||
'after_expire_at'=>$this->checked_at->addDays(7),
|
||||
]);
|
||||
//看这个包裹的订单下是否还有未签收的包裹,没有,则把订单status改为已完成, 并完成订单
|
||||
if ($this->order->packages()->where('is_failed', false)->whereNotIn('status', [self::STATUS_CHECK, self::STATUS_AUTOCHECK])->doesntExist()) {
|
||||
$this->order()->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,14 @@ use App\Exceptions\ShippingNotSupportedException;
|
|||
use App\Helpers\Numeric;
|
||||
use App\Helpers\Order as OrderHelper;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderLog;
|
||||
use App\Models\OrderPackage;
|
||||
use App\Models\OrderPackageProduct;
|
||||
use App\Models\OrderProduct;
|
||||
use App\Models\ProductSku;
|
||||
use App\Models\ShippingAddress;
|
||||
use App\Models\User;
|
||||
use App\Models\UserCoupon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class OrderService
|
||||
|
|
@ -585,160 +584,6 @@ class OrderService
|
|||
return $user->shippingAddresses()->where('is_default', true)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单调整价格
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function adminReduceOrder(Order $order, int $reduceAmount)
|
||||
{
|
||||
if ($order->isPending()) {
|
||||
$res = $order->where('updated_at', $order->updated_at)->update([
|
||||
'reduced_amount' => $order->total_amount - $reduceAmount + $order->reduced_amount,
|
||||
'total_amount' => $reduceAmount,
|
||||
]);
|
||||
if ($res === 0) {
|
||||
throw new BizException('订单已发生改变');
|
||||
}
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '调整订单支付价格为:¥'.bcdiv($reduceAmount, 100, 2),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台支付订单
|
||||
*
|
||||
* @param Order $order
|
||||
* @return void
|
||||
*/
|
||||
public function adminPay(Order $order)
|
||||
{
|
||||
if ($order->isPending()) {
|
||||
//操作订单状态-需要调整为统一支付方法
|
||||
$order->update([
|
||||
'status' => Order::STATUS_PAID,
|
||||
]);
|
||||
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '改变订单状态为【已支付】',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加后台备注
|
||||
*
|
||||
* @param Order $order
|
||||
* @param string $remark
|
||||
* @return void
|
||||
*/
|
||||
public function adminRemark(Order $order, string $remark)
|
||||
{
|
||||
//操作订单状态-需要调整为统一支付方法
|
||||
$order->update([
|
||||
'remark' => $remark,
|
||||
]);
|
||||
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '修改订单备注:'.$remark,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改订单收货信息
|
||||
*
|
||||
* @param Order $order
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public function adminEditConsignee(Order $order, array $params)
|
||||
{
|
||||
$oldOrderConsignee = $order->consignee_name.','.$order->consignee_telephone.','.$order->consignee_zone.$order->consignee_address;
|
||||
$order->update($params);
|
||||
OrderLog::create([
|
||||
'order_id'=>$order->id,
|
||||
'content'=> '修改订单收货信息<br\>原收货信息:'.$oldOrderConsignee,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单发货单
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createPackage(Order $order, array $params)
|
||||
{
|
||||
//合并同样商品,并检验是否能发货
|
||||
$packageProducts = [];
|
||||
if (count($params['packages']) <= 0) {
|
||||
throw new BizException('请选择发货商品');
|
||||
}
|
||||
foreach ($params['packages'] as $item) {
|
||||
if ($item['quantity'] < 1) {
|
||||
throw new BizException('发货数量不能小于1');
|
||||
}
|
||||
if (isset($packageProducts[$item['order_product_id']])) {
|
||||
$packageProducts[$item['order_product_id']]['quantity'] += $item['quantity'];
|
||||
} else {
|
||||
$packageProducts[$item['order_product_id']] = [
|
||||
'order_product_id'=> $item['order_product_id'],
|
||||
'quantity' =>$item['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
foreach ($packageProducts as $product) {
|
||||
$_orderProduct = OrderProduct::where('after_sale_state', '<>', 1)
|
||||
->where('remain_quantity', '>=', $product['quantity'])
|
||||
->find($product['order_product_id']);
|
||||
if (is_null($_orderProduct)) {
|
||||
throw new BizException('发货数量不能大于剩余数量');
|
||||
}
|
||||
$_orderProduct->decrement('remain_quantity', $product['quantity']);
|
||||
}
|
||||
|
||||
//创建发货单数据
|
||||
$package = new OrderPackage();
|
||||
$package->order_id = $order->id;
|
||||
$package->consignee_name = $order->consignee_name;
|
||||
$package->consignee_telephone = $order->consignee_telephone;
|
||||
$package->consignee_zone = $order->consignee_zone;
|
||||
$package->consignee_address = $order->consignee_address;
|
||||
|
||||
$package->shipping_company = $params['shipping_company'];
|
||||
$package->shipping_code = Arr::get(Kuaidi100Service::$codeArr, $package->shipping_company, '');
|
||||
$package->shipping_number = $params['shipping_number'];
|
||||
|
||||
//保存发货单
|
||||
$package->save();
|
||||
//保存发货单商品
|
||||
OrderPackageProduct::insert(array_map(function ($value) use ($package) {
|
||||
return array_merge($value, [
|
||||
'order_package_id' => $package->id,
|
||||
'created_at' => $package->created_at,
|
||||
'updated_at' => $package->created_at,
|
||||
]);
|
||||
}, $packageProducts));
|
||||
if (config('settings.kuaidi100_is_use')) {
|
||||
$kuaidi100Service = new Kuaidi100Service();
|
||||
$kuaidi100Service->poll($package->shipping_number, $package->shipping_code, $package->consignee_telephone);
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
if (!OrderProduct::where('order_id', $order->id)->where('remain_quantity', '>', 0)->exists()) {
|
||||
$order->update([
|
||||
'shipping_state'=>Order::SHIPPING_STATE_PROCESSED,
|
||||
]);
|
||||
} elseif ($order->shipping_state == Order::SHIPPING_STATE_PENDING) {
|
||||
$order->update([
|
||||
'shipping_state'=>Order::SHIPPING_STATE_PROCESSING,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单付款
|
||||
*
|
||||
|
|
@ -770,4 +615,85 @@ class OrderService
|
|||
|
||||
throw new BizException('支付方式不支持');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新包裹状态
|
||||
*
|
||||
* @param Collection|null $packages
|
||||
* @param integer $status
|
||||
* @return void
|
||||
*/
|
||||
public function updatePackageStatus(?Collection $packages, int $status)
|
||||
{
|
||||
if ($packages instanceof OrderPackage) {
|
||||
$packages[] = $packages;
|
||||
$packages = collect($packages);
|
||||
}
|
||||
|
||||
//如果签收状态
|
||||
if ($status == OrderPackage::STATUS_CHECK || $status == OrderPackage::STATUS_AUTOCHECK) {
|
||||
$this->checkOrderPackage($packages, $status, now());
|
||||
} else {
|
||||
OrderPackage::whereIn('id', $packages->pluck('id')->toArray())->update([
|
||||
'status'=>$status,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 签收订单包裹
|
||||
*
|
||||
* status 区分3签收(物流推送签收)和11自动签收(属于用户手动点,或者时间到了)
|
||||
* @return void
|
||||
*/
|
||||
public function checkOrderPackage(?Collection $packages, int $status, ?Carbon $time = null)
|
||||
{
|
||||
$checkTime = $time ?? now();//没指定签收时间,则默认当前时间
|
||||
if ($packages instanceof OrderPackage) {
|
||||
$packages[] = $packages;
|
||||
$packages = collect($packages);
|
||||
}
|
||||
//物流签收,更新包裹状态。自动签收不更新包裹状态
|
||||
if ($status == OrderPackage::STATUS_CHECK) {
|
||||
OrderPackage::whereIn('id', $packages->pluck('id')->toArray())->update([
|
||||
'status'=> $status,
|
||||
'checked_at' => $checkTime,
|
||||
]);
|
||||
}
|
||||
|
||||
//按订单ID分组包裹
|
||||
$packages = $packages->groupBy('order_id');
|
||||
|
||||
//获取所有订单
|
||||
$orders = Order::whereIn('id', $packages->keys()->toArray())->get()->keyBy('id');
|
||||
|
||||
$checkedOrder = [];//已执行过订单状态更新的订单IDS
|
||||
//遍历包裹-处理签收
|
||||
foreach ($packages as $orderId => $orderPackages) {
|
||||
$_order = $orders[$orderId];
|
||||
//如果订单已完成,则不更新
|
||||
if ($_order->isCompleted()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//处理包裹下订单商品售后过期时间
|
||||
foreach ($orderPackages as $package) {
|
||||
$package->orderProducts()->update([
|
||||
'after_expire_at'=>$checkTime->addDays(7),
|
||||
]);
|
||||
}
|
||||
|
||||
//处理订单已完成发货,且包裹已全部签收,则更新为完成状态、记录完成时间;
|
||||
if (!in_array($_order->id, $checkedOrder)
|
||||
&& $_order->isShipped()
|
||||
&& $_order->packages()->where('is_failed', false)->whereNotIn('status', [OrderPackage::STATUS_CHECK, OrderPackage::STATUS_AUTOCHECK])->doesntExist()
|
||||
) {
|
||||
Order::where('id', $_order->id)->update([
|
||||
'status' => Order::STATUS_COMPLETED,
|
||||
'completed_at' => now(),
|
||||
]);
|
||||
$checkedOrder[] = $_order->id;//每个订单只更新执行一次
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue