Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
Models
/
Filename :
Product.php
back
Copy
<?php namespace App\Models; use App\Libraries\Helpers; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Casts\Attribute; use Config; use Exception; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOneOrMany; use Illuminate\Database\Eloquent\Relations\HasOneThrough; class Product extends Model { use SoftDeletes; protected $table = 'products'; protected $primaryKey = 'product_id'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'uuid', 'parent_product_id', 'product_type', 'task_id', 'group_id', 'product_category_id', 'product_name', 'description', 'product_image', 'product_sp', 'expires_on', 'is_featured', 'latitude', 'longitude', 'status', 'app_share_link', 'android_share_link', 'ios_share_link', 'created_by', 'created_at', 'updated_at', 'deleted_at' ]; protected $appends = [ 'expires_on_ymd' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'deleted_at', ]; protected function productImage(): Attribute { return Attribute::make( get: function (string $value) { $firstImage = $this->images()->first(); if ($firstImage) { return $firstImage->image_name; } return Config::get('constants.app.api-product-image-url').$value; } ); } protected function expiresOnYmd(): Attribute { return Attribute::make( get: function () { $originalExpiresOn = isset($this->getAttributes()['expires_on']) && !empty($this->getAttributes()['expires_on']) ? $this->getAttributes()['expires_on'] : null; return !empty($originalExpiresOn) ? date("Y-m-d", strtotime($originalExpiresOn)) : null; } ); } // protected function expiresOn(): Attribute // { // return Attribute::make( // get: fn (?string $value) =>!empty($value) ? date("jS M", strtotime($value)) : null, // ); // } protected function expiresOnJSM(): Attribute { return Attribute::make( get: fn (?string $value) =>!empty($value) ? date("jS M", strtotime($value)) : null, ); } /** * Get the images for the product. */ public function images(): HasMany { return $this->hasMany('App\Models\ProductImage', 'product_id'); } public function category(): HasOne { return $this->hasOne('App\Models\ProductCategory', 'product_category_id', 'product_category_id'); } public function rewardSetting(): HasOne { return $this->hasOne('App\Models\RewardSetting', 'reward_id', 'product_id'); } public function getTaskForUser(User $user){ $arrTasksAssigned = TaskAssigned::where('user_id', $user->user_id) ->get(); $taskAssignedIdToObject = array(); foreach($arrTasksAssigned AS $taskAssigned){ $taskAssignedIdToObject[$taskAssigned->task_id] = $taskAssigned; } $tasks = Task::whereIn('task_id',array_keys($taskAssignedIdToObject)) ->where('product_id', $this->product_id)->get(); $taskToReturn = null; foreach($tasks AS $task){ if(isset($taskAssignedIdToObject[$task->task_id])) { $taskToReturn = $task; $taskToReturn['assignedTo'] = $taskAssignedIdToObject[$task->task_id]; break; }; } if(!isset($taskToReturn) || $taskToReturn == null) { return null; } else { return $taskToReturn; } } /** * Get the tasks for the product. */ public function tasks(): HasMany { return $this->hasMany(Task::class, 'product_id'); } public function storeProducts(): HasMany { return $this->hasMany(StoreProduct::class, 'product_id', 'product_id'); } public function isClaimed($storeId, $userId): bool { // Check if the product is claimed by the specified user in the given store return $this->storeProducts() ->where('store_id', $storeId) ->whereHas('productClaims', function ($query) use ($userId) { $query->where('user_id', $userId) ->whereIn('status',['APPROVED', 'REWARDED']); }) ->exists(); } public function isClaimUnderReview($storeId, $userId): bool { // Check if the product is claimed by the specified user in the given store return $this->storeProducts() ->where('store_id', $storeId) ->whereHas('productClaims', function ($query) use ($userId) { $query->where('user_id', $userId) ->whereIn('status',['PENDING_APPROVAL']); }) ->exists(); } public static function populateRewardAdditionalAttributes(Product &$product, $user, $activeStore){ $product['claim_status'] = $product->isClaimed($activeStore->store_id, $user->user_id) ? 'APPROVED' : ($product->isClaimUnderReview($activeStore->store_id, $user->user_id) ? "PENDING_APPROVAL" : "PENDING"); } public function productSetting(): HasOne { return $this->hasOne(RewardSetting::class, 'reward_id', 'product_id'); } public function customFields(): HasMany { return $this->hasMany(CustomField::class, 'entity_id', 'product_id') ->where('entity_type', '=', 'PRODUCT'); } //additional attributes: private $store_product; private $claimed_by_map; private $associated_task; private $productWishlistObj; public function setStoreProduct(StoreProduct $store_product){ $this->store_product = $store_product; } public function getStoreProduct(){ return $this->store_product; } public function addClaimedBy($user_id, $productClaim){ if(!isset($claimed_by_map[$user_id])) $claimed_by_map = []; $this->claimed_by_map[$user_id] = isset($this->claimed_by_map[$user_id]) ? ($this->claimed_by_map[$user_id][] = $productClaim) : [$productClaim]; } public function isClaimedUserInStore($store_id, $user_id){ if($this->claimed_by_map == null) throw new Exception('ClaimedBy not yet populated'); return isset($this->claimed_by_map[$user_id]) ? ($this->claimed_by_map[$user_id]->store_id == $store_id) : false ; } public function setAssociatedTask($task){ $this->associated_task = $task; } public function getAssociatedTask(){ return $this->associated_task; } public function setProductWishlist(ProductWishlist $productWishlistObj){ $this->productWishlistObj = $productWishlistObj; } public function getProductWishlist(){ return $this->productWishlistObj; } public function cloneProduct(){ $excludeColumns = ['created_by', 'created_at','updated_at']; $clone = $this->replicate(); $clone->unsetRelation('category'); $clone->unsetRelation('tasks'); //TODO: remove this if we want all the repeted rewards to be added in all stores $clone->unsetRelation('storeProducts'); $clone->uuid = Helpers::getStringUUID(); $clone->created_by = null; $clone->created_at = null; $clone->updated_at = null; $clone->expires_on = $this->expires_on_ymd != null ? $this->expires_on_ymd : null; //set new values $clone->parent_product_id = $this->product_id; $clone->created_at = Carbon::now(); $clone->updated_at = Carbon::now(); $clone->save(); return $clone; } }