<?php
defined('BASEPATH') OR exit('No direct script access allowed');
defined('DIR_SEPARATOR') || define('DIR_SEPARATOR', '/');

class Checkoutmain extends CI_Model {
	
	public function __construct() { parent::__construct(); }
	
	public function getProject($projectID){
    	$query = $this->db->select('*')
						  ->where('ID', $projectID)
                          ->where('is_delete', 0)
                          ->get('project');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result())[0]:null;
  	}

	  public function getProjectsbyUser($userID){
    	$query = $this->db->select('*')
                          ->where('user', $userID)
                          ->where('is_delete', 0)
                          ->get('project a');

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

	public function getPaymentStatus($projectID){
		if($this->getProjectBills($projectID) != null){
			$query = $this->db->select('paid')
                          	  ->where('ID', $projectID)
                          	  ->get('project');

        	return ($this->ObjToArray($query->result())[0]['paid'] == 1)?true:false;
		} else {
			return false;
		}
	}

	  public function getVotersbyProject($projectID){
		$query = $this->db->select('*')
                          ->where('projectID', $projectID)
                          ->get('user');

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

	  public function getVotersbyPaymentStatus($projectID, $paid){
		$query = $this->db->select('*')
                          ->where('projectID', $projectID)
                          ->where('paid', $paid)
                          ->get('user');

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

	  public function getUser($userID){
		$query = $this->db->select('*')
                          ->where('fldid', $userID)
                          ->get('tbl_usermaster');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result())[0]:null;
	  }

	  public function getUserBillingInfo($userID){
		$query = $this->db->select('*')
                          ->where('user', $userID)
                          ->get('user_billing_info');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result())[0]:null;
	  }

	  function getProjectBills($projectID){
		  $query = $this->db->select('*')
                          ->where('ID', $projectID)
                          ->get('user_billing_detail');

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

	  public function getVoters($projectID){
		$query = $this->db->select('COUNT(b.userID) as totalVoters')
						  ->join('user b', 'b.projectID = a.ID')
						  ->where('a.ID', $projectID)
                          ->where('a.is_delete', 0)
                          ->get('project a');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result())[0]:null;
	  }

	  public function getPaidProjectSize($projectID){
		$query = $this->db->select_sum('totalVoters')
                          ->where('projectID', $projectID)
                          ->get('user_billing_detail');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result())[0]:null;
	  }

	  function getPaymentConfig(){
		$query = $this->db->select('*')
						  ->order_by("level", "asc")
                          ->get('payment_setup');

        return ($query->num_rows() > 0)?$this->ObjToArray($query->result()):null;
	}
	
	/**
	* ADDED BY ZUL ZOLKAFFLY
	*/
	public function getBillDetail($billID) {
		$sql = "SELECT
							user_billing_detail.billID, user_billing_detail.userID, user_billing_detail.projectID,
							project.title, project.ID projectID, project.createdTS,
							project.paid, 
							tbl_usermaster.username,
							user_billing_info.phoneNo,
							user_billing_info.faxNo,
							user_billing_info.email,
							user_billing_info.address,
							user_billing_info.city,
							user_billing_info.postcode,
							(SELECT fldname FROM _ref_state WHERE _ref_state.fldid = user_billing_info.state) stateName,
							(SELECT fldcountry FROM _ref_country WHERE _ref_country.fldid = user_billing_info.country) countryName,
							(SELECT COUNT(1) FROM user WHERE user.projectID = user_billing_detail.projectID) totalVoter
						FROM tbl_usermaster, project, user_billing_detail, user_billing_info
						WHERE tbl_usermaster.fldid = user_billing_detail.userID
						AND project.ID = user_billing_detail.projectID
						AND tbl_usermaster.fldid = user_billing_info.user
						AND billID = '".$billID."'";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0]) ? $this->ObjToArray($r[0]) : NULL;
	}
	
	public function getUserDetail($user) {
	}
	
	
	
	public function ObjToArray($item) { return json_decode(json_encode($item),true); }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
