<?php
	namespace App\Http\Model;
	use Illuminate\Database\Eloquent\Model;
	use DB;
	use Auth;
	class batchModel extends Model
	{		
		public function QPermission($id)
	 	{
		    $QModules=DB::table('modules')->select('Mid')->where('mUrl',$id['url'])->first();
			$Mid=$QModules->Mid;
			return $Qpermission=DB::table('permission')->where('Mid',$Mid)->first();
		}
		public function loadBatchView()
		{
			return $SalesView=DB::table('bch_batch')->select('bch_id','bch_batchcode','bch_dttim','bch_strchicks','bch_hatchdt','bch_disptime','bch_remark','bch_status','shd_name','usr_name','usr_loginid')
			->leftjoin('shd_shed','shd_shed.shd_id','=','bch_batch.shd_id')
			->leftjoin('usr_user','usr_user.usr_id','=','bch_batch.usr_id')
			->where('bch_batch.bch_status','<>','Deleted')
			->get();
		}		
		public function loadUsers()
		{
			return $sqlusr_user=DB::table('usr_user')->select('usr_id','usr_name','usr_loginid')
			->where('usr_user.usr_status','=','Active')
			->get();
		}
		//###Batch Info function starts here
		public function viewbatchinfotabBatchmodel($id)
		{
			$batchid=$id['batchid'];
			return $SalesView=DB::table('bch_batch')
			->select('bch_id','bch_batchcode','bch_dttim','bch_strchicks','bch_hatchdt','bch_disptime','bch_remark','bch_status','shd_name','usr_name','usr_loginid','bch_hatchery','itm_item.itm_id','itm_code','itm_name')
			->leftjoin('shd_shed','shd_shed.shd_id','=','bch_batch.shd_id')
			->leftjoin('usr_user','usr_user.usr_id','=','bch_batch.usr_id')
			->leftjoin('itm_item','itm_item.itm_id','=','bch_batch.itm_id')
			->where('bch_batch.bch_id','=',$batchid)
			->first();
		}
		//###Batch Info function ends here
		//Coding to calculate paid chicken
		public function viewbatchinfotabcountpaidchicken($id)
		{
			$batchid=$id['batchid'];
			$itemid=$id['batchrec']->itm_id;
			return $SalesView=DB::table('bit_billitemrecord')
			->select('bit_qty','bit_cost')
			->leftjoin('bil_bill','bil_bill.bil_id','=','bit_billitemrecord.bil_id')
			->where('bit_billitemrecord.itm_id','=',$itemid)
			->where('bil_bill.bil_type','=','Purchase')
			->where('bil_bill.bil_status','=','Active')
			->where('bil_bill.bch_id','=',$batchid)
			->where('bit_billitemrecord.bit_cost','!=',0)			
			->get();
		}
		//Coding to calculate paid chicken ends here
		//Coding to calculate free chicken
		public function viewbatchinfotabcountfreechicken($id)
		{
			$batchid=$id['batchid'];
			$itemid=$id['batchrec']->itm_id;
			return $SalesView=DB::table('bit_billitemrecord')
			->select('bit_qty','bit_cost')
			->leftjoin('bil_bill','bil_bill.bil_id','=','bit_billitemrecord.bil_id')
			->where('bit_billitemrecord.itm_id','=',$itemid)
			->where('bil_bill.bil_type','=','Purchase')
			->where('bil_bill.bil_status','=','Active')
			->where('bil_bill.bch_id','=',$batchid)
			->where('bit_billitemrecord.bit_cost','=',0)			
			->get();
		}
		//Coding to calculate free chicken ends here		
		//###Info
		public function viewmasterfeed($id)
		{
			$batchid=$id['batchid'];
			$totday=$id['totday'];
			$itmid=$id['batchrec']->itm_id;
			return $SalesView=DB::table('fed_masterfeed')
			->select('fed_id','itm_id','fed_day','fed_feed','fed_water','fed_medicine','fed_status')
			->where('fed_masterfeed.itm_id','=',$itmid)
			->where('fed_masterfeed.fed_day','=',$totday)
			->where('fed_masterfeed.fed_status','=','Active')			
			->first();
		}
		public function insertentry($id)
		{
		 	//###Entry table insertion code
			$Entry=DB::table('feed_entry')->insertGetId(['bch_id'=>$id['batchid'],'feed_dt'=>$id['entrydate'],'feed_mortality'=>$id['mortality'],'feed_feed'=>$id['feed'],'feed_water'=>$id['water'],'feed_medical'=>$id['medvac'],'feed_temp'=>$id['temperature'],'feed_humidity'=>$id['humidity'],'feed_observation'=>$id['observation'],'feed_suggestion'=>$id['suggestion'],'fed_id'=>$id['fed_id']]);
			//###Item insertion record		
			$weit_slno = $id['arrslno'];
			$weit_totbirds = $id['arrnobirds'];
			$weit_gm = $id['arrtotweight'];
					
			for($i=0;$i<count($weit_slno);$i++)
			{			
				$transaction=DB::table('weit_feedweight')->insertGetId(['feed_id'=>$Entry,'weit_slno'=>$weit_slno[$i],'weit_totbirds'=>$weit_totbirds[$i],'weit_gm'=>$weit_gm[$i],'weit_status'=>'Active']);
			}
			return $data=['status' => 'Success','entry_id' => $weit_gm[0]];
		}		
		public function viewbatchentryrecords($id)
		{
			$batchid=$id['batchid'];
			return $SalesView=DB::table('feed_entry')
			->select('feed_dt','feed_mortality','feed_feed','feed_water','feed_medical','feed_temp','feed_humidity','feed_observation','feed_suggestion',DB::raw("(SELECT sum(`weit_gm`) FROM `weit_feedweight` WHERE feed_id=feed_entry.feed_id) as totalweight"))
			->where('feed_entry.bch_id','=',$batchid)
			->get();
		}
		//Coding for info farmsheet tab
		public function viewfarmsheetrecords($id)
		{
			$batchid=$id['batchid'];
			return $SalesView=DB::table('feed_entry')
			->select('feed_dt','feed_mortality','feed_feed','feed_water','feed_medical','feed_temp','feed_humidity','feed_observation','feed_suggestion',DB::raw("(SELECT sum(`bit_qty`) FROM `bit_billitemrecord` left join bil_bill on bit_billitemrecord.bil_id=bil_bill.bil_id where bch_id='$batchid' AND bil_date=feed_dt AND bil_type='Sales') as totalsold"),DB::raw("(SELECT `bit_cost` FROM `bit_billitemrecord` left join bil_bill on bit_billitemrecord.bil_id=bil_bill.bil_id where bch_id='$batchid' AND bil_date=feed_dt AND bil_type='Sales') as bit_cost"))
			->where('feed_entry.bch_id','=',$batchid)
			->get();
		}
		//Coding for info farmsheet tab ends here
		public function viewInfoDailyComparisonmodel()
		{

		}			
		public function viewInfoPurchasemodel($id)
		{
			$batchid=$id['batchid'];
			return $SalesView=DB::table('bit_billitemrecord')
			->select('bil_date','itm_name','bit_qty','bit_cost')
			->leftjoin('bil_bill','bit_billitemrecord.bil_id','=','bil_bill.bil_id')
			->leftjoin('itm_item','itm_item.itm_id','=','bit_billitemrecord.itm_id')
			->where('bil_bill.bil_status','=','Active')
			->where('bil_bill.bil_type','=','Purchase')
			->where('bil_bill.bch_id','=',$batchid)
			->get();
		}			
		public function viewInfoSalesmodel($id)
		{
			$batchid=$id['batchid'];
			return $SalesView=DB::table('bit_billitemrecord')
			->select('bil_date','itm_name','bit_qty','bit_cost')
			->leftjoin('bil_bill','bit_billitemrecord.bil_id','=','bil_bill.bil_id')
			->leftjoin('itm_item','itm_item.itm_id','=','bit_billitemrecord.itm_id')
			->where('bil_bill.bil_status','=','Active')
			->where('bil_bill.bil_type','=','Sales')
			->where('bil_bill.bch_id','=',$batchid)
			->get();
		}			
		public function viewInfoPerformancemodel()
		{

		}			
		public function viewInfoStandardPerformancemodel()
		{

		}			
		public function viewInfoplmodel()
		{

		}	
		
	}