vedi questo codice: from datetime import date, timedelta, datetime from…
- User
vedi questo codice:
from datetime import date, timedelta, datetime from dateutil.relativedelta import relativedelta from decimal import Decimal, ROUND_DOWN, ROUND_HALF_UP from reportlab.lib.pagesizes import letter from reportlab.lib import colors from reportlab.pdfgen import canvas from reportlab.platypus import Table, TableStyle
class InstallmentPlan: HOLIDAYS = [ (1, 1), # Capodanno (1, 6), # Epifania (4, 25), # Festa della Liberazione (5, 1), # Festa del Lavoro (6, 2), # Festa della Repubblica (8, 15), # Ferragosto (11, 1), # Ognissanti (12, 8), # Immacolata Concezione (12, 25), # Natale (12, 26) # Santo Stefano ]
def __init__(self, total_debt, communication_date, processing_date, is_electronic, num_installments): self.total_debt = Decimal(str(total_debt)).quantize(Decimal('0.01')) self.communication_date = communication_date self.processing_date = processing_date self.is_electronic = is_electronic self.num_installments = min(num_installments, 20) self.installments = [] self.interest_start_date = self._calculate_interest_start_date() self._generate_payment_schedule() def _is_holiday(self, d): return d.weekday() >= 5 or (d.month, d.day) in self.HOLIDAYS or d == self._calculate_easter(d.year) def _next_working_day(self, d): while self._is_holiday(d): d += timedelta(days=1) return d def _calculate_easter(self, year): a = year % 19 b = year // 100 c = year % 100 d = b // 4 e = b % 4 f = (b + 8) // 25 g = (b - f + 1) // 3 h = (19 * a + b - d - g + 15) % 30 i = c // 4 k = c % 4 l = (32 + 2 * e + 2 * i - h - k) % 7 m = (a + 11 * h + 22 * l) // 451 month = (h + l - 7 * m + 114) // 31 day = ((h + l - 7 * m + 114) % 31) + 1 return date(year, month, day) def _calculate_interest_start_date(self): start_date = self.processing_date + relativedelta(months=2) start_date = start_date.replace(day=1) return self._next_working_day(start_date) def _calculate_first_due_date(self): base_days = 30 suspension_start = date(self.communication_date.year, 8, 1) suspension_end = date(self.communication_date.year, 9, 5) if self.communication_date < suspension_start: temp_due_date = self.communication_date + timedelta(days=base_days) if temp_due_date < suspension_start: raw_due_date = temp_due_date else: days_before_suspension = (suspension_start - self.communication_date).days remaining_days = base_days - days_before_suspension raw_due_date = suspension_end + timedelta(days=remaining_days) elif self.communication_date < suspension_end: raw_due_date = suspension_end + timedelta(days=base_days) else: raw_due_date = self.communication_date + timedelta(days=base_days) adjusted_due_date = self._next_working_day(raw_due_date) return raw_due_date, adjusted_due_date def _generate_payment_schedule(self): if self.num_installments == 0: return rata_base = (self.total_debt / self.num_installments).quantize(Decimal('0.01'), ROUND_DOWN) totale_ripartito = rata_base * self.num_installments differenza_totale = self.total_debt - totale_ripartito raw_first, adjusted_first = self._calculate_first_due_date() self.installments.append({ 'raw_due_date': raw_first, 'due_date': adjusted_first, 'principal': rata_base + differenza_totale, 'interest': Decimal('0') }) raw_date = raw_first for _ in range(self.num_installments - 1): raw_date = (raw_date + relativedelta(months=3)).replace(day=1) + relativedelta(months=1) - timedelta(days=1) adjusted_date = self._next_working_day(raw_date) self.installments.append({ 'raw_due_date': raw_date, 'due_date': adjusted_date, 'principal': rata_base, 'interest': None }) def calculate_interest(self, installment_num): if installment_num == 0 or installment_num >= len(self.installments): return Decimal('0') rata = self.installments[installment_num] capitale = rata['principal'] interest_start = self.interest_start_date due_date = rata['raw_due_date'] giorni = (due_date - interest_start).days + 1 return (capitale * Decimal('0.035') * Decimal(giorni) / Decimal('365')).quantize(Decimal('0.01'), ROUND_HALF_UP) def get_payment_schedule(self): schedule = [] total_days = 0 total_interest = Decimal('0') for i, rata in enumerate(self.installments): interest = self.calculate_interest(i) if i > 0 else Decimal('0') total_interest += interest total_days += (rata['raw_due_date'] - self.interest_start_date).days + 1 schedule.append({ 'raw_due_date': rata['raw_due_date'], 'due_date': rata['due_date'], 'principal': rata['principal'], 'interest': interest }) daily_interest = total_interest / total_days if total_days > 0 else Decimal('0') return schedule, total_days, daily_interest def get_annual_interest_summary(self): summary = {} schedule, _, _ = self.get_payment_schedule() for rata in schedule: year = rata['raw_due_date'].year if year not in summary: summary[year] = { 'interest': Decimal('0'), 'start_date': rata['raw_due_date'], 'end_date': rata['raw_due_date'], 'days_count': 0 # Aggiunta per il conteggio dei giorni } summary[year]['interest'] += rata['interest'] summary[year]['end_date'] = rata['raw_due_date'] # Aggiorna sempre la data di fine return summary - Assistant
- User
di questo
Calcolare il numero totale di giorni tra la prima e l'ultima scadenza
total_days = (last_due_date - first_due_date).days + 1 daily_interest = Decimal(total_interest / total_days) daily_interest = Decimal(total_interest / total_days) dobbiamo prendere 8 cifre dopo la virgola.Calcolo del Rateo/Risconto
current_interest = annual_summary[year]['interest'] if index == 0: # Primo anno if current_interest < interest_for_period: rateo_riscatto = interest_for_period - current_interest # Rateo else: rateo_riscatto = current_interest - interest_for_period # Risconto else: # Anni successivi rateo_riscatto = previous_rateo_riscatto + current_interest - interest_for_period previous_rateo_riscatto = rateo_riscatto # Aggiorna per uso successivo # Imposta il segno per rateo/risconto if rateo_riscatto < 0: rateo_riscatto = abs(rateo_riscatto) # Risconto else: rateo_riscatto = -rateo_riscatto # Rateo annual_data.append([ year, f"€{self.format_number(current_interest)}", start_date.strftime('%d/%m/%Y'), end_date.strftime('%d/%m/%Y'), days_count, f"€{self.format_number(interest_for_period)}", f"€{self.format_number(rateo_riscatto)}" # Rateo/Risconto ]) - Assistant
- User
gli arrotondamenti non vanno:
Anno Int. da piano Data inizio Data fine Giorni Quota int. annualeRateo/Risconto 2023 €64,65000000 08/01/2023 31/12/2023 358 €420,14497298 €-355,49000000 2024 €238,81000000 01/01/2024 31/12/2024 366 €429,53368746 €190,72000000 2025 €412,76000000 01/01/2025 31/12/2025 365 €428,36009815 €206,32000000 2026 €586,57000000 01/01/2026 31/12/2026 365 €428,36009815 €48,11000000 2027 €760,38000000 01/01/2027 31/10/2027 304 €356,77115024 €-355,49000000
- Assistant