Edit File: Product.php
<?php namespace App\Models; use Session; use DateTime; use App\Traits\Uploadable; use App\Traits\GeneralTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; class Product extends Model { use HasFactory, HasTranslations, GeneralTrait, Uploadable ; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['name','image','desc','type','num_rating' , 'rate','available','store_menu_category_id','store_id']; protected $guarded = []; public $translatable = ['name','desc']; public function store(){ return $this->belongsTo(Store::class); } protected function asJson($value) { return json_encode($value, JSON_UNESCAPED_UNICODE); } public function users() { return $this->belongsToMany(User::class, 'favourites'); } public function storeMenuCategory(){ return $this->belongsTo(StoreMenuCategory::class); } public function productfeatures(){ return $this->hasMany(Productfeature::class); } public function groups(){ return $this->hasMany(ProductGroup::class); } public function groupOne(){ return $this->groups()->where('properities' , null)->first(); } public function qty(){ if($this->type=='simple'){ $qty = $this->groups()->first()->in_stock_qty; }else{ $qty = $this->groups()->where('properities','!=',NULL)->sum('in_stock_qty'); } return $qty; } public function setImageAttribute($value) { if ( null != $value && is_file($value) ) { $this->attributes['image'] = $this->uploadOne($value, 'products'); } } public function getImagePathAttribute(){ return asset('assets/uploads/products/' . $this->attributes['image']); } public function additives(){ return $this->belongsToMany(ProductAdditiveCategory::class , 'product_additives' , 'product_id' , 'product_additive_category_id'); } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ self::deleted(function ($model) { $model->deleteFile($model->attributes['image'], 'products'); }); } public function reviews(){ return $this->hasMany(Review::class,'product_id'); } public function rating(){ $reviewsCount = $this->reviews()->count(); if($reviewsCount == 0){ return number_format(0,2); } $reviews = $this->reviews; $sum = 0; foreach($reviews as $review){ $sum+=$review->rate; } return number_format(round($sum/$reviewsCount,2),2); } public function display_price(){ $lang = app()->getLocale(); if($this->type == 'simple'){ $sar = trans('stores.sar'); $group = $this->groups()->first(); $hasDiscount = 0; if($group && $group->discount_price != NULL && $group->discount_price > 0){ $paymentDate = date('Y-m-d'); $paymentDate = date('Y-m-d', strtotime($paymentDate)); if($group->from == NULL){ $contractDateBegin = date('Y-m-d'); }else{ $contractDateBegin = date('Y-m-d', strtotime($group->from)); } if($group->to == NULL){ $datetime = new DateTime('tomorrow'); $contractDateEnd = $datetime->format('Y-m-d'); }else{ $contractDateEnd = date('Y-m-d', strtotime($group->to)); } if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){ $price = $group->discount_price; $hasDiscount = 1; } } if($hasDiscount == 1){ return '<span style="color:#2D7679">'.$price.' '.$sar.' </span><del style="color:#BCBCBC"><small>'.$group->price.' '.$sar.'</small></del>'; }else{ return '<span style="color:#2D7679">'.$group->price.' '.$sar.'</span>'; } }else{ return '<span style="color:#2D7679">'.trans('stores.price_according_to_choice').'</span>'; } } public function price(){ $lang = app()->getLocale(); if($this->type == 'simple'){ $sar = trans('stores.sar'); $group = $this->groups()->first(); $hasDiscount = 0; if($group && $group->discount_price != NULL && $group->discount_price > 0){ $paymentDate = date('Y-m-d'); $paymentDate = date('Y-m-d', strtotime($paymentDate)); if($group->from == NULL){ $contractDateBegin = date('Y-m-d'); }else{ $contractDateBegin = date('Y-m-d', strtotime($group->from)); } if($group->to == NULL){ $datetime = new DateTime('tomorrow'); $contractDateEnd = $datetime->format('Y-m-d'); }else{ $contractDateEnd = date('Y-m-d', strtotime($group->to)); } if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){ $price = $group->discount_price; $hasDiscount = 1; } } if($hasDiscount == 1){ if($lang == 'ar'){ $price = $this->convert2arabic($price); $group->price = $this->convert2arabic($group->price); } return $price." ".$sar; }else{ if($lang == 'ar'){ if($group){ $group->price = $this->convert2arabic($group->price); return $group->price.' '.$sar; } } } }else{ return trans('stores.price_according_to_choice'); } } public function main_price(){ $lang = app()->getLocale(); $price=$this->groups()->first()?->price; if($lang == 'ar'){ $price = $this->convert2arabic( $price); } return $price; } }
Back to File Manager