django-afip
django-afip copied to clipboard
Added discount field and _validate_discount method to ReceiptEntry.
Added discount field for a receipt entry, that is applied to tax base. e.g:
quantity = 2
unit_price = 2
discount = 1
# tax base or total before discount
total = quantity * unit_price
# total = 4 -> 2 * 2
# discount applied to total
total_price = quantity * unit_price - discount
# total_price = 3 -> 2 * 2 - 1
discount is being validated before saving the model due to that negative or greater than total before discount values cannot be assign to this entry, see: https://github.com/WhyNotHugo/django-afip/blob/aebb5cc45a97143628d9581da53aa84df6b3e2fe/django_afip/models.py#L1512-L1534
Also some tests cases were added, see: https://github.com/WhyNotHugo/django-afip/blob/aebb5cc45a97143628d9581da53aa84df6b3e2fe/tests/test_models.py#L348-L389
closes: #133
Sorry for the slowness looking at this. I need to double-check a few details with respect to how AFIP usually models discounts. A bit packed this week, I hope I'll have to to look at this near the end of the week.
HI @WhyNotHugo don't worry about that, I understand that this is better to get it right.
To add some hints of what I did, my understanding is that with wsfev1 AFIP does not models any kind of discount per invoice, what AFIP really cares is your balance (purchases vs sales). I search for it in the wsfev1 developers manual and I did not found any reference to discount, what I do know is that if you must send invoices via wsmtxca service you have to specify every detail even discounts.
I hope this add some clarification and I hope that I'm not too wrong in this.
The reason for making discount direct to quantity * unit_price was that I thought that (example with discounts as percentage here, not as net discount as in the model):
AFIP required some percentage of our net_taxed.
Let's say that:
net_taxed = total_per_entry = quantity * unit_price = $83.80 --> then if VAT is 21% we get that
net_taxed + 21% VAT($17.6) = $101.4
if we make a discount of 10% to our entry we get that total_per_entry = $75.42 --> so, now our
net_taxed = $75.42
net_taxed + 21%VAT($15.84) = $91.26
that is the same of making the discount to the total of our net_taxed + VAT
net_taxed + 21% VAT - discount = $101.4 - 10% = $91.26
As we are getting the total VAT calculation latter, we only take into account net_taxed = quantity * unit_price - discount
Yes! it makes much more sense the way you put it, using DB constraint seems much better. I'll test everything, commit this changes and update the PR.
PD: Sorry for the typos, already fixed them.