<?php

/*!
 * 这里操作的都是商务表, 传统订单的表不在这里
 */

class Orders_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
        //读取默认配置
        $this->COLI_WebCode = $this->config->item('Site_Code');
        $this->COLI_Area = $this->config->item('Site_Area');
        $this->COLI_CustomerType = $this->config->item('Site_DepartmentID');
        $this->COLI_department = $this->config->item('Site_Department');
        $this->COLI_Currency = $this->config->item('Site_Currency');
        $this->COLI_InterestRate = $this->config->item('Site_InterestRate');
        $this->COLI_TrueCardRate = $this->config->item('Site_TrueCardRate');
        $this->COLI_TouristLGC = $this->config->item('Site_ServiceLGC');
        $this->COLI_OrderStartDate = null;
        $this->COLI_Keywords = NULL;
        switch ($this->check_device()) {
            case 'mobile':
                $this->COLI_OrderSource = '62003';
                break;
            case 'tablet':
                $this->COLI_OrderSource = '62002';
                break;
            default:
                $this->COLI_OrderSource = '62001';
        }
    }

    public function get_orderinfo_detail($COLI_ID)
    {
        $sql = "SELECT top 1 coli.COLI_ID,
                       coli.COLI_Department,
                       cold.COLD_ServiceSN,
                       cold.COLD_ServiceSN2,
                       cold.COLD_ServiceCity,
                       gut.GUT_NationalityID,
                       *
                FROM BIZ_ConfirmLineInfo coli
                INNER JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_COLI_SN=coli.COLI_SN
                LEFT JOIN BIZ_GUEST gut ON gut.GUT_SN=coli.COLI_GUT_SN
                WHERE coli.COLI_ID='$COLI_ID'
                ORDER BY COLI_ApplyDate DESC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_guestlist($COLD_SN_str)
    {
        $sql = "SELECT *
                FROM BIZ_BookPeopleList BPL
                INNER JOIN BIZ_BookPeople BPE ON BPL_BPE_SN=BPE_SN
                WHERE BPL_COLD_SN IN ($COLD_SN_str)";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    /*!
     * 获取行程详情
     */
    /** 根据订号 */
    public function get_scheduleDetails($COLD_SN_str)
    {
        $sql = "SELECT *
                FROM BIZ_PackageInfo2 pag2
                INNER JOIN BIZ_PackageInfo pag ON pag.PAG_SN=pag2.PAG2_PAG_SN
                INNER JOIN CItyInfo2 cii2 on CII2_CII_SN=PAG_CII_SN and CII2_LGC=2
                INNER JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_ServiceSN=pag2.PAG2_PAG_SN AND (PAG2_LGC = 2)
                left join BIZ_PackageInfoSub pis on pis.PAGS_PAG_SN=pag.PAG_SN and PAGS_LGC=1 and cold.COLD_ServiceSN2=PAGS_SN
                WHERE COLD_SN IN ($COLD_SN_str)";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    /** 根据线路代号 */
    public function get_packageDetails($pag_code_str)
    {
        $sql = "SELECT *
                FROM BIZ_PackageInfo2 pag2
                INNER JOIN BIZ_PackageInfo pag ON pag.PAG_SN=pag2.PAG2_PAG_SN and pag2.PAG2_LGC=2 and pag.PAG_DEI_SN=30
                INNER JOIN CItyInfo2 cii2 on CII2_CII_SN=PAG_CII_SN and CII2_LGC=2
                WHERE pag.PAG_Code IN ($pag_code_str)
                order by pag.PAG_Code ";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_paymentDetails($COLI_ID)
    {
        $sql = "SELECT *
                FROM BIZ_GroupAccountInfo bgai
                INNER JOIN BIZ_ConfirmLineInfo coli ON bgai.GAI_COLI_SN=coli.COLI_SN
                WHERE coli_ID = '$COLI_ID'";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_groupCodeList($GroupCodeList_str)
    {
        $sql = "SELECT GRI_No
                FROM GRoupInfo gri
                INNER JOIN BIZ_ConfirmLineInfo coli ON coli.COLI_GRI_SN=gri.GRI_SN
                WHERE gri.GRI_No IN ($GroupCodeList_str)";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_packageSN($pag_code)
    {
        $sql = "SELECT top 1 PAG2_SN,PAG_CII_SN,PAG2_PAG_SN
                FROM BIZ_PackageInfo2 pag2
                INNER JOIN BIZ_PackageInfo pag ON pag.PAG_SN=pag2.PAG2_PAG_SN
                WHERE pag.PAG_Code = '$pag_code' and pag2.PAG2_LGC=2 and pag.PAG_DEI_SN=30
                ";
        $query = $this->HT->query($sql);
        if ($query->row()) {
            return $query->row();
        }
        return NULL;
    }

    /*!
     * 需要更新调度信息的订单 -- 商务表
     * @param  integer    $coli_sn   [description]
     * @param  [type]     $startDate [description]
     * @param  [type]     $endDate   [description]
     */
    public function get_groupCombineInfo($coli_sn=0, $get_vendorID=null, $startDate=null, $endDate=NULL)
    {
        $createTime_format = date('H')>12 ? 'Y-m-d 13:00:00' : 'Y-m-d 00:00:00';
        $sql = "SELECT top 1 coli.COLI_ID, coli.COLI_SN, coli.COLI_GRI_SN, cold.COLD_SN, coli.COLI_OrderDetailText, coli.COLI_Memo,coli.COLI_State,coli.COLI_OPI_ID,
        cold.COLD_PlanVEI_SN, cold.COLD_MemoText, gci.*,'0' as 'isHistory'
                FROM GroupCombineInfo gci
                LEFT JOIN BIZ_ConfirmLineInfo coli ON coli.COLI_GRI_SN=gci.GCI_GRI_SN
                --and coli.COLI_State NOT IN ('30','40','50')
                  and coli.COLI_State<>50
                  and (select OPI_DEI_SN from OperatorInfo where OPI_SN=coli.COLI_OPI_ID)=30
                LEFT JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_COLI_SN=coli.COLI_SN
                WHERE 1=1 ";
        if ($coli_sn !== 0) {
            $sql .= " and coli.COLI_SN='$coli_sn' ";
        }
        if ($get_vendorID !== null) {
            $sql .= " and GCI_VendorOrderId='$get_vendorID' ";
        }
        if ($startDate !== NULL) {
            $sql .= " and (
                        gci.GCI_travelDate between '$startDate' and '$endDate'
                        OR
                        CONVERT(DATE, COLI_ApplyDate) = CONVERT(DATE, GETDATE())
                      )
                      and gci.GCI_createTime < '" . date($createTime_format) . "' ";
        }
        // 近期的订单同步完成之后, 同步历史数据
        // 这个sql很慢, 导致锁表, 分开处理
        // $sql .= " UNION ALL ";
        // $sql .= " SELECT top 1 coli.COLI_ID, coli.COLI_SN, coli.COLI_GRI_SN, cold.COLD_SN, coli.COLI_OrderDetailText, coli.COLI_Memo,coli.COLI_State,coli.COLI_OPI_ID,
        // cold.COLD_PlanVEI_SN, cold.COLD_MemoText, gci.*,'1' as 'isHistory'
        //         from GroupCombineInfo gci
        //         inner join GRoupInfo on GRI_SN=GCI_GRI_SN and GRI_No<>''
        //         LEFT JOIN BIZ_ConfirmLineInfo coli ON coli.COLI_GRI_SN=gci.GCI_GRI_SN
        //           and coli.COLI_State<>50
        //           and (select OPI_DEI_SN from OperatorInfo where OPI_SN=coli.COLI_OPI_ID)=30
        //         LEFT JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_COLI_SN=coli.COLI_SN
        //         where
        //         GCI_combineNo is not null and GCI_combineNo not in ('cancel','forbidden')
        //         and GCI_leaveDate < '" . date('Y-m-d', strtotime("-7 days")) . "'
        //         and gci.GCI_createTime < '" . date('Y-m-d') . "'
        //         and GCI_combineNo not like '%取消%'
        //         ";
        $sql .= " ORDER BY isHistory ASC, GCI_createTime ASC ";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_groupCombineInfo_finance()
    {
      // return array(); // 历史数据已获取完毕
      $to_update_month_begin = date('Y-m-01', strtotime("-2 months", strtotime(date("Y-m-d"))));
      $to_update_month_end = date('Y-m-01', strtotime("-1 months", strtotime(date("Y-m-d"))));
      $end_d = strtotime($to_update_month_end)-1;
      $to_update_month_end = date("Y-m-d 23:59:59", $end_d);
      $set_time = date("Y-m-d 00:00:00", strtotime("+50 days",$end_d));
      $last_update_time = date("Y-m-d 00:00:00");
      $sql = " SELECT top 1 coli.COLI_ID, coli.COLI_SN, coli.COLI_GRI_SN, cold.COLD_SN, coli.COLI_OrderDetailText, coli.COLI_Memo,coli.COLI_State,coli.COLI_OPI_ID,
      cold.COLD_PlanVEI_SN, cold.COLD_MemoText, gci.*,'1' as 'isHistory'
              from GroupCombineInfo gci
              inner join GRoupInfo on GRI_SN=GCI_GRI_SN and GRI_No<>''
              LEFT JOIN BIZ_ConfirmLineInfo coli ON coli.COLI_GRI_SN=gci.GCI_GRI_SN
              LEFT JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_COLI_SN=coli.COLI_SN
              where
              GCI_combineNo is not null and GCI_combineNo not in ('cancel','forbidden')
              and coli.COLI_State<>50
              and (select OPI_DEI_SN from OperatorInfo where OPI_SN=coli.COLI_OPI_ID)=30
              -- 已生成账单的不再自动同步
              and NOT EXISTS (
                SELECT 1 FROM report_order WHERE ordernumber=COLI_ID AND orderstats=1
              )
              and GCI_travelDate between '$to_update_month_begin' and '$to_update_month_end'
              and gci.GCI_createTime < '$set_time'
              and GCI_combineNo not like '%取消%'
              ";
      $sql .= " ORDER BY isHistory ASC,GCI_createTime ASC ";
      $query = $this->HT->query($sql);
      return $query->result();
    }

    /*!
     * 获取团计划信息的记录 - 根据地接社订单id
     * 计划变更和订单状态确认时使用
     * @param  integer     $vendorOrderId 图兰朵地接社系统的订单id
     */
    public function get_vendorarrangestate_byVendor($vendorOrderId, $vei_sn)
    {
      $sql = "SELECT coli.COLI_ID, coli.COLI_SN,opi.OPI_Email,opi.OPI_Name,
                     vas.VAS_ChangeText, vas.VAS_ConfirmInfo, vas.VAS_SN
              FROM GroupCombineInfo gci
              INNER JOIN BIZ_ConfirmLineInfo coli ON gci.GCI_GRI_SN=coli.COLI_GRI_SN
              INNER JOIN VendorArrangeState vas ON vas.VAS_GRI_SN=coli.COLI_GRI_SN
              LEFT JOIN OperatorInfo opi ON opi.OPI_SN=coli.COLI_OPI_ID
              WHERE gci.GCI_VendorOrderId=? and vas.VAS_VEI_SN=? and gci.GCI_VEI_SN=? ";
      $query = $this->HT->query($sql, array($vendorOrderId, $vei_sn, $vei_sn));
      return $query->result();
    }
    /*!
     * 获取团计划信息的记录 - 根据组团社团号 - 商务订单
     * 计划变更和订单状态确认时使用
     * @param  string      $groupCode 团号
     * @param  integer     $vei_sn    HT系统中供应商key
     */
    public function get_vendorarrangestate_byGroup($groupCode, $vei_sn)
    {
      $sql = "SELECT coli.COLI_ID, coli.COLI_SN,opi.OPI_Email,opi.OPI_Name,
                     vas.VAS_ChangeText, vas.VAS_ConfirmInfo, vas.VAS_SN
              FROM GRoupInfo gri
              INNER JOIN BIZ_ConfirmLineInfo coli ON gri.GRI_SN=coli.COLI_GRI_SN
              INNER JOIN VendorArrangeState vas ON vas.VAS_GRI_SN=coli.COLI_GRI_SN
              LEFT JOIN OperatorInfo opi ON opi.OPI_SN=coli.COLI_OPI_ID
              WHERE vas.VAS_VEI_SN=? and gri.GRI_No LIKE '%$groupCode%'";
      $query = $this->HT->query($sql, array($vei_sn));
      return $query->result();
    }
    /*!
     * 获取团计划信息的记录 - 根据组团社团号 - 传统订单
     * 计划变更和订单状态确认时使用
     * @param  string      $groupCode 团号
     * @param  integer     $vei_sn    HT系统中供应商key
     */
    public function get_vendorarrangestate_byGroup_T($groupCode, $vei_sn)
    {
      $sql = "SELECT coli.COLI_ID, coli.COLI_SN,opi.OPI_Email,opi.OPI_Name,
                     vas.VAS_ChangeText, vas.VAS_ConfirmInfo, vas.VAS_SN
              FROM GRoupInfo gri
              INNER JOIN ConfirmLineInfo coli ON gri.GRI_SN=coli.COLI_GRI_SN
              INNER JOIN VendorArrangeState vas ON vas.VAS_GRI_SN=coli.COLI_GRI_SN
              LEFT JOIN OperatorInfo opi ON opi.OPI_SN=coli.COLI_OPI_ID
              WHERE vas.VAS_VEI_SN=? and gri.GRI_No LIKE '%$groupCode%'";
      $query = $this->HT->query($sql, array($vei_sn));
      return $query->result();
    }

    /*!
     * 生成预定传真
     */
    public function sp_biz_arrange()
    {
      $sql = "SP_BIZ_Arrange ?";
      $query = $this->HT->query($sql, array($this->GRI_SN));
      return $query->result();
    }

    public $GRI_SN=0; // 团号
    public $GRI_No; // 团号
    public $GRI_OrderType; // 订单类型
    public $GRI_Name;   // 团名
    public $GRI_PersonNum;  // 人数
    public $GRI_Days;   // 行程天数
    public $GRI_IsCancel=0;
    public $GRI_DeleteFlag=0;
    public $GRI_OPI_ID=0;
    public $GRI_operator=0;
    public $GRI_Creator=0;
    public $GRI_CreateDate=0;
    /** 团信息 */
    public function groupinfo_save()
    {
        $sql = "INSERT INTO GRoupInfo
                       (GRI_No
                       ,GRI_Name
                       ,GRI_PersonNum
                       ,GRI_Days
                       ,GRI_IsCancel
                       ,DeleteFlag
                       ,GRI_OPI_ID
                       ,GRI_operator
                       ,GRI_Creator
                       ,GRI_CreateDate
                       ,GRI_OrderType)
                 VALUES (N?,N?,?,?,?,?,?,?,?,GETDATE(),?)";
        $query = $this->HT->query($sql, array(
            $this->GRI_No,
            $this->GRI_Name,
            $this->GRI_PersonNum,
            $this->GRI_Days,
            $this->GRI_IsCancel,
            $this->GRI_DeleteFlag,
            $this->GRI_OPI_ID,
            $this->GRI_operator,
            $this->GRI_Creator,
            $this->GRI_OrderType
        ));
        $this->GRI_SN = $this->HT->query("select MAX(GRI_SN) as insert_id FROM GRoupInfo WHERE GRI_operator=435 AND GRI_No='" . $this->GRI_No . "'")->row('insert_id');
        return $this->GRI_SN;
    }

    public $GCI_SN;
    public $GCI_VEI_SN;
    public $GCI_combineNo='';   // 拼团团号
    public $GCI_GRI_SN;    // 团key
    public $GCI_VendorOrderId;  // 地接社系统订单id
    public $GCI_FromAgc;  // 组团社来源
    public $GCI_groupType;  // 组团社来源
    public $GCI_travelDate;
    public $GCI_leaveDate;
    public $GCI_createTime;
    /** 目的地订单 拼团信息 */
    public function biz_groupcombineinfo_save()
    {
        $sql = "IF NOT EXISTS(
                   SELECT TOP 1 1
                   FROM   GroupCombineInfo
                   WHERE  GCI_VendorOrderId = ? and GCI_GRI_SN=? and GCI_VEI_SN=?
               )
               INSERT INTO GroupCombineInfo
                       (GCI_combineNo
                       ,GCI_GRI_SN
                       ,GCI_VEI_SN
                       ,GCI_VendorOrderId
                       ,GCI_FromAgc
                       ,GCI_groupType
                       ,GCI_travelDate
                       ,GCI_leaveDate
                       ,GCI_createTime)
                 VALUES
                       (N?
                       ,?
                       ,?
                       ,?
                       ,N?
                       ,?
                       ,?
                       ,?
                       ,?)
                       ";
        $query = $this->HT->query($sql, array(
            $this->GCI_VendorOrderId
           ,$this->GCI_GRI_SN
           ,$this->GCI_VEI_SN
           ,$this->GCI_combineNo
           ,$this->GCI_GRI_SN
           ,$this->GCI_VEI_SN
           ,$this->GCI_VendorOrderId
           ,$this->GCI_FromAgc
           ,$this->GCI_groupType
           ,$this->GCI_travelDate
           ,$this->GCI_leaveDate
           ,$this->GCI_createTime
        ));
        $this->GCI_SN = $this->HT->query("select MAX(GCI_SN) as insert_id FROM GroupCombineInfo WHERE GCI_combineNo='" . $this->GCI_combineNo . "'")->row('insert_id');
        return $this->GCI_SN;
    }

    public function biz_groupcombineoperationdetail_cut($combineNo, $vei_sn)
    {
      $sql = "DELETE
              FROM GroupCombineOperationDetail
              WHERE GCOD_GCI_combineNo = N? and GCOD_VEI_SN=? ";
      $query = $this->HT->query($sql, array($combineNo, $vei_sn));
      return $query;
    }

    /*!
     * 暂时没用
     */
    public function combineoperation_exist($combineNo='', $operation="")
    {
        if( ! $combineNo) { return array(); }
        $sql = "SELECT TOP 1 GCOD_GCI_combineNo
                   FROM   GroupCombineOperationDetail
                   WHERE  GCOD_GCI_combineNo = N? and GCOD_operationType=?";
        $query = $this->HT->query($sql, array(
            $combineNo,$operation
        ));
        return $query->result();
    }
    public $GCOD_SN;
    public $GCOD_VEI_SN;
    public $GCOD_GCI_combineNo = '';
    public $GCOD_operationType = '';
    public $GCOD_subType = '';
    public $GCOD_title = '';
    public $GCOD_dutyName = '';
    public $GCOD_dutyTel;
    public $GCOD_dutyPhoto;
    public $GCOD_startDate;
    public $GCOD_endDate;
    public $GCOD_useNum=1;
    public $GCOD_sumMoney;
    public $GCOD_standard = '';
    public $GCOD_carLicense = '';
    public $GCOD_remark = '';
    public $GCOD_creatTime;
    public function biz_groupcombineoperationdetail_save()
    {
        // IF NOT EXISTS(
        //            SELECT TOP 1 1
        //            FROM   GroupCombineOperationDetail
        //            WHERE  GCOD_GCI_combineNo = N? and GCOD_operationType=N?
        //        )
        $sql = "INSERT INTO GroupCombineOperationDetail
                   (GCOD_GCI_combineNo
                   ,GCOD_VEI_SN
                   ,GCOD_operationType
                   ,GCOD_subType
                   ,GCOD_title
                   ,GCOD_dutyName
                   ,GCOD_dutyTel
                   ,GCOD_dutyPhoto
                   ,GCOD_startDate
                   ,GCOD_endDate
                   ,GCOD_useNum
                   ,GCOD_sumMoney
                   ,GCOD_standard
                   ,GCOD_carLicense
                   ,GCOD_remark
                   ,GCOD_creatTime)
             VALUES
                   (N?
                   ,?
                   ,N?
                   ,N?
                   ,N?
                   ,N?
                   ,?
                   ,?
                   ,?
                   ,?
                   ,?
                   ,?
                   ,N?
                   ,N?
                   ,N?
                   ,GETDATE())
            ";
        $query = $this->HT->query($sql, array(
            $this->GCOD_GCI_combineNo
           ,$this->GCOD_VEI_SN
           ,$this->GCOD_operationType
           // ,$this->GCOD_GCI_combineNo
           // ,$this->GCOD_operationType
           ,$this->GCOD_subType
           ,$this->GCOD_title
           ,$this->GCOD_dutyName
           ,$this->GCOD_dutyTel
           ,$this->GCOD_dutyPhoto
           ,$this->GCOD_startDate
           ,$this->GCOD_endDate
           ,$this->GCOD_useNum
           ,$this->GCOD_sumMoney
           ,$this->GCOD_standard
           ,$this->GCOD_carLicense
           ,$this->GCOD_remark
        ));
        return $query;
    }



    var $GUT_SN;
    var $GUT_FirstName; //联系人
    var $GUT_LastName = ""; //联系人
    var $GUT_Title;     //称谓
    var $GUT_Email;     //主email
    var $GUT_Email2;     //备用email
    var $GUT_NationalityID;  //国家
    var $GUT_Passport;   //护照
    var $GUT_TEL;    //座机
    var $GUT_MoveTel;    //手机

    /**
     * 商务联系人表入库
     *
     * @return  int GUT_SN  插入id
     */

    function biz_guest_save() {
        //生成一个号码，用于MAX函数来查询插入ID时避免获得其它线程插入的值
        $AddCode = $this->MakeOrderNumber();
        $sql = "INSERT INTO BIZ_Guest \n"
                . "  ( \n"
                . "    GUT_FirstName, \n"
                . "    GUT_LastName, \n"
                . "    GUT_Title, \n"
                . "    GUT_Email, \n"
                . "    GUT_Email2, \n"
                . "    GUT_NationalityID, \n"
                . "    GUT_Passport, \n"
                . "    GUT_TEL, \n"
                . "    GUT_MoveTel, \n"
                . "    GUT_AddCode, \n"
                . "    GUT_CreateDate \n"
                . "  ) \n"
                . "VALUES \n"
                . "  ( \n"
                . "    N?, \n"
                . "    N?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    GETDATE() \n"
                . "  )";
        $query = $this->HT->query($sql, array(
            mb_convert_encoding($this->GUT_FirstName, 'UTF-8'),
            mb_convert_encoding($this->GUT_LastName, 'UTF-8'),
            $this->GUT_Title,
            $this->GUT_Email,
            $this->GUT_Email2,
            $this->GUT_NationalityID,
            mb_convert_encoding($this->GUT_Passport, 'UTF-8'),
            $this->GUT_TEL,
            $this->GUT_MoveTel, $AddCode
        ));
        $this->GUT_SN = $this->HT->query('select MAX(GUT_SN) as insert_id FROM BIZ_Guest WHERE GUT_AddCode=' . $AddCode)->row('insert_id');
        return $this->GUT_SN;
    }

    public function get_SN_by_vendorOrderId($vendorOrderId, $vendorID)
    {
        $sql = "SELECT TOP 1 coli.COLI_GRI_SN,coli.COLI_SN,gci.GCI_SN
                FROM GroupCombineInfo gci
                INNER JOIN BIZ_ConfirmLineInfo coli ON coli.COLI_GRI_SN=gci.GCI_GRI_SN
                WHERE gci.GCI_VendorOrderId='$vendorOrderId' "; // and GCI_VEI_SN='$vendorID'
        $query = $this->HT->query($sql);
        if ($query->row()) {
            $this->BIZ_COLI_SN = $query->row()->COLI_SN;
            $this->GRI_SN = $query->row()->COLI_GRI_SN;
            $this->GCI_SN = $query->row()->GCI_SN;
            return $query->row();
        }
        return NULL;
    }

    /*!
     * 1. 获取列表时: 按团号查询团是否已存在, 避免渠道和目的地组同时操作
     * @param  [type]     $code          [description]
     * @param  [type]     $vendorOrderId [description]
     */
    public function get_SN_by_groupCode($code, $vendorOrderId=NULL)
    {
      $sql = "SELECT top 1 COLI_SN,gri.GRI_SN,cold.COLD_PlanVEI_SN,cold.COLD_SN,coli.COLI_ID,coli.COLI_Memo,coli.COLI_OrderDetailText,coli.COLI_State,coli.COLI_OPI_ID,coli.COLI_Price,coli.COLI_CUrrency
                ,GRI_OPI_ID,GRI_operator,GRI_No,GRI_Name
              FROM BIZ_ConfirmLineInfo coli
              inner join BIZ_ConfirmLineDetail cold on cold.COLD_COLI_SN=COLI_SN --and COLI_State not in (30,40,50)
              LEFT JOIN GRoupInfo gri ON coli.COLI_GRI_SN=gri.GRI_SN and GRI_OrderType=227002
                and gri.GRI_operator<>435 and gri.GRI_operator is not null
              WHERE (select OPI_DEI_SN from OperatorInfo where OPI_SN=COLI_OPI_ID)=30 and gri.GRI_No LIKE '%$code%'
              order by COLI_SN desc"; // and gri.GRI_Name like '%$NoName%'
      $query = $this->HT->query($sql);
      if ($query->num_rows() > 0) {
          $this->BIZ_COLI_SN     = $query->row()->COLI_SN;
          $this->GRI_SN          = $query->row()->GRI_SN;
          $this->GRI_No          = $query->row()->GRI_No;
          $this->GRI_operator    = $query->row()->GRI_operator;
          $this->COLD_PlanVEI_SN = $query->row()->COLD_PlanVEI_SN;
          return $query->row();
      }
      return NULL;
    }

    /*!
     * 更新时:
     * @date   2018-08-23
     * @param  [type]     $code [description]
     */
    public function get_order_by_groupcode($code, $order_id=0, $vei_sn=0)
    {
        $vei_sn_sql = $vei_sn===0 ? " " : " AND isnull(COLD_PlanVEI_SN,1343) in (1343,$vei_sn) ";
        $sql = "SELECT COLI_SN,gri.GRI_SN,cold.COLD_PlanVEI_SN,cold.COLD_SN,coli.COLI_ID,
                    (select top 1 case gci_vendororderId when $order_id then gci_vendororderId else 0 end
                      from groupcombineinfo where gci_gri_sn=coli_gri_sn
                      order by case gci_vendororderId when $order_id then 0 else 1 end asc
                    ) as gci,
                    ISNULL(coli.COLI_SenderIP,'') senderIP,
                    coli.COLI_OrderDetailText,coli.COLI_GroupCode,coli.COLI_GRI_SN,
                    coli.COLI_State,coli.COLI_OPI_ID,coli.COLI_Price,coli.COLI_CUrrency
                    GRI_OPI_ID,GRI_operator,GRI_No,GRI_Name,
                    coli.COLI_Memo,cold.COLD_MemoText
                FROM BIZ_ConfirmLineInfo coli
                inner join BIZ_ConfirmLineDetail cold on cold.COLD_COLI_SN=COLI_SN
                    $vei_sn_sql
                left JOIN GRoupInfo gri ON coli.COLI_GRI_SN=gri.GRI_SN and GRI_OrderType=227002
                WHERE (select OPI_DEI_SN from OperatorInfo where OPI_SN=COLI_OPI_ID)=30
                  and coli.COLI_State<>50 and coli.COLI_GroupCode LIKE '%$code%'
                order by case COLI_OPI_ID when 435 then 1 else 0 end asc,COLI_SN desc";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    /*!
     * 获取地接社接受计划的人员信息
     * @param       $vendorID 地接社ID
     */
    public function get_vendorContact($vendorID)
    {
      $sql = "SELECT top 1 lmi2.LMI2_Name,
                     lmi.LMI_SN,
                     lmi.LMI_AutoFax,
                     lmi.LMI_Telephone,
                     lmi.LMI_Mobile,
                     lmi.LMI_ListMail
                     ,vei2.VEI2_CompanyBN
              FROM LinkmanInfo lmi
              INNER JOIN LinkManInfo2 lmi2 ON lmi2.LMI2_LMI_SN=lmi.LMI_SN AND lmi2.LMI2_LGC=2
              INNER JOIN VEndorInfo2 vei2 ON vei2.VEI2_VEI_SN=LMI_VEI_SN AND VEI2_LGC=2
              WHERE LMI_Receiver='Yes' AND LMI_VEI_SN=$vendorID";
      $query = $this->HT->query($sql);
      return $query->row();
    }

    var $BIZ_COLI_SN;
    var $BIZ_COLI_ID;
    var $BIZ_COLI_GUT_SN;   //联系人id
    var $BIZ_COLI_Area;     //市场
    var $BIZ_COLI_ApplyDate = '';    //提交日期
    var $BIZ_COLI_Price;   //订单总价
    var $BIZ_COLI_Cost; //总成本
    var $BIZ_COLI_Currency; //币种
    var $BIZ_COLI_TrueCardRate; //信用卡手续费
    var $BIZ_COLI_SenderIP = ''; //客人ip
    var $BIZ_COLI_WebCode = '';  //站点code
    var $BIZ_COLI_servicetype;  //订单来源类型
    var $BIZ_COLI_sourcetype;   //预定类型
    var $BIZ_COLI_AgencyID;
    var $BIZ_COLI_ConfirmType;  //提交方式
    var $BIZ_COLI_OrderDetailText;
    var $BIZ_COLI_OriginalText='';
    var $BIZ_COLI_Memo;
    var $BIZ_COLI_GRI_SN;
    var $BIZ_COLI_GroupCode='';
    var $BIZ_COLI_PayManner;
    var $BIZ_COLI_State;
    var $BIZ_COLI_OPI_ID;
    var $BIZ_COLI_IsSuccess;

    /**
     * 商务订单主表入库
     * @return  int BIZ_COLI_ID  插入id
     */
    function biz_confirm_save() {
        // if (empty($this->BIZ_COLI_WebCode)) {
            $this->BIZ_COLI_WebCode = '';// 来源图兰朵
        // }
        //生成一个号码，用于MAX函数来查询插入ID时避免获得其它线程插入的值
        $AddCode = $this->MakeOrderNumber();
        $sql = "INSERT INTO BIZ_ConfirmLineInfo \n"
                . "( \n"
                . "	COLI_ID, \n"
                . "	COLI_GUT_SN, \n"
                . "	COLI_Area, \n"
                . "	COLI_ApplyDate, \n"
                . "	COLI_Price, \n"
                . "	COLI_Cost, \n"
                . "	COLI_Currency, \n"
                . "	COLI_TrueCardRate, \n"
                . "	COLI_AgencyID, \n"
                . "	COLI_OrderDetailText, \n"
                . "	COLI_SenderIP, \n"
                . "	COLI_WebCode, \n"
                . "	COLI_servicetype, \n"
                . "	COLI_sourcetype, \n"
                . "	COLI_ConfirmType, \n"
                . " COLI_State, \n"
                . " COLI_Department, \n"
                . " COLI_AddCode, \n"
                . " COLI_OrderSource, \n"
                . " COLI_Memo, \n"
                . " COLI_GRI_SN, \n"
                . " COLI_OPI_ID, \n"
                . " COLI_GroupCode, \n"
                . " COLI_PayManner, \n"
                . " COLI_IsSuccess, \n"
                . " COLI_OriginalText \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	N?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    N?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    N? \n"
                . ")";
        $query = $this->HT->query($sql, array(
            $this->BIZ_COLI_ID,
            $this->BIZ_COLI_GUT_SN,
            2,
            // $this->config->item('Site_Area'),
            //date("Y-m-d H:i:s"),
            $this->BIZ_COLI_ApplyDate,
            $this->BIZ_COLI_Price,
            $this->BIZ_COLI_Cost,
            $this->config->item('Site_Currency'),
            $this->BIZ_COLI_TrueCardRate,
            $this->BIZ_COLI_AgencyID,
            $this->BIZ_COLI_OrderDetailText,
            $this->BIZ_COLI_SenderIP,
            $this->BIZ_COLI_WebCode,
            $this->BIZ_COLI_servicetype,
            $this->BIZ_COLI_sourcetype,
            $this->BIZ_COLI_ConfirmType,
            $this->BIZ_COLI_State,
            30,// $this->config->item('Site_Department'),
            $AddCode,
            $this->COLI_OrderSource,
            $this->BIZ_COLI_Memo,
            $this->BIZ_COLI_GRI_SN,
            $this->BIZ_COLI_OPI_ID,
            $this->BIZ_COLI_GroupCode,
            $this->BIZ_COLI_PayManner,
            $this->BIZ_COLI_IsSuccess,
            $this->BIZ_COLI_OriginalText
        ));
        $this->BIZ_COLI_SN = $this->HT->query('select MAX(COLI_SN) as insert_id FROM BIZ_ConfirmLineInfo WHERE COLI_AddCode=' . $AddCode)->row('insert_id');
        return $this->BIZ_COLI_SN;
    }

    public function update_confirmLineInfo()
    {
        $sql = "UPDATE BIZ_ConfirmLineInfo SET
                COLI_GRI_SN=?,
                COLI_GroupCode=?
                WHERE COLI_SN=?
                ";
        $query = $this->HT->query($sql, array(
            $this->BIZ_COLI_GRI_SN
            ,$this->BIZ_COLI_GroupCode
            ,$this->BIZ_COLI_SN
        ));
        return $this->query;
    }

    var $COLD_SN;
    var $COLD_COLI_SN;      // 订单主表sn
    var $COLD_ServiceType;  // 服务类型
    var $COLD_StartDate;    // 产品的服务的开始日期
    var $COLD_EndDate;      // 产品的服务的结束日期
    var $COLD_TotalCost;    // 总成本
    var $COLD_TotalPrice;   // 总报价
    var $COLD_Count;        // 产品数量
    var $COLD_PersonNum;    // 成人数
    var $COLD_ChildNum;     // 小孩数
    var $COLD_BabyNum;      // 婴儿数
    var $cold_state;        // 状态
    var $DeleteFlag;        // 删除标志
    var $COLD_DeliveryCharge = 0; //服务费 + 快递费用 CNY
    var $COLD_PlanVEI_SN     = NULL; // 默认供应商 628-火车桂林国旅
    var $COLD_SPFS           = NULL; // 快递方式：1自取 2酒店 3指定地址
    var $COLD_ServiceSN      = NULL; // 产品ID 除机票外 其它自基础产品库各产品ID
    var $COLD_ServiceSN2     = NULL;
    var $COLD_ServiceCity    = NULL;
    var $COLD_Memo           = NULL;
    var $COLD_MemoText       = NULL;

    /**
     * 商务订单子（详细）表入库
     *
     * @return int  插入id
     */

    function biz_confirm_detail_save() {
        //生成一个号码，用于MAX函数来查询插入ID时避免获得其它线程插入的值
        $AddCode = $this->MakeOrderNumber();
        $sql = "INSERT INTO BIZ_ConfirmLineDetail \n"
                . "( \n"
                . "	COLD_COLI_SN, \n"
                . "	COLD_ServiceType, \n"
                . "	COLD_StartDate, \n"
                . "	COLD_EndDate, \n"
                . "	COLD_TotalCost, \n"
                . "	COLD_TotalPrice, \n"
                . "	COLD_Count, \n"
                . "	COLD_PersonNum, \n"
                . "	COLD_ChildNum, \n"
                . "	COLD_BabyNum, \n"
                . "	cold_state, \n"
                . "	DeleteFlag, \n"
                . "    COLD_DeliveryCharge, \n"
                . "    COLD_AddCode, \n"
                . "    COLD_PlanVEI_SN, \n"
                . "    COLD_SPFS, \n"
                . "    COLD_Memo, \n"
                . "    COLD_MemoText, \n"
                . "    COLD_ServiceSN, \n"
                . "    COLD_ServiceSN2, \n"
                . "    COLD_ServiceCity \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . "	?, \n"
                . "  ? \n"
                . ")";
        $HT1 = $this->load->database('HT', true);
        $query = $HT1->query($sql, array(
            $this->COLD_COLI_SN,
            $this->COLD_ServiceType,
            $this->COLD_StartDate,
            $this->COLD_EndDate,
            $this->COLD_TotalCost,
            $this->COLD_TotalPrice,
            $this->COLD_Count,
            $this->COLD_PersonNum,
            $this->COLD_ChildNum,
            $this->COLD_BabyNum,
            $this->cold_state,
            $this->DeleteFlag,
            $this->COLD_DeliveryCharge,
            $AddCode,
            $this->COLD_PlanVEI_SN,
            $this->COLD_SPFS,
            $this->COLD_Memo,
            $this->COLD_MemoText,
            $this->COLD_ServiceSN,
            $this->COLD_ServiceSN2,
            $this->COLD_ServiceCity)
        );
        //查出最近插入的id
        $HT2 = $this->load->database('HT', true);
        $this->COLD_SN = $HT2->query('select MAX(COLD_SN) as insert_id FROM BIZ_ConfirmLineDetail WHERE COLD_AddCode=' . $AddCode)->row('insert_id');
        return $this->COLD_SN;
    }

    var $BIZ_COLL_COLI_SN;
    var $BIZ_COLL_type;
    var $BIZ_COLL_COLI_Field;
    var $BIZ_COLL_COLI_value;
    var $BIZ_COLL_OPI_SN;

    function biz_confirm_line_log_save()
    {
        $sql = "INSERT INTO BIZ_ConfirmLineLog (
                    COLL_COLI_SN,
                    COLL_LogTime,
                    COLL_type,
                    COLL_COLI_Field,
                    COLL_COLI_value,
                    COLL_OPI_SN
                ) VALUES (?,GETDATE(),?,?,?,?) ";
        $HT1 = $this->load->database('HT', true);
        $query = $HT1->query($sql, array($this->BIZ_COLL_COLI_SN,
            $this->BIZ_COLL_type,
            $this->BIZ_COLL_COLI_Field,
            $this->BIZ_COLL_COLI_value,
            $this->BIZ_COLL_OPI_SN)
        );
        return $query;
    }

    var $POI_SN;
    var $POI_COLD_SN;
    var $POI_FlightsNo = '';
    var $POI_AirPort = '';
    var $POI_Time;
    var $POI_Hotel = '';
    var $POI_HotelAddress = '';
    var $POI_HotelPhone = '';
    var $POI_HotelCheckInName = '';
    var $POI_HotelCheckIn = '';
    var $POI_HotelCheckOut = '';
    var $POI_EndTime = '';
    var $POI_QuotationType; // 1 报价 2 网络支付价 3 促销价
    /** 包价线路订单入库 */
    public function biz_packageorder_save()
    {
        $sql = "IF NOT EXISTS(
                   SELECT TOP 1 1
                   FROM   BIZ_PackageOrderInfo
                   WHERE  POI_COLD_SN = ?
               )
               INSERT INTO BIZ_PackageOrderInfo
                       (POI_COLD_SN
                       ,POI_FlightsNo
                       ,POI_AirPort
                       ,POI_Time
                       ,POI_Hotel
                       ,POI_QuotationType
                       ,POI_HotelAddress
                       ,POI_HotelPhone
                       ,POI_HotelCheckInName
                       ,POI_HotelCheckIn
                       ,POI_HotelCheckOut
                       ,POI_EndTime)
                 VALUES
                       (?
                       ,?
                       ,N?
                       ,?
                       ,N?
                       ,?
                       ,N?
                       ,N?
                       ,N?
                       ,N?
                       ,N?
                       ,N?)
                       ";
        $query = $this->HT->query($sql, array(
            $this->POI_COLD_SN
            ,$this->POI_COLD_SN
            ,$this->POI_FlightsNo
            ,$this->POI_AirPort
            ,$this->POI_Time
            ,$this->POI_Hotel
            ,$this->POI_QuotationType
            ,$this->POI_HotelAddress
            ,$this->POI_HotelPhone
            ,$this->POI_HotelCheckInName
            ,$this->POI_HotelCheckIn
            ,$this->POI_HotelCheckOut
            ,$this->POI_EndTime
        ));
        $this->POI_SN = $this->HT->query('select MAX(POI_SN) as insert_id FROM BIZ_PackageOrderInfo WHERE POI_COLD_SN=' . $this->POI_COLD_SN)->row('insert_id');
        return $this->POI_SN;
    }

    var $FOI_SN;
    var $FOI_COLD_SN;       // 订单子表sn
    var $Aircompany;        // 航空公司编码
    var $FlightsNo;         // 航班号
    var $Cabin;             // 舱位
    var $DepartAirport;     // 出发机场
    var $ArrivalAirport;    // 抵达机场
    var $DepartureCity;     // 出发城市
    var $DepartureTime;     // 出发日期
    var $ArrivalCity;       // 抵达城市
    var $Arrivaltime;       // 抵达时间
    var $DepartureDate;     // 出发时间
    var $adultCost;         // 成人成本
    var $childCost;         // 小孩成倍
    var $babyCost;          // 婴儿成本
    var $adultPrice;        // 成人报价
    var $childPrice;        // 小孩报价
    var $babyPrice;         // 婴儿报价
    var $Stopover;          //
    var $PriceY;            // Y仓价格
    var $price_low;         // 最低价格
    var $FOI_Mile;          // 里程
    var $TicketAddress;     // 寄送地址
    var $FOI_CostTime         = '';   // 运行时间
    var $Aircraft             = '';   // 12306座位编号
    var $FOI_ServiceFee_adult = NULL; // 成人服务费
    var $FOI_ServiceFee_child = NULL; // 儿童服务费
    var $FOI_DeliveryFee      = NULL; // 寄票费
    var $FOI_SelectedSeat     = ""; // 选座

    /**
     *
     * 商务机票订单入库
     *
     */

    function biz_flight_order_save() {
        //生成一个号码，用于MAX函数来查询插入ID时避免获得其它线程插入的值
        $AddCode = $this->MakeOrderNumber();
        $sql = "INSERT INTO BIZ_FlightsOrderInfo \n"
                . "( \n"
                . "	FOI_COLD_SN, \n"
                . "	Aircompany, \n"
                . "	FlightsNo, \n"
                . "	Cabin, \n"
                . "	DepartAirport, \n"
                . "	ArrivalAirport, \n"
                . "	DepartureCity, \n"
                . "	DepartureTime, \n"
                . "	ArrivalCity, \n"
                . "	Arrivaltime, \n"
                . "	DepartureDate, \n"
                . "	adultCost, \n"
                . "	childCost, \n"
                . "	babyCost, \n"
                . "	adultPrice, \n"
                . "	childPrice, \n"
                . "	babyPrice, \n"
                . "	Stopover, \n"
                . "	PriceY, \n"
                . "	price_low, \n"
                . "	FOI_Mile, \n"
                . " TicketAddress, \n"
                . " FOI_CostTime, \n"
                . " FOI_AddCode, \n"
                . " Aircraft, \n"
                . " FOI_ServiceFee_adult, \n"
                . " FOI_ServiceFee_child, \n"
                . " FOI_SelectedSeat, \n"
                . " FOI_DeliveryFee \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . "	? \n"
                . ")";
        $HT1 = $this->load->database('HT', true);
        $query = $HT1->query($sql, array($this->FOI_COLD_SN,
            $this->Aircompany,
            $this->FlightsNo,
            $this->Cabin,
            $this->DepartAirport,
            $this->ArrivalAirport,
            $this->DepartureCity,
            $this->DepartureTime,
            $this->ArrivalCity,
            $this->Arrivaltime,
            $this->DepartureDate,
            $this->adultCost,
            $this->childCost,
            $this->babyCost,
            $this->adultPrice,
            $this->childPrice,
            $this->babyPrice,
            $this->Stopover,
            $this->PriceY,
            $this->price_low,
            $this->FOI_Mile,
            $this->TicketAddress,
            $this->FOI_CostTime,
            $AddCode,
            $this->Aircraft,
            $this->FOI_ServiceFee_adult,
            $this->FOI_ServiceFee_child,
            $this->FOI_SelectedSeat,
            $this->FOI_DeliveryFee
            ));
        $this->FOI_SN = $HT1->query('select MAX(FOI_SN) as insert_id FROM BIZ_FlightsOrderInfo WHERE FOI_AddCode=' . $AddCode)->row('insert_id');
        return $this->FOI_SN;
    }

    /*!
     * 查询是否已经写入过客人列表
     * @date   2018-04-28
     * @param  [type]     $cold_sn 订单字表key
     */
    public function bookpeople_exist($cold_sn)
    {
        if( ! $cold_sn) { return array(); }
        $sql = "SELECT top 1 *
                FROM BIZ_BookPeopleList BPL
                WHERE BPL_COLD_SN=? ";
        $query = $this->HT->query($sql, array($cold_sn));
        return $query->result();
    }

    var $BPE_SN;
    var $BPE_FirstName;     //客人
    var $BPE_MiddleName;    //客人
    var $BPE_LastName;      //客人
    var $BPE_GuestType;     //客人类型
    var $BPE_Passport;      //护照
    var $BPE_imageSrc = NULL;   //护照图片
    var $BPE_Nationality;   //国籍
    var $BPE_SEX;           //性别
    var $BPE_BirthDate;     //生日
    var $BPE_PassportType = "Passport No.";  //护照类型

    /**
     *
     * 商务订单参团客人入库
     *
     */

    function biz_book_people_save() {
        //生成一个号码，用于MAX函数来查询插入ID时避免获得其它线程插入的值
        $AddCode = $this->MakeOrderNumber();
        $sql = "INSERT INTO BIZ_BookPeople \n"
                . "( \n"
                . " BPE_FirstName, \n"
                . " BPE_MiddleName, \n"
                . " BPE_LastName, \n"
                . " BPE_GuestType, \n"
                . " BPE_Passport, \n"
                . " BPE_imageSrc, \n"
                . " BPE_Nationality, \n"
                . " BPE_SEX, \n"
                . " BPE_BirthDate, \n"
                . " BPE_PassportType, \n"
                . " BPE_AddCode \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . " N?, \n"
                . " N?, \n"
                . " N?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ?, \n"
                . " ? \n"
                . ")";
        $query = $this->HT->query($sql, array(
            mb_convert_encoding($this->BPE_FirstName, 'UTF-8'),
            mb_convert_encoding($this->BPE_MiddleName, 'UTF-8'),
            mb_convert_encoding($this->BPE_LastName, 'UTF-8'), $this->BPE_GuestType,
            mb_convert_encoding($this->BPE_Passport, 'UTF-8'), $this->BPE_imageSrc,
            $this->BPE_Nationality,
            $this->BPE_SEX, $this->BPE_BirthDate, $this->BPE_PassportType, $AddCode));
        $this->BPE_SN = $this->HT->query('select MAX(BPE_SN) as insert_id FROM BIZ_BookPeople WHERE BPE_AddCode=' . $AddCode)->row('insert_id');
        return $this->BPE_SN;
    }

    /**
     * 参团人关联
     *
     * @param   int 商务子表sn
     * @param   int 参团客人sn
     */
    function biz_bookpeople_List_save($COLD_SN, $BPE_SN) {
        $sql = "INSERT INTO BIZ_BookPeopleList \n"
                . "( \n"
                . "	BPL_COLD_SN, \n"
                . "	BPL_BPE_SN \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	? \n"
                . ")";
        $query = $this->HT->query($sql, array($COLD_SN, $BPE_SN));
    }

    /*
     * 生成订单号
     * 根据系统时间生成，精确到0.0001微秒
     */

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

    /**
     * 生成商务订单号
     */
    function biz_make_order_number() {
        /*
          $date = date('ymd',time());
          $sql = "SELECT MAX( \n"
          . "           CONVERT( \n"
          . "               INT, \n"
          . "               CASE  \n"
          . "                    WHEN ISNUMERIC(RIGHT(COLI_ID, 3)) = 0 THEN LEFT(RIGHT(COLI_ID, 4), 3) \n"
          . "                    ELSE RIGHT(COLI_ID, 3) \n"
          . "               END \n"
          . "           ) \n"
          . "       ) AS SN \n"
          . "FROM   dbo.BIZ_ConfirmLineInfo  \n"
          . "WHERE  (LEFT(COLI_ID, 6) = ?)";
          $query = $this->HT->query($sql,array($date));
          $id = $query->row()->SN;
          if (is_null($id)||empty($id))
          {
          $id = 0;
          }
          $ids = $date.(sprintf('%03d',(int)$id+1));
          return $ids;
         */
        //call $conn
        include('c:/database_conn.php');
        $connection = array(
            'UID' => $db['HT']['username'],
            'PWD' => $db['HT']['password'],
            'Database' => 'tourmanager',
            'ConnectionPooling' => 1,
            'CharacterSet' => 'utf-8',
            'ReturnDatesAsStrings' => 1
        );
        $conn = sqlsrv_connect($db['HT']['hostname'], $connection);
        $stmt = sqlsrv_query($conn, "declare @ccid varchar(20);exec dbo.SP_GetBIZOrderNo @ccid out;select @ccid as ccid;");
        if ($stmt === false) {
            echo "Error in executing statement 3.\n";
            die(print_r(sqlsrv_errors(), true));
        } else {
            //存储过程中每一个select都会产生一个结果集，取某个结果集就需要从第一个移动到需要的那个结果集
            //如果结果集为空就移到下一个
            while (sqlsrv_has_rows($stmt) !== TRUE) {
                sqlsrv_next_result($stmt);
            }

            $result_object = array();
            while ($row = sqlsrv_fetch_object($stmt)) {
                $result_object[] = $row;
            }

            sqlsrv_free_stmt($stmt);
            sqlsrv_close($conn);

            return($result_object[0]->ccid);
        }
    }

    /*!
     * 删除收款记录以更新
     * @date   2018-04-28
     * @param  [type]     $coli_sn 订单key
     * @param  [type]     $paytype 付款方式
     */
    public function biz_groupaccountinfo_cut($coli_sn, $paytype)
    {
      $sql = "DELETE from BIZ_GroupAccountInfo
              WHERE GAI_COLI_SN=?  AND GAI_Operator = 435"; // AND GAI_Type=?, $paytype
      $query = $this->HT->query($sql, array($coli_sn));
      return $query;
    }

    /**
     *
     * 插入收款记录
     *
     */
    public $GAI_COLI_SN;
    public $GAI_GRI_SN;
    public $GAI_COLI_ID;
    public $GAI_Type;
    public $GAI_SQJE;
    public $GAI_SQDate;
    public $GAI_SQJECurrency;
    public $GAI_SSJE;
    public $GAI_SSDate;
    public $GAI_Operator;
    public $GAI_VEI_SN;
    public $GAI_Memo="";
    public function biz_groupaccountinfo_save()
    {
      $sql = "INSERT INTO BIZ_GroupAccountInfo \n"
                . "  ( \n"
                . "    GAI_COLI_SN, \n"
                . "    GAI_GRI_SN, \n"
                . "    GAI_COLI_ID, \n"
                . "    GAI_Type, \n"
                . "    GAI_SQJE, \n"
                . "    GAI_SQDate, \n"
                . "    GAI_SQJECurrency, \n"
                . "    GAI_SSJE, \n"
                . "    GAI_SSDate, \n"
                . "    GAI_Operator, \n"
                . "    GAI_VEI_SN, \n"
                . "    GAI_Memo \n"
                . "  ) \n"
                . "VALUES \n"
                . "  ( \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ?, \n"
                . "    ? \n"
                . "  )";
      $query = $this->HT->query($sql, array(
        $this->GAI_COLI_SN
        ,$this->GAI_GRI_SN
        ,$this->GAI_COLI_ID
        ,$this->GAI_Type
        ,$this->GAI_SQJE
        ,$this->GAI_SQDate
        ,$this->GAI_SQJECurrency
        ,$this->GAI_SSJE
        ,$this->GAI_SSDate
        ,$this->GAI_Operator
        ,$this->GAI_VEI_SN
        ,$this->GAI_Memo
      ));
      return $query;
    }

    function GetNationalityID($nationalityName) {
        if (!$nationalityName) {
            return 0;
        }
        if (is_numeric($nationalityName)) {
            return $nationalityName;
        } else {
            $sql = "SELECT TOP 1 ci2.COI2_COI_SN \n"
                    . "FROM   COuntryInfo2 ci2 \n"
                    . "WHERE  ci2.COI2_Country = ? ";
            $query = $this->HT->query($sql, array($nationalityName));
            if ($query->result()) {
                $row = $query->row();
                return $row->COI2_COI_SN;
            } else {
                return 0;
            }
        }
    }

    function GetNationalityName($nationalityID) {
        if (!is_numeric($nationalityID)) {
            return $nationalityID;
        } else {
            $sql = "SELECT TOP 1  ci2.COI2_Country \n"
                    . "FROM   COuntryInfo2 ci2 \n"
                    . "WHERE  ci2.COI2_LGC = 2 \n"
                    . "       AND ci2.COI2_COI_SN = ? ";
            $query = $this->HT->query($sql, array($nationalityID));
            if ($query->result()) {
                $row = $query->row();
                return $row->COI2_Country;
            } else {
                return $nationalityID;
            }
        }
    }

    /**
     *
     * 获取商务订单信息
     * @param   string  $order_id   订单id
     * @return  mixed   订单信息
     *
     */
    public function get_flight_order_by_id($order_id) {
        $sql = "SELECT DISTINCT cli.COLI_ID, \n"
                . "       cli.COLI_Price, \n"
                . "       bg.GUT_FirstName, \n"
                . "       bg.GUT_LastName, \n"
                . "       bg.GUT_Email, \n"
                . "       bg.GUT_Email2, \n"
                . "       bpe.BPE_SN, \n"
                . "       bpe.BPE_GuestType, \n"
                . "       bpe.BPE_FirstName, \n"
                . "       bpe.BPE_MiddleName, \n"
                . "       bpe.BPE_LastName, \n"
                . "       bpe.BPE_GuestType, \n"
                . "       bpe.BPE_Passport, \n"
                . "       cld.COLD_Count, \n"
                . "       foi.FlightsNo, \n"
                . "       foi.DepartureDate, \n"
                . "       foi.DepartureTime, \n"
                . "       foi.ArrivalTime, \n"
                . "       cli.COLI_PayManner, \n"
                . "       cli.COLI_State, \n"
                . "       cli.COLI_sourcetype, \n"
                . "       foi.Cabin, \n"
                . "       foi.DepartAirport, \n"
                . "       foi.ArrivalAirport, \n"
                . "       cld.COLD_SN, \n"
                . "       foi.DepartureCity, \n"
                . "       foi.ArrivalCity, \n"
                . "       bg.GUT_TEL, \n"
                . "       bg.GUT_NationalityID, \n"
                . "       cli.COLI_OrderDetailText, \n"
                . "       bpe.BPE_imageSrc, \n"
                . "       cli.COLI_Cost, \n"
                . "       cld.COLD_TotalPrice, \n"
                . "       cld.COLD_TotalCost \n"
                . "FROM   BIZ_ConfirmLineInfo cli \n"
                . "       INNER JOIN BIZ_ConfirmLineDetail cld \n"
                . "            ON  cli.COLI_SN = cld.COLD_COLI_SN \n"
                . "       INNER JOIN BIZ_GUEST bg \n"
                . "            ON  bg.GUT_SN = cli.COLI_GUT_SN \n"
                . "       INNER JOIN BIZ_BookPeopleList bpl \n"
                . "            ON  bpl.BPL_COLD_SN = cld.COLD_SN \n"
                . "       INNER JOIN BIZ_BookPeople bpe \n"
                . "            ON  bpl.BPL_BPE_SN = bpe.BPE_SN \n"
                . "       INNER JOIN BIZ_FlightsOrderInfo foi \n"
                . "            ON  foi.FOI_COLD_SN = cld.COLD_SN \n"
                . "WHERE  cli.COLI_ID = ? AND isnull(cld.deleteflag,0) = 0 ";

        $query = $this->HT->query($sql, array($order_id));
        return $query->result();
    }

    /**
     *
     * 获取机票订单乘员列表
     * @param   string  $order_id   订单id
     * @return  mixed   订单信息
     *
     */
    public function get_bpe_list_by_id($order_id) {
        $sql = "SELECT DISTINCT bpe.BPE_SN, \n"
                . "       bpe.BPE_FirstName, \n"
                . "       bpe.BPE_MiddleName, \n"
                . "       bpe.BPE_LastName, \n"
                . "       bpe.BPE_Passport \n"
                . "FROM   BIZ_BookPeople bpe \n"
                . "       INNER JOIN BIZ_BookPeopleList bpl \n"
                . "            ON  bpe.BPE_SN = bpl.BPL_BPE_SN \n"
                . "       INNER JOIN BIZ_ConfirmLineDetail cold \n"
                . "            ON  bpl.BPL_COLD_SN = cold.COLD_SN \n"
                . "       INNER JOIN BIZ_ConfirmLineInfo coli \n"
                . "            ON  coli.COLI_SN = cold.COLD_COLI_SN \n"
                . "WHERE  coli.COLI_ID = ?";
        $query = $this->HT->query($sql, array($order_id));
        //echo('<!--'.$this->HT->compile_binds($sql,array($order_id)).'-->');
        return $query->result();
    }

    /**
     *
     * 获取机票电子票号
     * @param   array  bpe_sn   乘客sn数组
     * @return  mixed   机票票号
     *
     */
    public function get_ticket_no($bpe_sn) {
        if (is_array($bpe_sn)) {
            $instr = join(',', $bpe_sn);
        } elseif (is_string($bpe_sn)) {
            $instr = $bpe_sn;
        } else {
            $instr = 0;
        }
        $sql = "SELECT DISTINCT ftn.FTN_FilghtsNo, \n"
                . "       ftn.FTN_TicketNo, \n"
                . "       ftn.FTN_GuestNo \n"
                . "FROM   BIZ_FlightsTicketNo ftn \n"
                . "WHERE  ftn.FTN_GuestNo IN (" . $instr . ")";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    /*
     * 发送邮件
     */
    public function save_automail($M_SenderName, $M_SenderEmail, $fromName, $fromEmail, $toName, $toEmail, $subject, $body, $frominfo = 'vendorConfirm msg', $M_RelatedInfo = '', $M_State = 0, $M_AddTime = '', $M_Web = 'vendorConfirm msg') {
        $sql = "INSERT INTO
                Email_AutomaticSend (
                    M_SenderName,
                    M_SenderEmail,
                    M_ReplyToName,
                    M_ReplyToEmail,
                    M_ToName,
                    M_ToEmail,
                    M_Title,
                    M_Body,
                    M_Web,
                    M_FromName,
                    M_ServiceSN,
                    M_State,
                    M_AddTime
                ) VALUES (N?, N?, N?, N?, N?, N?, N?, N?, ?, N?, ?,?,getdate()) ";
        $query = $this->HT->query($sql, array($M_SenderName, $M_SenderEmail, $fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_Web, $frominfo, $M_RelatedInfo, $M_State));
        return $query;
    }

    /**
     * wifi预订入库(目前仅CHT使用)
     *
     * @return int  插入id
     */
    var $WOI_COLD_SN;
    var $WOI_Device;       //设备（智能手机、pad）
    var $WOI_DeviceCount;  //设备数量
    var $WOI_UsersCount;   //使用人数
    var $WOI_Package;      //Wi-Fi套餐
    var $WOI_PackageCount; //套餐数量
    var $WOI_DeliverDate;  //起租日期
    var $WOI_DeliverCity;  //起租城市
    var $WOI_DeliverAddr;  //起租地址
    var $WOI_ReturnDate;   //归还日期
    var $WOI_ReturnCity;   //归还城市
    var $WOI_ReturnAddr;   //归还地址
    var $WOI_OtherService; //其他服务
    var $WOI_GroupNo;      //团号
    var $WOI_ExpressNo;    //快递单号

    public function biz_wifi_info_save() {
        $sql = "INSERT INTO BIZ_WifiOrderInfo \n"
                . "( \n"
                . "	WOI_COLD_SN, \n"
                . "	WOI_Device, \n"
                . "	WOI_DeviceCount, \n"
                . "	WOI_UsersCount, \n"
                . "	WOI_Package, \n"
                . "	WOI_PackageCount, \n"
                . "	WOI_DeliverDate, \n"
                . "	WOI_DeliverCity, \n"
                . "	WOI_DeliverAddr, \n"
                . "	WOI_ReturnDate, \n"
                . "	WOI_ReturnCity, \n"
                . "	WOI_ReturnAddr, \n"
                . "    WOI_OtherService, \n"
                . "    WOI_GroupNo, \n"
                . "    WOI_ExpressNo \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	? \n"
                . ")";
        $HT1 = $this->load->database('HT', true);
        $query = $HT1->query($sql, array($this->WOI_COLD_SN,
            $this->WOI_Device,
            $this->WOI_DeviceCount,
            $this->WOI_UsersCount,
            $this->WOI_Package,
            $this->WOI_PackageCount,
            $this->WOI_DeliverDate,
            $this->WOI_DeliverCity,
            $this->WOI_DeliverAddr,
            $this->WOI_ReturnDate,
            $this->WOI_ReturnCity,
            $this->WOI_ReturnAddr,
            $this->WOI_OtherService,
            $this->WOI_GroupNo,
            $this->WOI_ExpressNo
        ));
    }

    /**
     * 酒店预订入库
     *
     * @return int  插入id
     */
    var $HOI_COLD_SN; //必选
    var $HOI_NoSmoking = null;  //无烟房
    var $HOI_EarlyTime = null;  //最早确认时间，已不用
    var $HOI_LastTime = null;   //最晚确认时间，已不用
    var $HOI_Room_NO = null;    //房号
    var $HOI_ExtraNum = 0;      //加床
    var $HOI_RoomTypeName = null; //房型
    var $HOI_BreakNum = null;   //早餐人数
    var $HOI_PriceType = null;  //价格类型
    var $HOI_BreakType = null;  //早餐类型
    var $HOI_RoomRates = null;
    var $HOI_ExtrabedRates = null;
    var $HOI_TaxFee = null;

    public function biz_hotel_order_save() {
        /* ASP版本
          sql="select * from BIZ_HotelOrderInfo where 1=2"
          rs2.open sql,conn,3,3,1
          rs2.addnew
          rs2("HOI_COLD_SN")=COLD_SN
          rs2("HOI_ExtraNum") = extrabed
          if clng(Smoking)<2 then
          rs2("HOI_NoSmoking")=Smoking
          end if
          rs2("HOI_EarlyTime")=earlydate
          rs2("HOI_LastTime")=""
          rs2.update
          rs2.close
         */
        $sql = "INSERT INTO BIZ_HotelOrderInfo \n"
                . "( \n"
                . "	HOI_COLD_SN, \n"
                . "	HOI_NoSmoking, \n"
                . "	HOI_EarlyTime, \n"
                . "	HOI_LastTime, \n"
                . "	HOI_Room_NO, \n"
                . "	HOI_ExtraNum, \n"
                . "	HOI_RoomTypeName, \n"
                . "	HOI_BreakNum, \n"
                . "	HOI_PriceType, \n"
                . "	HOI_BreakType, \n"
                . "	HOI_RoomRates, \n"
                . "	HOI_ExtrabedRates, \n"
                . "	HOI_TaxFee \n"
                . ") \n"
                . "VALUES \n"
                . "( \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	?, \n"
                . "	? \n"
                . ")";
        $HT1 = $this->load->database('HT', true);
        $query = $HT1->query($sql, array(
            $this->HOI_COLD_SN,
            $this->HOI_NoSmoking,
            $this->HOI_EarlyTime,
            $this->HOI_LastTime,
            $this->HOI_Room_NO,
            $this->HOI_ExtraNum,
            $this->HOI_RoomTypeName,
            $this->HOI_BreakNum,
            $this->HOI_PriceType,
            $this->HOI_BreakType,
            $this->HOI_RoomRates,
            $this->HOI_ExtrabedRates,
            $this->HOI_TaxFee
        ));
    }

    /**
     *
     * 返回成行订单
     * 条件1：订单类型
     * 天剑2：网站来源
     *
     */
    public function get_order_info($COLI_sourcetype, $COLI_WebCode, $biz = true) {
        if ($biz) {
            $sql = "SELECT TOP 200 c.COLI_WebCode, \n"
                    . "       c.COLI_ID, \n"
                    . "       c.COLI_ConfirmDate, \n"
                    . "       c.COLI_ApplyDate, \n"
                    . "       ISNULL(c.COLI_IsSuccess, 0) AS success \n"
                    . "FROM   BIZ_ConfirmLineInfo c \n"
                    . "WHERE  c.COLI_sourcetype = ? \n"
                    . "       AND c.COLI_WebCode = ? \n"
                    . "ORDER BY \n"
                    . "       c.COLI_SN DESC";
            $query = $this->HT->query($sql, array($COLI_sourcetype, $COLI_WebCode));
        } else {
            $sql = "SELECT TOP 200 c.COLI_WebCode, \n"
                    . "       c.COLI_ID, \n"
                    . "       c.COLI_ConfirmDate, \n"
                    . "       c.COLI_ApplyDate, \n"
                    . "       CASE WHEN c.COLI_Sended = 5 THEN 1 ELSE 0 END AS success \n"
                    . "FROM   ConfirmLineInfo c \n"
                    . "WHERE  c.COLI_sourcetype = ? \n"
                    . "       AND c.COLI_WebCode = ? \n"
                    . "ORDER BY \n"
                    . "       c.COLI_SN DESC";
            $query = $this->HT->query($sql, array($COLI_sourcetype, $COLI_WebCode));
        }
        $this->sql = $this->HT->queries;
        return $query->result();
    }

    //传统订单支付之后，插入新的订单信息
    public function insert_daytrip_order($coli_sn, $pay_manner, $gri_sn, $state, $deleteflag) {
        //获取订单
        $order_info_sql = "
                SELECT confirmlineinfotmp.COLI_OrderPrice,
                       memberinfotmp.MEI_FirstName,
                       memberinfotmp.MEI_LastName,
                       memberinfotmp.MEI_Mail
                FROM   memberinfotmp
                       INNER JOIN customerlisttmp
                               ON memberinfotmp.mei_sn = customerlisttmp.cul_cui_sn
                       INNER JOIN confirmlineinfotmp
                               ON customerlisttmp.cul_coli_sn = confirmlineinfotmp.coli_sn
                WHERE  (customerlisttmp.cul_coli_sn = ? )";
        $query = $this->HT->query($order_info_sql, array($coli_sn));
        $order_info = $query->row();

        //插入记录
        $sql = "INSERT INTO GroupAccountInfoTmp
                (
                  GAI_COLI_SN,
                  GAI_SQJE,
                  GAI_SQDate,
                  GAI_CusName,
                  GAI_CusEmail,
                  GAI_SQJECurrency,
                  GAI_Type,
                  LastEditTime,
                  GAI_GRI_SN,
                  GAI_State,
                  DeleteFlag
                )
                VALUES (?,?,?,?,?,?,?,?,?,?,?)";

        $query = $this->HT->query($sql, array($coli_sn,
            $order_info->COLI_OrderPrice,
            date('Y-m-d H:i:s'),
            $order_info->MEI_FirstName . " " . $order_info->MEI_LastName,
            $order_info->MEI_Mail,
            $this->config->item('Site_Currency'),
            $pay_manner,
            date('Y-m-d H:i:s'),
            $gri_sn,
            $state,
            $deleteflag
                )
        );
    }

    /** 获取产品的渠道价格 */
    public function get_partner_price($code="", $person_num=0, $price_date="")
    {
        $sql = "SELECT TOP 1 PKP_Currency,
                    PKP_PriceGrade,PKP_ValidDate,PKP_InvalidDate,
                    PKP_AdultCost,PKP_ChildCost,PKP_BabyCost,
                    PKP_PriceType --0每人 1每团
                    ,p.*
                from BIZ_PackageInfo pag
                left join BIZ_PackagePrice p on  p.PKP_PAG_SN=PAG_SN
                where PAG_Code=? and PAG_DEI_SN=30
                and ? between PKP_PersonStart and PKP_PersonStop
                and ? between PKP_ValidDate and PKP_InvalidDate
                and PKP_VEI_SN in (1343,29188,30548)
                order by p.Checked desc, p.PKP_PriceGrade asc,p.PKP_ValidDate desc"; // 重复日期的取新的
        return $this->HT->query($sql, array($code, $person_num, $price_date))->row();
    }

    //来源终端 tablet mobile desktop
    public function check_device() {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $ua = $_SERVER['HTTP_USER_AGENT'];
        } else {
            $ua = '';
        }
        ## This credit must stay intact (Unless you have a deal with @lukasmig or frimerlukas@gmail.com
        ## Made by Lukas Frimer Tholander from Made In Osted Webdesign.
        ## Price will be $2
        $iphone = strstr(strtolower($ua), 'mobile'); //Search for 'mobile' in user-agent (iPhone have that)
        $android = strstr(strtolower($ua), 'android'); //Search for 'android' in user-agent
        $windowsPhone = strstr(strtolower($ua), 'phone'); //Search for 'phone' in user-agent (Windows Phone uses that)

        if (!function_exists('androidTablet')) {

            function androidTablet($ua) { //Find out if it is a tablet
                if (strstr(strtolower($ua), 'android')) { //Search for android in user-agent
                    if (!strstr(strtolower($ua), 'mobile')) { //If there is no ''mobile' in user-agent (Android have that on their phones, but not tablets)
                        return true;
                    }
                }
            }

        }
        $androidTablet = androidTablet($ua); //Do androidTablet function
        $ipad = strstr(strtolower($ua), 'ipad'); //Search for iPad in user-agent

        if ($androidTablet || $ipad) { //If it's a tablet (iPad / Android)
            return 'tablet';
        } elseif ($iphone && !$ipad || $android && !$androidTablet || $windowsPhone) { //If it's a phone and NOT a tablet
            return 'mobile';
        } else { //If it's not a mobile device
            return 'desktop';
        }
    }

    public function ip_limit($ip = "0.0.0.0")
    {
        if (strcmp($ip, "0.0.0.0") === 0 || empty($ip)) {
            return TRUE;
        }
        $sql = "SELECT COUNT(1) cnt
                FROM ConfirmLineInfoTmp
                WHERE 1=1
                  AND COLI_SenderIP = ?
                  AND DateDiff(dd,COLI_ApplyDate,getdate())=0";
        $query = $this->HT->query($sql, array($ip));
        $ret = $query->row();
        if ($ret->cnt > 50) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function convert_RMB_to_currency($money=0, $toCurrency='USD')
    {
        return $this->HT
                    ->query("SELECT tourmanager.dbo.[ConvertCurrencyToCurrency](1,'RMB','$toCurrency',$money) as rmb ")
                    ->row()->rmb;
    }

    public function test()
    {
      // return NULL;
      $sql = "
       ";
      $query = $this->HT->query($sql);
    }


}
