Files
stiftung-management-system/app/stiftung/management/commands/fix_advance_payment_dates.py
Jan Remmer Siebels 6aa218004c Implement advance payment schedule for quarterly payments
BREAKING CHANGE: Payment due dates now follow advance payment schedule:
- Q1 payments: Due December 15 of previous year
- Q2 payments: Due March 15 of same year
- Q3 payments: Due June 15 of same year
- Q4 payments: Due September 15 of same year

Changes:
- Updated create_quarterly_support_payment() to use advance schedule
- Enhanced diagnostic commands to check advance payment dates
- Added fix_advance_payment_dates command for comprehensive fixes
- Updated fix_q4_payment_dates to correct Q4 from 31.12 to 15.09

This aligns with semester-based document submissions while maintaining
the advance payment system where payments are made before each quarter.
2025-10-01 12:04:34 +02:00

99 lines
4.3 KiB
Python

from django.core.management.base import BaseCommand
from django.db import transaction
from datetime import date
from stiftung.models import DestinataerUnterstuetzung
class Command(BaseCommand):
help = 'Fix all quarterly payment dates to use advance payment schedule'
def add_arguments(self, parser):
parser.add_argument(
'--dry-run',
action='store_true',
help='Show what would be updated without making changes',
)
parser.add_argument(
'--year',
type=int,
default=2025,
help='Year to fix payments for (default: 2025)',
)
def handle(self, *args, **options):
dry_run = options['dry_run']
year = options['year']
if dry_run:
self.stdout.write(self.style.WARNING('DRY RUN MODE - No changes will be made'))
self.stdout.write(f'Checking quarterly payments for {year} using advance payment schedule...')
# Define correct advance payment schedule
advance_schedule = {
f'Q1/{year}': date(year - 1, 12, 15), # Q1 paid in December of previous year
f'Q2/{year}': date(year, 3, 15), # Q2 paid in March of same year
f'Q3/{year}': date(year, 6, 15), # Q3 paid in June of same year
f'Q4/{year}': date(year, 9, 15), # Q4 paid in September of same year
}
total_fixed = 0
for quarter_desc, correct_date in advance_schedule.items():
# Find payments for this quarter that have wrong dates
wrong_payments = DestinataerUnterstuetzung.objects.filter(
beschreibung__icontains=quarter_desc
).exclude(faellig_am=correct_date)
quarter_count = wrong_payments.count()
if quarter_count > 0:
self.stdout.write(f'\n{quarter_desc}: Found {quarter_count} payments with wrong due dates')
with transaction.atomic():
for payment in wrong_payments:
old_date = payment.faellig_am
if not dry_run:
payment.faellig_am = correct_date
payment.save(update_fields=['faellig_am'])
total_fixed += 1
# Show some examples
if total_fixed <= 10 or options['verbosity'] >= 2:
self.stdout.write(
f' {payment.destinataer.get_full_name()}: '
f'{old_date}{correct_date} ({payment.betrag}€)'
)
else:
self.stdout.write(f'{quarter_desc}: ✓ All payments have correct due date ({correct_date})')
# Summary
if total_fixed == 0:
self.stdout.write(self.style.SUCCESS(f'\n✓ All {year} quarterly payments already have correct advance payment dates!'))
elif dry_run:
self.stdout.write(
self.style.WARNING(f'\nDRY RUN: Would fix {total_fixed} payment due dates')
)
else:
self.stdout.write(
self.style.SUCCESS(f'\n✅ Successfully fixed {total_fixed} payment due dates')
)
# Show the advance payment schedule
self.stdout.write(f'\nAdvance Payment Schedule for {year}:')
for quarter_desc, due_date in advance_schedule.items():
self.stdout.write(f' {quarter_desc}: Due {due_date.strftime("%B %d, %Y")}')
# Check for other years that might need fixing
if year == 2025:
other_year_payments = DestinataerUnterstuetzung.objects.filter(
beschreibung__regex=r'Q[1-4]/202[6-9]'
).exclude(
faellig_am__in=[date(2024, 12, 15), date(2026, 3, 15), date(2026, 6, 15), date(2026, 9, 15)]
).count()
if other_year_payments > 0:
self.stdout.write(f'\n⚠️ Found {other_year_payments} payments for other years that may need fixing')
self.stdout.write(' Run this command with --year 2026, etc. to fix other years')