<?php
class Room_m extends CI_Model {
function __construct() {
parent::__construct();
$this->load->library('mydate');
}
function get_all($txtsearch, $offset, $per_page = 20, $url = '/room/ajax_get_grid') {
if ($txtsearch != '') {
$this->db->like('name', $txtsearch);
}
$total = $this->db->get('tbroom')->num_rows();
if ($txtsearch != '') {
$this->db->like('name', $txtsearch);
}
$this->db->limit($per_page, $offset);
$this->db->order_by('name', 'asc');
$query = $this->db->get('tbroom')->result_array();
$this->load->library('pagination');
$data['pagination'] = $this->pagination->pagin($total, $url, $per_page);
$data['query'] = $query;
return $data;
}
function get_roomname($room_id){
$room_name = '';
$this->db->where('id',$room_id);
$row = $this->db->get('tbroom')->row_array();
if(isset($row['id'])){
$room_name = $row['name'];
}
return $room_name;
}
/*
* ตรวจสอบห้องที่ต้องการว่างหรือไม่
* return true = ว่าง, false = ไม่ว่าง
*/
function room_busy($room_id, $rdate, $start_time, $end_time) {
$this->load->library('mydate');
$this->db->where('id', $room_id);
$room = $this->db->get('tbroom')->row_array();
$room_name = $room['name'];
$this->db->where('room_id', $room_id);
$this->db->where('rdate', $rdate);
$this->db->where('status <> ', '2');
$this->db->where('status <> ', '3');
$result = $this->db->get('tbreserv')->result_array();
$ret = $this->room_busy_process($result, $rdate, $start_time, $end_time, $room_name);
$msg = $ret['msg'];
$busy = $ret['busy'];
//ตรวจสอบที่ตาราง tbbooking ด้วย
if ($busy) {
$sql = "SELECT tbbooking.room_id, tbbooking.rdate, tbbooking.start_time, tbbooking.end_time,";
$sql.= " tbreserv.status, tbreserv.member_id FROM tbreserv Inner Join tbbooking ON tbbooking.doc_id = tbreserv.id";
$sql.= " Where tbbooking.room_id='$room_id' And tbbooking.rdate='$rdate' And tbreserv.status<>'2' And tbreserv.status<>'3'";
$result = $this->db->query($sql)->result_array();
$ret = $this->room_busy_process($result, $rdate, $start_time, $end_time, $room_name);
$msg = $ret['msg'];
$busy = $ret['busy'];
}
if ($busy) {
if ($this->db->table_exists('tmp_booking')) {
$this->db->where('room_id', $room_id);
$this->db->where('rdate', $rdate);
$result = $this->db->get('tmp_booking')->result_array();
$ret = $this->room_busy_process($result, $rdate, $start_time, $end_time, $room_name);
$msg = $ret['msg'];
$busy = $ret['busy'];
}
}
$data = array(
'msg' => $msg,
'busy' => $busy
);
return $data;
}
function room_busy_process($result, $rdate, $start_time, $end_time, $room_name) {
$msg = 'ห้องว่างสามารถจองได้';
$busy = true;
foreach ($result as $r) {
$member = $this->get_member($r['member_id']);
$member_txt = " <b>ผู้จอง:</b> ".$member['name']." ติดต่อ ".$member['mobile']." ,".$member['tel'];
$db_start_time = $r['start_time'];
$db_end_time = $r['end_time'];
if ($start_time >= $db_start_time) {
if ($start_time <= $db_end_time) {
if ($r['status'] == '0') {
$msg = "<b>$room_name</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
$busy = false;
} else if ($r['status'] == '1') {
$msg = "<b>$room_name</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
$busy = false;
}
}
} else if ($start_time <= $db_start_time && $end_time >= $db_start_time) {
if ($end_time <= $db_end_time) {
if ($r['status'] == '0') {
$msg = "<b>$room_name</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
$busy = false;
} else if ($r['status'] == '1') {
$msg = "<b>$room_name</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
$busy = false;
}
}
}
if ($start_time <= $db_start_time && $end_time >= $db_end_time) {
if ($r['status'] == '0') {
$msg = "<b>$room_name</b> ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " อยู่ระหว่างการรออนุมัติ ไม่สามารถจองได้ในขณะนี้ $member_txt <br/>";
$busy = false;
} else if ($r['status'] == '1') {
$msg = "<b>$room_name</b> ไม่ว่าง ณ .วันที่ " . $this->mydate->dateToText($rdate) . " ในช่วงเวลา " . $r['start_time'] . " - " . $r['end_time'] . " $member_txt <br/>";
$busy = false;
}
}
}
$data = array(
'msg' => $msg,
'busy' => $busy
);
return $data;
}
function get_member($member_id) {
$this->db->where('id', $member_id);
$row = $this->db->get('tbmember')->row_array();
$data = array();
if (isset($row['id'])) {
$data = array(
'name' => $row['name'],
'mobile' => $row['mobile'],
'tel' => $row['tel']
);
}
return $data;
}
/*
* ดึงรายการห้องที่ว่างทั้งหมดที่สามารถจองได้ ในช่วงวัน เวลาที่ต้องการ
* แต่จะยังไม่เช็คสถานะการกำหนดการจองล่วงหน้าว่าต้องจองก่อนกี่นาที
*/
function get_all_roombusy($rdate, $start_time, $end_time) {
$this->db->where('status', '0');
$query = $this->db->get('tbroom')->result_array();
$result = array();
foreach ($query as $r) {
$ret = $this->room_busy($r['id'], $rdate, $start_time, $end_time);
$hours_dif = $this->mydate->dateTimeDiff(date("Y-m-d H:i"), "$rdate $start_time");
$ok = true;
$ok_txt = '';
if ($r['pre_hours'] > 0) {
if ($hours_dif < $r['pre_hours']) {
$ok = false;
$ok_txt = 'จองล่วงหน้า ' . $r['pre_hours'] . " ชม.";
}
} else {
if ($hours_dif < $r['pre_hours']) {
$ok = false;
$ok_txt = "จองย้อนหลังไม่ได้";
}
}
if ($ret['busy']) {
$result[] = array(
'id' => $r['id'],
'name' => $r['name'],
'room_size' => $r['room_size'],
'sit_amount' => $r['sit_amount'],
'pre_hours' => $r['pre_hours'],
'ok' => $ok,
'ok_txt' => $ok_txt
);
}
}
return $result;
}
}
?>