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

/**
* MODEL FOR REVIEW CHILDREN
* @author : Zul Zolkaffly
* @last-update : Zul Zolkaffly (15/08/2016)
*/
class Children extends CI_Model {
	protected $table = 'company_shareholder_old';

	public function setTable($table) {
		$this->table = $table;
		return $this;
	}

	public function getAll($company, $where = array()) {
		$this->db->where('companyID', $company)->where('is_delete',0);
		if($where) {
			foreach($where as $key=>$value) {
				$this->db->where($key, $value);
			}
		}
		$query = $this->db->get($this->table);

		if ($query && $query->num_rows() >= 1) {
			$rs = $query->result();
			return ($rs) ? $rs : NULL;
		} else {
			return NULL;
		}
	}

	public function getRecord($id) {
		$this->db->where('ID', $id);
		$query = $this->db->get($this->table);

		if ($query && $query->num_rows() >= 1) {
			$rs = $query->result();
			return ($rs[0]) ? $rs[0] : NULL;
		} else {
			return NULL;
		}
	}

	public function deleteRecord($id) {
		$q = $this->db->where('ID',$id)->update($this->table, array('is_delete'=>1));

		return ($q === TRUE) ? TRUE : FALSE;
	}

	public function updateRecord($id, $company, $data) {
		$q = $this->db->select(array('1'),false)->where('ID',$id)->where('companyID', $company)->get($this->table);
		if ($q && $q->num_rows() >= 1) {
			$q = $this->db->where('ID',$id)->where('companyID', $company)->update($this->table, $data);
		} else {
			unset($data['ID']);
			$data['companyID'] = $company;
			$q = $this->db->insert($this->table, $data);
		}

		return ($q === TRUE) ? TRUE : FALSE;
	}

	public function updateAll($company, $data) {
		$q = $this->db->where('companyID', $company)->update($this->table, $data);

		return ($q === TRUE) ? TRUE : FALSE;
	}

	public function getCompanyName($companyID){
		$query = $this->db->select("CONCAT(companyName, ' (', companyNo, ')') AS companyInfo", false)
						  ->where('ID', $companyID)
						  ->get('company_main');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getCompanyPhone($companyID){
		$query = $this->db->select('phoneNo')
						  ->where('ID', $companyID)
						  ->get('company_main');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getCompanyAddress($companyID){
		$query = $this->db->select('b.address, b.city, b.postCode')
						  ->select('c.fldname as stateName')
						  ->join('company_address b', 'b.companyID = a.ID')
						  ->join('_ref_state c', 'c.fldid = b.stateID')
						  ->where('a.ID', $companyID)
						  ->get('company_main a', 1);

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getCompanyBranch($companyID){
		$query = $this->db->select('a.address, a.city, a.postCode')
						  ->select('b.fldname as stateName')
						  ->join('_ref_state b', 'b.fldid = a.stateID')
						  ->where('a.companyID', $companyID)
						  ->where('is_delete', 0)
						  ->get('company_branch a');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReviewMethod(){
		$query = $this->db->select("CONCAT(methodCode, '. ', methodDesc) as methodName", false)
						  ->select("methodCode, methodDesc")
						  ->get('_ref_method');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReview($methodID, $companyID){
		$query = $this->db->query("SELECT `a`.`id`, `a`.`text`, `b`.`mark`, `b`.`comment` FROM (`_ref_review` a) LEFT OUTER JOIN (`review` b) ON `b`.`ref_reviewID` = `a`.`ID` AND `b`.`companyID` = $companyID WHERE `a`.`code` REGEXP '^-?[0-9]+$' AND `a`.`methodID` = $methodID AND `a`.`is_delete` = 0 ORDER BY `a`.`ID`");

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReviewPrintHeader($reviewID){
		$query = $this->db->select("CONCAT(b.methodCode, ' - ', b.methodDesc)",false)
						  ->select("CONCAT(b.methodCode, '-', a.code)",false)
						  ->select('a.objective, a.text')
						  ->join('_ref_method b', 'b.methodID = a.methodID')
						  ->where('a.ID', $reviewID)
					  	  ->get('_ref_review a');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReviewChildrenPrintHeader($reviewID){
		$query = $this->db->select('text')
						  ->where('parentID', $reviewID)
						  ->order_by('id', 'ASC')
					  	  ->get('_ref_review');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReviewPrintBody($reviewID){
		$query = $this->db->select("CONCAT(c.methodCode, '-', a.code) as bil",false)
						  ->select('a.methodID, b.text')
					  	  ->join('_ref_review_detail b', 'b.reviewID = a.ID')
						  ->join('_ref_method c', 'a.methodID = c.methodID')
						  ->where('reviewID', $reviewID)
						  ->get('_ref_review a');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getReviewChildrenPrintBody($reviewID){
		$query = $this->db->select("CONCAT('A-2', '-', b.code) as bil",false)
						  ->select('a.id ,b.methodID, a.text')
						  ->join('_ref_review b', 'b.ID = a.reviewID')
						  ->where('a.reviewID', $reviewID)
						  ->get('_ref_review_detail a');

		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function getAnswer($reviewID) {

	}
}
