2
I have a table in the bank called Quota, this quota has two columns one for data_start and data_end, so far so good, now I have another table called Order that also has data_start and data_end, these tables are related 1 to N, a quota can have N Orders, I created a validation to not let the user create an order that is not larger or smaller than the date of the registered quota, all order must be within the range registered in Quota, my problem is that I can also not let the user register an order that enters within the range of other order, example:
Date of Quota: starting date 10/08/2017 and enddate: 20/08/2017
1: Order date: starting date 11/08/2017 and ending date: 15/08/2017
Here is the problem:
2: Date of the order: starting date 13/08/2017 and ending date: 17/08/2017
The starting date 13/08/2017 of this last order entered the interval of 1 Order: start_date 11/08/2017 and end_date: 08/15/2017 this cannot occur.
Is there any method or solution for this, suggested improvement ? Here’s the method with some validations I 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");
}
}
}