<?php

namespace App\Http\Controllers;

use App\GoodsIssueProduct;
use App\Group;
use App\Goodsin;
use App\Purchase;
use App\PurchaseProduct;
use App\Product;
use App\GoodsinProduct;
use App\QuotationProduct;
use App\Supplier;
use App\ProductUnit;
use App\RequisitionProduct;
use App\NewRequisitionProduct;
use App\NewRequisition;
use App\Requisition;
use App\Unit;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Builder;

use Illuminate\Support\Str;

class GoodsinController extends Controller
{
    // public function dlall() {
    //       $units = ProductUnit::get();
    //       $products = Product::All();
    //   $goods_in = GoodsinProduct::where(get_where())->where('supplier_id', null)->delete();
    //     dd('ok');
    // }

    public function index()
    {
        //GoodsinProduct::where('supplier_id',null)->update(['supplier_id'=>111]);

        $units = ProductUnit::get();

        $goods_in = GoodsinProduct::query()->where(get_where())->with('product', 'product.group')->get();
        $products = Product::all();
        return view('inventory.goodsin.add-goodsin', compact('products', 'units', 'goods_in'));
    }

    public function edit($id)
    {
        $goods_in = GoodsinProduct::find($id);
        $units = ProductUnit::get();
        $products = Product::all();
        return view('inventory.goodsin.edit-goodsin', compact('units', 'products', 'goods_in'));
    }

    public function update(Request $request, $id)
    {
        //return $request->all();
        $goods_in = GoodsinProduct::find($id);
        $goods_in->update($request->all());

        session()->flash('success', 'Item Information Updated');
        return back();
    }

    public function delete($id)
    {
        $goods_in = GoodsinProduct::find($id)->delete();

        if ($goods_in) {
            return response()->json(['success' => true]);
        } else {
            return response()->json(['success' => false]);
        }
    }

    public function setUnit($item_id)
    {
        //$units = ProductUnit::get();
        $item_unit = Product::find($item_id)->product_unit_id;
        return response()->json([
            //'units'=>$units,
            'item_unit' => $item_unit,
        ]);
    }

    public function newGoodsIn()
    {

        $unit = Auth::user()->active_unit;
        $products = Product::all();
        $purchases = Purchase::query()->with('requisition')->where('unit_id', $unit)->orderBy('id', 'desc')->get();

        return view('inventory.goodsin.new-goodsin', compact('purchases', 'products'));
    }

    public function getSupplier($purchase_id)
    {

        $purchase_product = PurchaseProduct::where('purchase_id', $purchase_id)->first();
        if ($purchase_product) {
            $supplier_id = $purchase_product->supplier_id;
            $supplier_name = Supplier::find($supplier_id)->name;
        } else {
            $supplier_name = 'No Name';
        }
        return response()->json(['supplier_name' => $supplier_name]);
    }

    public function mrReport()
    {

        $goods_in = Goodsin::query()->where(get_where())->orderBy('id', 'desc')->get();
        return view('inventory.goodsin.mr-report', compact('goods_in'));
    }

     public function mrReportDelete($id)
    {
        $goods_in = Goodsin::find($id);
        $delete_goods_in = $goods_in->delete();
        $delete_goods_in_product = GoodsinProduct::where('goodsin_id',$goods_in->id)->delete();

        if($delete_goods_in && $delete_goods_in_product){
            // check if any mrr for this purchase order is available or not
            $check_mrr = Goodsin::where('purchase_id',$goods_in->purchase_id)->first();
            if(!isset($check_mrr->id)){
                Purchase::find($goods_in->purchase_id)->update(['mrr_status'=>0]);
            }
            return response()->json(['success'=>true]);
        }
        return response()->json(['success'=>false]);
    }

    public function mrReportPrint($id)
    {
        // dd($id);

        $goods_in = Goodsin::with('products')->find($id);

        // $slice = Str::before($goods_in->note, ',Total Amount');
        // $slice = Str::after($slice, 'Tk ,');
        // $tvalue = Str::after($goods_in->note, ' Charge =19500 Tk ,');
        // $suppliers = QuotationProduct::query()->where('product_id',$id)->get()->groupBy('supplier_id');
        // $allProducts = QuotationProduct::query()->where('product_id',$requisition_id)->get()->groupBy('requisition_product_id');

        // dd($suppliers);

        $allProducts = QuotationProduct::query()->where('requisition_id',$id)->get()->groupBy('requisition_product_id');

          return view('inventory.goodsin.mr-report-print', compact('goods_in','suppliers','allProducts'));
    }

