<?php

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

class BIZ_Orders_model extends CI_Model {

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

    public function get_orderinfo_detail($gri_sn, $vei_sn_str)
    {
        $sql = "SELECT coli.COLI_ID,
                       coli.COLI_SN,
                       coli.COLI_Department,
                       cold.COLD_ServiceSN,
                       (select PAG_Code from biz_packageinfo where PAG_SN=cold.COLD_ServiceSN) as pag_code,
                       cold.COLD_ServiceSN2,
                       cold.COLD_ServiceCity,
                       cold.COLD_PlanVEI_SN,
                       gut.GUT_NationalityID,
                       isnull(COLI_PVT, 0) is_pvt,
                       (select POI_QuotationType from BIZ_PackageOrderInfo where POI_COLD_SN=COLD_SN) as settle,
                       (select opi2_name from tourmanager.dbo.operatorinfo2 where opi2_opi_sn=coli_opi_id and opi2_lgc=2) as operator,
                       *
                FROM BIZ_ConfirmLineInfo coli
                INNER JOIN BIZ_ConfirmLineDetail cold ON cold.COLD_COLI_SN=coli.COLI_SN and cold.DeleteFlag=0
                LEFT JOIN BIZ_GUEST gut ON gut.GUT_SN=coli.COLI_GUT_SN
                WHERE coli.COLI_GRI_SN=$gri_sn
                  AND COLD_PlanVEI_SN in ($vei_sn_str)
                ORDER BY COLD_StartDate ASC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_guestlist($COLD_SN_str)
    {
        $sql = "SELECT
                    isnull(BPE.BPE_FirstName,'') +' '+isnull(BPE.BPE_MiddleName,'') +' '+isnull(BPE.BPE_LastName ,'') as MemberName,
                    BPE.BPE_BirthDate BirthDay,
                    ISNULL((select SYC2_CodeDiscribe
                            from V_System_Code
                            where LGC_LGC=2
                                and SYC_SN=BPE.BPE_SEX),''
                    ) Gender,
                    (select COI2_Country from V_Country_Info
                        where LGC_LGC = 2
                            and COI_SN = BPE.BPE_Nationality
                    ) as Country,
                    '' as phone,
                    BPE.BPE_Passport PassportNo,
                    BPE.BPE_PassExpdate PassportValidDate
                    ,*
                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 COLD_StartDate,
                  (select CII2_name from CItyInfo2 where CII2_CII_SN=PAG_CII_SN and CII2_LGC=2) as city_chinese,
                  (select CII_PKCode from CItyInfo where CII_SN=PAG_CII_SN) as city_code
                ,*
                FROM BIZ_ConfirmLineDetail cold
                LEFT JOIN BIZ_PackageInfo2 pag2 ON cold.COLD_ServiceSN=pag2.PAG2_PAG_SN AND (PAG2_LGC = 2)
                LEFT JOIN BIZ_PackageInfo pag ON pag.PAG_SN=pag2.PAG2_PAG_SN
                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)
                ORDER BY cold.COLD_StartDate asc ";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    /** 根据线路代号 */
    public function get_packageDetails($pag_code_str='', $vei_sn_str='', $is_trippest=true)
    {
        $code_sql = $pag_code_str=='' ? " " : " and pag.PAG_Code IN ($pag_code_str) ";
        $vei_sql = $vei_sn_str=='' ? " " : " and isnull(pag.PAG_DefaultVEI_SN,0) IN (0,$vei_sn_str) ";
        $trippest_sql = $is_trippest===true ? " and pag.PAG_DEI_SN=30 " : " ";
        $sql = "SELECT *,
                  (select CII2_name from CItyInfo2 where CII2_CII_SN=PAG_CII_SN and CII2_LGC=2) as city_chinese,
                  (select CII_PKCode from CItyInfo where CII_SN=PAG_CII_SN) as city_code
                FROM BIZ_PackageInfo2 pag2
                INNER JOIN BIZ_PackageInfo pag ON pag.PAG_SN=pag2.PAG2_PAG_SN and pag2.PAG2_LGC=2
                    $trippest_sql
                WHERE 1=1
                    $code_sql
                    $vei_sql
                order by pag.PAG_Code ";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    /** 酒店信息 */
    public function get_package_order($COLD_SN_str)
    {
        $sql = "SELECT *
                from BIZ_PackageOrderInfo poi
                where poi.POI_COLD_SN IN ($COLD_SN_str)";
        return $this->HT->query($sql)->result();
    }

    /*!
     * [get_paymentDetails description]
     * @date   2019-05-22
     * VEI:
     * * 30561 桂林海纳国旅
     * GAI_Type
     * * 15006 地接代收
     * * 15008 现付
     * * 15017 内部转账
     * * 15020 地接社代收
     */
    public function get_paymentDetails($COLI_ID, $vei_sn_str)
    {
        $sql = "SELECT ISNULL(bgai.GAI_VEI_SN,0) payment_vei
                ,bgai.*
                FROM BIZ_GroupAccountInfo bgai
                INNER JOIN BIZ_ConfirmLineInfo coli ON bgai.GAI_COLI_SN=coli.COLI_SN
                WHERE bgai.DeleteFlag=0 --AND bgai.GAI_Type NOT IN (15006,15008,15017)
                    AND ISNULL(bgai.GAI_VEI_SN,0) in (0,$vei_sn_str)
                    AND bgai.GAI_SQJE > 0
                    AND coli_ID = '$COLI_ID'";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function get_cold_amount($COLD_SN_str)
    {
        $sql = "SELECT COLD_TotalPrice, dbo.GetSSJEFromSQJE(
            (select top 1 GAI_Type from BIZ_GroupAccountInfo b where gai_coli_sn=cold.COLD_COLI_SN and b.DeleteFlag=0)
            ,coli.COLI_Currency,COLD_TotalPrice) as CNY_price
            ,(select top 1 GAI_Type from BIZ_GroupAccountInfo b where gai_coli_sn=cold.COLD_COLI_SN and b.DeleteFlag=0)
            as payment_type
            -- ,cold.*
        from BIZ_ConfirmLineDetail cold
        inner join BIZ_ConfirmLineInfo coli on coli.COLI_SN=cold.COLD_COLI_SN
        where cold.COLD_SN in ($COLD_SN_str)
            and not exists (
                select 1 from BIZ_PackageOrderInfo where POI_COLD_SN=cold.COLD_SN and POI_QuotationType=4
            ) ";
        $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();
    }

    /*!
     * 获取地接社接受计划的人员信息
     * @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();
    }

    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   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;
    }

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

    /*!
     * 写入商务订单操作记录
     * @param  [int]     $coli_sn
     * @param  [string]  $log_info varchar(100)
     */
    public function insert_biz_order_log($coli_sn, $log_info)
    {
        $db_column = array(
            "BOL_COLI_SN" => $coli_sn
            ,"BOL_OPI_SN" => 29
            ,"BOL_OPType" => $log_info
            ,"BOL_OPTime" => date('Y-m-d H:i:s')
            ,"BOL_Creator" => 29
            ,"BOL_CreateTime" => date('Y-m-d H:i:s')
        );
        return $this->HT->insert("BIZ_OrderOperationLog", $db_column);
    }

    /*!
     * @Author: LYT
     * @Date: 2019-06-04 14:16:01
     * @Desc: 商务订单的预订详情
     */
    public function get_all_cold($COLD_SN_str)
    {
        $sql = "SELECT  *
                from BIZ_ConfirmLineDetail cold
                inner join BIZ_ConfirmLineInfo coli on coli.COLI_SN=COLD_COLI_SN
                where COLD_SN in (
                    $COLD_SN_str
                ) and cold.DeleteFlag=0";
        return $this->HT->query($sql)->result();
    }

    /*!
     * @Author: LYT
     * @Date: 2019-06-04 11:36:50
     * @Desc: 商务订单:火车票预订;机票预订;
     */
    public function get_train_flight_order($COLD_SN_str)
    {
        $sql = "SELECT
                [dbo].[GetStationCNName_LGC](f.DepartureCity, f.DepartAirport, 2) as depart_station
                ,[dbo].[GetStationCNName_LGC](f.ArrivalCity, f.ArrivalAirport, 2) as arrival_station
                ,[dbo].[GetStationCabinName_LGC](f.Cabin,2) as cabin_name
                ,CONVERT(date,f.DepartureDate) departure_date
                ,SUBSTRING(f.DepartureTime,1,7)+'-'+SUBSTRING(f.ArrivalTime,1,7) as duration
                ,f.adultcost
                ,f.TicketAddress
                ,cold.COLD_PersonNum as adult_num
                ,isnull(cold.COLD_ChildNum,0)+isnull(cold.COLD_BabyNum,0) as child_num
                ,cold.COLD_StartDate,cold.COLD_EndDate
                ,(select CII2_Name from CItyInfo2
                    inner join VEndorInfo on VEI_CII_Name=CII2_CII_SN
                    where VEI_SN=cold.COLD_PlanVEI_SN and CII2_LGC=2) as city_name
                ,(select CII_PKCode from CItyInfo
                    inner join VEndorInfo on VEI_CII_Name=CII_SN
                    where VEI_SN=cold.COLD_PlanVEI_SN ) as city_code
                ,f.* from BIZ_FlightsOrderInfo f
                inner join BIZ_ConfirmLineDetail cold on COLD_SN=FOI_COLD_SN
                where f.FOI_COLD_SN in (
                    $COLD_SN_str
                )
                order by COLD_StartDate ";
        return $this->HT->query($sql)->result();
    }

    /*!
     * @Author: LYT
     * @Date: 2019-06-04 15:32:16
     * @Desc: 商务订单:酒店预订
     * @Deprecated 酒店不通过地接社预定.除桂林地接外
     */
    public function get_hotel_order($COLD_SN_str)
    {
        $sql = "SELECT VEI2_CompanyBN,ROT2_TypeName,
                COLD_COUNT room_count,HOI_Room_NO,HOI_EarlyTime,
                HOI_LastTime,HOI_ExtraNum,isnull(HOI_NoSmoking,0) as HOI_NoSmoking,
                COLD_StartDate,COLD_EndDate,COLD_PersonNum,COLD_ChildNum,COLD_BabyNum ,
                HOI_RoomTypeName
                from biz_ConfirmLineDetail
                left join Vendorinfo2 on COLD_ServiceSN=VEI2_VEI_SN and VEI2_LGC=2
                left join roomtype2 on ROT2_ROT_SN = COLD_ServiceSN2 and ROT2_LGC = 2
                left join BIZ_HotelOrderInfo on COLD_SN = HOI_COLD_SN
                where 1=1
                and COLD_SN in ($COLD_SN_str)";
        return $this->HT->query($sql)->result();
    }

    /*!
     * @Author: LYT
     * @Date: 2019-06-11 17:54:12
     * @Desc: 获取小包价子产品的信息
     */
    public function get_sub_pag_info($PAGS_SN)
    {
        $sql = "SELECT * from BIZ_PackageInfoSub p
                where p.PAGS_SN=$PAGS_SN ";
        return $this->HT->query($sql)->row();
    }

    /*!
     * @Author: LYT
     * @Date: 2019-06-25 10:12:52
     * @Desc: 商务订单联系人信息
     */
    public function get_customer_contact($COLD_SN_str)
    {
        $sql = "SELECT
                isnull(GUT_FirstName,'') + ' '+isnull(GUT_LastName,'') as MemberName,
                '' as BirthDay,
                '' as Gender,
                isnull(GUT_POST ,'') + ' ' + isnull(GUT_tel,'') + isnull(GUT_MoveTel,'') as phone,
                (select COI2_Country from V_Country_Info
                    where LGC_LGC = 2
                        and COI_SN = GUT_NationalityID
                ) as Country,
                GUT_Passport as PassportNo,
                '' as PassportValidDate
                --,BIZ_GUEST.*
                from BIZ_GUEST
                inner join BIZ_ConfirmLineInfo on COLI_GUT_SN=GUT_SN
                inner join BIZ_ConfirmLineDetail on COLI_SN=COLD_COLI_SN
                where COLD_SN IN ($COLD_SN_str) ";
        return $this->HT->query($sql)->row();
    }
}
