<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Warehouse;
use App\Models\About;
use App\Models\Productcategory;
use App\Models\Supplier;
use App\Models\Product;
use App\Models\Purchase;
use App\Models\PurchaseDetail;
use App\Models\StockLedger;

class StockController extends Controller
{
    /*--------stock list product wise----------*/
    public function stocklist(){
        $products  = DB::table('product_stocks')
                ->join('products', 'product_stocks.product_id', '=', 'products.id')
                ->join('productcategories', 'product_stocks.productcategory_id', '=', 'productcategories.id')
                ->join('units', 'product_stocks.unit_id', '=', 'units.id')
                ->select('product_stocks.*','products.product_name','products.code','productcategories.name as category_name','units.name as unit_name')
                ->get();   

        return view('backend.stock.stockList',compact('products'));
    }
     /*----------stock search------------*/
     public function stocks(Request $request){
          $about = About::find(1);
          $warehouses = Warehouse::orderBy('warehouse_name','ASC')->get();
          $warehouse =''; 
  
          $search_by = $request->search_by;
          $search_value = $request->search_value;
        
          $products  = DB::table('purchase_stocks')
                  ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                  ->join('categories', 'purchase_stocks.category_id', '=', 'categories.id')
                  ->join('brands', 'purchase_stocks.brand_id', '=', 'brands.id')
                  ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                  ->select('purchase_stocks.*','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.model_no','products.re_order_label', 'categories.category_name', 'brands.brand_name','units.unit_name')
                  ->where('purchase_stocks.'.$search_by ,$search_value)
                  ->get(); 
          return view('backend.admin.stock.stockList',compact('products','about','warehouses','warehouse'));
      }
    /*--------warehouse stock list-------------*/
    public function wstock($id){
        $about = About::find(1);
        $warehouse = Warehouse::find($id); 

        $products  = DB::table('purchase_stocks')
                ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                ->join('categories', 'purchase_stocks.category_id', '=', 'categories.id')
                ->join('brands', 'purchase_stocks.brand_id', '=', 'brands.id')
                ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                ->select('purchase_stocks.*','purchase_stocks.now_stock as product_stock','products.product_name','products.model_no','products.re_order_label', 'categories.category_name', 'brands.brand_name','units.unit_name')
                ->where('warehouse_id',$id)
                ->get();   

        return view('backend.admin.stock.stockList',compact('products','about','warehouse'));
    }
   /*=========================filtering section================================*/
    /*---------------------------Total Stock List--------------------------------*/
    public function store_stock(){
       // $warehouses = Warehouse::orderBy('warehouse_name','ASC')->get();
        $categories = Productcategory::orderBy('name','ASC')->get();
       // $brands = Brand::orderBy('brand_name','ASC')->get();

        $products  = DB::table('purchase_stocks')
                        ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                        ->join('productcategories', 'purchase_stocks.productcategory_id', '=', 'productcategories.id')
                        ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                        ->select('purchase_stocks.*','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.code','products.re_order_label', 'productcategories.name as category_name','units.name as unit_name')
                        ->where('purchase_stocks.status',false)
                        ->get();
       return view('backend.stock.filterStock',compact('products','about','warehouses','categories','brands'));
    }
    public function house_stock(){
        // $warehouses = Warehouse::orderBy('warehouse_name','ASC')->get();
         $categories = Productcategory::orderBy('name','ASC')->get();
        // $brands = Brand::orderBy('brand_name','ASC')->get();
 
         $products  = DB::table('purchase_stocks')
                         ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                         ->join('productcategories', 'purchase_stocks.productcategory_id', '=', 'productcategories.id')
                         ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                         ->select('purchase_stocks.*','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.code','products.re_order_label', 'productcategories.name as category_name','units.name as unit_name')
                         ->where('purchase_stocks.status',true)
                         ->get();
        return view('backend.stock.filterStock',compact('products','about','warehouses','categories','brands'));
     }
    public function stockStatus($id){
        $stock = PurchaseDetail::find($id);
        if($stock->status == false){
            $stock->status = true;
        }
        else{
            $stock->status = false;
        }
        $stock->save();
        return back()->with('success','Stock Transfer to House successfully');
    }
    /*----------------------------category wise product stock search------------------------*/
    public function cpList(Request $request){
        $products  = DB::table('purchase_stocks')
                        ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                        ->join('warehouses', 'purchase_stocks.warehouse_id', '=', 'warehouses.id')
                        ->join('categories', 'purchase_stocks.category_id', '=', 'categories.id')
                        ->join('brands', 'purchase_stocks.brand_id', '=', 'brands.id')
                        ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                        ->select('purchase_stocks.*','warehouses.warehouse_name','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.model_no','products.re_order_label', 'categories.category_name', 'brands.brand_name','units.unit_name')
                        ->where('purchase_stocks.category_id' ,$request->category)
                        ->get();
        return response()->json($products);
    }
    /*----------------------------brand wise stock search------------------------*/
    public function bpList(Request $request){
        $products  = DB::table('purchase_stocks')
                        ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                        ->join('warehouses', 'purchase_stocks.warehouse_id', '=', 'warehouses.id')
                        ->join('categories', 'purchase_stocks.category_id', '=', 'categories.id')
                        ->join('brands', 'purchase_stocks.brand_id', '=', 'brands.id')
                        ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                        ->select('purchase_stocks.*','warehouses.warehouse_name','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.model_no','products.re_order_label', 'categories.category_name', 'brands.brand_name','units.unit_name')
                        ->where('purchase_stocks.brand_id' ,$request->brand)
                        ->get();
        return response()->json($products);
    }
    /*----------------------------brand wise stock search------------------------*/
    public function pList(Request $request){
        $products  = DB::table('purchase_stocks')
                        ->join('products', 'purchase_stocks.product_id', '=', 'products.id')
                        ->join('warehouses', 'purchase_stocks.warehouse_id', '=', 'warehouses.id')
                        ->join('categories', 'purchase_stocks.category_id', '=', 'categories.id')
                        ->join('brands', 'purchase_stocks.brand_id', '=', 'brands.id')
                        ->join('units', 'purchase_stocks.unit_id', '=', 'units.id')
                        ->select('purchase_stocks.*','warehouses.warehouse_name','purchase_stocks.now_stock as product_stock','products.product_name as product_name','products.model_no','products.re_order_label', 'categories.category_name', 'brands.brand_name','units.unit_name')
                        ->where('purchase_stocks.product_id' ,$request->product)
                        ->get();
        return response()->json($products);
    }
    /*--------------------stock Transfer search-----------------------------------*/
    public function stock_transfer(Request $request){
        $warehouses = Warehouse::active()->orderBy('warehouse_code','ASC')->get();
        return view('backend.admin.stock.transfer_search',compact('warehouses'));
    }
    /*----------ajax request section-------------*/
    public function stock_ledger(Request $request){
        $from_date =  date('Y-m-01');
        $to_date =  date('Y-m-d') ;
        
        $ty = 'All';
        $products = Product::orderBy('product_name')->get();
        $transactions = StockLedger::whereDate('transaction_date', '>=', $from_date)->whereDate('transaction_date', '<=', $to_date)->get();

        return view('backend.stock.stock_ledger',compact('ty','products','transactions','from_date','to_date')); 
    }
    public function stock_ledgers(Request $request){
        $from_date =  date('Y-m-d',strtotime($request->from_date));
        $to_date =  date('Y-m-d',strtotime($request->to_date)) ;
        
        $products = Product::orderBy('product_name')->get();
        
        //if($request->report_type == "Sale"){
            if($request->product_id == "All"){
                $ty = $request->product_id ;
                $transactions = StockLedger::whereDate('transaction_date', '>=', $from_date)->whereDate('transaction_date', '<=', $to_date)->orderBy('id')->get();
            }else{
                $types = Product::find($request->product_id);
                $ty = $types->product_name;
                $transactions = StockLedger::where('product_id',$request->product_id)->whereDate('transaction_date', '>=', $from_date)->whereDate('transaction_date', '<=', $to_date)->orderBy('id')->get();
            }
        //}
        return view('backend.stock.stock_ledger',compact('ty','products','transactions','from_date','to_date')); 
    }
}
