<?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 MaterialRequestReportController 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();

		$list = MaterialRequisition::join('mr_products','mr_products.material_requisition_id','=','material_requisition.id')
							->join('products','products.id','=','mr_products.product_id')
							
                         	->join('customers','material_requisition.customer_id','=','customers.id')
                           	->leftjoin('parent_master','parent_master.id','=','material_requisition.location_id')                          	
							->where('material_requisition.request_date','>=',$from_date)
                           	->where('material_requisition.request_date','<=',$to_date)
                           	->where('material_requisition.customer_id','=',$request->get('customer'))
                           	->where('material_requisition.mode_details_id','=',$mode_id->id)
                           	->selectRaw('sum(mr_products.qty) AS sum_qty,parent_master.name as location,products.name as product,products.id pro_id')
                        	->groupBy('parent_master.name','products.name','products.id')
                        	->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');
		
		return view('mr_report/report',compact('list','location','from_date','to_date','location_qty','customer_id'));
	}


}