sarga/packages/Webkul/Sales/src/Models/OrderItem.php

236 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Sales\Models;
2021-12-22 13:40:40 +00:00
use Illuminate\Database\Eloquent\Factories\Factory;
use Webkul\Product\Type\AbstractType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Webkul\Sales\Database\Factories\OrderItemFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Webkul\Sales\Contracts\OrderItem as OrderItemContract;
class OrderItem extends Model implements OrderItemContract
{
use HasFactory;
2020-03-04 06:32:53 +00:00
protected $guarded = [
'id',
'child',
'children',
'created_at',
'updated_at',
];
2018-10-05 06:18:58 +00:00
2018-10-26 04:20:43 +00:00
protected $casts = [
'additional' => 'array',
];
2019-06-28 14:18:52 +00:00
protected $typeInstance;
/**
* Retrieve type instance
*
* @return AbstractType
*/
public function getTypeInstance(): AbstractType
2019-06-28 14:18:52 +00:00
{
2020-02-20 06:47:44 +00:00
if ($this->typeInstance) {
2019-06-28 14:18:52 +00:00
return $this->typeInstance;
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
2021-10-06 12:26:08 +00:00
$this->typeInstance = app(config('product_types.' . $this->type . '.class'));
2019-06-28 14:18:52 +00:00
2020-02-20 06:47:44 +00:00
if ($this->product) {
2019-06-28 14:18:52 +00:00
$this->typeInstance->setProduct($this);
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
return $this->typeInstance;
}
/**
* @return bool
*/
public function isStockable(): bool
2019-06-28 14:18:52 +00:00
{
return $this->getTypeInstance()
->isStockable();
2019-06-28 14:18:52 +00:00
}
/**
* Checks if new shipment is allowed or not
2019-06-28 14:18:52 +00:00
*/
public function canShip(): bool
2019-06-28 14:18:52 +00:00
{
if (!$this->isStockable()) {
2019-06-28 14:18:52 +00:00
return false;
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
2020-02-20 06:47:44 +00:00
if ($this->qty_to_ship > 0) {
2019-06-28 14:18:52 +00:00
return true;
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
return false;
}
/**
* Get remaining qty for shipping.
*/
public function getQtyToShipAttribute()
{
if (!$this->isStockable()) {
2019-06-28 14:18:52 +00:00
return 0;
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
2018-10-15 10:39:09 +00:00
return $this->qty_ordered - $this->qty_shipped - $this->qty_refunded - $this->qty_canceled;
}
2019-06-28 14:18:52 +00:00
/**
* Checks if new invoice is allow or not
*/
public function canInvoice()
{
2020-02-20 06:47:44 +00:00
if ($this->qty_to_invoice > 0) {
2019-06-28 14:18:52 +00:00
return true;
2020-02-20 06:47:44 +00:00
}
2019-06-28 14:18:52 +00:00
return false;
}
/**
* Get remaining qty for invoice.
*/
public function getQtyToInvoiceAttribute()
{
2018-10-15 10:39:09 +00:00
return $this->qty_ordered - $this->qty_invoiced - $this->qty_canceled;
}
2019-06-28 14:18:52 +00:00
/**
* Checks if new cancel is allow or not
*/
public function canCancel(): bool
2019-06-28 14:18:52 +00:00
{
return $this->qty_to_cancel > 0;
2019-06-28 14:18:52 +00:00
}
2018-10-15 10:39:09 +00:00
/**
* Get remaining qty for cancel.
*/
public function getQtyToCancelAttribute()
{
2018-10-15 10:39:09 +00:00
return $this->qty_ordered - $this->qty_canceled - $this->qty_invoiced;
}
2019-09-18 13:27:40 +00:00
/**
* Get remaining qty for refund.
*/
public function getQtyToRefundAttribute()
{
return $this->qty_invoiced - $this->qty_refunded;
}
/**
* Get the order record associated with the order item.
*/
public function order(): BelongsTo
{
return $this->belongsTo(OrderProxy::modelClass());
}
/**
* Get the product record associated with the order item.
*/
public function product(): MorphTo
{
return $this->morphTo();
}
/**
* Get the child item record associated with the order item.
*/
public function child(): HasOne
{
return $this->hasOne(OrderItemProxy::modelClass(), 'parent_id');
}
2018-10-05 06:18:58 +00:00
/**
* Get the parent item record associated with the order item.
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
2019-08-30 11:39:15 +00:00
/**
* Get the children items.
*/
public function children(): HasMany
2019-08-30 11:39:15 +00:00
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* Get the invoice items record associated with the order item.
*/
public function invoice_items(): HasMany
{
return $this->hasMany(InvoiceItemProxy::modelClass());
}
/**
* Get the shipment items record associated with the order item.
*/
public function shipment_items(): HasMany
{
return $this->hasMany(ShipmentItemProxy::modelClass());
2018-10-05 06:18:58 +00:00
}
2018-10-26 04:20:43 +00:00
2019-06-28 14:18:52 +00:00
/**
2019-09-18 13:27:40 +00:00
* Get the refund items record associated with the order item.
*/
public function refund_items(): HasMany
2019-09-18 13:27:40 +00:00
{
return $this->hasMany(RefundItemProxy::modelClass());
}
2018-10-26 04:20:43 +00:00
/**
* Returns configurable option html
2019-06-28 14:18:52 +00:00
*/
public function downloadable_link_purchased(): HasMany
2019-06-28 14:18:52 +00:00
{
return $this->hasMany(DownloadableLinkPurchasedProxy::modelClass());
}
2019-09-18 13:27:40 +00:00
/**
* @return array
*/
public function toArray(): array
2019-09-18 13:27:40 +00:00
{
$array = parent::toArray();
$array['qty_to_ship'] = $this->qty_to_ship;
$array['qty_to_invoice'] = $this->qty_to_invoice;
$array['qty_to_cancel'] = $this->qty_to_cancel;
$array['qty_to_refund'] = $this->qty_to_refund;
$array['downloadable_links'] = $this->downloadable_link_purchased;
2019-09-18 13:27:40 +00:00
return $array;
}
/**
* Create a new factory instance for the model.
*
2021-12-22 13:40:40 +00:00
* @return Factory
*/
2021-12-22 13:40:40 +00:00
protected static function newFactory(): Factory
{
return OrderItemFactory::new();
}
}