public void ValidateLoad(LoadDetailData load, Guid customerId, OrderAddressValidationEnum validateAddress, bool manuallyCreated, string urnPrefix, BaseServiceResponse response) { _messages = manuallyCreated ? new WebUILoadValidationMessages() : new LoadValidationMessages(); if (string.IsNullOrWhiteSpace(load.ReferenceLoadId)) { response.AddError($"{urnPrefix}:{nameof(load.ReferenceLoadId)}", _messages.ReferenceLoadIdRequired); } if (string.IsNullOrWhiteSpace(load.ReferenceLoadDisplay)) { response.AddError($"{urnPrefix}:{nameof(load.ReferenceLoadDisplay)}", _messages.ReferenceLoadDisplayRequired); } if (string.IsNullOrWhiteSpace(load.EquipmentType)) { response.AddError($"{urnPrefix}:{nameof(load.EquipmentType)}", _messages.EquipmentTypeRequired); } else { var equipment = _context.Equipment.FirstOrDefault(_ => _.EquipmentId == load.EquipmentType || _.EquipmentDesc == load.EquipmentType); if (equipment == null) { response.AddError($"{urnPrefix}:{nameof(load.EquipmentType)}", _messages.EquipmentTypeInvalid); } else { load.EquipmentType = equipment.EquipmentId; } } if (load.LineHaulRate < 0) { response.AddError($"{urnPrefix}:{nameof(load.LineHaulRate)}", _messages.LineHaulRateInvalid); } if (load.FuelRate < 0) { response.AddError($"{urnPrefix}:{nameof(load.FuelRate)}", _messages.FuelRateInvalid); } var totalLineWeight = load.LineItems?.Sum(_ => _.Weight); if (load.Weight <= 0 && totalLineWeight > 0) { load.Weight = totalLineWeight.Value; } else if (load.Weight < 0) { response.AddError($"{urnPrefix}:{nameof(load.Weight)}", _messages.WeightInvalid); } else if (totalLineWeight > 0 && totalLineWeight != load.Weight) { response.AddError($"{urnPrefix}:{nameof(load.Weight)}", _messages.TotalWeightInvalid); } //default line item volume's to 1 if (load.LineItems != null) { foreach (var lineItem in load.LineItems.Where(_ => _.Volume == 0)) { lineItem.Volume = 1; } } var totalLineVolume = load.LineItems?.Sum(_ => _.Volume); if (load.Cube <= 0 && totalLineVolume > 0) { load.Cube = totalLineVolume.Value; } else if (load.Cube < 0) { response.AddError($"{urnPrefix}:{nameof(load.Cube)}", _messages.CubeInvalid); } else if (totalLineVolume > 0 && load.Cube != totalLineVolume) { response.AddError($"{urnPrefix}:{nameof(load.Cube)}", _messages.TotalVolumeInvalid); } ConvertAndValidateTransportationMode(load, manuallyCreated, urnPrefix, response); ConvertAndValidateStopData(load, customerId, validateAddress, manuallyCreated, urnPrefix, response); ValidateContactData(load, manuallyCreated, urnPrefix, response); ConvertAndValidateLineData(load, manuallyCreated, urnPrefix, response); ConvertAndValidateServiceTypes(load, urnPrefix, response); }
public virtual void ConvertAndValidateStopData(LoadDetailData load, Guid customerId, OrderAddressValidationEnum validateAddress, bool manuallyCreated, string urnPrefix, BaseServiceResponse response) { if (load.LoadStops == null || load.LoadStops.Count < 2) { response.AddError($"{urnPrefix}:{nameof(load.LoadStops)}", _messages.LoadStopsTooFew); return; } //convert the stop type var usedStopNumbers = new List <int>(); var assignStopTypeIfOmitted = load.LoadStops.Count == 2 && !manuallyCreated; bool firstDeliveryStopFound = false; DateTime lastPickupDateTime = DateTime.MinValue; for (int i = 0; i < load.LoadStops.Count; i++) { var stopUrnPrefix = $"{urnPrefix}:{nameof(load.LoadStops)}:{i}"; var stop = load.LoadStops[i]; if (usedStopNumbers.Contains(stop.StopNbr)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.StopNbr)}", _messages.StopNumberDuplicated); } usedStopNumbers.Add(stop.StopNbr); if (!string.IsNullOrWhiteSpace(stop.StopType)) { stop.StopTypeId = StopTypes?.FirstOrDefault(_ => string.Compare(_.Name, stop.StopType, true) == 0)?.StopTypeId; if (stop.StopTypeId == null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeInvalid); } } else if (assignStopTypeIfOmitted) { stop.StopTypeId = i == 0 ? (int?)StopTypeEnum.Pickup : (int?)StopTypeEnum.Delivery; } else if (manuallyCreated) { response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeRequired); } if (stop.StopTypeId != null) { if (stop.StopTypeId == (int)StopTypeEnum.Delivery) { firstDeliveryStopFound = true; } else if (firstDeliveryStopFound) { response.AddError($"{stopUrnPrefix}:{nameof(stop.StopType)}", _messages.StopTypeOutOfOrder); } } if (manuallyCreated && stop.StopType == StopTypeEnum.Delivery.ToString()) { var stopLineItems = load.LineItems?.Where(x => x.DeliveryStopNumber == stop.StopNbr); if (stopLineItems == null || stopLineItems.Count() == 0) { response.AddError($"{stopUrnPrefix}:{nameof(load.LineItems)}", _messages.LineItemsTooFew); } } if (manuallyCreated) { if (string.IsNullOrWhiteSpace(stop.LocationName)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.LocationName)}", _messages.LocationNameRequired); } if (string.IsNullOrWhiteSpace(stop.Address1)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.Address1)}", _messages.Address1Required); } if (string.IsNullOrWhiteSpace(stop.City)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.City)}", _messages.CityRequired); } if (string.IsNullOrWhiteSpace(stop.State)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.State)}", _messages.StateRequired); } if (string.IsNullOrWhiteSpace(stop.Country)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.Country)}", _messages.CountryRequired); } if (string.IsNullOrWhiteSpace(stop.PostalCode)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.PostalCode)}", _messages.PostalCodeRequired); } } if (validateAddress == OrderAddressValidationEnum.Validate) { if (!_addressValidationService.IsAddressValid(customerId, stop)) { response.AddError($"{stopUrnPrefix}", _messages.AddressInvalid); } } else if (validateAddress == OrderAddressValidationEnum.Replace) { var address = _addressValidationService.GetValidAddress(stop); if (address != null) { stop.Address1 = address.Address1; stop.City = address.City; stop.State = address.State; stop.Country = address.Country; stop.PostalCode = address.PostalCode; } } if (stop.StopTypeId == (int)StopTypeEnum.Pickup) { lastPickupDateTime = stop.LateDtTm; } ValidateStopTimes(stop, manuallyCreated, lastPickupDateTime, stopUrnPrefix, response); if (string.IsNullOrWhiteSpace(stop.ApptType)) { response.AddError($"{stopUrnPrefix}", _messages.ApptTypeRequired); } var schedulingCode = string.IsNullOrWhiteSpace(stop.AppointmentSchedulingCode) ? null : stop.AppointmentSchedulingCode; var confirmationCode = string.IsNullOrWhiteSpace(stop.AppointmentConfirmationCode) ? null : stop.AppointmentConfirmationCode; if (manuallyCreated) { if (schedulingCode == null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeRequired); } if (confirmationCode == null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeRequired); } } else { //API loads can have both or none if (schedulingCode == null && confirmationCode != null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeMissing); } if (schedulingCode != null && confirmationCode == null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeMissing); } } if (schedulingCode != null && confirmationCode != null) { if (!(AppointmentSchedulerConfirmationTypes?.Where(_ => string.Compare(_.AppointmentSchedulingCode, schedulingCode, true) == 0).Any() ?? false)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentSchedulingCode)}", _messages.AppointmentSchedulingCodeInvalid); } if (!(AppointmentSchedulerConfirmationTypes?.Where(_ => string.Compare(_.AppointmentConfirmationCode, confirmationCode, true) == 0).Any() ?? false)) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentConfirmationCodeInvalid); } stop.AppointmentSchedulerConfirmationTypeId = AppointmentSchedulerConfirmationTypes? .FirstOrDefault(_ => string.Compare(_.AppointmentSchedulingCode, schedulingCode, true) == 0 && string.Compare(_.AppointmentConfirmationCode, confirmationCode, true) == 0) ?.AppointmentSchedulerConfirmationTypeId; if (stop.AppointmentSchedulerConfirmationTypeId == null) { response.AddError($"{stopUrnPrefix}:{nameof(stop.AppointmentConfirmationCode)}", _messages.AppointmentCodeMismatch); } } if (stop.StopTypeId == (int)StopTypeEnum.Pickup) { if (manuallyCreated && !(load.LineItems?.Any(_ => _.PickupStopNumber == stop.StopNbr) ?? false)) { response.AddError($"{stopUrnPrefix}", _messages.DeliveryItemRequired); } } ValidateStopContactData(stop, stopUrnPrefix, response); } }