<?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 Custpoint_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']);
		$countRefer = 0;
		foreach ($introduceList as $person) {
			# code...

			$amount = $this->getAmountToPoint($orderdata['amount'], $person['introducelevel']);
			$data = array(
				'customer_id' => $person['customer_id'],
				'order_id' => $orderdata['order_id'],
				'amount' => $amount,
				'date' => date('Y-m-d'),
			);
			if ($person['introducelevel'] == 0) {
				// $data['referer'] = 0;
				$data['referer'] = $person['introducelevel'];
			} else {
				//referer_level !=0 is referer
				// $data['referer'] = 1 / 2;
				$data['referer'] = $person['introducelevel'];
				$data['referer_id'] = $orderdata['customer_id'];
			}

			$query = $this->db->insert('customer_point', $data);
			$countRefer = $countRefer + 1;
		} //foreach ($introduceList as $person) {

		// 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->introducer_email;
			}
		}
		//end introducelevel 1

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

		return $interducerlist;
	}

	public function getUserPoint($customer_id, $withinPeriod = 'no') {

		$userPoint = 0;
		$userUsedPoint = 0;

		$this->db->select('SUM(amount) as total');
		$this->db->from('customer_point');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('delete_time', NULL);
		if ($withinPeriod == 'yes') {
			//for searching userpoint within period
			$today = date("Y-m-d");
			$startdate = $this->getfunAvailableStartDate($today);
			$lastdate = $this->getfunAvailableLastDate($today);
			$this->db->where('date >=', $startdate);
			$this->db->where('date <=', $lastdate);

		}
		$result = $this->db->get()->row();
		if (!empty($result)) {
			$userPoint = $result->total;
		}

		//get User used point
		//actual value is the used point
		$this->db->select('SUM(actual_value) as total');
		$this->db->from('customer_usedpoint');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('delete_time', NULL);
		if ($withinPeriod == 'yes') {
			//for searching userpoint within period
			$today = date("Y-m-d");
			$startdate = $this->getfunAvailableStartDate($today);
			$lastdate = $this->getfunAvailableLastDate($today);
			$this->db->where('date >=', $startdate);
			$this->db->where('date <=', $lastdate);

		}
		$result = $this->db->get()->row();
		if (!empty($result)) {
			$userUsedPoint = $result->total;
		}
		//end get User used point
		$userPoint = $userPoint - $userUsedPoint;

		return $userPoint;
		// return $this->db->last_query();

	}

	public function pointToPaymentDollar($point) {
		$dollar = 0;
		if (!empty($point) && $point >= 0) {
			$dollar = $point * 1;
		}
		return $dollar;
	}
	public function paymentUpdatePoint($order_id) {
		$this->db->trans_begin();
		$this->db->set('confirmed', 1);
		$this->db->where('order_id', $order_id);
		$this->db->update('customer_point');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
	public function paymentResetPoint($order_id) {
		$this->db->trans_begin();
		$this->db->set('confirmed', 0);
		$this->db->where('order_id', $order_id);
		$this->db->update('customer_point');
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
	public function getCustomerPointRecord($customer_id, $withinPeriod = 'no') {

		$this->db->select('*');
		$this->db->from('customer_point');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('delete_time', NULL);
		if ($withinPeriod == 'yes') {
			//for searching userpoint within period
			$today = date("Y-m-d");
			$startdate = $this->getfunAvailableStartDate($today);
			$lastdate = $this->getfunAvailableLastDate($today);
			$this->db->where('date >=', $startdate);
			$this->db->where('date <=', $lastdate);

		}
		$query = $this->db->get();
		return $query->result();

	}
	public function getCustomerUsedPointRecord($customer_id, $withinPeriod = 'no') {

		$this->db->select('*');
		$this->db->from('customer_usedpoint');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('delete_time', NULL);
		if ($withinPeriod == 'yes') {
			//for searching userpoint within period
			$today = date("Y-m-d");
			$startdate = $this->getfunAvailableStartDate($today);
			$lastdate = $this->getfunAvailableLastDate($today);
			$this->db->where('date >=', $startdate);
			$this->db->where('date <=', $lastdate);

		}
		$query = $this->db->get();
		return $query->result();

	}
	public function getAvailableDate($date) {
		// $date = "2021-11-23";
		$inputDate = date("Y-m-d", strtotime($date));
		// $inputDateTimeStamp = strtotime($date);
		$availableDate = "";
		$dt = date("Y-01-01");
		$firstJan = date("Y-m-01", strtotime($dt));
		// $lastJan = date("Y-m-t", strtotime($dt));
		$dt = date("Y-03-01");
		// $firstMar = date("Y-m-01", strtotime($dt));
		$lastMar = date("Y-m-t", strtotime($dt));
		// $lastMarTimeStamp = strtotime($lastMar);
		$dt = date("Y-04-01");
		$firstApr = date("Y-m-01", strtotime($dt));
		// $firstAprTimeStamp = strtotime($firstApr);
		$dt = date("Y-06-01");
		$lastJun = date("Y-m-t", strtotime($dt));
		// $lastJunTimeStamp = strtotime($lastJun);
		$dt = date("Y-07-01");
		$firstJul = date("Y-m-01", strtotime($dt));
		// $firstJulTimeStamp = strtotime($firstJul);
		$dt = date("Y-09-01");
		$lastSep = date("Y-m-t", strtotime($dt));
		// $lastSepTimeStamp = strtotime($lastSep);
		$dt = date("Y-10-01");
		$firstOct = date("Y-m-01", strtotime($dt));
		$dt = date("Y-12-01");
		$lastDec = date("Y-m-t", strtotime($dt));

		if ($inputDate >= $firstJan && $inputDate <= $lastMar) {
			$availableDate = $lastMar;
		}
		if ($inputDate >= $firstApr && $inputDate <= $lastJun) {
			$availableDate = $lastJun;
		}
		if ($inputDate >= $firstJul && $inputDate <= $lastSep) {
			$availableDate = $lastSep;
		}
		if ($inputDate >= $firstOct && $inputDate <= $lastDec) {
			$availableDate = $lastDec;
		}
		return $availableDate;

	}
	public function getfunAvailableStartDate($date) {
		$inputDate = date("Y-m-d", strtotime($date));
		$startDate = "";
		$dt = date("Y-01-01");
		$firstJan = date("Y-m-01", strtotime($dt));
		// $lastJan = date("Y-m-t", strtotime($dt));
		$dt = date("Y-03-01");
		// $firstMar = date("Y-m-01", strtotime($dt));
		$lastMar = date("Y-m-t", strtotime($dt));
		// $lastMarTimeStamp = strtotime($lastMar);
		$dt = date("Y-04-01");
		$firstApr = date("Y-m-01", strtotime($dt));
		// $firstAprTimeStamp = strtotime($firstApr);
		$dt = date("Y-06-01");
		$lastJun = date("Y-m-t", strtotime($dt));
		// $lastJunTimeStamp = strtotime($lastJun);
		$dt = date("Y-07-01");
		$firstJul = date("Y-m-01", strtotime($dt));
		// $firstJulTimeStamp = strtotime($firstJul);
		$dt = date("Y-09-01");
		$lastSep = date("Y-m-t", strtotime($dt));
		// $lastSepTimeStamp = strtotime($lastSep);
		$dt = date("Y-10-01");
		$firstOct = date("Y-m-01", strtotime($dt));
		$dt = date("Y-12-01");
		$lastDec = date("Y-m-t", strtotime($dt));

		if ($inputDate >= $firstJan && $inputDate <= $lastMar) {
			$startDate = $firstJan;
		}
		if ($inputDate >= $firstApr && $inputDate <= $lastJun) {
			$startDate = $firstApr;
		}
		if ($inputDate >= $firstJul && $inputDate <= $lastSep) {
			$startDate = $firstJul;
		}
		if ($inputDate >= $firstOct && $inputDate <= $lastDec) {
			$startDate = $firstOct;
		}
		return $startDate;
	}
	public function getfunAvailableLastDate($date) {
		$inputDate = date("Y-m-d", strtotime($date));
		$lastDate = "";
		$dt = date("Y-01-01");
		$firstJan = date("Y-m-01", strtotime($dt));
		// $lastJan = date("Y-m-t", strtotime($dt));
		$dt = date("Y-03-01");
		// $firstMar = date("Y-m-01", strtotime($dt));
		$lastMar = date("Y-m-t", strtotime($dt));
		// $lastMarTimeStamp = strtotime($lastMar);
		$dt = date("Y-04-01");
		$firstApr = date("Y-m-01", strtotime($dt));
		// $firstAprTimeStamp = strtotime($firstApr);
		$dt = date("Y-06-01");
		$lastJun = date("Y-m-t", strtotime($dt));
		// $lastJunTimeStamp = strtotime($lastJun);
		$dt = date("Y-07-01");
		$firstJul = date("Y-m-01", strtotime($dt));
		// $firstJulTimeStamp = strtotime($firstJul);
		$dt = date("Y-09-01");
		$lastSep = date("Y-m-t", strtotime($dt));
		// $lastSepTimeStamp = strtotime($lastSep);
		$dt = date("Y-10-01");
		$firstOct = date("Y-m-01", strtotime($dt));
		$dt = date("Y-12-01");
		$lastDec = date("Y-m-t", strtotime($dt));

		if ($inputDate >= $firstJan && $inputDate <= $lastMar) {
			$lastDate = $lastMar;
		}
		if ($inputDate >= $firstApr && $inputDate <= $lastJun) {
			$lastDate = $lastJun;
		}
		if ($inputDate >= $firstJul && $inputDate <= $lastSep) {
			$lastDate = $lastSep;
		}
		if ($inputDate >= $firstOct && $inputDate <= $lastDec) {
			$lastDate = $lastDec;
		}
		return $lastDate;
	}

	public function getIntroducerLevel($customer_id, $referer_id, $referer) {
		$level = '';
		if (empty($referer_id) && $referer == 0) {
			// $level = 1;
			$level = $referer + 1;
		}
		if (!empty($referer_id)) {
			$level = $referer + 1;

		}
		return $level;
	} //public function getIntroducerLevel($customer_id, $referer_id, $referer) {
	public function deleteCustomerPoint($order_id) {
		$this->db->trans_begin();
		$this->db->set('delete_time', date("Y-m-d"));
		$this->db->where('order_id', $order_id);
		$this->db->update('customer_point');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}

	public function deleteCustomerUsedPoint($order_id) {
		$this->db->trans_begin();
		$this->db->set('delete_time', date("Y-m-d"));
		$this->db->where('order_id', $order_id);
		$this->db->update('customer_usedpoint');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
}
?>