Increase of two days for each month worked

1

I have a table of usuarios in a BD, where I have the following fields:

id_ususario
login
passwd
dias_disponibles (Los usuarios fijos tienen 24 días disponibles)
fecha_ingreso (Fecha ingreso en empresa)
tipo_usuario (Fijo o Temporal)

Each user can make a request for a vacation. The user admin is responsible for adding the new users. My problem is with users temporales . A temporary user will have the available days according to the months worked up to one year. For each month worked, dias_disponibles is increased by 2. For example, if usuario1 since you joined the company, takes 2 months worked because it will have 4 dias_disponibles and so on until it takes 12 months, then starting from there, they will always be 24 dias_disponibles . I have this function that calculates the difference of months since I joined the company until today:

function difmeses($fechaingreso){
$fechainicial = new DateTime($fechaingreso);

$fechaactual = (new DateTime)->format('Y-m-d H:i');
$fechafinal = new DateTime($fechaactual);

$diferencia = $fechainicial->diff($fechafinal);

$meses = ( $diferencia->y * 12 ) + $diferencia->m;

return $meses;
}

My question is what to use to increase it until 12 months in the company, if it has been 12 months. Any help on how to do the increase of 2 days, a for , a while ... ??

    
asked by Xerox 14.03.2018 в 16:21
source

2 answers

1

You can change the return of the function as follows:

function difmeses($fechaingreso){
    $fechainicial = new DateTime($fechaingreso);

    $fechaactual = (new DateTime)->format('Y-m-d H:i');
    $fechafinal = new DateTime($fechaactual);
    $diferencia = $fechainicial->diff($fechafinal);

    $meses = ( $diferencia->y * 12 ) + $diferencia->m;

    return $meses > 12? 12:$meses;
}

In this way, if months is less than 12, you will return the value that corresponds, otherwise it always returns 12.

Here I leave you a sandbox with the test

EDIT

Regarding your comment, it occurs to me that you can do something like this:

first you bring all the users select * from sec_users , then you travel with a for all the users and you apply a function that calculates the available days and then you update the field dias_disponibles of the user.

Here is a pseudocode to make it look better:

$usuarios <- "select * from sec_users"

foreach($usuarios as $usuario){
    $usuario["dias_disponibles"] = calcularDias($usuario["fecha_ingreso"])
    -> update sec_users set dias_disponibles = $usuario["dias_disponibles"] where id_usuario = $usuario["id_usuario"];
}

And the function calcularDias would return the days corresponding to that user, it would be equal to the function difmeses, only multiplying the result by two.

function calcularDias($fechaingreso){
    $fechainicial = new DateTime($fechaingreso);

    $fechaactual = (new DateTime)->format('Y-m-d H:i');
    $fechafinal = new DateTime($fechaactual);
    $diferencia = $fechainicial->diff($fechafinal);

    $meses = ( $diferencia->y * 12 ) + $diferencia->m;

    return ($meses > 12? 12:$meses)*2;
}
    
answered by 14.03.2018 / 16:37
source
0

The most natural solution is to use a cron task in linux, or a task programmed in windows, that attacks a specific url with a php process that makes the change in the database after checking that the conditions are correct.

Another possible solution would be to create a trigger process in mysql programmed every month to perform a select query and an insert based on it.

    
answered by 14.03.2018 в 16:36