<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Auth\Middleware\Authenticate;
use App\MaterialRequisitionSea;
use App\CategoryMaster;
use App\Product;
use App\Customer;
use App\NotificationType;
use App\NotificationStatus;
use App\Notification;
use App\Common;
use App\MRseacomment;
use App\Supplier;
use App\ContractSeaPurchase;
use App\ProductStandard;
use App\StandardMaster;
use App\MRseaproduct;
use App\MRseaproductschedule;
use App\ProductUnit;
use App\UnitMeasurement;
use App\ProductGrade;
use App\GradeMaster;
use App\PRSupplierProductSea;
use App\PRproductScheduleSea;
use App\SupplierUnitRate;
use App\SupplierRateEntry;
use App\PRproductSea;
use App\PRsupplierSea;
use App\PrMrSea;
use App\PurchaseRequestSea;


use Auth;


class SeaContractController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function viewList()
	{	
		$customer = Auth::User()->customer_id;

		$list = MaterialRequisitionSea::where('customer_id','=',$customer)
					->whereNotNull('contract_sea_id')
					->whereIN('status',['DRT','REQ'])
					->get();

		$x = 0;
		return view('seacontracts/list',compact('list','x'));
	}

	public function index($id)
	{		
		$x = $id;
		$customer = Auth::User()->customer_id;
		$category = CategoryMaster::all();

		///products
		$customerType = Auth::User()->customer->customer_type_id;
		if($customerType == 3)
		{
			$cus_prd = CustomerProduct::where('customer_id','=',$customer)->pluck('product_id')->toArray();
			$prd = Product::whereIN('id',$cus_prd)->get();
		}else{
			$prd = Product::all();
		}

		/////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);

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

		if($id == 0){
			
			$count = MaterialRequisitionSea::where('customer_id','=',$customer)->count();
			$req_no = 'MR'.'|'.$customer.'|'.($count+1);
			$last_entries = MaterialRequisitionSea::where('customer_id','=',$customer)
							->orderBy('id','DESC')->take(5)->get();
			$contract = ContractSeaPurchase::where('id','=',0)->first();
		}
		else{
			$m_r = MaterialRequisitionSea::find($id);
			$req_no = $m_r->request_no;
			$products = MRseaproduct::where('material_requisition_sea_id','=',$id)->orderBy('id','DESC')->get();
			$last_entries = MaterialRequisitionSea::where('customer_id','=',$customer)
							->where('id','!=',$id)
							->orderBy('id','DESC')->take(5)->get();
			$contract = ContractSeaPurchase::where('id','=',$m_r->contract_sea_id)->first();
		}

		$schedules = Common::where('masterkeyword','=','SEA SCHEDULES')->where('name','!=','NORMAL')->get();
		$month = date("01-M-Y");
		for($i = 1; $i <=12; $i++) {
			$months[] = date("01-M-Y", strtotime($month."+".$i." month"));
		}

		$mr_sea_cmts = MRseacomment::where('material_requisition_sea_id','=',$id)->get();
		
		$suppliers = Supplier::all();
		$contract_types = Common::where('masterkeyword','=','SEA CONTRACT')->get();

		

		return view('seacontracts/index',compact('req_no','x','products','last_entries','n_type_count','nfn','prd','category','schedules','months','mr_sea_cmts','suppliers','contract_types','contract'));
	}

	public function createProductSchedule($x,$pr_insert,$end_dt)
	{
		$schedule = Common::find($pr_insert->schedule_id);
		/*
		if($schedule->name == "WEEKLY") {
			// req month
			$req_date = date('Y-m-d', strtotime($pr_insert->requested_month));
			$cur = date('Y-m-d', strtotime($pr_insert->requested_month));
			$i = 0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=7;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));	

			}
			
			$qty = $pr_insert->qty;
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $pr_insert->requested_month) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
						'material_requisition_sea_id' => $x,
						'mr_sea_product_id' => $pr_insert->id,
						'product_id' => $pr_insert->product_id,
						'unit_id' => $pr_insert->unit_id,
						'grade_id' => $pr_insert->grade_id,
						'standard_id' => $pr_insert->standard_id,
						'unit_price' => $pr_insert->unit_price,
						'qty' => $e_wk_qty,
						'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				 
			}
		} */
		if($schedule->name == "MONTHLY TWO") {
			// req month
			$req_date = date('Y-m-d', strtotime($pr_insert->requested_month));
			$cur = date('Y-m-d', strtotime($pr_insert->requested_month));
			$i = 0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=15;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));	

			}
			
			$qty = $pr_insert->qty;
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $pr_insert->requested_month) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
						'material_requisition_sea_id' => $x,
						'mr_sea_product_id' => $pr_insert->id,
						'product_id' => $pr_insert->product_id,
						'unit_id' => $pr_insert->unit_id,
						'grade_id' => $pr_insert->grade_id,
						'standard_id' => $pr_insert->standard_id,
						'unit_price' => $pr_insert->unit_price,
						'qty' => $e_wk_qty,
						'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				 
			}
		}
		elseif($schedule->name == "MONTHLY THREE") {
			// req month
			$req_date = date('Y-m-d', strtotime($pr_insert->requested_month));
			$cur = date('Y-m-d', strtotime($pr_insert->requested_month));
			$i = 0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=10;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));	

			}
			
			$qty = $pr_insert->qty;
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $pr_insert->requested_month) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
						'material_requisition_sea_id' => $x,
						'mr_sea_product_id' => $pr_insert->id,
						'product_id' => $pr_insert->product_id,
						'unit_id' => $pr_insert->unit_id,
						'grade_id' => $pr_insert->grade_id,
						'standard_id' => $pr_insert->standard_id,
						'unit_price' => $pr_insert->unit_price,
						'qty' => $e_wk_qty,
						'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				 
			}
		} elseif($schedule->name == "MONTHLY FOUR") {
			// req month
			$req_date = date('Y-m-d', strtotime($pr_insert->requested_month));
			$cur = date('Y-m-d', strtotime($pr_insert->requested_month));
			$i = 0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=7;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));	

			}
			
			$qty = $pr_insert->qty;
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $pr_insert->requested_month) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
						'material_requisition_sea_id' => $x,
						'mr_sea_product_id' => $pr_insert->id,
						'product_id' => $pr_insert->product_id,
						'unit_id' => $pr_insert->unit_id,
						'grade_id' => $pr_insert->grade_id,
						'standard_id' => $pr_insert->standard_id,
						'unit_price' => $pr_insert->unit_price,
						'qty' => $e_wk_qty,
						'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				 
			}
		}
		else {
			// req month
			$req_date = date('Y-m-d', strtotime($pr_insert->requested_month));
			$cur = date('Y-m-d', strtotime($pr_insert->requested_month));
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				$mnth[] = $cur;
				$cur = date('Y-m-d', strtotime('+1 month', strtotime($cur)));

			}
			$qty = $pr_insert->qty;
			$w = count($mnth);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);


			foreach($mnth as $e_mth) {
				$e_mth_qty = $each_sch_qty; 
				if($e_mth == $pr_insert->requested_month) {
					$r = ($rmd > 0)? 1 : 0;
					$e_mth_qty = $e_mth_qty + $r;
				}
				if($e_mth_qty) {
					MRseaproductschedule::create([
							'material_requisition_sea_id' => $x,
							'mr_sea_product_id' => $pr_insert->id,
							'product_id' => $pr_insert->product_id,
							'unit_id' => $pr_insert->unit_id,
							'grade_id' => $pr_insert->grade_id,
							'standard_id' => $pr_insert->standard_id,
							'unit_price' => $pr_insert->unit_price,
							'qty' => $e_mth_qty,
							'scheduled_date' => date('Y-m-d', strtotime($e_mth))
					]);
				}
				 
			}
		} 
	}

	public function insertRequest(Request $request)
	{
		$schedule = Common::find($request->get('schedule'));
		$entered_by = Auth::User()->id;
		$customer = Auth::User()->customer_id;
		$count = MaterialRequisitionSea::where('customer_id','=',$customer)->count();
		$req_no = $count+1;

		if($request->get('x') == 0){


			$contract = ContractSeaPurchase::create([
				'contract_id' => $request->get('contract_type'),
				'supplier_id' => $request->get('supplier_id'),
				'address' 	  => $request->get('address'),
				'contact_person' => $request->get('contact_person'),
				'mobile_no' => $request->get('mobile_no'),
				'email_id' => $request->get('email_id'),
				'port_arrival' => $request->get('port_of_arrival'),
				'payment_terms' => $request->get('payment_terms'),
				'shipping_terms' => $request->get('shipping_terms'),
				'terms_and_condition' => $request->get('terms_conditions'),
				'time_frame_from' => date('Y-m-d',strtotime($request->get('from_date'))),
				'time_frame_to' => date('Y-m-d',strtotime($request->get('end_date')))
			]);
			
			$insert = MaterialRequisitionSea::create([
							'request_no' => 'MR'.'|'.$customer.'|'.$req_no,
							'entered_by' => $entered_by,
							'customer_id' => $customer,
							'request_date' => date('Y-m-d'),
							'status' => 'REQ',
							'contract_sea_id' => $contract->id,
							'flag_contract' => 'SEANEW'
						]);

			$pr_insert = MRseaproduct::create([
							'material_requisition_sea_id' => $insert->id,
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $request->get('qty'),
							'requested_month' => date('Y-m-d', strtotime($request->get('from_date'))),
							'schedule_id' => $request->get('schedule')
						]);
			$req_id = $insert->id;
			$x = $insert->id;
			
			$this->createProductSchedule($x,$pr_insert,$request->get('end_date'));

		}
		else{
			$x = $request->get('x');
			
			
			$z = MRseaproduct::where('material_requisition_sea_id','=',$x)
							->where('product_id','=',$request->get('product'))
							->where('unit_id','=',$request->get('unit'))
							->where('grade_id','=',$request->get('grade'))
							->where('standard_id','=',$request->get('standard'))
							->count();
							
			if($z > 0){
				return Redirect::back()->with(['exist' => true]);
				return redirect()->action('SeaContractController@index',['id'=>$x]);
			}

			// check already product is requested in this month
			$dup_req_prod_mnth = MRseaproduct::where('product_id','=',$request->get('product'))
									->where('unit_id','=',$request->get('unit'))
									->where('grade_id','=',$request->get('grade'))
									->where('standard_id','=',$request->get('standard'))
									->whereNull('deleted_at')
									->where('requested_month','>=',date('Y-m-d', strtotime($request->get('from_date'))))
									->where('requested_month','<=',date('Y-m-d', strtotime($request->get('end_date'))))
									->count();

			if($dup_req_prod_mnth) {
				return Redirect::back()->with(['exist' => true]);
			}

			$mr = MaterialRequisitionSea::find($x);
			
			ContractSeaPurchase::where('id','=',$mr->contract_sea_id)
			->update([
				'contract_id' => $request->get('contract_type'),
				'supplier_id' => $request->get('supplier_id'),
				'address' 	  => $request->get('address'),
				'contact_person' => $request->get('contact_person'),
				'mobile_no' => $request->get('mobile_no'),
				'email_id' => $request->get('email_id'),
				'port_arrival' => $request->get('port_of_arrival'),
				'payment_terms' => $request->get('payment_terms'),
				'shipping_terms' => $request->get('shipping_terms'),
				'terms_and_condition' => $request->get('terms_conditions'),
				'time_frame_from' => date('Y-m-d',strtotime($request->get('from_date'))),
				'time_frame_to' => date('Y-m-d',strtotime($request->get('end_date')))
			]);

			$pr_insert = MRseaproduct::create([
							'material_requisition_sea_id' => $request->get('x'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $request->get('qty'),
							'requested_month' => date('Y-m-d', strtotime($request->get('from_date'))),
							'schedule_id' => $request->get('schedule')
						]);
			$req_id = $request->get('x');

			$this->createProductSchedule($x,$pr_insert,$request->get('end_date'));
		}


		$category = CategoryMaster::all();
		$products = MRseaproduct::where('material_requisition_sea_id','=',$req_id)->get();
		
		
		return redirect()->action('SeaContractController@index',['id'=>$x]);
	}

	public function editProduct(Request $request, $id)
	{
		$mr = MRseaproduct::find($id);

		///products
		$customerType = $mr->materialRequest->customer->customer_type_id;

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

		$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();
		$x = $mr->material_requisition_sea_id;
		$products = MRseaproduct::where('material_requisition_sea_id','=',$x)->orderBy('id','DESC')->get();

		$schedules = Common::where('masterkeyword','=','SEA SCHEDULES')->get();
		$month = date("01-M-Y");
		for($i = 1; $i <=12; $i++) {
			$months[] = date("01-M-Y", strtotime($month."+".$i." month"));
		}

		$stds = ProductStandard::where('product_id','=',$mr->product_id)
						->where('grade_id','=',$mr->grade_id)
						->pluck('standard_id')->toArray();
		
		$standards = StandardMaster::whereIn('id',$stds)
						->where('is_active','=','Y')
						->selectRaw('id,standard')
						->get();

		$mr_sea_cmts = MRseacomment::where('material_requisition_sea_id','=',$x)->get();
		$mrs = MaterialRequisitionSea::find($x);
		$suppliers = Supplier::all();
		$contract = ContractSeaPurchase::where('id','=',$mrs->contract_sea_id)->first();
		$contract_types = Common::where('masterkeyword','=','SEA CONTRACT')->get();
		
		return view('seacontracts/edit_product',compact('mr','x','product','unit','grade','products','schedules','months','mr_sea_cmts','standards','suppliers','contract','contract_types'));
	}

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


	public function productGradeStandards(Request $request)
	{
		$stds = ProductStandard::where('product_id','=',$request->get('product'))
						->where('grade_id','=',$request->get('grade'))
						->pluck('standard_id')->toArray();

		$standards = StandardMaster::whereIn('id',$stds)
						->where('is_active','=','Y')
						->selectRaw('id,standard')
						->get();

		return $standards;
	}

	public function supplierDetails(Request $request)
	{
		$supplier = Supplier::where('id','=',$request->get('supplier'))->first();

		return $supplier;
	}

	public function updateProduct(Request $request)
	{
		$schedule = Common::find($request->get('schedule'));
		MRseaproductschedule::where('mr_sea_product_id','=',$request->get('mr_id'))->delete();
		
		if($schedule->name == "MOTNTHLY TWO") {
			// req month
			$req_date = date('Y-m-d', strtotime($request->get('from_date')));
			$cur = date('Y-m-d', strtotime($request->get('from_date')));
			$i=0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=15;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));				
			}
			
			$qty = $request->get('qty');
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $req_date) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
							'material_requisition_sea_id' => $request->get('x'),
							'mr_sea_product_id' => $request->get('mr_id'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $e_wk_qty,
							'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				
			}
		}  elseif($schedule->name == "MOTNTHLY THREE") {
			// req month
			$req_date = date('Y-m-d', strtotime($request->get('from_date')));
			$cur = date('Y-m-d', strtotime($request->get('from_date')));
			$i=0;
			while ($cur <=  date('Y-m-d', strtotime($end_dt))) {
				//wk end month
				$nxt_m = date("m", strtotime($cur."+".$i."days"));

				// check wk end in same month
				if(date('m', strtotime($end_dt)) >= $nxt_m) {
					$wks[] = date("Y-m-d", strtotime($cur));
				}

				$i=10;
				$cur = date("Y-m-d", strtotime($cur."+".$i."days"));				
			}
			
			$qty = $request->get('qty');
			$w = count($wks);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);
			// 
			
			foreach($wks as $e_wk) {
				$e_wk_qty = $each_sch_qty; 
				if($e_wk == $req_date) {
					$r = ($rmd > 0)? 1 : 0;
					$e_wk_qty = $e_wk_qty + $r;
				}
				if($e_wk_qty) {
					MRseaproductschedule::create([
							'material_requisition_sea_id' => $request->get('x'),
							'mr_sea_product_id' => $request->get('mr_id'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $e_wk_qty,
							'scheduled_date' => date('Y-m-d', strtotime($e_wk))
					]);
				}
				
			}
		}  
		else {
			// req month
			$req_date = date('Y-m-d', strtotime($request->get('from_date')));
			$cur = date('Y-m-d', strtotime($request->get('from_date')));
			while ($cur <=  date('Y-m-d', strtotime($request->get('end_date')))) {
				$mnth[] = $cur;
				$cur = date('Y-m-d', strtotime('+1 month', strtotime($cur)));
			}
			$qty = $request->get('qty');
			$w = count($mnth);
			//get reminder
			$rmd =  $qty % $w;
			// schd qty
			$each_sch_qty = (int) round($qty/$w, PHP_ROUND_HALF_DOWN);

			foreach($mnth as $e_mth) {
				$e_mth_qty = $each_sch_qty; 
				if($e_mth == $req_date) {
					$r = ($rmd > 0)? 1 : 0;
					$e_mth_qty = $e_mth_qty + $r;
				}
				if($e_mth_qty) {
					MRseaproductschedule::create([
							'material_requisition_sea_id' => $request->get('x'),
							'mr_sea_product_id' => $request->get('mr_id'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $e_mth_qty,
							'scheduled_date' => date('Y-m-d', strtotime($e_mth))
					]);
				}
			}
		}
		
		$update = 	MRseaproduct::where('id','=',$request->get('mr_id'))
						->update([
							'material_requisition_sea_id' => $request->get('x'),
							'product_id' => $request->get('product'),
							'unit_id' => $request->get('unit'),
							'grade_id' => $request->get('grade'),
							'standard_id' => $request->get('standard'),
							'unit_price' => $request->get('unit_price'),
							'qty' => $request->get('qty'),
							'requested_month' => date('Y-m-01', strtotime($request->get('from_date'))),
							'schedule_id' => $request->get('schedule')
					]);


		$x = $request->get('x');

		$mr = MaterialRequisitionSea::find($x);

		ContractSeaPurchase::where('id','=',$mr->contract_sea_id)
			->update([
				'contract_id' => $request->get('contract_type'),
				'supplier_id' => $request->get('supplier_id'),
				'address' 	  => $request->get('address'),
				'contact_person' => $request->get('contact_person'),
				'mobile_no' => $request->get('mobile_no'),
				'email_id' => $request->get('email_id'),
				'port_arrival' => $request->get('port_of_arrival'),
				'payment_terms' => $request->get('payment_terms'),
				'shipping_terms' => $request->get('shipping_terms'),
				'terms_and_condition' => $request->get('terms_conditions'),
				'time_frame_from' => date('Y-m-d',strtotime($request->get('from_date'))),
				'time_frame_to' => date('Y-m-d',strtotime($request->get('end_date')))
		]);
		
		return redirect()->action('SeaContractController@index',['id'=>$x]);
	}

	public function requestSubmit(Request $request)
	{
		if (isset($_POST['submit'])) {
			MaterialRequisitionSea::where('id','=',$request->get('x'))
					->update(['status' => 'REQ']);

			$mr_sea = MaterialRequisitionSea::find($request->get('x'));
			if($mr_sea) {
				$cont_sea_purch = ContractSeaPurchase::find($mr_sea->contract_sea_id);
				$pr_no = PurchaseRequestSea::count() + 1;
				$pr_sea = PurchaseRequestSea::create([
					'request_no' => 'PR'.$pr_no,
					'request_date' => date('Y-m-d'),
					'entered_by' => Auth::user()->id,
					'status' => 'SUP'
				]);

				PrMrSea::create([
					'pr_sea_id' => $pr_sea->id,
					'mr_sea_id' => $mr_sea->id
				]);

				$mr_sea_products = MRseaproduct::where('material_requisition_sea_id','=',$mr_sea->id)
										->whereNull('deleted_at')
										->get();

				$pr_sup_sea = PRsupplierSea::create([
					'purchase_request_sea_id' => $pr_sea->id,
					'supplier_id' => $cont_sea_purch->supplier_id,
					'status' => 'PR',
					'eta' => $cont_sea_purch->time_frame_from,
					'supp_flag' => 'CNTRCT'
				]);

				foreach($mr_sea_products as $mr_sea_prd) {
					$pr_prd_sea = PRproductSea::create([
						'purchase_request_sea_id' => $pr_sea->id,
						'product_id' => $mr_sea_prd->product_id,
						'qty' => $mr_sea_prd->qty,
						'apr_qty' => $mr_sea_prd->qty,
						'unit_id' => $mr_sea_prd->unit_id,
						'grade_id' => $mr_sea_prd->grade_id,
						'standard_id' => $mr_sea_prd->standard_id,
						'status' => 'PR',
						'requested_month' => $mr_sea_prd->requested_month,
						'schedule_id' => $mr_sea_prd->schedule_id
					]);
					$mr_sea_prd_schds = MRseaproductschedule::where('mr_sea_product_id','=',$mr_sea_prd->id)
											->where('material_requisition_sea_id','=',$mr_sea->id)->get();
					///
					$grade = ($mr_sea_prd->grade_id)? $mr_sea_prd->grade_id : 0;
					$std = ($mr_sea_prd->standard_id)? $mr_sea_prd->standard_id : 0;
					$wgt = 0;
					$bprd = Product::find($mr_sea_prd->product_id);
					$unit_change = SupplierUnitRate::where('supplier_id','=',$cont_sea_purch->supplier_id)
				   					->where('product_id','=',$mr_sea_prd->product_id)
				   					->where('standard_id','=',$std)
				   					->where('grade_id','=',$grade)
				   					->where('unit_id','=',$bprd->base_unit_id)
				   					->first();

				   					// ->where('is_active','=','Y')

				   	$actual_rate = SupplierRateEntry::where('supplier_id','=',$cont_sea_purch->supplier_id)
				   					->where('product_id','=',$mr_sea_prd->product_id)
				   					->where('standard_id','=',$std)
				   					->where('grade_id','=',$grade)
				   					->where('unit_id','=',$mr_sea_prd->unit_id)
				   					->where('is_active','=','Y')
				   					->first();

				   	$actual_unit = ($actual_rate)? $actual_rate->price : 0.000;
				   	$unitprice = $mr_sea_prd->unit_price;
				   	$price = $mr_sea_prd->qty * $unitprice;
				   	if($mr_sea_prd->unit_id != 1) {
				   		$wgt = $unit_change * $mr_sea_prd->qty;
				   	} else {
				   		$wgt = $mr_sea_prd->qty;
				   	}

					foreach($mr_sea_prd_schds as $sea_prd_schdls) {
						$pr_prod_sch_sea = PRproductScheduleSea::create([
							'purchase_request_sea_id' => $pr_sea->id,
							'pr_products_sea_id' => $pr_prd_sea->id,
							'product_id' => $sea_prd_schdls->product_id,
							'qty' => $sea_prd_schdls->qty,
							'apr_qty' => $sea_prd_schdls->qty,
							'unit_id' => $sea_prd_schdls->unit_id,
							'grade_id' => $sea_prd_schdls->grade_id,
							'standard_id' => $sea_prd_schdls->standard_id,
							'scheduled_date' => $sea_prd_schdls->scheduled_date,
							'schedule_id' => $mr_sea_prd->schedule_id,
							'supplier_id' => $cont_sea_purch->supplier_id,
							'status' => 'PR',
						]);

						PRSupplierProductSea::create([
							'pr_supplier_sea_id' => $pr_sup_sea->id,
							'pr_product_sea_id' => $pr_prd_sea->id,
							'pr_products_schedule_sea_id' => $pr_prod_sch_sea->id,
							'product_id' => $sea_prd_schdls->product_id,
							'qty' => $sea_prd_schdls->qty,
							'apr_qty' => $sea_prd_schdls->qty,
							'unit_id' => $sea_prd_schdls->unit_id,
							'grade_id' => $sea_prd_schdls->grade_id,
							'standard_id' => $sea_prd_schdls->standard_id,
							'unitprice' => $unitprice,
							'actual_unit' => $actual_unit,
							'price' => $price,
							'weight' => $wgt,
							'scheduled_date' => $sea_prd_schdls->scheduled_date,
							'schedule_id' => $mr_sea_prd->schedule_id,
							'status' => 'PR'
						]); 
					}

				}

			}


		}

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

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

	public function viewEdit($id)
	{	
		$material_request = MaterialRequisitionSea::find($id);
		///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)
							->get();
		}else{
			$prd = Product::all();
		}
		/////////////////////////
		$products = MRseaproduct::where('material_requisition_sea_id','=',$id)->orderBy('id','DESC')->get();
		$category = CategoryMaster::all();
		$customer = Auth::User()->customer_id;
		$last_entries = MaterialRequisitionSea::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);

        $schedules = Common::where('masterkeyword','=','SEA SCHEDULES')->take(2)->get();
		$month = date("01-M-Y");
		for($i = 1; $i <=12; $i++) {
			$months[] = date("01-M-Y", strtotime($month."+".$i." month"));
		}

		$mr_sea_cmts = MRseacomment::where('material_requisition_sea_id','=',$id)->get();
		//////////////////////
		$suppliers = Supplier::all();
		$contract_types = Common::where('masterkeyword','=','SEA CONTRACT')->get();
		$contract = ContractSeaPurchase::where('id','=',$material_request->contract_sea_id)->first();
		$x = $material_request->id;
		return view('seacontracts/edit',compact('material_request','products','mr','category','last_entries','n_type_count','nfn','prd','months','schedules','mr_sea_cmts','suppliers','contract_types','contract','x'));
	}
}