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 F02300;
(function (F02300) {
    var TableRules = (function () {
        function TableRules(_document) {
            this._document = _document;
        }
        return TableRules;
    }());
    F02300.TableRules = TableRules;
    var P1TableStrKeys;
    (function (P1TableStrKeys) {
        P1TableStrKeys[P1TableStrKeys["Total"] = 1] = "Total";
        P1TableStrKeys[P1TableStrKeys["P2"] = 2] = "P2";
        P1TableStrKeys[P1TableStrKeys["P3"] = 3] = "P3";
        P1TableStrKeys[P1TableStrKeys["P4"] = 4] = "P4";
    })(P1TableStrKeys = F02300.P1TableStrKeys || (F02300.P1TableStrKeys = {}));
    var P1TotalTable = (function (_super) {
        __extends(P1TotalTable, _super);
        function P1TotalTable() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        P1TotalTable.prototype.GetTotalKey = function () {
            return P1TableStrKeys.Total;
        };
        P1TotalTable.prototype.SumChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            if (this.StrKey === 100) {
                if (this.GetFieldValue(fieldId) < -100 || this.GetFieldValue(fieldId) > 100) {
                    Client.ShowMessage("\u041E\u0448\u0438\u0431\u043A\u0430", "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u043F\u043E\u043B\u044F \u0434\u043E\u043B\u0436\u043D\u043E \u043D\u0430\u0445\u043E\u0434\u0438\u0442\u044C\u0441\u044F \u0432 \u0434\u0438\u0430\u043F\u0430\u0437\u043E\u043D\u0435 \u043E\u0442 -100 \u0434\u043E 100.", MessageIcons.Error);
                    this.SetFieldValue(fieldId, oldValue);
                }
            }
            _super.prototype.SumChangeEventHandler.call(this, tableId, oldValue, newValue, fieldId);
            if (this.StrKey === P1TableStrKeys.Total) {
                var analyticsDataTable = this._document.AnalyticsDataTable;
                analyticsDataTable.Locate(analyticsDataTable.RecordKey.Id, 2);
                var yearOffset = ObasHelper.GetYearOffsetById(fieldId) + 1;
                analyticsDataTable.SetFieldValue("Y" + yearOffset, this.GetFieldValue(fieldId));
            }
        };
        return P1TotalTable;
    }(P1TotalObasTable));
    F02300.P1TotalTable = P1TotalTable;
    var IndexationTableKeys = (function (_super) {
        __extends(IndexationTableKeys, _super);
        function IndexationTableKeys(Year) {
            var _this = _super.call(this) || this;
            _this.Year = Year;
            return _this;
        }
        return IndexationTableKeys;
    }(ObasTableKeys));
    var IndexationTable = (function (_super) {
        __extends(IndexationTable, _super);
        function IndexationTable(id, document) {
            var _this = _super.call(this, id, ["Year"], document) || this;
            _this._value = null;
            _this._year = null;
            return _this;
        }
        Object.defineProperty(IndexationTable.prototype, "Year", {
            get: function () {
                if (this._year == null) {
                    this._year = new NumberObasTableField("Year", this);
                }
                return this._year;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(IndexationTable.prototype, "Value", {
            get: function () {
                if (this._value == null) {
                    this._value = new NumberObasTableField("Value", this);
                }
                return this._value;
            },
            enumerable: true,
            configurable: true
        });
        IndexationTable.prototype.GetKeys = function (year) {
            if (year === void 0) { year = this.Year.Value; }
            if (this._keys == null) {
                this._keys = new IndexationTableKeys(year);
            }
            else {
                this._keys.Year = year;
            }
            return this._keys;
        };
        IndexationTable.prototype.GetValueByYear = function (year) {
            return this.GetValueByKeys(this.Value.Id, this.GetKeys(year));
        };
        IndexationTable.prototype.ValueChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            var p2Sp1DataTable = this._document.P2Sp1DataTable;
            var yearOffset = this.Year.Value - ObasStageSettings.CurrentYear;
            if (yearOffset >= 0) {
                var field = p2Sp1DataTable.PlannedCount.GetFieldByYearOffset(yearOffset);
                if (p2Sp1DataTable.Locate("StrKey", P2Sp1RowsKeys.C0110)) {
                    p2Sp1DataTable.PlannedCountChangeEventHandler(p2Sp1DataTable.Id, 0, field.NValue, field.Id);
                }
                if (p2Sp1DataTable.Locate("StrKey", P2Sp1RowsKeys.C0120)) {
                    p2Sp1DataTable.PlannedCountChangeEventHandler(p2Sp1DataTable.Id, 0, field.NValue, field.Id);
                }
            }
        };
        return IndexationTable;
    }(ObasTableWithKeys));
    F02300.IndexationTable = IndexationTable;
    var IndexTransposedTable = (function (_super) {
        __extends(IndexTransposedTable, _super);
        function IndexTransposedTable(id, fieldsCount, _startYear, _document) {
            var _this = _super.call(this, id, fieldsCount, ["PseudoKey"], ["Year"]) || this;
            _this._startYear = _startYear;
            _this._document = _document;
            _this._value = null;
            return _this;
        }
        Object.defineProperty(IndexTransposedTable.prototype, "Value", {
            get: function () {
                if (this._value == null) {
                    this._value = new NumberGenericObasTableField("Value", this);
                }
                return this._value;
            },
            enumerable: true,
            configurable: true
        });
        IndexTransposedTable.prototype.GetFieldIndexEventHandler = function (year) {
            return (year - this._startYear);
        };
        IndexTransposedTable.prototype.GetFieldValueEventHandler = function (fieldIndex, fieldId) {
            return this._startYear + fieldIndex;
        };
        IndexTransposedTable.prototype.IterateByYears = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            var indexCorrection = this._document.InitYear - ObasStageSettings.CurrentYear;
            var startIndex = 0;
            var yearsCount = this._document.FullYearsCount;
            if (converToYearIndex) {
                startIndex += indexCorrection;
                yearsCount += indexCorrection;
            }
            for (var i = startIndex; i < yearsCount; i++) {
                handler(converToYearIndex ? i + 1 : i);
            }
        };
        return IndexTransposedTable;
    }(TransposedObasTable));
    F02300.IndexTransposedTable = IndexTransposedTable;
    var P2Sp1RowsKeys;
    (function (P2Sp1RowsKeys) {
        P2Sp1RowsKeys[P2Sp1RowsKeys["Total"] = 14] = "Total";
        P2Sp1RowsKeys[P2Sp1RowsKeys["C0110"] = 1] = "C0110";
        P2Sp1RowsKeys[P2Sp1RowsKeys["C0120"] = 6] = "C0120";
    })(P2Sp1RowsKeys = F02300.P2Sp1RowsKeys || (F02300.P2Sp1RowsKeys = {}));
    var RowsTable = (function (_super) {
        __extends(RowsTable, _super);
        function RowsTable(id) {
            var _this = _super.call(this, id, [BaseObasTableFields.RecordKeyField.Id], BaseObasTableFields.StrCodeField.Id, "StrName") || this;
            _this._isAgr = null;
            _this._parentKeyField = null;
            return _this;
        }
        Object.defineProperty(RowsTable.prototype, "IsAgr", {
            get: function () {
                if (this._isAgr == null) {
                    this._isAgr = new ObasTableField("IsAgr", this, false);
                }
                return this._isAgr;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(RowsTable.prototype, "ParentKeyField", {
            get: function () {
                if (this._parentKeyField == null) {
                    this._parentKeyField = new ObasTableField("ParentKey", this, false);
                }
                return this._parentKeyField;
            },
            enumerable: true,
            configurable: true
        });
        RowsTable.prototype.IsArgRow = function () {
            return this.IsAgr.Value;
        };
        return RowsTable;
    }(SprTable));
    F02300.RowsTable = RowsTable;
    var PxSp1DataTable = (function (_super) {
        __extends(PxSp1DataTable, _super);
        function PxSp1DataTable(id, document, parentTable, _sumTable) {
            var _this = _super.call(this, id, document, parentTable) || this;
            _this._sumTable = _sumTable;
            _this._yearData = null;
            _this._isAgr = null;
            _this._agrKeys = null;
            _this._isHasParentRows = null;
            _this._parentKeyField = null;
            _this._count = null;
            return _this;
        }
        Object.defineProperty(PxSp1DataTable.prototype, "Count", {
            get: function () {
                if (this._count == null) {
                    this._count = new NumberGenericObasTableField("Count_Y", this);
                }
                return this._count;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "YearData", {
            get: function () {
                if (this._yearData == null) {
                    this._yearData = new NumberGenericObasTableField(BaseObasTableFields.YearDataField.Id, this);
                }
                return this._yearData;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "IsAgr", {
            get: function () {
                if (this._isAgr == null) {
                    this._isAgr = new ObasTableField("IsAgr", this, false);
                }
                return this._isAgr;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "ParentKeyField", {
            get: function () {
                if (this._parentKeyField == null) {
                    this._parentKeyField = new ObasTableField("ParentKey", this, false);
                }
                return this._parentKeyField;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "ParentStrKey", {
            get: function () {
                return this.ParentKeyField.Value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "IsHasParentRows", {
            get: function () {
                if (this._isHasParentRows == null) {
                    this._isHasParentRows = this.IsFieldExists("ParentKey");
                }
                return this._isHasParentRows;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PxSp1DataTable.prototype, "AgrKeys", {
            get: function () {
                var _this = this;
                if (this._agrKeys == null) {
                    this._agrKeys = [];
                    var callback = function () {
                        if (_this.IsAgr.Value) {
                            _this._agrKeys.push(_this.ParentTable.RecordKey.Value);
                        }
                    };
                    this.ParentTable.Iterate(callback);
                }
                return this._agrKeys;
            },
            enumerable: true,
            configurable: true
        });
        PxSp1DataTable.prototype.ChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            if (this.IsHasParentRows) {
                var parentKey = this.ParentStrKey;
                if (parentKey != null) {
                    this.SetSumByKeys(fieldId, this.GetKeys(parentKey), oldValue, newValue, true);
                }
            }
        };
        PxSp1DataTable.prototype.SumChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            if (this.StrKey === this.TotalRowKey) {
                this._sumTable.SetValueByKeys(fieldId, this._sumTable.GetKeys(ExpenseRowsKeys.Obas), newValue);
            }
            this.ChangeEventHandler(tableId, oldValue, newValue, fieldId);
        };
        return PxSp1DataTable;
    }(ObasTableWithStrKeysParent));
    var P2Sp1DataTable = (function (_super) {
        __extends(P2Sp1DataTable, _super);
        function P2Sp1DataTable(id, document, parentTable, sumTable, _indexationtable) {
            var _this = _super.call(this, id, document, parentTable, sumTable) || this;
            _this._indexationtable = _indexationtable;
            _this._standardExpens = null;
            _this._plannedDeviation = null;
            _this._plannedCount = null;
            _this._expensesVolume = null;
            _this._paymentP1 = null;
            _this._paymentP2 = null;
            _this._paymentP3 = null;
            _this._copyFields = null;
            return _this;
        }
        Object.defineProperty(P2Sp1DataTable.prototype, "CopyFields", {
            get: function () {
                var _this = this;
                if (this._copyFields == null) {
                    this._copyFields = [this.StrKeyField];
                    this.IterateThreePastFields(function (i) {
                        _this._copyFields.push(_this.Count.GetFieldByYearIndex(i));
                        _this._copyFields.push(_this.PaymentP1.GetFieldByYearIndex(i));
                    });
                    this.IterateTwoPastFields(function (i) {
                        _this._copyFields.push(_this.PaymentP2.GetFieldByYearIndex(i));
                    });
                    this.IteratePlannedFields(function (i) {
                        _this._copyFields.push(_this.PlannedCount.GetFieldByYearIndex(i));
                        _this._copyFields.push(_this.PlannedDeviation.GetFieldByYearIndex(i));
                    });
                    this._copyFields.push(this.PaymentP3.GetFieldByYearIndex(0));
                }
                return this._copyFields;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "PaymentP3", {
            get: function () {
                if (this._paymentP3 == null) {
                    this._paymentP3 = new NumberGenericObasTableField("Payment_P3_Y", this);
                }
                return this._paymentP3;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "PaymentP2", {
            get: function () {
                if (this._paymentP2 == null) {
                    this._paymentP2 = new NumberGenericObasTableField("Payment_P2_Y", this);
                }
                return this._paymentP2;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "PaymentP1", {
            get: function () {
                if (this._paymentP1 == null) {
                    this._paymentP1 = new NumberGenericObasTableField("Payment_P1_Y", this);
                }
                return this._paymentP1;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "ExpensesVolume", {
            get: function () {
                if (this._expensesVolume == null) {
                    this._expensesVolume = new NumberObasTableField("Expens_Volume", this);
                }
                return this._expensesVolume;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "PlannedCount", {
            get: function () {
                if (this._plannedCount == null) {
                    this._plannedCount = new NumberGenericObasTableField("Planned_Count_Y", this);
                }
                return this._plannedCount;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "TotalRowKey", {
            get: function () {
                return P2Sp1RowsKeys.Total;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "PlannedDeviation", {
            get: function () {
                if (this._plannedDeviation == null) {
                    this._plannedDeviation = new NumberGenericObasTableField("Planned_Deviation_Y", this);
                }
                return this._plannedDeviation;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P2Sp1DataTable.prototype, "StandardExpens", {
            get: function () {
                if (this._standardExpens == null) {
                    this._standardExpens = new NumberGenericObasTableField("Standard_Expens_Y", this);
                }
                return this._standardExpens;
            },
            enumerable: true,
            configurable: true
        });
        P2Sp1DataTable.prototype.ExpensPartsChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            if (this.StrKey !== P2Sp1RowsKeys.Total) {
                this.YearData.GetFieldByField(fieldId).NValue =
                    this.StandardExpens.GetFieldByField(fieldId).NValue *
                        (1 + this.PlannedDeviation.GetFieldByField(fieldId).NValue / 100);
            }
            this.ChangeEventHandler(tableId, oldValue, newValue, fieldId);
        };
        P2Sp1DataTable.prototype.UpdateStandardExpens = function (yearOffset, expensVolume) {
            this.StandardExpens.GetFieldByYearOffset(yearOffset).NValue =
                expensVolume *
                    (this.PlannedCount.GetFieldByYearOffset(yearOffset).NValue *
                        this._indexationtable.GetValueByYear(this.Document.Settings.StartYear + yearOffset)) /
                    0.94;
        };
        P2Sp1DataTable.prototype.PlannedCountChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            if (this.StrKey !== P2Sp1RowsKeys.Total) {
                this.UpdateStandardExpens(ObasHelper.GetYearOffsetById(fieldId), this.ExpensesVolume.NValue);
            }
            this.ChangeEventHandler(tableId, oldValue, newValue, fieldId);
        };
        P2Sp1DataTable.prototype.ExpensesVolumeChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            var _this = this;
            if (this.StrKey !== P2Sp1RowsKeys.Total) {
                this.Document.IterateByYears(function (yearOffset) {
                    if (yearOffset > 0) {
                        _this.UpdateStandardExpens(yearOffset, newValue);
                    }
                }, false);
            }
        };
        P2Sp1DataTable.prototype.UpdateExpensesVolume = function () {
            var _this = this;
            var paymentP1 = this.PaymentP1;
            var paymentP2 = this.PaymentP2;
            var totalPaymentP1Third = 0;
            var totalPaymentP1Second = 0;
            var totalPaymentP1Next = 0;
            var totalPaymentP2Second = 0;
            var totalPaymentP2Next = 0;
            this.DoWithRestorePosition(function () {
                if (_this.LocateByKeys(_this.GetKeys(P2Sp1RowsKeys.Total).ToArray())) {
                    totalPaymentP1Third = paymentP1.GetFieldByYearIndex(-3).NValue;
                    totalPaymentP1Second = paymentP1.GetFieldByYearIndex(-2).NValue;
                    totalPaymentP1Next = paymentP1.GetFieldByYearIndex(-1).NValue;
                    totalPaymentP2Second = paymentP2.GetFieldByYearIndex(-2).NValue;
                    totalPaymentP2Next = paymentP2.GetFieldByYearIndex(-1).NValue;
                }
            });
            var count = this.Count;
            var countThird = count.GetFieldByYearIndex(-3).NValue;
            var countSecond = count.GetFieldByYearIndex(-2).NValue;
            var countNext = count.GetFieldByYearIndex(-1).NValue;
            var paymentP1Third = paymentP1.GetFieldByYearIndex(-3).NValue;
            var paymentP1Second = paymentP1.GetFieldByYearIndex(-2).NValue;
            var paymentP1Next = paymentP1.GetFieldByYearIndex(-1).NValue;
            var paymentP2Second = paymentP2.GetFieldByYearIndex(-2).NValue;
            var paymentP2Next = paymentP2.GetFieldByYearIndex(-1).NValue;
            var paymentP3 = this.PaymentP3;
            var paymentP3Next = paymentP3.GetFieldByYearIndex(-1).NValue;
            var startYear = this.Document.CurYear;
            var indexationTable = this._indexationtable;
            var k1 = indexationTable.GetValueByYear(startYear - 3);
            var k2 = indexationTable.GetValueByYear(startYear - 2);
            var k3 = indexationTable.GetValueByYear(startYear - 1);
            var result = 0;
            try {
                var part1 = paymentP1Next / paymentP1Second + totalPaymentP1Next / totalPaymentP1Second;
                result = 1 / 4 *
                    ((paymentP1Second / paymentP1Third * 1 / 3 + paymentP2Next / paymentP2Second * 2 / 3) +
                        (totalPaymentP1Second / totalPaymentP1Third * 1 / 3 +
                            totalPaymentP2Next / totalPaymentP2Second * 2 / 3)) *
                    part1 *
                    paymentP3Next / countNext *
                    1 / k3 *
                    3 / 6 +
                    1 / 2 * part1 * paymentP2Next / countSecond * 1 / k2 * 2 / 6 +
                    paymentP1Next / countThird * 1 / k1 * 1 / 6;
            }
            catch (error) {
                result = 0;
            }
            if (!isFinite(result)) {
                result = 0;
            }
            this.ExpensesVolume.NValue = result;
        };
        P2Sp1DataTable.prototype.ChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            _super.prototype.ChangeEventHandler.call(this, tableId, oldValue, newValue, fieldId);
            var rowKey = this.StrKey;
            if (rowKey === P2Sp1RowsKeys.C0110 || rowKey === P2Sp1RowsKeys.C0120) {
                this.UpdateExpensesVolume();
            }
        };
        P2Sp1DataTable.prototype.InitCopyFieldsInfo = function () {
            return ObasHelper.CreateSimpleInitCopyFieldsInfo(this);
        };
        P2Sp1DataTable.prototype.ResetData = function (keys) {
            var _this = this;
            var resetDataHandler = function () {
                _this.IterateThreePastFields(function (i) {
                    _this.Count.GetFieldByYearIndex(i).NValue = 0;
                    _this.PaymentP1.GetFieldByYearIndex(i).NValue = 0;
                });
                _this.IterateTwoPastFields(function (i) {
                    _this.PaymentP2.GetFieldByYearIndex(i).NValue = 0;
                });
                _this.IteratePlannedFields(function (i) {
                    _this.PlannedCount.GetFieldByYearIndex(i).NValue = 0;
                    _this.StandardExpens.GetFieldByYearIndex(i).NValue = 0;
                    _this.PlannedDeviation.GetFieldByYearIndex(i).NValue = 0;
                    _this.YearData.GetFieldByYearIndex(i).NValue = 0;
                });
                _this.PaymentP3.GetFieldByYearIndex(0).NValue = 0;
            };
            if (keys == null) {
                this.Iterate(resetDataHandler, true);
            }
            else if (this.LocateByKeys(keys.ToArray())) {
                resetDataHandler();
            }
        };
        P2Sp1DataTable.prototype.CollectUserData = function () {
            var _this = this;
            var result = this.CollectTableData(this.InitCopyFieldsInfo());
            var strKeyId = this.StrKeyField.Id;
            var _loop_1 = function (record) {
                var data = record.Data;
                var strKey = data.getValue(strKeyId).Value;
                if (strKey === P2Sp1RowsKeys.Total) {
                    this_1.IterateThreePastFields(function (i) {
                        data.remove(_this.Count.GenerateId(i));
                        data.remove(_this.PaymentP1.GenerateId(i));
                    });
                    this_1.IterateTwoPastFields(function (i) {
                        data.remove(_this.PaymentP2.GenerateId(i));
                    });
                    this_1.IteratePlannedFields(function (i) {
                        data.remove(_this.PlannedCount.GenerateId(i));
                        data.remove(_this.PlannedDeviation.GenerateId(i));
                    });
                    data.remove(this_1.PaymentP3.GenerateId(0));
                }
                else if (strKey === P2Sp1RowsKeys.C0120 || strKey === P2Sp1RowsKeys.C0110) {
                    this_1.IterateThreePastFields(function (i) {
                        data.remove(_this.PaymentP1.GenerateId(i));
                    });
                    this_1.IterateTwoPastFields(function (i) {
                        data.remove(_this.PaymentP2.GenerateId(i));
                    });
                    data.remove(this_1.PaymentP3.GenerateId(0));
                }
                else {
                    this_1.IterateThreePastFields(function (i) {
                        data.remove(_this.Count.GenerateId(i));
                    });
                    this_1.IteratePlannedFields(function (i) {
                        data.remove(_this.PlannedCount.GenerateId(i));
                        data.remove(_this.PlannedDeviation.GenerateId(i));
                    });
                }
            };
            var this_1 = this;
            for (var _i = 0, result_1 = result; _i < result_1.length; _i++) {
                var record = result_1[_i];
                _loop_1(record);
            }
            return result;
        };
        P2Sp1DataTable.prototype.IterateThreePastFields = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            for (var i = 0; i < 3; i++) {
                handler(converToYearIndex ? i - 2 : i);
            }
        };
        P2Sp1DataTable.prototype.IteratePlannedFields = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            for (var i = 0; i < this.Document.Settings.YearsCount - 1; i++) {
                handler(converToYearIndex ? i + 2 : i);
            }
        };
        P2Sp1DataTable.prototype.IterateTwoPastFields = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            for (var i = 0; i < 2; i++) {
                handler(converToYearIndex ? i - 1 : i);
            }
        };
        P2Sp1DataTable.prototype.IterateByYears = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            var indexCorrection = this.Document.InitYear - ObasStageSettings.CurrentYear;
            var startIndex = 0;
            var yearsCount = this.Document.FullYearsCount;
            if (converToYearIndex) {
                startIndex += indexCorrection;
                yearsCount += indexCorrection;
            }
            for (var i = startIndex; i < yearsCount; i++) {
                handler(converToYearIndex ? i + 1 : i);
            }
        };
        return P2Sp1DataTable;
    }(PxSp1DataTable));
    F02300.P2Sp1DataTable = P2Sp1DataTable;
    var P3Sp1RowsKeys;
    (function (P3Sp1RowsKeys) {
        P3Sp1RowsKeys[P3Sp1RowsKeys["Total"] = 1] = "Total";
    })(P3Sp1RowsKeys || (P3Sp1RowsKeys = {}));
    var P3Sp1DataTable = (function (_super) {
        __extends(P3Sp1DataTable, _super);
        function P3Sp1DataTable() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this._coefficient = null;
            _this._insuranceCount = null;
            _this._salary = null;
            _this._copyFields = null;
            return _this;
        }
        Object.defineProperty(P3Sp1DataTable.prototype, "CopyFields", {
            get: function () {
                var _this = this;
                if (this._copyFields == null) {
                    this._copyFields = [this.StrKeyField];
                    var startIndex = this.Document.InitYear - ObasStageSettings.CurrentYear;
                    for (var j = startIndex; j < 1; j++) {
                        this._copyFields.push(this.YearData.GetFieldByYearOffset(j));
                    }
                    this.IterateByYears(function (i) {
                        _this._copyFields.push(_this.Count.GetFieldByYearIndex(i));
                        _this._copyFields.push(_this.InsuranceCount.GetFieldByYearIndex(i));
                        _this._copyFields.push(_this.Salary.GetFieldByYearIndex(i));
                    });
                }
                return this._copyFields;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P3Sp1DataTable.prototype, "Salary", {
            get: function () {
                if (this._salary == null) {
                    this._salary = new NumberGenericObasTableField("Salary_Y", this);
                }
                return this._salary;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P3Sp1DataTable.prototype, "InsuranceCount", {
            get: function () {
                if (this._insuranceCount == null) {
                    this._insuranceCount = new NumberGenericObasTableField("Insurance_Y", this);
                }
                return this._insuranceCount;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P3Sp1DataTable.prototype, "Coefficient", {
            get: function () {
                if (this._coefficient == null) {
                    this._coefficient = new NumberObasTableField("Coefficient", this);
                }
                return this._coefficient;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(P3Sp1DataTable.prototype, "TotalRowKey", {
            get: function () {
                return P3Sp1RowsKeys.Total;
            },
            enumerable: true,
            configurable: true
        });
        P3Sp1DataTable.prototype.UpdatePayment = function (fieldId, salary) {
            this.YearData.GetFieldByField(fieldId).NValue =
                this.InsuranceCount.GetFieldByField(fieldId).NValue *
                    this.Coefficient.NValue *
                    salary;
        };
        P3Sp1DataTable.prototype.ExpensPartsChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            var _this = this;
            if (this.StrKey !== P3Sp1RowsKeys.Total) {
                var salary_1 = 0;
                this.DoWithRestorePosition(function () {
                    salary_1 = _this.GetValueByKeys(_this.Salary.GenerateId(fieldId), _this.GetKeys(P3Sp1RowsKeys.Total));
                });
                this.UpdatePayment(fieldId, salary_1);
            }
            this.ChangeEventHandler(tableId, oldValue, newValue, fieldId);
        };
        P3Sp1DataTable.prototype.SalaryChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            var _this = this;
            this.DoWithRestorePosition(function () {
                _this.Iterate(function () {
                    if (_this.StrKey !== P3Sp1RowsKeys.Total) {
                        _this.UpdatePayment(fieldId, newValue);
                    }
                }, true);
            });
        };
        P3Sp1DataTable.prototype.InitCopyFieldsInfo = function () {
            return ObasHelper.CreateSimpleInitCopyFieldsInfo(this);
        };
        P3Sp1DataTable.prototype.ResetData = function (keys) {
            var _this = this;
            var resetDataHandler = function () {
                _this.IterateByYears(function (i) {
                    _this.Count.GetFieldByYearIndex(i).NValue = 0;
                    _this.InsuranceCount.GetFieldByYearIndex(i).NValue = 0;
                    _this.Salary.GetFieldByYearIndex(i).NValue = 0;
                    _this.YearData.GetFieldByYearIndex(i).NValue = 0;
                });
            };
            if (keys == null) {
                this.Iterate(resetDataHandler, true);
            }
            else if (this.LocateByKeys(keys.ToArray())) {
                resetDataHandler();
            }
        };
        P3Sp1DataTable.prototype.IterateByYears = function (handler, converToYearIndex) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            var indexCorrection = this.Document.InitYear - ObasStageSettings.CurrentYear;
            var startIndex = 0;
            var yearsCount = this.Document.FullYearsCount;
            if (converToYearIndex) {
                startIndex += indexCorrection;
                yearsCount += indexCorrection;
            }
            for (var i = startIndex; i < yearsCount; i++) {
                handler(converToYearIndex ? i + 1 : i);
            }
        };
        P3Sp1DataTable.prototype.CollectUserData = function () {
            var result = this.CollectTableData(this.InitCopyFieldsInfo());
            var strKeyId = this.StrKeyField.Id;
            var _loop_2 = function (record) {
                var data = record.Data;
                var strKey = data.getValue(strKeyId).Value;
                if (strKey === P3Sp1RowsKeys.Total) {
                    var insuranceField_1 = this_2.InsuranceCount;
                    var yearDataField_1 = this_2.YearData;
                    this_2.IterateByYears(function (i) {
                        data.remove(insuranceField_1.GenerateId(i));
                        data.remove(yearDataField_1.GenerateId(i));
                    });
                }
                else {
                    var count_1 = this_2.Count;
                    var salary_2 = this_2.Salary;
                    this_2.IterateByYears(function (i) {
                        data.remove(count_1.GenerateId(i));
                        data.remove(salary_2.GenerateId(i));
                    });
                }
            };
            var this_2 = this;
            for (var _i = 0, result_2 = result; _i < result_2.length; _i++) {
                var record = result_2[_i];
                _loop_2(record);
            }
            return result;
        };
        P3Sp1DataTable.prototype.ShiftData = function () {
            var _this = this;
            var srcFields = [];
            var destFields = [];
            var yearData = this.YearData;
            var insuranceField = this.InsuranceCount;
            var salary = this.Salary;
            var count = this.Count;
            this.IterateByYears(function (yearIndex) {
                _this.Document.CommonRules.ShiftFieldGenerator(yearIndex, srcFields, destFields, yearData, insuranceField, salary, count);
            });
            this.Document.CommonRules.ShiftTableData(this, srcFields, destFields);
        };
        P3Sp1DataTable.prototype.CopyData = function (srcYear, destYear) {
            var yearData = this.YearData;
            var insuranceField = this.InsuranceCount;
            var salary = this.Salary;
            var count = this.Count;
            this.Document.CommonRules.CopyTableData(this, srcYear, destYear, null, yearData, insuranceField, salary, count);
        };
        return P3Sp1DataTable;
    }(PxSp1DataTable));
    F02300.P3Sp1DataTable = P3Sp1DataTable;
    var ExpenseRowsKeys;
    (function (ExpenseRowsKeys) {
        ExpenseRowsKeys[ExpenseRowsKeys["Insurance"] = 1] = "Insurance";
        ExpenseRowsKeys[ExpenseRowsKeys["Obas"] = 2] = "Obas";
    })(ExpenseRowsKeys || (ExpenseRowsKeys = {}));
    var ExpenseDataTable = (function (_super) {
        __extends(ExpenseDataTable, _super);
        function ExpenseDataTable(id, document, _sumTable, _sumKey) {
            var _this = _super.call(this, id, document, null) || this;
            _this._sumTable = _sumTable;
            _this._sumKey = _sumKey;
            _this._copyFields = null;
            _this._yearData = null;
            return _this;
        }
        Object.defineProperty(ExpenseDataTable.prototype, "YearData", {
            get: function () {
                if (this._yearData == null) {
                    this._yearData = new NumberGenericObasTableField(BaseObasTableFields.YearDataField.Id, this);
                }
                return this._yearData;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(ExpenseDataTable.prototype, "CopyFields", {
            get: function () {
                if (this._copyFields == null) {
                    this._copyFields = [this.StrKeyField];
                    var count = this.Document.Settings.YearsCount - 3;
                    var startIndex = this.Document.InitYear - ObasStageSettings.CurrentYear;
                    for (var j = startIndex; j < count; j++) {
                        this._copyFields.push(this.YearData.GetFieldByYearOffset(j));
                    }
                }
                return this._copyFields;
            },
            enumerable: true,
            configurable: true
        });
        ExpenseDataTable.prototype.CollectUserData = function () {
            return this.CollectTableData(this.InitCopyFieldsInfo());
        };
        ExpenseDataTable.prototype.SumChangeEventHandler = function (tableId, oldValue, newValue, fieldId) {
            this._sumTable.SetValueByKeys(fieldId, this._sumTable.GetKeys(this._sumKey), newValue);
        };
        ExpenseDataTable.prototype.InitCopyFieldsInfo = function () {
            return ObasHelper.CreateSimpleInitCopyFieldsInfo(this);
        };
        ExpenseDataTable.prototype.ResetData = function (keys) {
            var _this = this;
            var resetDataHandler = function () {
                _this.IterateByYears(function (i) {
                    _this.YearData.GetFieldByYearIndex(i).NValue = 0;
                });
            };
            if (keys == null) {
                this.Iterate(resetDataHandler, true);
            }
            else if (this.LocateByKeys(keys.ToArray())) {
                resetDataHandler();
            }
        };
        ExpenseDataTable.prototype.IterateByYears = function (handler, converToYearIndex, yearCountCorrection) {
            if (converToYearIndex === void 0) { converToYearIndex = true; }
            if (yearCountCorrection === void 0) { yearCountCorrection = 0; }
            var indexCorrection = this.Document.InitYear - ObasStageSettings.CurrentYear;
            var startIndex = 0;
            var yearsCount = this.Document.FullYearsCount + yearCountCorrection;
            if (converToYearIndex) {
                startIndex += indexCorrection;
                yearsCount += indexCorrection;
            }
            for (var i = startIndex; i < yearsCount; i++) {
                handler(converToYearIndex ? i + 1 : i);
            }
        };
        ExpenseDataTable.prototype.ShiftData = function () {
            var _this = this;
            var srcFields = [];
            var destFields = [];
            var yearData = this.YearData;
            this.IterateByYears(function (yearIndex) {
                _this.Document.CommonRules.ShiftFieldGenerator(yearIndex, srcFields, destFields, yearData);
            });
            this.Document.CommonRules.ShiftTableData(this, srcFields, destFields);
        };
        return ExpenseDataTable;
    }(ObasTableWithStrKeysParent));
    F02300.ExpenseDataTable = ExpenseDataTable;
})(F02300 || (F02300 = {}));
