<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ZakatGroup extends CI_Model{
	
	public function group_all(){
		$this->db->select('a.*');
		$this->db->select('b.name as parentName');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->order_by('a.companyID');
		$this->db->from('zakat_group a');
		$this->db->join('zakat_group b', 'b.ID = a.parentID','left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function ZakatGroup_Insert(){
		$newData = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'name' => $this->input->post('name'),
			'description' => $this->input->post('description'),
			'parentID' => $this->input->post('parentID'),
			'sequence' => $this->input->post('sequence'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query = $this->db->insert('zakat_group', $newData);
		if($query){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	
	public function wakaf_Insert(){
		$newData = array(
			'ID' => $this->input->post('ID'),
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'waqif' => $this->input->post('waqif'),
			'category' => $this->input->post('category'),
			'beneficiariesID' => $this->input->post('beneficiariesID'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query = $this->db->insert('wakaf_master', $newData);
		$wakafID = $this->db->insert_id();
		$newData2 = array(
			'wakafID' => $wakafID,
                        'accountID' => $this->input->post('chargeAccount'),
			'contributionPercentage' => $this->input->post('contributionPercentageLumpSum'),
			'amount' => $this->input->post('amountLumpSum'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query2 = $this->db->insert('wakaf_details_allocation', $newData2);
//insert benefeciaries
		  foreach ($this->input->post('contribution') as $row => $val){
			  if ($val > 0) {
				$newData3 = array(
					'wakafID' => $wakafID,
					'beneficiariesID' => $row,
					'contributionPercentage' => $val,
					'description' => ($row == 6)?$this->input->post('descriptionDistribution'):'',
					'createdBy' => element('role', $this->session->userdata('logged_in'))
				);
				$query3 = $this->db->insert('wakaf_details_distribution', $newData3);
				echo '<br>'.$this->db->last_query();
			  }
		  }
		if($query && $query2 && $query3){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	
	public function wakafBeneficiaries_all(){
		$this->db->select('a.ID');
		$this->db->select('a.name');
		$this->db->order_by('a.ID', 'asc');
		$this->db->from('wakaf_beneficiaries a');
		$query = $this->db->get();
//		echo var_dump($query);
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function wakafBeneficiaries_ID($ID){
		$this->db->select('a.ID');
		$this->db->select('a.name');
		$this->db->select('b.contributionPercentage as contributionPercentageDistribution');
		$this->db->select('b.description');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('a.ID', $ID);
		$this->db->order_by('a.ID', 'asc');
		$this->db->from('wakaf_master a');
		$this->db->join('wakaf_details_distribution b', 'b.wakafID = a.ID', 'left outer');
		$this->db->join('wakaf_beneficiaries c', 'b.ID = a.beneficiariesID', 'left outer');
		$query = $this->db->get();
//		echo var_dump($query);
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function wakafListDate($dateFrom, $dateTo){
		$this->db->select('wakaf_center.name');
		$this->db->select('wakaf.ID');
		$this->db->select('wakaf_details_allocation.saleAmount');
		//$this->db->select('wakaf.amountAllocatedPercent');
		$this->db->select('wakaf.amountAllocated');
		$this->db->select('date_format(wakaf.createdTS, "%d-%m-%Y") as datewakaf', false);
		$this->db->where('wakaf.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('wakaf.createdTS between  "'.$dateFrom.'" and "'.$dateTo.'"');
		//$this->db->select('date_format(comp_receive_payment.receivePaymentDate, "%d-%m-%Y") as recpaydate', false);
		$this->db->from('wakaf');
		$this->db->join('wakaf_center', 'wakaf_center.ID = wakaf.wakafCenterID');
		$this->db->join('wakaf_details_allocation', 'wakaf_details_allocation.wakafID = wakaf.ID');
		$this->db->order_by('wakaf.createdTS desc');
		$query = $this->db->get();
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function ZakatGroup_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('zakat_group', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
        
        
	public function ZakatGroup_Delete($ID){
		$this->db->where('ID', $ID);
		$query = $this->db->delete('zakat_group'); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
        
	public function ZakatGroup_ID($ID){
		$this->db->where('ID', $ID);
		$this->db->from('zakat_group');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function ZakatGroup_search($where){
		$this->db->where($where);
		$this->db->from('zakat_group');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
// zakat details

	public function ZakatNisabDetails_GroupID($ID){
		$this->db->select('a.*');
		$this->db->select('b.acctCode');
		$this->db->select('b.acctName');
		$this->db->where('groupID', $ID);
		$this->db->from('zakat_group_details a');
		$this->db->join('comp_chart_of_acct b', 'b.ID = a.acctID','left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function ZakatNisabDetails_Insert(){
		$newData = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'groupID' => $this->input->post('parentID'),
			'acctID' => $this->input->post('acctID'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query = $this->db->insert('zakat_group_details', $newData);
		if($query){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	public function ZakatNisabDetails_Delete($ID){
		$this->db->where('ID', $ID);
		$query = $this->db->delete('zakat_group_details'); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	public function ZakatNisabDetails_ID($ID){
		$this->db->select('a.*');
		$this->db->select('b.acctCode');
		$this->db->select('b.acctName');
		$this->db->where('a.ID', $ID);
		$this->db->from('zakat_group_details a');
		$this->db->join('comp_chart_of_acct b', 'b.ID = a.acctID','left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
        
        public function insert($tname, $newData){
		$sql = $this->db->insert_string($tname, $newData);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			//$last_query = $this->db->last_query();
			//return $last_query;
			return 0;
		}
	}
        
        public function getWakafMaster($ID){
		$this->db->select('*');
		$this->db->from('wakaf');
                $this->db->where('ID', $ID);
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
        
        public function getWakafDetail($ID){
		
                $sql='SELECT a.*, COALESCE(b.name, "Others", b.name ) as benef
			FROM wakaf_details_distribution a LEFT OUTER JOIN wakaf_beneficiaries b ON b.ID = a.beneficiariesID
			where wakafID ='.$ID;
                $query = $this->db->query($sql);

		if($query->num_rows() >=1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
        
        public function getWakafSaleDetails($ID){
            $sql='SELECT a.*, CONCAT("[",b.acctCode,"] ", b.acctName) as acct
			FROM wakaf_details_allocation a LEFT OUTER JOIN comp_chart_of_acct b ON b.ID = a.accountID
			where wakafID ='.$ID;
                $query = $this->db->query($sql);

		if($query->num_rows() >=1){
			return $query->result();
		}else{
			return FALSE;
		}
        }


        public function ZakatGroup_delByTname($tname,$ID){
		$this->db->where('ID', $ID);
		$query = $this->db->delete($tname); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
        
        
        public function ZakatGroup_UpdateByTname($tname, $ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update($tname, $data); 
		return $query;
	}
        
         public function ZakatGroup_UpdateWakafDetail($masterId, $wakafAmnt){
             
             $sql="Update wakaf_details_distribution set amount = contributionPercentage*$wakafAmnt/100 where wakafID =".$masterId;
                $query = $this->db->query($sql);

		return $query;
	}
        
}

/* End of file ZakatGroup.php */
/* Location: ./application/models/ZakatGroup.php */
/* Created By : Yahaya Abdollah */
