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

class BrBookingModel extends CI_Model {

	function __construct() {
		parent::__construct();
		$this->load->model('TransactionModel', 'transaction_model');
		$this->load->model('BrservicesuserModel', 'serviceUser_model');
		$this->load->model('BrservicesModel', 'service_model');
		date_default_timezone_set('Asia/Hong_Kong');

		$this->load->model('Brshopmodel', 'shop_model');
		$this->load->model('Bremployeemodel', 'employee_model');
		$this->load->model('Brcustomermodel', 'customer_model');
		$this->load->model('LivingareaModel', 'livingarea_model');
		$this->load->model('ServicesModel', 'services_model');

		$this->load->model('BrRoomModel', 'room_model');
		$this->load->model('BrBookingModel', 'booking_model');

	}

	//new added booking
	function getAllBooking($shopId) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->result();
	}
	function getAllBookingById($shopId, $userid) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		$this->db->group_start();
		$this->db->where('pic_1', $userid);
		$this->db->or_where('pic_2', $userid);
		$this->db->group_end();
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->result();
	}
	function getAllBookingWithinDate($shopId, $pickedDate, $pickedmode) {
		//for calendar.js function reloadBookingData
		//added at 2020-08-07
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		if ($pickedmode == 'd') {
			$this->db->where('startdate =', $pickedDate);
		}
		if ($pickedmode == 'w') {

			$uDate = strtotime($pickedDate . ' + 1 week');
			$upperdate = date('Y-m-d', $uDate);
			$lDate = strtotime($pickedDate . ' - 1 week');
			$lowerdate = date('Y-m-d', $lDate);
			$this->db->where('startdate <=', $upperdate);
			$this->db->where('startdate >=', $lowerdate);
		}
		if ($pickedmode == 'm') {
			$uDate = strtotime($pickedDate . ' + 4 week');
			$upperdate = date('Y-m-d', $uDate);
			$lDate = strtotime($pickedDate . ' - 4 week');
			$lowerdate = date('Y-m-d', $lDate);
			$this->db->where('startdate <=', $upperdate);
			$this->db->where('startdate >=', $lowerdate);
		}
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->result();

	}
	function getAllBookingWithinDateById($shopId, $pickedDate, $pickedmode, $userid) {
		//for calendar.js function reloadBookingData
		//added at 2020-08-07
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		if ($pickedmode == 'd') {
			$this->db->where('startdate =', $pickedDate);
		}
		if ($pickedmode == 'w') {

			$uDate = strtotime($pickedDate . ' + 1 week');
			$upperdate = date('Y-m-d', $uDate);
			$lDate = strtotime($pickedDate . ' - 1 week');
			$lowerdate = date('Y-m-d', $lDate);
			$this->db->where('startdate <=', $upperdate);
			$this->db->where('startdate >=', $lowerdate);
		}
		if ($pickedmode == 'm') {
			$uDate = strtotime($pickedDate . ' + 4 week');
			$upperdate = date('Y-m-d', $uDate);
			$lDate = strtotime($pickedDate . ' - 4 week');
			$lowerdate = date('Y-m-d', $lDate);
			$this->db->where('startdate <=', $upperdate);
			$this->db->where('startdate >=', $lowerdate);
		}

		$this->db->group_start();
		$this->db->where('pic_1', $userid);
		$this->db->or_where('pic_2', $userid);
		$this->db->group_end();
		// $this->db->order_by('startdate DESC');

		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->result();

	}
	function getCustomerBookingById($userid) {
		//for listing all booking data
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('user_id', $userid);
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		$bookings = $query->result();
		$bookingdata = array();

		$count = 0;
		foreach ($bookings as $bookingrow) {
			$bookinglist = array();

			$bookinglist['id'] = $bookingrow->id;
			$bookinglist['starttime'] = DateTime::createFromFormat('Y-m-d H:i', $bookingrow->startdate . " " . $bookingrow->starttime);
			$starttimestamp = $bookinglist['starttime']->getTimestamp();
			$bookinglist['endtime'] = DateTime::createFromFormat('Y-m-d H:i', $bookingrow->startdate . " " . $bookingrow->endtime);

			$weekday = date("w", $starttimestamp);
			$weeklist = array('日', '一', '二', '三', '四', '五', '六');
			$bookinglist['weekdata'] = $weeklist[$weekday];
			$bookinglist['missedReservation'] = $bookingrow->missedReservation;

			//get booking items
			$bookingitemsname = "";
			$bookingitemscode = "";
			// $bookingitems=$this->booking_model->getBookingItem($bookingrow->id);

			//add use service
			$servicebookingitems = $this->booking_model->getServiceItemByBooking($bookingrow->id);

			$count = 0;
			foreach ($servicebookingitems as $bookingitemsrow) {

				$bookingitemsname = $bookingitemsname . $bookingitemsrow->name;
				$bookingitemscode = $bookingitemscode . $bookingitemsrow->code;
				// if($count!=0){
				$bookingitemscode = $bookingitemscode . " <br>";
				$bookingitemsname = $bookingitemsname . " <br>";
				// }
				$count = $count + 1;
			} //foreach ($bookingitems as $bookingitemsrow) {
			//end add use service

			$useServicebookingitems = $this->booking_model->getUseServiceItemByBooking($bookingrow->id);

			$count = 0;
			foreach ($useServicebookingitems as $bookingitemsrow) {

				$bookingitemsname = $bookingitemsname . $bookingitemsrow->name;
				$bookingitemscode = $bookingitemscode . $bookingitemsrow->code;
				// if($count!=0){
				$bookingitemscode = $bookingitemscode . " <br>";
				$bookingitemsname = $bookingitemsname . " <br>";
				// }
				$count = $count + 1;
			} //foreach ($bookingitems as $bookingitemsrow) {

			$bookinglist['bookingitemsname'] = $bookingitemsname;
			$bookinglist['bookingitemscode'] = $bookingitemscode;

			//end get booking items

			//get therapist name
			$pic_1name = "";
			if (!empty($bookingrow->pic_1)) {
				$bookinglist['pic_1name'] = $this->employee_model->getEmpolyeeName($bookingrow->pic_1);
			}
			$pic_2name = "";
			if (!empty($bookingrow->pic_2)) {
				$bookinglist['pic_2name'] = $this->employee_model->getEmpolyeeName($bookingrow->pic_2);
			}
			//end get therapist name

			$shoprow = $this->shop_model->getShopbyid($bookingrow->shopId);
			$bookinglist['shopname'] = $shoprow->name;

			$bookinglist['roomname'] = $this->room_model->getRoomName($bookingrow->room_id);
			$bookinglist['customersrow'] = $this->customer_model->getCustomer($bookingrow->user_id);
			$bookinglist['create_time'] = $bookingrow->create_time;
			$bookinglist['in_time'] = $this->booking_model->getIntime($bookingrow->id);
			$bookinglist['out_time'] = $this->booking_model->getOuttime($bookingrow->id);
			$bookinglist['isConfirm'] = $bookingrow->isConfirm;
			$bookinglist['isArrive'] = $bookingrow->isArrive;
			$bookinglist['isReturn'] = $bookingrow->isReturn;
			$bookinglist['remark'] = $bookingrow->remark;
			$bookinglist['isCancel'] = $bookingrow->isCancel;
			array_push($bookingdata, $bookinglist);

		} //foreach ($bookings as $bookingrow) {

		return $bookingdata;
	} //function getCustomerBookingById($userid){

	function getBookingItem($id) {
		$this->db->select('b.*, s.code, s.category_id, s.name, s.work, s.price,s.no_credit_rebate');
		$this->db->from('brbooking_items b,brservices s');
		$this->db->where('b.delete_time', NULL);
		$this->db->where('b.service_id = s.id');
		$this->db->where('booking_id', $id);
		$query = $this->db->get();
		return $query->result();

	} //function getBookingItem($id){

	function countTodayBooking($shopid) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopid);
		$this->db->where('startdate', date("Y-m-d"));
		// $this->db->where('shop_id',$id);
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->num_rows();
	}
	function countTodayBookingById($shopId, $userid) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		$this->db->group_start();
		$this->db->where('pic_1', $userid);
		$this->db->or_where('pic_2', $userid);
		$this->db->group_end();
		$this->db->where('startdate', date("Y-m-d"));
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->num_rows();
	}

	function countTodayNotConfirmBooking($shopid) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopid);
		$this->db->where('isConfirm', 0);
		$this->db->where('startdate', date("Y-m-d"));
		// $this->db->where('shop_id',$id);
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->num_rows();
	}
	function countTodayNotConfirmBookingById($shopId, $userid) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		$this->db->where('isConfirm', 0);
		// $this->db->where('shop_id',$id);
		$this->db->group_start();
		$this->db->where('pic_1', $userid);
		$this->db->or_where('pic_2', $userid);
		$this->db->group_end();
		$this->db->where('startdate', date("Y-m-d"));
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->num_rows();
	}

	function booking_exists($data) {
		$this->db->select('*');
		$this->db->from('brbookings');
		$this->db->where('startdate', $data['startdate']);
		$this->db->where('starttime', $data['starttime']);
		$this->db->where('endtime', $data['endtime']);
		$this->db->where('room_id', $data['room_id']);
		$this->db->where('user_id', $data['user_id']);
		$query = $this->db->get();
		// if($query->num_rows() == 1){
		if ($query->num_rows() >= 1) {
			return false;
		} else {
			return true;
		}

	}
	function booking_existsById($data) {
		$this->db->select('*');
		$this->db->from('brbookings');
		$this->db->where('startdate', $data['startdate']);
		$this->db->where('starttime', $data['starttime']);
		$this->db->where('endtime', $data['endtime']);
		$this->db->where('room_id', $data['room_id']);
		$this->db->where('user_id', $data['user_id']);
		$this->db->where('id !=', $data['bookingid']);

		//added for no same date booking 2020-10-19
		$this->db->where('delete_time', NULL);
		//end added for no same date booking 2020-10-19

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

	}
	function insertBooking($inputdata) {
		// 'inputadminname'=>$inputadminname,
		//         'inputloginname'=>$inputloginname,
		//         'inputPassword'=>$inputPassword,

		$this->db->trans_begin();
		$data = array(
			'startdate' => $inputdata['startdate'],
			'starttime' => $inputdata['starttime'],
			'endtime' => $inputdata['endtime'],
			'user_id' => $inputdata['user_id'],
			// 'fullName' => $inputdata['inputadminname'] ,
			'room_id' => $inputdata['room_id'],
			'pic_marked' => $inputdata['pic_marked'],
			'pic_1' => $inputdata['pic_1'],
			'pic_2' => $inputdata['pic_2'],
			'status' => 1,
			'display' => 1,
			'remark' => $inputdata['remark'],
			'shopId' => $inputdata['shopId'],

		);

		$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('brbookings', $data);
		$insert_id = $this->db->insert_id();
		//insert useService
		if (!empty($inputdata['useserviceboxvalue'])) {

			foreach ($inputdata['useserviceboxvalue'] as $useservice) {

				$serviceUserRow = $this->serviceUser_model->getServiceUserById($useservice);
				$data = array(
					'booking_id' => $insert_id,
					'service_user_id' => $useservice,
					'unit_price' => $serviceUserRow->unit_price,
					'quantity' => 1,
					'amount' => $serviceUserRow->unit_price * 1,
					// 'endtime' => $data['endtime'] ,
					// 'user_id' => $data['user_id'] ,
					// // 'fullName' => $data['inputadminname'] ,
					// 'room_id' => $data['room_id'] ,
					// 'pic_marked'=>$data['pic_marked'],
					// 'pic_1'=>$data['pic_1'],
					// 'pic_2'=>$data['pic_2'],
					'status' => 1,
					'display' => 1,
					// 'remark'=>$data['remark'],

				);
				$this->db->set('update_time', time(), FALSE);
				$this->db->set('create_time', time(), FALSE);
				$query = $this->db->insert('brbooking_items', $data);
			} //foreach ($data['useserviceboxvalue'] as $useservice)

		} //if(!empty($inputdata['useserviceboxvalue'])){
		//end

		//insert useProduct
		if (!empty($inputdata['useproductboxvalue'])) {

			foreach ($inputdata['useproductboxvalue'] as $useproduct) {

				$productRow = $this->service_model->getServices($useproduct);
				$data = array(
					'booking_id' => $insert_id,
					'service_id' => $useproduct,
					'unit_price' => $productRow->price,
					'quantity' => 1,
					'amount' => $productRow->price * 1,
					// 'endtime' => $data['endtime'] ,
					// 'user_id' => $data['user_id'] ,
					// // 'fullName' => $data['inputadminname'] ,
					// 'room_id' => $data['room_id'] ,
					// 'pic_marked'=>$data['pic_marked'],
					// 'pic_1'=>$data['pic_1'],
					// 'pic_2'=>$data['pic_2'],
					'status' => 1,
					'display' => 1,
					// 'remark'=>$data['remark'],

				);
				$this->db->set('update_time', time(), FALSE);
				$this->db->set('create_time', time(), FALSE);
				$query = $this->db->insert('brbooking_items', $data);
			} //foreach ($data['useserviceboxvalue'] as $useservice)

		} //if(!empty($inputdata['useserviceboxvalue'])){

		//end

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	}
	function updateBooking($inputdata) {
		// 'inputadminname'=>$inputadminname,
		//         'inputloginname'=>$inputloginname,
		//         'inputPassword'=>$inputPassword,
		$this->db->trans_begin();

		$data = array(
			'startdate' => $inputdata['startdate'],
			'starttime' => $inputdata['starttime'],
			'endtime' => $inputdata['endtime'],
			// 'user_id' => $inputdata['user_id'] ,
			// 'fullName' => $inputdata['inputadminname'] ,
			'room_id' => $inputdata['room_id'],
			'pic_marked' => $inputdata['pic_marked'],
			'pic_1' => $inputdata['pic_1'],
			'pic_2' => $inputdata['pic_2'],
			'status' => 1,
			'display' => 1,
			'remark' => $inputdata['remark'],
			'shopId' => $inputdata['shopId'],
			'isConfirm' => $inputdata['isConfirm'],
		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);
		// $this->db->set('create_time', time(), FALSE);
		$this->db->where('id', $inputdata['bookingid']);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->update('brbookings', $data);
		// $insert_id = $this->db->insert_id();

		//clear useBooking Item
		$this->db->where('booking_id', $inputdata['bookingid']);
		$this->db->delete('brbooking_items');
		//end clear useBooking Item

		//insert useService
		if (!empty($inputdata['useserviceboxvalue'])) {

			foreach ($inputdata['useserviceboxvalue'] as $useservice) {

				$serviceUserRow = $this->serviceUser_model->getServiceUserById($useservice);
				$data = array(
					'booking_id' => $inputdata['bookingid'],
					'service_user_id' => $useservice,
					'service_id' => NULL,
					'unit_price' => $serviceUserRow->unit_price,
					'quantity' => 1,
					'amount' => $serviceUserRow->unit_price * 1,
					// 'endtime' => $data['endtime'] ,
					// 'user_id' => $data['user_id'] ,
					// // 'fullName' => $data['inputadminname'] ,
					// 'room_id' => $data['room_id'] ,
					// 'pic_marked'=>$data['pic_marked'],
					// 'pic_1'=>$data['pic_1'],
					// 'pic_2'=>$data['pic_2'],
					'status' => 1,
					'display' => 1,
					// 'remark'=>$data['remark'],

				);
				$this->db->set('update_time', time(), FALSE);
				$this->db->set('create_time', time(), FALSE);
				$query = $this->db->insert('brbooking_items', $data);
			} //foreach ($data['useserviceboxvalue'] as $useservice)

		} //if(!empty($inputdata['useserviceboxvalue'])){
		//end

		//insert useProduct
		if (!empty($inputdata['useproductboxvalue'])) {

			foreach ($inputdata['useproductboxvalue'] as $useproduct) {

				$productRow = $this->service_model->getServices($useproduct);
				$data = array(
					'booking_id' => $inputdata['bookingid'],
					'service_user_id' => NULL,
					'service_id' => $useproduct,
					'unit_price' => $productRow->price,
					'quantity' => 1,
					'amount' => $productRow->price * 1,
					// 'endtime' => $data['endtime'] ,
					// 'user_id' => $data['user_id'] ,
					// // 'fullName' => $data['inputadminname'] ,
					// 'room_id' => $data['room_id'] ,
					// 'pic_marked'=>$data['pic_marked'],
					// 'pic_1'=>$data['pic_1'],
					// 'pic_2'=>$data['pic_2'],
					'status' => 1,
					'display' => 1,
					// 'remark'=>$data['remark'],

				);
				$this->db->set('update_time', time(), FALSE);
				$this->db->set('create_time', time(), FALSE);
				$query = $this->db->insert('brbooking_items', $data);
			} //foreach ($data['useserviceboxvalue'] as $useservice)

		} //if(!empty($inputdata['useserviceboxvalue'])){

		//end

		// return $query;

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	}
	function deleteBooking($inputdata) {

		$this->db->trans_begin();

		$id = $inputdata['id'];
		date_default_timezone_set('Asia/Hong_Kong');
		//update booking table
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbookings');
		// return $query;

		//end

		//update booking item table
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbooking_items');
		//end

		//update customer point item
		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('booking_id', $id);
		$query = $this->db->update('brcustomer_usedpoint');
		//end

		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}

	}
	function getMissedDetials($id) {
		$this->db->select('brbookings.*,brcustomer.display_name,brcustomer.chi_name,brcustomer.mobile,brcustomer.prefer_pic');
		$this->db->from('brbookings');
		$this->db->join('brcustomer', 'brbookings.user_id = brcustomer.id');
		$this->db->where('brbookings.delete_time', NULL);
		$this->db->where('brbookings.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row()->missedReservation;
		} else {
			return "";
		}
	}
	function getCancelDetials($id) {
		$this->db->select('brbookings.*,brcustomer.display_name,brcustomer.chi_name,brcustomer.mobile,brcustomer.prefer_pic');
		$this->db->from('brbookings');
		$this->db->join('brcustomer', 'brbookings.user_id = brcustomer.id');
		$this->db->where('brbookings.delete_time', NULL);
		$this->db->where('brbookings.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row()->isCancel;
		} else {
			return "";
		}
	}

	function arriveBooking($id) {
		// $id=$inputdata['id'];
		// date_default_timezone_set('Asia/Hong_Kong');
		//update booking table
		$this->db->trans_begin();
		$data = array(
			'isArrive' => 1,
		);
		$this->db->set('arriveTime', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbookings', $data);

		// return  $query;

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}

	}
	function missedBooking($id, $missedReservation) {
		// $id=$inputdata['id'];
		// date_default_timezone_set('Asia/Hong_Kong');
		//update booking table
		$this->db->trans_begin();
		$inputValue = "";
		if ($missedReservation == 0) {
			$inputValue = 1;
		}
		if ($missedReservation == 1) {
			$inputValue = 0;
		}

		$data = array(
			'missedReservation' => $inputValue,
		);
		$this->db->set('update_time', time(), FALSE);
		// $this->db->set('arriveTime', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbookings', $data);

		// return  $query;

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}

	}
	function custcancelBooking($id) {
		$this->db->trans_begin();

		$data = array(
			'isCancel' => 1,
		);
		$this->db->set('update_time', time(), FALSE);
		// $this->db->set('arriveTime', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbookings', $data);

		// return  $query;

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	}

	function getBookingByid($id) {
		$this->db->select('brbookings.*,brcustomer.display_name,brcustomer.chi_name,brcustomer.mobile,brcustomer.prefer_pic');
		$this->db->from('brbookings');
		$this->db->join('brcustomer', 'brbookings.user_id = brcustomer.id');
		$this->db->where('brbookings.delete_time', NULL);
		$this->db->where('brbookings.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row();
		} else {
			return "";
		}
	} //function getBookingByid($id){
	function getBookingItemByid($id) {
		$this->db->select('brbooking_items.*');
		$this->db->from('brbooking_items');

		$this->db->where('brbooking_items.id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row();
		} else {
			return "";
		}
	} //function getBookingByid($id){
	function getBookingService($bookingid) {
		$this->db->select('service_user_id,service_id');
		$this->db->from('brbooking_items');
		$this->db->where('brbooking_items.booking_id', $bookingid);
		$query = $this->db->get();
		return $query->result();
	}
	function getUseServiceItemByBooking($bookingid) {
		// $this->db->select('brservices.code,brservices.name as name,brbooking_items.id as itemid,brbooking_items.code_1 as code_1,brbooking_items.code_2 as code_2,brbooking_items.code_area as code_area,brbooking_items.code_remark as code_remark');
		$this->db->select('brservices.code,brservices.name as name,brbooking_items.id as itemid,brbooking_items.code_1 as code_1,brbooking_items.code_2 as code_2,brbooking_items.code_area as code_area,brbooking_items.code_remark as code_remark,brservices.category_id as category_id,brservices.work as work,brservice_user.unit_price');
		$this->db->from('brbooking_items');
		$this->db->join('brservice_user', 'brservice_user.id = brbooking_items.service_user_id');
		$this->db->join('brservices', 'brservices.id = brservice_user.service_id');
		$this->db->where('brbooking_items.service_id', NULL);
		$this->db->where('brbooking_items.delete_time', NULL);
		$this->db->where('brbooking_items.booking_id', $bookingid);
		$this->db->order_by('brservices.code', 'ASC');

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

	}
	function getAllUseServiceItem() {
		$this->db->select('brbooking_items.booking_id as booking_id,brservices.code,brservices.name as name,brbooking_items.id as itemid,brbooking_items.code_1 as code_1,brbooking_items.code_2 as code_2,brbooking_items.code_area as code_area,brbooking_items.code_remark as code_remark');
		$this->db->from('brbooking_items');
		$this->db->join('brservice_user', 'brservice_user.id = brbooking_items.service_user_id');
		$this->db->join('brservices', 'brservices.id = brservice_user.service_id');
		$this->db->where('brbooking_items.service_id', NULL);
		$this->db->where('brbooking_items.delete_time', NULL);
		//$this->db->where('brbooking_items.booking_id',$bookingid);
		$this->db->order_by('brservices.code', 'ASC');

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

	}

	function getServiceItemByBooking($bookingid) {
		$this->db->select('brservices.code,brservices.name as name,brbooking_items.id as itemid,brbooking_items.code_1 as code_1,brbooking_items.code_2 as code_2,brbooking_items.code_area as code_area,brbooking_items.code_remark as code_remark');
		$this->db->from('brbooking_items');
		$this->db->join('brservices', 'brservices.id = brbooking_items.service_id');
		$this->db->where('brbooking_items.service_user_id', NULL);
		$this->db->where('brbooking_items.delete_time', NULL);
		$this->db->where('brbooking_items.booking_id', $bookingid);
		$this->db->order_by('brservices.code', 'ASC');

		$query = $this->db->get();
		return $query->result();
	}
	function getAllServiceItem() {
		$this->db->select('brbooking_items.booking_id as booking_id,brservices.code,brservices.name as name,brbooking_items.id as itemid,brbooking_items.code_1 as code_1,brbooking_items.code_2 as code_2,brbooking_items.code_area as code_area,brbooking_items.code_remark as code_remark');
		$this->db->from('brbooking_items');
		$this->db->join('brservices', 'brservices.id = brbooking_items.service_id');
		$this->db->where('brbooking_items.service_user_id', NULL);
		$this->db->where('brbooking_items.delete_time', NULL);
		//$this->db->where('brbooking_items.booking_id',$bookingid);
		$this->db->order_by('brservices.code', 'ASC');

		$query = $this->db->get();
		return $query->result();
	}
	function updateCheckOutBookingItem($inputdata) {
		$this->db->trans_begin();
		// $id=$inputdata['id'];
		$data = array(
			'code_1' => $inputdata['code_1'],
			'code_2' => $inputdata['code_2'],
			'code_area' => $inputdata['code_area'],
			'code_remark' => $inputdata['code_remark'],

		);
		// date_default_timezone_set('UTC');

		//  $this->db->set('update_time', time(), FALSE);
		// $this->db->set('create_time', time(), FALSE);
		$this->db->where('id', $inputdata['id']);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->update('brbooking_items', $data);
		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
	}
	function updateCheckOutReturnBooking($id) {

		// remove check return at 2020-08-25
		// if($this->checkBookingIsReturn($id)==false){
		$this->db->trans_begin();
		$data = array(
			'isReturn' => 1,

		);
		$this->db->set('returnTime', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brbookings', $data);
		// return $query;
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			// echo "roll back";
			return false;
		} else {
			// $this->db->trans_rollback();
			$this->db->trans_commit();
			// echo "commit transaction";
			return true;
		}
		// }//if($this->checkBookingIsReturn($id)==false){
	}
	function checkBookingIsReturn($id) {
		$this->db->where('returnTime!=', NULL);
		$this->db->where('isReturn', 1);
		$this->db->where('id', $id);
		$query = $this->db->get('brbookings');
		if ($query->num_rows() == 1) {
			return true;
		} else {
			return false;
		}
	}
	function checkIsItNeedPrintInvoice($id) {
		$this->db->where('service_id!=', NULL);
		$this->db->where('booking_id', $id);
		$query = $this->db->get('brbooking_items');
		if ($query->num_rows() >= 1) {
			return true;
		} else {
			return false;
		}
	}

	//end new added booking

	function getIntime($id) {
		$this->db->select('arriveTime');
		$this->db->from('brbookings');
		$this->db->where('id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row()->arriveTime;
		} else {
			return "";
		}

	}

	function getOuttime($id) {
		$this->db->select('returnTime');
		$this->db->from('brbookings');
		$this->db->where('id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			return $query->row()->returnTime;
		} else {
			return "";
		}

	}
	function checkin($dataori) {
		// $data = array(
		//     'id' => $dataori['id'] ,
		//  );

		$this->db->select('booking_id');
		$this->db->from('mas_bookings_attendance');
		$this->db->where('booking_id', $dataori['id']);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			//update attendance
			date_default_timezone_set('UTC');

			$data = array(
				'in_time' => time(),
			);
			$this->db->set('update_time', 'NOW()', FALSE);
			$this->db->where('booking_id', $dataori['id']);
			$this->db->update('mas_bookings_attendance', $data);
			return true;
		} else {
			//insert attendance

			date_default_timezone_set('UTC');

			$data = array(
				'in_time' => time(),
				'booking_id' => $dataori['id'],
				'user_id' => $dataori['user_id'],
				// 'endtime' => $data['endtime'] ,
				// 'user_id' => $data['user_id'] ,
				// // 'fullName' => $data['inputadminname'] ,
				// 'room_id' => $data['room_id'] ,
				// 'pic_marked'=>$data['pic_marked'],
				// 'pic_1'=>$data['pic_1'],
				// 'pic_2'=>$data['pic_2'],
				// 'status'=>1,
				// 'display'=>1,
				// 'remark'=>$data['remark'],
			);
			// $this->db->set('update_time', 'NOW()', FALSE);
			$this->db->set('date', 'NOW()', FALSE);
			$this->db->set('create_time', 'NOW()', FALSE);
			$query = $this->db->insert('mas_bookings_attendance', $data);
			return $query;
			//end insert
		}
	}
	function checkout($dataori) {
		// $data = array(
		//     'id' => $dataori['id'] ,
		//  );

		$this->db->select('booking_id');
		$this->db->from('mas_bookings_attendance');
		$this->db->where('booking_id', $dataori['id']);
		$query = $this->db->get();
		if ($query->num_rows() == 1) {
			//update attendance
			date_default_timezone_set('UTC');

			$data = array(
				'out_time' => time(),
			);
			$this->db->set('update_time', 'NOW()', FALSE);
			$this->db->where('booking_id', $dataori['id']);
			$this->db->update('mas_bookings_attendance', $data);
			return true;
		} else {
			//insert attendance

			date_default_timezone_set('UTC');

			$data = array(
				'out_time' => time(),
				'booking_id' => $dataori['id'],
				'user_id' => $dataori['user_id'],
				// 'endtime' => $data['endtime'] ,
				// 'user_id' => $data['user_id'] ,
				// // 'fullName' => $data['inputadminname'] ,
				// 'room_id' => $data['room_id'] ,
				// 'pic_marked'=>$data['pic_marked'],
				// 'pic_1'=>$data['pic_1'],
				// 'pic_2'=>$data['pic_2'],
				// 'status'=>1,
				// 'display'=>1,
				// 'remark'=>$data['remark'],
			);
			// $this->db->set('update_time', 'NOW()', FALSE);
			$this->db->set('date', 'NOW()', FALSE);
			$this->db->set('create_time', 'NOW()', FALSE);
			$query = $this->db->insert('mas_bookings_attendance', $data);
			return $query;
			//end insert
		}
	}

	//new latepoint function
	function get_total_amount_paid_from_transactions($id) {
		$transactions = $this->transaction_model->getAllTransactions($id);
		$total = 0;
		foreach ($transactions as $transaction) {
			$total += $transaction->amount;
		}
		return $total;

	}

	function get_default_payment_method() {
		if ($this->payment_method) {
			return $this->payment_method;
		} else {
			//not set as helper not set 2020-06-01
			// return OsPaymentsHelper::default_payment_method();
			return "cash";
		}
	}
	function delete_meta_by_key($meta_key) {
		//not really know the usage
	}
	function get_payment_portion_nice_name() {
		//return partial payment name
		$paymentportion = array();
		$paymentportion[0] = "全數";
		$paymentportion[1] = "訂金";

		return $paymentportion;

	}
	function get_payment_method_nice_name() {
		//return partial payment name
		$paymentmethod[1] = "Jarvix";
		$paymentmethod = array();
		$paymentmethod[0] = "現金";
		$paymentmethod[2] = "EPS";
		$paymentmethod[3] = "CUP";
		$paymentmethod[4] = "VISA";
		$paymentmethod[5] = "MASTER";

		return $paymentmethod;

	}
	function get_ical_download_link() {
		//not find the use
	}
	function get_print_link() {
		//not find the use
	}
	function get_meta_by_key($meta_key, $default = false) {
		//not find the use
	}
	function save_meta_by_key($meta_key, $meta_value) {
		//not find the use
	}
	function calculate_end_date($id) {
		//return end date if start_time larger than 24*60 of booking duration
	}
	function calculate_end_time($id) {
		$this->db->from('mas_bookings');
		$this->db->where('id', $id);
		$starttime = $this->db->get()->row()->starttime;

		$end_time = $starttime + $this->get_total_duration();
		// continues to next day?
		if ($end_time > (24 * 60)) {
			$end_time = $end_time - (24 * 60);
		}
		return $end_time;
	}
	function get_total_duration($id) {
		$this->db->from('mas_bookings');
		$this->db->where('id', $id);
		$endtime = $this->db->get()->row()->endtime;
		$starttime = $this->db->get()->row()->starttime;

		$start_t = new DateTime($starttime);
		$current_t = new DateTime($endtime);
		$difference = $start_t->diff($current_t);
		$return_time = $difference->format('%H:%I:%S');

		return $return_time;
	}
	function get_start_time_shifted_for_customer() {
	}
	function get_end_time_shifted_for_customer() {
	}

	function get_nice_created_at() {
		$this->db->from('mas_bookings');
		$this->db->where('id', $id);
		$createtime = $this->db->get()->row()->create_time;
		return $createtime;
	}
	// ONLY USED ON LAST STEP OF BOOKING, TO CONVERT SELECTED BOOKING TIME FROM CUSTOMER ZONE TO OUR INTERNAL TIMEZONE
	// DO NOT USE ANYWHERE ELSE BECAUSE IT MODIFIES ACTUAL VALUES
	function apply_customer_timeshift() {
		//may be switch the the time zone to 8 hours later
		//not really use at all
		$shift_in_minutes = $this->customer->get_timeshift_in_minutes();
		$this->start_time = $this->start_time - $shift_in_minutes;
		if ($this->start_time < 0) {
			$date_obj = new OsWpDateTime($this->start_date);
			$this->start_date = $date_obj->modify('-1 day')->format('Y-m-d');
			$this->start_time = $this->start_time + (24 * 60);
		} elseif ($this->start_time > (24 * 60)) {
			$date_obj = new OsWpDateTime($this->start_date);
			$this->start_date = $date_obj->modify('+1 day')->format('Y-m-d');
			$this->start_time = $this->start_time - (24 * 60);
		}
	}

	function save_from_booking_form() {
		//1.check current logged in user
		//2.check service id
		//3.if any agent selected
		//4.get agent id from OsBookingHelpher

	}
	function get_nice_status($id) {
		$this->db->from('mas_bookings');
		$this->db->where('id', $id);
	}

	//end new latepoint function

	function getAllRoom($id) {
		$this->db->from('mas_room');
		$this->db->where('shop_id', $id);
		$query = $this->db->get();
		return $query->result();
	}

	function room_exists($shop_id, $name) {
		// $this->db->from('adminUsers');
		$this->db->where('LOWER(name)', $name);
		$this->db->where('shop_id', $shop_id);
		$query = $this->db->get('mas_room');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}

	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;
			}

		}
	}
	//added for get employee booking
	function employeefinishedBookingById($shopId, $userid, $startdate, $enddate) {
		$this->db->from('brbookings');
		$this->db->where('delete_time', NULL);
		$this->db->where('shopId', $shopId);
		// $this->db->where('shop_id',$id);
		$this->db->group_start();
		$this->db->where('pic_1', $userid);
		$this->db->or_where('pic_2', $userid);
		$this->db->group_end();
		$this->db->where('startdate<=', $enddate);
		$this->db->where('startdate>=', $startdate);
		$this->db->order_by('startdate DESC');
		$query = $this->db->get();
		return $query->result();
	}

	//end added for get employee booking

}