<?php
defined('BASEPATH') OR exit('No direct script access allowed');

define('PAY_OTHER','15006, 15017,15008,15006,15020');

class Vendor_money_model extends CI_Model {

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

    public function checked_group_list($vendor, $sourcetype, $start_date, $end_date, $all_vendor)
    {
        /*
        SELECT sum_opi.COLI_OPI_ID,sum(sum_opi.海纳收款) as trippest_sum,sum(sum_opi.地接社收款) as vendor_sum,
                      sum(sum_opi.other_price_RMB) as other_price_sum
                      , sum(sum_opi.other_cost_RMB) as other_cost_sum
                      ,sum(sum_opi.other_vendorprice_RMB) as other_vendorprice_RMB_sum
                from (
            ) as sum_opi
            group by sum_opi.COLI_OPI_ID
            order by case sum_opi.COLI_OPI_ID when '435' then 0 else 1 end
        */
        $sql = "SELECT
                    (select isnull(SUM(COLD_TotalPrice),0) from BIZ_ConfirmLineDetail cold
                        where cold.COLD_COLI_SN=cgi_group.COLI_SN
                        and COLD_ServiceType <> 'D'
                        and COLD_PlanVEI_SN IN ($all_vendor)
                        and cold.DeleteFlag=0
                    )*cgi_group.gai_rate as other_price_RMB,
                    (select isnull(SUM(COLD_TotalCost),0) from BIZ_ConfirmLineDetail cold
                        where cold.COLD_COLI_SN=cgi_group.COLI_SN
                        and COLD_ServiceType <> 'D'
                        and COLD_PlanVEI_SN  IN ($all_vendor)
                        and cold.DeleteFlag=0
                    ) as other_cost_RMB,
                    (dbo.ConvertToRMB('USD',other_vendor_price)) as other_vendorprice_RMB,
                    case when other_vendor_price > 0 then
                            (order_total_receipt-dbo.ConvertToRMB('USD',other_vendor_price))
                        else ht_origin end
                    as haina_receipt,
                    case when other_vendor_price > 0  then
                            (isnull(sync_total_receipt_sum,0)-vendor_receipt)
                        else ht_origin end
                    as sync_haina_receipt,
                    *
                    ,(select ROUND(SUM(isnull(tourcost,0)),4)
                        from report_tour where ordernumber=COLI_ID
                        and exists (
                            select 1 from BIZ_ConfirmLineDetail where COLD_SN=RPT_COLD_SN
                            and COLD_PlanVEI_SN IN ($all_vendor)
                        )
                    ) as group_vendor_cost
                from (
                    select
                        COLI_SN,COLI_ID,GRI_No,
                        (select COUNT(0) from BIZ_ConfirmLineDetail
                            where COLD_COLI_SN=COLI_SN
                            and COLD_ServiceType='D'
                            and DeleteFlag=0
                        ) as service_cnt,
                        (select isnull(SUM(COLD_TotalPrice),0) from BIZ_ConfirmLineDetail
                        left join biz_packageorderinfo on poi_cold_sn=cold_sn
                            where COLD_COLI_SN=COLI_SN
                            --and COLD_ServiceType='D'
                            and DeleteFlag=0
                            --and COLD_PlanVEI_SN<>$vendor
                            and ( COLD_PlanVEI_SN not in ($all_vendor) or isnull(poi_quotationType,0)=4)
                        ) as other_vendor_price,
                        COLI.COLI_sourcetype,
                        COLI.COLI_Price,
                        coli.COLI_Currency,
                        case when coli.COLI_Price <> 0 then
                            convert(decimal(10,2),round((select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                                where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                            )/isnull(COLI.COLI_Price,1),2))
                        else 0 end  as gai_rate,
                        (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                            where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                        ) as order_total_receipt,
                        (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                            where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                            --and GAI_Type not in (" . PAY_OTHER . ")
                            and isnull(GAI_VEI_SN,30561) = 30561
                        ) as ht_origin,
                        (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                            where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                            and GAI_VEI_SN in ($all_vendor)
                            --and GAI_Type in (" . PAY_OTHER . ")
                        ) as vendor_receipt
                        ,(select SUM(convert(decimal(10,2),isnull(GCI_priceCNY,0))) from
                            GroupCombineInfo where GCI_GRI_SN=GRI_SN
                            and ISNULL(GCI_combineNo,'') not in ('','cancel','forbidden')
                        ) as sync_total_receipt_sum
                        ,coli.COLI_OPI_ID
                        ,(
                            select top 1 v.COLD_PlanVEI_SN from BIZ_ConfirmLineDetail v where COLD_COLI_SN=COLI_SN
                                and v.DeleteFlag=0 and COLD_PlanVEI_SN in ($all_vendor)
                        ) as vendor
                    from CK_GroupInfo cgi
                    inner join GRoupInfo gri on CGI_GRI_SN=GRI_SN
                    inner join BIZ_ConfirmLineInfo coli on COLI_GRI_SN=GRI_SN
                    where 1=1
                    and CGI_ArriveDate between '$start_date' and '$end_date'
                    and exists (
                        select 1 from OperatorInfo where OPI_DEI_SN=30 and OPI_SN=CGI_OPI_SN
                    )
                    and CGI_Checked=1
                    and GRI_OrderType=227002
                    and COLI_State<>50

                    and exists (
                        select 1 from BIZ_ConfirmLineDetail where COLD_COLI_SN=coli.COLI_SN
                            and DeleteFlag=0
                            and COLD_PlanVEI_SN in ($all_vendor)
                    )
                    and exists (
						select 1 from GroupCombineInfo where GCI_GRI_SN=GRI_SN and GCI_VEI_SN in ($all_vendor)
							and GCI_travelDate between '$start_date' and '$end_date'
                    )

                ) as cgi_group";
                // -- and COLI_SN not in ($duplicate_gri)
                // -- and COLI_sourcetype=$sourcetype
        $query = $this->HT->query($sql);
        $opi_sum_money = $query->result_array();
        return $opi_sum_money;
    }

