Funzione relativeWorkDate

This commit is contained in:
2025-04-09 12:59:45 +02:00
parent b3f7ab53bc
commit 2b4e98ca80

View File

@@ -0,0 +1,61 @@
package it.integry.ems.migration.model;
import it.integry.ems.migration._base.BaseMigration;
import it.integry.ems.migration._base.MigrationModelInterface;
public class Migration_20250409125908 extends BaseMigration implements MigrationModelInterface {
@Override
public void up() throws Exception {
if (isHistoryDB())
return;
createOrUpdateFunction("relativeWorkDateConsLav", "CREATE FUNCTION [dbo].[relativeWorkDateConsLav] (@StartDate DATE, @DaysToAdd INT)\n" +
"RETURNS DATETIME\n" +
"AS\n" +
"BEGIN\n" +
" DECLARE @CurrentDate DATE = @StartDate;\n" +
" DECLARE @Counter INT = 0;\n" +
" DECLARE @gg INT = 0;\n" +
"\n" +
" IF @DaysToAdd < 0 \n" +
" BEGIN\n" +
" SELECT @gg = -1 -- Per sottrarre giorni\n" +
" SELECT @DaysToAdd = ABS(@DaysToAdd) -- Rendi il numero di giorni positivo\n" +
" END\n" +
" ELSE\n" +
" SELECT @gg = 1 -- Per aggiungere giorni\n" +
"\n" +
" -- Inizia a contare i giorni lavorativi, considerando weekend e festività\n" +
" WHILE @Counter < @DaysToAdd\n" +
" BEGIN\n" +
" SET @CurrentDate = DATEADD(DAY, @gg, @CurrentDate); -- Aggiungi o sottrai un giorno\n" +
"\n" +
" -- Verifica se la data corrente è un sabato, una domenica o una festività\n" +
" IF (DATEPART(WEEKDAY, @CurrentDate) NOT IN (SELECT dayweek.rowId \n" +
" FROM getTableRowByNumber(1, 7) dayweek \n" +
" LEFT OUTER JOIN (SELECT CONVERT(INTEGER, value_string) AS rowid\n" +
" FROM ParseStringIntoArray((SELECT dbo.getGestSetup('DATI_AZIENDA','DOCU_ORD','GG_CONSEGNA')), '|') \n" +
" WHERE LEN(value_string) <> 0) dayLav \n" +
" ON dayweek.rowid = dayLav.rowid\n" +
" WHERE dayLav.rowid IS NULL) OR (SELECT COUNT(*)\n" +
" FROM ParseStringIntoArray((SELECT dbo.getGestSetup('DATI_AZIENDA','DOCU_ORD','GG_CONSEGNA')), '|') \n" +
" WHERE LEN(value_string) <> 0\n" +
" ) = 0)\n" +
" AND @CurrentDate NOT IN (SELECT data FROM jtb_ricorrenze WHERE DATEPART(YEAR, @CurrentDate) = anno) -- Verifica che non sia una festività\n" +
" BEGIN\n" +
" SET @Counter = @Counter + 1; -- Incrementa il contatore se è un giorno lavorativo\n" +
" END\n" +
" END\n" +
"\n" +
" RETURN @CurrentDate; -- Restituisce la data finale\n" +
"END");
}
@Override
public void down() throws Exception {
}
}