using Khaemhat.Core.Tax;
using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy;
///
/// Italian VAT deadlines (wiki Fiscalità italiana, "Scadenze e regole principali"):
///
/// - monthly VAT by the 16th of the next month;
/// - quarterly VAT by the 16th of the second month after the quarter, with 1% interest; the fourth quarter is paid
/// with the annual balance by 16 March;
/// - payments due from 1 to 20 August move to 20 August (art. 3-quater DL 16/2012);
/// - a deadline on a Saturday, Sunday or public holiday moves to the next working day (art. 7 DL 70/2011).
///
///
public sealed class ItalianFiscalCalendar : IFiscalCalendar
{
/// Interest on quarterly VAT, in percent.
public const decimal QuarterlyInterestRate = 1m;
///
public DateOnly VatPaymentDue(TaxPeriod period)
{
ArgumentNullException.ThrowIfNull(period);
DateOnly due;
if (period.Periodicity == VatPeriodicity.Monthly)
{
due = new DateOnly(period.Year, period.Number, 16).AddMonths(1);
}
else if (period.Number == 4)
{
due = new DateOnly(period.Year + 1, 3, 16);
}
else
{
due = new DateOnly(period.End.Year, period.End.Month, 16).AddMonths(2);
}
if (due.Month == 8 && due.Day <= 20)
{
due = new DateOnly(due.Year, 8, 20);
}
return NextWorkingDay(due);
}
///
public decimal VatInterestRate(VatPeriodicity periodicity) =>
periodicity == VatPeriodicity.Quarterly ? QuarterlyInterestRate : 0m;
///
public bool IsNonWorkingDay(DateOnly day) =>
day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || IsPublicHoliday(day);
///
public DateOnly NextWorkingDay(DateOnly day)
{
while (IsNonWorkingDay(day))
{
day = day.AddDays(1);
}
return day;
}
/// Whether a day is an Italian national public holiday.
/// The day.
/// for a holiday.
public static bool IsPublicHoliday(DateOnly date)
{
switch ((date.Month, date.Day))
{
case (1, 1): // Capodanno
case (1, 6): // Epifania
case (4, 25): // Liberazione
case (5, 1): // Festa del lavoro
case (6, 2): // Festa della Repubblica
case (8, 15): // Ferragosto
case (11, 1): // Ognissanti
case (12, 8): // Immacolata
case (12, 25): // Natale
case (12, 26): // Santo Stefano
return true;
case (10, 4) when date.Year >= 2026: // San Francesco, national holiday again from 2026
return true;
default:
return date == EasterSunday(date.Year).AddDays(1); // Lunedì dell'Angelo
}
}
/// Easter Sunday of a year (Gregorian calendar, anonymous algorithm).
/// The year.
/// The date.
public static DateOnly EasterSunday(int year)
{
var a = year % 19;
var b = year / 100;
var c = year % 100;
var d = b / 4;
var e = b % 4;
var f = (b + 8) / 25;
var g = (b - f + 1) / 3;
var h = ((19 * a) + b - d - g + 15) % 30;
var i = c / 4;
var k = c % 4;
var l = (32 + (2 * e) + (2 * i) - h - k) % 7;
var m = (a + (11 * h) + (22 * l)) / 451;
var month = (h + l - (7 * m) + 114) / 31;
var day = ((h + l - (7 * m) + 114) % 31) + 1;
return new DateOnly(year, month, day);
}
}