    // Trippest自营订单,含地接代收的
    public function trippest_order_with_vendormoney($vendor, $sourcetype, $start_date, $end_date)
    {
        $sql = "SELECT COLI_SN,coli_id,COLI_GroupCode,
                convert(date,cgi.CGI_ArriveDate) CGI_ArriveDate,
                COLI.COLI_sourcetype, COLI.COLI_Price, coli.COLI_Currency,
                (SELECT isnull(SUM(GAI_SSJE),0)
                 FROM BIZ_GroupAccountInfo
                 WHERE DeleteFlag=0
                   AND GAI_COLI_SN=COLI_SN
                   AND GAI_Type IN (" . PAY_OTHER . ")) AS vendor_sum ,
                    coli.COLI_OPI_ID
                FROM CK_GroupInfo cgi
                INNER JOIN GRoupInfo gri ON CGI_GRI_SN=GRI_SN
                INNER JOIN BIZ_ConfirmLineInfo coli ON COLI_GRI_SN=GRI_SN
                WHERE 1=1
                AND CGI_ArriveDate BETWEEN '$start_date' AND '$end_date'
                AND EXISTS
                  (SELECT 1
                   FROM OperatorInfo
                   WHERE OPI_DEI_SN=30
                     AND OPI_SN=CGI_OPI_SN)
                AND CGI_Checked=1
                AND GRI_OrderType=227002
                AND COLI_sourcetype=$sourcetype
                AND COLI_OPI_ID <> 435
                AND exists (
                    select 1 from BIZ_ConfirmLineDetail where COLD_COLI_SN=COLI_SN and COLD_PlanVEI_SN=$vendor
                )
                AND exists(
                    SELECT 1
                     FROM BIZ_GroupAccountInfo
                     WHERE DeleteFlag=0
                       AND GAI_COLI_SN=COLI_SN
                       AND GAI_SSJE>0
                       AND GAI_Type IN (" . PAY_OTHER . ")
                )
                ORDER BY vendor_sum ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function vendor_cost($vendor_str, $start_date, $end_date)
    {
        $sql = "SELECT tmp.vendor_code ,
                SUM(tmp.cost) vendor_cost
                from
                (
                    SELECT group_cost.GCI_VEI_SN vendor_code,
                    (SELECT SUM(CONVERT(float, gcod.GCOD_sumMoney))
                      FROM GroupCombineOperationDetail gcod
                      WHERE gcod.GCOD_GCI_combineNo=GCI_combineNo
                        AND gcod.GCOD_operationType <> 'otherReceives' ) AS cost
                    FROM
                    (SELECT DISTINCT gci.GCI_combineNo,
                    GCI_VEI_SN
                    FROM GroupCombineInfo gci
                    WHERE 1=1
                        AND GCI_VEI_SN IN ($vendor_str)
                        AND EXISTS
                        ( SELECT 1
                            FROM CK_GroupInfo
                            WHERE CGI_Checked=1
                            AND CGI_GRI_SN=GCI_GRI_SN
                            AND CGI_ArriveDate BETWEEN '$start_date' AND '$end_date')
                        and not exists (
                            select 1 from GroupCombineInfo g2 where g2.GCI_combineNo=gci.GCI_combineNo
                            and g2.GCI_VEI_SN<>gci.GCI_VEI_SN
                        )
                    ) AS group_cost
                  ) as tmp
                GROUP BY tmp.vendor_code  ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function trippest_order_multi_city($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT COLI_SN,COLI_ID,COLI_GroupCode,
                    (select top 1 ISNULL(GCI_combineNo,'') from GroupCombineInfo where GCI_GRI_SN=COLI_GRI_SN) as gci_no,
                    convert(date,cgi.CGI_ArriveDate) CGI_ArriveDate,
                    COLI.COLI_sourcetype, COLI.COLI_Price, coli.COLI_Currency,
                    (select isnull(SUM(dbo.ConvertToRMB('USD',COLD_TotalPrice)),0) from BIZ_ConfirmLineDetail
                        where COLD_COLI_SN=COLI_SN
                        and DeleteFlag=0
                        and COLD_PlanVEI_SN not in ($all_vendor)
                    ) as other_vendorprice_RMB
                    ,(select isnull(SUM(dbo.ConvertToRMB('USD',COLD_TotalPrice)),0) from BIZ_ConfirmLineDetail
                        where COLD_COLI_SN=COLI_SN
                        and DeleteFlag=0
                        and COLD_PlanVEI_SN in ($all_vendor)
                    ) as this_vendorprice_RMB
                    ,(SELECT isnull(SUM(GAI_SSJE),0)
                        FROM BIZ_GroupAccountInfo
                        WHERE DeleteFlag=0
                        AND GAI_COLI_SN=COLI_SN
                    ) AS money_sum
                    ,(select top 1 dbo.[GetVendorName](COLD_PlanVEI_SN,2) from BIZ_ConfirmLineDetail
                        where COLD_COLI_SN=COLI_SN
                        and DeleteFlag=0
                        and COLD_PlanVEI_SN  in ($all_vendor)
                    ) as vendor_name
                    ,(select SUM(convert(decimal(10,2),isnull(GCI_priceCNY,0))) from
                        GroupCombineInfo where GCI_GRI_SN=GRI_SN and GCI_VEI_SN in ($all_vendor)
                        --and COLI_OPI_ID<>435
                        and ISNULL(GCI_combineNo,'') not in ('','cancel')
                    ) as sync_price_sum
                FROM CK_GroupInfo cgi
                INNER JOIN GRoupInfo gri ON CGI_GRI_SN=GRI_SN
                INNER JOIN BIZ_ConfirmLineInfo coli ON COLI_GRI_SN=GRI_SN
                WHERE 1=1
                AND CGI_ArriveDate BETWEEN '$start_date' and '$end_date'
                AND EXISTS
                    (SELECT 1
                    FROM OperatorInfo
                    WHERE OPI_DEI_SN=30
                        AND OPI_SN=CGI_OPI_SN)
                AND CGI_Checked=1
                AND GRI_OrderType=227002
                AND COLI_OPI_ID <> 435
                AND exists (
                    select 1 from BIZ_ConfirmLineDetail d1 where COLD_COLI_SN=COLI_SN
                    and d1.DeleteFlag=0
                    and COLD_PlanVEI_SN in ($all_vendor) --in ($all_vendor)
                )
                AND exists (
                    select 1 from BIZ_ConfirmLineDetail d2 where COLD_COLI_SN=COLI_SN
                    and d2.DeleteFlag=0
                    and COLD_PlanVEI_SN not in ($all_vendor)
                )
                ORDER BY COLI_sourcetype ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function group_detail_list($vendor, $sourcetype, $start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT
                        (dbo.ConvertToRMB('USD',other_vendor_price)) as other_vendorprice_RMB,
                        case when other_vendor_price > 0 then
                                (total_income-dbo.ConvertToRMB('USD',other_vendor_price))
                            else haina_income_origin end
                        as haina_income,
                        *
                    from (
                        select
                            COLI_SN,
                            (select isnull(SUM(COLD_TotalPrice),0) from BIZ_ConfirmLineDetail
                                where COLD_COLI_SN=COLI_SN
                                --and COLD_ServiceType='D'
                                and DeleteFlag=0
                                and COLD_PlanVEI_SN<>$vendor
                                and COLD_PlanVEI_SN not in ($all_vendor)
                            ) as other_vendor_price,
                            COLI.COLI_sourcetype,
                            COLI.COLI_Price,
                            coli.COLI_Currency,
                            case when coli.COLI_Price <> 0 then
                                convert(decimal(10,2),round((select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                                    where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                                )/isnull(COLI.COLI_Price,1),2))
                            else 0 end  as exchange_rate,
                            (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                                where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                            ) as total_income,
                            (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                                where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                                and GAI_Type not in (" . PAY_OTHER . ")
                            ) as haina_income_origin,
                            (select isnull(SUM(GAI_SSJE),0) from BIZ_GroupAccountInfo
                                where DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                                and GAI_VEI_SN=$vendor
                                and GAI_Type in (" . PAY_OTHER . ")
                            ) as vendor_income
                            ,coli.COLI_OPI_ID
                            , gri.GRI_No
                            ,(select ROUND(SUM(isnull(tourcost,0)),4) from report_tour where ordernumber=COLI_ID and exists (
                                    select 1 from BIZ_ConfirmLineDetail where COLD_SN=RPT_COLD_SN
                                    and COLD_PlanVEI_SN=$vendor
                                )
                            ) as group_vendor_cost
                            ,(select sum(RPT_Total )
                                from report_tour
                                where ordernumber=COLI_ID and exists (
                                    select 1 from BIZ_ConfirmLineDetail where COLD_SN=RPT_COLD_SN
                                    and COLD_PlanVEI_SN=$vendor
                                )
                            ) as gcod_total
                            ,(select SUM(isnull(basemoney,0)) from report_order where gri_sn=RO_GRI_SN
                            ) as order_cost
                            ,(select top 1 GCI_combineNo from GroupCombineInfo where GRI_SN=GCI_GRI_SN) as gci_no
                        from CK_GroupInfo cgi
                        inner join GRoupInfo gri on CGI_GRI_SN=GRI_SN
                        inner join BIZ_ConfirmLineInfo coli on COLI_GRI_SN=GRI_SN
                        where 1=1
                        and CGI_ArriveDate between '$start_date' and '$end_date'
                        and exists (
                            select 1 from OperatorInfo where OPI_DEI_SN=30 and OPI_SN=CGI_OPI_SN
                        )
                        and CGI_Checked=1
                        and GRI_OrderType=227002

                        and exists (
                            select 1 from BIZ_ConfirmLineDetail where COLD_COLI_SN=coli.COLI_SN
                                and DeleteFlag=0
                                and COLD_PlanVEI_SN=$vendor
                        )

                    ) as cgi_group";
                    // and COLI_SN not in ($duplicate_gri)
                    // -- and COLI_sourcetype=$sourcetype
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function cost_debug_detail($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT
                    ordernumber,RPT_Total
                    ,round(tourCost/case isnull(tourRSd,0)+ISNULL(tourRSx,0) when 0 then 1 else isnull(tourRSd,0)+ISNULL(tourRSx,0) end *case isnull(RPT_PersonGrade,1) when 0 then 1 else isnull(RPT_PersonGrade,1) end,2) as cal_total
                    ,tourRSd,isnull(tourRSx,0) tourRSx,tourcost,RPT_PersonGrade,tourBZ,tourCode
                from report_tour rt
                inner join BIZ_ConfirmLineDetail cold on RPT_COLD_SN=COLD_SN
                where COLD_StartDate between '$start_date' and '$end_date'
                    and COLD_PlanVEI_SN in ($all_vendor)
                    and ISNULL(RPT_Total,0)<>case when SUBSTRING(tourBZ,1,3)='PVT' then tourcost
                        else round(
                        (tourCost/case isnull(tourRSd,0)+ISNULL(tourRSx,0) when 0 then 1 else isnull(tourRSd,0)+ISNULL(tourRSx,0) end
                            *case isnull(RPT_PersonGrade,1) when 0 then 1 else isnull(RPT_PersonGrade,1) end),2)
                        end
                    and exists (
                        select 1 from OperatorInfo
                        inner join BIZ_ConfirmLineInfo on  COLI_ID=ordernumber
                        where OPI_SN=COLI_OPI_ID and OPI_DEI_SN=30
                    )
                order by tourBZ desc";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_combine_cost($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT group_cost.GCI_VEI_SN vendor_code,GCI_combineNo,
                    (SELECT isnull(SUM(CONVERT(float, isnull(gcod.GCOD_sumMoney,0))),0)
                FROM GroupCombineOperationDetail gcod
                WHERE gcod.GCOD_GCI_combineNo=GCI_combineNo
                    AND gcod.GCOD_operationType<> 'otherReceives'
                    ) AS cost
                FROM
                (SELECT DISTINCT isnull(gci.GCI_combineNo,'?') GCI_combineNo,
                GCI_VEI_SN
                FROM GroupCombineInfo gci
                WHERE 1=1
                    AND GCI_VEI_SN IN ($all_vendor)
                    -- AND EXISTS
                    -- ( SELECT 1
                    --     FROM CK_GroupInfo
                    --     WHERE CGI_Checked=1
                    --     AND CGI_GRI_SN=GCI_GRI_SN
                    --     AND CGI_ArriveDate BETWEEN '$start_date' AND '$end_date')
                    and exists (
                        select 1 from BIZ_ConfirmLineInfo
                        inner join BIZ_ConfirmLineDetail d on COLI_SN=COLD_COLI_SN
                        left join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=d.COLD_SN
                        where COLI_GRI_SN=GCI_GRI_SN and d.DeleteFlag=0
                        and isnull(poi_quotationType,0)<>4
                        and d.COLD_PlanVEI_SN in ($all_vendor)
                        and COLD_StartDate BETWEEN '$start_date' AND '$end_date'
                    )
                ) AS group_cost
                order by GCI_combineNo desc";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_cancel_not_check($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT
            COLI_ID, COLI_GroupCode,GCI_combineNo,
            GCI_priceCNY
        from GroupCombineInfo gci
        inner join BIZ_ConfirmLineInfo coli on COLI_GRI_SN=GCI_GRI_SN
        where
            GCI_travelDate between '$start_date' and '$end_date'
            and convert(decimal(10,2),isnull(GCI_priceCNY,0))<>0
            and COLI_State in (30,40)
            and GCI_VEI_SN in ($all_vendor)
            and isnull(GCI_combineNo,'') not in ('','cancel','forbidden')
            and not	exists(
                select 1 from CK_GroupInfo where CGI_GRI_SN=COLI_GRI_SN
                and CGI_Checked=1
            )";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_precheck_price_error($start_date, $end_date, $all_vendor)
    {
        $expire_fresh = date('Y-m-d',strtotime("+3 months", strtotime($start_date)));
        $sql = "SELECT
                data_tmp.TLD_total_price-(HT_haina_receipt+HT_agency_receipt) as 'total_price_err', --总团款差额 (账单减HT)',
                case data_tmp.COLI_OPI_ID when 435
                    then data_tmp.TLD_total_price-HT_agency_receipt
                    else data_tmp.TLD_agency_receipt-HT_agency_receipt
                end as 'agency_receipt_err', --地接代收差额 (账单减HT)',
                case data_tmp.COLI_OPI_ID when 435
                    then TLD_total_price-HT_agency_receipt
                    else TLD_total_price-TLD_agency_receipt-HT_haina_receipt
                end as 'HT_receipt_err' --海纳收差额 (账单减HT)'
                ,data_tmp.total_income-(round(HT_haina_receipt,1)+ ROUND(HT_agency_receipt,1)) as 'total_income_err' --'海纳收入差额'
                ,(select top 1 GCI_combineNo from groupcombineinfo where gci_gri_sn=data_tmp.GRI_SN order by GCI_combineNo desc) as combineNo
                ,(select top 1 GCI_VendorOrderId from groupcombineinfo
                    where gci_gri_sn=data_tmp.GRI_SN and ( GCI_createTime<'$expire_fresh' or (GCI_createTime>'$expire_fresh' and isnull(convert(decimal(10,2),GCI_priceCNY),0)=0) )
                    order by GCI_createTime
                ) as vendor_order
                ,* from
                (
                select
                gri.GRI_Name,
                ro.money total_income,
                (select SUM(COLD_TotalPrice) from BIZ_ConfirmLineDetail d
                    LEFT join BIZ_PackageOrderInfo p on p.POI_COLD_SN=d.COLD_SN
                    where d.DeleteFlag=0
                    and COLD_COLI_SN=coli.COLI_SN
                    and ( d.COLD_PlanVEI_SN not in ($all_vendor) or
                        p.POI_QuotationType=4 )
                ) as other_price,
                (select isnull(SUM(cast(GCI_priceCNY as decimal(10,2))),0) from GroupCombineInfo where GCI_GRI_SN=COLI_GRI_SN and isnull(GCI_combineNo,'') not in ('','cancel','forbidden') ) as TLD_total_price,
                (select isnull(SUM(cast(GCI_agencyReceipt as decimal(10,2))),0) from GroupCombineInfo where GCI_GRI_SN=COLI_GRI_SN and isnull(GCI_combineNo,'') not in ('','cancel','forbidden') ) as TLD_agency_receipt,
                (select isnull(SUM(cast(GAI_SSJE as decimal(10,2))),0) from BIZ_GroupAccountInfo where GAI_COLI_SN=COLI_SN
                    --and GAI_Type in (" . PAY_OTHER . ")
                    AND GAI_VEI_SN in ($all_vendor,1343) -- 图兰朵的代收
                    and DeleteFlag=0
                ) as HT_agency_receipt,
                (select isnull(SUM(cast(GAI_SSJE as decimal(10,2))),0) from BIZ_GroupAccountInfo where GAI_COLI_SN=COLI_SN
                    --and GAI_Type not in (" . PAY_OTHER . ")
                    and DeleteFlag=0
                    and isnull(GAI_VEI_SN,30561) = 30561
                ) as HT_haina_receipt,
                (select top 1 CONVERT(varchar(10),ISNULL( GAI_SQJE,0)) + isnull(GAI_SQJECurrency,'')+',' + isnull(GAI_Memo,'') from BIZ_GroupAccountInfo ga where ga.DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                    and GAI_SQJE>0 and isnull(GAI_SSJE,0)=0
                ) as ht_receipt_memo,
                (select SUM(ABS(GAI_SSJE)) from BIZ_GroupAccountInfo ga where ga.DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                    and GAI_SSJE<0
                ) as refund,
                (select top 1 GAI_Memo from BIZ_GroupAccountInfo ga where ga.DeleteFlag=0 and GAI_COLI_SN=COLI_SN
                    and GAI_SSJE<=0
                ) as refund_memo,
                (select isnull(SUM(tourcost),0) from report_tour
                inner join BIZ_ConfirmLineDetail on COLD_SN=RPT_COLD_SN and COLD_PlanVEI_SN in ($all_vendor)
                where ordernumber=COLI_ID  ) AS cost
                ,coli.COLI_OPI_ID,COLI_Memo, COLI_SN,COLI_Currency
                ,gri.GRI_SN
                --,*
                    from
                    --CK_GroupInfo cgi inner join
                    GRoupInfo gri --on CGI_GRI_SN=GRI_SN
                    inner join BIZ_ConfirmLineInfo coli on COLI_GRI_SN=GRI_SN
                        and COLI_State not in (50)
                    inner join report_order ro on ro.ordernumber=COLI_ID
                    where 1=1
                    --and CGI_ArriveDate BETWEEN '$start_date' and '$end_date'
                    and exists (
                        select 1 from OperatorInfo where OPI_SN=COLI_OPI_ID and  (OPI_DEI_SN=30 or OPI_SN=435)
                    )
                    and ro.orderstats=1
                    and GRI_OrderType=227002
                    and exists (
                        select 1 from BIZ_ConfirmLineDetail where COLD_COLI_SN=coli.COLI_SN
                            and DeleteFlag=0
                            and COLD_PlanVEI_SN in ($all_vendor)
                            and COLD_StartDate BETWEEN '$start_date' and '$end_date'
                    )
                ) as data_tmp
                where 1=1
                -- 总团款不相等
                and data_tmp.TLD_total_price-(HT_haina_receipt+HT_agency_receipt)<>0
                -- 总收入和实收不相等
                OR data_tmp.total_income-(round(HT_haina_receipt,1)+ ROUND(HT_agency_receipt,1))<>0
                -- 地接代收不相等:同步
                OR (
                    data_tmp.COLI_OPI_ID=435 and HT_haina_receipt<>0 and TLD_agency_receipt<>0
                    and	data_tmp.TLD_total_price-HT_agency_receipt<>0
                    )
                -- 地接代收不相等:自营
                OR (
                    data_tmp.COLI_OPI_ID<>435 --and HT_haina_receipt<>0 and TLD_agency_receipt<>0
                    and	data_tmp.TLD_agency_receipt-HT_agency_receipt<>0
                    )
                -- 海纳收不相等:同步
                OR (
                    data_tmp.COLI_OPI_ID=435
                    and (HT_haina_receipt<>0 or TLD_total_price-HT_agency_receipt<>0)
                )
                -- 海纳收不相等:自营
                OR (
                    data_tmp.COLI_OPI_ID<>435
                    and TLD_total_price-TLD_agency_receipt-HT_haina_receipt<>0
                )
                OR exists (
                    select 1 from groupcombineinfo
                    where gci_gri_sn=data_tmp.GRI_SN and ( GCI_createTime<'$expire_fresh' AND GCI_combineNo is null)
                )
                order by total_income_err desc,agency_receipt_err desc,HT_receipt_err desc";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_order_not_found_gci($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT *
                from BIZ_ConfirmLineInfo coli
                inner join GRoupInfo gri on gri.GRI_SN=COLI_GRI_SN
                where COLI_OPI_ID=435 and ISNULL(COLI_SenderIP,'')=''
                and not exists (
                    select 1 from GroupCombineInfo where GCI_GRI_SN=GRI_SN
                )
                and exists (
                    select 1 from BIZ_ConfirmLineDetail where COLD_COLI_SN=COLI_SN
                        and COLD_PlanVEI_SN in ($all_vendor)
                        and COLD_StartDate between '$start_date' and '$end_date'
                )
                and isnull(coli.DeleteFlag,0)=0
                and COLI_IsSuccess=1
                order by COLI_ApplyDate desc";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_sync_price_cost($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT
                    SUM(combine.SyncHainaReceipt) as SyncHainaReceipt,
                    SUM(combine.SyncAgencyReceipt) as SyncAgencyReceipt,
                    SUM(combine.SyncCombineCost) as SyncCombineCost,
                    combine.GCI_VEI_SN as vendor_code
                from (
                    select
                        SUM(gri_coli.SyncPrice) as SyncPrice,
                        SUM(gri_coli.SyncAgencyReceipt) as SyncAgencyReceipt,
                        SUM(gri_coli.SyncHainaReceipt) as SyncHainaReceipt,
                        (select SUM(CONVERT(decimal(10,2), gcod.GCOD_sumMoney)) total_cost
                            from GroupCombineOperationDetail gcod
                            where gcod.GCOD_GCI_combineNo=gri_coli.GCI_combineNo and gcod.GCOD_VEI_SN=gri_coli.GCI_VEI_SN
                        ) as SyncCombineCost,
                        gri_coli.GCI_combineNo,
                        gri_coli.GCI_VEI_SN
                    from (
                        select
                            convert(decimal(10,2),isnull(gci.GCI_priceCNY,0)) as SyncPrice,
                            case when COLI_OPI_ID=435 then CONVERT(decimal(10,2),isnull(gci.GCI_priceCNY,0))
                                else convert(decimal(10,2),isnull(gci.GCI_agencyReceipt,0)) end
                            as SyncAgencyReceipt,
                            case when COLI_OPI_ID=435 then 0
                                else (convert(decimal(10,2),isnull(gci.GCI_priceCNY,0))-convert(decimal(10,2),isnull(gci.GCI_agencyReceipt,0))) end
                            as SyncHainaReceipt,
                            GCI_createTime as lastSynsTime,
                            GCI_combineNo,
                            GCI_VEI_SN
                        from GroupCombineInfo gci
                        inner join BIZ_ConfirmLineInfo coli on COLI_GRI_SN=GCI_GRI_SN
                        where
                            GCI_travelDate between '$start_date' and '$end_date'
                            and COLI_State<>50
                            and isnull(GCI_combineNo,'') not in ('','cancel','forbidden')
                    ) as gri_coli
                    group by GCI_combineNo,
                            GCI_VEI_SN
                ) as combine
                group by combine.GCI_VEI_SN
                order by vendor_code";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_duplicate_vei_combineno($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT
                    duplicate_gri.*
                    ,(
                        SELECT  isnull(SUM(GAI_SSJE),0)
                        FROM BIZ_GroupAccountInfo
                        WHERE DeleteFlag=0
                        AND GAI_GRI_SN=COLI_SN
                        AND GAI_VEI_SN IN ($all_vendor)
                        AND GAI_Type IN (" . PAY_OTHER . ")
                    ) AS vendor_sum
                    ,(
                        SELECT  isnull(SUM(GAI_SSJE),0)
                        FROM BIZ_GroupAccountInfo
                        WHERE DeleteFlag=0
                        AND GAI_COLI_SN=COLI_SN ) AS total_receipt
                    ,(
                        SELECT  isnull(SUM(GAI_SSJE),0)
                        FROM BIZ_GroupAccountInfo
                        WHERE DeleteFlag=0
                        AND GAI_COLI_SN=COLI_SN
                        AND GAI_Type not IN (" . PAY_OTHER . ") ) AS trippest_sum
                    ,0 AS other_price_sum
                    ,0 AS other_cost_sum
                    ,0 AS other_vendorprice_RMB_sum
                    from(
                        select tmp.GCI_combineNo, tmp.GCI_SN, tmp.GCI_VEI_SN vendor1,real_unique.*
                        ,(SELECT  top 1 c.COLI_SN FROM BIZ_ConfirmLineInfo c WHERE c.coli_gri_sn=real_unique.gci_gri_sn) as COLI_SN
                        ,(SELECT  top 1 GRI_No FROM groupInfo where gri_sn=real_unique.gci_gri_sn) as gri_no
                        ,(
                            SELECT  SUM(tourcost)
                            FROM BIZ_ConfirmLineInfo
                            INNER JOIN GroupCombineInfo
                            ON COLI_GRI_SN=GCI_GRI_SN
                            INNER JOIN report_tour
                            ON ordernumber=COLI_ID
                            WHERE 1=1
                            AND GCI_VEI_SN IN ($all_vendor)
                            and GCI_GRI_SN=tmp.GCI_GRI_SN
                            AND exists (
                            SELECT  1
                            FROM VEndorInfo2
                            WHERE VEI2_LGC=2
                            AND VEI2_VEI_SN=GCI_VEI_SN
                            AND VEI2_CompanyBN=tourProvide )
                        ) AS report_cost
                        ,(
                            SELECT  isnull(SUM(CONVERT(float,isnull(gcod.GCOD_sumMoney,0))),0)
                            FROM GroupCombineOperationDetail gcod
                            WHERE gcod.GCOD_GCI_combineNo=tmp.GCI_combineNo
                            AND gcod.GCOD_operationType<> 'otherReceives' ) AS pure_cost
                        from GroupCombineInfo tmp
                        inner join (
                            select GCI_GRI_SN
                            from  GroupCombineInfo unique_gci
                            where GCI_travelDate between '$start_date' AND '$end_date'
                            and isnull(GCI_combineNo,'') not in ('','cancel','forbidden')
                            and isnull(convert(decimal(10,2), GCI_priceCNY),0)>0
                            group by GCI_GRI_SN
                            having count(GCI_GRI_SN)>1
                        ) as real_unique on real_unique.GCI_GRI_SN=tmp.GCI_GRI_SN
                        where isnull(tmp.GCI_combineNo,'') not in ('','cancel','forbidden')
                    ) as duplicate_gri
                    order by duplicate_gri.COLI_SN
        ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function report_tour_diff($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT * ,
    (SELECT top 1 RPT_Total
    FROM report_tour
    WHERE 1=1
            AND tourProvide=gci_vei_name
            AND tourBZ LIKE '%'+REPLACE(gci_no,'[','[[]')+'%'
            AND ( RPT_Total<>to_be_diff.report_total_cost0
            OR RPT_Total<>to_be_diff.total_cost ) ) AS diff_report_total_cost0
    ,case isnull(to_be_diff.no_fresh, 0) when 0 then 1 else 0 end to_fresh
FROM
    (SELECT
        (select top 1 1
                from BIZ_ConfirmLineInfo
                inner join GroupCombineInfo cc on cc.GCI_GRI_SN=COLI_GRI_SN
                        and GCI_combineNo=all_gci_no.gci_no
                where
                exists (
                    select 1 from BIZ_ConfirmLineDetail
                    where COLD_COLI_SN=COLI_SN
                    and DeleteFlag=0
                    and COLD_PlanVEI_SN not in ($all_vendor)
                )
        ) as no_fresh
        , * ,
        (SELECT isnull(SUM(CONVERT(float,gcod.GCOD_sumMoney)),0)
        FROM GroupCombineOperationDetail gcod
        WHERE GCOD_GCI_combineNo=all_gci_no.gci_no
                AND gcod.GCOD_operationType <> 'otherReceives'
                AND GCOD_VEI_SN=all_gci_no.GCI_VEI_SN
        ) AS total_cost ,
        (SELECT isnull(round(SUM(isnull(tourcost,0)),2),0)
        FROM report_tour
        WHERE 1=1
                AND ( tourProvide=gci_vei_name OR tourProvide='' )
                -- AND ( tourProvide=gci_vei_name  )
                -- AND tourBZ LIKE '%'+REPLACE(gci_no,'[','[[]')+'%'
                and CHARINDEX(gci_no, tourBZ) > 0
                and exists (
                    select 1 from BIZ_ConfirmLineInfo where COLI_ID=ordernumber and COLI_State<>50
                )
        ) AS report_cost ,
        (SELECT top 1 RPT_Total
        FROM report_tour
        WHERE 1=1
            AND tourProvide=gci_vei_name
            AND tourBZ LIKE '%'+REPLACE(gci_no,'[','[[]')+'%'
        ) AS report_total_cost0
        FROM ( select
            CASE WHEN GCI_combineNo is NULL THEN 'nn'
                WHEN GCI_combineNo='cancel' THEN CONVERT(varchar(50), 'nn')
                ELSE GCI_combineNo END gci_no ,
            GCI_VEI_SN ,
            (SELECT VEI2_CompanyBN
            FROM VEndorInfo2
            WHERE VEI2_VEI_SN=GCI_VEI_SN
                    AND VEI2_LGC=2 ) AS gci_vei_name
            FROM GroupCombineInfo
            WHERE GCI_travelDate
                BETWEEN '$start_date'
                    AND '$end_date'
                and GCI_VEI_SN in ($all_vendor)
                -- and CHARINDEX('TA', GCI_combineNo ) = 0
            GROUP BY  GCI_combineNo,GCI_VEI_SN
        ) all_gci_no
    ) AS to_be_diff
WHERE CONVERT(decimal(10,2),to_be_diff.report_cost) <> to_be_diff.report_total_cost0
    OR CONVERT(decimal(10,2),to_be_diff.total_cost) <> to_be_diff.report_total_cost0
    OR CONVERT(decimal(10,2),to_be_diff.total_cost) <> round(to_be_diff.report_cost,2)
ORDER BY  to_be_diff.GCI_VEI_SN
        ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

    public function get_gci_coli($gci)
    {
        $sql = "SELECT COLI_SN, COLI_ID, COLI_GroupCode
                    ,(
                        select COUNT(COLD_SN) from BIZ_ConfirmLineDetail
                        where COLD_COLI_SN=COLI_SN
                        and DeleteFlag=0
                        and COLD_PlanVEI_SN not in (1343,29188,30548)
                    ) as no_fresh
                    ,(
                        select COUNT(CGI_SN) from CK_GroupInfo  k
                        where k.CGI_GRI_SN=GCI_GRI_SN and CGI_Checked=1
                    ) as cgi_check
                from BIZ_ConfirmLineInfo
                inner join GroupCombineInfo cc on cc.GCI_GRI_SN=COLI_GRI_SN
                        and GCI_combineNo=?";
        $query = $this->HT->query($sql, array($gci));
        return $query->result_array();
    }

    public function agreement_diff($start_date, $end_date, $all_vendor)
    {
        $sql = "SELECT *
            ,ROUND( dbo.GetSSJEFromSQJE(
                (select top 1 GAI_Type from BIZ_GroupAccountInfo b where gai_coli_sn=coli_sn and b.DeleteFlag=0)
                ,COLI_Currency,com_sum_price),2) as com_Currency_CNY_price
            ,ROUND( dbo.GetSSJEFromSQJE(
                (select top 1 GAI_Type from BIZ_GroupAccountInfo b where gai_coli_sn=coli_sn and b.DeleteFlag=0)
                ,COLI_Currency,not_com_sum_price),2) as not_com_Currency_CNY_price
            --,ROUND( dbo.GetSSJEFromSQJE(
            --	(select top 1 GAI_Type from BIZ_GroupAccountInfo b where gai_coli_sn=coli_sn and b.DeleteFlag=0)
            --	,'USD',com_sum_price),2) as USD_CNY_price
            ,(select SUM(GAI_SSJE) from BIZ_GroupAccountInfo gai where gai.GAI_COLI_SN=COLI_SN and gai.DeleteFlag=0) as total_income
        from (
            select
            COLI_ID,COLI_GroupCode,COLI_SN,COLI_Currency,COLI_GRI_SN
            ,(
                select SUM(cold.COLD_TotalPrice) from BIZ_ConfirmLineDetail cold
                left join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=cold.COLD_SN
                where cold.COLD_COLI_SN=coli.COLI_SN
                and COLD_StartDate between '$start_date' and '$end_date'
                and cold.DeleteFlag=0
                and poi_quotationType=4
                and COLD_PlanVEI_SN in ($all_vendor)
            ) as com_sum_price
            ,isnull((
                select SUM(cold.COLD_TotalPrice) from BIZ_ConfirmLineDetail cold
                left join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=cold.COLD_SN
                where cold.COLD_COLI_SN=coli.COLI_SN
                and COLD_StartDate between '$start_date' and '$end_date'
                and cold.DeleteFlag=0
                and isnull(poi_quotationType,0)<>4
                and COLD_PlanVEI_SN in ($all_vendor)
            ),0) as not_com_sum_price
            ,isnull((
                select SUM(CONVERT(decimal(10,2), isnull(GCI_priceCNY,0))) from GroupCombineInfo where GCI_GRI_SN=coli.COLI_GRI_SN
                    and isnull(GCI_combineNo,'') not in ('','cancel','forbidden')
            ),0) as in_gci
            from
            BIZ_ConfirmLineInfo coli
            where  exists (
                    select 1 from OperatorInfo where OPI_DEI_SN=30 and OPI_SN=COLI_OPI_ID
                )
            and exists (
                select 1 from BIZ_ConfirmLineDetail cold
                left join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=cold.COLD_SN
                where cold.COLD_COLI_SN=coli.COLI_SN
                and COLD_StartDate between '$start_date' and '$end_date'
                and cold.DeleteFlag=0
                and poi_quotationType=4
                and COLD_PlanVEI_SN in ($all_vendor)
            )
        ) as all_comprehensive
        where in_gci>0 ";
        $query = $this->HT->query($sql);
        return $query->result_array();
    }

}

/* End of file vendor_money.php */
/* Location: ./third_party/trippestOrderSync/models/vendor_money.php */
