KUJUNTI.ID MINISH3LL
Path : /var/www/html/jewelry-pos/app/Http/Controllers/
(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/jewelry-pos/app/Http/Controllers/BuildStockDateController.php


<?php

namespace App\Http\Controllers;

use App\Models\BuildStockDate;
use App\Models\BuildStockProduct;
use App\Models\BuildStockProductAttribute;
use App\Models\ProductCategory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BuildStockDateController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function __construct()
    {
        set_time_limit(500000);
    }
    public function index(Request $request , $id)
    {
        $product_category_id = $request->get('product_category_id');
        if($product_category_id == -5){
            $category = ProductCategory::whereIn('name' , ['មាស','Gold','ทอง'])->first();
        }elseif($product_category_id == -100)
        {
            $category = ProductCategory::get();
        }else{
            $category = ProductCategory::find($product_category_id);
        }

        $date = $request->date;

        $productCategories = ProductCategory::get();

        $check_fixed_price = $request->get('check_fixed_price');

        $gold_category = clone $productCategories;
        $gold=$gold_category->whereIn('name',['មាស','Gold','ทอง'])->first();

        $child_gold = ProductCategory::where('parent_id' , $gold->id)->get();

        $platinume_clone = clone $productCategories;
        $platinume_Category = $platinume_clone->whereIn('name' , ['ផ្លាកទីន','Platinum','แพลตตินัม'])->first();
        $child_p = ProductCategory::where('parent_id' , $platinume_Category->id)->get();

        $product_pig_category = ProductCategory::whereIn('name' , ['ផលិតផលបន្ថែម' , 'Other Stock' ,'Extra Stock'])->first();


        $paginate = $request->get('paginate')??25;
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $check_fixed_price = $request->get('check_fixed_price');

        if($product_category_id == -100){
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
            ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.build_stock_date_id', $id)->where('build_stock_products.weight' , '<>' , 0);
        }else{
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
            ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 1)
            ->where('build_stock_products.build_stock_date_id', $id);

        }

        $productCategory = [];
        if($product_category_id != 0 && $product_category_id != -5 && $product_category_id != -100){
            $productCategory = ProductCategory::findOrFail($product_category_id);
            $build_stocks = $build_stocks->whereIn('build_stock_products.product_category_id' , $productCategory->IdvsAllSubCategories());
        }

        if($product_category_id == -5){
            $productCategory = $category;
            $build_stocks = $build_stocks->where('build_stock_products.product_category_id' , $productCategory->id);
        }

        if($product_category_id == -100){
            $productCategory = $category;
        }


        $productCategories1 = clone $productCategories;
        $main_categories = $productCategories1->where('parent_id' , null);
        $productCategories2 = clone $productCategories;
        $child_categories = $productCategories2->where('parent_id' , '<>' , '');

        if($check_fixed_price == 1){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , false);
        }

        $search = $request->get('search');
        if($search){
            $build_stocks = $build_stocks->where('build_stock_products.part_number' , $search);
        }
        $products1 = Clone $build_stocks;
        $product11= Clone $build_stocks;
        $totalCost = $products1->sum('build_stock_products.cost');

        $products2 = Clone $build_stocks;
        $totalWage = $products2->sum('build_stock_products.wages');
        $totalWagesInWeight = $products2->sum('build_stock_products.wages_in_weight');

        $product3 = Clone $build_stocks;
        $totalOriginalWage = $product3->sum('build_stock_products.original_wages');
        $totalOriginalWagesInWeight = $product3->sum('build_stock_products.original_wages_in_weight');

        $product4 = Clone $build_stocks;
        $totalQty = $product4->sum('build_stock_products.qty');

        if($product_category_id == -100){
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight, (build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.weight' , '<>' , 0)->pluck('totalWeight');
        }else{
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight,(build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->pluck('totalWeight');
        }

        $totalGoldWeight = array_sum(($totalGoldWeight->toArray()));


        $product8 = Clone $build_stocks;
        $totalWeight = $product8->select(DB::raw('build_stock_products.weight as totalWeight'))->pluck('totalWeight'); //NOT DONE

        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $build_stocks;
        $product51 = Clone $build_stocks;
        $children = $gold->children->where('name' , 'មាសដុំ')->first();


        if($product_category_id && $product_category_id != -100){
            if($productCategory->fixed_price){
                $totalPrice = $product5->sum('price') + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');
            }else {

                $product_fixed_clone = clone $build_stocks;
                $cost_fixed_clone = clone $build_stocks;
                $cost_notFix_clone = clone $build_stocks;
                $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
                $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

                $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
                $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

                $totalPrice = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');

                $cost_clone_product = clone $build_stocks;
                $totalCost = $cost_clone_product->sum('build_stock_products.cost');
                if ($check_fixed_price == 1) {
                    $totalPrice = $totalPriceFixed;
                    $totalCost = $totalCostFixed;
                } elseif ($check_fixed_price == 2) {
                    $totalPrice = $totalPriceNotFix;
                    $totalCost = $totalCostNotFix;
                }
                    $pig_clone = clone $build_stocks;
                    if ($productCategory->name == 'ផលិតផលបន្ថែម' || $productCategory->name == 'Other Stock' || $productCategory->name == 'Extra Stock') {
                        $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $productCategory->id)->first();
                        $totalPrice = $totalPrice + ($pig_price->totalPricePig);
                    }

//                    $pig_clone = clone $build_stocks;
//                    if ($product-categories['name'] == 'ផលិតផលបន្ថែម' || $product-categories['name'] == 'Other Stock' || $product-categories['name'] == 'Extra Stock') {
//                        $pig_price = $pig_clone->select(DB::raw('sum(products.price*products.qty) as totalPricePig'))->where('product_category_id', $product-categories->id)->first();
//                        $totalPrice = $totalPrice + ($pig_price->totalPricePig);
//                    }

//                $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id',$product51->pluck('build_stock_products.id'))->sum('price');
            }
        }else{
            $children_product = clone $build_stocks;
            if($children){
                $children_weight = $children_product->where('build_stock_products.product_category_id' , $children->id)->sum('build_stock_products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }
            $product_fixed_clone = clone $build_stocks;
            $cost_fixed_clone = clone $build_stocks;
            $cost_notFix_clone = clone $build_stocks;
            $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
            $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

            $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

            $totalPrice = $totalPriceNotFix + $totalPriceFixed;

            $cost_clone_product = clone $build_stocks;
            $totalCost = $cost_clone_product->sum('build_stock_products.cost');
            if ($check_fixed_price == 1) {
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            } elseif ($check_fixed_price == 2) {
                $totalPrice = $totalPriceNotFix;
                $totalCost = $totalCostNotFix;
            }
            $pig_clone = clone $build_stocks;
            if ($product_pig_category) {
                $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $product_pig_category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }

        $build_stocks = $build_stocks->orderBy('build_stock_products.name')->select('build_stock_products.*')->paginate($paginate);
        return view('build-stock-dates.list' , compact('build_stocks' , 'id' , 'paginate' , 'product_category_id' , 'gold', 'child_gold',
            'productCategories' , 'main_categories' , 'child_categories' , 'productCategory', 'platinume_Category' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'paginate' ,'totalCost','totalWage','totalOriginalWage','totalQty','totalPrice','totalGoldWeight' , 'totalWeight' , 'check_fixed_price' , 'date' ,
             'search' , 'category'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function print(Request $request , $id)
    {
        $product_category_id = $request->get('product_category_id');
        if($product_category_id == -5){
            $category = ProductCategory::whereIn('name' , ['មាស','Gold','ทอง'])->first();
        }elseif($product_category_id == -100)
        {
            $category = ProductCategory::get();
        }else{
            $category = ProductCategory::find($product_category_id);
        }

        $date = $request->date;

        $productCategories = ProductCategory::get();

        $check_fixed_price = $request->get('check_fixed_price');

        $gold_category = clone $productCategories;
        $gold=$gold_category->whereIn('name',['មាស','Gold','ทอง'])->first();

        $child_gold = ProductCategory::where('parent_id' , $gold->id)->get();

        $platinume_clone = clone $productCategories;
        $platinume_Category = $platinume_clone->whereIn('name' , ['ផ្លាកទីន','Platinum','แพลตตินัม'])->first();
        $child_p = ProductCategory::where('parent_id' , $platinume_Category->id)->get();

        $product_pig_category = ProductCategory::whereIn('name' , ['ផលិតផលបន្ថែម' , 'Other Stock' ,'Extra Stock'])->first();


        $paginate = $request->get('paginate')??25;
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $check_fixed_price = $request->get('check_fixed_price');

        if($product_category_id == -100){
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
            ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)
                ->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.build_stock_date_id', $id)->where('build_stock_products.weight' , '<>' , 0);
        }else{
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
                ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)
                ->where(function ($query) {
                    $query->where('build_stock_products.for_sale', 1)
                        ->orWhere(function ($q) {
                            $q->where('build_stock_products.for_sale', 0)
                                ->where('build_stock_products.weight', '<>', 0);
                        });
                })

                ->where('build_stock_products.build_stock_date_id', $id);

        }

        $productCategory = [];
        if($product_category_id != 0 && $product_category_id != -5 && $product_category_id != -100){
            $productCategory = ProductCategory::findOrFail($product_category_id);
            $build_stocks = $build_stocks->whereIn('build_stock_products.product_category_id' , $productCategory->IdvsAllSubCategories());
        }

        if($product_category_id == -5){
            $productCategory = $category;
            $build_stocks = $build_stocks->where('build_stock_products.product_category_id' , $productCategory->id);
        }

        if($product_category_id == -100){
            $productCategory = $category;
        }


        $productCategories1 = clone $productCategories;
        $main_categories = $productCategories1->where('parent_id' , null);
        $productCategories2 = clone $productCategories;
        $child_categories = $productCategories2->where('parent_id' , '<>' , '');

        if($check_fixed_price == 1){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , false);
        }

        $search = $request->get('search');
        if($search){
            $build_stocks = $build_stocks->where('build_stock_products.part_number' , $search);
        }
        $products1 = Clone $build_stocks;
        $product11= Clone $build_stocks;
        $totalCost = $products1->sum('build_stock_products.cost');

        $products2 = Clone $build_stocks;
        $totalWage = $products2->sum('build_stock_products.wages');
        $totalWagesInWeight = $products2->sum('build_stock_products.wages_in_weight');
        $product3 = Clone $build_stocks;
        $totalOriginalWage = $product3->sum('build_stock_products.original_wages');
        $totalOriginalWagesInWeight = $product3->sum('build_stock_products.original_wages_in_weight');

        $product4 = Clone $build_stocks;
        $totalQty = $product4->sum('build_stock_products.qty');

        if($product_category_id == -100){
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight, (build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.weight' , '<>' , 0)->pluck('totalWeight');
        }else{
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight,(build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->pluck('totalWeight');
        }

        $totalGoldWeight = array_sum(($totalGoldWeight->toArray()));


        $product8 = Clone $build_stocks;
        $totalWeight = $product8->select(DB::raw('build_stock_products.weight as totalWeight'))->pluck('totalWeight'); //NOT DONE

        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $build_stocks;
        $product51 = Clone $build_stocks;
        $children = $gold->children->where('name' , 'មាសដុំ')->first();

        if($product_category_id && $product_category_id != -100){
            if($productCategory->fixed_price){
                $totalPrice = $product5->sum('price') + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');
            }else {

                $product_fixed_clone = clone $build_stocks;
                $cost_fixed_clone = clone $build_stocks;
                $cost_notFix_clone = clone $build_stocks;
                $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
                $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

                $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
                $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

                $totalPrice = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');

                $cost_clone_product = clone $build_stocks;
                $totalCost = $cost_clone_product->sum('build_stock_products.cost');
                if ($check_fixed_price == 1) {
                    $totalPrice = $totalPriceFixed;
                    $totalCost = $totalCostFixed;
                } elseif ($check_fixed_price == 2) {
                    $totalPrice = $totalPriceNotFix;
                    $totalCost = $totalCostNotFix;
                }
                    $pig_clone = clone $build_stocks;
                    if ($productCategory->name == 'ផលិតផលបន្ថែម' || $productCategory->name == 'Other Stock' || $productCategory->name == 'Extra Stock') {
                        $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $productCategory->id)->first();
                        $totalPrice = $totalPrice + ($pig_price->totalPricePig);
                    }

//                    $pig_clone = clone $build_stocks;
//                    if ($product-categories['name'] == 'ផលិតផលបន្ថែម' || $product-categories['name'] == 'Other Stock' || $product-categories['name'] == 'Extra Stock') {
//                        $pig_price = $pig_clone->select(DB::raw('sum(products.price*products.qty) as totalPricePig'))->where('product_category_id', $product-categories->id)->first();
//                        $totalPrice = $totalPrice + ($pig_price->totalPricePig);
//                    }

//                $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id',$product51->pluck('build_stock_products.id'))->sum('price');
            }
        }else{
            $children_product = clone $build_stocks;
            if($children){
                $children_weight = $children_product->where('build_stock_products.product_category_id' , $children->id)->sum('build_stock_products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }
            $product_fixed_clone = clone $build_stocks;
            $cost_fixed_clone = clone $build_stocks;
            $cost_notFix_clone = clone $build_stocks;
            $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
            $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

            $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

            $totalPrice = $totalPriceNotFix + $totalPriceFixed;

            $cost_clone_product = clone $build_stocks;
            $totalCost = $cost_clone_product->sum('build_stock_products.cost');
            if ($check_fixed_price == 1) {
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            } elseif ($check_fixed_price == 2) {
                $totalPrice = $totalPriceNotFix;
                $totalCost = $totalCostNotFix;
            }
            $pig_clone = clone $build_stocks;
            if ($product_pig_category) {
                $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $product_pig_category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }

        $build_stocks = $build_stocks->orderBy('build_stock_products.name')->select('build_stock_products.*')->get();

        return view('build-stock-dates.print' , compact('build_stocks' , 'id' , 'paginate' , 'product_category_id' , 'gold', 'child_gold',
            'productCategories' , 'main_categories' , 'child_categories' , 'productCategory', 'platinume_Category' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'paginate' ,'totalCost','totalWage','totalOriginalWage','totalQty','totalPrice','totalGoldWeight' , 'totalWeight' , 'check_fixed_price' , 'date' ,
             'search' , 'category'));
    }

    public function printSummary(Request $request , $id)
    {
        $product_category_id = $request->get('product_category_id');
        if($product_category_id == -5){
            $category = ProductCategory::whereIn('name' , ['មាស','Gold','ทอง'])->first();
        }elseif($product_category_id == -100)
        {
            $category = ProductCategory::get();
        }else{
            $category = ProductCategory::find($product_category_id);
        }

        $date = $request->date;

        $productCategories = ProductCategory::where('is_main',true)->get();

        $check_fixed_price = $request->get('check_fixed_price');

        $gold_category = clone $productCategories;
        $gold=$gold_category->whereIn('name',['មាស','Gold','ทอง'])->first();

        $child_gold = ProductCategory::where('parent_id' , $gold->id)->get();

        $platinume_clone = clone $productCategories;
        $platinume_Category = $platinume_clone->whereIn('name' , ['ផ្លាកទីន','Platinum','แพลตตินัม'])->first();
        $child_p = ProductCategory::where('parent_id' , $platinume_Category->id)->get();


        $product_pig_category = ProductCategory::whereIn('name' , ['ផលិតផលបន្ថែម' , 'Other Stock' ,'Extra Stock'])->first();


        $paginate = $request->get('paginate')??25;
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $check_fixed_price = $request->get('check_fixed_price');

        if($product_category_id == -100){
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
            ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.build_stock_date_id', $id)->where('build_stock_products.weight' , '<>' , 0);
        }else{
            $build_stocks = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
            ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)
                ->where('build_stock_products.for_sale' , 1)->whereNull('build_stock_products.deleted_at')
            ->where('build_stock_products.build_stock_date_id', $id);
            $accessories = BuildStockProduct::join('product_categories', 'product_categories.id', '=', 'build_stock_products.product_category_id')
                ->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
                ->where('build_stock_products.build_stock_date_id', $id)
//                ->where('product_category_id' , 2)
                ->where('build_stock_products.weight' , '<>' , 0);
        }

        $productCategory = [];
        if($product_category_id != 0 && $product_category_id != -5 && $product_category_id != -100){
            $productCategory = ProductCategory::findOrFail($product_category_id);
            $build_stocks = $build_stocks->whereIn('build_stock_products.product_category_id' , $productCategory->IdvsAllSubCategories());
            $accessories = $accessories->whereIn('build_stock_products.product_category_id' , $productCategory->IdvsAllSubCategories());
        }

        if($product_category_id == -5){
            $productCategory = $category;
            $build_stocks = $build_stocks->where('build_stock_products.product_category_id' , $productCategory->id);
            $accessories = $accessories->where('build_stock_products.product_category_id' , $productCategory->id);
        }

        if($product_category_id == -100){
            $productCategory = $category;
        }


        $productCategories1 = clone $productCategories;
        $main_categories = $productCategories1->where('parent_id' , null);
        $productCategories2 = clone $productCategories;
        $child_categories = $productCategories2->where('parent_id' , '<>' , '');

        if($check_fixed_price == 1){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , true);
            $accessories = $accessories->where('build_stock_products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $build_stocks = $build_stocks->where('build_stock_products.fixed_price' , false);
            $accessories = $accessories->where('build_stock_products.fixed_price' , false);
        }

        $search = $request->get('search');
        if($search){
            $build_stocks = $build_stocks->where('build_stock_products.part_number' , $search);
            $accessories = $accessories->where('build_stock_products.part_number' , $search);
        }
        $products1 = Clone $build_stocks;
        $product11= Clone $build_stocks;
        $totalCost = $products1->sum('build_stock_products.cost');

        $products2 = Clone $build_stocks;
        $totalWage = $products2->sum('build_stock_products.wages');
        $totalWagesInWeight = $products2->sum('build_stock_products.wages_in_weight');

        $product3 = Clone $build_stocks;
        $totalOriginalWage = $product3->sum('build_stock_products.original_wages');
        $totalOriginalWagesInWeight = $product3->sum('build_stock_products.original_wages_in_weight');

        $product4 = Clone $build_stocks;
        $totalQty = $product4->sum('build_stock_products.qty');
        $totalQtyAccessory = $accessories->sum('build_stock_products.qty');

        if($product_category_id == -100){
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight, (build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->where('build_stock_products.for_sale' , 0)
            ->where('build_stock_products.weight' , '<>' , 0)->pluck('totalWeight');
        }else{
            $product7 = Clone $build_stocks;
            $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight,(build_stock_products.weight*product_categories.purity_level) as totalWeight'))->where('build_stock_products.is_active' , true)->where('build_stock_products.qty' , '>' , 0)->pluck('totalWeight');
        }

        $totalGoldWeight = array_sum(($totalGoldWeight->toArray()));


        $product8 = Clone $build_stocks;
        $totalWeight = $product8->select(DB::raw('build_stock_products.weight as totalWeight'))->pluck('totalWeight'); //NOT DONE

        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $build_stocks;
        $product51 = Clone $build_stocks;
        $children = $gold->children->where('name' , 'មាសដុំ')->first();


        if($product_category_id && $product_category_id != -100){
            if($productCategory->fixed_price){
                $totalPrice = $product5->sum('price') + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');
            }else {

                $product_fixed_clone = clone $build_stocks;
                $cost_fixed_clone = clone $build_stocks;
                $cost_notFix_clone = clone $build_stocks;
                $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
                $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

                $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
                $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

                $totalPrice = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->pluck('build_stock_products.id'))->sum('price');

                $cost_clone_product = clone $build_stocks;
                $totalCost = $cost_clone_product->sum('build_stock_products.cost');
                if ($check_fixed_price == 1) {
                    $totalPrice = $totalPriceFixed;
                    $totalCost = $totalCostFixed;
                } elseif ($check_fixed_price == 2) {
                    $totalPrice = $totalPriceNotFix;
                    $totalCost = $totalCostNotFix;
                }
                    $pig_clone = clone $build_stocks;
                    if ($productCategory->name == 'ផលិតផលបន្ថែម' || $productCategory->name == 'Other Stock' || $productCategory->name == 'Extra Stock') {
                        $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $productCategory->id)->first();
                        $totalPrice = $totalPrice + ($pig_price->totalPricePig);
                    }
            }
        }else{
            $children_product = clone $build_stocks;
            if($children){
                $children_weight = $children_product->where('build_stock_products.product_category_id' , $children->id)->sum('build_stock_products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }
            $product_fixed_clone = clone $build_stocks;
            $cost_fixed_clone = clone $build_stocks;
            $cost_notFix_clone = clone $build_stocks;
            $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price', false)->sum('build_stock_products.cost');
            $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price', true)->sum('build_stock_products.cost');

            $totalPriceNotFix = ($totalGoldWeight * $gold->selling_to_usd_exchange_rate) + BuildStockProductAttribute::whereIn('build_stock_product_id', $product51->where('build_stock_products.fixed_price', false)->pluck('build_stock_products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('build_stock_products.fixed_price', true)->sum('price');

            $totalPrice = $totalPriceNotFix + $totalPriceFixed;

            $cost_clone_product = clone $build_stocks;
            $totalCost = $cost_clone_product->sum('build_stock_products.cost');
            if ($check_fixed_price == 1) {
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            } elseif ($check_fixed_price == 2) {
                $totalPrice = $totalPriceNotFix;
                $totalCost = $totalCostNotFix;
            }
            $pig_clone = clone $build_stocks;
            if ($product_pig_category) {
                $pig_price = $pig_clone->select(DB::raw('sum(build_stock_products.price*build_stock_products.qty) as totalPricePig'))->where('product_category_id', $product_pig_category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }

        $build_stocks = $build_stocks->orderBy('build_stock_products.name')->select('build_stock_products.*')->get();

        $productCategoriesWithoutP = $productCategories->where('name' , '<>' , $platinume_Category->name);

        return view('build-stock-dates.print-summary' , compact('build_stocks' , 'id' , 'paginate' , 'product_category_id' , 'gold', 'child_gold',
            'productCategories' , 'main_categories' , 'child_categories' , 'productCategory', 'platinume_Category' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'paginate' ,'totalCost','totalWage','totalOriginalWage','totalQty','totalPrice','totalGoldWeight' , 'totalWeight' , 'check_fixed_price' , 'child_p' , 'date' ,
             'search' , 'category' , 'productCategoriesWithoutP' , 'accessories' , 'totalQtyAccessory'));
    }

    /**
     * Show the form for editing the specified resource.
     */

    public function show(Request $request)
    {
        $paginate = $request->get('paginate')??25;
        $build_stock_dates = BuildStockDate::orderBy('id' , 'DESC')->paginate($paginate);
        return view('build-stock-dates.index' , compact('build_stock_dates' , 'paginate'));
    }
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}

© KUJUNTI.ID