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 F03310;
(function (F03310) {
    var InterfaceRules = (function (_super) {
        __extends(InterfaceRules, _super);
        function InterfaceRules() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        InterfaceRules.prototype.ObjectDescSheetFormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 0:
                case 1:
                case 2:
                    return SheetFormatCollection.Related;
                case 8:
                    return SheetFormatCollection.Default;
                case 9:
                case 13:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        InterfaceRules.prototype.ObjectDescSheetCalcEventHandler = function (sheetId, row, column, fieldId) {
            if (BaseObasTableFields.StrCodeField.Equal(fieldId)) {
                return ObasHelper.FillWithCharacter(row + 1, 6);
            }
            return undefined;
        };
        InterfaceRules.prototype.ObjectDescSheetCanAddEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return column > 7;
        };
        InterfaceRules.prototype.ObjectDescSheetCanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var table = this._document.Sheets.getValue(sheetId).Table;
            if (column > 7) {
                return table.GetKeyBySourceTable(this._document.P3RefObjDescNpaTable) != null;
            }
            else {
                return table.GetKeyBySourceTable(this._document.P3RefObjDescNpaTable.ParentTable) != null;
            }
        };
        InterfaceRules.prototype.ObjectDescSheetCanDeleteEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            var table = this._document.Sheets.getValue(sheetId).Table;
            if (column > 7) {
                return table.GetKeyBySourceTable(this._document.P3RefObjDescNpaTable) != null;
            }
            else {
                return false;
            }
        };
        InterfaceRules.prototype.NpaSheetFormatEventHandler = function (sheetId, row, column, groupIndex) {
            switch (column) {
                case 0:
                case 1:
                    return SheetFormatCollection.Related;
                case 2:
                    return SheetFormatCollection.Default;
                case 3:
                case 7:
                    return SheetFormatCollection.Spr;
                default:
                    return SheetFormatCollection.Free;
            }
        };
        InterfaceRules.prototype.NpaSheetCalcEventHandler = function (sheetId, row, column, fieldId) {
            if (column === 2) {
                return ObasHelper.FillWithCharacter((row + 1).toString(), 5);
            }
        };
        InterfaceRules.prototype.FilterRecipientsSprTable = function () {
            var pnoSpr = ObasTableCollection.PnoSprTable;
            return ObasTableCollection.RecipientCategorySprTable.RecordKey.Value ===
                pnoSpr.RecipientCategory.LookupByKeys(this.Document.PnoDataTable.Pno.ForeignKey.Value);
        };
        InterfaceRules.prototype.FilterOKPD = function () {
            var okpdCode = ObasTableCollection.OkpdSprTable.Code.Value || "";
            return okpdCode.length >= 5;
        };
        InterfaceRules.prototype.FilterNPAStatuses = function () {
            var npaStatusesTable = ObasTableCollection.NpaStatusesSprTable;
            return npaStatusesTable.RecordKey.Value === 1 || npaStatusesTable.RecordKey.Value === 2;
        };
        InterfaceRules.prototype.LoadDataFromProcurementMenuItemClickEventHandler = function () {
            this.ImportProcurementList();
            if (this._document.ShowEditor("ImportedProcurementListEditor")) {
                this.LoadDataFromProcurement();
            }
            this._document.ImportedProcurementTable.DeleteAllRow();
        };
        InterfaceRules.prototype.ImportedProcurementSprSheetCanEditEventHandler = function (sheetId, row, column, fieldId, rowLevel) {
            return (column === 0);
        };
        InterfaceRules.prototype.ImportProcurementList = function () {
            var procurementPlanTable = ObasTableCollection.RroProcurementPlanTable;
            procurementPlanTable.Last();
            var procurementTable = ObasTableCollection.RroProcurementTable;
            var procurementKeys = new Array();
            while (procurementTable.Locate(procurementTable.ProcurementPlanKey.Id, procurementPlanTable.RecordKey.Value, true)) {
                procurementKeys.push(procurementTable.RecordKey.Value);
            }
            procurementTable.ClearLocateFlag();
            var procurementKBKTable = ObasTableCollection.RroProcurementKBKTable;
            var importedProcurementTable = this._document.ImportedProcurementTable;
            importedProcurementTable.DeleteAllRow();
            for (var i = 0; i < procurementKeys.length; i++) {
                var keyValues = this._document.MainParametersTable.GetKbkKeyFieldsValue();
                keyValues.push(procurementKeys[i]);
                var keyFieldIds = procurementKBKTable.GetKbkKeyFieldsId();
                keyFieldIds.push(procurementKBKTable.ProcurementKey.Id);
                while (procurementKBKTable.Locate(keyFieldIds, keyValues, true)) {
                    importedProcurementTable.AddRow();
                    importedProcurementTable.IsMarked.Value = false;
                    importedProcurementTable.ProcurementKBK.ForeignKey.Value = procurementKBKTable.RecordKey.Value;
                    importedProcurementTable.FullKBKCode.Value = procurementKBKTable.CalcFullKBKCode();
                    importedProcurementTable.PostRow();
                }
                procurementKBKTable.ClearLocateFlag();
            }
        };
        InterfaceRules.prototype.LoadDataFromProcurement = function () {
            var procurementId = "Procurement_ID";
            var importedProcurementTable = this._document.ImportedProcurementTable;
            var reader = importedProcurementTable.CreateReader();
            while (reader.Read()) {
                if (importedProcurementTable.IsMarked.Value) {
                    var procurementKBKTable = ObasTableCollection.RroProcurementKBKTable;
                    procurementKBKTable.Locate(procurementKBKTable.RecordKey.Id, importedProcurementTable.ProcurementKBK.ForeignKey.Value);
                    if (!procurementKBKTable.IsForeignProcurement.Value) {
                        var purchaseObjectsSprTable = this._document.PurchaseObjectsSprTable;
                        if (!purchaseObjectsSprTable.Locate([purchaseObjectsSprTable.Name.Id, procurementId], [procurementKBKTable.ObjectName.Value, procurementKBKTable.ProcurementKey.Value])) {
                            purchaseObjectsSprTable.AddRow();
                            purchaseObjectsSprTable.Name.Value = procurementKBKTable.ObjectName.Value;
                            purchaseObjectsSprTable.SetFieldValue(procurementId, procurementKBKTable.ProcurementKey.Value);
                            purchaseObjectsSprTable.PostRow();
                        }
                        var pnoDataTable = this._document.PnoDataTable;
                        if (!pnoDataTable.Locate(procurementId, procurementKBKTable.ProcurementKey.Value)) {
                            pnoDataTable.AddRow();
                            pnoDataTable.SetFieldValue(procurementId, procurementKBKTable.ProcurementKey.Value);
                            pnoDataTable.PostRow();
                        }
                        var recipientsDataTable = this._document.RecipientsDataTable;
                        if (!recipientsDataTable.Locate(recipientsDataTable.OwnerKey.Id, pnoDataTable.RecordKey.Value)) {
                            recipientsDataTable.AddRow();
                            recipientsDataTable.OwnerKey.Value = pnoDataTable.RecordKey.Value;
                            recipientsDataTable.PostRow();
                        }
                        var p2RefObjTable = this._document.P2RefObjTable;
                        if (!p2RefObjTable.Locate(p2RefObjTable.ObjectKey.Id, purchaseObjectsSprTable.RecordKey.Value)) {
                            p2RefObjTable.AddRow();
                            p2RefObjTable.OwnerKey.Value = recipientsDataTable.RecordKey.Value;
                            p2RefObjTable.ObjectKey.Value = purchaseObjectsSprTable.RecordKey.Value;
                            p2RefObjTable.PostRow();
                        }
                        var p2RefOkpdTable = this._document.P2RefOkpdTable;
                        if (!p2RefOkpdTable.Locate([p2RefOkpdTable.OwnerKey.Id, p2RefOkpdTable.OKPDKey.Id], [p2RefObjTable.RecordKey.Value, procurementKBKTable.OKPDKey.Value])) {
                            p2RefOkpdTable.AddRow();
                            p2RefOkpdTable.OwnerKey.Value = p2RefObjTable.RecordKey.Value;
                            p2RefOkpdTable.OKPDKey.Value = procurementKBKTable.OKPDKey.Value;
                            p2RefOkpdTable.PostRow();
                        }
                        var p2RefKosguTable = this._document.P2RefKosguTable;
                        p2RefKosguTable.AddRow();
                        p2RefKosguTable.OwnerKey.Value = p2RefOkpdTable.RecordKey.Value;
                        p2RefKosguTable.Year.Value = procurementKBKTable.GetProcurementPlacementYear();
                        p2RefKosguTable.PostRow();
                        for (var i = 0; i <= ObasStageSettings.YearsCount - 1; i++) {
                            var fieldId = "Y" + (i + 1);
                            p2RefKosguTable.SetFieldValue(F03310.ObasTableFields.FnYearField.GenerateId(fieldId), procurementKBKTable.GetFieldValue(fieldId));
                        }
                        var procurementTable = ObasTableCollection.RroProcurementTable;
                        procurementTable.Locate(procurementTable.RecordKey.Id, procurementKBKTable.ProcurementKey.Value);
                        var p3RefObjDescTable = this._document.P3RefObjDescTable;
                        p3RefObjDescTable.Locate(p3RefObjDescTable.ObjectKey.Id, purchaseObjectsSprTable.RecordKey.Value);
                        p3RefObjDescTable.Program.Value = procurementTable.GetFieldValue("MainStateProgram");
                        p3RefObjDescTable.Action.Value = procurementTable.GetFieldValue("MainEventStateProgram");
                        p3RefObjDescTable.Rationale.Value = procurementTable.GetFieldValue("JustificationConformityObject");
                        if (procurementTable.ProcurementTypeKey.Value === 1) {
                            p3RefObjDescTable.Result.Value = procurementTable.GetFieldValue("ExpectedResult");
                            p3RefObjDescTable.Info.Value = procurementTable.GetFieldValue("IsInfoTechDifficulties_Name");
                        }
                        var procurementNPATable = ObasTableCollection.RroProcurementNPATable;
                        var p3RefObjDescNpaTable = this._document.P3RefObjDescNpaTable;
                        while (procurementNPATable.Locate(procurementNPATable.ProcurementKey.Id, procurementKBKTable.ProcurementKey.Value, true)) {
                            var objDescNpaIds = [];
                            objDescNpaIds.push(p3RefObjDescNpaTable.OwnerKey.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaType.ForeignKey.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaNumber.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaDate.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaName.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaStatus.ForeignKey.Id);
                            objDescNpaIds.push(p3RefObjDescNpaTable.NpaStatusChangeDate.Id);
                            var objDescNpaValues = [];
                            objDescNpaValues.push(p3RefObjDescTable.RecordKey.Value);
                            objDescNpaValues.push(procurementNPATable.NpaType.ForeignKey.Value);
                            objDescNpaValues.push(procurementNPATable.NpaNumber.Value);
                            objDescNpaValues.push(procurementNPATable.NpaDate.Value);
                            objDescNpaValues.push(procurementNPATable.NpaName.Value);
                            objDescNpaValues.push(procurementNPATable.NpaStatus.ForeignKey.Value);
                            objDescNpaValues.push(procurementNPATable.NpaStatusChangeDate.Value);
                            if (!p3RefObjDescNpaTable.Locate(objDescNpaIds, objDescNpaValues)) {
                                p3RefObjDescNpaTable.AddRow();
                                for (var i = 0; i < objDescNpaIds.length; i++) {
                                    p3RefObjDescNpaTable.SetFieldValue(objDescNpaIds[i], objDescNpaValues[i]);
                                }
                                p3RefObjDescNpaTable.PostRow();
                            }
                        }
                        procurementNPATable.ClearLocateFlag();
                    }
                }
            }
        };
        return InterfaceRules;
    }(BaseInterfaceRules));
    F03310.InterfaceRules = InterfaceRules;
    var AddYearsHelper = (function (_super) {
        __extends(AddYearsHelper, _super);
        function AddYearsHelper() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        AddYearsHelper.prototype.GetAddDollarRateYearsFieldRangeEventHandler = function (sheetId) {
            if (sheetId === void 0) { sheetId = ""; }
            var result = [];
            var yearsCount = ObasStageSettings.YearsCount;
            for (var i = yearsCount; i < yearsCount + this._document.AddDataYearsCount; i++) {
                result.push(i);
            }
            return result;
        };
        AddYearsHelper.prototype.GetAddYearsFieldRangeEventHandler = function (sheetId) {
            if (sheetId === void 0) { sheetId = ""; }
            var result = [];
            var yearsCount = ObasStageSettings.YearsCount;
            for (var i = yearsCount; i < yearsCount + this._document.AddDataYearsCount; i++) {
                result.push(i);
            }
            return result;
        };
        return AddYearsHelper;
    }(AddYearsDocumentHelper));
    F03310.AddYearsHelper = AddYearsHelper;
    var P1Sheet = (function (_super) {
        __extends(P1Sheet, _super);
        function P1Sheet(id, _document) {
            return _super.call(this, id, _document, 1) || this;
        }
        P1Sheet.prototype.FormatEventHandler = function (sheetId, row, column, groupIndex) {
            if (this.RowNameColumn === column) {
                return SheetFormatCollection.Default;
            }
            else {
                return SheetFormatCollection.Related;
            }
        };
        return P1Sheet;
    }(YearsSheet));
    F03310.P1Sheet = P1Sheet;
    var BaseHierarchySheet = (function (_super) {
        __extends(BaseHierarchySheet, _super);
        function BaseHierarchySheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        BaseHierarchySheet._totalRowName = "\u0412\u0441\u0435\u0433\u043E";
        BaseHierarchySheet._totalPnoRowName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043F\u0443\u0431\u043B\u0438\u0447\u043D\u043E\u043C\u0443 \u043E\u0431\u044F\u0437\u0430\u0442\u0435\u043B\u044C\u0441\u0442\u0432\u0443";
        BaseHierarchySheet._totalCategoryRowName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043A\u0430\u0442\u0435\u0433\u043E\u0440\u0438\u0438 \u043F\u043E\u043B\u0443\u0447\u0430\u0442\u0435\u043B\u0435\u0439";
        BaseHierarchySheet._totalObjRowName = "\u0418\u0442\u043E\u0433\u043E \u043F\u043E \u043E\u0431\u044A\u0435\u043A\u0442\u0443 \u0437\u0430\u043A\u0443\u043F\u043A\u0438";
        BaseHierarchySheet._totalRowInfo = {
            IsCalculated: false,
            Name: BaseHierarchySheet._totalRowName,
            Format: SheetFormatCollection.Default
        };
        BaseHierarchySheet._totalPnoRowInfo = {
            IsCalculated: false,
            Name: BaseHierarchySheet._totalPnoRowName,
            Format: SheetFormatCollection.Default
        };
        BaseHierarchySheet._totalCategotyRowInfo = {
            IsCalculated: false,
            Name: BaseHierarchySheet._totalCategoryRowName,
            Format: SheetFormatCollection.Default
        };
        BaseHierarchySheet._totalObjRowInfo = {
            IsCalculated: false,
            Name: BaseHierarchySheet._totalObjRowName,
            Format: SheetFormatCollection.Default
        };
        BaseHierarchySheet._subTotalRowInfos = [
            BaseHierarchySheet._totalPnoRowInfo, BaseHierarchySheet._totalCategotyRowInfo, BaseHierarchySheet._totalObjRowInfo
        ];
        BaseHierarchySheet.SprDefaultTotalFirstSubtotal = {
            Cell: { Type: SheetCellTypes.Spr },
            SubTotalRow: [SheetRowInfoCollection.Default, SheetRowInfoCollection.SprOnlyFormatRow, SheetRowInfoCollection.SprOnlyFormatRow, SheetRowInfoCollection.SprOnlyFormatRow],
            TotalRow: SheetRowInfoCollection.Default
        };
        BaseHierarchySheet.SprDefaultTotalSecondSubtotal = {
            Cell: { Type: SheetCellTypes.Spr },
            SubTotalRow: [SheetRowInfoCollection.Default, SheetRowInfoCollection.Default, SheetRowInfoCollection.SprOnlyFormatRow, SheetRowInfoCollection.SprOnlyFormatRow],
            TotalRow: SheetRowInfoCollection.Default
        };
        BaseHierarchySheet.SprColConstAll = {
            Cell: { Type: SheetCellTypes.Spr },
            SubTotalRow: BaseHierarchySheet._subTotalRowInfos,
            TotalRow: BaseHierarchySheet._totalRowInfo
        };
        BaseHierarchySheet.RelatedColConstAll = {
            Cell: { Type: SheetCellTypes.Related },
            SubTotalRow: BaseHierarchySheet._subTotalRowInfos,
            TotalRow: BaseHierarchySheet._totalRowInfo
        };
        BaseHierarchySheet.FreeColConstAll = {
            Cell: { Type: SheetCellTypes.Free },
            SubTotalRow: BaseHierarchySheet._subTotalRowInfos,
            TotalRow: BaseHierarchySheet._totalRowInfo
        };
        BaseHierarchySheet.DefaultColConstAll = {
            Cell: { Type: SheetCellTypes.Default },
            SubTotalRow: BaseHierarchySheet._subTotalRowInfos,
            TotalRow: BaseHierarchySheet._totalRowInfo
        };
        BaseHierarchySheet._CodeInfo = {
            Column: 10,
            Length: 7,
            LevelIncs: [90000, 9000, 900, 1],
            CalcTotalCode: function () {
                return "0900000";
            }
        };
        return BaseHierarchySheet;
    }(HierarchyYearGroupSheet));
    F03310.BaseHierarchySheet = BaseHierarchySheet;
    var PurchaseObjectHierarchySheet = (function (_super) {
        __extends(PurchaseObjectHierarchySheet, _super);
        function PurchaseObjectHierarchySheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        Object.defineProperty(PurchaseObjectHierarchySheet.prototype, "SheetInfo", {
            get: function () {
                return PurchaseObjectHierarchySheet._sheetInfo;
            },
            enumerable: true,
            configurable: true
        });
        PurchaseObjectHierarchySheet.prototype.GetYearsSumCaptionEventHandler = function (tableId, index, defaultCaption, fieldCaption) {
            return this.Document.CommonRules.CalcColumnCaption(index, 15, 2);
        };
        PurchaseObjectHierarchySheet._sheetInfo = {
            MaxLevel: 4,
            CodeInfo: PurchaseObjectHierarchySheet._CodeInfo,
            ColumnsInfo: [
                SheetColumnInfoCollection.SprColDefaultTotal,
                PurchaseObjectHierarchySheet.SprDefaultTotalFirstSubtotal,
                PurchaseObjectHierarchySheet.SprDefaultTotalSecondSubtotal,
                PurchaseObjectHierarchySheet.SprDefaultTotalSecondSubtotal,
                PurchaseObjectHierarchySheet.SprDefaultTotalSecondSubtotal,
                SheetColumnInfoCollection.SprColAllDefault,
                SheetColumnInfoCollection.SprColAllDefault,
                SheetColumnInfoCollection.SprColAllDefault,
                SheetColumnInfoCollection.SprColAllDefault,
                PurchaseObjectHierarchySheet.FreeColConstAll,
                PurchaseObjectHierarchySheet.DefaultColConstAll,
                SheetColumnInfoCollection.CalcColAllCalc,
                SheetColumnInfoCollection.CalcColAllCalc,
                SheetColumnInfoCollection.CalcColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc,
                SheetColumnInfoCollection.FreeColAllCalc
            ]
        };
        return PurchaseObjectHierarchySheet;
    }(BaseHierarchySheet));
    F03310.PurchaseObjectHierarchySheet = PurchaseObjectHierarchySheet;
})(F03310 || (F03310 = {}));
