<?php

class form_model extends CI_Model {

    var $COLI_WebCode = 'CHT'; //订单来源站点代号
    var $COLI_Servicetype = 'T'; //服务类型 T线路 4游船
    var $COLI_sourcetype = '32002'; //订单类型，楼下有一个表
    var $COLI_SenderIP = ''; //客人的IP地址
    var $COLI_Area = '2'; //市场区域 2欧美市场 9日本市场
    var $COLI_Currency = 'USD'; //结算货币 USD美元 RMB人民币 RUB卢布 EUR欧元
    var $COLI_CustomerType = '101'; //所属小组ID 楼下有个表
    var $COLI_department = '1'; //所属小组SN 楼下有个表
    var $COLI_OrderDetailText = ''; //订单预定信息，保存所有POST数据
    var $COLI_ID = ''; //订单号，根据$COLI_SN获取
    var $COLI_SN = ''; //订单表主键
    var $COLI_Name = ''; //订单名称
    var $COLI_Sended = 1; //是否预定newsletter
    var $COLI_TouristLGC = "102001"; //导游语种
    var $COLI_PersonNum = '0'; //预定人数，成人
    var $COLI_ChildNum = '0'; //预定人数，小孩
    var $COLI_BabyNum = '0'; //预定人数，婴儿
    var $COLI_OrderStartDate = ''; //行程开始时间
    var $COLI_OrderEndDate = ''; //行程结束时间
    var $COLI_ApplyDate = ''; //订单提交日期
    var $COLI_InterestRate = '0.2'; //利率
    var $COLI_TrueCardRate = '0.025'; //信用卡手续费
    var $COLI_CustomerClass = '3001'; //结算类型 3001综合服务
    var $COLI_OrderType = '19001'; //线路类型 19001散客定制
    var $COLI_PayManner = '123001'; //付款方式 123001信用卡
    var $COLI_GroupType = '19001'; //订单类型
    var $COLI_Days = '0'; //行程时间
    var $COLI_ProductSN = '';
    var $COLI_Keywords = ''; //关键字
    var $COLI_Memo = NULL;
    var $COLI_OrderPrice = NULL; //订单总价
    var $COLI_OPI_SN = NULL;
    var $COLI_OrderSource = NULL; //订单来源设备 PC:62001 Table:62002 mobile 62003
    var $MEI_SN = ''; //联系人表主键
    var $MEI_FirstName = ''; //用户名
    var $MEI_LastName = ''; //用户名
    var $MEI_MailList = ''; //邮箱
    var $MEI_Mail = ''; //备用邮箱
    var $MEI_Nationality = '0'; //国家SN
    var $MEI_Phone = ''; //联系电话
    var $MEI_Gender = ''; //性别  100003女 100001男
    var $MEI_ServiceLang = ''; //导游语种SN
    var $MEI_Country = ''; //联系人国家SN
    var $MEI_Birthday = ''; //客人生日

    function __construct() {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    //查询
    public function get_froms($from_sn = false, $form_web = false) {


        $sql = "SELECT   form_sn,
                         form_name,
                         form_structure,
                         form_web_structure,
                         form_status,
                         form_user,
                         form_web,
                         form_type,
                         form_date,
                         form_udate,
                         OPI_Name
                FROM     FormDesign
                         LEFT JOIN OperatorInfo ON form_user=OPI_SN
                WHERE 1=1 
                ";
        empty($from_sn) ? '' : $sql .= " AND form_sn= " . $this->HT->escape($from_sn);
        empty($form_web) ? '' : $sql.=' AND form_web= ' . $this->HT->escape($form_web);
        $sql.=' ORDER BY form_udate desc ';
        $query = $this->HT->query($sql);

        if (!empty($from_sn)) {
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            }
        }
        return $query->result();
    }

    //更新
    public function update($form_sn, $form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type) {
        $sql = "UPDATE  FormDesign
                   SET  form_name=?,
                        form_structure=N?,
                        form_web_structure=N?,
                        form_status=?,
                        form_user=?,
                        form_web=?,
                        form_type=?,
                        form_udate=GETDATE()
                 WHERE  form_sn = ?";
        $result = $this->HT->query($sql, array($form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type, $form_sn));
        return $result;
    }

    //更新
    public function stop($form_sn, $form_status) {
        $sql = "UPDATE  FormDesign SET  form_status=? WHERE  form_sn = ?";
        $result = $this->HT->query($sql, array($form_status, $form_sn));
        return $result;
    }

