<?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\CompanyModel;
	use Validator;
	
	class SalesCompanyController extends Controller
	{
		public function __construct()
		{
			$this->company= new CompanyModel;
		}
		public function SalesLoadCompany()
		{
			$data['url']='salesloadcompany';
		    $Mid=$this->company->QutationPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$biztype1 = 'Buyer';
			$biztype2 = 'Buyer & Seller';
			$status= 'Deleted';	
			$data['Company'] = DB::table('cmp_company')->where('cmp_status','<>',$status)->Where('cmp_type','=',$biztype1)->orwhere('cmp_type','=',$biztype2)->orderby('cmp_name', 'asc')->get();
			return view('salescompany',compact('data'));
		}
		
		public function SaleshelperCollection()
		{
			$newCollection = collect([1, 2, 3, 4, 5]);
			dd($newCollection);
		}
		
		public function SalesInsertCompany(Request $req)
		{
			$cmp_id =  $req->input('cmp_id');
			$companyname= $req->input('companyname');
			$companytype= $req->input('companytype');
			$description= $req->input('description');
			$status = $req->input('status');
			$data = array('cmp_type'=>$companytype,'cmp_name'=>$companyname,'cmp_description'=>$description,'cmp_status'=>$status);
			
			if($cmp_id == 0)
			{
				$insertdetails = DB::table('cmp_company')->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
			{
				$updatedetails = DB::table('cmp_company')->where('cmp_id',$cmp_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 SalesEditCompany(Request $request)
		{
			$id = $request->input('editId');
			$company = DB::table('cmp_company')->where('cmp_id',$id)->first();
			return view('companyedit',compact('company'));
		}
		public function SaleseditcompanyUpdate(Request $request)
		{
			$data['cmp_id'] =  $request->input('cmp_id');
			$data['companyname']= $request->input('companyname');
			$data['companytype']= $request->input('companytype');
			$data['description']= $request->input('description');
			$data['status'] = $request->input('status');
			return $id=$this->company->purchaseeditcompanyUpdate($data);
			
		}
		public function SalesDeleteCompany(Request $request)
		{
			$id = $request->input('id');
			
			return $UpdateDetails = DB::table('cmp_company')->where('cmp_id',$id)->update(['cmp_status'=>'Deleted']);
			
		}
	}
	
?>