<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Customer;
use App\Common;
use App\CategoryMaster;
use App\Product;
use App\Location;
use App\ModeDetail;
use App\ParentMaster;
use App\CustomerParent;
use App\MaterialRequisition;
use Auth;

class RequestsReportController extends Controller
{

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

	public function index()
	{
		$customer = Customer::all();
		return view('mr_report/index',compact('customer'));
	}
	public function mrReport(Request $request)
	{		
		$date = explode('-', $request->get('date'));
		$to_date = date('Y-m-d',strtotime($date[1]));
		$from_date = date('Y-m-d',strtotime($date[0]));

		// $mr_list = MaterialRequisition::where('customer_id','=',$request->get('customer'))
		// 								->whereBetween('request_date',[$from_date,$to_date]) 
		// 								->get();

        $mode_id =  ModeDetail::where('mode_name','=','CREDIT')->first();

        $customer_id = $request->get('customer');


		$lists = MaterialRequisition::join('mr_products','mr_products.material_requisition_id','=','material_requisition.id')
							->join('products','products.id','=','mr_products.product_id')
							
                         	->leftjoin('customers','material_requisition.customer_id','=','customers.id')
                           	->leftjoin('parent_master','parent_master.id','=','material_requisition.location_id')   
                           	->join('unit_of_measurement','unit_of_measurement.id','=','mr_products.unit_id')
                           	->join('grade_master','grade_master.id','=','mr_products.grade_id')   
                           	->join('users','users.id','=','material_requisition.entered_by')                     	                       	
							->where('material_requisition.request_date','>=',$from_date)
                           	->where('material_requisition.request_date','<=',$to_date);

			if ($customer_id != null) {

           	$lists = $lists->where('material_requisition.customer_id','=',$request->get('customer'));
             }
             if(Auth::User()->location_id != null)
            {
               $lists =  $lists->where('users.location_id','=',Auth::User()->location_id);
            }
             
            $list =  $lists->selectRaw('sum(mr_products.qty) AS sum_qty,products.name as product,products.id pro_id,unit_of_measurement.unit,grade_master.grade')
                        	->groupBy('products.name','products.id','unit_of_measurement.unit','grade_master.grade')
                        	->orderBy('products.id')
                           	->get();

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

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

		$location_qty = new ParentMaster;

		$customer_id = $request->get('customer');
		if ($customer_id == null) {
			$customer_name = null;
		}else{
			$customer_name = Customer::where('id','=',$request->get('customer'))->first()->name;
		}
		return view('mr_report/report',compact('list','location','from_date','to_date','location_qty','customer_id','customer_name'));
	}


}
