<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
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\ProductionStock;
use DB;

class StockController extends Controller
{
    /*--------stock list product wise----------*/
    public function stocklist(){
        $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.*','products.product_name','products.code','products.re_order_label','productcategories.name as category_name','units.name as unit_name')
                ->get();   

        return view('backend.stock.stockList',compact('products'));
    }
    public function production_stock(){
        $products  = DB::table('production_stocks')
                ->join('products', 'production_stocks.product_id', '=', 'products.id')
                ->join('productcategories', 'production_stocks.productcategory_id', '=', 'productcategories.id')
                ->join('units', 'production_stocks.unit_id', '=', 'units.id')
                ->select('production_stocks.*','products.product_name','products.code','products.re_order_label','productcategories.name as category_name','units.name as unit_name')
                ->where('production_stocks.now_stock','>',0)
                ->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 tsList(){
        $about = About::find(1);
        $warehouses = Warehouse::orderBy('warehouse_name','ASC')->get();
        $categories = Category::orderBy('category_name','ASC')->get();
        $brands = Brand::orderBy('brand_name','ASC')->get();

        $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')
                        ->get();
       return view('backend.admin.stock.filterStock',compact('products','about','warehouses','categories','brands'));
    }
    /*----------------------------warehouse product List ------------------------*/
    public function wpList(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.warehouse_id' ,$request->warehouse)
                        ->get();
        return response()->json($products);
    }
    /*----------------------------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 spList(Request $request){
        $products = DB::table('purchase_stocks')
                ->select('purchase_stocks.id as id','purchase_stocks.product_name','purchase_stocks.model_no')
                ->where('warehouse_id',$request->id)
                ->get();
        return response()->json($products);
    }
    public function spDetails(Request $request){
        $product  = 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')
                ->join('warehouses', 'purchase_stocks.warehouse_id', '=', 'warehouses.id')
                ->select('purchase_stocks.*','purchase_stocks.id as purchase_detail_id','warehouses.warehouse_name')
                ->where('purchase_stocks.id',$request->id)
            ->first();
       /* $product = Product::find($request->id);*/
        return response()->json($product);
    }
    public function sptupdate(Request $request){
        $data = $request->except('_token','submit');

        for($i = 0; $i<count($data['purchase_detail_id']); $i++ ){
            $pur = PurchaseDetail::find($data['purchase_detail_id'][$i]);
            $pur->warehouse_id = $data['warehouse_id'][$i];
            $pur->rack_no = $data['rack_no'][$i];
            $pur->save();
            $pur->updated_by = Auth::id();
        }
        return redirect()->route('admin.stock.stocklist')->with('success','Stock Transfer Successful');
    }
    public function roll_cutting(){
        $categories = Productcategory::orderBy('name','ASC')->get();
        $stocks = 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')
                         ->get();
        return view('backend.stock.role_cutting',compact('stocks','categories'));
    }
    public function cutting_process($id){
        $product = PurchaseDetail::find($id);
        $products = Product::where('id','!=',$product->product_id)->where('productcategory_id',$product->productcategory_id)->orderBy('product_name','ASC')->get();
        return view('backend.stock.cutting_process',compact('product','products'));
    }
    public function roll_update(Request $request){
        $data = $request->except('_token','submit');
        $purchase = PurchaseDetail::find($request->purchase_detail_id);

        for($i = 0; $i<count($data['product_id']); $i++ ){

            $product = Product::find($data['product_id'][$i]);

            $list = new PurchaseDetail();
            $list->purchase_id = $purchase->purchase_id;
            $list->supplier_id =  $purchase->supplier_id;
            $list->product_id =  $data['product_id'][$i];
            $list->productcategory_id = $product->productcategory_id;
            $list->unit_id = $product->unit_id;
            $list->chalan_no = $purchase->chalan_no;
            $list->rate = $data['supplier_price'][$i];
            $list->quantity = $data['ordered_quantity'][$i];
            $list->now_stock = $data['ordered_quantity'][$i];
            $list->purchase_date =  date('Y-m-d');
            $list->created_by = Auth::id();
            $list->save();
        }
        $purchase->now_stock = $purchase->now_stock - $request->cutting_quantity;
        $purchase->save();

        return redirect()->route('stock.roll_cutting')->with('success','Stock quantity cutting success');
    }
}
