KUJUNTI.ID MINISH3LL
Path : /var/www/html/backup/jewelry-pos/app/Models/
(S)h3ll Cr3at0r :
F!le Upl0ad :

B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H

Current File : /var/www/html/backup/jewelry-pos/app/Models/Product.php


<?php

namespace App\Models;

use http\Env\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use PhpParser\Node\Expr\FuncCall;

class Product extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'part_number', 'name', 'cost', 'price', 'description', 'image', 'is_active', 'weight',
        'user_id', 'product_category_id', 'wages', 'diamond_size', 'qty', 'for_sale', 'original_wages', 'weight_description',
        'supplier_rate', 'gia_number', 'supplier_id', 'fixed_price', 'wages_in_weight', 'original_wages_in_weight', 'purity_upgrade_to', 'selling_purity'
        , 'purity_level', 'old_code', 'original_invoice' , 'cost_discount'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function productCategory()
    {
        return $this->belongsTo(ProductCategory::class);
    }

    public function sales()
    {
        return $this->hasMany(Sale::class);
    }

    public function qtyHistory()
    {
        return $this->hasMany(QtyHistory::class);
    }

    public function productAttributes()
    {
        return $this->hasMany(ProductAttribute::class);
    }

    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }

    public function goldWeight()
    {
        if (isset($this->productCategory->name)) {
            if ($this->productCategory->purity_level) {
//                dd($this->product-categories->purity_level * $this->weight , $this->weight);
                return $this->weight * $this->productCategory->purity_level;
            }
            return $this->weight;
        }
        return 0;
    }

    public function upgradeGoldWeight()
    {
        if (isset($this->productCategory->name)) {
            $gold = ProductCategory::whereIn('name', ['មាស', 'Gold', 'ทอง'])->first();
            if ($this->productCategory->purity_level != 1) {
                return $this->weight * $this->selling_purity;
            }
            if ($this->productCategory->name == $gold->name) {
                return $this->weight;
            }
            return $this->weight;
        }
        return 0;
    }

    public function selling_wagesInWeight()
    {
        if ($this->wages_in_weight != 0) {
            if ($this->productCategory?->fixed_price || $this->fixed_price) {
                return $this->wages;
            } else {
                $gold = ProductCategory::whereIn('name', ['មាស', 'Gold', 'ทอง'])->first();
                $wages_in_weight = $this->wages_in_weight * ($gold->selling_to_usd_exchange_rate);
                if ($this->purity_level == 0 || $this->purity_level == 1) {
                    $wages_in_weight = $this->wages_in_weight * ($this->productCategory->sellingPrice());
                }
//            dd($wages_in_weight , $this->wages , $this->supplier_rate , $this);
                return $wages_in_weight;
            }
        } else {
            return $this->wages;
        }
//        if($this->productCategory->parent?->name??'' == 'ផ្លាកទីន' || $this->productCategory->parent?->name??'' == 'Platinum' || $this->productCategory->parent?->name??'' == 'แพลตตินัม') {
//            $wages_in_weight = $this->wages / ($this->supplier_rate / 1000);
//            return $wages_in_weight;
//        }
    }

    public function cost_wagesInWeight($price = null)
    {
        if ($this->original_wages_in_weight != 0) {
            if ($this->productCategory?->fixed_price || $this->fixed_price) {
                return $this->original_wages;
            } else {
                $rate1 = $this->supplier_rate / 1000 ?? $this->productCategory->price($price, $this->purity_level);
                $wages_in_weight = $this->original_wages_in_weight * ($rate1);
                if ($this->purity_level == 0 || $this->purity_level == 1) {
                    $rate2 = $this->supplier_rate / 1000 ?? $this->productCategory->price();
                    $wages_in_weight = $this->original_wages_in_weight * ($rate2);
                }
//            dd($wages_in_weight , $this->wages , $this->supplier_rate , $this);
                return $wages_in_weight;
            }
        } else {
            return $this->original_wages;
        }

//        if($this->productCategory->parent?->name??'' == 'ផ្លាកទីន' || $this->productCategory->parent?->name??'' == 'Platinum' || $this->productCategory->parent?->name??'' == 'แพลตตินัม') {
//            $wages_in_weight = $this->wages / ($this->supplier_rate / 1000);
//            return $wages_in_weight;
//        }
    }

    public function productCategoryNameSelling()
    {
        $purity = $this->selling_purity * 10;
        $productMainCategory = $this->productCategory->parent?->name . 'ទឹក ' . $purity;
        return $productMainCategory;
    }

    public function originalWagesInWeight()
    {
        if ($this->productCategory->fixed_price || $this->fixed_price) {
            return $this->original_wages;
        } else {
            if ($this->supplier_rate == 0) {
                return $this->original_wages;
            }
            $wages_in_weight = $this->original_wages * ($this->productCategory->sellingPrice($this->purity_level));
            return $wages_in_weight;
        }
//        if($this->productCategory->parent?->name??'' == 'ផ្លាកទីន' || $this->productCategory->parent?->name??'' == 'Platinum' || $this->productCategory->parent?->name??'' == 'แพลตตินัม') {
//            $wages_in_weight = $this->original_wages / ($this->supplier_rate / 1000);
//            return $wages_in_weight;
//        }
    }

    public function upgradeSellingPrice()
    {
        if (isset($this->productCategory->name)) {
            if ($this->productCategory->fixed_price == true || $this->fixed_price == true) {
                return $this->price;
            } else {
                $price = ($this->weight * ($this->productCategory->upgradeSellingRate($this->selling_purity)));

                $productAttributes = $this->productAttributes;
                foreach ($productAttributes as $productAttribute) {
                    $price = $price + $productAttribute->price;
                }
                return $price;
            }
        }
    }

    public function sellingPrice($weight = null)
    {
        if ($weight != null) {
            $this->weight = $weight;
        }
        if (isset($this->productCategory->name)) {
            if ($this->productCategory->fixed_price == true || $this->fixed_price == true) {
                return $this->price;
            } else {
                // if($this->product-categories->parent?->name=='Platinum'|| $this->product-categories->parent?->name=='ផ្លាកទីន'||$this->product-categories->parent?->name=='แพลตตินัม'){
                //     $price = ($this->goldWeight()*$this->product-categories->price());
                //     dd($this->goldWeight(), $this->product-categories->price(),$price);
                // }else{
                $purity = $this->selling_purity;
                if ($purity == 0 || $purity == null) {
                    $purity = $this->purity_level;
                }
                $price = ($this->weight * $this->productCategory->sellingPrice($purity));
                // }
                $productAttributes = $this->productAttributes;
                foreach ($productAttributes as $productAttribute) {
                    $price = $price + $productAttribute->price;
                }
                if ($this->productCategory->name == 'ផ្លាកទីន វៀតណាម') {
                    return $price;
                }
                // dd($price , $this->weight , $this->productCategory->sellingPrice($this->purity_level) , $this->purity_level);
//                dd($this->weight , $this->product-categories->sellingPrice());
//                dd($price , $this->wages , $productAttributes);
                return $price + $this->selling_wagesInWeight();
            }
        }
        return null;
    }

    public function sellingCost($weight = null, $price = null)
    {
        if (isset($this->productCategory->name)) {
            if ($this->productCategory->fixed_price == true || $this->fixed_price == true || ($this->productCategory->name == 'មាសថៃ' || $this->productCategory->name == 'Thai Gold' || $this->productCategory->name == 'ทองไทย')) {
                return $this->cost;
            }
            // else if($this->product-categories->name == 'មាស' || $this->product-categories->name == 'Gold' || $this->product-categories->name == 'ทอง'){
            //     $cost = $this->cost;
            //     $productAttributes = $this->productAttributes;
            //     foreach($productAttributes  as $productAttribute){
            //         $cost = $cost+ $productAttribute->cost;
            //     }
            //     return $cost;
            // }
            else {
                $cost = ($weight ?? $this->weight) * $this->productCategory->price($price, $this->purity_level ?? $this->selling_purity);
//                dd($cost ,$weight , $this->weight , $this->productCategory->price($price , $this->purity_level));
                $productAttributes = $this->productAttributes;
                foreach ($productAttributes as $productAttribute) {
                    $cost = $cost + $productAttribute->cost;
                }
//                return $cost+$this->original_wages;
                if ($this->purity_upgrade_to) {
                    return $this->cost + $this->original_wages ?? $this->cost_wagesInWeight();
                }
                return $cost + ($this->original_wages ? $this->original_wages : $this->cost_wagesInWeight());
            }
        }
        return null;
    }
    // public function costAttributes(){
    //     $cost = 0;
    //     $productAttributes = $this->productAttributes;
    //     foreach($productAttributes  as $productAttribute){
    //         $cost = $cost+ $productAttribute->cost;
    //     }
    //     return $cost;
    // }
    public function tempQty()
    {
        return $this->qty + ($this->sales->sum('qty'));
    }

    public function cost($weight = null, $date = null)
    {
        if ($weight != null) {
            $this->weight = $weight;
        }
        if ($this->for_sale == 0) {
            if ($this->part_number == '') {
                $cost = $this->weight * ($this->productCategory->price(null, null, $date));
                return $cost;
            }
            $cost = $this->weight * ($this->supplier_rate / 1000);
            return $cost;
        }
        return $this->cost;
        // if($this->productCategory->fixed_price==true || $this->fixed_price==true||($this->productCategory->name=='មាសថៃ'||$this->productCategory->name=='Thai Gold'||$this->productCategory->name=='ทองไทย')){
        //         return $this->cost;
        // }

        // return $this->cost + $this->productAttributes()->sum('cost');
    }
    public function costNotDiscount($weight = null, $date = null)
    {
        $costNotDiscount = $this->cost_discount + $this->cost();
        return $costNotDiscount;
    }

}

© KUJUNTI.ID