private void SaveTariffRates(IUnitOfWork uow) { string[] cellIDs = hidTariffRateChanges.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (!cellIDs.Any() || cboTariffTable.SelectedIndex < 0) { return; } var repo = DIContainer.CreateRepository <Repositories.ITariffRepository>(uow); var tariffTableID = int.Parse(cboTariffTable.SelectedValue); var tariffTable = repo.FindTariffTable(tariffTableID); foreach (var cellID in cellIDs) { //The cellID is made up of <GridName>:<ZoneId>:<Pallet> string[] parts = cellID.Split(':'); var zoneID = int.Parse(parts[1]); var scaleValueID = int.Parse(parts[2]); //Find the TariffRate for the Zone and ScaleValue var tariffRate = tariffTable.TariffRates.SingleOrDefault(tr => tr.ScaleValueID == scaleValueID && tr.ZoneID == zoneID); //Create a new one if one could not be found if (tariffRate == null) { tariffRate = new Models.TariffRate { ScaleValueID = scaleValueID, ZoneID = zoneID }; tariffTable.TariffRates.Add(tariffRate); } decimal value = 0; if (decimal.TryParse(this.Request.Form[cellID], out value)) { tariffRate.IsEnabled = true; tariffRate.Rate = value; } else { tariffRate.IsEnabled = false; tariffRate.Rate = 0m; } } }
private Models.TariffTable ImportRateTable() { //Create a new TariffVersion and assign entered values var table = this.PopulateEmptyTariffTable(); using (var uow = DIContainer.CreateUnitOfWork()) { var repo = DIContainer.CreateRepository <Repositories.ITariffRepository>(uow); var tariffVersion = repo.FindTariffVersion(this.TariffVersionID); //Add the new Tariff table to the Tariff Version tariffVersion.TariffTables.Add(table); ////////////////////////////////////////////////////// // Insert the rates here var scaleValues = tariffVersion.Scale.ScaleValues.OrderBy(sv => sv.Value).Select(sv => new { sv.ScaleValueID, sv.Value }).ToList(); var fieldCount = 0; using (var sr = new StringReader(txtImportRateTable.Text)) { var line = string.Empty; while (!string.IsNullOrEmpty(line = sr.ReadLine())) { // File will be tab delimited. string[] fields = line.Split("\t".ToCharArray()); fieldCount = fields.Length; if ((fieldCount - 1) != scaleValues.Count) { throw new ApplicationException("Column count and scale count mismatch. You must provide a column of data for each scale value."); } string zoneDescription = fields[0]; var zone = tariffVersion.ZoneMap.Zones.FirstOrDefault(z => z.Description == zoneDescription); if (zone == null) { // if the zone description starts with a number then add a leading "0" and try and find it again if (char.IsDigit(zoneDescription.Substring(0, 1), 0)) { for (var i = 0; i < 2; i++) { zoneDescription = string.Format("0{0}", zoneDescription); zone = tariffVersion.ZoneMap.Zones.FirstOrDefault(z => z.Description == zoneDescription); if (zone != null) { break; } } } if (zone == null) { throw new ApplicationException(string.Format("Zone {0} cannot be found for the selected tariff table.", zoneDescription)); } } for (var iCol = 1; iCol < fieldCount; iCol++) { // Check the zone exists. // Get the scale value to update if there is one, else create it. var scaleValue = scaleValues[iCol - 1]; var rate = 0m; var isNumeric = decimal.TryParse(RemoveSpacesAndCurrencySymbol(fields[iCol]), out rate); if (!isNumeric) { throw new ApplicationException(string.Format("The rate: {0} is not a valid number.", fields[iCol])); } var tariffRate = new Models.TariffRate { ZoneID = zone.ZoneID, ScaleValueID = scaleValue.ScaleValueID, Rate = rate, IsEnabled = true }; table.TariffRates.Add(tariffRate); } } } // ////////////////////////////////////////////////////// //Save changes to database uow.SaveChanges(); } //and return the new tariff with its assigned TariffId return(table); }