def find_item_discount(item): all_discounts = discounts.get_all() all_discounts.sort(key=lambda x: x.timestamp_utc) for d in all_discounts: if d.discount_scope == discounts.DiscountScope.ITEM_ONLY: if item.id() == d.obj_id: item.before_discount = item.price item.price = get_discounted_price(item, d) return True if d.discount_scope == discounts.DiscountScope.CONTAINER_WIDE: if item.container_id == d.obj_id: item.before_discount = item.price item.price = get_discounted_price(item, d) return True
def apply_discounts(order_obj): """ Given an order, updates the order with prevailing discount rules onto the order's debit attribute """ all_dedits = order_obj.debits other_debit = filter(lambda x: x["coll_name"] != discounts.Discount.coll_name(), all_dedits) all_discounts = discounts.get_all() valid_discounts = [] for item_dic in order_obj.items: for d in all_discounts: item_obj = items.get(coerce_bson_id(item_dic["obj_id"])) if item_obj is None: continue if discounts.valid_on_item(d, item_obj): valid_discounts += [{ "obj_id": d._id, "coll_name": discounts.Discount.coll_name(), "amount": discounts.discounted_value(item_obj.price, d), }] break order_obj.debits = other_debit + valid_discounts return valid_discounts