<?php
	
	namespace App\Http\Controllers;
	use Illuminate\Foundation\Bus\DispatchesJobs;
	use Illuminate\Routing\Controller as BaseController;
	use Illuminate\Foundation\Validation\ValidatesRequests;
	use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
	use Illuminate\Foundation\Auth\Access\AuthorizesResources;
	use Illuminate\Http\Request;
	use DB;
	use App\Http\Model\WBReceiptModel;
	use Validator;
	
	class WBReceiptController extends Controller
	{
		public function __construct()
		{
			$this->WBReceiptModel= new WBReceiptModel;
		}
		public function LoadReceipt()
		{
			$data['url']='WBReceipt';
		    $Mid=$this->WBReceiptModel->ReceiptPermission($data);	
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			return view('WBReceipt',compact('data'));
		}		
		public function InsertBranch(Request $req)
		{
			$br_id = $req->input('br_id');
			$branchname= $req->input('branchname');
			$branchtype= $req->input('branchtype');
			$description= $req->input('description');
			$status = $req->input('status');
			
			if($br_id == 0)
			{
				$data = array('br_name'=>$branchname,'br_type'=>$branchtype,'br_description'=>$description,'br_status'=>$status);
				$insertdetails = DB::table('br_branch')->insert($data);
				if (!is_null($insertdetails))
				{
					return response()->json(['status' => '1', 'message' => trans('Record Inserted Successfully.')]);
				}
				else
				{
					return response()->json(['status' => '0', 'message' => trans('Error in Insertion.. Please Try again..')]);
				}
			}
			else
			{
				$data = array('br_name'=>$branchname,'br_type'=>$branchtype,'br_description'=>$description,'br_status'=>$status);
				$updatedetails = DB::table('br_branch')->where('br_id',$br_id)->update($data);
				if (!is_null($updatedetails))
				{
					return response()->json(['status' => '1', 'message' => trans('Record Updated Successfully.')]);
				}
				else
				{
					return response()->json(['status' => '0', 'message' => trans('Error in Updation.. Please Try again..')]);
				}
			}			
		}		
		public function EditBranch(Request $req)
		{
			$id = $req->id;
			$branch = DB::table('br_branch')->where(['br_id'=>$id])->get();
			return response()->json($branch);
		}		
		public function DeleteBranch(Request $req)
		{
			$id = $req->id;
			$status= 'Deleted';
			$data = array('br_status'=>$status);
			
			$UpdateDetails = DB::table('br_branch')->where('br_id',$id)->update($data);
			if (!is_null($UpdateDetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Deleted Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Deletion.. Please Try again..')]);
			}
		}
		public function searchrecord(Request $req)
		{
		  return response()->json($this->WBReceiptModel->searchquery($req['query']));  
		}
		public function Displaycstrecord(Request $req)
		{
			$custid= $req->input('custid');
			$data['customer'] = $this->WBReceiptModel->modSearchcstrecord($custid);
			$data['receiptrec'] = $this->WBReceiptModel->modSearchreceiptreceipt($custid);
			$data['nextreceiptnumber'] = $this->WBReceiptModel->modnextreceiptno($data['customer']->usr_id,"Receipt");
			$data['balamt'] = $this->WBReceiptModel->modbalamt($custid);
			return response()->json($data);
		}
		public function calculatetariffchart(Request $req)
		{
			$tarf_id= $req->input('tarfid');
			$data['tariffcharge'] = $this->WBReceiptModel->modtariffchargelist($tarf_id);
			return response()->json($data);
		}
		//Inserting water receipt record
		public function insertreceipt(Request $req)
		{
			$custid = $req->input('custid');
			$cmboperator =  $req->input('cmboperator');
			$customername =  $req->input('customername');
			$billno =  $req->input('billno');
			$prevbilldt =  $req->input('prevbilldt');
			$receiptdt =  $req->input('receiptdt');
			$prevreading =  $req->input('prevreading');
			$currreading =  $req->input('currreading');
			$tarifftype =  $req->input('tarifftype');
			$tariffunit =  $req->input('tariffunit');
			$prevbal =  $req->input('prevbal');
			$currbill =  $req->input('currbill');
			$cmbmeterstatus =  $req->input('cmbmeterstatus');
			$meterreason =  $req->input('meterreason');		 
		
			$tim = date("H:i:s");
			$dttim= date("Y-m-d H:i:s");
			
			$data = array('cst_id'=>$custid,'usr_id'=>$custid,'tarf_id'=>$custid,'wtrb_rpttype'=>'Receipt','wtrb_billno'=>$billno,'wtrb_billdate'=>$receiptdt,'wtrb_billtime'=>$tim,'wtrb_prevbal'=>$prevbal,'wtrb_currpayable'=>$currbill,'wtrb_prevreading'=>$prevreading,'wtrb_currreading'=>$currreading,'wtrb_mtrstatus'=>$cmbmeterstatus,'wtrb_mtrreason'=>$meterreason,'wtrb_reentrydttim'=>$dttim,'wtrb_status'=>'Pending');
			$insertdetails = DB::table('wtrb_waterbill')->insert($data);
			if (!is_null($insertdetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Inserted Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Insertion.. Please Try again..')]);
			}
		}
		//Load agent records in the dropdownbox
		public function LoadAgent()
		{
			$status= 'Active';
			$usr_user = DB::table('usr_user')
			->select('usr_id','usr_name','usr_loginid')
			->where('usr_status','=',$status)
			->orderby('usr_name', 'asc')
			->get();
			return response()->json($usr_user);
		}
		
		public function ViewPendingReceipt()
		{
			$data['WBAllReport']=$this->WBReceiptModel->ViewAllPendingReceipt();
			return response()->json($data);
		}
		public function DeleteReceiptRecord(Request $request)
        {
			$id=$request->input('delid');
			$result = DB::table('wtrb_waterbill')-> where('wtrb_id',$id)-> update(['wtrb_status'=>'Deleted']);
			return $result;// response()->json(['status' => '1', 'message' => trans('Error in Insertion.. Please Try again..')]);
		}
		public function SaveReceiptRecord(Request $request)
        {
			$result = DB::table('wtrb_waterbill')-> where('wtrb_status','Pending')-> update(['wtrb_status'=>'Active']);
			return $result;// response()->json(['status' => '1', 'message' => trans('Error in Insertion.. Please Try again..')]);
		}
	}