116 lines
3.3 KiB
C#
116 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Web.Mvc;
|
|
using MileageTraker.Web.Attributes;
|
|
using MileageTraker.Web.DAL;
|
|
|
|
namespace MileageTraker.Web.Models
|
|
{
|
|
public class Log : IValidatableObject
|
|
{
|
|
[Key]
|
|
[HiddenInput(DisplayValue = false)]
|
|
public int LogId { get; set; }
|
|
|
|
[Required]
|
|
[Remote("Exists", "Vehicle", ErrorMessage = "ID not found")]
|
|
[StringLength(6, MinimumLength = 4, ErrorMessage = "Enter 4 digit number")]
|
|
[Display(Name = "Vehicle ID")]
|
|
[InputSize("mini")]
|
|
[RegularExpression(@"\d+", ErrorMessage = "Enter only numbers")]
|
|
public string VehicleId { get; set; }
|
|
|
|
[Required]
|
|
[Range(1, 500000, ErrorMessage = "Between 1 and 500k")]
|
|
[Display(Name = "End Odometer")]
|
|
[InputSize("small")]
|
|
[Units("Miles")]
|
|
public int EndOdometer { get; set; }
|
|
|
|
[Required]
|
|
[Display(Name = "Type")]
|
|
[NoEditLabel]
|
|
public MileageLogTypeWrapper LogType { get; set; }
|
|
|
|
[Required]
|
|
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
|
|
[InputSize("medium")]
|
|
public string CityName { get; set; }
|
|
|
|
[Required]
|
|
[Display(Name = "Employee / Driver")]
|
|
[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Need complete name")]
|
|
[InputSize("medium")]
|
|
public string EmployeeName { get; set; }
|
|
|
|
[Display(Name = "Employee / Driver")]
|
|
[HiddenInput(DisplayValue = false)]
|
|
public virtual User User { get; set; }
|
|
|
|
[Range(0, 50)]
|
|
[DisplayFormat(DataFormatString = "{0:0.000}", ApplyFormatInEditMode = true)]
|
|
[Units("Gallons")]
|
|
[FormatHint("n.nnnn")]
|
|
[InputSize("mini")]
|
|
public double GasPurchased { get; set; }
|
|
|
|
[Required]
|
|
[DataType(DataType.Date)]
|
|
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
|
|
[DenyFutureDate(ErrorMessage = "Future date")]
|
|
[FormatHint("mm/dd/yyyy")]
|
|
[InputSize("small")]
|
|
public DateTime Date { get; set; }
|
|
|
|
[Display(Name = "Time Created")]
|
|
[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy h:mm tt}")]
|
|
[HiddenInput]
|
|
public DateTime Created { get; set; }
|
|
|
|
/// <summary>
|
|
/// url route that was used to create this instance, ie 'CreateLog\Confirm' or 'Logs\Create'
|
|
/// </summary>
|
|
[HiddenInput(DisplayValue = false)]
|
|
public string Source { get; set; }
|
|
|
|
[HiddenInput(DisplayValue = false)]
|
|
public string UserHostAddress { get; set; }
|
|
|
|
[HiddenInput(DisplayValue = false)]
|
|
public string UserAgent { get; set; }
|
|
|
|
[HiddenInput(DisplayValue = false)]
|
|
public int? VehiclePreviousLogId { get; set; }
|
|
|
|
[HiddenInput(DisplayValue = false)]
|
|
public virtual Log VehiclePreviousLog { get; set; }
|
|
|
|
public Log()
|
|
{
|
|
LogType = new MileageLogTypeWrapper();
|
|
}
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
{
|
|
if (LogType == MileageLogType.GasPurchase && (GasPurchased.Equals(0)))
|
|
yield return new ValidationResult("Enter amount of gas purchased", new[] { "GasPurchased" });
|
|
|
|
if (LogType.Value == 0)
|
|
yield return new ValidationResult("Required", new[] { "LogType" });
|
|
|
|
ValidationResult result = null;
|
|
try
|
|
{
|
|
var dataService = new DataService();
|
|
dataService.ValidateOdometerChronology(VehicleId, EndOdometer, Date);
|
|
}
|
|
catch (ChronologicalOrderException ex)
|
|
{
|
|
result = new ValidationResult(ex.Message, new[] { "EndOdometer" });
|
|
}
|
|
if (result != null)
|
|
yield return result;
|
|
}
|
|
}
|
|
} |