var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var FOutlay;
(function (FOutlay) {
    var TableRules = (function () {
        function TableRules(_document) {
            this._document = _document;
            this._filterPart3 = ["450", "460", "5..", "6..", "7..", "810", "820", "830", "840", "860", "870"];
            this._filterPart5_2 = ["31.", "330"];
            this._filterPart5_3_1 = ["32."];
            this._filterPart5_3_2 = ["2.."];
            this._filterPart5_3_3 = ["52.", "530", "540", "81."];
        }
        TableRules.prototype.CheckByFilter = function (sourceTable) {
            var filterTable = ObasTableCollection.RroOutlaySumByAnalyticsFilterTable;
            return (sourceTable.GetFieldValue(filterTable.RroOutlayFieldId.Value) === filterTable.RroOutlayKey.Value);
        };
        TableRules.prototype.IsEmptyRow = function (sourceTable) {
            var fieldId = "";
            for (var i = 1; i <= ObasStageSettings.YearsCount; i++) {
                if (this._document.OutlayType === 1) {
                    fieldId = "" + BaseObasTableFields.YearDataField.GenerateId(i);
                }
                if (this._document.OutlayType === 2) {
                    fieldId = BaseObasTableFields.YearDataField.GenerateId(i) + "_D";
                }
                if ((sourceTable.GetFieldValue(fieldId) || 0) !== 0) {
                    return false;
                }
            }
            return true;
        };
        TableRules.prototype.IsEmptyRowPart2 = function (sourceTable) {
            var result = this.IsEmptyRow(sourceTable);
            if (result) {
                return result;
            }
            else {
                var keyFieldIds = sourceTable.GetKbkKeyFieldsId();
                keyFieldIds.push(sourceTable.Level.Id);
                var keyValues = sourceTable.GetKbkKeyFieldsValue();
                keyValues.push(sourceTable.Level.Value);
                var level = sourceTable.Level.Value;
                if ((level === 2) || (level === 3)) {
                    keyFieldIds.push(sourceTable.Kosgu.ForeignKey.Id);
                    keyValues.push(sourceTable.Kosgu.ForeignKey.Value);
                }
                if (level === 3) {
                    keyFieldIds.push(sourceTable.AddAnalyticIndicator.ForeignKey.Id);
                    keyValues.push(sourceTable.AddAnalyticIndicator.ForeignKey.Value);
                }
                var analyticsPart4Table = ObasTableCollection.RroOutlaySumByAnalyticsPart4Table;
                if (!analyticsPart4Table.Locate(keyFieldIds, keyValues)) {
                    return false;
                }
                else {
                    var fieldId = "";
                    for (var i = 1; i <= ObasStageSettings.YearsCount; i++) {
                        if (this._document.OutlayType === 1) {
                            fieldId = "" + BaseObasTableFields.YearDataField.GenerateId(i);
                        }
                        if (this._document.OutlayType === 2) {
                            fieldId = BaseObasTableFields.YearDataField.GenerateId(i) + "_D";
                        }
                        if ((sourceTable.GetFieldValue(fieldId) || 0) !== (analyticsPart4Table.GetFieldValue(fieldId) || 0)) {
                            return false;
                        }
                    }
                    return true;
                }
            }
        };
        TableRules.prototype.FilterPart1Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsTable;
            return this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart1GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part1_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart2Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsTable;
            var result = !ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart3, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart2GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part2_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart3Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsTable;
            var result = ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart3, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart3GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part3_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart4Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsPart4Table;
            return this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart4GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part4_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart5sp1Data = function (filterTableId) {
            return false;
        };
        TableRules.prototype.FilterPart5sp1GroupData = function (filterTableId) {
            return false;
        };
        TableRules.prototype.FilterPart5sp2Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsTable;
            var result = ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart5_2, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart5sp2GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part5sp2_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart5sp3p1Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsPart5xxTable;
            var result = ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart5_3_1, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart5sp3p1GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part5sp3p1_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart5sp3p2Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsPart5xxTable;
            var result = ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart5_3_2, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart5sp3p2GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part5sp3p2_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        TableRules.prototype.FilterPart5sp3p3Data = function (filterTableId) {
            var analyticsDataTable = ObasTableCollection.RroOutlaySumByAnalyticsPart5xxTable;
            var result = ObasHelper.CheckPartialCode(analyticsDataTable.CostType.Code, this._filterPart5_3_3, 3);
            return result && this.CheckByFilter(analyticsDataTable) && !this.IsEmptyRow(analyticsDataTable);
        };
        TableRules.prototype.FilterPart5sp3p3GroupData = function (filterTableId) {
            var analyticsGroupTable = ObasTableCollection.RroOutlaySumByAnalyticsGroupTable;
            var table = this._document.Tables.getValue("Part5sp3p3_Outlay_SumByAnalytics");
            return this.CheckByFilter(analyticsGroupTable) && table.Locate("FCRCodeSection", analyticsGroupTable.FcrCodeSection.Value);
        };
        return TableRules;
    }());
    FOutlay.TableRules = TableRules;
})(FOutlay || (FOutlay = {}));