    public function store(Request $request)
    {
        // dd('this service is not abalable right now');

         $product = Product::query()->findOrFail($request->item);
            GoodsinProduct::insert([
                'goodsin_id' => 0,
                'group_id' => $product->group_id != null ? $product->group_id : null,
                'supplier_id' => 419,
                'unit_id' => auth_unit()?? null,
                'product_id' => $request->item,
                'product_unit_id' => $request->unit_id,
                'qty' => $request->qty,
                'price' => $request->price,
                'user_id' => Auth::id(),
                'created_at' => date('Y-m-d H:i:s'),
            ]);
    //     $data['supplier_id'] = 419;
    //     $data['goodsin_id'] = 0;
    //     $data['product_id'] = $request->item;
    //     $data['group_id'] = $product->group_id != null ? $product->group_id : null;
    //     $data['unit_id'] = auth_unit()?? null;
    //     $data['product_unit_id'] = $request->unit_id;
    //     $data['qty'] = $request->qty;
    //     $data['price'] = $request->price;
    //     $data['user_id'] = Auth::id();
    //     $goods_in_product = GoodsinProduct::create($data);

         /*Goods In Product Save End */

         session()->flash('success', 'Goods in Created successfully');
         return redirect()->back();
    }


    public function storeNew(Request $request)
    {

//        dd($request->all());
        $checkCount = 0;
        if(!empty($request->receive_qty)){
            foreach ($request->receive_qty as $checkKey => $checkQty) {
                if($checkQty > 0){
                    $checkCount++;
                }
            }
        }
        if($checkCount == 0){
            return back()->with('error','Invalid Data');
        }
        $total_receive_qty = 0;
        $qty_greater_than_zero = 0;
        $name_of_products = '';
        foreach ($request->receive_qty as $productKey => $data) {
            $purchase_product = PurchaseProduct::query()->where('purchase_id', $request->purchase_id)->where('product_id',$request->product_id[$productKey])->first();
            if($data > $purchase_product->qty){
                $qty_greater_than_zero++;
                $name_of_product =  Product::find($request->product_id[$productKey])->name;
                $name_of_products .= ''.$name_of_product.', ';
            }
            $total_receive_qty += intval($data);
        }

        if($qty_greater_than_zero > 0){
            session()->flash('error', 'Greater value given for '.$name_of_products);
            return redirect()->back();
        }

        // Rezaul Hoque 11/01/2021
        // this option modify for mothin vai requirement.0.5 value not working for this logic if ($total_receive_qty > 0) { so i changed logic 

        // if ($total_receive_qty > 0) {

        //     $unit = auth_unit()?? null;

        //     $request['user_id'] = Auth::id();
        //     $request['unit_id'] = $unit;
        //     $purchase = Purchase::query()->find($request->purchase_id);

        //     //$result = Goodsin::query()->where('unit_id', $unit)->get()->last();
        //     $result = Goodsin::query()->where('unit_id', $unit)->max('mrr_no');

        //     if($result != 0){
        //         $request['mrr_no'] = $result +1;
        //     }else{
        //         $request['mrr_no'] =1;
        //     }


        //     $goods_in = Goodsin::create($request->all());
        //     $purchase->update(['mrr_status'=>1]);
        //     $no_of_goods_in = 0;

        //     foreach ($request->product_id as $key => $product_id) {
        //         $purchase_product = PurchaseProduct::query()->where('purchase_id', $purchase->id)->where('product_id',$product_id)->first();
        //         if ($request->receive_qty[$key] > 0 && $request->receive_qty[$key] <= $purchase_product->qty) {
        //             $product = Product::query()->findOrFail($product_id);
        //             $data = new GoodsinProduct;
        //             $data->unit_id = $unit;

        //             $data->goodsin_id = $goods_in->id;
        //             $data->product_id = $product_id;
        //             $data->group_id = $product->group_id;
        //             $data->qty = $request->receive_qty[$key];
        //             $data->price = $request->price[$key];
        //             $data->purchase_id = $goods_in->purchase_id;
        //             $data->remarks = $request->remarks[$key];
        //             $data->supplier_id = $purchase->supplier_id;

        //             $data->requisition_id = $purchase->requisition_id;
        //             $data->product_unit_id = $product->product_unit_id;
        //             $data->save();

        //             $no_of_goods_in++;
        //         }

        //     }
        //     /*Goods In Product Save End */

        //     session()->flash('success', 'Goods in Created successfully');
        //     return redirect()->back();
        // } else {
        //     session()->flash('error', 'Nothing to Add');
        //     return redirect()->back();
        // }
        
        
            // Rezaul Hoque 11/01/2021
            // this option modify for mothin vai requirement.0.5 value not working for this logic if ($total_receive_qty > 0) { so i changed logic
        
            $unit = auth_unit()?? null;

            $request['user_id'] = Auth::id();
            $request['unit_id'] = $unit;
            $purchase = Purchase::query()->find($request->purchase_id);

            //$result = Goodsin::query()->where('unit_id', $unit)->get()->last();
            $result = Goodsin::query()->where('unit_id', $unit)->max('mrr_no');

            if($result != 0){
                $request['mrr_no'] = $result +1;
            }else{
                $request['mrr_no'] =1;
            }


            $goods_in = Goodsin::create($request->all());
            $purchase->update(['mrr_status'=>1]);
            $no_of_goods_in = 0;

            foreach ($request->product_id as $key => $product_id) {
                $purchase_product = PurchaseProduct::query()->where('purchase_id', $purchase->id)->where('product_id',$product_id)->first();
                if ( $request->receive_qty[$key] <= $purchase_product->qty && $request->receive_qty[$key] > 0) {
                    $product = Product::query()->findOrFail($product_id);
                    $data = new GoodsinProduct;
                    $data->unit_id = $unit;

                    $data->goodsin_id = $goods_in->id;
                    $data->product_id = $product_id;
                    $data->group_id = $product->group_id;
                    $data->qty = $request->receive_qty[$key];
                    $data->price = $request->price[$key];
                    $data->value= $request->receive_qty[$key] * $request->price[$key];
                    $data->purchase_id = $goods_in->purchase_id;
                    $data->remarks = $request->remarks[$key];
                    $data->supplier_id = $purchase->supplier_id;

                    $data->requisition_id = $purchase->requisition_id;
                    $data->product_unit_id = $product->product_unit_id;
                    $data->save();

                    $no_of_goods_in++;
                }

            }
            /*Goods In Product Save End */

            session()->flash('success', 'Goods in Created successfully');
            return redirect()->back();
   


    }

