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

class Brcustomerpointmodel extends CI_Model {

	function __construct() {
		parent::__construct();
		$this->load->model('Brcustomerusedpointmodel', 'customerusedpoint_model');
		date_default_timezone_set('Asia/Hong_Kong');
	}
	//new added customer point
	function insertCustomerPoint($inputdata) {
		$data = array(
			'purchased_date' => $inputdata['purchased_date'],
			'point' => $inputdata['point'],
			'purchased_price' => $inputdata['purchased_price'],
			'remark' => $inputdata['remark'],
			'user_id' => $inputdata['user_id'],
			'status' => 1,
			'display' => 0,

		);
		//
		if (isset($inputdata['invoice_id'])) {
			$data['invoice_id'] = $inputdata['invoice_id'];
		}
		date_default_timezone_set('Asia/Hong_Kong');

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->insert('brcustomer_point', $data);
		return $query;
	} //function insertCustomerPoint($inputdata){
	function updateCustomerPoint($data) {
		date_default_timezone_set('Asia/Hong_Kong');
		$data2 = array(
			'purchased_date' => $data['purchased_date'],
			'point' => $data['point'],
			'purchased_price' => $data['purchased_price'],
			// 'fullName' => $data['inputadminname'] ,
			'purchased_price' => $data['purchased_price'],
			'remark' => $data['remark'],
		);

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brcustomer_point', $data2);
		return $query;

	}
	function deleteCustomerPoint($data) {
		$this->db->trans_begin();

		date_default_timezone_set('Asia/Hong_Kong');
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $data['id']);
		$query = $this->db->update('brcustomer_point');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
	function deletePointByInvoiceid($invoiceid) {
		//added at 2020-12-09
		$this->db->trans_begin();

		date_default_timezone_set('Asia/Hong_Kong');
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('invoice_id', $invoiceid);
		$query = $this->db->update('brcustomer_point');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
		//end added at 2020-12-09
	}
	function getCustomerPointHistoryById($user_id) {
		$this->db->from('brcustomer_point');
		$this->db->where('delete_time', NULL);
		$this->db->where('user_id', $user_id);
		// $this->db->where('user_id', 'product');
		$this->db->order_by('create_time desc');
		$query = $this->db->get();

		return $query->result();
	} //function getCustomerPointHistoryById($user_id){
	function getCustomerPointDataById($id) {
		$this->db->from('brcustomer_point');
		// $this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		// $this->db->join('brcustomer', 'brservice_user.user_id = brcustomer.id');
		// $this->db->where('brservice_user.delete_time', NULL);
		$this->db->where('id', $id);
		// $this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get()->row();
		return $query;
	}
	function getCustomerPoint($user_id) {
		$this->db->select('SUM(point) as totalpoint');
		$this->db->from('brcustomer_point');
		// $this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		// $this->db->join('brcustomer', 'brservice_user.user_id = brcustomer.id');
		$this->db->where('delete_time', NULL);
		$this->db->where('user_id', $user_id);
		// $this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get()->row();

		$totalpoint = '';
		if (!empty($query)) {
			$totalpoint = $query->totalpoint;
		}

		$usedpoint = $this->customerusedpoint_model->getCustomerUsedPointByUserId($user_id);

		if (!empty($usedpoint)) {
			$totalpoint = $totalpoint - $usedpoint;
		}

		return $totalpoint;
	}
	function getCustomerTotalPoint($user_id) {
		$this->db->select('SUM(point) as totalpoint');
		$this->db->from('brcustomer_point');
		// $this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		// $this->db->join('brcustomer', 'brservice_user.user_id = brcustomer.id');
		$this->db->where('delete_time', NULL);
		$this->db->where('user_id', $user_id);
		// $this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get()->row();

		$totalpoint = '';
		if (!empty($query)) {
			$totalpoint = $query->totalpoint;
		}

		return $totalpoint;
	}
	function getInvoicePointByInvoiceId($invoiceid, $userid) {
		$this->db->select('SUM(point) as totalpoint');
		$this->db->from('brcustomer_point');
		// $this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		// $this->db->join('brcustomer', 'brservice_user.user_id = brcustomer.id');
		$this->db->where('delete_time', NULL);
		$this->db->where('invoice_id', $invoiceid);
		$this->db->where('user_id', $userid);
		// $this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get()->row();

		$totalpoint = '';
		if (!empty($query)) {
			$totalpoint = $query->totalpoint;
		}

		return $totalpoint;
	}

	//end new added customer point

