public static FeeCalcHelper GetFeeCalculation(int calcID)
        {
            IDalSession session = NHSessionFactory.CreateSession();
            FeeCalcHelper helper = null;

            IFeeCalc calc = FeeCalcMapper.GetFeeCalculation(session, calcID);
            if (calc != null)
                helper = new FeeCalcHelper(calc);

            session.Close();
            return helper;
        }
        public static bool UpdateCalcVersion(FeeCalcHelper helper)
        {
            if ((helper != null))
            {
                IDalSession session = NHSessionFactory.CreateSession();
                ILogin login = LoginMapper.GetCurrentLogin(session);
                IFeeCalc calc;
                IFeeCalcVersion version = null;
                ICurrency currency = null;
                bool createNewVersion = true;

                if (helper.CalcID == 0)   //First Create the Model
                {
                    FeeType feeType = FeeRuleMapper.GetFeeType(session, helper.FeeType);
                    currency = InstrumentMapper.GetCurrency(session, helper.CurrencyID);
                    IAssetManager manager = (IAssetManager)LoginMapper.GetCurrentManagmentCompany(session);
                    calc = new FeeCalc(feeType, helper.CalcName, currency, manager);
                }
                else
                {
                    calc = FeeCalcMapper.GetFeeCalculation(session, helper.CalcID);
                    currency = calc.FeeCurrency;
                    if (helper.Equals(calc))
                        createNewVersion = false;
                    else if (calc.LatestVersion.StartPeriod == helper.StartPeriod)
                        createNewVersion = false;

                    // check if previous version exists with the same startPeriod
                    if (createNewVersion)
                    {
                        if (calc.Versions.Where(u => u.StartPeriod == helper.StartPeriod).Count() == 1)
                            createNewVersion = false;
                    }
                }

                calc.Name = helper.CalcName;
                calc.IsActive = helper.IsActive;

                if (createNewVersion)
                {
                    Money fixedSetup = new Money(helper.SetUp, currency);
                    switch (helper.FeeCalcType)
                    {
                        case FeeCalcTypes.Flat:
                            version = new FeeCalcVersionFlat(calc, fixedSetup, null, null, helper.StartPeriod, login.UserName);
                            break;
                        case FeeCalcTypes.Simple:
                            version = new FeeCalcVersionSimple(calc, fixedSetup, helper.StartPeriod, login.UserName);
                            break;
                    }
                    calc.Versions.Add(version);
                }
                else
                {
                    version = calc.Versions.Where(u => u.StartPeriod == helper.StartPeriod).FirstOrDefault();
                    if (version == null)
                        version = calc.LatestVersion;
                }

                if (version.Equals(calc.LatestVersion))
                    version.EndPeriod = helper.EndPeriod;
                switch (version.FeeCalcType)
                {
                    case FeeCalcTypes.Flat:
                        IFeeCalcVersionFlat flat = (IFeeCalcVersionFlat)version;
                        flat.MinValue = (helper.MinValue != 0 ? new Money(helper.MinValue, currency) : null);
                        flat.MaxValue = (helper.MaxValue != 0 ? new Money(helper.MaxValue, currency) : null);

                        if (helper.Lines == null || helper.Lines.Count == 0)
                            throw new ApplicationException("Staffel is missing.");

                        int counter = 0;
                        foreach (FeeCalcLineHelper line in helper.Lines.OrderBy(u => u.LowerRange))
                        {
                            IFeeCalcLine feeLine = flat.FeeLines.GetItemBySerialNo(counter);
                            if (feeLine == null)
                            {
                                feeLine = new FeeCalcLine(
                                    new Money(line.LowerRange, currency),
                                    (line.StaticCharge != 0 ? new Money(line.StaticCharge, currency) : null),
                                    line.FeePercentage);
                                flat.FeeLines.Add(feeLine);
                            }
                            else
                            {
                                feeLine.LowerRange = new Money(line.LowerRange, currency);
                                feeLine.FeePercentage = line.FeePercentage;
                                feeLine.StaticCharge = (line.StaticCharge != 0 ? new Money(line.StaticCharge, currency) : null);
                            }
                            counter++;
                        }

                        // get rid of removed lines
                        if (flat.FeeLines.Count > helper.Lines.Count)
                        {
                            for (int i = flat.FeeLines.Count; i > helper.Lines.Count; i--)
                            {
                                flat.FeeLines.RemoveAt(i - 1);
                            }
                        }
                        break;
                    case FeeCalcTypes.Simple:
                        IFeeCalcVersionSimple simple = (IFeeCalcVersionSimple)version;
                        simple.NoFees = helper.NoFees;
                        break;

                }

                session.InsertOrUpdate(calc);
                session.Close();
            }
            return true;
        }
    protected void btnUpdateVersion_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate();
            if (Page.IsValid)
            {
                FeeCalcHelper fch = new FeeCalcHelper();
                fch.CalcID = CalcID;
                fch.CalcName = this.tbCalcName.Text;
                fch.FeeType = (B4F.TotalGiro.Fees.FeeTypes)Utility.GetKeyFromDropDownList(ddlFeeType);
                fch.CurrencyID = Utility.GetKeyFromDropDownList(ddlCurrency);
                fch.IsActive = this.chkIsActive.Checked;
                fch.StartPeriod = ppStartPeriod.SelectedPeriod;
                fch.EndPeriod = ppEndPeriod.SelectedPeriod;
                fch.FeeCalcType = (B4F.TotalGiro.Fees.FeeCalcTypes)Convert.ToInt32(rbSlabFlat.SelectedValue);
                fch.NoFees = chkNoFees.Checked;
                fch.SetUp = dbSetUp.Value;
                fch.MinValue = dbMinValue.Value;
                fch.MaxValue = dbMaxValue.Value;

                int index = 0;
                FlatSlab flatslabcontrol = (FlatSlab)this.FlatSlabPlaceHolder.FindControl(string.Format("flatslab_{0}", index++));
                while (flatslabcontrol != null)
                {
                    fch.Lines.Add(createFeeCalcLineHelper(flatslabcontrol));
                    flatslabcontrol = (FlatSlab)this.FlatSlabPlaceHolder.FindControl(string.Format("flatslab_{0}", index++));
                }

                FeeCalculationsOverviewAdapter.UpdateCalcVersion(fch);
                setupEditMode(false, EDITMODE.None);
                initialSetup();
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = Utility.GetCompleteExceptionMessage(ex);
        }
    }