    // public function lcCost(){
    //     $lcs = Goodsin::pluck('lc','id');
    //     $costs = Cost::pluck('name','id');
    //     $lccosts = CostGoodsin::all();
    //     return view('inventory.goodsin.lc-cost',compact('lcs','costs','lccosts'));
    // }

    // public function storeLcCost(Request $request){

    //     $this->validate($request,[
    //         'cost_id'=>'required',
    //         'goodsin_id'=>'required'
    //     ]);

    //     CostGoodsin::create($request->all());
    //     session()->flash('success','LC Costing save successfully Complete');
    //     return redirect()->route('lc.cost');
    // }


    /* LC Report start */
    public function lcReport(){
        $lcs = Goodsin::pluck('lc','id');
        return view('inventory.report.lc-report',compact('lcs'));
    }
    /* LC Report End */



    /* Individual LC Report Findout Start */
    // public function lcReportFind(Request $request){
    //     $id = $request->goodsin_id;
    //     $goodsin = Goodsin::findOrFail($id);
    //     $sl=0;
    //     foreach ($goodsin->costs as $key=>$info){
    //         $data['sl'][$key] = ++$sl;
    //         $data['lc'][$key] = $goodsin->lc;
    //         $data['cost'][$key] = $info->cost->name;
    //         $data['amount'][$key] = $info->amount;
    //         $data['time'][$key] = $info->created_at->diffForHumans();
    //     }
    //     return json_encode(['success'=>$data]);
    // }

    /* Individual LC Report Findout End*/


//    public function findPurchaseProducts2(Request $request){
//        $purchase = Purchase::find($request->purchase_id);
//        if($purchase->quotation_id == 0){
//            $purchase_products = PurchaseProduct::where('purchase_id',$purchase->id)->get(['id','quotation_id']);
//            $quotation_array = array();
//            if(isset($purchase_products[0]->id)){
//                foreach ($purchase_products as $key => $value) {
//                    array_push($quotation_array, $value->quotation_id);
//                }
//            }
//
//            if(!empty($quotation_array)){
//                $products = QuotationProduct::whereIn('id',$quotation_array)->get();
//            }
//        }else{
//            $products = NewRequisitionProduct::where('new_requisition_id',$purchase->requisition_id)->get();
//        }
//
//        $sl =0;
//        $html ='';
//
//        if(count($products)>0){
//            return view('inventory.goodsin.product-list', compact('products','purchase'));
//        }else{
//            return $html.='<tr><td colspan="11" class="text-center bg-danger">No Product Information Found</td></tr>';
//        }
//    }

