6
0
Fork 0

确认订单无法发起售后

release
李静 2022-02-14 13:59:59 +08:00
parent 7be71e7a6e
commit 9a119e311e
9 changed files with 120 additions and 34 deletions

View File

@ -34,15 +34,27 @@ class OrderSettleCommand extends Command
public function handle() public function handle()
{ {
while (true) { while (true) {
$page = 0; Order::where(
'completed_at',
'<=',
now()->subDays(app_settings('distribution.settle_days', 7))
)->where([
'status' => Order::STATUS_COMPLETED,
'is_settlable' => false,
'is_settle' => false,
])->chunkById(200, function ($orders) {
foreach ($orders as $order) {
$order->update([
'is_settlable' => true,
]);
}
});
// 只查询可结算的订单,并且没有处理中的售后单
// 检查订单是否有未执行的分销任务
Order::whereDoesntHave('afterSales', function ($query) { Order::whereDoesntHave('afterSales', function ($query) {
return $query->processing(); return $query->processing();
})->whereDoesntHave('distributionPreIncomeJobs', function ($query) { })->whereDoesntHave('distributionPreIncomeJobs', function ($query) {
return $query->pending(); return $query->pending();
})->settlable()->chunkById(200, function ($orders) use (&$page) { })->settlable()->chunkById(200, function ($orders) {
$orders->load(['user', 'afterSales']); $orders->load(['user', 'afterSales']);
foreach ($orders as $order) { foreach ($orders as $order) {
@ -58,17 +70,9 @@ class OrderSettleCommand extends Command
report($e); report($e);
} }
} }
$page++;
}); });
if ($page === 0) { sleep(60);
sleep(60);
} elseif ($page === 1) {
sleep(30);
} else {
sleep(15);
}
} }
} }

View File

@ -136,7 +136,7 @@ class OrderController extends Controller
DB::transaction(function () use ($id, $user) { DB::transaction(function () use ($id, $user) {
$order = $user->orders()->lockForUpdate()->findOrFail($id); $order = $user->orders()->lockForUpdate()->findOrFail($id);
(new OrderService())->confirm($order); (new OrderService())->confirm($order, true);
}); });
return response()->noContent(); return response()->noContent();

View File

@ -0,0 +1,30 @@
<?php
namespace App\Endpoint\Api\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class OrderProductSimpleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'id' => $this->id,
'sku_id' => $this->sku_id,
'name' => $this->name,
'cover' => $this->cover,
'specs' => array_values((array) $this->specs),
'sell_price' => $this->sell_price_format,
'vip_price' => $this->vip_price_format,
'total_amount' => $this->total_amount,
'quantity' => $this->quantity,
'is_gift' => $this->isGift(),
];
}
}

View File

@ -14,6 +14,12 @@ class OrderResource extends JsonResource
*/ */
public function toArray($request) public function toArray($request)
{ {
if ($this->resource->relationLoaded('products')) {
foreach ($this->resource->products as $product) {
$product->setRelation('order', $this->resource);
}
}
return [ return [
'id' => $this->id, 'id' => $this->id,
'sn' => $this->sn, 'sn' => $this->sn,

View File

@ -21,7 +21,7 @@ class OrderResourceCollection extends ResourceCollection
'total_amount' => $item->total_amount_format, 'total_amount' => $item->total_amount_format,
'status' => $item->order_status, 'status' => $item->order_status,
'created_date' => $item->created_at->toDateString(), 'created_date' => $item->created_at->toDateString(),
'products' => OrderProductResource::collection($item->whenLoaded('products')), 'products' => OrderProductSimpleResource::collection($item->whenLoaded('products')),
'expires_at' => $item->expires_at, 'expires_at' => $item->expires_at,
]; ];
})->toArray(); })->toArray();

View File