	//new added services user
	function insertServicesUser($inputdata) {
		$this->load->model('BrservicesModel', 'services_model');

		$category_id = $this->services_model->getCategoryid($inputdata['service_id']);
		$calunit_price = $inputdata['purchased_price'] / $inputdata['purchased_qty'];

		$unit_price = round($calunit_price, 2);
		// $d = DateTime::createFromFormat(
		//     'd-m-Y ',
		//     $inputdata['purchased_date'],
		//     new DateTimeZone('Asia/Hong_Kong')
		// );
		// $purchase_timestamp=$d->getTimestamp();
		$data = array(
			'category_id' => $category_id,
			'user_id' => $inputdata['user_id'],
			'service_id' => $inputdata['service_id'],
			'purchased_date' => $inputdata['purchased_date'],
			'unit_price' => $unit_price,
			'purchased_price' => $inputdata['purchased_price'],
			// 'fullName' => $data['inputadminname'] ,
			'purchased_qty' => $inputdata['purchased_qty'],
			'available_qty' => $inputdata['available_qty'],
			'remark' => $inputdata['remark2'],
			'status' => 1,
			'display' => 0,

		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->insert('brservice_user', $data);
		return $query;

	}
	function insertServicesUserForAddService($inputdata) {
		//for service user
		//may be different data input -2020-07-13
		$this->load->model('BrservicesModel', 'services_model');

		$category_id = $this->services_model->getCategoryid($inputdata['service_id']);
		// $calunit_price=$inputdata['purchased_price']/$inputdata['purchased_qty'] ;

		// $unit_price=round($calunit_price, 2);
		// $d = DateTime::createFromFormat(
		//     'd-m-Y ',
		//     $inputdata['purchased_date'],
		//     new DateTimeZone('Asia/Hong_Kong')
		// );
		// $purchase_timestamp=$d->getTimestamp();
		// $data = array(
		//     'category_id' => $inputdata['category_id'] ,
		//     'user_id' => $inputdata['user_id'] ,
		//     'service_id' => $inputdata['service_id'] ,
		//     'available_qty' =>  $inputdata['available_qty'],
		//     'purchased_qty' =>$inputdata['purchased_qty'],
		//     'unit_price' => $inputdata['unit_price'] ,
		//     // 'fullName' => $data['inputadminname'] ,
		//     'purchased_price' => $inputdata['purchased_price'] ,
		//     'remark' => $inputdata['remark'] ,
		//     'status' => 1 ,
		//     'display' => 0 ,

		//  );
		$data = array(
			'category_id' => $category_id,
			'user_id' => $inputdata['user_id'],
			'service_id' => $inputdata['service_id'],
			'available_qty' => $inputdata['available_qty'],
			'purchased_qty' => $inputdata['purchased_qty'],
			'unit_price' => $inputdata['unit_price'],
			// 'fullName' => $data['inputadminname'] ,
			'purchased_price' => $inputdata['purchased_price'],
			'remark' => $inputdata['remark'],
			'status' => 1,
			'display' => 0,

		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->insert('brservice_user', $data);
		return $query;

	}
	function getCustServiceById($id) {
		$this->db->select("brservice_user.id,brservice_user.purchased_date,brservice_user.category_id,brservices.name as serviceName,brservice_user.purchased_qty,brservice_user.available_qty,brservice_user.remark");
		$this->db->from('brservice_user');
		$this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		$this->db->where('brservice_user.delete_time', NULL);
		$this->db->where('brservice_user.user_id', $id);
		$this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get();
		return $query->result();
	} //function getCustServiceById($id){
	function getCustServiceByRowId($id) {
		$this->db->select("brservice_user.id,brservice_user.purchased_date,brservice_user.category_id,brservices.name as serviceName,brservice_user.purchased_qty,brservice_user.purchased_price,brservice_user.available_qty,brcustomer.display_name as display_name,brcustomer.remark as remark,brservice_user.service_id,brservice_user.unit_price,brservice_user.user_id,brservice_user.remark as serviceRemark");
		$this->db->from('brservice_user');
		$this->db->join('brservices', 'brservice_user.service_id = brservices.id');
		$this->db->join('brcustomer', 'brservice_user.user_id = brcustomer.id');
		$this->db->where('brservice_user.delete_time', NULL);
		$this->db->where('brservice_user.id', $id);
		$this->db->order_by('brservice_user.create_time', 'DESC');
		$query = $this->db->get()->row();
		return $query;
	} //function getCustServiceByRowId($id){
	function updateServicesUser($data) {
		$this->load->model('BrservicesModel', 'services_model');
		$category_id = $this->services_model->getCategoryid($data['service_id']);

		//added content

		date_default_timezone_set('Asia/Hong_Kong');
		$purchatedateformat = DateTime::createFromFormat('Y-m-d', $data['purchased_date']);
		$purchasetimeStamp = "";
		if (!empty($data['purchased_date'])) {
			if ($purchatedateformat === false) {

			} else {
				$purchasetimeStamp = $purchatedateformat->getTimestamp();
			}
		}

		//end added content

		$data2 = array(
			'category_id' => $category_id,
			'service_id' => $data['service_id'],
			'available_qty' => $data['available_qty'],
			'purchased_qty' => $data['purchased_qty'],
			'unit_price' => $data['unit_price'],
			// 'fullName' => $data['inputadminname'] ,
			'purchased_price' => $data['purchased_price'],
			'remark' => $data['remark'],
			'purchased_date' => $purchasetimeStamp,
		);

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservice_user', $data2);
		return $query;

	}
	function deleteServicesUser($data) {
		$this->db->trans_begin();

		date_default_timezone_set('Asia/Hong_Kong');
		$this->db->set('available_qty', 0);
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservice_user');

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return false;
		} else {
			$this->db->trans_commit();
			return true;
		}
	}
	function getAvailableServiceUser($service_user_id) {
		$this->db->select("SUM(quantity) as totalused");
		$this->db->from('brbooking_items');
		$this->db->join('brbookings', 'brbooking_items.booking_id = brbookings.id');
		$this->db->where('brbooking_items.service_id', NULL);
		$this->db->where('brbooking_items.service_user_id', $service_user_id);
		$this->db->where('brbookings.isReturn', 1);
		$this->db->where('brbookings.returnTime!=', NULL);
		$query = $this->db->get()->row();
		return $query->totalused;
		// return $this->db->last_query();

	}
	function getTicketNum($booking_id, $brbooking_items_id) {

		//get service user id
		$this->db->select("service_user_id");
		$this->db->where('id', $brbooking_items_id);
		$this->db->from('brbooking_items');

		$service_user_id = $this->db->get()->row()->service_user_id;

		//end get service user id

		//get create time
		$this->db->select("create_time");
		$this->db->where('id', $brbooking_items_id);
		$this->db->from('brbooking_items');

		$create_time = $this->db->get()->row()->create_time;

		//end get create time

		$isBookingReturn = false;
		//if booking is returned, returned =1
		$this->db->select("id");
		$this->db->where('isReturn', 1);
		$this->db->where('delete_time', NULL);
		$this->db->where('id', $booking_id);
		$this->db->from('brbookings');

		$query = $this->db->get()->row();
		// if($query->num_rows() == 1){
		if ($query) {
			$isBookingReturn = true;
		} else {
			$isBookingReturn = false;
		}

		if ($isBookingReturn == true) {

			//get available qty
			$this->db->select("available_qty");
			$this->db->where('id', $service_user_id);
			$this->db->from('brservice_user');
			$available_qty = $this->db->get()->row()->available_qty;
			//end get available qty

			$this->db->select("SUM(quantity) as usedquantity");
			$this->db->from('brbooking_items');
			$this->db->join('brbookings', 'brbooking_items.booking_id = brbookings.id');
			$this->db->where('brbooking_items.service_id', NULL);
			$this->db->where('brbooking_items.service_user_id', $service_user_id);
			$this->db->where('brbooking_items.create_time<=', $create_time);
			$this->db->where('brbookings.isReturn', 1);
			$this->db->where('brbookings.returnTime!=', NULL);
			$this->db->where('brbookings.returnTime!=', NULL);
			$usedquantity = $this->db->get()->row()->usedquantity;

			return $available_qty - $usedquantity + 1;
		} //if($isBookingReturn==true){

		if ($isBookingReturn == false) {
			//get available qty
			$this->db->select("available_qty");
			$this->db->where('id', $service_user_id);
			$this->db->from('brservice_user');

			$available_qty = $this->db->get()->row()->available_qty;
			//end get available qty

			//get used quantity
			$this->db->select("SUM(quantity) as totalused");
			$this->db->from('brbooking_items');
			$this->db->join('brbookings', 'brbooking_items.booking_id = brbookings.id');
			$this->db->where('brbooking_items.service_id', NULL);
			$this->db->where('brbooking_items.service_user_id', $service_user_id);
			$this->db->where('brbookings.isReturn', 1);
			$this->db->where('brbookings.returnTime!=', NULL);
			$totalused = $this->db->get()->row()->totalused;

			//end get used quantity

			return $available_qty - $totalused;
		}

	} //function getTicketNum($booking_id,$brbooking_items_id){
	//end new added services user
	function getAllServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function getServices($id) {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->where('id', $id);
		// $this->db->order_by('code');
		$query = $this->db->get();
		$ret = $query->row();
		return $ret;
	}
	function getFTServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('category_id', 1);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function getTServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('category_id', 2);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function services_exists($code) {
		// $this->db->from('adminUsers');
		// $this->db->where('LOWER(name)', $name);
		$this->db->where('LOWER(code)', strtolower($code));
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$query = $this->db->get('brservices');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}
	function getServiceUserById($id) {
		$this->db->select("*");
		$this->db->from('brservice_user');
		$this->db->where('id', $id);
		$query = $this->db->get()->row();
		return $query;
	} //function getCustServiceByRowId($id){
	function services_existsbyid($code, $id) {
		// $this->db->from('adminUsers');
		// $this->db->where('LOWER(name)', $name);
		$this->db->where('LOWER(code)', strtolower($code));
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->where('id !=', $id);
		$query = $this->db->get('mas_services');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}
	function insertAdmin($data) {
		// 'inputadminname'=>$inputadminname,
		//         'inputloginname'=>$inputloginname,
		//         'inputPassword'=>$inputPassword,
		$data = array(
			'code' => $data['code'],
			'name' => $data['name'],
			'work' => $data['work'],
			'price' => $data['price'],
			// 'fullName' => $data['inputadminname'] ,
			'time' => $data['time'],
			'time_type' => $data['time_type'],
			'category_id' => $data['category_id'],
			'no_credit_rebate' => $data['no_credit_rebate'],
			'status' => $data['status'],
			'display' => $data['display'],
			'remark' => $data['remark'],
			'type' => 'product',

		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->insert('brservices', $data);
		return $query;
	}
	function updateAdmin($data) {
		$data2 = array(
			'code' => $data['code'],
			'name' => $data['name'],
			'work' => $data['work'],
			'price' => $data['price'],
			// 'fullName' => $data['inputadminname'] ,
			'time' => $data['time'],
			'time_type' => $data['time_type'],
			'category_id' => $data['category_id'],
			'no_credit_rebate' => $data['no_credit_rebate'],
			'status' => $data['status'],
			'display' => $data['display'],
			'remark' => $data['remark'],
			'type' => 'product',

		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservices', $data2);
		return $query;

	}

	function getCategoryid($id) {
		$this->db->from('brservices');
		$this->db->where('id', $id);
		$category_id = $this->db->get()->row()->category_id;
		return $category_id;
	}
	function updateStatus($data) {
		$data2 = array(
			'status' => $data['status'],
		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservices', $data2);
		return $query;
	}
	function deleteService($id) {
		date_default_timezone_set('UTC');

		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brservices');
		return $query;
	}

	function getBannerAll() {

		$this->db->from('frontendUsers');
		// $this->db->where(array('show' => 1));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}

	function checking() {

		$userid = $this->session->userdata('userid');
		$username = $this->session->userdata('username');
		$fullName = $this->session->userdata('fullName');
		$logged_in = $this->session->userdata('logged_in');
		$secret = $this->session->userdata('secret');
		$usertype = $this->session->userdata('usertype');

		$passLogin = false;
		if ($usertype == "frontenduser") {
			if ($logged_in == false) {
				return false;
			}
			if (empty($userid)) {
				return false;
			}
			if (empty($secret)) {
				return false;
			}
			$this->db->where('id', $userid);
			$this->db->where('secret', $secret);
			if ($this->db->get('frontendUsers')->row_array()) {
				$passLogin = true;
				return true;
			} else {
				return false;
			}

		} //if($usertype=="frontenduser"){
		if ($usertype == "backenduser") {
			if ($logged_in == false) {
				return false;
			}
			if (empty($userid)) {
				return false;
			}
			if (empty($secret)) {
				return false;
			}
			$this->db->where('id', $userid);
			$this->db->where('secret', $secret);
			if ($this->db->get('adminUsers')->row_array()) {
				$passLogin = true;
				return true;
			} else {
				return false;
			}

		}
	}
	function invoiceAmountToPoint($invoiceAmount) {
		//added at 2020-12-09
		$point = 0;
		if ($invoiceAmount > 0) {
			$point = ($invoiceAmount * 5) / 100;
			$point = round($point);
		}
		return $point;
		//end added at 2020-12-09
	}
	function invoiceAmountToRefererPoint($invoiceAmount) {
		//added at 2020-12-09
		$point = 0;
		if ($invoiceAmount > 0) {
			$point = ($invoiceAmount * 5) / 100;
			$point = round($point);
		}
		return $point;
		//end added at 2020-12-09
	}
}