    //新增
    public function add($form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type) {
        $sql = "INSERT INTO FormDesign (
                             form_name,
                             form_structure,
                             form_web_structure,
                             form_status,
                             form_user,
                             form_web,
                             form_type,
                             form_date,
                             form_udate
                    ) VALUES (?,N?,N?,?,?,?,?,GETDATE(),GETDATE())";
        $query = $this->HT->query($sql, array($form_name, $form_structure, $form_web_structure, $form_status, $form_user, $form_web, $form_type));
        return $this->HT->last_id('FormDesign');
    }

    //删除
    public function delete($form_sn) {
        $sql = "DELETE FROM FormDesign WHERE  form_sn = ?";
        $result = $this->HT->query($sql, array($form_sn));
        return $result;
    }

    function MakeOrderNumber() {
        return str_replace('.', '', sprintf('%11.4f', gettimeofday(TRUE)));
    }

    //订单处理

    function TourOrderSave() {
        //订单表
        $sql = "INSERT INTO ConfirmLineInfoTmp (
                    COLI_Keywords, 
                    COLI_ID, 
                    COLI_WebCode, 
                    COLI_Servicetype, 
                    COLI_sourcetype, 
                    COLI_SenderIP, 
                    COLI_Area, 
                    COLI_Currency, 
                    COLI_CustomerType, 
                    COLI_department,
                    COLI_OrderDetailText, 
                    COLI_Name, 
                    COLI_Sended, 
                    COLI_PersonNum, 
                    COLI_ChildNum,
                    COLI_BabyNum, 
                    COLI_OrderStartDate, 
                    COLI_OrderEndDate, 
                    COLI_InterestRate, 
                    COLI_TrueCardRate, 
                    COLI_CustomerClass, 
                    COLI_OrderType, 
                    COLI_PayManner,
                    COLI_TouristLGC,
                    COLI_GroupType,
                    COLI_Days,
                    COLI_ProductSN, 
                    COLI_ApplyDate,
                    COLI_OPI_ID,
                    COLI_Memo,
                    COLI_OrderPrice,
                    COLI_OrderSource
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, N?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,GETDATE(),?,?,?,?)";
        $query = $this->HT->query($sql, array($this->COLI_Keywords, $this->COLI_ID, $this->COLI_WebCode, $this->COLI_Servicetype, $this->COLI_sourcetype, $this->COLI_SenderIP, $this->COLI_Area, $this->COLI_Currency, $this->COLI_CustomerType, $this->COLI_department, $this->COLI_OrderDetailText, $this->COLI_Name, $this->COLI_Sended, $this->COLI_PersonNum, $this->COLI_ChildNum, $this->COLI_BabyNum, $this->COLI_OrderStartDate, $this->COLI_OrderEndDate, $this->COLI_InterestRate, $this->COLI_TrueCardRate, $this->COLI_CustomerClass, $this->COLI_OrderType, $this->COLI_PayManner, $this->COLI_TouristLGC, $this->COLI_GroupType, $this->COLI_Days, $this->COLI_ProductSN, $this->COLI_OPI_SN, $this->COLI_Memo, $this->COLI_OrderPrice, $this->COLI_OrderSource));
        $this->COLI_SN = $this->HT->last_id('ConfirmLineInfoTmp');
        //print_r($this->HT->queries);
        //客人信息表
        $sql = "INSERT INTO MEmberInfoTmp (
                    MEI_FirstName, 
                    MEI_LastName, 
                    MEI_MailList, 
                    MEI_Nationality, 
                    MEI_Phone,
                    MEI_Gender, 
                    MEI_ServiceLang, 
                    MEI_Country, 
                    MEI_Birthday,
                    MEI_Mail, 
                    CreateDate 
                ) VALUES (N?, N?, ?, ?, ?, ?, ?, ?, ?,?, GETDATE())";
        $query = $this->HT->query($sql, array($this->MEI_FirstName, $this->MEI_LastName, $this->MEI_MailList, $this->MEI_Nationality, $this->MEI_Phone, $this->MEI_Gender, $this->MEI_ServiceLang, $this->MEI_Nationality, $this->MEI_Birthday, $this->MEI_Mail));
        $this->MEI_SN = $this->HT->last_id('MEmberInfoTmp');

        //订单与客人关联
        $sql = "INSERT INTO CUstomerListTmp  ( CUL_COLI_SN, CUL_CUI_SN, CreateDate ) VALUES (?, ?, GETDATE())";
        $query = $this->HT->query($sql, array($this->COLI_SN, $this->MEI_SN));
        $CUL_SN = $this->HT->last_id('CUstomerListTmp');

        return $CUL_SN;
    }

}
