def update_twosell_direct(self): """ Update Twosell direct sale stats for purchase. Only applies to Purchase that is final. Aggregate values of Twosell direct sales. """ # Bail early if this is a preliminary receipt if not self.final: raise CalculationException('Cannot calculate Twosell direct on a preliminary purchase.') purprods = self.purchasedproduct_set.all() # Bail if both prel and final does not exist try: prel = Purchase.prels.get(transactionid=self.transactionid) except Purchase.DoesNotExist: purprods.update(direct_gross_incl_vat=0, direct_gross_excl_vat=0) self.direct_gross_incl_vat = purprods.aggregate(Sum('direct_gross_incl_vat')).values()[0] or Decimal(0) self.direct_gross_excl_vat = purprods.aggregate(Sum('direct_gross_excl_vat')).values()[0] or Decimal(0) return # Check conditions for qualifying purchase as Twosell generated: # - Final must have total cost greater or equal to total cost for Prel # - No products should have been removed from Final compared to Prel # - Must have received a status report that indicates Twosell screen was shown. # # Only update Twosell direct for each row if all conditions are met if prel.total_cost >= self.total_cost or self.removed_products() or not self.direct_reported_shown: purprods.update(direct_gross_incl_vat=0, direct_gross_excl_vat=0) else: for purprod in purprods: purprod.update_twosell_direct(count_extra=settings.COUNT_EXTRA_AS_TWOSELL) purprod.save() # Update Twosell totals on purchase self.direct_gross_incl_vat = purprods.aggregate(Sum('direct_gross_incl_vat')).values()[0] or Decimal(0) self.direct_gross_excl_vat = purprods.aggregate(Sum('direct_gross_excl_vat')).values()[0] or Decimal(0) # Handle limits to Twosell contribution try: max_twosell_direct = TwosellConf.objects.latest().max_twosell_direct except TwosellConf.DoesNotExist: max_twosell_direct = 1000 if self.direct_gross_incl_vat > max_twosell_direct: self.direct_net_incl_vat = max_twosell_direct else: self.direct_net_incl_vat = self.direct_gross_incl_vat # Original vat_rate try: vat_rate = 100 * (self.direct_gross_incl_vat - self.direct_gross_excl_vat) / self.direct_gross_excl_vat except (ZeroDivisionError, InvalidOperation): vat_rate = 0 self.direct_net_excl_vat = calc_excl_vat(vat_rate, self.direct_net_incl_vat)
def _update_excl_vat(self): """Update excl_vat fields.""" vat_rate = self.product.vat_rate self.total_cost_excl_vat = calc_excl_vat(vat_rate, self.total_cost) self.direct_gross_excl_vat = calc_excl_vat(vat_rate, self.direct_gross_incl_vat) self.coupon_gross_excl_vat = calc_excl_vat(vat_rate, self.coupon_gross_incl_vat)