    public function findPurchaseProducts(Request $request){
        $purchase = Purchase::query()->find($request->purchase_id);
        $html ='';

        if($purchase !=null){
            return view('inventory.goodsin.product-list', compact('purchase'));
        }else{
            return $html.='<tr><td colspan="11" class="text-center bg-danger">No Product Information Found</td></tr>';
        }
    }

    public function goodsin_history(Request $request)
    {
        $goodsin_products = GoodsinProduct::where(get_where())->get();
        return view('inventory.goodsin.goodsin-history',compact('goodsin_products'));
    }

    public function datewise_stock_summary( Request $request )
    {
         // $from = date('Y-m-d',strtotime('first day of last month'));
        // $to = date('Y-m-d', strtotime('last day of last month'));
        $from = date('Y-m-d',strtotime('2021-12-06'));
        $to = date('Y-m-d', strtotime('2021-12-30'));

        $groups = Group::query()->whereNotIn('id',[152.153,168,171])->orderBy('name','asc')->pluck('name','id');
        if (Input::get('group_id') != null && Input::get('group_id')) {
            $to = date('Y-m-d', strtotime($request->to));
            $group_id = Input::get('group_id');
            $group_name = Group::query()->findOrFail($group_id)->name;
            $stocks = GoodsinProduct::query()->where(get_where())->where('group_id',$group_id)->whereBetween('created_at', array( $from, $to)) ->with('product','product.group')->get()->groupBy('product_id');
            $dataArray =[];
            foreach ($stocks as $product=>$stock){

                $stocksIssue = GoodsIssueProduct::query()->where(get_where())->where('product_id',$product)->get();
                if($stock->sum('qty') - $stocksIssue->sum('qty') > 0.01){
                    $dataArray[] =[
                        'product_id'=>$stock->first()->product->id,
                        'name'=>$stock->first()->product->name,
                        'unit'=>optional($stock->first()->product->unit)->name,
                        'qty'=> $stock->sum('qty') - $stocksIssue->sum('qty'),
                        'price'=>$stock->sum('price')/count($stock),
    //                    'value'=>$stock->sum('qty')* $stock->sum('price'),
                    ];
                }
                

            }
            $stocks = collect($dataArray);

        }else{
            $group_id = '';
            $stocks = [];
            $group_name = '';
        }

        return view('inventory.goodsin.datewise-stock-summary',compact('groups','stocks','group_name','group_id','from','to'));
    }


    public function stock_summary()
    {
        $groups = Group::query()->whereNotIn('id',[152.153,168,171])->orderBy('name','asc')->pluck('name','id');
        if (Input::get('group_id') != null && Input::get('group_id')) {
            $group_id = Input::get('group_id');
            $group_name = Group::query()->findOrFail($group_id)->name;
            $stocks = GoodsinProduct::query()->where(get_where())->where('group_id',$group_id)->where('supplier_id', '!=', null)->with('product','product.group')->get()->groupBy('product_id');
            $dataArray =[];
            foreach ($stocks as $product=>$stock){

                $stocksIssue = GoodsIssueProduct::query()->where(get_where())->where('product_id',$product)->get();
                if($stock->sum('qty') - $stocksIssue->sum('qty') > 0.01){
                    $dataArray[] =[
                        'product_id'=>$stock->first()->product->id,
                        'name'=>$stock->first()->product->name,
                        'unit'=>optional($stock->first()->product->unit)->name,
                        'qty'=> $stock->sum('qty') - $stocksIssue->sum('qty'),
                        'price'=>$stock->sum('price')/count($stock),
    //                    'value'=>$stock->sum('qty')* $stock->sum('price'),
                    ];
                }
                

            }
            $stocks = collect($dataArray);

        }else{
            $group_id = '';
            $stocks = [];
            $group_name = '';
        }

        return view('inventory.goodsin.stock-summary',compact('groups','stocks','group_name','group_id'));
    }

