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


<?php

namespace App\Http\Controllers;

use App\Exports\CategoryProductExport;
use App\Exports\SampleProductExport;
use App\Exports\productAccessoryExport;
use App\Imports\ProductImport;
use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductCategory;
use App\Models\QtyHistory;
use App\Models\Sale;
use App\Models\Supplier;
use http\Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class WarehouseController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function __construct()
    {
        set_time_limit(500000);
        $this->middleware('permission:reports-category-list' , ['only' => ['warehouse_product_category_show' , 'index' , 'print' , 'export']]);
    }
    public function index(Request $request , $id)
    {
        $category = ProductCategory::findOrFail($id);
        $platinumCategories = ProductCategory::where('parent_id' , $id)->get();
        $paginate = $request->get('paginate')??25;
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $platinum_category_id = $request->get('platinum_category');
        $check_fixed_price = $request->get('check_fixed_price');
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $products = Product::join('product_categories', 'product_categories.id', '=', 'products.product_category_id')->where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 1)->whereIn('products.product_category_id' , $category->IdvsAllSubCategories())->orderBy('products.name');

        if($platinum_category_id){
            $products = $products->where('products.product_category_id' , $platinum_category_id);
        }
        if($check_fixed_price == 1){
            $products = $products->where('products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $products = $products->where('products.fixed_price' , false);
        }
        $search = $request->get('search');
        if($search){
            $products = $products->where('products.part_number' , $search);
        }
        $products1 = Clone $products;
        $product11= Clone $products;
        $totalCost = $products1->sum('products.cost');
        $products2 = Clone $products;
        $productsPrice = Clone $products;
        $productRate = Clone $products;
        $totalWage = $products2->sum('products.wages');
        $totalWagesInWeight = $products2->sum('products.wages_in_weight');
        $totalWagesInWeightPrice = $productsPrice->sum('products.wages_in_weight') * $category->sellingPrice();

        $product3 = Clone $products;
        $productPrice = Clone $products;
//        $totalOriginalWage = $product3->sum('products.original_wages');
        $totalOriginalWage = $product3->get()->sum(function ($item) {
            return $item->cost_wagesInWeight();
        });
        $totalOriginalWagesInWeight = $product3->sum('products.original_wages_in_weight');
        $totalOriginalWagesInWeightPrice = $productPrice->sum('products.original_wages_in_weight') * $category->price();

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

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

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

        $product8 = Clone $products;
        $totalWeight = $product8->select(DB::raw('products.weight as totalWeight'))->pluck('totalWeight');

        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $products;
        $product51 = Clone $products;
        $children = $category->children->where('name' , 'មាសដុំ')->first();
        if($category->fixed_price){
            $totalPrice = $product5->sum('price')+ ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');
        }
        else{
            $children_product = clone $products;
            if($children){
                $children_weight = $children_product->where('product_category_id' , $children->id)->sum('products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }

            $product_fixed_clone = clone $products;
            $cost_fixed_clone = clone $products;
            $cost_notFix_clone = clone $products;
            //Total 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');

            //Total Price
            $totalPriceNotFix = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->where('products.fixed_price' , false)->pluck('products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('products.fixed_price' , true)->sum('price');

            $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');

            $cost_clone_product = clone $products;
            $totalCost = $cost_clone_product->sum('products.cost');
            if($check_fixed_price == 1){
                $totalPrice = $totalPriceFixed;
                $totalCost = $totalCostFixed;
            }elseif($check_fixed_price == 2){
                $totalPrice = $totalPriceNotFix;
                $totalCost = $totalCostNotFix;
            }
            $pig_clone = clone $products;
            if($category->name == 'ផលិតផលបន្ថែម' || $category->name == 'Other Stock' || $category->name == 'Extra Stock'){
                $pig_price = $pig_clone->select(DB::raw('sum(products.price*products.qty) as totalPricePig'))->where('product_category_id' , $category->id)->first();
                $totalPrice = $totalPrice + ($pig_price->totalPricePig);
            }
        }
        $totalPrice = $totalWagesInWeight + $totalPrice;
        if($category->fixed_price == true){
            $totalCost = $totalCost + $totalOriginalWage;
        }
        $products = $products->orderBy('products.id' , 'desc')->select('products.*')->paginate($paginate);
        return view('warehouses.index', compact('products', 'gold' , 'children' , 'check_fixed_price' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'totalWeight' , 'category' , 'platinumCategories' , 'platinum_category_id' , 'paginate' , 'end_date' , 'start_date' , 'search','totalCost','totalWage','totalOriginalWage',
            'totalQty','totalPrice','totalGoldWeight'));

    }

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

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

    public function print(Request $request , $id)
    {
        $category = ProductCategory::findOrFail($id);
        $platinumCategories = ProductCategory::where('parent_id' , $id)->get();
        $paginate = $request->get('paginate')??25;
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $platinum_category_id = $request->get('platinum_category');
        $check_fixed_price = $request->get('check_fixed_price');
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $products = Product::join('product_categories', 'product_categories.id', '=', 'products.product_category_id')->where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 1)->whereIn('products.product_category_id' , $category->IdvsAllSubCategories())->orderBy('products.name');

        if($platinum_category_id){
            $products = $products->where('products.product_category_id' , $platinum_category_id);
        }
        if($check_fixed_price == 1){
            $products = $products->where('products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $products = $products->where('products.fixed_price' , false);
        }
        $search = $request->get('search');
        if($search){
            $products = $products->where('products.part_number' , $search);
        }
        $products1 = Clone $products;
        $product11= Clone $products;
        $totalCost = $products1->sum('products.cost');
        $products2 = Clone $products;
        $productsPrice = Clone $products;
        $productRate = Clone $products;
//        $totalWage = $products2->sum('products.wages');
        $totalWage = $products2->get()->sum(function ($items) {
            return $items->selling_wagesInWeight();
        });
        $totalWagesInWeight = $products2->sum('products.wages_in_weight');
        $totalWagesInWeightPrice = $productsPrice->sum('products.wages_in_weight') * $category->sellingPrice();

        $product3 = Clone $products;
        $productPrice = Clone $products;
//        $totalOriginalWage = $product3->sum('products.original_wages');
        $totalOriginalWage = $product3->get()->sum(function ($item) {
            return $item->cost_wagesInWeight();
        });
        $totalOriginalWagesInWeight = $product3->sum('products.original_wages_in_weight');
        $totalOriginalWagesInWeightPrice = $productPrice->sum('products.original_wages_in_weight') * $category->price();

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

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

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

        $product8 = Clone $products;
        $totalWeight = $product8->select(DB::raw('products.weight as totalWeight'))->pluck('totalWeight');

        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $products;
        $product51 = Clone $products;
        $children = $category->children->where('name' , 'មាសដុំ')->first();
        if($category->fixed_price){
            $totalPrice = $product5->sum('price')+ ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');
        }
        else{

            $children_product = clone $products;
            if($children){
                $children_weight = $children_product->where('product_category_id' , $children->id)->sum('products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }

            $product_fixed_clone = clone $products;
            $cost_fixed_clone = clone $products;
            $cost_notFix_clone = clone $products;
            //Total 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');

            //Total Price
            $totalPriceNotFix = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->where('products.fixed_price' , false)->pluck('products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('products.fixed_price' , true)->sum('price');

            $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');

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

        $totalPrice = $totalWagesInWeight + $totalPrice;
        if($category->fixed_price == true){
            $totalCost = $totalCost + $totalOriginalWage;
        }
        $products = $products->orderBy('products.id' , 'desc')->select('products.*')->get();
        return view('warehouses.print', compact('products' , 'children' , 'totalWeight' , 'check_fixed_price' , 'category' , 'gold'
            , 'platinumCategories' , 'platinum_category_id' , 'paginate' , 'end_date' , 'start_date' , 'search','totalCost','totalWage','totalOriginalWage',
            'totalQty','totalPrice','totalGoldWeight' , 'totalOriginalWagesInWeight' , 'totalWagesInWeight'));

    }

    public function export(Request $request, $id)
    {
        return Excel::download(new CategoryProductExport($id , $request->get('start_date') ,
            $request->get('end_date') , $request->get('search') , $request->get('paginate') , $request->get('platinum_category')
            , $request->get('check_fixed_price')), 'Product Report.xlsx');
    }

    public function sample()
    {
        return Excel::download(new SampleProductExport(), 'Product Sample.xlsx');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $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 warehouse_product_category_show(Request $request)
    {
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $product = Product::all();
        $countProduct = count($product);

        $totalDeposit = $countProduct;
        $totalWithdraw = 0;
        $totalRebate = 0;
        $totalMember = 0;

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

        return view('warehouses.category-show', compact('start_date', 'end_date', 'totalDeposit', 'totalWithdraw',
            'totalRebate', 'totalMember' , 'productCategories'));
    }

    public function modify(Request $request , $id)
    {
        $request->validate([
            'browModify' => 'required|file|mimes:xlsx,xls|max:2048',
        ]);

        if (empty($request->file('browModify')->getRealPath())) {
            return back()->with('success', 'No file selected');
        } else {
            $import = new ProductImport();
            $array = Excel::toCollection($import, $request->file('browModify'));
            foreach ($array as $i => $row) {
                foreach ($row as $s => $val) {
                    if ($s > 0) {
                        $products = Product::get();
                        $part_number = $val[6] ?? '';
                        $sales = Sale::where('part_number' , $part_number)->first();
                        $product = Product::where('part_number' , $part_number)->first();

                        if($product && $product->qty > 0){
                            if($val[12]){
                                $original_wages = ($val[12] * $val[16]) / 1000;
                                $original_wages_in_weight = $val[12]??0;
                            }else{
                                $original_wages = $val[13]??0;
                                $val16 = $val[16]??0;
                                $val13 = $val[13]??0;

                                $original_wages_in_weight = $val13 / ($val16 * 1000);
                            }

                            if($product->productCategory->fixed_price == false){
                                $wages_in_weight = $val[24]??0;
                                $selling_wages = ($val[24] * $val[16]) / 1000;
                            }else{
                                $selling_wages = $val[24]??0;
                                $wages_in_weight = $val[24]/($val[16]*1000);
                            }
                            $fixed_price = false;
                            $selling_price = 0;
                            $sellPrice = $val[28]??0;
                            if($sellPrice){
                                $selling_price = $val[28]??0;
                                $fixed_price = true;
                            }
                            $cost = $val[22]??0;
                            if($fixed_price == true){
                                $cost_invoice = $val[23] - $cost - $original_wages;
                            }else{
                                $cost_invoice = $val[23] - $cost;
                            }

                            $product->update([
//                                'original_wages' => $original_wages??0,
//                                'wages' => $selling_wages??0,
//                                'price' => $selling_price??0,
//                                'cost' => $cost_invoice??0,
//                                'wages_in_weight' => $wages_in_weight,
//                                'original_wages_in_weight' => $original_wages_in_weight,
                                'weight' => $val[8]??0,
//                                'purity_upgrade_to' => $val[31]??'',
//                                'selling_purity' => $val[32]??'',
                            ]);
                        }
//                        if(!$sales && !$product){
//
//                            $productCategory = ProductCategory::where('id', $id)->first();
//                            $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
//
//                            if($productCategory->name == 'ផ្លាកទីន' || $productCategory->name == 'Platinum' || $productCategory->name == 'แพลตตินัม') {
//
//                                if (!$val[9]) {
//                                    $product_category = ProductCategory::whereIn('name', ['ផ្លាកទីនអ៊ីតាលី', 'អ៊ីតាលី', 'Platinum Italy', 'Italy', 'แพลตตินั่มอิตาลี', 'อิตาลี'])->first();
//                                } else {
//                                    $product_category = ProductCategory::where('name', 'like', 'ផ្លាកទីន%')->where('purity_level', $val[9])->first();
//                                }
//                                if(!$product_category && $val[32]){
//                                    $product_category = ProductCategory::where('name' , 'like' , 'ផ្លាកទីន%')->where('purity_level', $val[32])->first();
//                                }
//                            }
//                            $purity_level = $val[9]??'';
//
//                            if($productCategory->name === $gold->name){
//                                if($val[7] == 'មាសថៃ'){
//                                    $product_category = ProductCategory::whereIn('name' , ['មាសថៃ' , 'Thai Gold' , 'ทองไทย' ,])->first();
//                                }elseif($val[7] == 'មាសដុំ'){
//                                    $product_category = ProductCategory::whereIn('name' , ['មាសដុំ'])->first();
//                                }else{
//                                    $product_category = $productCategory;
//                                }
//                            }
//
//                            if($val[12]){
//                                $original_wages = ($val[12] * $val[16]) / 1000;
//                                $original_wages_in_weight = $val[12]??0;
//                            }else{
//                                $original_wages = $val[13]??0;
//                                $val16 = $val[16]??0;
//                                $val13 = $val[13]??0;
//
//                                $original_wages_in_weight = $val13 / ($val16 * 1000);
//                            }
//
//                            if($productCategory->fixed_price == false){
//                                $wages_in_weight = $val[24]??0;
//                                $selling_wages = ($val[24] * $val[16]) / 1000;
//                            }else{
//                                $selling_wages = $val[24]??0;
//                                $wages_in_weight = $val[24]/($val[16]*1000);
//                            }
//
//                            $product_category_id = $product_category->id??$productCategory->id;
//                            try {
//                                $price = $val[25]??0;
//                                $cost = $val[22]??0;
//                            }catch (Exception $e){
//                                echo 'Caught exception: ',  $e->getMessage(), "\n";
//                            }
//
//                            $weight_description = '';
//                            if($productCategory->fixed_price){
//                                $weight_description = $val[9]??'';
//                            }
//                            if($val[4]){
//                                $supplier = Supplier::where('name' , $val[4])->first();
//                                if(!$supplier){
//                                    $supplier = Supplier::create([
//                                        'name' => $val[4],
//                                        'phone' => '',
//                                        'note' => '',
//                                    ]);
//                                }
//                            }
//                            $supplier_id = $supplier->id??0;
//                            $fixed_price = false;
//                            $selling_price = 0;
//                            $sellPrice = $val[28]??0;
//                            if($sellPrice){
//                                $selling_price = $val[28]??0;
//                                $fixed_price = true;
//                            }
//                            if($fixed_price == true){
//                                $cost_invoice = $val[23] - $cost - $original_wages;
//                            }else{
//                                $cost_invoice = $val[23] - $cost;
//                            }
//
//                            $qty = $val[5]??1;
//
//                            $products = Product::create([
//                                'fixed_price' => $fixed_price,
//                                'supplier_id' => $supplier_id,
//                                'supplier_rate' => $val[16]??'',
//                                'gia_number' => $val[18],
//                                'part_number' => $val[6],
//                                'name' => $val[7]??'',
//                                'weight' => $val[8]??0,
//                                'original_wages' => $original_wages??0,
//                                'wages' => $selling_wages??0,
//                                'diamond_size' => $val[17]??'',
//                                'weight_description' => $weight_description,
//                                'cost' => $cost_invoice??0,
//                                'price' => $selling_price??0,
//                                'product_category_id' => $product_category_id,
//                                'description' => $val[29]??'',
//                                'qty' => $qty,
//                                'for_sale' => 1,
//                                'purity_level' => $purity_level??'',
//                                'user_id' => Auth::user()->id,
//                                'wages_in_weight' => $wages_in_weight,
//                                'original_wages_in_weight' => $original_wages_in_weight,
//                                'purity_upgrade_to' => $val[31]??'',
//                                'selling_purity' => $val[32]??'',
//                            ]);
//                            if($price != 0 && $price != ''){
//                                ProductAttribute::create([
//                                    'name' => $val[20]??'',
//                                    'cost' => $cost??0,
//                                    'price' => $price??0,
//                                    'qty' => $val[21]??'',
//                                    'product_id' => $products->id,
//                                ]);
//                            }
//
//                            QtyHistory::create([
//                                'qty' => $products->qty,
//                                'qty_after_transaction' => $products->qty,
//                                'qty_before_transaction' => 0,
//                                'product_id' => $products->id,
//                                'product_category_id' => $products->product_category_id,
//                                'user_id' => Auth::user()->id,
//                                'note' => '',
//                                'grn' => $val[30]??'',
//                            ]);
//
//                        }
                    }
                }
            }
        }
        return redirect()->back();
    }
    public function import(Request $request , $id)
    {
        $request->validate([
            'browFile' => 'required|file|mimes:xlsx,xls|max:2048',
        ]);

        if (empty($request->file('browFile')->getRealPath())) {
            return back()->with('success', 'No file selected');
        } else {
            $import = new ProductImport();
            $array = Excel::toCollection($import, $request->file('browFile'));
            foreach ($array as $i => $row) {
                foreach ($row as $s => $val) {
                    if ($s > 0) {
                        $productCategory = ProductCategory::where('id', $id)->first();
                        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();

                        if($productCategory->name == 'ផ្លាកទីន' || $productCategory->name == 'Platinum' || $productCategory->name == 'แพลตตินัม') {

                                $product_category = ProductCategory::where('name', 'like', 'ផ្លាកទីន%')->where('purity_level', $val[9])->first();

                            if(!$product_category && $val[32]){
                                $product_category = ProductCategory::where('name' , 'like' , 'ផ្លាកទីន%')->where('purity_level', $val[32])->first();
                            }
                        }

                        if ($val[1] == 'ITALY') {
                            $product_category = ProductCategory::whereIn('name', ['ផ្លាកទីនអ៊ីតាលី', 'អ៊ីតាលី', 'Platinum Italy', 'Italy', 'แพลตตินั่มอิตาลี', 'อิตาลี'])->first();
                        }

                        $purity_level = $val[9]??'';

                        if($productCategory->name === $gold->name){
                            if($val[7] == 'មាសថៃ' || $val[1] == 'THAI GOLD'){
                                $product_category = ProductCategory::whereIn('name' , ['មាសថៃ' , 'Thai Gold' , 'ทองไทย' ,])->first();
                            }elseif($val[7] == 'មាសដុំ'){
                                $product_category = ProductCategory::whereIn('name' , ['មាសដុំ'])->first();
                            }elseif($val[1] == 'ផ្លាកទីនអ៊ីតាលី'|| $val[1] == 'អ៊ីតាលី'|| $val[1] == 'ITALY'|| $val[1] == 'Platinum Italy'|| $val[1] == 'Italy'|| $val[1] == 'แพลตตินั่มอิตาลี'|| $val[1] == 'อิตาลี'){
                                $product_category = ProductCategory::whereIn('name', ['ផ្លាកទីនអ៊ីតាលី', 'អ៊ីតាលី', 'Platinum Italy', 'Italy', 'แพลตตินั่มอิตาลี', 'อิตาลี'])->first();
                            }else{
                                $product_category = $productCategory;
                            }
                        }


                        $sell_rate = $productCategory->sellingPrice($val[32]);
                        $cost_rate = $productCategory->price(0,$val[32]);
                        $val16 = $val[16]??0;
                        $val13 = $val[13]??0;

                        $cost_rate1 = ($val16/1000)??$cost_rate;

//                        if($val[12]){
//                            $original_wages = ($val[12] * $cost_rate1);
//                            $original_wages_in_weight = $val[12]??0;
//                        }else{
//                            if($productCategory->name == 'ផលិតផលបន្ថែម'){
//                                $original_wages = 0;
//                                $original_wages_in_weight = 0;
//                            }else{
//                                $original_wages = $val[13]??0;
//
//
//                                // $original_wages_in_weight = ($val[13]*$val[16])/1000;
//
//
//                                if($val[16]){
//
//
////                                    $original_wages_in_weight = number_format(($val13 / ($cost_rate)) * 1_000_000 , 4 , '.' , '');
//
//
//                                    $original_wages_in_weight = number_format($val13 / $cost_rate1 , 2);
//                                }else{
//                                    $original_wages_in_weight = $val[12]??0;
//                                }
//
//                            }
//                        }
//
//                        if($productCategory->fixed_price == false){
//                            if (is_numeric($val[24]) && is_numeric($val[16]) && $val[16]) {
//                                $wages_in_weight = $val[24]??0;
//                                $selling_wages = ($val[24] * $sell_rate);
//                            }else{
//                                $wages_in_weight = $val[24]??0;
//                                $selling_wages = ($wages_in_weight + $original_wages_in_weight) * $sell_rate ;
//
//                            }
//                        }else{
//                            $selling_wages = $val[24]??0;
//
//
////                            $wages_in_weight = $val[24]/($sell_rate);
//
//
//                            $wages_in_weight = 0;
//                        }

                        if($productCategory->fixed_price == true){
                            $selling_wages = $val[24]??0;
                            $wages_in_weight = 0;
                            $original_wages = $val[13]??0;
                            $original_wages_in_weight = 0;
                        }else{
                            $selling_wages = 0;
                            $wages_in_weight = $val[24]??0;
                            $original_wages = $val[13]??0;
                            $original_wages_in_weight = 0;
                        }


//                        if($val[12]){
//                            $original_wages = $val[12]??0;
//                        }else{
//                            $original_wages = ($val[13] * $val[16]) / 1000;
//                        }

                        $product_category_id = $product_category->id??$productCategory->id;
                        try {
                            $price = $val[25]??0;
                            $cost = $val[22]??0;
                        }catch (Exception $e){
                            echo 'Caught exception: ',  $e->getMessage(), "\n";
                        }

                        $weight_description = '';
                        if($productCategory->fixed_price){
                            $weight_description = $val[9]??'';
                        }
                        if($val[4]){
                            $supplier = Supplier::where('name' , $val[4])->first();
                            if(!$supplier){
                                $supplier = Supplier::create([
                                    'name' => $val[4],
                                    'phone' => '',
                                    'note' => '',
                                ]);
                            }
                        }
                        $supplier_id = $supplier->id??0;
                        $fixed_price = false;
                        $selling_price = 0;
                        $sellPrice = $val[28]??0;
                        if($sellPrice){
                            $selling_price = $val[28]??0;
                            $fixed_price = true;
                        }

                        // if($fixed_price == true){
                        //     $cost_invoice = $val[23] - $cost - $original_wages;
                        // }else{
                        //     $cost_invoice = floatval($val[23]) - $cost;
                        // }

                        // $cost_invoice = floatval($val[23]) - $cost;
                        $cost_invoice = floatval($val[23]);

                        $qty = $val[5]??1;
                        $old_code = '';
                        if($val[33]){
                            $old_code = $val[33];
                        }

                        $products = Product::create([
                            'fixed_price' => $fixed_price,
                            'supplier_id' => $supplier_id,
                            'supplier_rate' => $val[16]??'',
                            'gia_number' => $val[18],
                            'part_number' => $val[6],
                            'name' => $val[7]??'',
                            'weight' => $val[8]??0,
                            'original_wages' => $original_wages??0,
                            'wages' => $selling_wages??0,
                            'diamond_size' => $val[17]??'',
                            'weight_description' => $weight_description,
                            'cost' => $cost_invoice??0,
                            'price' => $selling_price??0,
                            'product_category_id' => $product_category_id,
                            'description' => $val[29]??'',
                            'qty' => $qty,
                            'for_sale' => 1,
                            'purity_level' => $purity_level??'',
                            'user_id' => Auth::user()->id,
                            'wages_in_weight' => $wages_in_weight,
                            'original_wages_in_weight' => $original_wages_in_weight,
                            'purity_upgrade_to' => $val[31]??'',
                            'selling_purity' => $val[32]??'',
                            'old_code' => $old_code,
                            'original_invoice' => $val[34]??'',
                        ]);
                        if($price != 0 && $price != ''){
                            ProductAttribute::create([
                                'name' => $val[20]??'',
                                'cost' => $cost??0,
                                'price' => $price??0,
                                'qty' => $val[21]??'',
                                'product_id' => $products->id,
                            ]);
                        }

                        QtyHistory::create([
                            'qty' => $products->qty,
                            'qty_after_transaction' => $products->qty,
                            'qty_before_transaction' => 0,
                            'product_id' => $products->id,
                            'product_category_id' => $products->product_category_id,
                            'user_id' => Auth::user()->id,
                            'note' => '',
                            'grn' => $val[30]??'',
                        ]);

                    }
                }
            }
        }

        return redirect()->route('warehouse-product.index' , $id)->with('__("message.successfully")');
    }

    public function productAccessory(Request $request){
        $paginate = $request->get('paginate')??25;
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $productCategories = ProductCategory::get();
        $platinum_category_id = $request->get('platinum_category');
        $check_fixed_price = $request->get('check_fixed_price');
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $products = Product::
        join('product_categories', 'products.product_category_id', '=', 'product_categories.id')->
        where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 0)->where('products.weight' , '<>' , 0);
        if($platinum_category_id){
            $products = $products->where('product_category_id' , $platinum_category_id);
        }
        if($check_fixed_price == 1){
            $products = $products->where('products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $products = $products->where('products.fixed_price' , false);
        }
        $search = $request->get('search');
        if($search){
            $products = $products->where('products.part_number' , $search);
        }
        $products2 = Clone $products;
        $productsPrice = Clone $products;
        $totalWage = $products2->sum('products.wages');
        $totalWagesInWeight = $products2->sum('products.wages_in_weight');
        $totalWagesInWeightPrice = $productsPrice->sum('products.wages_in_weight') * $gold->sellingPrice();

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

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

        $product7 = Clone $products;
        $totalGoldWeight = $product7
        ->select(DB::raw('products.weight, (products.weight * product_categories.purity_level) as totalWeight'))->where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 0)
        ->where('products.weight' , '<>' , 0)
        ->pluck('totalWeight');

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

        $product8 = Clone $products;
        $totalWeight = $product8->select(DB::raw('products.weight as totalWeight'))->pluck('totalWeight');
        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $products;
        $product51 = Clone $products;
        $children = $gold->children->where('name' , 'មាសដុំ')->first();
        if($gold->fixed_price){
            $totalPrice = $product5->sum('products.price')+ ProductAttribute::whereIn('product_id',$product51->pluck('id'))->sum('price');
        }
        else{
            $children_product = clone $products;
            if($children){
                $children_weight = $children_product->where('products.product_category_id' , $children->id)->sum('products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }
            $product_fixed_clone = clone $products;
            //Total Cost

            //Total Price
            $totalPriceNotFix = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->where('products.fixed_price' , false)->pluck('products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('products.fixed_price' , true)->sum('products.price');

            $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');

            if($check_fixed_price == 1){
                $totalPrice = $totalPriceFixed;
            }elseif($check_fixed_price == 2){
                $totalPrice = $totalPriceNotFix;
            }
        }
        $totalPrice = $totalWagesInWeightPrice + $totalPrice;
        $products = $products->orderBy('products.name')->select('*')->paginate($paginate);
        return view('warehouses.accessory', compact('products', 'gold' , 'children' , 'check_fixed_price' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'totalWeight' , 'platinum_category_id' , 'paginate' , 'end_date' , 'start_date' , 'search','totalWage','totalOriginalWage',
            'totalQty','totalPrice','totalGoldWeight' , 'productCategories'));
    }

    public function productAccessoryPrint(Request $request){
        $paginate = $request->get('paginate')??25;
        $start_date = $request->get('start_date') ?? date('Y-m-d');
        $end_date = $request->get('end_date') ?? date('Y-m-d');
        $dates = explode(' - ', $request->date);
        $gold=ProductCategory::whereIn('name',['មាស','Gold','ทอง'])->first();
        $productCategories = ProductCategory::get();
        $platinum_category_id = $request->get('platinum_category');
        $productCategory = ProductCategory::where('id' , $platinum_category_id)->first();
        $check_fixed_price = $request->get('check_fixed_price');
        if ($request->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $products = Product::join('product_categories', 'products.product_category_id', '=', 'product_categories.id')->
        where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 0)->where('products.weight' , '<>' , 0);
        if($platinum_category_id){
            $products = $products->where('product_category_id' , $platinum_category_id);
        }
        if($check_fixed_price == 1){
            $products = $products->where('products.fixed_price' , true);
        }elseif($check_fixed_price == 2){
            $products = $products->where('products.fixed_price' , false);
        }
        $search = $request->get('search');
        if($search){
            $products = $products->where('products.part_number' , $search);
        }
        $products2 = Clone $products;
        $productsPrice = Clone $products;
        $totalWage = $products2->sum('products.wages');
        $totalWagesInWeight = $products2->sum('products.wages_in_weight');
        $totalWagesInWeightPrice = $productsPrice->sum('products.wages_in_weight') * $gold->sellingPrice();

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

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

        $product7 = Clone $products;
        $totalGoldWeight = $product7
        ->select(DB::raw('products.weight, (products.weight * product_categories.purity_level) as totalWeight'))->where('products.is_active' , true)->where('products.qty' , '>' , 0)->where('products.for_sale' , 0)
        ->where('products.weight' , '<>' , 0)
        ->pluck('totalWeight');

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

        $product8 = Clone $products;
        $totalWeight = $product8->select(DB::raw('products.weight as totalWeight'))->pluck('totalWeight');
        $totalWeight = array_sum(($totalWeight->all()));
        $product5 = Clone $products;
        $product51 = Clone $products;
        $children = $gold->children->where('name' , 'មាសដុំ')->first();
        if($gold->fixed_price){
            $totalPrice = $product5->sum('products.price')+ ProductAttribute::whereIn('product_id',$product51->pluck('id'))->sum('price');
        }
        else{
            $children_product = clone $products;
            if($children){
                $children_weight = $children_product->where('products.product_category_id' , $children->id)->sum('products.weight');
                $totalGoldWeight = $totalGoldWeight + $children_weight;
            }
            $product_fixed_clone = clone $products;
            //Total Cost

            //Total Price
            $totalPriceNotFix = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->where('products.fixed_price' , false)->pluck('products.id'))->sum('price');
            $totalPriceFixed = $product_fixed_clone->where('products.fixed_price' , true)->sum('products.price');

            $totalPrice = ($totalGoldWeight*$gold->selling_to_usd_exchange_rate) + ProductAttribute::whereIn('product_id',$product51->pluck('products.id'))->sum('price');

            if($check_fixed_price == 1){
                $totalPrice = $totalPriceFixed;
            }elseif($check_fixed_price == 2){
                $totalPrice = $totalPriceNotFix;
            }
        }
        $totalPrice = $totalWagesInWeightPrice + $totalPrice;
        $products = $products->orderBy('products.name')->select('*')->paginate($paginate);
        return view('warehouses.accessory-print', compact('products', 'gold' , 'children' , 'check_fixed_price' , 'totalWagesInWeight' , 'totalOriginalWagesInWeight'
            , 'totalWeight' , 'platinum_category_id' , 'paginate' , 'end_date' , 'start_date' , 'search','totalWage','totalOriginalWage',
            'totalQty','totalPrice','totalGoldWeight' , 'productCategories' , 'productCategory'));
    }

    public function productAccessoryExport(Request $request)
    {
        return Excel::download(new productAccessoryExport($request->get('start_date') ,
            $request->get('end_date') , $request->get('search') , $request->get('paginate') , $request->get('platinum_category')
            , $request->get('check_fixed_price')), 'Product Report.xlsx');
    }

}

© KUJUNTI.ID