ECOMMERCE/app/Models/Items.php

233 lines
7.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use DB;
// use Awobaz\Compoships\Compoships;
use Spatie\Activitylog\LogOptions;
use Cache;
class Items extends Model
{
use HasFactory, SoftDeletes;
// use Compoships;
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}
// protected $with = ['brands', 'categories'];
protected $table = 'items';
protected $primaryKey = 'id';
protected $guarded = ['id'];
protected $fillable = [
'number',
'name',
'brand_id',
'category_id',
'image',
'description',
'weight',
'is_publish',
// BC data
'type',
'unit',
'display_unit',
'unit_cost',
'costing_method',
'last_direct_cost',
'unit_price',
'price_include_vat',
'sales_blocked',
'purchasing_blocked',
'profit_percent',
'net_weight',
'gross_weight',
'unit_volume',
'net_price',
'ref_id',
// purchase,
'alias_name',
'group_type',
'model_type',
'slug'
];
public function scopeFilter(Builder $query, array $filters)
{
$query->when($filters['search'] ?? false, function ($query, $search) {
return $query
->where('number', 'iLIKE', '%' . $search . '%')
->orWhere('name', 'iLIKE', '%' . $search . '%');
});
}
public function order()
{
return $this->hasMany(SaleOrderDetail::class, 'item_id', 'id');
}
public function dimension()
{
return $this->hasOne(ItemDimension::class,"no","number");
}
public function reference()
{
return $this->hasOne(ItemReference::class,'item_id')
->where(function($query){
$query->whereNull("item_variant_id")->orWhere("item_variant_id",0);
});
}
public function variants()
{
return $this->hasMany(ItemVariant::class,"item_id")->leftJoin("stocks","item_variant_id","=","item_variants.id")
->select("item_variants.id","item_variants.code","item_variants.display_name","item_variants.is_publish", DB::raw("SUM(quantity) as stock"),"description","item_variants.item_id")
->groupBy("item_variants.id","item_variants.code","description","item_variants.item_id");
}
public function images()
{
return $this->hasMany(ItemImage::class,'item_id')->whereNull('item_variant_id');
}
public function itemVariants()
{
return $this->hasMany(ItemVariant::class,"item_id")->with("reference");
}
public function publishedItemVariants()
{
return $this->hasMany(ItemVariant::class,"item_id")->with("reference")->where('is_publish', true);
}
public function brands()
{
return $this->belongsTo(Brand::class, 'brand_id', 'id');
}
public function categories()
{
return $this->belongsTo(Categories::class, 'categories_id', 'id');
}
public function brand()
{
return $this->belongsTo(Brand::class, 'brand_id', 'id');
}
public function gender()
{
return $this->belongsTo(Gender::class, 'gender_id', 'id');
}
public function category()
{
return $this->belongsTo(Categories::class, 'category_id', 'id');
}
public function discount()
{
$user = auth()->user();
list($location_id, $is_consignment) = Cache::remember("employee_user_".$user->id, 60 * 60 * 24, function() use ($user){
if ($user == null)
return [10, false];
$employee = @$user->employee;
$location_id = @$employee->location_id;
$location = @$employee->location;
$is_consignment = (boolean) @$location->is_consignment;
return [$location_id, $is_consignment];
});
return $this->hasOne(Discount::class,DB::raw("item_reference.item_id"))
->leftJoin("discount_items","discounts.id","=","discount_id")
->leftJoin("item_reference","item_reference.id","=","item_reference_id")
->where("discounts.type","discount")
->orderBy("discounts.created_at","desc")
->where(function ($query) {
$query->whereNull("valid_at")
->orWhereDate("valid_at", "<=", Carbon::now());
})
->where(function ($query) {
$query->whereNull("expired_at")
->orWhereDate("expired_at", ">=", Carbon::now());
})
->where(function($query) use ($location_id, $is_consignment){
if (!$is_consignment)
$query->whereNull("discounts.location_id");
if ($location_id)
$query->orWhere("discounts.location_id", $location_id);
})
->select("item_id","price");
}
public function price()
{
$user = auth()->user();
list($location_id, $is_consignment) = Cache::remember("employee_user_".$user->id, 60 * 60 * 24, function() use ($user){
if ($user == null)
return [10, false];
$employee = @$user->employee;
$location_id = @$employee->location_id;
$location = @$employee->location;
$is_consignment = (boolean) @$location->is_consignment;
return [$location_id, $is_consignment];
});
return $this->hasOne(Discount::class,DB::raw("item_reference.item_id"))
->leftJoin("discount_items","discounts.id","=","discount_id")
->leftJoin("item_reference","item_reference.id","=","item_reference_id")
->where("discounts.type","price")
->orderBy("discounts.created_at","desc")
->where(function ($query) {
$query->whereNull("valid_at")
->orWhereDate("valid_at", "<=", Carbon::now());
})
->where(function ($query) {
$query->whereNull("expired_at")
->orWhereDate("expired_at", ">=", Carbon::now());
})
->where(function($query) use ($location_id, $is_consignment){
if (!$is_consignment)
$query->whereNull("discounts.location_id");
if ($location_id)
$query->orWhere("discounts.location_id", $location_id);
})
->select("item_id","price");
}
public function stock()
{
return $this->hasOne(Stock::class,"item_id")
->leftJoin("locations","locations.id","=","location_id")
->whereNotNull("locations.display_name")
->where("locations.display_name","<>","")
->where("quantity",">","0")
->groupBy("item_id")
->select("item_id",DB::raw("SUM(quantity) as quantity"));
}
public function attributes()
{
return $this->hasMany(ItemAttribute::class, 'item_id');
}
}