"""
Total price for the given quantity, applying any pack rule from
PACK_PRICING. Pass unit_price (e.g. CartItem's locked-in price)
- to override self.price.
+ to override self.price. Each full multiple of pack_size triggers
+ a new pack, remaining units use extra_unit_price.
"""
if unit_price is None:
unit_price = self.price or 0
cast = type(unit_price)
pack_price = cast(rule['pack_price'])
extra_price = cast(rule['extra_unit_price'])
- return pack_price + (quantity - rule['pack_size']) * extra_price
+ packs, extras = divmod(quantity, rule['pack_size'])
+ return packs * pack_price + extras * extra_price
def get_invoice_lines(self, unit_price, quantity, description):
"""
Yield invoice line dicts for this conference at the given quantity,
splitting into a pack line + extras line when a pack rule applies.
+ Number of packs = quantity // pack_size.
"""
rule = self.pack_pricing
if not rule or quantity < rule['pack_size']:
yield {'designation': description, 'VAT': 0, 'qte': quantity, 'PUHT': unit_price}
return
cast = type(unit_price)
+ packs, extras = divmod(quantity, rule['pack_size'])
yield {
'designation': "Pack de %d - %s" % (rule['pack_size'], description),
'VAT': 0,
- 'qte': 1,
+ 'qte': packs,
'PUHT': cast(rule['pack_price']),
}
- extra = quantity - rule['pack_size']
- if extra > 0:
+ if extras > 0:
yield {
'designation': description,
'VAT': 0,
- 'qte': extra,
+ 'qte': extras,
'PUHT': cast(rule['extra_unit_price']),
}