<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Report_model extends CI_Model {

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

    /** 插入对账单记录 */
    public function new_report($warrant_arr = array())
    {
        $sql = "INSERT INTO PaymentReports
                    (
                    PR_payType
                   ,PR_orderType
                   ,PR_COLI_SN
                   ,PR_orderId
                   ,PR_dealType
                   ,PR_AccreditNo
                   ,PR_currency
                   ,PR_amount
                   ,PR_rate
                   ,PR_entryCurrency
                   ,PR_entryAmount
                   ,PR_fee
                   ,PR_status
                   ,PR_buyerName
                   ,PR_buyerEmail
                   ,PR_paymentTime
                   ,PR_time
                )
                VALUES
                    (?,?,?,?,N?,?,?,?,?,?,?,?,N?,N?,N?,?
                    ,GETDATE()
                )";
        $query = $this->INFO->query($sql,array(
            $warrant_arr["PR_payType"]
            ,$warrant_arr["PR_orderType"]
            ,$warrant_arr["PR_COLI_SN"]
            ,$warrant_arr["PR_orderId"]
            ,$warrant_arr["PR_dealType"]
            ,$warrant_arr["PR_AccreditNo"]
            ,$warrant_arr["PR_currency"]
            ,$warrant_arr["PR_amount"]
            ,$warrant_arr["PR_rate"]
            ,$warrant_arr["PR_entryCurrency"]
            ,$warrant_arr["PR_entryAmount"]
            ,$warrant_arr["PR_fee"]
            ,$warrant_arr["PR_status"]
            ,$warrant_arr["PR_buyerName"]
            ,$warrant_arr["PR_buyerEmail"]
            ,$warrant_arr["PR_paymentTime"]
        ));
        return $query;
    }

    /*!
     * 在ipaylinks对账单中, 未找到对应的HT订单记录
     * * 金额不相等(包括退款后金额不等)
     * * HT中没有自动录入的收款记录, 收款记录被删除或没有成功录入(需要查询问题)
     * @author LYT <lyt@hainatravel.com>
     * @date   2018-03-28
     * @return [type]     [description]
     */
    public function ipaylinks_abnormal_in_HT()
    {
      $sql = "SELECT PR.*
              FROM [InfoManager].[dbo].[PaymentReports] pr
              WHERE pr.PR_dealType='清算' and PR_payType='15018'
              EXCEPT
                SELECT PR.*
                FROM [InfoManager].[dbo].[PaymentReports] pr
                INNER JOIN Tourmanager.dbo.BIZ_GroupAccountInfo bgai ON bgai.GAI_COLI_ID=pr.PR_orderId
                AND pr.PR_dealType='清算'
                INNER JOIN Tourmanager.dbo.BIZ_ConfirmLineInfo bcoli ON bcoli.COLI_SN=bgai.GAI_COLI_SN
                AND bcoli.COLI_Department = 16 WHERE PR_amount=bgai.GAI_SQJE and PR_payType='15018'
                AND GAI_SQJECurrency=PR_currency
              EXCEPT
                SELECT PR.*
                FROM Tourmanager.dbo.GroupAccountInfo gai
                INNER JOIN Tourmanager.dbo.ConfirmLineInfo coli ON coli.COLI_SN=gai.GAI_COLI_SN
                AND coli.COLI_Department <> 16
                INNER JOIN [InfoManager].[dbo].[PaymentReports] pr ON gai.GAI_AccreditNo=pr.PR_AccreditNo WHERE pr.PR_dealType='清算'
                AND PR_amount = gai.GAI_SQJE and PR_payType='15018'
              EXCEPT
                SELECT PR.*
                FROM Tourmanager.dbo.BIZ_GroupAccountInfo bgai
                INNER JOIN Tourmanager.dbo.BIZ_ConfirmLineInfo bcoli ON bcoli.COLI_SN=bgai.GAI_COLI_SN
                AND bcoli.COLI_Department <> 16
                INNER JOIN [InfoManager].[dbo].[PaymentReports] pr ON bgai.GAI_AccreditNo=pr.PR_AccreditNo WHERE pr.PR_dealType='清算'
                AND PR_amount = bgai.GAI_SQJE and PR_payType='15018'
                ORDER BY PR_paymentTime asc    ";
      $query = $this->INFO->query($sql);
      return $query->result();
    }

    public function HT_no_dealId()
    {
      $sql = "SELECT GAI_Type,Bgai1.GAI_SQJECurrency,GAI_SQJE,
                     Bgai1.GAI_Memo ,
                     '商务--B' AS orderType,
                     bcoli.COLI_SN,
                     bcoli.COLI_ID AS COLI_ID,
                     Bgai1.GAI_AccountDate
              FROM Tourmanager.dbo.BIZ_GroupAccountInfo Bgai1
              INNER JOIN Tourmanager.dbo.BIZ_ConfirmLineInfo bcoli ON bgai1.GAI_COLI_SN=bcoli.COLI_SN
              WHERE Bgai1.GAI_Type IN ('15018')
                AND Bgai1.GAI_AccreditNo IS NULL
                AND Bgai1.GAI_SQJE > 0
                AND bcoli.COLI_Department <> 16
              UNION
              SELECT GAI_Type,gai1.GAI_SQJECurrency,GAI_SQJE,
                     gai1.GAI_Memo ,
                     '传统--T' AS orderType,
                     coli.COLI_SN,
                     coli.COLI_ID AS COLI_ID,
                     gai1.GAI_AccountDate
              FROM Tourmanager.dbo.GroupAccountInfo gai1
              INNER JOIN Tourmanager.dbo.ConfirmLineInfo coli ON gai1.GAI_COLI_SN=coli.COLI_SN
              WHERE gai1.GAI_Type IN ('15018')
                AND gai1.GAI_AccreditNo IS NULL
                AND gai1.GAI_SQJE > 0
                AND coli.COLI_Department <> 16
              ORDER BY  GAI_AccountDate   ";
      $query = $this->INFO->query($sql);
      return $query->result();
    }

}
