Torna al Thread
ALTER PROCEDURE [dbo].[Imposta_Soggiorno]
@codalbergo as Int,
@start as smalldatetime,
@end as smalldatetime
AS
BEGIN
select
alloggiati.codice_albergo,
alberghi.descrizione,
categorie.descrizione as categoria,
--alloggiati.data_arrivo,
--alloggiati.data_partenza,
case when alloggiati.comune_residenza_codice > 0 then 'ITA' else 'STR' End as TipoMovimento,
imposta.tipo_esenti,
imposta.desc_esenti,
imposta.imposta_ita,
imposta.imposta_str,
Sum(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),alloggiati.data_partenza,112)<@end then alloggiati.data_partenza
else @end end)=0
then 1 else
(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end)>imposta.giorni_imposta then imposta.giorni_imposta else
DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end) END) end)as Presenze,
case when alloggiati.comune_residenza_codice > 0 then
Sum(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),alloggiati.data_partenza,112)<@end then alloggiati.data_partenza
else @end end)=0
then 1 else
(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end)>imposta.giorni_imposta then imposta.giorni_imposta else
DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end) END) end)*imposta.imposta_ita
else
Sum(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),alloggiati.data_partenza,112)<@end then alloggiati.data_partenza
else @end end)=0
then 1 else
(case when DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end)>imposta.giorni_imposta then imposta.giorni_imposta else
DATEDIFF("dd", alloggiati.data_arrivo, case when convert(varchar(8),(case when alloggiati.data_partenza='1900-01-01' then NULL else alloggiati.data_partenza END),112)<@end then alloggiati.data_partenza
else @end end) END) end)*imposta.imposta_str
End
as Imposta
from tab_alloggiati as alloggiati
join
tab_alberghi as alberghi
On alloggiati.codice_albergo=alberghi.codice
join
tab_categorie as categorie
On alberghi.categoria_codice=categorie.codice
join
tab_imposta_soggiorno as imposta
On alberghi.comune_codice=imposta.idcomune and alloggiati.esente_imposta=imposta.tipo_esenti and categorie.codice_istat=imposta.codcategoria
Where alloggiati.codice_albergo=@codalbergo and alloggiati.data_arrivo between @start and @end
group by
alloggiati.codice_albergo,
alberghi.descrizione,
categorie.descrizione,
--alloggiati.data_arrivo,
--alloggiati.data_partenza,
alloggiati.comune_residenza_codice,
imposta.tipo_esenti,
imposta.desc_esenti,
imposta.imposta_ita,
imposta.imposta_str
order by imposta.tipo_esenti,tipomovimento
END