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 F02110;
(function (F02110) {
    var FDocument = (function (_super) {
        __extends(FDocument, _super);
        function FDocument(id, version, oldVersion) {
            var _this = _super.call(this, id, version, oldVersion, "F02_110_DO") || this;
            _this._p1TotalTable = null;
            _this._analyticsDataTable = null;
            _this._purchaseObjectsSprTable = null;
            _this._p2sp1RefObjTable = null;
            _this._p2sp1RefOkpdTable = null;
            _this._p2sp1RefKosguTable = null;
            _this._p3RefObjDescTable = null;
            _this._p3RefObjDescNpaTable = null;
            _this._p2UniqueOrgs = null;
            _this._p2sp2RefObjTable = null;
            _this._p2sp2RefOkpdTable = null;
            _this._p2sp2RefKosguTable = null;
            _this._importedProcurementTable = null;
            _this._p1Sheet = null;
            _this._analyticSheet = null;
            _this._p2Sp1Sheet = null;
            _this._p2Sp2Sheet = null;
            _this._addYearsHelper = null;
            _this._addDataYearsCount = 1;
            _this._mainDataYearsCount = ObasStageSettings.YearsCount;
            _this._importRules = null;
            _this._tableRules = new F02110.TableRules(_this);
            _this._interfaceRules = new F02110.InterfaceRules(_this);
            _this._updateRules = new F02110.UpdateRules(_this);
            _this._exportRules = new F02110.ExportRules(_this);
            return _this;
        }
        Object.defineProperty(FDocument.prototype, "AddYearsHelper", {
            get: function () {
                if (this._addYearsHelper == null) {
                    this._addYearsHelper = new F02110.AddYearsHelper(this);
                }
                return this._addYearsHelper;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P1Sheet", {
            get: function () {
                if (this._p1Sheet == null) {
                    this._p1Sheet = new F02110.P1Sheet("R1", this);
                }
                return this._p1Sheet;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2Sp1Sheet", {
            get: function () {
                if (this._p2Sp1Sheet == null) {
                    this._p2Sp1Sheet = new F02110.PurchaseObjectHierarchySheet(this);
                }
                return this._p2Sp1Sheet;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2Sp2Sheet", {
            get: function () {
                if (this._p2Sp2Sheet == null) {
                    this._p2Sp2Sheet = new F02110.PurchaseObjectUsdHierarchySheet(this);
                }
                return this._p2Sp2Sheet;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "TotalTable", {
            get: function () {
                return this.P1TotalTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P1TotalTable", {
            get: function () {
                if (this._p1TotalTable == null) {
                    this._p1TotalTable = new F02110.FObasTableP1Total("F02_100_R1_Values", this);
                }
                return this._p1TotalTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "PurchaseObjectsSprTable", {
            get: function () {
                if (this._purchaseObjectsSprTable == null) {
                    this._purchaseObjectsSprTable = new F02110.PurchaseObjectsSprTable();
                }
                return this._purchaseObjectsSprTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp1RefObjTable", {
            get: function () {
                if (this._p2sp1RefObjTable == null) {
                    this._p2sp1RefObjTable = new F02110.PartObjectDataTable("F02_110_R2_1_Ref_Object", this, this.P3RefObjDescTable);
                }
                return this._p2sp1RefObjTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp1RefOkpdTable", {
            get: function () {
                if (this._p2sp1RefOkpdTable == null) {
                    this._p2sp1RefOkpdTable = new F02110.PartOkpdTable("F02_110_R2_1_Ref_Okpd", this, this.P2sp1RefObjTable);
                }
                return this._p2sp1RefOkpdTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp1RefKosguTable", {
            get: function () {
                if (this._p2sp1RefKosguTable == null) {
                    this._p2sp1RefKosguTable = new F02110.PartKosguTable("F02_110_R2_1_Ref_Kosgu", this, this.P2sp1RefOkpdTable);
                }
                return this._p2sp1RefKosguTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2UniqueOrgs", {
            get: function () {
                if (this._p2UniqueOrgs == null) {
                    this._p2UniqueOrgs = new PartUniqueOrgsObasTable("F02_110_R2_2_Org", this, this.UniqueOrgsTable);
                }
                return this._p2UniqueOrgs;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp2RefObjTable", {
            get: function () {
                if (this._p2sp2RefObjTable == null) {
                    this._p2sp2RefObjTable = new F02110.UsdPartObjectDataTable("F02_110_R2_2_Ref_Object", this, this.P2UniqueOrgs, this.P3RefObjDescTable);
                }
                return this._p2sp2RefObjTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp2RefOkpdTable", {
            get: function () {
                if (this._p2sp2RefOkpdTable == null) {
                    this._p2sp2RefOkpdTable = new F02110.PartOkpdTable("F02_110_R2_2_Ref_Okpd", this, this.P2sp2RefObjTable);
                }
                return this._p2sp2RefOkpdTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P2sp2RefKosguTable", {
            get: function () {
                if (this._p2sp2RefKosguTable == null) {
                    this._p2sp2RefKosguTable = new F02110.UsdPartKosguTable("F02_110_R2_2_Ref_Kosgu", this, this.P2sp2RefOkpdTable);
                }
                return this._p2sp2RefKosguTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P3RefObjDescTable", {
            get: function () {
                if (this._p3RefObjDescTable == null) {
                    this._p3RefObjDescTable = new F02110.ObjectDescTable("F02_110_R3_Ref_ObjectDesc", this);
                }
                return this._p3RefObjDescTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "P3RefObjDescNpaTable", {
            get: function () {
                if (this._p3RefObjDescNpaTable == null) {
                    this._p3RefObjDescNpaTable = new F02110.ObjectDescNpaTable("F02_110_R3_Ref_Npa", this, this.P3RefObjDescTable);
                }
                return this._p3RefObjDescNpaTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "ImportedProcurementTable", {
            get: function () {
                if (this._importedProcurementTable == null) {
                    this._importedProcurementTable = new F02110.ImportedProcurementTable();
                }
                return this._importedProcurementTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "AnalyticsDataTable", {
            get: function () {
                if (this._analyticsDataTable == null) {
                    this._analyticsDataTable = new AnalyticsKosgu.AnalyticsDataTable("AnalyticsData", this);
                }
                return this._analyticsDataTable;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "AnalyticSheet", {
            get: function () {
                if (this._analyticSheet == null) {
                    this._analyticSheet = new AnalyticsKosgu.AnalyticsSheet("SHT_RA", this);
                }
                return this._analyticSheet;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "AddDataYearsCount", {
            get: function () {
                return this._addDataYearsCount;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "MainDataYearsCount", {
            get: function () {
                return this._mainDataYearsCount;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "TableRules", {
            get: function () {
                return this._tableRules;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "InterfaceRules", {
            get: function () {
                return this._interfaceRules;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "UpdateRules", {
            get: function () {
                return this._updateRules;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "ExportRules", {
            get: function () {
                return this._exportRules;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(FDocument.prototype, "ImportRules", {
            get: function () {
                if (this._importRules == null) {
                    this._importRules = new F02110.ImportRules(this);
                }
                return this._importRules;
            },
            enumerable: true,
            configurable: true
        });
        FDocument.prototype.ObjectDataTableIds = function () {
            return ["F02_110_R2_1_Ref_Object", "F02_110_R2_2_Ref_Object"];
        };
        FDocument.prototype.SaveTotalTableAndPartIndicateBudgetTables = function (tableId) {
            this.CommonRules.SaveTotalTable(tableId);
            var data = [];
            var kosguKeys = [];
            var yearField = F02110.ObasTableFields.R4YearField;
            var yearsCnt = ObasStageSettings.YearsCount;
            var reader = this.P2sp1RefKosguTable.CreateReader();
            while (reader.Read()) {
                var record = {
                    Key: reader.GetFieldValue("KOSGU_Id"),
                    Data: []
                };
                if (kosguKeys.indexOf(record.Key) > -1) {
                    record = data[kosguKeys.indexOf(record.Key)];
                    for (var i = 0; i < yearsCnt; i++) {
                        record.Data[i] = record.Data[i] + reader.GetFieldValue(yearField.GenerateId(i + 1)) || 0;
                    }
                }
                else {
                    kosguKeys.push(record.Key);
                    for (var i = 0; i < yearsCnt; i++) {
                        record.Data[i] = reader.GetFieldValue(yearField.GenerateId(i + 1)) || 0;
                    }
                    data.push(record);
                }
            }
            var reader = this.P2sp2RefKosguTable.CreateReader();
            while (reader.Read()) {
                var record = {
                    Key: reader.GetFieldValue("KOSGU_Id"),
                    Data: []
                };
                if (kosguKeys.indexOf(record.Key) > -1) {
                    record = data[kosguKeys.indexOf(record.Key)];
                    for (var i = 0; i < yearsCnt; i++) {
                        record.Data[i] = record.Data[i] + reader.GetFieldValue(yearField.GenerateId(i + 1)) || 0;
                    }
                }
                else {
                    kosguKeys.push(record.Key);
                    for (var i = 0; i < yearsCnt; i++) {
                        record.Data[i] = reader.GetFieldValue(yearField.GenerateId(i + 1)) || 0;
                    }
                    data.push(record);
                }
            }
            Client.SendMessage(Client.MainDocumentId, "%Doc%.TableRules.UpdateRROIndicateBudget", [this.FileType, this.ObasVersion, data]);
        };
        return FDocument;
    }(BaseExtendOrgDocumentObject));
    F02110.FDocument = FDocument;
})(F02110 || (F02110 = {}));
