I have a table in the database named Quota, this quota has two columns one for start_date and end_date, so far so good, now I have another table called Order that also has start_date and date_fim, these tables are related 1 for N, one quota can have N Orders, I created a validation to not let the user create an order that is not greater or smaller than the date of the registered quota, every order must be within the range registered in the quota, my problem is that I can not leave the order too user to enter an order that enters within the range of another order, example:
Date Quota: start_date 10/08/2017 and end_data: 08/20/2017
1: Order date: start_date 11/08/2017 and end_data: 08/15/2017
Here's the problem:
2: Order date: start_date 08/13/2017 and end_date: 08/17/2017
The date_inicio 08/13/2017 of this last order has entered the range of 1 Order: date_inicio 11/08/2017 and date_fim: 08/15/2017 this can not occur.
Is there any method or solution to this, suggestion for improvement? Here is the method with some validations I have made.
public void ValidarDataDaOrdem(decimal idCota, DateTime dataInicio, DateTime dataFim)
{
var cota = Context.Cota.Include("Ordem").Where(e => e.IdCota == idCota).FirstOrDefault();
var ordens = cota.Ordem.ToList();
if (dataInicio < cota.dataInicioCota)
{
throw new InfoException("Data Inico tem que ser maior que a data inicio da cota");
}
if (dataFim > cota.DataFimCota)
{
throw new InfoException("Data Final tem que ser menor que a data final da cota");
}
foreach (var item in ordens)
{
if (item.DataInicioOrdem == dataInicio && item.DataInicioOrdem == DataFim)
{
throw new InfoException("Ja existe solicitação cadastrada para esse periodo");
}
if (item.DataInicioOrdem == dataInicio || item.DataInicioOrdem == DataFim)
{
throw new InfoException("Ja existe solicitação cadastrada para essa data");
}
}
}