如何根据Django中一个模型的计算重新计算另一个模型中的字段



我的项目中有三个模型,它们是:

class Package(models.Model):
patient=models.ForeignKey(Patient, on_delete=CASCADE)
diagnosis=models.ForeignKey(Diagnosis, on_delete=CASCADE)
treatment=models.ForeignKey(Treatment, on_delete=CASCADE)
patient_type=models.ForeignKey(PatientType, on_delete=CASCADE)
date_of_admission=models.DateField(default=None)
max_fractions=models.IntegerField(default=None)
total_package=models.DecimalField(max_digits=10, decimal_places=2)
class Receivables(models.Model):
patient=models.ForeignKey(Patient, on_delete=CASCADE)
pattern = RegexValidator(r'(RT|rt|rT|Rt)/[0-9]{4}/[0-9]{2}/[0-9]{4}', 'Enter RT Number properly!')
rt_number=models.CharField(max_length=15, validators=[pattern])
discount=models.DecimalField(max_digits=9, decimal_places=2, default=0)
approved_package=models.DecimalField(max_digits=10, decimal_places=2, default=0)
proposed_fractions=models.IntegerField()
done_fractions=models.IntegerField()
base_value=models.DecimalField(max_digits=10, decimal_places=2, blank=True)
expected_value=models.DecimalField(max_digits=10, decimal_places=2, blank=True)
@property
def package(self):
try:
return Package.objects.filter(patient=self.patient).order_by('-id').first()
except Package.DoesNotExist:
return None
def calculate_approved_discount(self):
if(self.package):
if self.package.patient_type=='CASH':
self.approved_package = self.package.total_package - self.discount
else:
pass
def calculate_base_value(self):
if (self.package):
process_charge=self.package.total_package*15/100
net_total_package=self.package.total_package - process_charge
amount_per_fraction=net_total_package/self.package.max_fractions
net_cost=amount_per_fraction*self.done_fractions
gross_cost=net_cost+process_charge
self.base_value=gross_cost
else:
self.base_value=self.approved_package
def calculate_expected_value(self): #Edited
if not self.expected_value < 0:
if (self.package):
if self.base_value<self.approved_package:
self.expected_value=self.base_value
else:
self.expected_value=self.approved_package
else:
self.expected_value=0
else:
return abs(self.expected_value)
def save(self, *args, **kwargs):
self.calculate_approved_discount()
self.calculate_base_value()
self.calculate_expected_value()
super(Receivables, self).save(*args, **kwargs)
class Realization(models.Model):
patient=models.ForeignKey(Patient, on_delete=CASCADE)
cash=models.BooleanField(default=False)
amount_received=models.DecimalField(max_digits=10, decimal_places=2, default=0)
billing_month=models.DateField(default=None)
deficit_or_surplus_amount=models.DecimalField(max_digits=8, decimal_places=2, blank=True)
deficit_percentage=models.FloatField(blank=True, default=0)
surplus_percentage=models.FloatField(blank=True, default=0)
remarks=models.TextField(max_length=500, default="N/A")
@property
def receivables(self):
try:
return Receivables.objects.filter(patient=self.patient).order_by('-id').first()
except Receivables.DoesNotExist:
return None
@property
def discharge(self):
try:
return Discharge.objects.filter(patient=self.patient).order_by('-id').first()
except Discharge.DoesNotExist:
return None
@property
def package(self):
try:
return Package.objects.filter(patient=self.patient).order_by('-id').first()
except Package.DoesNotExist:
return None

def calculate_deficit_surplus_amount(self):
if (self.receivables):
print(self.receivables)
self.deficit_or_surplus_amount=self.amount_received-self.receivables.expected_value
else:
self.deficit_or_surplus_amount=0
def calculate_deficit_or_surplus_percentage(self):
if (self.receivables):
if self.amount_received>self.receivables.expected_value:
self.surplus_percentage=self.deficit_or_surplus_amount/self.receivables.expected_value*100
self.deficit_percentage=0
elif self.amount_received<self.receivables.expected_value:
self.deficit_percentage=self.deficit_or_surplus_amount/self.receivables.expected_value*100
self.surplus_percentage=0
elif self.amount_received==self.receivables.expected_value:
self.deficit_percentage=0
self.surplus_percentage=0
else:
self.surplus_percentage=0
self.deficit_percentage=0
def save(self, *args, **kwargs):
self.calculate_deficit_surplus_amount()
self.calculate_deficit_or_surplus_percentage()
super(Realization, self).save(*args, **kwargs)

