2018-09-28 12:54:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Sales\Models;
|
|
|
|
|
|
2021-12-22 13:40:40 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
2021-10-04 08:30:32 +00:00
|
|
|
use Webkul\Product\Type\AbstractType;
|
2018-09-28 12:54:26 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-10-04 08:30:32 +00:00
|
|
|
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;
|
2018-09-28 12:54:26 +00:00
|
|
|
use Webkul\Sales\Contracts\OrderItem as OrderItemContract;
|
|
|
|
|
|
|
|
|
|
class OrderItem extends Model implements OrderItemContract
|
|
|
|
|
{
|
2021-10-04 08:30:32 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function isStockable(): bool
|
2019-06-28 14:18:52 +00:00
|
|
|
{
|
2021-10-04 08:30:32 +00:00
|
|
|
return $this->getTypeInstance()
|
|
|
|
|
->isStockable();
|
2019-06-28 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-04 08:30:32 +00:00
|
|
|
* Checks if new shipment is allowed or not
|
2019-06-28 14:18:52 +00:00
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function canShip(): bool
|
2019-06-28 14:18:52 +00:00
|
|
|
{
|
2022-07-12 12:04:13 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get remaining qty for shipping.
|
|
|
|
|
*/
|
2018-12-26 10:00:23 +00:00
|
|
|
public function getQtyToShipAttribute()
|
|
|
|
|
{
|
2022-07-12 12:04:13 +00:00
|
|
|
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;
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get remaining qty for invoice.
|
|
|
|
|
*/
|
2018-12-26 10:00:23 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function canCancel(): bool
|
2019-06-28 14:18:52 +00:00
|
|
|
{
|
2021-10-04 08:30:32 +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.
|
|
|
|
|
*/
|
2018-12-26 10:00:23 +00:00
|
|
|
public function getQtyToCancelAttribute()
|
|
|
|
|
{
|
2018-10-15 10:39:09 +00:00
|
|
|
return $this->qty_ordered - $this->qty_canceled - $this->qty_invoiced;
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 13:27:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get remaining qty for refund.
|
|
|
|
|
*/
|
|
|
|
|
public function getQtyToRefundAttribute()
|
|
|
|
|
{
|
|
|
|
|
return $this->qty_invoiced - $this->qty_refunded;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:54:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get the order record associated with the order item.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function order(): BelongsTo
|
2018-09-28 12:54:26 +00:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(OrderProxy::modelClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-19 07:00:42 +00:00
|
|
|
* Get the product record associated with the order item.
|
2018-09-28 12:54:26 +00:00
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function product(): MorphTo
|
2018-09-28 12:54:26 +00:00
|
|
|
{
|
|
|
|
|
return $this->morphTo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the child item record associated with the order item.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function child(): HasOne
|
2018-09-28 12:54:26 +00:00
|
|
|
{
|
2018-10-09 12:02:22 +00:00
|
|
|
return $this->hasOne(OrderItemProxy::modelClass(), 'parent_id');
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|
2018-10-05 06:18:58 +00:00
|
|
|
|
2019-09-03 10:16:13 +00:00
|
|
|
/**
|
|
|
|
|
* Get the parent item record associated with the order item.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function parent(): BelongsTo
|
2019-09-03 10:16:13 +00:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(self::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 11:39:15 +00:00
|
|
|
/**
|
|
|
|
|
* Get the children items.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function children(): HasMany
|
2019-08-30 11:39:15 +00:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get the invoice items record associated with the order item.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function invoice_items(): HasMany
|
2018-12-26 10:00:23 +00:00
|
|
|
{
|
2018-10-09 12:02:22 +00:00
|
|
|
return $this->hasMany(InvoiceItemProxy::modelClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the shipment items record associated with the order item.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
public function shipment_items(): HasMany
|
2018-12-26 10:00:23 +00:00
|
|
|
{
|
2018-10-09 12:02:22 +00:00
|
|
|
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.
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
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
|
|
|
*/
|
2021-10-04 08:30:32 +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
|
|
|
|
|
*/
|
2021-10-04 08:30:32 +00:00
|
|
|
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;
|
|
|
|
|
|
2020-06-24 18:53:01 +00:00
|
|
|
$array['downloadable_links'] = $this->downloadable_link_purchased;
|
|
|
|
|
|
2019-09-18 13:27:40 +00:00
|
|
|
return $array;
|
|
|
|
|
}
|
2021-10-04 08:30:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new factory instance for the model.
|
|
|
|
|
*
|
2021-12-22 13:40:40 +00:00
|
|
|
* @return Factory
|
2021-10-04 08:30:32 +00:00
|
|
|
*/
|
2021-12-22 13:40:40 +00:00
|
|
|
protected static function newFactory(): Factory
|
2021-10-04 08:30:32 +00:00
|
|
|
{
|
|
|
|
|
return OrderItemFactory::new();
|
|
|
|
|
}
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|