<?php
	namespace App\Http\Model;
	use Illuminate\Database\Eloquent\Model;
	use DB;
	use Auth;
	class PurchaseModel 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 QutationPermission($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 ShedNAmes()
		{
			$shd_shed=DB::table('shd_shed')->where('shd_status','=','Active')->get();
			return $shd_shed;
		}
		public function AddNewPurchase()
		{
			$OrderNo=DB::table('frm_firm')->first();
			$purchaseBillno1=$OrderNo->pur_bill_no;
			return $purchaseBillno=$purchaseBillno1+1;
		}
		public function PurchaseCompany()
		{
			return $PurchaseCompany=DB::table('cmp_company')->get();
		}
		public function AddSubPurchase($id)
		{
			$subCompanyId=$id['compId'];
			return $PurchaseCompany=DB::table('cst_customer')->select('cst_id','cst_name')
			->where('cmp_id',$subCompanyId)
			->where('cst_type','<>','Buyer')
			->where('cst_status','Active')
			->get();
		}
		public function itemCompany()
		{
			return $itemCompany=DB::table('itm_item')->select('itm_id','itm_name')
			->where('itm_status','Active')
			->get();
		}
		public function PurchaseBillitem($id)
		{
			$number=$id['num'];
			$bil_no1=$id['itemcode'];
			
			
			$bill=DB::table('bil_bill')->insertGetId(['bil_type'=>'0','bil_no'=>$id['itemcode'],'bil_date'=>$id['saleorderdate'],'cst_id'=>$id['itemcategory1'],'bil_note'=>$id['description'],'bil_status'=>'Active','BillStatus'=>'0','BillPaid'=>'0','Bill_amount'=>'0','taxId'=>'0','TaxAmount'=>'0']);
			
			for($i=1;$i<=$number;$i++)
			{
				$orderId=DB::table('bit_billitemrecord')->insertGetId(['bit_type'=>'0','bil_id'=>$bill,'cmp_id'=>$id['itemcategory'],'itm_id'=>$id['Schoolname'.$i],'bit_qty'=>$id['Major'.$i],'bit_cost'=>'00','bit_disctype'=>$id['Degree'.$i],'bit_discount'=>'0','bit_status'=>'Active']);
			}
			
			$transactionId=DB::table('trn_transaction')->insertGetId(['trn_type'=>'0','trn_code'=>$orderId,'trn_billno'=>$id['itemcode'],'cst_id'=>$id['itemcategory1'],'br_id'=>$id['itemcategory'],'trn_amt'=>'00','trn_date'=>date('Y-m-d'),'trn_note'=>'0','trn_paytype'=>'0','trn_paydetail'=>'Active','trn_status'=>'Active']);
			
			/****************UPDATE BILL NO************************/
			$pur_bill_no=DB::table('frm_firm')->select('pur_bill_no')->first();
			$pur_bill_no2=$pur_bill_no->pur_bill_no;
			$pur_bill_no1=$pur_bill_no2+1;
			
			DB::table('frm_firm')->update(['pur_bill_no'=>$pur_bill_no1]);
			
			/****************UPDATE BILL NO************************/
			return $bil_no1;
		}
		public function ViewPurchaseOrder()
		{
			return $ViewPurchaseOrder=DB::table('bil_bill')->select('bil_no','bil_date','cst_name','cmp_name','bil_status')
			->leftjoin('cst_customer','cst_customer.cst_id','=','bil_bill.cst_id')
			->leftjoin('cmp_company','cmp_company.cmp_id','=','cst_customer.cmp_id')
			->where('bil_bill.bil_status','Active')
			->where('bil_bill.BillStatus','0')
			->get();
		}
		
		public function printCustomerDetails($id)
		{
		    $billNo=$id['Billno'];
			return $ViewPurchaseOrder=DB::table('cst_customer')->select('cst_name','cst_address','cst_city','cst_state','cst_country','cst_contact1','cst_email','bil_date','cmp_name','bil_note')
			->leftjoin('bil_bill','bil_bill.cst_id','=','cst_customer.cst_id')
			->leftjoin('cmp_company','cmp_company.cmp_id','=','cst_customer.cmp_id')
			->where('bil_bill.bil_no','=',$billNo)
			->where('bil_bill.BillStatus','0')
			->first();
		}
		
		public function printBillItem($id)
		{
		    $billNo=$id['Billno'];
			return $ViewPurchaseOrder=DB::table('bil_bill')->select('itm_name','itm_purchasecost','itm_salescost','bit_disctype','bit_qty')
			->leftjoin('bit_billitemrecord','bit_billitemrecord.bil_id','=','bil_bill.bil_id')
			->leftjoin('itm_item','itm_item.itm_id','=','bit_billitemrecord.itm_id')
			//->leftjoin('cmp_company','cmp_company.cmp_id','=','cst_customer.cmp_id')
			->where('bil_bill.bil_no','=',$billNo)
			->where('bil_bill.BillStatus','0')			
			->get();
		}
		
		public function MaxprintCustomerDetails()
		{
		    
			$MaxBillno=DB::table('bil_bill')->MAX('bil_no');
			
			return $ViewPurchaseOrder=DB::table('cst_customer')->select('cst_name','cst_address','cst_city','cst_state','cst_country','cst_contact1','cst_email','bil_date','cmp_name')
			->leftjoin('bil_bill','bil_bill.cst_id','=','cst_customer.cst_id')
			->leftjoin('cmp_company','cmp_company.cmp_id','=','cst_customer.cmp_id')
			->where('bil_bill.bil_no','=',$MaxBillno)
			->first();
		}
		
		public function MaxprintBillItem()
		{
		    $MaxBillno=DB::table('bil_bill')->MAX('bil_no');
			
			return $ViewPurchaseOrder=DB::table('bil_bill')->select('itm_name','itm_purchasecost','itm_salescost','bit_disctype','bit_qty')
			->leftjoin('bit_billitemrecord','bit_billitemrecord.bil_id','=','bil_bill.bil_no')
			->leftjoin('itm_item','itm_item.itm_id','=','bit_billitemrecord.itm_id')
			//->leftjoin('cmp_company','cmp_company.cmp_id','=','cst_customer.cmp_id')
			->where('bil_bill.bil_no','=',$MaxBillno)
			->get();
		}
		public function CompanyData()
		{
            return $ViewPurchaseOrder=DB::table('frm_firm')->first();
		}
	}
	
