Edit File: ProductGroup.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\SoftDeletes; use Session; use DateTime; use App\Models\Properity; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; class ProductGroup extends Model { use HasFactory , SoftDeletes; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['properities','price','discount','discount_price','from','to','in_stock_type','in_stock_sku','allow_in_stock_system','in_stock_notification','in_stock_qty','allow_late_orders','allow_purchase_of_one_quantity','require_shipping','enable_the_great_discount','the_great_discount','the_great_discount_min_qty','appearance','status','product_id']; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = ['properities_data']; public function product(){ return $this->belongsTo(Product::class); } public function getProperitiesDataAttribute(){ $properities = []; if($this->properities !== null) { foreach (json_decode($this->properities) as $value) { $properity = Properity::find($value); if($properity){ array_push($properities,$properity); } } } return $properities ; } public function getProperitiesNameAttribute(){ $properities = []; if($this->properities !== null) { $properities = Properity::whereIn('id' , json_decode($this->properities))->pluck('name')->toArray(); } return $properities ; } // public function setDiscountAttribute($value){ // if($value){ // $this->attributes['discount_price'] = ($value/100) * $this->price; // $this->attributes['discount'] = $value; // } // } public function price(){ $price = $this->price ?? 0; $group = ProductGroup::where('product_id' , $this->product_id)->first(); if($group && $group->discount != NULL && $group->discount > 0 && $group->discount <= 100){ $price -= ($price*$group ->discount/100); } return $price; } public function display_price(){ $lang = app()->getLocale(); $sar = trans('stores.sar'); $hasDiscount = 0; $price = $this->price; if($this->discount != NULL && $this->discount > 0 && $this->discount <= 100){ $price -= ($price*$this->discount/100); } return '<span style="color:#2D7679">'.$price.' '.$sar.'</span>'; } public function _price_(){ $price = $this->price; $hasDiscount = 0; if($this->discount_price != NULL && $this->discount_price > 0){ $paymentDate = date('Y-m-d'); $paymentDate = date('Y-m-d', strtotime($paymentDate)); if($this->from == NULL){ $contractDateBegin = date('Y-m-d'); }else{ $contractDateBegin = date('Y-m-d', strtotime($this->from)); } if($this->to == NULL){ $datetime = new DateTime('tomorrow'); $contractDateEnd = $datetime->format('Y-m-d'); }else{ $contractDateEnd = date('Y-m-d', strtotime($this->to)); } if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){ $price = $this->discount_price; $hasDiscount = 1; } } if($hasDiscount == 1){ return $this->price; }else{ return 0; } } public function _price(){ $sar = Session::has('lang')&&Session::get('lang')=='en'?'SAR':'ر.س'; $price = $this->price; $hasDiscount = 0; if($this->discount_price != NULL && $this->discount_price > 0){ $paymentDate = date('Y-m-d'); $paymentDate = date('Y-m-d', strtotime($paymentDate)); if($this->from == NULL){ $contractDateBegin = date('Y-m-d'); }else{ $contractDateBegin = date('Y-m-d', strtotime($this->from)); } if($this->to == NULL){ $datetime = new DateTime('tomorrow'); $contractDateEnd = $datetime->format('Y-m-d'); }else{ $contractDateEnd = date('Y-m-d', strtotime($this->to)); } if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){ $price = $this->discount_price; $hasDiscount = 1; } } if($hasDiscount == 1){ return '<span>'.$price.' '.$sar.'</span>'.'<strike>'.$this->price.' '.$sar.'</strike>'; }else{ return $this->price.' '.$sar; } } public function _single_price(){ $sar = Session::has('lang')&&Session::get('lang')=='en'?'SAR':'ر.س'; $price = $this->price; $hasDiscount = 0; if($this->discount_price != NULL && $this->discount_price > 0){ $paymentDate = date('Y-m-d'); $paymentDate=date('Y-m-d', strtotime($paymentDate)); if($this->from == NULL){ $contractDateBegin = date('Y-m-d'); }else{ $contractDateBegin = date('Y-m-d', strtotime($this->from)); } if($this->to == NULL){ $datetime = new DateTime('tomorrow'); $contractDateEnd = $datetime->format('Y-m-d'); }else{ $contractDateEnd = date('Y-m-d', strtotime($this->to)); } if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){ $price = $this->discount_price; $hasDiscount = 1; } } if($hasDiscount == 1){ return '<p class="before"> <span class=" value"> <span class=" preReductionPrice"> <span class="value">'.$price.'</span> <span class="currency null"> '.$sar.' </span> </span> </span> </p> <p class="after"> <span class=" value"> <strike class=" preReductionPrice"> <span class="value">'.$this->price.'</span> <span class="currency null"> '.$sar.' </span> </strike> </span> </p> '; }else{ return ' <p class="before"> <span class=" value"> <span class=" preReductionPrice"> <span class="value">'.$this->price.'</span> <span class="currency null"> '.$sar.' </span> </span> </span> </p>'; } } }
Back to File Manager