<?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 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;
		}
	}
}

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