<?php
/**
 * Reservation Management
 * @package     ci-htel hotel
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @license     New BSD Licence
 */
require_once (dirname(__FILE__) . "/ADMIN_Controller.php");

class Res extends ADMIN_Controller {

    function Res()
    {
        parent::ADMIN_Controller();
        $this->load->model('Common_model');
        $this->load->model('User_model');
        $this->load->model('Res_model');
        $this->load->model('Hotel_model');
    }
    
    public function index($hotelid)
    {
        $this->load->helper(array('form', 'url'));
        redirect('admin/res/page/' . $hotelid);
    }
    
    /**
     * 予約一覧
     */
    public function page($hotelid, $offset = 0)
    {
        $this->load->helper(array('form', 'url'));
        
        $limit = 20;    // 1ページ表示件数
        
        $this->db->select('reservations.resid, reservations.created, reservations.memberid, reservation_member.name, reservations.cin, reservations.cout, reservations.plan_num, reservations.room_num, reservations.total_price, reservations.status, plans.plan_name');
        $this->db->from('reservations');
        $this->db->where('reservations.hotelid', $hotelid);
        $this->db->order_by('reservations.created DESC');
        $this->db->join('reservation_member', 'reservation_member.resid = reservations.resid');
        $this->db->join('plans', 'plans.planid = reservations.planid');
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        
        $data['ress'] = $query->result_array();
        //var_dump($data);
        //$data['title'] = "Plans";
        //$data['headline'] = "";
        //$this->load->vars($data);
        
        $this->load->library('pagination');
        
        // 総レコード数の取得
        $total_rows = $this->db->count_all('reservations');
        
        $pager_config['uri_segment'] = 4;
        $pager_config['base_url'] = base_url() . 'admin/res/page/';
        $pager_config['total_rows'] = $total_rows;
        $pager_config['per_page'] = $limit;
        
        $this->pagination->initialize($pager_config);
        
        $data['links'] = $this->pagination->create_links();
        $this->load->vars($data);
        
        $this->load->view('admin/res/page');
    }
    
    /**
     * 予約詳細
     */
    public function detail($hotelid, $resid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data['hotelid'] = $hotelid;
        $data['resid'] = $resid;
        
        $this->db->where('hotelid', $hotelid);
        $query = $this->db->get('guesttypes');
        $guesttypes = array();
        foreach ($query->result_array() as $row)
        {
            $guesttypes[$row['guesttypeid']] = $row;
        }
        $data['guesttypes'] = $guesttypes;
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('resid', $resid);
        $query = $this->db->get('reservations', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        $data['reservation'] = $query->row_array();
        
        $planid = $data['reservation']['planid'];
        $this->db->where('hotelid', $hotelid);
        $this->db->where('planid', $planid);
        $query = $this->db->get('plans', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        $data['plan'] = $query->row_array();
        
        $roomtypeid = $data['reservation']['roomtypeid'];
        $this->db->where('hotelid', $hotelid);
        $this->db->where('roomtypeid', $roomtypeid);
        $query = $this->db->get('roomtypes', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        $data['roomtype'] = $query->row_array();
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('resid', $resid);
        $query = $this->db->get('reservation_member', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        $data['reservation_member'] = $query->row_array();
        $this->load->vars($data);
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('resid', $resid);
        $this->db->order_by('day DESC');
        $this->db->order_by('room_no DESC');
        $query = $this->db->get('reservation_detail');
        $data['reservation_detail'] = $query->result_array();
        $this->load->vars($data);
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('resid', $resid);
        $this->db->order_by('day DESC');
        $query = $this->db->get('reservation_options');
        $data['reservation_options'] = $query->result_array();
        $this->load->vars($data);
        
        $this->load->view('admin/res/detail');
    }
    
    /**
     * 予約ステータスを変更する。
     * 変更後、予約詳細画面に遷移する。
     */
    public function changeStatus($hotelid, $resid, $new_status)
    {
        $this->Hotel_model->load($hotelid);
        $this->load->helper(array('form', 'url'));
        
        $this->db->where('resid', $resid);
        $query = $this->db->get('reservation', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        
        $row = $query->row_array();
        $status = $row['status'];
        
        if ($new_status == 2)
        {
            /**
             * 仮予約->本予約
             */
            $this->Res_model->confirmRes($resid, $this->Hotel_model);
        }
        else if ($new_status == 0)
        {
            if ($status == 1)
            {
                // 仮予約 -> キャンセル
                $this->Res_model->cancel($resid, 2, $this->Hotel_model);
            }
            else if ($status == 2)
            {
                // 本予約 -> キャンセル
                $this->Res_model->cancel($resid, 3, $this->Hotel_model);
            }
        }
        
        redirect('admin/res/detail/' . $hotelid . '/' . $resid);
    }
}
/* End of file res.php */