编辑:

现在,我想要的是,当患者支付金额时,如果存在赤字(包含在deficit_or_surplus_amount中),我希望将赤字金额保存在应收账款的预期值中,这样,当我们收回金额时,当我们收到赤字金额时,进一步的计算将基于该预期值。

我不想删除以前的实例,而是创建一个新的应收账款实例,该实例具有新的预期值,现在包含上次支付的金额。因此,我们希望患者能提供这样的金额。因此,当病人下次付款时,赤字或盈余将根据他这次的预期付款进行计算。

例如:

如果病人入院并有政府计划为他支付费用。假设他的总期望值是75000。他出院了,我们向政府索赔。当我们收到金额时,我们得到的金额更少,比方说我们只收到了70000。因此,5000赤字。我们收回拒付的款项。现在,我们预计将从政府收到5000美元,而不是75000美元。因此,如果我们从该计划中收到的金额不是5000,而是只有2500,那么变现表的方法应该能够根据现在的预期值计算赤字或盈余,即5000。

我希望我的问题现在已经清楚了。如果你还想进一步解释,请告诉我。

我试着用一个信号来达到这个目的:

@receiver(post_save, sender=Realization)
def new_receivable_save(sender, instance, created, **kwargs):
pkg=Package.objects.filter(patient=instance.patient).order_by('-id').first()
rec=Receivables.objects.filter(patient=instance.patient).order_by('-id').first()
if created:
if pkg.patient_type!='CASH':
if instance.deficit_or_surplus_amount<0:
Receivables(
patient=rec.patient, 
rt_number=rec.rt_number, 
discount=rec.discount, 
approved_package=rec.approved_package, 
proposed_fractions=rec.proposed_fractions, 
done_fractions=rec.done_fractions, 
base_value=rec.base_value, 
expected_value=abs(instance.deficit_or_surplus_amount)
).save()

此函数确实创建了Receivables的新实例,但使用了以前的expected_value,而不是重新计算的实例。

我哪里错了?我该怎么做?提前谢谢。

您每次都正确保存deficit金额。

但是您根本没有保存/更新expected_value

第一次从base_value保存expected_value时,这是正确的。

下次收到的金额必须更新expected_value(必须是50002500)。

  • 75000base_value、75000expected_value
  • 70000amount_received-5000deficit-5000expected_value
  • 2500amount_received-2500deficit-2500expected_value
  • 1300amount_received-1200deficit-1200expected_value

这里是amount_received-以前的expected_value(70000-75000=-5000)。你的做法是消极的。那很好。

Receivables模型有save(),其中包含calculate_expected_value

def calculate_expected_value(self):
if (self.package):
if self.base_value<self.approved_package:
self.expected_value=self.base_value
else:
self.expected_value=self.approved_package
else:
self.expected_value=0

此方法将覆盖此模型中的expected_value。在初始化Receivables(....).save()对象时,它不采用传递的期望值。

尝试修改上面的方法如下:

def calculate_expected_value(self):
if not self.expected_value < 0: # try something here, if expected value exists already while saving, we don't reassign it below.        
if (self.package):
if self.base_value<self.approved_package:
self.expected_value=self.base_value
else:
self.expected_value=self.approved_package
else:
self.expected_value=0

这就是你可能出错的地方。如果我错了,请纠正我。

编辑:

def calculate_expected_value(self): #Edited
if not self.expected_value < 0:
if (self.package):
if self.base_value<self.approved_package:
self.expected_value=self.base_value
else:
self.expected_value=self.approved_package
else:
self.expected_value=0
else:
exp_value = self.expected_value * -1
return exp_value

相关内容

  • 没有找到相关文章

最新更新