public int createTariffBand(int uppperLimit, int lowerLimit, double rate, int tariffId)
        {
            TariffBand band = new TariffBand()
            {
                UpperkWhLimit = uppperLimit,
                LowerkWhLimit = lowerLimit,
                UnitRate = rate
            };

            int bandId = mediator.DataManager.saveTariffBand(band);
            addBandToTariff(bandId, tariffId);
            return bandId;
        }
 public abstract TariffBand editTariffBand(int tariffBandId, TariffBand tariffBand);
 public abstract int saveTariffBand(TariffBand tariffBand);
 /// <summary>
 /// Saves a tariff band object to the database
 /// </summary>
 /// <param name="tariffBand"></param>
 public override int saveTariffBand(TariffBand tariffBand)
 {
     emdb.TariffBands.Add(tariffBand);
     emdb.SaveChanges();
     return tariffBand.Id;
 }
        /// <summary>
        /// Update tariffBand with specified id.
        /// </summary>
        /// <param name="tariffBandId">id of tariff band to update</param>
        /// <param name="tariffBand">replacement tariff band object</param>
        public override TariffBand editTariffBand(int tariffBandId, TariffBand newBand)
        {
            TariffBand oldBand = emdb.TariffBands.Find(tariffBandId);

            oldBand.UnitRate = newBand.UnitRate;
            oldBand.LowerkWhLimit = newBand.LowerkWhLimit;
            oldBand.UpperkWhLimit = newBand.UpperkWhLimit;

            return oldBand;
        }
        public static Tariff createTariffFromViewModel(TariffViewModel tariffViewModel)
        {
            Tariff tariff = new Tariff();

            if (tariffViewModel.Id != 0)
            {
                tariff.Id = tariffViewModel.Id;
            }

            tariff.SCPeriod = new Period { Id = tariffViewModel.SCPeriodId };
            tariff.BandPeriod = new Period { Id = tariffViewModel.BandPeriodId };
            tariff.SCValue = tariffViewModel.SCValue;
            tariff.StartDate = tariffViewModel.StartDate;
            tariff.Bands = new List<TariffBand>();

            TariffBand newBand = new TariffBand();
            newBand.LowerkWhLimit = (int)tariffViewModel.LowerLimit1;
            newBand.UnitRate = (int)tariffViewModel.Rate1;
            newBand.Id = tariffViewModel.Band1Id;

            ///isBanded is determined during conversion to tariffViewModel when the tariff object is retreived in the HTTP GET Edit method
            ///of the TariffController.  It is preserved to HTTP POST Edit via hidden fields in the Edit.cshtml view.

            ///isbanded should be determined at each conversion since the user may want to edit an existing tariff to include additional / fewer bands
            ///

            tariffViewModel.IsBanded = !((tariffViewModel.LowerLimit1 == 0 | tariffViewModel.LowerLimit1 == null) &&
                                        (tariffViewModel.UpperLimit1 == 0 | tariffViewModel.UpperLimit1 == null));

            if (!tariffViewModel.IsBanded)
            {
                ///no upper limit and therefore no subsequent bands

                newBand.UpperkWhLimit = 0;
                tariff.Bands.Add(newBand);
            }
            else
            {
                ///get upperLimit for first band & save to tariff model object
                newBand.UpperkWhLimit = (int)tariffViewModel.UpperLimit1;
                tariff.Bands.Add(newBand);

                ///follow same pattern for second band
                TariffBand newBand2 = new TariffBand();
                newBand2.LowerkWhLimit = (int)tariffViewModel.LowerLimit2;
                newBand2.UnitRate = tariffViewModel.Rate2;
                newBand2.Id = tariffViewModel.Band2Id;

                if ((tariffViewModel.UpperLimit2 == null | tariffViewModel.UpperLimit2 == 0) && (tariffViewModel.LowerLimit2 == 0 | tariffViewModel.LowerLimit2 == null))
                {
                    ///there is no band if lower is null/zero AND upper is null/zero
                    newBand2.UpperkWhLimit = -1; ///no band
                }
                else
                {
                    ///there is a band...
                    if (tariffViewModel.UpperLimit2 != 0)
                    {
                        ///with an upper limit
                        newBand2.UpperkWhLimit = (int)tariffViewModel.UpperLimit2;
                    }
                    else
                    {
                        ///without an upper limit
                        newBand2.UpperkWhLimit = 0;
                    }
                }

                tariff.Bands.Add(newBand2);

                TariffBand newBand3 = new TariffBand();
                newBand3.LowerkWhLimit = (int)tariffViewModel.LowerLimit3;
                newBand3.UnitRate = tariffViewModel.Rate3;
                newBand3.Id = tariffViewModel.Band3Id;

                if ((tariffViewModel.UpperLimit3 == null | tariffViewModel.UpperLimit3 == 0) && (tariffViewModel.LowerLimit3 == 0 | tariffViewModel.LowerLimit3 == null))
                {
                    ///there is no band if lower is null/zero and upper is null/zero
                    newBand3.UpperkWhLimit = -1; ///no band
                }
                else
                {
                    ///there is a band...
                    if (tariffViewModel.UpperLimit3 != 0)
                    {
                        ///with an upper limit
                        newBand3.UpperkWhLimit = (int)tariffViewModel.UpperLimit3;
                    }
                    else
                    {
                        ///without an upper limit
                        newBand3.UpperkWhLimit = 0;
                    }
                }

                tariff.Bands.Add(newBand3);
            }

            return tariff;
        }