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

class ZakatCalculate extends CI_Model{
	
	public function method1(){
		$this->db->select('a.*');
		$this->db->select('b.parentID');
		$this->db->select('b.name as groupName');
		$this->db->select('b.description as description');
		$this->db->select('c.name as topGroupName');
		$this->db->select('sum(d.total) as totalAmount');
		$this->db->select('e.acctName');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->group_by('a.acctID');
		$this->db->from('zakat_group_details a');
		$this->db->join('zakat_group b', 'b.ID = a.groupID','left outer');
		$this->db->join('zakat_group c', 'c.ID = b.parentID','left outer');
		$this->db->join('comp_acc_det d', 'd.acctID = a.acctID and d.companyID = a.companyID and d.year <='.$this->input->post('nisabYear'),'left outer');
		$this->db->join('comp_chart_of_acct e', 'e.ID = a.acctID','left outer');
		$query = $this->db->get();
//			$this->db->flush_cache();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function method1_1(){
		$this->db->select('a.*');
		$this->db->select('b.parentID');
		$this->db->select('b.name as groupName');
		$this->db->select('b.description as description');
		$this->db->select('c.name as topGroupName');
		$this->db->select('sum(d.total) as totalAmount');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->group_by('b.parentID');
		$this->db->from('zakat_group_details a');
		$this->db->join('zakat_group b', 'b.ID = a.groupID','left outer');
		$this->db->join('zakat_group c', 'c.ID = b.parentID','left outer');
		$this->db->join('comp_acc_det d', 'd.acctID = a.acctID and d.companyID = a.companyID and d.year <='.$this->input->post('nisabYear'),'left outer');
		$query = $this->db->get();
//			$this->db->flush_cache();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function method1_2(){
		$this->db->select('a.*');
		$this->db->select('b.parentID');
		$this->db->select('b.name as groupName');
		$this->db->select('b.description as description');
		$this->db->select('c.name as topGroupName');
		$this->db->select('sum(d.total) as totalAmount');
		$this->db->select('e.muslimShareholder');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->group_by('c.parentID');
		$this->db->from('zakat_group_details a');
		$this->db->join('zakat_group b', 'b.ID = a.groupID','left outer');
		$this->db->join('zakat_group c', 'c.ID = b.parentID','left outer');
		$this->db->join('comp_acc_det d', 'd.acctID = a.acctID and d.companyID = a.companyID and d.year <='.$this->input->post('nisabYear'),'left outer');
		$this->db->join('company e', 'e.ID = a.companyID','left outer');
		$query = $this->db->get();
		if($query->num_rows() == 1){
			$tempData = $query->result();
			$this->db->flush_cache();
			$this->db->select('a.nisab');
			$this->db->flush_cache();
			$this->db->where('companyID',element('compID', $this->session->userdata('logged_in')));
			$this->db->where('centerID',$this->input->post('centerID'));
			$this->db->where('year',$this->input->post('nisabYear'));
			$this->db->from('zakat_nisab a');
			$queryNisab = $this->db->get();
			if($queryNisab->num_rows() >= 1){
				$tempData2 = $queryNisab->result();
//			echo var_dump($this->db->last_query());
				if($tempData[0]->totalAmount >= $tempData2[0]->nisab){
					$ZakatAmount = ($tempData[0]->muslimShareholder / 100) * $tempData[0]->totalAmount * 0.025;
				}else{
					$ZakatAmount = 0;
				}
			}else{
					$ZakatAmount = ($tempData[0]->muslimShareholder / 100) * $tempData[0]->totalAmount * 0.025;
			}
			return $ZakatAmount;
		}else{
			return FALSE;
		}
	}
	
}

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