    //Sakib
    public function stockRegister(Request $request)
    {
        $payment_method = '';
        $from = date('d-m-Y',strtotime('-1 month'));
        $today_date = $to = date('d-m-Y');
        $groups = Group::pluck('name','id');
        $group = '';
        $items = Product::pluck('name','id');
        $suppliers = Supplier::pluck('name','id');
        $goodsin_products = [];
        $item_name = 'All Items';
        $item = '';
        $supplier = '';
        $unit_name = '';
        
        if($request->from != null && $request->to != null){
            $payment_method = $request->payment_method;
            // $from = $request->from;
            // $to = $request->to;
            $from = date('Y-m-d', strtotime($request->from));
            $to =date('Y-m-d', strtotime( $request->to));
            // where(\DB::raw('substr(`created_at`,1,10)'), '>=', $from)->where(\DB::raw('substr(`created_at`,1,10)'),'<=',$to)
            $goodsin_products = GoodsinProduct::whereHas('goodsin', function ($query) use($from,$to){
                    return $query->where('bill_date','>=',$from)->where('bill_date','<=',$to)->where(get_where());
                })->when($payment_method == '1' || $payment_method == '2', function($query) use ($payment_method){
                    return $query->whereHas('purchase', function ($query) use ($payment_method){
                        return $query->where('payment_type', $payment_method);
                    });
                })->get();
                
            if($request->product_id != null){
                $item = $request->product_id;
                $product = Product::find($request->product_id);
                $item_name = $product->name;
                $group = $product->group_id;
                $goodsin_products = $goodsin_products->where('product_id',$request->product_id);
                if($request->supplier_id != null){
                    $supplier = $request->supplier_id;
                    $goodsin_products = $goodsin_products->where('supplier_id',$request->supplier_id);
                }
            }else{
                if($request->supplier_id != null){
                    $supplier = $request->supplier_id;
                    $goodsin_products = $goodsin_products->where('supplier_id',$request->supplier_id);
                }
                if($request->group_id != null){
                    $item_name = Group::find($request->group_id)->name;
                    $group = $request->group_id;
                    $products_ids = Product::where('group_id',$request->group_id)->pluck('id');
                    $goodsin_products = $goodsin_products->whereIn('product_id',$products_ids);
                }
            }
        }
        return view('inventory.goodsin.stock-register',compact('goodsin_products','today_date','items','suppliers','from','to','item_name','unit_name','item','supplier','payment_method','groups','group'));
    }

    public function stockTransaction(Request $request)
    {
        $stockTransaction = [];
        $from = date('Y-m-d',strtotime('first day of last month'));
        $to = date('Y-m-d', strtotime('last day of last month'));
        $groups_array = array();
        $groups = Group::all();
        if($request->from != null && $request->to != null){
            $from = date('Y-m-d',strtotime($request->from));
            $to = date('Y-m-d',strtotime($request->to));
            
            foreach ($groups as $key => $value) {
                $products_array = Product::where('group_id',$value->id)->pluck('id');
                if(isset($products_array[0])){
                    //opening
                    $opening_goodsin = GoodsinProduct::whereIn('product_id',$products_array)->whereHas('goodsin', function ($query) use ($from,$to){
                        return $query->where(get_where())->where('bill_date', '<', $from);
                    })->get();
                    // return $opening_goodsin->sum(function($t){ return $t->qty * $t->price; });
                    $opening_goodsissue = GoodsIssueProduct::where(get_where())->whereIn('product_id',$products_array)->where(\DB::raw('substr(`created_at`,1,10)'), '<', $from)->get();
                    $opening_qty = $opening_goodsin->sum('qty') - $opening_goodsissue->sum('qty');
                    $opening_price = $opening_goodsin->sum(function($t){ return $t->qty * $t->price; }); - $opening_goodsissue->sum(function($t){ return $t->qty * $t->price; });

                    //goods receive
                    $goods_received = GoodsinProduct::whereIn('product_id',$products_array)->whereHas('goodsin', function ($query) use ($from,$to){
                        return $query->where(get_where())->where('bill_date', '>=', $from)->where('bill_date', '<=', $to);
                    })->get();
                    $goods_received_qty = $goods_received->sum('qty');
                    $goods_received_price = $goods_received->sum(function($t){ return $t->qty * $t->price; });

                    //goods issue
                    $goods_issue = GoodsIssueProduct::where(get_where())->whereIn('product_id',$products_array)->where(\DB::raw('substr(`created_at`,1,10)'), '>=', $from)->where(\DB::raw('substr(`created_at`,1,10)'), '<=', $to)->get();
                    $goods_issue_qty = $goods_issue->sum('qty');
                    $goods_issue_price = ($goods_received_qty > 0 ? $goods_received_price/$goods_received_qty : 0) * $goods_issue_qty;

                    //closing
                    $closing_qty = $opening_qty + $goods_received_qty - $goods_issue_qty;
                    $closing_price = $opening_price + $goods_received_price - $goods_issue_price;

                    $stockTransaction[$value->id]['group_name'] = $value->name;
                    $stockTransaction[$value->id]['opening_qty'] = $opening_qty;
                    // $stockTransaction[$value->id]['opening_qty'] = $opening_qty > 0;
                    $stockTransaction[$value->id]['opening_price'] = $opening_price;
                    $stockTransaction[$value->id]['goods_received_qty'] = $goods_received_qty;
                    $stockTransaction[$value->id]['goods_received_price'] = $goods_received_price;
                    $stockTransaction[$value->id]['goods_issue_qty'] = $goods_issue_qty;
                    $stockTransaction[$value->id]['goods_issue_price'] = $goods_issue_price;
                    $stockTransaction[$value->id]['closing_qty'] = $closing_qty;
                    $stockTransaction[$value->id]['closing_price'] = $closing_price;

                    if($closing_qty > 0 ){
                        // if($goods_received_qty > 0 || $goods_issue_qty > 0){
                            array_push($groups_array, $value->id);
                        // }
                    }
                    
                }
                
            }
        }
        $groups = Group::whereIn('id',$groups_array)->get();
        return view('inventory.goodsin.stock-transaction',compact('stockTransaction','groups','from','to'));
    }

