sarga/packages/Webkul/Product/src/Models/Product.php

281 lines
7.4 KiB
PHP
Raw Normal View History

2018-07-27 06:22:12 +00:00
<?php
namespace Webkul\Product\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Attribute\Models\AttributeFamily;
2018-07-27 09:30:48 +00:00
use Webkul\Category\Models\Category;
use Webkul\Attribute\Models\Attribute;
use Webkul\Product\Models\ProductAttributeValue;
2018-08-09 04:35:13 +00:00
use Webkul\Product\Models\ProductInventory;
use Webkul\Product\Models\ProductImage;
2018-07-31 07:50:54 +00:00
use Webkul\Inventory\Models\InventorySource;
2018-08-21 10:58:55 +00:00
use Webkul\Product\Models\ProductReview;
2018-07-27 06:22:12 +00:00
class Product extends Model
{
protected $fillable = ['type', 'attribute_family_id', 'sku', 'parent_id'];
2018-07-27 09:30:48 +00:00
2018-08-22 07:05:17 +00:00
protected $with = ['attribute_family', 'inventories'];
2018-08-01 06:11:33 +00:00
2018-09-10 09:31:34 +00:00
// protected $table = 'products';
2018-08-01 06:11:33 +00:00
/**
* Get the product attribute family that owns the product.
*/
public function attribute_family()
{
return $this->belongsTo(AttributeFamily::class);
}
/**
* Get the product attribute values that owns the product.
*/
public function attribute_values()
{
return $this->hasMany(ProductAttributeValue::class);
}
/**
2018-08-09 04:35:13 +00:00
* Get the product variants that owns the product.
*/
2018-08-09 04:35:13 +00:00
public function variants()
{
return $this->hasMany(self::class, 'parent_id');
}
2018-08-21 10:58:55 +00:00
/**
* Get the product reviews that owns the product.
*/
public function reviews()
{
return $this->hasMany(ProductReview::class);
}
2018-08-09 04:35:13 +00:00
/**
* Get the product that owns the product.
*/
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
2018-08-09 04:35:13 +00:00
}
2018-07-27 09:30:48 +00:00
/**
* The categories that belong to the product.
*/
public function categories()
{
return $this->belongsToMany(Category::class, 'product_categories');
}
2018-07-31 07:50:54 +00:00
/**
* The inventories that belong to the product.
*/
public function inventories()
{
2018-08-09 04:35:13 +00:00
return $this->hasMany(ProductInventory::class, 'product_id');
}
/**
* The ordered inventories that belong to the product.
*/
public function ordered_inventories()
{
return $this->hasMany(ProductOrderedInventory::class, 'product_id');
}
2018-08-09 04:35:13 +00:00
/**
* The inventory sources that belong to the product.
*/
public function inventory_sources()
{
return $this->belongsToMany(InventorySource::class, 'product_inventories')->withPivot('id', 'qty');
2018-07-31 07:50:54 +00:00
}
/**
* The super attributes that belong to the product.
*/
public function super_attributes()
{
return $this->belongsToMany(Attribute::class, 'product_super_attributes');
}
2018-08-09 04:35:13 +00:00
/**
* The images that belong to the product.
*/
public function images()
{
return $this->hasMany(ProductImage::class, 'product_id');
}
2018-07-31 07:50:54 +00:00
/**
* The related products that belong to the product.
*/
public function related_products()
{
return $this->belongsToMany(self::class, 'product_relations');
}
/**
* The up sells that belong to the product.
*/
public function up_sells()
{
2018-08-30 13:22:15 +00:00
return $this->belongsToMany(self::class, 'product_up_sells', 'parent_id', 'child_id');
2018-07-31 07:50:54 +00:00
}
/**
* The cross sells that belong to the product.
*/
public function cross_sells()
{
return $this->belongsToMany(self::class, 'product_cross_sells');
}
2018-09-05 10:56:42 +00:00
2018-09-10 09:20:08 +00:00
/**
* @param string $key
*
* @return bool
*/
public function isSaleable()
{
2018-10-05 11:59:43 +00:00
if(!$this->status)
return false;
2018-10-05 11:59:43 +00:00
if($this->haveSufficientQuantity(1))
return true;
2018-09-10 09:20:08 +00:00
return false;
}
/**
* @param integer $qty
*
* @return bool
*/
public function inventory_source_qty($inventorySource)
{
return $this->inventories()
->where('inventory_source_id', $inventorySource->id)
->sum('qty');
}
2018-10-05 11:59:43 +00:00
/**
* @param integer $qty
*
* @return bool
*/
public function haveSufficientQuantity($qty)
{
$total = 0;
$channelInventorySourceIds = core()->getCurrentChannel()
->inventory_sources()
->where('status', 1)
->pluck('id');
foreach ($this->inventories as $inventory) {
if(is_numeric($index = $channelInventorySourceIds->search($inventory->inventory_source_id))) {
$total += $inventory->qty;
}
2018-10-05 11:59:43 +00:00
}
$orderedInventory = $this->ordered_inventories()
->where('channel_id', core()->getCurrentChannel()->id)
->first();
2018-10-05 11:59:43 +00:00
if ($orderedInventory) {
$total -= $orderedInventory->qty;
2018-10-05 11:59:43 +00:00
}
return $qty <= $total ? true : false;
}
2018-08-09 04:35:13 +00:00
/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key)
2018-09-20 10:01:51 +00:00
{
2018-08-22 07:05:17 +00:00
if (!method_exists(self::class, $key) && !in_array($key, ['parent_id', 'attribute_family_id']) && !isset($this->attributes[$key])) {
2018-10-12 08:22:06 +00:00
if (isset($this->id)) {
2018-08-22 07:05:17 +00:00
$this->attributes[$key] = '';
2018-10-12 08:22:06 +00:00
$attribute = app(\Webkul\Attribute\Repositories\AttributeRepository::class)->findOneByField('code', $key);
2018-08-09 04:35:13 +00:00
2018-10-12 08:22:06 +00:00
$this->attributes[$key] = $this->getCustomAttributeValue($attribute);
2018-08-22 07:05:17 +00:00
return $this->getAttributeValue($key);
2018-08-09 04:35:13 +00:00
}
}
return parent::getAttribute($key);
}
2018-08-01 06:11:33 +00:00
/**
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
$hiddenAttributes = $this->getHidden();
2018-08-30 13:22:15 +00:00
if(isset($this->id)) {
foreach ($this->attribute_family->custom_attributes as $attribute) {
if (in_array($attribute->code, $hiddenAttributes)) {
continue;
2018-08-09 04:35:13 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-10 10:26:27 +00:00
$attributes[$attribute->code] = $this->getCustomAttributeValue($attribute);
2018-08-30 13:22:15 +00:00
}
}
2018-08-01 06:11:33 +00:00
return $attributes;
2018-08-01 06:11:33 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-10 10:26:27 +00:00
/**
* Get an product attribute value.
*
* @return mixed
*/
public function getCustomAttributeValue($attribute)
{
if(!$attribute)
return;
2018-10-12 08:22:06 +00:00
$channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode());
2018-10-10 10:26:27 +00:00
$locale = request()->get('locale') ?: app()->getLocale();
if($attribute->value_per_channel) {
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first();
}
} else {
if($attribute->value_per_locale) {
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
}
}
return $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
}
2018-08-30 13:22:15 +00:00
/**
* Overrides the default Eloquent query builder
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newEloquentBuilder($query)
{
return new \Webkul\Product\Database\Eloquent\Builder($query);
}
2018-07-27 06:22:12 +00:00
}