def get_qty_price(self, qty, show_trial=True, include_discount=True): """ If QTY_DISCOUNT prices are specified, then return the appropriate discount price for the specified qty. Otherwise, return the unit_price returns price as a Decimal Note: If a subscription has a trial, then we'll return the first trial price, otherwise the checkout won't balance and it will look like there are items to be paid on the order. """ if show_trial: trial = self.get_trial_terms(0) else: trial = None if trial: price = trial.price * qty else: if include_discount: price = get_product_quantity_price(self.product, qty) else: adjustment = get_product_quantity_adjustments(self, qty) if adjustment.price is not None: price = adjustment.price.price else: price = None if not price and qty == Decimal('1'): # Prevent a recursive loop. price = Decimal("0.00") elif not price: price = self.product._get_fullPrice() return price
def get_qty_price(self, qty, include_discount=True): """ If QTY_DISCOUNT prices are specified, then return the appropriate discount price for the specified qty. Otherwise, return the unit_price returns price as a Decimal """ if include_discount: price = get_product_quantity_price(self.product, qty) else: adjustment = get_product_quantity_adjustments(self, qty) if adjustment.price is not None: price = adjustment.price.price else: price = None if not price and qty == Decimal('1'): # Prevent a recursive loop. price = Decimal("0.00") elif not price: price = self.product._get_fullPrice() return price * self.downpayment / 100
def get_qty_price(self, qty, include_discount=True): """ If QTY_DISCOUNT prices are specified, then return the appropriate discount price for the specified qty. Otherwise, return the unit_price returns price as a Decimal """ if include_discount: price = get_product_quantity_price(self.product, qty) else: adjustment = get_product_quantity_adjustments(self.product, qty) if adjustment.price is not None: price = adjustment.price.price else: price = None if not price and qty == Decimal('1'): # Prevent a recursive loop. price = Decimal("0.00") elif not price: price = self.product._get_fullPrice() return price * self.downpayment / 100
def force_recalculate_total(self, save=True): """Calculates sub_total, taxes and total.""" zero = Decimal("0.0000000000") total_discount = Decimal("0.0000000000") discount = Discount.objects.by_code(self.discount_code) discount.calc(self) discounts = discount.item_discounts itemprices = [] fullprices = [] for lineitem in self.orderitem_set.all(): lid = lineitem.id if lid in discounts: lineitem.discount = discounts[lid] else: lineitem.discount = zero # now double check against other discounts, such as tiered discounts adjustment = get_product_quantity_adjustments(lineitem.product, qty=lineitem.quantity) if adjustment and adjustment.price: baseprice = adjustment.price.price finalprice = adjustment.final_price() #We need to add in any OrderItemDetail price adjustments before we do anything else baseprice += lineitem.get_detail_price() finalprice += lineitem.get_detail_price() if baseprice > finalprice or baseprice != lineitem.unit_price: unitdiscount = (lineitem.discount/lineitem.quantity) + baseprice-finalprice unitdiscount = trunc_decimal(unitdiscount, 2) linediscount = unitdiscount * lineitem.quantity total_discount += linediscount fullydiscounted = (baseprice - unitdiscount) * lineitem.quantity lineitem.unit_price = baseprice lineitem.discount = linediscount lineitem.line_item_price = baseprice * lineitem.quantity log.debug('Adjusting lineitem unit price for %s. Full price=%s, discount=%s. Final price for qty %d is %s', lineitem.product.slug, baseprice, unitdiscount, lineitem.quantity, fullydiscounted) if save: lineitem.save() itemprices.append(lineitem.sub_total) fullprices.append(lineitem.line_item_price) shipprice = Price() shipprice.price = self.shipping_cost shipadjust = PriceAdjustmentCalc(shipprice) if 'Shipping' in discounts: shipadjust += PriceAdjustment('discount', _('Discount'), discounts['Shipping']) signals.satchmo_shipping_price_query.send(self, adjustment=shipadjust) shipdiscount = shipadjust.total_adjustment() self.shipping_discount = shipdiscount total_discount += shipdiscount self.discount = total_discount if itemprices: item_sub_total = reduce(operator.add, itemprices) else: item_sub_total = zero if fullprices: full_sub_total = reduce(operator.add, fullprices) else: full_sub_total = zero self.sub_total = full_sub_total taxProcessor = get_tax_processor(self) totaltax, taxrates = taxProcessor.process() self.tax = totaltax # clear old taxes for taxdetl in self.taxes.all(): taxdetl.delete() for taxdesc, taxamt in taxrates.items(): taxdetl = OrderTaxDetail(order=self, tax=taxamt, description=taxdesc, method=taxProcessor.method) taxdetl.save() log.debug("Order #%i, recalc: sub_total=%s, shipping=%s, discount=%s, tax=%s", self.id, moneyfmt(item_sub_total), moneyfmt(self.shipping_sub_total), moneyfmt(self.discount), moneyfmt(self.tax)) self.total = Decimal(item_sub_total + self.shipping_sub_total + self.tax) if save: self.save()