56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
"""初始化完整财务数据:科目 + 期间。"""
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from datetime import date
|
|
|
|
from apps.core.models import Tenant
|
|
from apps.finance import services as fs
|
|
from apps.finance.models import Period
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "初始化完整财务:会计科目 + 当月会计期间(往前往后各 3 个月)"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--back", type=int, default=3, help="往前几个月"
|
|
)
|
|
parser.add_argument(
|
|
"--forward", type=int, default=3, help="往后几个月"
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
back = options["back"]
|
|
forward = options["forward"]
|
|
|
|
for tenant in Tenant.objects.filter(is_active=True):
|
|
# 会计科目
|
|
n_acc = fs.init_chart_of_accounts(tenant)
|
|
self.stdout.write(f"[{tenant.code}] 科目 +{n_acc} 条")
|
|
|
|
# 期间:往前往后
|
|
today = date.today()
|
|
cur = today
|
|
for _ in range(back):
|
|
cur = _prev_month(cur)
|
|
n_p = fs.ensure_periods(tenant, cur, today)
|
|
cur = today
|
|
for _ in range(forward):
|
|
cur = _next_month(cur)
|
|
n_p += fs.ensure_periods(tenant, today, cur)
|
|
self.stdout.write(f"[{tenant.code}] 期间新增 +{n_p} 个({today} ± {back}/{forward} 月)")
|
|
|
|
self.stdout.write(self.style.SUCCESS("\n完整财务数据初始化完成"))
|
|
|
|
|
|
def _prev_month(d):
|
|
if d.month == 1:
|
|
return d.replace(year=d.year - 1, month=12, day=1)
|
|
return d.replace(month=d.month - 1, day=1)
|
|
|
|
|
|
def _next_month(d):
|
|
if d.month == 12:
|
|
return d.replace(year=d.year + 1, month=1, day=1)
|
|
return d.replace(month=d.month + 1, day=1)
|