<?php
class Carbooking extends MY_Controller {
public $page_title = 'บันทึกจองใช้รถ';
function __construct() {
parent::__construct();
$this->load->model('db_m');
$this->load->model('script_m');
$this->load->library('mydate');
$this->load->model('car_m');
$s_imeeting['activemenu'] = '004';
$this->session->set_userdata('s_imeeting', $s_imeeting);
}
function index($success = '0') {
$this->create_sess_table();
$rconfig = $this->db->get('tbconfig')->row_array();
$data['sendemail'] = '0';
if (isset($rconfig['id'])) {
$data['sendemail'] = $rconfig['sendemail'];
}
$s_login = $this->session->userdata('s_login');
$this->db->where('id', $s_login['login_id']);
$member = $this->db->get('tbmember')->row_array();
$data['member'] = $member;
$data['extraHeadContent'] = $this->script_m->modal_window();
$data['extraHeadContent'].= $this->script_m->bootstrap_tab();
$data['extraHeadContent'].= $this->script_m->calendar();
$data['extraHeadContent'].= $this->script_m->formatCurrency();
$data['extraHeadContent'].= $this->script_m->textformat();
$data['timerange'] = $this->db->get('tbtimerange')->result_array();
$data['car'] = $this->db->get('tbcar')->result_array();
if ($success == '1') {
$data['msg_text'] = 'บันทึกรายการจองสำเร็จ';
}
$data['page_title'] = $this->page_title;
$data['content'] = 'car/carbooking_v';
$this->load->view('template_v', $data);
}
function news() {
redirect('carbooking');
}
//สร้างตารางชั่วคราวเพื่อเพิ่มจำนวนวันจอง
function create_tmp_table() {
$query = $this->db->query("DROP TEMPORARY TABLE IF EXISTS tmp_carbooking;");
$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS tmp_carbooking
(
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`car_id` INT NULL ,
`car_code` VARCHAR( 255 ) NULL ,
`rdate` DATE NULL ,
`start_time` VARCHAR( 10 ) NULL ,
`end_time` VARCHAR( 10 ) NULL
) ENGINE = MYISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
$this->db->query($sql);
return true;
}
//สร้างตารางชั่วคราวเพื่อเพิ่มจำนวนวันจอง
function create_sess_table($arr = array()) {
$this->session->set_userdata('tmp_carbooking', $arr);
return true;
}
//แสดงรายการจองรถวันเพิ่มเติม
function ajax_get_grid_tmp() {
$data['mydate'] = $this->mydate;
$data['query'] = $this->session->userdata('tmp_carbooking');
$this->load->view('car/carbooking_grid_tmp_v', $data);
}
//ลบรายการจองห้องประชุมวันเพิ่มเติม
function ajax_delete_tmp() {
$id = $this->input->post('id');
$tmp_carbooking = $this->session->userdata('tmp_carbooking');
unset($tmp_carbooking[$id]);
$this->create_sess_table($tmp_carbooking);
echo true;
}
function ajax_timerange() {
$start_time = $this->input->post('start_time');
$this->db->where('wtime > ', $start_time);
$data = $this->db->get('tbtimerange')->result_array();
echo json_encode($data);
}
function ajax_checkbusy() {
$car_id = $this->input->post('car_id');
$rdate = $this->input->post('rdate');
$start_time = $this->input->post('start_time');
$end_time = $this->input->post('end_time');
$ret = $this->car_m->car_busy($car_id, $this->mydate->dateToMySql($rdate), $start_time, $end_time);
$data = array(
'msg' => $ret['msg'],
'busy' => $ret['busy']
);
echo json_encode($data);
}
function ajax_save_tmp() {
$tmp_carbooking = $this->session->userdata('tmp_carbooking');
$tmp_carbooking[] = array(
'car_id' => $this->input->post('car_id'),
'car_code' => $this->input->post('car_code'),
'rdate' => $this->mydate->dateToMySql($this->input->post('rdate')),
'start_time' => $this->input->post('start_time'),
'end_time' => $this->input->post('end_time')
);
$this->create_sess_table($tmp_carbooking);
}
//ตรวจสอบห้องว่างอีกครั้ง เผื่อมีการจองเข้ามาในช่วงวินาทีนั้น ก่อน SAVE
function ajax_validate() {
$car_id = $this->input->post('car_id');
$rdate = $this->mydate->dateToMySql($this->input->post('rdate'));
$start_time = $this->input->post('start_time');
$end_time = $this->input->post('end_time');
//ตรวจสอบห้องว่างอีกครั้ง เผื่อมีการจองเข้ามาในช่วงวินาทีนั้น
$busy_txt = '';
$ret = $this->car_m->car_busy($car_id, $rdate, $start_time, $end_time);
if (!$ret['busy'])
$busy_txt.=$ret['msg'] . " \n";
//ตรวจสอบตาราง tmp
$tmp_carbooking = $this->session->userdata('tmp_carbooking');
foreach ($tmp_carbooking as $r) {
$ret = $this->car_m->car_busy($r['car_id'], $r['rdate'], $r['start_time'], $r['end_time']);
if (!$ret['busy'])
$busy_txt.=$ret['msg'] . " \n";
}
$data['busy_txt'] = $busy_txt;
echo json_encode($data);
}
function save() {
$this->load->model('email_m');
$this->db->trans_begin();
$car_id = $this->input->post('car_id');
$rdate = $this->mydate->dateToMySql($this->input->post('rdate'));
$start_time = $this->input->post('start_time');
$end_time = $this->input->post('end_time');
$s_login = $this->session->userdata('s_login');
$login_id = $s_login['login_id'];
$data = array(
'cdate' => date('Y-m-d H:i:s'),
'name' => $this->input->post('name'),
'car_id' => $car_id,
'rdate' => $rdate,
'start_time' => $start_time,
'end_time' => $end_time,
'amount_people' => $this->input->post('amount_people'),
'employee' => $this->input->post('employee'),
'detail' => $this->input->post('detail'),
'contact_name' => $this->input->post('contact_name'),
'contact_tel' => $this->input->post('contact_tel'),
'member_id' => $login_id
);
$this->db->insert('tbcarreserv', $data);
$id = $this->db->insert_id();
//บันทึกข้อมูลเพิ่มวันจองลง tbcarbooking โดย loop จากตารางชั่วคราว
$tmp_carbooking = $this->session->userdata('tmp_carbooking');
foreach ($tmp_carbooking as $r) {
$data = array(
'doc_id' => $id,
'car_id' => $r['car_id'],
'rdate' => $r['rdate'],
'start_time' => $r['start_time'],
'end_time' => $r['end_time']
);
$this->db->insert('tbbooking_car', $data);
}
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
} else {
$this->db->trans_commit();
if ($this->email_m->check_sendmail()) {
//********************* ส่งเมลล์ ********************************
//ส่งเมลล์รายการจอง 1 ฉบับ ต่อ 1 ผู้อนุมัติ
$subject = 'รายการจองใช้รออนุมัติ';
$message = '<b>จองใช้เพื่อ</b> ' . $this->input->post('name') . "<br/>";
$message.= '<b>วันที่จอง</b> ' . $this->mydate->dateTotext($rdate) . " <b>เวลา</b> " . $start_time . " <b>ถึง</b> " . $end_time;
$message.= " <b>ทะเบียน </b> " . $this->car_m->get_carcode($car_id) . "<br/>";
$this->db->where('doc_id', $id);
$this->db->where('wtype', '0');
$booking = $this->db->get('tbbooking_car')->result_array();
foreach ($booking as $b) {
$message.= "วันที่ </b>" . $this->mydate->dateToText($b['rdate']) . " <b>เวลา</b> " . $b['start_time'] . " <b>ถึง</b> " . $b['end_time'];
$message.= " <b>ทะเบียน</b> " . $this->car_m->get_carcode($b['car_id']) . "<br/>";
}
$this->db->where('id', $login_id);
$member = $this->db->get('tbmember')->row_array();
$message.= "<b>ผู้จอง</b> " . $member['name'] . " <b>ติดต่อ</b> " . $member['mobile'] . ", " . $member['tel'];
$this->db->where('depart_id', $member['depart_id']);
$query = $this->db->get('tbmembertodepart')->result_array();
foreach ($query as $r) {
$this->db->where('id', $r['member_id']);
$member_approve = $this->db->get('tbmember')->row_array();
if (isset($member_approve['id'])) {
if ($member_approve['email'] != '') {
$this->email_m->sendmail($member['email'], 'ระบบ iMeeting', $member_approve['email'], $subject, $message, $member['id']);
}
}
}
}
}
redirect('carbooking/index/1');
}
}
?>