<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Auth\Middleware\Authenticate;
use Auth;
use App\CategoryMaster;
use App\Product;
use App\UnitMeasurement;
use App\GradeMaster;
use App\CustomerProduct;
use App\Customer;
use App\ProductUnit;
use App\ProductGrade;
use App\MaterialRequisition;
use App\MRproduct;
use App\NotificationType;
use App\Notification;
use App\NotificationStatus;
use App\ProductNotification;
use App\MRapprovalproduct;
use App\Pallet;
use App\Common;
use App\Location;
use App\LocationProduct;
use App\CustomerLocation;
use App\CustomerParent;
use App\ParentMaster;
use App\ModeDetail;
use App\MRorder;
/**
* 
*/
class MaterialRequestController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    
	public function viewList()
	{	
		$customer = Auth::User()->customer_id;
        if(Auth::user()->choose_cust == 'Y'){
            $list = MaterialRequisition::where('entered_by','=',Auth::User()->id)
					->whereIN('status',['REQ','DRT','APR'])
					->get();
        }else{
        	if($customer)
        	{
	        	$list = MaterialRequisition::where('customer_id','=',$customer)
										   ->whereIN('status',['REQ','DRT','APR'])
										   // ->where('location_id','=',Auth::User()->location_id)
										   ->get();
			}else
			{
				$list = MaterialRequisition::where('entered_by','=',Auth::User()->id)
											->whereIN('status',['REQ','DRT','APR'])
									  		 ->get();
			}
        }
		
					
		$x = 0;
		return view('material_request/list',compact('list','x'));
	}

	public function index($id)
	{		
		
		$x = $id;
		$category = CategoryMaster::all();
		$loc_prdt = new LocationProduct();
		$product_ids = $loc_prdt->productList(Auth::user()->location_id);
		///products
		// $customerType = Auth::User()->customer->customer_type_id;



		if($id == 0){
			$count = MaterialRequisition::where('customer_id','=',$customer)->max('mr_cnt');
			$count = ($count <= 0)? 101 : ($count+1);
			$req_no = 'CMR'.'-'.$customer.'-'.$count;
			
			
			$last_entries = MaterialRequisition::where('customer_id','=',$customer)
							->orderBy('id','DESC')->take(5)->get();
		    $request = NULL;					


			$customerType = Auth::User()->customer->customer_type_id;

			$customer = Auth::User()->customer_id;

		}
		else{
			$m_r = MaterialRequisition::find($id);
			
			$customer = $m_r->customer_id;

			$customerType = Customer::where('id','=',$m_r->customer_id)
										->first()->customer_type_id;
			
			$req_no = $m_r->request_no;
			$products = MRproduct::where('material_requisition_id','=',$id)->orderBy('id','DESC')->get();
			$last_entries = MaterialRequisition::where('customer_id','=',$customer)
							->where('id','!=',$id)
							->orderBy('id','DESC')->take(5)->get();
            $request = MaterialRequisition::find($id);
		}


		if($customerType == 3)
		{
			$cus_prd = CustomerProduct::where('customer_id','=',$customer)->pluck('product_id')->toArray();
			// $prd = Product::whereIN('id',$cus_prd)->where('is_active' ,'=' ,'Y')->get();
			if($cus_prd){
				$prd = Product::whereIN('id',$cus_prd)
							->where('is_active' ,'=' ,'Y')->selectRaw('id,secondry_name as name')->get();
			} else {
				$prd = Product::whereIN('id',$product_ids)
							->where('is_active' ,'=' ,'Y')->selectRaw('id,secondry_name as name')->get();
			}
		}else{
			$prd = Product::whereIN('id',$product_ids)
							->where('is_active' ,'=' ,'Y')->get();
			// $prd = Product::where('is_active' ,'=' ,'Y')->get();
		}

		

		/////Notification//////

		$cus = Customer::find($customer);
        $n_type =  NotificationType::where('customer_type_id','=',$cus->customer_type_id)
            ->pluck('notification_id')->toArray();
        $n_status = NotificationStatus::where('customer_id','=',$customer)
        			->where('status','=','READ')->pluck('notification_id')->toArray();

        $nfn = Notification::whereIN('id',$n_type)
        			->whereNotIN('id',$n_status) 
                    ->where('valid_from','<=',date('Y-m-d'))
                    ->where('valid_to','>=',date('Y-m-d'))->get();

        $n_type_count = count($nfn);

		//////////////////////

		$active = Common::where('name','=','ACTIVE')->first();
        $customers = Customer::where('status','=',$active->id)
                             ->orderBy('name')->get();
		
		$location = CustomerParent::all();

		return view('material_request/index',compact('req_no','x','products','last_entries','n_type_count','nfn','prd','category','customers','request','location'));
	}

	public function showProduct(Request $request)
	{
		$cat_id = $request->get('option');
		$customer = Auth::User()->customer_id;
		$customerType = Auth::User()->customer->customer_type_id;

		if($customerType == 3)
		{
		$cus_prd = CustomerProduct::where('customer_id','=',$customer)->pluck('product_id')->toArray();
		$product = Product::whereIN('id',$cus_prd)->where('category_id','=',$cat_id)
									->where('is_active' ,'=' ,'Y')->get();
		}else{
			$product = Product::where('category_id','=',$cat_id)
									->where('is_active' ,'=' ,'Y')->get();
		}
		

		return $product;
	}

	public function showUnit(Request $request)
	{
		$prod_id = $request->get('option');
		$unit = ProductUnit::join('unit_of_measurement','unit_of_measurement.id','=','product_unit.unit_id')
				->selectRaw('unit_of_measurement.unit,unit_of_measurement.id')
				->where('product_unit.product_id','=',$prod_id)
				->get();
		/*
		$grade = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
				->selectRaw('grade_master.grade,grade_master.id')
				->where('product_grade.product_id','=',$prod_id)
				->where('product_grade.is_active','=','Y')
				->get();
		*/
		if($request->get('option1') != null)
		{

			$customerType = Customer::where('id','=',$request->get('option1'))
									->first()->customer_type_id;
		}else
		{

			$customerType = Auth::User()->customer->customer_type_id;

		}
		if($customerType == 3 && CustomerProduct::where('customer_id','=',Auth::user()->customer_id)->count() >= 1) {
			$grade = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
					->selectRaw('grade_master.grade,grade_master.id')
					->where('product_grade.product_id','=',$prod_id)
					->where('product_grade.is_active','=','Y')
					->get();
		} else {
			$grade = LocationProduct::join('grade_master','grade_master.id','=','location_products.grade_id')
					->selectRaw('grade_master.grade,grade_master.id')
					->where('location_products.product_id','=',$prod_id)
					->where('location_products.location_id','=',Auth::user()->location_id)
					->get();
			if($grade) {
				$grade = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
							->selectRaw('grade_master.grade,grade_master.id')
							->where('product_grade.product_id','=',$prod_id)
							->where('product_grade.is_active','=','Y')
							->get();
			}
		}
		$data = array($unit,$grade);

		return $data;
	}

	public function showGrade(Request $request)
	{
		$product_id = $request->get('prd');
		

		$grade = ProductGrade::join('grade_master','grade_master.id','=','product_grade.grade_id')
					->selectRaw('grade_master.grade,grade_master.id')
					->where('product_grade.product_id','=',$product_id)
					->get();

		return $grade;
	}

	public function insertRequest(Request $request)
	{	
		$entered_by = Auth::User()->id;
		if($request->get('customers') == 0){
			$customer = Auth::User()->customer_id;
		}else{
			$customer = $request->get('customers');
		}

		$count = MaterialRequisition::where('customer_id','=',$customer)->max('mr_cnt');
		// $req_no = ($count <= 0)? 101 : ($count+1);

		$req_no = ($count <= 0)? 1000 : ($count+1);
		
		if($request->get('x') == 0){
			$insert = MaterialRequisition::create([
							'request_no' => $req_no,
							'mr_cnt' => $req_no,
							'entered_by' => $entered_by,
							'customer_id' => $customer,
							'location_id' => Auth::User()->location_id,
							'request_date' => date('Y-m-d'),
							'status' => 'REQ'
						]);

			$pr_insert = MRproduct::create([
							'material_requisition_id' => $insert->id,
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'qty' => $request->get('qty')
						]);
			$req_id = $insert->id;
			$x = $insert->id;
		}
		else{
			$x = $request->get('x');
			$count = MaterialRequisition::where('customer_id','=',$customer)->max('mr_cnt');
			$req_no = ($count <= 0)? 101 : ($count+1);
			
			MaterialRequisition::where('id','=',$x)
			                   ->update(['mr_cnt' => $req_no]);
			$z = MRproduct::where('material_requisition_id','=',$x)
							->where('product_id','=',$request->get('product'))
							->where('unit_id','=',$request->get('unit'))
							->where('grade_id','=',$request->get('grade'))->count();
			if($z > 0){
				return redirect()->action('MaterialRequestController@index',['id'=>$x]);
			}
			$pr_insert = MRproduct::create([
							'material_requisition_id' => $request->get('x'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'qty' => $request->get('qty')
						]);
			$req_id = $request->get('x');
		}
		
		$category = CategoryMaster::all();
		$products = MRproduct::where('material_requisition_id','=',$req_id)->get();
		
		
		return redirect()->action('MaterialRequestController@viewEdit',['id'=>$x]);
		
	}


	public function requestSubmit(Request $request)
	{	
		$check = MaterialRequisition::find($request->get('x'));
		if($check->status == 'APR'){
			return redirect()->route('material_request.list')->with('approved',true);
		}
		// if (isset($_POST['submit'])) {
		// 	MaterialRequisition::where('id','=',$request->get('x'))
		// 			->update(['status' => 'REQ']);
		// }

		// if (isset($_POST['draft'])) {
		// 	MaterialRequisition::where('id','=',$request->get('x'))
		// 			->update(['status' => 'DRT']);
		// }
		
			MaterialRequisition::where('id','=',$request->get('x'))
					->update(['status' => 'DRT']);

		return redirect()->route('material_request.list');
	}

	public function deleteProduct(Request $request)
	{	
		$data = $request->get('option');
		$s = MRproduct::where('id','=',$request->get('option'))
					->delete();
		
		return $s;
	}

	public function editProduct($id)
	{	
		$mr = MRproduct::find($id);
		if($mr->status == "APR") {
			return redirect()->route('material_request.list');
		}
		$loc_prdt = new LocationProduct();
		///products
		$customerType = $mr->materialRequest->customer->customer_type_id;
		$product_ids = $loc_prdt->productList(Auth::user()->location_id);
		if($customerType == 3)
		{
			$cus_prd = CustomerProduct::where('customer_id','=',Auth::user()->customer_id)->pluck('product_id')->toArray();
			//$product = Product::whereIN('id',$cus_prd)->where('is_active' ,'=' ,'Y')->get();
			if($cus_prd){
				$product = Product::whereIN('id',$cus_prd)
							->where('is_active' ,'=' ,'Y')->selectRaw('id,secondry_name as name')->get();
			} else {
				$product = Product::whereIN('id',$product_ids)
							->where('is_active' ,'=' ,'Y')->selectRaw('id,secondry_name as name')->get();
			}
		}else{
			$product = Product::whereIN('id',$product_ids)
							->where('is_active' ,'=' ,'Y')->get();
			//$product = Product::where('is_active' ,'=' ,'Y')->get();
		}
		/////////////////////////

		$p_unit = ProductUnit::where('product_id','=',$mr->product_id)->pluck('unit_id')->toArray();
		$unit = UnitMeasurement::whereIN('id',$p_unit)->get();
		/*
		$p_grade = ProductGrade::where('product_id','=',$mr->product_id)->pluck('grade_id')->toArray();
		$grade = GradeMaster::whereIN('id',$p_grade)->get();
		*/
		if($customerType == 3 && CustomerProduct::where('customer_id','=',Auth::user()->customer_id)->count() >= 1) {
			$p_grade = ProductGrade::where('product_id','=',$mr->product_id)->pluck('grade_id')->toArray();
			$grade = GradeMaster::whereIN('id',$p_grade)->get(); 
		} else {
		 	$p_grade = LocationProduct::where('product_id','=',$mr->product_id)
		 					->where('location_id','=',Auth::user()->location_id)->pluck('grade_id')->toArray();
		 	$grade = GradeMaster::whereIN('id',$p_grade)->get(); 
		}
		$x = $mr->material_requisition_id;
		$products = MRproduct::where('material_requisition_id','=',$x)->orderBy('id','DESC')->get();
		
		$product = Product::where('is_active' ,'=' ,'Y')->get();

		$p_grade = ProductGrade::where('product_id','=',$mr->product_id)
					->where('is_active','=','Y')->pluck('grade_id')->toArray();

		$grade = GradeMaster::whereIN('id',$p_grade)->get(); 

		return view('material_request/edit_product',compact('mr','x','product','unit','grade','products'));
	}

	public function updateProduct(Request $request)
	{	
		$update = MRproduct::where('id','=',$request->get('mr_id'))
					->update([
							'material_requisition_id' => $request->get('x'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'qty' => $request->get('qty')]);
		$x = $request->get('x');
		
		return redirect()->action('MaterialRequestController@index',['id'=>$x]);
	}

	public function viewEdit($id)
	{	
		$material_request = MaterialRequisition::find($id);
		if($material_request->status == "APR") {
			return redirect()->route('material_request.list');
		}
		///products
		$customerType = $material_request->customer->customer_type_id;
		
		if($customerType == 3)
		{
		$cus_prd = CustomerProduct::where('customer_id','=',$customer)->pluck('product_id')->toArray();
		$prd = Product::whereIN('id',$cus_prd)
							->where('is_active' ,'=' ,'Y')->get();
		}else{
			$prd = Product::where('is_active' ,'=' ,'Y')->get();
		}
		/////////////////////////
		$products = MRproduct::where('material_requisition_id','=',$id)->orderBy('id','DESC')->get();
		$category = CategoryMaster::all();
		// $customer = Auth::User()->customer_id;
		$customer =$material_request->customer->id;
		$last_entries = MaterialRequisition::where('customer_id','=',$customer)
							->where('id','!=',$id)
							->orderBy('id','DESC')->take(5)->get();

		$cus = Customer::find($customer);
        $n_type =  NotificationType::where('customer_type_id','=',$cus->customer_type_id)
            ->pluck('notification_id')->toArray();
        $n_status = NotificationStatus::where('customer_id','=',$customer)
        			->where('status','=','READ')->pluck('notification_id')->toArray();

        $nfn = Notification::whereIN('id',$n_type)
        			->whereNotIN('id',$n_status)
                    ->where('valid_from','<=',date('Y-m-d'))
                    ->where('valid_to','>=',date('Y-m-d'))->get();

  	
        $n_type_count = count($nfn);
        $mr = null;

		//////////////////////
		
		return view('material_request/edit',compact('material_request','products','mr','category','last_entries','n_type_count','nfn','prd'));
	}


	public function addLastEntries($id)
	{	
		$exp = explode("-", $id);
		$mr_id = $exp[0];
		$current_id = $exp[1];

		if($current_id == 0){

			$entered_by = Auth::User()->id;
			$customer = Auth::User()->customer_id;
			$count = MaterialRequisition::where('customer_id','=',$customer)->max('mr_cnt');
			// $req_no = ($count <= 0)? 101 : ($count+1);


			$req_no = ($count <= 0)? 1000 : ($count+1);
			
			$insert = MaterialRequisition::create([
							'request_no' => $req_no,
							'mr_cnt' => $req_no,
							'entered_by' => $entered_by,
							'customer_id' => $customer,
							'location_id' => Auth::User()->location_id,
							'request_date' => date('Y-m-d'),
							'status' => 'REQ'
						]);
			$exist_values = MRproduct::where('material_requisition_id','=',$mr_id)->get();

			foreach($exist_values as $exist){
				$pr_insert = MRproduct::create([
							'material_requisition_id' => $insert->id,
							'product_id' => $exist->product_id,
							'unit_id' => $exist->unit_id,
							'grade_id' => $exist->grade_id,
							'qty' => $exist->qty
						]);
			}
			
			$x = $insert->id;

			
		}
		else{
			$to_add = MRproduct::where('material_requisition_id','=',$mr_id)->get();
			$current = MRproduct::where('material_requisition_id','=',$current_id)->get();
			$x = $current_id;
			

			foreach ($to_add as $value) {
				$flag = 0;
				foreach($current as $curr){
					if($curr->product_id == $value->product_id && $curr->unit_id == $value->unit_id && $curr->grade_id == $value->grade_id){

						$update = MRproduct::where('id','=',$curr->id)
									->update(['qty' => $curr->qty + $value->qty]);

						$flag = 1;
					}
				}

				if($flag == 0){
					$create = MRproduct::create([
															'material_requisition_id' => $current_id,
															'product_id' => $value->product_id,
															'unit_id' => $value->unit_id,
															'grade_id' => $value->grade_id,
															'qty' => $value->qty
														]);
				}
			}	

		}
		return redirect()->action('MaterialRequestController@index',['id'=>$x]);

	}

	public function addNotificationStatus()
	{
		$customer = Auth::User()->customer_id;
		$cus = Customer::find($customer);
        
        $n_type =  NotificationType::where('customer_type_id','=',$cus->customer_type_id)
            ->pluck('notification_id')->toArray();
        $n_status = NotificationStatus::where('customer_id','=',$customer)
        			->where('status','=','READ')->pluck('notification_id')->toArray();
        $nfn = Notification::whereIN('id',$n_type)
        			->whereNotIN('id',$n_status)
                    ->where('valid_from','<=',date('Y-m-d'))
                    ->where('valid_to','>=',date('Y-m-d'))->get();

        foreach($nfn as $value){
        	$check = NotificationStatus::where('notification_id','=',$value->id)
        									->where('customer_id','=',$customer)->count();
        	if($check == 0){
        		NotificationStatus::create([
        									'notification_id' => $value->id,
        									'customer_id' => $customer,
        									'status' => 'READ']);
        	} 
        }

        return $customer;    
	}

	public function viewApprovedProduct($id)
	{	
		$m_r = MaterialRequisition::find($id);
		$products = MRproduct::where('material_requisition_id','=',$id)->orderBy('id','DESC')->get();
		return view('material_request/viewApproved',compact('products','m_r'));
	}			

	public function deleteMR(Request $request)
	{
		$id = $request->get('option');

		$mrobj = MaterialRequisition::find($id);
		if($mrobj->status == 'APR'){
			MRapprovalproduct::where('material_requisition_id','=', $id)->delete();
		}
		MRproduct::where('material_requisition_id','=', $id)->delete();
		$mrobj->delete();
	}


	public function received(Request $request)
	{	
		MaterialRequisition::where('id','=',$request->get('id'))
							->update([
										'recieved_date' => date('Y-m-d'),
										'status' => 'REC',
										'remarks' => $request->get('remarks')
									]);
		return redirect()->route('material_request.list');
	}

	public function ctnperPallet(Request $request)
	{
		$grade_id = $request->get('grade');
		$product_id = $request->get('product');
		$pallet = Pallet::where('product_id','=',$product_id);
		
		if(is_null($grade_id)) {
			$pallet->whereNull('grade_id');
		} else {
			$pallet->where('grade_id','=',$grade_id);
		}

		$pallet = $pallet->first();
		return ($pallet)? $pallet : 'false';
		
		return $pallet;
	}


	public function MRorderIndex($id)
	{		
		
		$x = $id;
		$customer = $id;
		$category = CategoryMaster::all();
		$loc_prdt = new LocationProduct();
		$product_ids = $loc_prdt->productList(Auth::user()->location_id);
		$mode_id = ModeDetail::where('mode_name','=','CREDIT')->first();
		///products
		$active = Common::where('name','=','ACTIVE')->first();
		if(sizeof($product_ids)) {

	        $prd = Product::whereIN('id',$product_ids)
								->where('is_active' ,'=' ,'Y')->get();

	        $customers = Customer::where('status','=',$active->id)
	                             ->where('customer_type_id','=',Auth::User()->customer->customer_type_id)
	                             ->where('mode_detail_id','=',$mode_id->id)
	                             ->orderBy('name')->get();
		}else
		{

			$prd = Product::where('is_active' ,'=' ,'Y')->get();

	        $customers = Customer::where('status','=',$active->id)  
	        					 ->where('mode_detail_id','=',$mode_id->id)
	                             ->orderBy('name')->get();
			
			
		}
		//////////////////////
		if ($id == null || $id == 0) {
		
		$products = null;

		$location = Location::where('is_active','=','Y')
							->orderBy('location')
							->get();
		$customer_loc = [];

		$request = null;
		}else
		{

		$products = MRorder::where('customer_id','=',$id)->selectRaw('product_id,unit_id,grade_id,sum(qty) AS qty,customer_id')->groupBy('product_id','unit_id','grade_id','customer_id')->get();
		$request = null;

		
		
		$customer_loc = CustomerParent::where('customer_id','=',$id)->pluck('parent_id');

		$location = ParentMaster::whereIn('id',$customer_loc)->get();

		$location_id = MRorder::where('customer_id','=',$id)->pluck('location_id');

		$customer_loc = CustomerParent::where('customer_id','=',$id)
										->whereIn('parent_id',$location_id)
										->pluck('parent_id')->toArray();
		}

		$req_no =null;
		return view('mr_order/index',compact('req_no','x','products','prd','category','customers','request','location','id','customer_loc'));
	}

	public function customerLocation(Request $request)
	{
		
		 MRorder::where('customer_id','=',$request->get('option'))->delete();
		$customer_loc = CustomerParent::where('customer_id','=',$request->get('option'))
										->pluck('parent_id');

		$location = ParentMaster::whereIn('id',$customer_loc)->get();

		return $location;
	}
	public function locationList(Request $request)
	{

		$location = ParentMaster::whereIn('id',$request->get('option'))->get();
		return $location;
	}
	public function addQty(Request $request)
	{
		$product 	= $request->get('qty_product');
		$unit 		= $request->get('qty_unit');
		$grade 		= $request->get('qty_grade');
		$customer 	= $request->get('qty_customer');
		$location_id= $request->get('location_id');

		foreach($location_id as $location) {
			if ($request->get('qty')[$location] != null) {
				
            $test = MRorder::create([
							'customer_id' => $customer,
							'location_id' => $location,
							'product_id' => $product,
							'unit_id' => $unit,
							'grade_id' => $grade,
							'qty' => $request->get('qty')[$location],
							'remarks' => $request->get('remarks'),
						]);
           	}else
           	{
           		$test = MRorder::create([
							'customer_id' => $customer,
							'location_id' => $location,
							'product_id' => $product,
							'unit_id' => $unit,
							'grade_id' => $grade,
							'qty' =>0,
							'remarks' => $request->get('remarks'),
						]);
           	}
        }


		return redirect()->action('MaterialRequestController@MRorderIndex',['id'=>$customer]);

	}


	public function deleteOrderProduct(Request $request)
	{	
		$data = $request->get('option');
		$s = MRorder::where('customer_id','=',$request->get('customer_id'))
					->where('product_id','=',$request->get('option'))
					->delete();
		
		return $s;
	}
	public function orderSubmit(Request $request)
	{
		$prefix = Location::where('id','=',Auth::user()->location_id)->first();
		
		$customers = $request->get('customers');
		$orders = MRorder::where('customer_id','=',$customers)->orderBy('location_id')->get();
		$prefix = Location::where('id','=',Auth::user()->location_id)->first();
		if ($prefix == null){
			$prefix = null;
		}else
		{
			if ($prefix->prefix  == null) {
			$prefix = null;
			}else
			{
				$prefix = $prefix->prefix;
			}
		}

		$location = 0;
		foreach($orders as $order) {
		
		  if ($order->qty != 0) {
			if($order->location_id != $location)
			{

				$entered_by = Auth::User()->id;
				
				$count = MaterialRequisition::where('prefix','=',$prefix)->max('mr_cnt');
				$req_no = ($count <= 0)? 1 : ($count+1);

				$mode_detail_id = Customer::where('id','=',$customers)
										 ->first()->mode_detail_id;


									// 'request_no' 	=> 'MR'.'-'.$customers.'-'.$req_no,
				
					$insert = MaterialRequisition::create([
									'prefix'	=> $prefix,
									'request_no' 	=> $req_no,
									'mr_cnt' 	 	=> $req_no,
									'entered_by' 	=> $entered_by,
									'customer_id' 	=> $customers,
									'location_id' 	=> $order->location_id,
									'request_date' 	=> date('Y-m-d'),
									'status' 		=> 'DRT',
									'mode_details_id'=> $mode_detail_id,
								]);
			}

			$pr_insert = MRproduct::create([
							'material_requisition_id' => $insert->id,
							'product_id' 			  => $order->product_id,
							'unit_id' 				  => $order->unit_id,
							'grade_id' 				  => $order->grade_id,
							'qty' 					  => $order->qty,
							'remarks'				  => $order->remarks,
						]);

			$location = $order->location_id;
			
		  }

		}

			MRorder::where('customer_id','=',$customers)->delete();
			return redirect()->route('material_request.list');
	}



}
