| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace Longyi\FreeGift\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Longyi\FreeGift\Config\Gift;
- use Longyi\FreeGift\Contracts\FreeGiftItem as FreeGiftItemContract;
- use Webkul\CartRule\Models\CartRule;
- use Webkul\Checkout\Models\Cart;
- use Webkul\Checkout\Models\CartItem;
- /**
- * 赠品在购物车中的状态(每个 购物车 + SKU + 规则 一行)。
- *
- * 对应 Amasty 的 amasty_ampromo_items_storage。
- */
- class FreeGiftItem extends Model implements FreeGiftItemContract
- {
- /**
- * The table associated with the model.
- *
- * @var string
- */
- protected $table = 'free_gift_items';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'cart_id',
- 'cart_item_id',
- 'product_id',
- 'sku',
- 'rule_id',
- 'rule_type',
- 'allowed_qty',
- 'reserved_qty',
- 'discount_amount',
- 'auto_add',
- 'is_item_deleted',
- ];
- /**
- * The attributes that should be cast.
- *
- * @var array
- */
- protected $casts = [
- 'cart_id' => 'integer',
- 'cart_item_id' => 'integer',
- 'product_id' => 'integer',
- 'rule_id' => 'integer',
- 'rule_type' => 'integer',
- 'allowed_qty' => 'float',
- 'reserved_qty' => 'float',
- 'auto_add' => 'boolean',
- 'is_item_deleted' => 'boolean',
- ];
- /**
- * 所属购物车。
- */
- public function cart(): BelongsTo
- {
- return $this->belongsTo(Cart::class, 'cart_id');
- }
- /**
- * 对应的购物车行。
- */
- public function cart_item(): BelongsTo
- {
- return $this->belongsTo(CartItem::class, 'cart_item_id');
- }
- /**
- * 赠送该赠品的规则。
- */
- public function cart_rule(): BelongsTo
- {
- return $this->belongsTo(CartRule::class, 'rule_id');
- }
- /**
- * 规则是否允许「一组合计送 N 个」。
- */
- public function isOneOf(): bool
- {
- return (int) $this->rule_type === Gift::TYPE_ONE;
- }
- /**
- * 规则是否允许「每个 SKU 各送 N 个」。
- */
- public function isAllOf(): bool
- {
- return (int) $this->rule_type === Gift::TYPE_ALL;
- }
- /**
- * 用户是否手动删除过该赠品(删除后不再自动加购,直到购物车清空)。
- */
- public function isDeleted(): bool
- {
- return (bool) $this->is_item_deleted;
- }
- /**
- * 还差几个需要补进购物车。
- */
- public function getQtyToProcessAttribute(): float
- {
- return max(0, (float) $this->allowed_qty - (float) $this->reserved_qty);
- }
- }
|