<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Customer;
use App\MaterialRequisition;
use App\ModeDetail;
use App\MRapprovalproduct;
use App\CustomerLocation;
use App\Location;
use App\MRproductHistory;
use Auth;

class CashAndCreditReportController extends Controller
{  
	public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
	{
		$customer = Customer::all();
		return view('cash_credit_report/index',compact('customer'));
	}
	public function report(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]));
		$type =$request->get('type');
		$mode_id =  ModeDetail::where('mode_name','=',$request->get('type'))->first()->id;
		$detailed =$request->get('detailed');
		$customer_id = CustomerLocation::where('location_id','=',Auth::User()->location_id)								->pluck('customer_id');

		if ($detailed) {

			$list = MaterialRequisition::leftjoin('customers','customers.id','=','material_requisition.customer_id')
                  ->join('users','users.id','=','material_requisition.entered_by')
									->where('mode_details_id','=',$mode_id)
                                   ->where('material_requisition.status','=','CMP')
                                   ->where('material_requisition.request_date','>=',$from_date)
                           			->where('material_requisition.request_date','<=',$to_date);

            
        	if(Auth::User()->location_id != null)
            {
               $list =  $list->where('users.location_id','=',Auth::User()->location_id);
            }
               $list =  $list->selectRaw('material_requisition.*,customers.name as customer')->orderBy('request_no')
                           		    ->get();

            $mr_id = MaterialRequisition::join('users','users.id','=','material_requisition.entered_by')
            					   ->where('mode_details_id','=',$mode_id)
                                   ->where('status','=','CMP')
                                   ->where('material_requisition.request_date','>=',$from_date)
                           			->where('material_requisition.request_date','<=',$to_date) ;
          
        	if(Auth::User()->location_id != null)
            {
               $mr_id =  $mr_id->where('users.location_id','=',Auth::User()->location_id);
            }
                $mr_id =  $mr_id->orderBy('material_requisition.request_no')
                           		    ->pluck('material_requisition.id');

            $products = MRapprovalproduct::whereIn('material_requisition_id',$mr_id)
            							->get();


		return view('cash_credit_report/detailed',compact('list','from_date','to_date','type','products'));
			
		}

       
       


	  	$list = MaterialRequisition::leftjoin('customers','customers.id','=','material_requisition.customer_id')->join('users','users.id','=','material_requisition.entered_by')
	  							   ->where('material_requisition.mode_details_id','=',$mode_id)
                                   ->where('material_requisition.status','=','CMP')
                                   ->where('material_requisition.request_date','>=',$from_date)
                           			->where('material_requisition.request_date','<=',$to_date);
            if(Auth::User()->location_id != null)
            {
               $list =  $list->where('users.location_id','=',Auth::User()->location_id);
            }
               $list =  $list->selectRaw('material_requisition.*,customers.name as customer')
                                  ->get();          

		return view('cash_credit_report/view',compact('list','from_date','to_date','type'));
	}

  public function historyOfOrder($id)
  {
      $order = MaterialRequisition::find($id);
      $order_products = MRproductHistory::where('material_requisition_id','=',$id)->get();
      return view('cash_credit_report/order_history',compact('order_products','order'));
  }

  public function orderHistoryReport(Request $request)
  {
    if($request->get('date')) {
      $date = explode('-', $request->get('date'));
      $to_date = date('Y-m-d',strtotime($date[1]));
      $from_date = date('Y-m-d',strtotime($date[0]));

      $order_products = MRproductHistory::join('material_requisition','material_requisition.id','=','mr_product_history.material_requisition_id')
                        ->join('products','products.id','=','mr_product_history.product_id')
                        ->join('unit_of_measurement','unit_of_measurement.id','=','mr_product_history.unit_id')
                        ->join('users','users.id','=','mr_product_history.user_id')
                        ->leftjoin('grade_master','grade_master.id','=','mr_product_history.grade_id')
                        ->leftjoin('mr_products','mr_products.id','=','mr_product_history.mr_product_id')
                        ->where('material_requisition.cash_mode','=','CASH')
                        ->where('material_requisition.request_date','>=',$from_date)
                        ->where('material_requisition.request_date','<=',$to_date)
                        ->selectRaw('material_requisition.request_no,material_requisition.created_at,material_requisition.updated_at,material_requisition.prefix,products.name,unit_of_measurement.unit,grade_master.grade,mr_product_history.qty,mr_product_history.price,mr_product_history.tax_amount,mr_product_history.total_amount,users.name as uname,material_requisition.request_date,mr_products.id as mrproduct,mr_product_history.mr_product_id')->orderBy('products.name')->get();
      // with(['materialRequest' => function($query) use ($from_date,$to_date) {
      //   $query->where('request_date','>=',$from_date)->where('request_date','<=',$to_date)
      //     ->where('cash_mode','=','CASH')->where('status','=','APR')->where('prefix','!=','')->where('request_no','!=','')->whereNotNull('prefix')->whereNotNull('request_no');
      // }])->take(200)->get();
      // die($order_products);
      $type = 'CASH';
      return view('cash_credit_report/order_history_report',compact('order_products','to_date','from_date','type')); 
    } else {
       return view('cash_credit_report/order_history_page'); 
    }
    
  }
}
	
