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'); } }