Skip to content

Commit

Permalink
fix: discount
Browse files Browse the repository at this point in the history
Fix the discount sent to SageX3
related to #257
  • Loading branch information
igobranco committed Apr 1, 2024
1 parent d286ffa commit d93aed3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apps/billing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def discount_rate(self):
The discount rate
"""
try:
round(self.discount_incl_tax / (self.unit_price_incl_vat + self.discount_incl_tax), 2)
return round(self.discount_incl_tax / (self.unit_price_incl_vat + self.discount_incl_tax), 2)
except ZeroDivisionError:
return 0

Expand Down
2 changes: 1 addition & 1 deletion apps/billing/services/processor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __generate_items_as_xml(self, items: list[TransactionItem]) -> str:
<FLD NAME="QTY">{item.quantity}</FLD>
<FLD NAME="STU">UN</FLD>
<FLD NAME="GROPRI">{item.unit_price_excl_vat}</FLD>
<FLD NAME="DISCRGVAL1">{item.discount_rate}</FLD>
<FLD NAME="DISCRGVAL1">{item.discount_rate * 100}</FLD>
<FLD NAME="VACITM1">{self.__vacitm1}</FLD>
</LIN>
"""
Expand Down
46 changes: 46 additions & 0 deletions apps/billing/tests/test_discount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import decimal
import logging

from django.test import TestCase

from apps.billing.factories import TransactionFactory, TransactionItemFactory

log = logging.getLogger(__name__)


class DiscountTest(TestCase):
"""
Test the discount
"""

def test_discount_transaction_no_all_zero(self):
"""
Test the discount model with discount with zero.
"""
transaction = TransactionFactory.build()
item = TransactionItemFactory.build(
transaction=transaction, discount_excl_tax=decimal.Decimal(0.00), discount_incl_tax=decimal.Decimal(0.00)
)
print(item.discount_incl_tax)
print(item.unit_price_incl_vat)
self.assertEqual(item.discount_rate, 0.00)

def test_discount_transaction_some_discount(self):
"""
Test discount value with including vat fields.
"""
transaction = TransactionFactory.build()
item = TransactionItemFactory.build(
transaction=transaction, unit_price_incl_vat=decimal.Decimal(9.00), discount_incl_tax=decimal.Decimal(1.00)
)
self.assertEqual(item.discount_rate, round(decimal.Decimal(0.10), 2))

def test_discount_transaction_some_discount_percentage(self):
"""
Test discount value like a percentage.
"""
transaction = TransactionFactory.build()
item = TransactionItemFactory.build(
transaction=transaction, unit_price_incl_vat=decimal.Decimal(4.00), discount_incl_tax=decimal.Decimal(1.00)
)
self.assertEqual(item.discount_rate * 100, 20)

0 comments on commit d93aed3

Please sign in to comment.