取消订单时,退还赠送的赠品
parent
fb89addef4
commit
f45182c1f2
|
|
@ -25,4 +25,14 @@ class ProductGift extends Model
|
||||||
{
|
{
|
||||||
return $this->belongsTo(ProductSku::class, 'gift_sku_id');
|
return $this->belongsTo(ProductSku::class, 'gift_sku_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 确认此赠品是否有限制
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isLimit(): bool
|
||||||
|
{
|
||||||
|
return $this->limit !== 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use App\Helpers\Numeric;
|
||||||
use App\Helpers\Order as OrderHelper;
|
use App\Helpers\Order as OrderHelper;
|
||||||
use App\Models\Order;
|
use App\Models\Order;
|
||||||
use App\Models\OrderProduct;
|
use App\Models\OrderProduct;
|
||||||
|
use App\Models\ProductGift;
|
||||||
use App\Models\ProductSku;
|
use App\Models\ProductSku;
|
||||||
use App\Models\ShippingAddress;
|
use App\Models\ShippingAddress;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
|
@ -293,28 +294,29 @@ class OrderService
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 需赠送礼品的总数
|
||||||
|
$num = $gift->num * $qty;
|
||||||
|
|
||||||
// 如果赠品有限,且剩余数量不足时,直接赠送剩余赠品
|
// 如果赠品有限,且剩余数量不足时,直接赠送剩余赠品
|
||||||
if ($gift->limit !== 0 && $gift->remaining < $qty) {
|
if ($gift->isLimit() && $num > $gift->remaining) {
|
||||||
$qty = $gift->remaining;
|
// 计算剩余可赠送的份数
|
||||||
|
$remainingQty = (int) ($gift->remaining / $gift->num);
|
||||||
|
|
||||||
|
$num = $gift->num * $remainingQty;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果赠送的份数小于1,则不赠送
|
if ($gift->isLimit()) {
|
||||||
if ($qty < 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($gift->limit === 0) {
|
|
||||||
$gift->increment('sent', $qty);
|
|
||||||
} else {
|
|
||||||
$gift->update([
|
$gift->update([
|
||||||
'remaining' => DB::raw("remaining-{$qty}"),
|
'remaining' => DB::raw("remaining-{$num}"),
|
||||||
'sent' => DB::raw("sent+{$qty}"),
|
'sent' => DB::raw("sent+{$num}"),
|
||||||
]);
|
]);
|
||||||
|
} else {
|
||||||
|
$gift->increment('sent', $num);
|
||||||
}
|
}
|
||||||
|
|
||||||
$gifts[] = [
|
$gifts[] = [
|
||||||
'sku' => $gift->giftSku,
|
'sku' => $gift->giftSku,
|
||||||
'num' => $qty*$gift->num, // 赠送商品总数
|
'num' => $num, // 赠送商品总数
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -790,9 +792,31 @@ class OrderService
|
||||||
$products = $order->products()->get();
|
$products = $order->products()->get();
|
||||||
|
|
||||||
foreach ($products->load('sku') as $product) {
|
foreach ($products->load('sku') as $product) {
|
||||||
// 取消订单时,赠品不退回
|
if ($product->sku === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果商品不是赠品,则直接增加商品库存
|
||||||
if (! $product->isGift()) {
|
if (! $product->isGift()) {
|
||||||
$product->sku?->increment('stock', $product->quantity);
|
$product->sku->increment('stock', $product->quantity);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$gift = ProductGift::where('sku_id', $product->gift_for_sku_id)
|
||||||
|
->where('gift_sku_id', $product->sku_id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($gift === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($gift->isLimit()) {
|
||||||
|
$gift->update([
|
||||||
|
'remaining' => DB::raw("remaining+{$product->quantity}"),
|
||||||
|
'sent' => DB::raw("sent-{$product->quantity}"),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
$gift->decrement('sent', $product->quantity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ class CreateProductGiftsTable extends Migration
|
||||||
$table->unsignedBigInteger('sku_id')->comment('主SKU商品ID');
|
$table->unsignedBigInteger('sku_id')->comment('主SKU商品ID');
|
||||||
$table->unsignedBigInteger('gift_sku_id')->comment('赠品SKU商品ID');
|
$table->unsignedBigInteger('gift_sku_id')->comment('赠品SKU商品ID');
|
||||||
$table->unsignedInteger('num')->default(0)->comment('每份赠送数量');
|
$table->unsignedInteger('num')->default(0)->comment('每份赠送数量');
|
||||||
$table->unsignedInteger('limit')->default(0)->comment('上限(份)');
|
$table->unsignedInteger('limit')->default(0)->comment('上限个数');
|
||||||
$table->unsignedInteger('sent')->default(0)->comment('已送(份)');
|
$table->unsignedInteger('sent')->default(0)->comment('已送个数');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class AddRemainingToProductGiftsTable extends Migration
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
Schema::table('product_gifts', function (Blueprint $table) {
|
Schema::table('product_gifts', function (Blueprint $table) {
|
||||||
$table->unsignedInteger('remaining')->default(0)->comment('剩余(份)');
|
$table->unsignedInteger('remaining')->default(0)->comment('剩余个数');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue