<?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\itemModel;
	use Validator;
	
	class salesItemController extends Controller
	{
		public function __construct()
		{
			$this->item= new itemModel;
		}
		public function salesLoadviewitem()
		{
			$data['url']='salesviewitem';
		    $Mid=$this->item->QPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$data['Item'] =$this->item->salesLoadviewitem();
			return view('salesitemview',compact('data'));
		}
		public function salesLoadItem()
		{
			return view('salesitem');
		}
		
		public function salesLoadUnit()
		{
			$status= 'Deleted';
			$unit = DB::select( DB::raw("SELECT * FROM `unt_unit` where unt_status != '$status'") );
			return response()->json($unit);
		}
		public function salesLoadCategory()
		{
			$status= 'Deleted';
			$cat = DB::select( DB::raw("SELECT * FROM `cat_category` where cat_status != '$status' and maincat_id=0") );
			return response()->json($cat);
		}
		
		public function salesLoadSubCategory(Request $req)
		{
			$status= 'Deleted';
			$catid= $req->input('catid');
			$subcat = DB::select( DB::raw("SELECT * FROM `cat_category` where cat_status != '$status' and maincat_id=$catid") );
			return response()->json($subcat);
		}
		
		public function salesValidateItemCode(Request $req)
		{
			$code= $req->input('code');
			$item = DB::select( DB::raw("SELECT * FROM `itm_item` where itm_code = '$code'") );
			return response()->json($item);
		}
		
		public function salesInsertItem(Request $req)
		{
			$itm_id = $req->input('itm_id');
			$cat= $req->input('catid');
			$subcat= $req->input('subcatid');
			$unit= $req->input('unit');
			$itemcode = $req->input('itemcode');
			$itemname= $req->input('itemname');
			$itemtype= $req->input('itemtype');
			$purchasecost= $req->input('purchasecost');
			$salescost= $req->input('salescost');
			if($req->input('description') == '')
			{
				$description= ''; 
			}
			else
			{
				$description= $req->input('description'); 
			}
			$status = $req->input('status');
			
			$data = array('cat_id'=>$subcat,'unt_id'=>$unit,'itm_type'=>$itemtype,'itm_code'=>$itemcode,'itm_name'=>$itemname,'itm_purchasecost'=>$purchasecost,'itm_salescost'=>$salescost,'itm_note'=>$description,'itm_status'=>$status);
			
			if($itm_id == 0)
			{
				$insertdetails = DB::table('itm_item')->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('itm_item')->where('itm_id',$itm_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 salesEditItem($id)
		{  
			$data = DB::select( DB::raw("SELECT i.*,c.cat_id as subcategory,sc.cat_id as category FROM `itm_item` as i inner join cat_category as c on i.cat_id=c.cat_id inner join cat_category as sc on c.maincat_id=sc.cat_id where itm_id=$id") );
			return view('salesitem',compact('data'));
		}
		
		public function salesDeleteItem(Request $req)
		{
			$id = $req->id;
			$status= 'Deleted';
			$data = array('itm_status'=>$status);
			
			$UpdateDetails = DB::table('itm_item')->where('itm_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..')]);
			}
		}
	}
