【发布时间】:2021-11-30 20:28:33
【问题描述】:
我有一张“预期付款”表格,可以是一次性付款,也可以是每月、每季度或每年定期付款。目标是计算未来 12 个月每个月的预期付款总额。
模型如下:
class ExpectedPayment(models.Model):
id = models.BigIntegerField(primary_key=True)
period = models.BigIntegerField()
date_start = models.DateField(blank=True, null=True)
date_end = models.DateField(blank=True, null=True)
amount = models.DecimalField(max_digits=1000, decimal_places=1000)
期间 1 = 每月重复
期间 2 = 每季度 - 开始日期之后。 (所以从 2 月开始,下个季度是 5 月)
第 3 期 = 每年重复 - 也在开始日期之后
第 4 期 = 一次性付款
如何正确计算每个月的总“金额”,同时考虑何时付款?尤其是季度付款..
我不知道如何合并付款的开始和结束时间,因为它可能在一年中开始,在 3 个月后结束,或者一开始就已经结束。当季度总是相对于它的 start_date 时,我怎么知道季度应该算作预期付款的月份。
单月示例:(当月有效的每月付款)
start_of_current_month = datetime.today().replace(day=1)
current_month_expected_monthly_payments = ExpectedPayment.objects.filter(period=1, date_start__lte=start_of_current_month, date_end__gte=start_of_current_month).aggregate(total=Sum("amount"))
【问题讨论】:
标签: python django django-queryset