How to check data range?

Asked

Viewed 195 times

8

When requesting a vehicle reservation, the system inserts the scheduled departure and return time code (we prefer to work with fixed codes instead of schedules in order to facilitate programming). What happens is that when the reservation is made at times ahead (for example, I am booking in the morning an hour in the afternoon) the system allows another reservation that goes "over" the previous reservation.

For example, if I book from 9:00 to 10:00, mine select does not display this interval after confirmation, but if someone reserves from 8:00 to 11:00 on the same day he will let record. With the select below I do not display the booked schedules, any idea how to do the validation?

SELECT H.* FROM HORARIOS H
WHERE NOT EXISTS (
    SELECT * FROM RESERVAS R
    WHERE H.COD BETWEEN R.COD_HORA_SAIDA AND
          R.COD_HORA_PREVISTA-1 AND
          R.COD_VEICULO = :codveiculo AND
          DATE_FORMAT(DATA_SAIDA, \'%d-%m-%Y\') = :codcalendario AND
          r.ativa = 1)
ORDER BY H.COD

Below is an example of the situation that occurs:

inserir a descrição da imagem aqui

  • I couldn’t understand what your problem is. Can you explain it better please?

  • For example, I enter in the "reservations" table my reservation, for example, from 14:00 to 16:00. The system blocks the display of this interval, so no one can reserve this same time. But if someone wants to book from 13:00 (time that is free) until 17:00 (time also free) will manage, going over the previous reservation.

  • So if someone wants to book from 1:00 to 5:00, they shouldn’t be able to book either, right?

  • How do you save that time code in the bank, @Diego? I wonder why, perhaps, if a second table were created, which would function as a list, and stored the hours that the car would remain, something like (following its example) [14 15 16], could be easy to measure, and your CONTAINS function by checking the saved record for the record to be saved.

  • Another question, if it was selected from 13h to 15h, and if there was already a record of 14h to 16h, should not be allowed?

  • @George B. exactly, if he wanted from 1:00 to 2:00 he could, but from 1:00 to 5:00 should be blocked.

  • @Gustavocinque I have a schedule, every half hour. In every system I treat the hours with the code. You mean instead of recording an intermission, recording all the time he gets "out"? Type, time reservation code 1 to 5 record entire interval (1,2,3,4,5) in the bank?

  • Yes, that’s right. So you could compare the two lists, if they contain the same elements. But you would still save the input and output columns, this second table would be for validation of this part of the system.

  • This would be a bank solution, you could implement the same idea programmatically.

  • @Gustavocinque I’m trying to build something with PHP, but really this is complicated.

Show 5 more comments

2 answers

2

Come on. I will try to use a pseudo code, because I have no basis in php.

Given the various:

horaEntradaBanco = registroBanco.horaEntrada;
horaSaidaBanco = registroBanco.horaSaida;
horaEntradaMarkup = $('#campoHorarioEntrada').getValue();
horaSaidaMarkup = $('#campoHorarioSaida').getValue();

You could do the following validations:

if(horaEntradaMarkup<=horaEntradaBanco && horaSaidaMarkup>=horaSaidaBanco){
    //bloquear utilização de horário
    //exemplo de entrada no if: Horas banco: 14h~15h
                                Horas Markup: 13h~17h
} else if(horaEntradaMarkup<=horaEntradaBanco && horaSaidaMarkup<=horaSaidaBanco) {
    //bloquear utilização de horário
    //exemplo de entrada no if: Horas banco: 14h~16h
                                Horas Markup: 13h~15h

    //Aqui eu não sei se já está sendo validado ou não, já que vc só falou do exemplo acima.
}

See if it works.

  • Your logic is correct, but I’m going to have to adapt it, because I can have multiple entries on the same day and somehow need to go through all.

2


I decided as follows:

SELECT count(*) as flag FROM HORARIOS H
                     WHERE EXISTS (
                           SELECT * FROM RESERVAS R
                                WHERE H.COD BETWEEN R.COD_HORA_SAIDA AND
                                      R.COD_HORA_PREVISTA-1 AND
                                      R.COD_VEICULO = 2 AND
                                      DATE_FORMAT(DATA_SAIDA, '%d-%m-%Y') = :data_saida and
                                      (h.cod between :codsaida and :codretorno) and
                                      r.ativa = 1)
                                ORDER BY H.COD;

That way, I made an if, if the result is more than 1 has something in the range and gives error. Thank you all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.