@ -44,6 +44,7 @@ class Order extends Model
'is_settle' => false, 'is_settle' => false,
'is_change' => false, 'is_change' => false,
'status' => self::STATUS_PENDING, 'status' => self::STATUS_PENDING,
'is_settlable' => false,
]; ];
/** /**
@ -56,6 +57,7 @@ class Order extends Model
'status' => 'int', 'status' => 'int',
'is_settle' => 'bool', 'is_settle' => 'bool',
'is_change' => 'bool', 'is_change' => 'bool',
'is_settlable' => 'bool',
]; ];
/** /**
@ -88,6 +90,7 @@ class Order extends Model
'is_change', 'is_change',
'is_settle', 'is_settle',
'sales_value', 'sales_value',
'is_settlable',
]; ];
public static $payWayText = [ public static $payWayText = [
@ -123,7 +126,7 @@ class Order extends Model
public function scopeSettlable($query) public function scopeSettlable($query)
{ {
return $query->where('status', static::STATUS_COMPLETED) return $query->where('status', static::STATUS_COMPLETED)
->where('completed_at', '<=', now()->subDays(app_settings('distribution.settle_days', 7))) ->where('is_settlable', true)
->where('is_settle', false); ->where('is_settle', false);
} }
@ -302,19 +305,6 @@ class Order extends Model
return $this->status === static::STATUS_CANCELLED; return $this->status === static::STATUS_CANCELLED;
} }
/**
* 将订单标记为已完成
*
* @return void
*/
public function markAsCompleted()
{
$this->update([
'status' => static::STATUS_COMPLETED,
'completed_at' => now(),
]);
}
/** /**
* 获取订单券优惠金额 * 获取订单券优惠金额
* *

View File

@ -81,6 +81,15 @@ class OrderProduct extends Model
return $this->belongsTo(ProductSku::class, 'sku_id'); return $this->belongsTo(ProductSku::class, 'sku_id');
} }
/**
* 此订单商品所属的SKU
*
*/
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
/** /**
* 确认此订单商品是否是赠品 * 确认此订单商品是否是赠品
* *
@ -100,11 +109,15 @@ class OrderProduct extends Model
{ {
$res = false; $res = false;
//老判断,有过期时间,且未到过期时间,未发起过售后 if ($this->order->is_settlable) {
return false;
}
// 老判断,有过期时间,且未到过期时间,未发起过售后
// $oldJudge = !is_null($this->after_expire_at) && $this->after_expire_at > now() && $this->after_sale_state == 0; // $oldJudge = !is_null($this->after_expire_at) && $this->after_expire_at > now() && $this->after_sale_state == 0;
//新判断, 有发货单,在售后时间范围内, 当前无售后; //新判断, 有发货单,在售后时间范围内, 当前无售后;
if ($this->packages()->where('is_failed', false)->count() >0) { if ($this->packages()->where('is_failed', false)->count() > 0) {
if ((is_null($this->after_expire_at) || $this->after_expire_at > now()) && $this->after_sale_state == 0) { if ((is_null($this->after_expire_at) || $this->after_expire_at > now()) && $this->after_sale_state == 0) {
$res = true; $res = true;
} }
@ -121,9 +134,11 @@ class OrderProduct extends Model
public function getHasAfterSaleAttribute(): bool public function getHasAfterSaleAttribute(): bool
{ {
$res = false; $res = false;
if ($this->afterSales()->count() > 0) { if ($this->afterSales()->count() > 0) {
$res =true; $res = true;
} }
return $res; return $res;
} }

View File

@ -819,14 +819,19 @@ class OrderService
* 确认订单 * 确认订单
* *
* @param \App\Models\Order $order * @param \App\Models\Order $order
* @param bool $isSettlable
* @return void * @return void
*/ */
public function confirm(Order $order) public function confirm(Order $order, $isSettlable = false)
{ {
if (! $order->isShipped()) { if (! $order->isShipped()) {
throw new BizException('订单包裹未发完'); throw new BizException('订单包裹未发完');
} }
if ($isSettlable && $order->afterSales()->processing()->count() > 0) {
throw new BizException('订单商品售后中,不能完成此订单');
}
$orderPackageService = new OrderPackageService(); $orderPackageService = new OrderPackageService();
$order->loadMissing('packages'); $order->loadMissing('packages');
@ -839,7 +844,11 @@ class OrderService
$orderPackageService->checkPackage($package, true); $orderPackageService->checkPackage($package, true);
} }
$order->markAsCompleted(); $order->update([
'is_settlable' => $isSettlable,
'status' => Order::STATUS_COMPLETED,
'completed_at' => now(),
]);
} }
/** /**

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIsSettlableToOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->boolean('is_settlable')->default(false)->comment('是否完成');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn(['is_settlable']);
});
}
}