<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * function list index
 * need to list the details and function called
 *



function point_entry($orderdata)



 * end function list index
 **/
class Test2_model extends CI_Model {
	public function __construct() {
		parent::__construct();
		$this->load->model('Homes', 'homes_model');
	}
	public function getAmountToPoint($amount, $introducelevel = 0) {
		$point = 0;
		$noOfCredit = floor($amount / 100);
		if ($introducelevel == 0) {
//customer
			$point = $noOfCredit * 3;
		}
		if ($introducelevel == 1) {
//first level referer
			$point = $noOfCredit * 2;
		}
		if ($introducelevel == 2) {
//second level referer
			$point = $noOfCredit * 1;
		}
		return $point;
	}
	public function point_entry($orderdata) {
		// $orderdata = [
		// 			'pay_method' => 3,
		// 			'used_id' => 5,
		// 			// 'customer_id' => $_GET['customer_id'],
		// 			'customer_id' => $customer_id,
		// 			// 'order_id' => $_GET['order_id'],
		// 			'order_id' => $order_id,
		// 			// 'trans_id' => $_GET['trans_id'],
		// 			// 'trans_id' => $trans_id,
		// 			'trans_id' => "",
		// 			// 'amount' => $_GET['amt'],
		// 			'amount' => $total_amount,
		// 			// 'store_amount' => $_GET['amt'],
		// 			'coupon_amnt' => $coupon_amnt,
		// 			// 'status' => $_GET['st'],
		// 			'status' => 0,
		// 			'trans_date' => date('Y-m-d'),
		// 			// 'currency' => 'USD',
		// 			'currency' => 'HKD',
		// 		];
		$this->db->trans_begin();
		$introduceList = $this->getIntroduceList($orderdata['customer_id']);
		$amount = $this->getAmountToPoint($total_amount);
		$data = array(
			'customer_id' => $orderdata['customer_id'],
			'order_id' => $orderdata['order_id'],
			'amount' => $amount,
			'date' => date('Y-m-d'),
		);
		if ($referer_level == 0) {
			$data['referer'] = 0;
		} else {
			//referer_level !=0 is referer
			$data['referer'] = 1;
		}

		$query = $this->db->insert('bemployee', $data);

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	} //public function point_entry($orderdata)
	public function getIntroduceList($customer_id) {
		$interducerlist = array();

		//customer
		$introduce = array();
		$introduce['customer_id'] = $customer_id;
		$introduce['introducelevel'] = 0;
		array_push($interducerlist, $introduce);
		//
		$introducer_email = "";
		$introducer2_email = "";

		//introducelevel 1
		$this->db->select("*");
		$this->db->from('customer_information');
		$this->db->where('customer_id', $customer_id);
		$result = $this->db->get()->row();
		$introducer_email = $result->introducer_email;
		if (!empty($introducer_email)) {
			$customer = $this->homes_model->check_customer($introducer_email);
			if (!empty($customer)) {
				$introduce = array();
				$introduce['customer_id'] = $customer->customer_id;
				$introduce['introducelevel'] = 1;
				array_push($interducerlist, $introduce);
				$introducer2_email = $customer->customer_id;
			}
		}
		//end introducelevel 1

		//introducelevel 2
		if (!empty($introducer2_email)) {
			$customer = $this->homes_model->check_customer($introducer_email);
			if (!empty($customer)) {
				$introduce = array();
				$introduce['customer_id'] = $customer->customer_id;
				$introduce['introducelevel'] = 2;
				array_push($interducerlist, $introduce);
			}
		}
		//end introducelevel 2

		return $introduce;
	}

}
?>