2018-09-28 12:54:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Sales\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Webkul\Sales\Contracts\OrderItem as OrderItemContract;
|
2018-10-23 11:45:44 +00:00
|
|
|
use Webkul\Product\Models\Product;
|
2018-09-28 12:54:26 +00:00
|
|
|
|
|
|
|
|
class OrderItem extends Model implements OrderItemContract
|
|
|
|
|
{
|
2019-08-30 11:39:15 +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()
|
|
|
|
|
{
|
|
|
|
|
if ($this->typeInstance)
|
|
|
|
|
return $this->typeInstance;
|
|
|
|
|
|
|
|
|
|
$this->typeInstance = app(config('product_types.' . $this->type . '.class'));
|
|
|
|
|
|
|
|
|
|
if ($this->product)
|
|
|
|
|
$this->typeInstance->setProduct($this);
|
|
|
|
|
|
|
|
|
|
return $this->typeInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isStockable()
|
|
|
|
|
{
|
|
|
|
|
return $this->getTypeInstance()->isStockable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if new shipment is allow or not
|
|
|
|
|
*/
|
|
|
|
|
public function canShip()
|
|
|
|
|
{
|
2019-08-20 12:27:39 +00:00
|
|
|
if (! $this->isStockable())
|
2019-06-28 14:18:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if ($this->qty_to_ship > 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2019-08-20 12:27:39 +00:00
|
|
|
if (! $this->isStockable())
|
2019-06-28 14:18:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
if ($this->qty_to_invoice > 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function canCancel()
|
|
|
|
|
{
|
|
|
|
|
if ($this->qty_to_cancel > 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:54:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get the order record associated with the order item.
|
|
|
|
|
*/
|
|
|
|
|
public function order()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(OrderProxy::modelClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the order record associated with the order item.
|
|
|
|
|
*/
|
|
|
|
|
public function product()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the child item record associated with the order item.
|
|
|
|
|
*/
|
|
|
|
|
public function child()
|
|
|
|
|
{
|
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-08-30 11:39:15 +00:00
|
|
|
/**
|
|
|
|
|
* Get the children items.
|
|
|
|
|
*/
|
|
|
|
|
public function children()
|
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2018-12-26 10:00:23 +00:00
|
|
|
public function invoice_items()
|
|
|
|
|
{
|
2018-10-09 12:02:22 +00:00
|
|
|
return $this->hasMany(InvoiceItemProxy::modelClass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the shipment items record associated with the order item.
|
|
|
|
|
*/
|
2018-12-26 10:00:23 +00:00
|
|
|
public function shipment_items()
|
|
|
|
|
{
|
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
|
|
|
/**
|
|
|
|
|
* Get the invoice items record associated with the order item.
|
|
|
|
|
*/
|
|
|
|
|
public function downloadable_link_purchased()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DownloadableLinkPurchasedProxy::modelClass());
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 04:20:43 +00:00
|
|
|
/**
|
|
|
|
|
* Returns configurable option html
|
|
|
|
|
*/
|
|
|
|
|
public function getOptionDetailHtml()
|
|
|
|
|
{
|
2019-06-29 12:21:49 +00:00
|
|
|
if ($this->type != 'configurable')
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (isset($this->additional['attributes'])) {
|
2018-10-26 04:20:43 +00:00
|
|
|
$labels = [];
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($this->additional['attributes'] as $attribute) {
|
2018-10-26 04:20:43 +00:00
|
|
|
$labels[] = $attribute['attribute_name'] . ' : ' . $attribute['option_label'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode(', ', $labels);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-28 14:18:52 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns configurable option html
|
|
|
|
|
*/
|
|
|
|
|
public function getDownloadableDetailHtml()
|
|
|
|
|
{
|
|
|
|
|
$labels = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->downloadable_link_purchased as $link) {
|
|
|
|
|
if (! $link->download_bought) {
|
|
|
|
|
$labels[] = $link->name . ' (' . $link->download_used . ' / U)';
|
|
|
|
|
} else {
|
|
|
|
|
$labels[] = $link->name . ' (' . $link->download_used . ' / ' . $link->download_bought . ')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode(', ', $labels);
|
|
|
|
|
}
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|