<?php
/**
 * Planprices Management
 * 
 */

require_once (dirname(__FILE__) . "/ADMIN_Controller.php");


/**
 *
 */
class Planprices extends ADMIN_Controller {
    
    function Planprices()
    {
        parent::ADMIN_Controller();
        $this->load->model('User_model');
    }
    
    /**
     * プラン料金の設定
     */
    public function index($hotelid, $planid)
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $data['hotelid'] = $hotelid;
        $data['planid'] = $planid;
        $this->load->vars($data);

        // 日タイプの取得
        $this->db->where('status', 1);
        $this->db->where('hotelid', $hotelid);
        $query = $this->db->get('daytypes');
        $daytypes = $query->result_array();
        $data['daytypes'] = $daytypes;
        $this->load->vars($data);
        
        // プランが対応している客タイプ
        $data = array();
        $this->db->select('*');
        $this->db->from('guesttypes');
        $this->db->join('plan_to_guesttypes', 
            'plan_to_guesttypes.hotelid = guesttypes.hotelid AND '
            . 'plan_to_guesttypes.guesttypeid = guesttypes.guesttypeid');
        $this->db->where('guesttypes.hotelid', $hotelid);
        $this->db->where('plan_to_guesttypes.planid', $planid);
        $this->db->order_by('ordering');
        $query = $this->db->get();
        $guesttypes = $query->result_array();
        $data['guesttypes'] = $guesttypes;
        $this->load->vars($data);
        
        if (isset($_POST['submit']))
        {
            // 一旦、削除する。
            $this->db->where('hotelid', $hotelid);
            $this->db->where('planid', $planid);
            $this->db->delete('plan_prices');
            
            $now = date("Y-m-d H:i:s");
            
            // 値が入力されていれば、追加する。
            foreach ($guesttypes as $guesttype)
            {
                $guesttypeid = $guesttype['guesttypeid'];
                foreach ($daytypes as $daytype)
                {
                    $daytypeid = $daytype['daytypeid'];
                    $index = $guesttypeid . "_" . $daytypeid;
                    if (isset($_POST['price'][$index]))
                    {
                        $val = trim($_POST['price'][$index]);
                        if ($val != "")
                        {
                            $data = array(
                                'hotelid' => $hotelid,
                                'planid' => $planid,
                                'daytypeid' => $daytypeid,
                                'guesttypeid' => $guesttypeid,
                                'created' => $now,
                                'modified' => $now,
                                'price' => $val);
                            $this->db->insert('plan_prices', $data);
                        }
                    }
                }
            }
        }
        $price = array();
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('planid', $planid);
        $query = $this->db->get('plan_prices');
        foreach ($query->result() as $row)
        {
            $index = $row->guesttypeid . '_' . $row->daytypeid;
            $price[$index] = $row->price;
        }
        $data['price'] = $price;
        $this->load->vars($data);

        $this->load->view('admin/planprices/index');
    }
}
/* End of file planprices.php */