    public function allUnitsSummary(Request $request){
        $from = date('d-m-Y',strtotime('-1 month'));
        $to = date('d-m-Y');
        $unit_id = 0;


        $units = Unit::whereIn('id',[8,10,12,13,15])->pluck('name','id');
        $all_units = [];
        $unit_stock = [];
        if($request->from != null && $request->to != null){
            $from = date('Y-m-d', strtotime($request->from));
            $to = date('Y-m-d', strtotime($request->to));
            

            $unit_id = $request->unit_id;
            $all_units = Unit::when($request->unit_id != null, function($query) use ($unit_id){
                return $query->where('id', $unit_id);
            })->when($request->unit_id == null, function($query){
                return $query->whereIn('id',[8,10,12,13,15]);
            })->get();

            
            foreach ($all_units as $key => $unit) {
                $goodsin_cash = GoodsinProduct::whereHas('goodsin', function ($query) use($from,$to){
                    return $query->where('bill_date','>=',$from)->where('bill_date','<=',$to)->whereHas('purchase', function ($p){
                        return $p->where('payment_type', 2);
                    });
                })->where('unit_id',$unit->id)->get();

                $goodsin_credit = GoodsinProduct::whereHas('goodsin', function ($query) use($from,$to){
                    return $query->where('bill_date','>=',$from)->where('bill_date','<=',$to)->whereHas('purchase', function ($p){
                        return $p->where('payment_type', 1);
                    });
                })->where('unit_id',$unit->id)->get();

                //total cash
                $unit_stock[$unit->id]['cash'] = $goodsin_cash->sum(function ($q){ return $q->qty * $q->price; });

                //total credit
                $unit_stock[$unit->id]['credit'] = $goodsin_credit->sum(function ($q){ return $q->qty * $q->price; });
            }
            
        }
        // return $unit_stock;
        return view('inventory.goodsin.all_units_summary',compact('units','unit_id','from','to','unit_stock','all_units'));
    }

    public function itemWiseallUnit(Request $request)
    {
        $products = Product::get();
        $units = Unit::whereIn('id',[8,10,12,13,15])->get(['name','id']);
        $item = null;
        $item = Product::where('id',$request->product_id)->first();
        return view('inventory.goodsin.itemwise_all_unit_summery',compact('units', 'item','products'));
    }




// -------------------Test--------------------


    public function customizedReport(){

        $requisitions = Requisition::where(get_where())->orderBy('unit_id')->latest()->get();
        //  $types = Requisition::where('parent_id',0)->get();

        // $categoris = Requisition::where('parent_id',0)->get();
        return view('inventory.goodsin.customized-report',compact('requisitions'));


    }

     public function test( Request $request ){
        $requisitions = Requisition::where('cost_c', $request->cost_center)->where('payment_mode', $request->payment_mode)->latest()->get();
        return view('inventory.goodsin.customized-report',compact('requisitions'));
    } 
}
