﻿AG.Data.Bond = {
    GetBidingSchedule: function (startIndex, endIndex) {
        var obj = AG.ClientAPI;
        var order = [{ Field: 'Code', Direction: 'ASC'}];
        var requestName = 'Schedule' + '_' + startIndex + '_' + endIndex;
        return obj.createData(BondBiding, requestName, [{ StartDate: AGFS.DateUtils.Now() }, { StartIndex: startIndex }, { EndIndex: endIndex }, { Type: AG.Config.BondBidingType.Schedule}], { static: ['Id', 'FullNameOfIssuer', 'ParValue', 'AuctionDate', 'Amount', 'Term', 'IssueDate', 'MaturityDate'] }, order);
    },
    GetCountBidingSchedule: function () {
        var obj = BondBiding;
        return obj.getCount([{ StartDate: AGFS.DateUtils.Now() }, { Type: AG.Config.BondBidingType.Schedule}]);
    },
    GetBidingResult: function (startDate, endDate, startIndex, endIndex, orderBy, orderType) {
        var obj = AG.ClientAPI;
        var order = [];
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var requestName = 'Result' + '_' + startDate + '_' + endDate + '_' + startIndex + '_' + endIndex + '_' + orderBy + '_' + orderType;
        return obj.createData(BondBiding, requestName, [{ StartDate: startDate }, { EndDate: endDate }, { StartIndex: startIndex }, { EndIndex: endIndex }, { Type: AG.Config.BondBidingType.Result}], { static: ['Id', 'AuctionDate', 'FullNameOfIssuer', 'IssueDate', 'MaturityDate', 'Term', 'OfferBLNDONG', 'BidYieldPCT', 'AcceptBLNDONG', 'AcceptYieldPCT', 'NoOfBidders'] }, order);
    },
    GetCountBidingResult: function (startDate, endDate) {
        var obj = BondBiding;
        return obj.getCount([{ StartDate: startDate }, { EndDate: endDate }, { Type: AG.Config.BondBidingType.Result}]);
    },
    GetBidingResultToExcel: function (startDate, endDate) {
        var obj = BondBiding;
        return obj.toExcel('BidingResult', [{ StartDate: startDate }, { EndDate: endDate }, { Type: AG.Config.BondBidingType.Result}], { static: ['Id', 'AuctionDate', 'FullNameOfIssuer', 'IssueDate', 'MaturityDate', 'Term', 'OfferBLNDONG', 'BidYieldPCT', 'AcceptBLNDONG', 'AcceptYieldPCT', 'NoOfBidders'] });
    },
    GetTradingResultTotalMarket: function (tradingDate, startIndex, endIndex, orderBy, orderType) {
        var obj = AG.ClientAPI;
        var order = [];
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var requestName = 'TradingResultTotalMarket' + '_' + tradingType + '_' + orderBy + '_' + orderType + '_' + startIndex + '_' + endIndex;
        return obj.createData(BondExchangeDaily, requestName, [{ TradingDate: tradingDate }, { StartIndex: startIndex }, { EndIndex: endIndex }, { Type: AG.Config.BondBidingType.Result}], { static: ['Id', 'Session', 'TradingDate', 'NumberOfConventionalOutrightTrades', 'TotalTradingVolumeOfConventionalOutrightTrades', 'TotalTradingValueOfConventionalOutrightTrades', 'NumberOfElectronicOutrightTrades', 'TotalTradingVolumeOfElectronicOutrightTrades', 'TotalTradingValueOfElectronicOutrightTrades', 'NumberOfReposTrades', 'TotalTradingVolumeOfReposTrades', 'TotalTradingValueOfReposTrades'] }, order);
    },
    GetCountTradingResultTotalMarket: function (tradingDate) {
        var obj = BondExchangeDaily;
        return obj.getCount([{ TradingDate: tradingDate}]);
    },
    GetTradingResultTotalMarketToExcel: function (tradingDate) {
        var obj = BondExchangeDaily;
        return obj.toExcel('TotalMarket', [{ TradingDate: tradingDate}], { static: ['Id', 'Session', 'TradingDate', 'NumberOfConventionalOutrightTrades', 'TotalTradingVolumeOfConventionalOutrightTrades', 'TotalTradingValueOfConventionalOutrightTrades', 'NumberOfElectronicOutrightTrades', 'TotalTradingVolumeOfElectronicOutrightTrades', 'TotalTradingValueOfElectronicOutrightTrades', 'NumberOfReposTrades', 'TotalTradingVolumeOfReposTrades', 'TotalTradingValueOfReposTrades'] });
    },
    GetTradingResultOutright: function (code, startDate, endDate, startIndex, endIndex, orderBy, orderType) {
        var obj = AG.ClientAPI;
        var order = [];
        var data = [{ StartDate: startDate }, { EndDate: endDate }, { StartIndex: startIndex }, { EndIndex: endIndex}];
        if (typeof (code) != "undefined" && code != '')
            data.push({ Code: code });
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var requestName = 'TradingResultOutright' + '_' + code + '_' + startDate + '_' + endDate + '_' + orderBy + '_' + orderType + '_' + startIndex + '_' + endIndex;
        return obj.createData(BondDaily, requestName, data, { static: ['Id', 'Code', 'Coupon', 'RemainingMaturity', 'OpenPrice', 'ClosePrice', 'HighestPrice', 'LowestPrice', 'TotalTradingVolume', 'TotalTradingValue', 'TradingDate', 'CloseInterest'], dynamic: ['OpenYield', 'CloseYield', 'HighestYield', 'LowestYield', 'Term', ] }, order);
    },
    GetCountTradingResultOutright: function (code, startDate, endDate) {
        var obj = BondDaily;
        //var data = [{ StartDate: startDate }, { EndDate: endDate }, { Type: 'B943E0FF-68BC-41C0-8A83-80FC0B50CB32'}];
        var data = [{ StartDate: startDate }, { EndDate: endDate}];
        if (typeof (code) != "undefined" && code != '')
            data.push({ Code: code });
        return obj.getCount(data);
    },
    GetTradingResultOutrightToExcel: function (code, startDate, endDate) {
        var obj = BondDaily;
        var data = [{ StartDate: startDate }, { EndDate: endDate }, { Type: 'B943E0FF-68BC-41C0-8A83-80FC0B50CB32'}];
        if (typeof (code) != "undefined" && code != '')
            data.push({ Code: code });
        return obj.toExcel('Outright', data, { static: ['Id', 'Code', 'Coupon', 'RemainingMaturity', 'OpenPrice', 'ClosePrice', 'HighestPrice', 'LowestPrice', 'TotalTradingVolume', 'TotalTradingValue', 'TradingDate'], dynamic: ['OpenYield', 'CloseYield', 'HighestYield', 'LowestYield', 'Term'] });
    },
    GetTradingResultRepos: function (tradingDate, startIndex, endIndex, orderBy, orderType) {
        var obj = AG.ClientAPI;
        var order = [];
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var requestName = 'TradingResultRepos' + '_' + tradingType + '_' + orderBy + '_' + orderType + '_' + startIndex + '_' + endIndex;
        return obj.createData(BondDaily, requestName, [{ TradingDate: tradingDate }, { StartIndex: startIndex }, { EndIndex: endIndex }, { Type: AG.Config.TradingResult.Repos}], { static: ['Id', 'ReposTerm', 'HighestInterest', 'LowestInterest', 'OpenInterest', 'CloseInterest', 'TotalFirstTradingValue', 'TotalSecondTradingValue', 'HighestRiskPremium', 'LowestRiskPremium'] }, order);
    },
    GetCountTradingResultRepos: function (tradingDate) {
        var obj = BondDaily;
        return obj.getCount([{ TradingDate: tradingDate}]);
    },
    GetTradingResultReposToExcel: function (tradingDate) {
        var obj = BondDaily;
        return obj.toExcel('Repos', [{ TradingDate: tradingDate }, { Type: AG.Config.TradingResult.Repos}], { static: ['Id', 'ReposTerm', 'HighestInterest', 'LowestInterest', 'OpenInterest', 'CloseInterest', 'TotalFirstTradingValue', 'TotalSecondTradingValue', 'HighestRiskPremium', 'LowestRiskPremium'] });
    },
    GetListed: function (issuer, bondType, termStart, termEnd, yield, startDate, endDate, startIndex, endIndex, orderBy, orderType) {
        var obj = AG.ClientAPI;
        var order = [];
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var data = [];
        if (issuer != '')
            data.push({ IssuerName: issuer });
        if (bondType != '')
            data.push({ Type: bondType });
        if (yield != '')
            data.push({ InterestRate: yield });
        data.push({ StartIndex: startIndex });
        data.push({ EndIndex: endIndex });
        data.push({ StartPeriod: termStart });
        data.push({ EndPeriod: termEnd });
        var filter = [];
        if (startDate != '')
            filter.push({ Field: 'MaturityDate', Index: 3, Type: 'Static', Operator: '>=', Value: startDate });
        if (endDate != '')
            filter.push({ Field: 'MaturityDate', Index: 4, Type: 'Static', Operator: '<=', Value: endDate });
        var requestName = 'Listed' + '_' + issuer + '_' + bondType + '_' + termStart + '_' + termEnd + '_' + yield + '_' + startDate + '_' + endDate + '_' + startIndex + '_' + endIndex + '_' + orderBy + '_' + orderType;
        return obj.createData(BondRealtime, requestName, data, { static: ['Id', 'Code', 'ListingDate', 'OutstandingVolume', 'InterestPaymentMode', 'IssueDate', 'MaturityDate', 'Coupon', 'RemainingMaturity', 'Period', 'ParValue', 'InterestRate', 'FirstTradingDate', 'IssuerName'] }, order, filter);
    },
    GetCountListed: function (issuer, bondType, termStart, termEnd, yield, startDate, endDate, orderBy, orderType) {
        var obj = BondRealtime;
        var data = [];
        if (issuer != '')
            data.push({ IssuerName: issuer });
        if (bondType != '')
            data.push({ Type: bondType });
        if (yield != '')
            data.push({ InterestRate: yield });
        var filter = [];
        data.push({ StartPeriod: termStart });
        data.push({ EndPeriod: termEnd });
        if (startDate != '')
            filter.push({ Field: 'MaturityDate', Type: 'Static', Operator: '>=', Value: startDate });
        if (endDate != '')
            filter.push({ Field: 'MaturityDate', Type: 'Static', Operator: '<=', Value: endDate });
        return obj.getCount(data, undefined, undefined, filter);
    },
    GetListedToExcel: function (issuer, bondType, termStart, termEnd, yield, startDate, endDate) {
        var obj = BondRealtime;
        var order = [];
        if (typeof (orderBy) != "undefined")
            order = [{ Field: orderBy, Direction: 'ASC'}];
        else
            order = [{ Field: 'ID', Direction: 'ASC'}];
        if (typeof (orderType) != "undefined") {
            order[0].Direction = orderType;
        }
        if (orderBy != 'Code')
            order.push({ Field: 'Code', Direction: 'ASC' });
        var data = [];
        if (issuer != '')
            data.push({ IssuerName: issuer });
        if (bondType != '')
            data.push({ Type: bondType });
        if (yield != '')
            data.push({ InterestRate: yield });
        data.push({ StartIndex: startIndex });
        data.push({ EndIndex: endIndex });
        var filter = [];
        if (termStart != '')
            filter.push({ Field: 'Period', Type: 'Static', Operator: '>=', Value: termStart });
        if (termEnd != '')
            filter.push({ Field: 'Period', Type: 'Static', Operator: '<=', Value: termEnd });
        if (startDate != '')
            filter.push({ Field: 'MaturityDate', Type: 'Static', Operator: '>=', Value: startDate });
        if (endDate != '')
            filter.push({ Field: 'MaturityDate', Type: 'Static', Operator: '<=', Value: endDate });
        obj.toExcel('ListedBond', data, undefined, order, filter);
    },
    GetDetail: function (code) {
        var obj = AG.ClientAPI;
        var requestName = 'Detail' + '_' + code;
        return obj.createData(BondRealtime, requestName, [{ Code: code}], { static: ['Id', 'IssuerName', 'IssuerHeadOffice', 'IssuerTel', 'IssuerFax', 'ParValue', 'OutstandingVolume', 'OutstandingValue', 'FormOfIssue', 'Period', 'InterestRate', 'InterestPaymentMode', 'IssueDate', 'MaturityDate', 'ListingDate', 'FirstTradingDate'] });
    },
    //ket qua giao dich
    GetTrade: function (bond) {
        return AG.ClientAPI.createData(BondDaily, 'GetTrade' + bond, [{ Code: bond}], { static: ['Id', 'TradingDate', 'TotalTradingVolume', 'ClosePrice', 'TotalTradingValue', 'CloseInterest'] });
    },
    //so ban ghi ket qua giao dich
    GetCountTrade: function (bond) {
        return BondDaily.getCount([{ Code: bond}]);
    },
    //lay danh sach loai trai phieu
    GetBondType: function () {
        var objBondType = AG.ClientAPI.createData(Config, 'GetBondType', [{ Code: 'BondType'}], { static: ['Id'] });
        if (objBondType != null) {
            return AG.ClientAPI.createData(Config, 'GetBondType' + objBondType[0].Id, [{ Type: objBondType[0].Id}], { static: ['Id', 'Code', 'Name'] });
        }
        else
            return [];
    },
    //danh sach ma trai phieu
    GetListBond: function () {
        return AG.ClientAPI.createData(BondRealtime, 'GetListBond', [{}], { static: ['Id', 'Code', 'Name'] });
    }
}
