<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Auth\Middleware\Authenticate;
use Auth;
use App\Product;
use App\UnitMeasurement;
use App\GradeMaster;
use App\PurchaseRequest;
use App\PRproduct;
Use App\Supplier;
use App\PO;
use App\POProducts;
use App\POconsignment;
use App\ConsignmentProduct;
use App\Port;
use App\Office;
/**
* 
*/
class PurchaseOrderController extends Controller
{
	public function index()
	{
		$polist = PO::orderBy('po_date','DESC')->get();
		$consignment = [];
		foreach($polist as $po){
			$consignment[$po->id] = POconsignment::where('po_id','=',$po->id)->count();
		}
		$group = Auth::user()->userTypes->first();

		return view('purchase_order/index',compact('polist','consignment','group'));
	}

	public function addConsignment($id)
	{	
		$po_id = $id;
		$ports = Port::all();
		$consignments = POconsignment::where('po_id','=',$id)->get();

		return view('purchase_order/addConsignment',compact('po_id','ports','consignments'));
	}

	public function loadports()
	{
		$ports = Port::all();
		return $ports;
	}

	public function insertConsignment(Request $request)
	{
		$count = POconsignment::where('id','=',$request->get('cons'))->count();

		if($count < 1){
			$consignment = POconsignment::create([
													'po_id' => $request->get('poid'),
													'bill_no' => $request->get('bill'),
													'dispatch_port' => $request->get('dispatch'),
													'arrival_port' => $request->get('delivery'),
													'eta' => $request->get('eta')]);
			$id = $consignment->id;
			$flag = 1;
		}
		else{
			$consignment = POconsignment::where('id','=',$request->get('cons'))
										->update([
													'bill_no' => $request->get('bill'),
													'dispatch_port' => $request->get('dispatch'),
													'arrival_port' => $request->get('delivery'),
													'eta' => $request->get('eta')]);
			$id = $request->get('cons');
			$flag = 0;
		}

		$data = array($id,$flag);

		return $data;
	}


	public function getProducts(Request $request)
	{	
		$omit = [];
		$check = ConsignmentProduct::where('po_consignment_id','=',$request->get('cons'))->count();
		$cons_products = ConsignmentProduct::join('products','products.id','=','consignment_products.product_id')
										->join('po_products','products.id','po_products.product_id')
										->join('unit_of_measurement','consignment_products.unit_id','=','unit_of_measurement.id')
										->leftjoin('grade_master','grade_master.id','consignment_products.grade_id')
										->where('consignment_products.po_consignment_id','=',$request->get('cons'))
										->where('po_products.po_id','=',$request->get('poid'))
										->select('products.name','products.id AS prd','consignment_products.supplied_qty AS supply','po_products.bal_qty AS tot_qty','unit_of_measurement.unit','unit_of_measurement.id AS unit_id','grade_master.grade','grade_master.id AS grade_id','consignment_products.id')->get();

		$prds = POProducts::where('po_id','=',$request->get('poid'))->get();
		foreach($prds as $prd){
			$check = ConsignmentProduct::where('po_id','=',$request->get('poid'))
										->where('product_id','=',$prd->product_id);

			if($check->count() > 0){
				if($check->where('po_consignment_id','=',$request->get('cons'))->count() > 0){
					$omit[] = $prd->product_id;
				}
				elseif( ($prd->qty < $check->sum('supplied_qty')) || ($prd->qty == $check->sum('supplied_qty')) ){

					$omit[] = $prd->product_id;
				}
			}
		}

		$products = POProducts::join('products','products.id','=','po_products.product_id')
								->join('unit_of_measurement','po_products.unit_id','=','unit_of_measurement.id')
								->leftjoin('grade_master','grade_master.id','po_products.grade_id')
								->where('po_products.po_id','=',$request->get('poid'))
								->whereNotIN('po_products.product_id',$omit)
								->where('po_products.bal_qty','!=',0)
								->select('products.name','products.id AS prd','po_products.qty AS supply','po_products.bal_qty AS tot_qty','unit_of_measurement.unit','unit_of_measurement.id AS unit_id','grade_master.grade','grade_master.id AS grade_id')->get();

		$data = array($cons_products,$products);
		return $data;
	}

	
	public function mapConsProducts(Request $request)
	{	
		$i = 0;
		$delete = ConsignmentProduct::where('po_consignment_id','=',$request->get('consignment_id'))
									->whereNotIN('product_id',$request->get('product'))->get();

		$check = ConsignmentProduct::where('po_consignment_id','=',$request->get('consignment_id'))->delete();

		foreach ($request->get('product') as $value) {

			if($request->get('qty_supply')[$i] != NULL){
				$map = ConsignmentProduct::create([
											'po_id' => $request->get('po'),
											'po_consignment_id' => $request->get('consignment_id'),
											'product_id' => $value,
											'tot_qty' => $request->get('tot_qty')[$i],
											'supplied_qty' => $request->get('qty_supply')[$i],
											'unit_id' => $request->get('unit')[$i],
											'grade_id' => $request->get('grade')[$i]
										]);

				$sum = ConsignmentProduct::where('po_id','=',$request->get('po'))
								->where('product_id','=',$value)->sum('supplied_qty');
		
				$po_prd = POProducts::where('po_id','=',$request->get('po'))
						->where('product_id','=',$value)->first();

				$bal = $po_prd->qty - $sum;

				POProducts::where('po_id','=',$request->get('po'))
						->where('product_id','=',$value)
						->update(['bal_qty' => ($bal < 0) ? 0 : $bal]);
			}

			$i++;
		}

		foreach($delete as $data){
			$prd = POProducts::where('po_id','=',$request->get('po'))
					->where('product_id','=',$data->product_id)->first();

			POProducts::where('po_id','=',$request->get('po'))
					->where('product_id','=',$data->product_id)
					->update(['bal_qty' => $prd->bal_qty + $data->supplied_qty]);
		}

		$poid = $request->get('po');

		return redirect("po/consignment/add/".$poid);
		
	}

	public function printPO($id)
	{	
		$po = PO::find($id);
		$products = POProducts::where('po_id','=',$id)->get();
	
		return view('purchase_order/printPO',compact('products','po'));
	}

	public function supplyQty(Request $request)
	{	
		$flag = 0;
		$po_prd = POProducts::where('po_id','=',$request->get('po_id'))
							->where('product_id','=',$request->get('product'))->first();
		if($request->get('cons_prd') != NULL){
			$cons_prd = ConsignmentProduct::find($request->get('cons_prd'));
			$add = $po_prd->bal_qty + $cons_prd->supplied_qty;

			if($request->get('qty') > $add){
				$flag = 1;
			}
		}
		else{
			if($request->get('qty') > $po_prd->bal_qty){
				$flag = 1;
			}
		}

		return $flag;
			
	}

	public function deleteConsignment(Request $request)
	{	

		$consignment = POconsignment::find($request->get('option'));
		$products = ConsignmentProduct::where('po_consignment_id','=',$consignment->id)->get();


		foreach($products as $prd)
		{
			$bal = POProducts::where('po_id','=',$consignment->po_id)
								->where('product_id','=',$prd->product_id)->first()->bal_qty;

			 POProducts::where('po_id','=',$consignment->po_id)
								->where('product_id','=',$prd->product_id)
								->update(['bal_qty' => $bal + $prd->supplied_qty]);

			ConsignmentProduct::find($prd->id)->delete();				

		}

		$consignment->delete();

		return;
	}	

}