I have the following 3 models.
The “amount_to_plan” method for Business works fine while the “invoice_amountVATe” does not.
I do not understand because it seems to me they are really equivalent (the first “binding” Business and “Invoice” and the latter binding “Invoice” with “InvoiceLign”
What could be wrong?
class Business(models.Model):
name = models.CharField('Affaire',max_length=200, null = True, blank=True)
customer = models.ForeignKey(Customer,on_delete=models.CASCADE,verbose_name='Client',
null = True,blank = True)
amount = models.DecimalField('Montant',max_digits=9, decimal_places=2, null=True,blank=True)
def amount_to_plan(self):
invoice = self.invoice_set.all()
a = self.amount
for i in invoice:
a -= i.amount
return a
def get_absolute_url(self):
return reverse('customer_detail_form', kwarg={'pk':self.pk})
class Meta:
ordering = ['ponderation','amount']
verbose_name = 'Affaire'
class Invoice(models.Model):
number = models.CharField('Numéro de facture', max_length=10, null=True, blank=True)
issuance_date = models.DateField('Date de facture', default=datetime.date.today)
amount = models.DecimalField('Montant',max_digits=11, decimal_places=2, default=0)
business = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name='Commande')
def invoice_amountVATe(self):
invoice_ligns = self.invoiceLign_set.all()
invoice_amountVATe = 0
for i in invoice_ligns:
invoice_amountVATe += i.amountVATe()
return invoice_amountVATe
class Meta:
ordering = ['issuance_date']
verbose_name = 'Facture'
def __str__(self):
return self.label
def get_absolute_url(self):
return reverse('invoice_detail', args=[str(self.id)])
class InvoiceLign(models.Model):
invoice = models.ForeignKey(Invoice, on_delete= models.CASCADE)
label = models.CharField('Désignation', max_length=200)
unit_price = models.DecimalField('Prix unitaire',max_digits=9, decimal_places=2)
quantity = models.IntegerField('Quantité')
VAT_rate = models.DecimalField('Taux de TVA', max_digits=5, decimal_places=2, default=20)
def amountVATe(self):
# Montant HT de la ligne
return self.quantity*self.unit_price
def VAT_amount(self):
# Montant de TVA de la ligne
return self.amountVATe*self.VAT_rate/100
def amountVATi(self):
# Montant TTC de la ligne
return self.amountVATe + self.VAT_amount
class Meta:
ordering = ['label']
verbose_name = 'Ligne de facture'
def __str__(self):
return self.label