Skip to content

Commit

Permalink
change discount mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
GijsHighbiza committed Mar 12, 2024
1 parent d8b6270 commit dba2c09
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions oscar_odin/mappings/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ def category(self, value: str):
"""Map category."""
return resources.order.DiscountCategory(value)

@odin.map_field(from_field="category")
def is_basket_discount(self, category) -> bool:
@odin.assign_field
def is_basket_discount(self) -> bool:
"""map is basket discount function"""
return category == "Basket"
return self.source.is_basket_discount

@odin.map_field(from_field="category")
def is_shipping_discount(self, category) -> bool:
@odin.assign_field
def is_shipping_discount(self) -> bool:
"""map is shipping discount function"""
return category == "Shipping"
return self.source.is_shipping_discount

@odin.map_field(from_field="category")
def is_post_order_action(self, category) -> bool:
@odin.assign_field
def is_post_order_action(self) -> bool:
"""map is post order action function"""
return category == "Deferred"
return self.source.is_post_order_action

@odin.assign_field
def description(self) -> str:
Expand All @@ -91,12 +91,12 @@ def discount_lines(self):

@odin.assign_field(to_list=True)
def discount_lines_per_tax_code(self):
"""get the total of all lines for each tax code"""
"""get the total discount of all lines for each tax code"""
discount_lines = []

for tax_code in self.source.discount_lines.values_list(
"line__tax_code", flat=True
).distinct():
for tax_code in sorted(
set(self.source.discount_lines.values_list("line__tax_code", flat=True))
):
amount = self.source.discount_lines.filter(
line__tax_code=tax_code
).aggregate(amount=Coalesce(Sum("amount"), Decimal(0)))["amount"]
Expand All @@ -105,15 +105,7 @@ def discount_lines_per_tax_code(self):
DiscountPerTaxCodeResource(amount=amount, tax_code=tax_code)
)

# remove items with duplicate tax codes
tax_codes = []
discount_lines_per_tax_code = []
for line in discount_lines:
if line.tax_code not in tax_codes:
discount_lines_per_tax_code.append(line)
tax_codes.append(line.tax_code)

return discount_lines_per_tax_code
return discount_lines


class ShippingEventToResource(OscarBaseMapping):
Expand Down

0 comments on commit dba2c09

Please sign in to comment.