def _activate(self, user): today = datetime.utcnow().date() code = "%x%x%x%x%x" % uuid1().fields[0:5] discount = Discount( site=self.site, description=_(u"Invite discount " u"for %(last_name)s %(first_name)s") % { 'last_name': user.last_name, 'first_name': user.first_name}, code=code, active=True, amount=self.amount, percentage=self.percentage, automatic=False, allowedUses=1, minOrder=self.minOrder, startDate=today, endDate=today + timedelta(days=self.daysValid), shipping=self.shipping, allValid=self.allValid) discount.save() for product in self.valid_products.all(): discount.valid_products.add(product) for category in self.valid_categories.all(): discount.valid_categories.add(category) discount.save() discount_amount = None if self.amount is not None: discount_amount = moneyfmt(self.amount) else: discount_amount = "%s%%" % self.percentage send_store_mail( subject=_(u"A discount coupon for you"), context={ 'first_name': user.first_name, 'invites_number': self.invites_target, 'discount_amount': discount_amount, 'coupon_code': discount.code, 'end_date': discount.endDate}, template='invites/mail/discount.txt', recipients_list=[user.email], template_html='invites/mail/discount.html') # create new InviterDiscount inviter_discount = InviterDiscount( user=user, coupon=discount, template=self) inviter_discount.save() return True
def testThirdsUneven(self): d = { 1 : Decimal("10.00"), 2 : Decimal("10.00"), 3 : Decimal("3.00"), } s = Discount.apply_even_split(d, Decimal("10.00")) self.assertEqual(s[1], Decimal("3.50")) self.assertEqual(s[2], Decimal("3.50")) self.assertEqual(s[3], Decimal("3.00"))
def testThirdsUneven(self): d = { 1: Decimal("10.00"), 2: Decimal("10.00"), 3: Decimal("3.00"), } s = Discount.apply_even_split(d, Decimal("10.00")) self.assertEqual(s[1], Decimal("3.51")) self.assertEqual(s[2], Decimal("3.50")) self.assertEqual(s[3], Decimal("3.00"))
def testPercentage1(self): d = { 1: Decimal("10.00"), 2: Decimal("10.00"), 3: Decimal("10.00"), } s = Discount.apply_percentage(d, Decimal("10.00")) self.assertEqual(s[1], Decimal("1.00")) self.assertEqual(s[2], Decimal("1.00")) self.assertEqual(s[3], Decimal("1.00"))
def testLargeThirds(self): d = { 1: Decimal("100.00"), 2: Decimal("100.00"), 3: Decimal("100.00"), } s = Discount.apply_even_split(d, Decimal("100.00")) self.assertEqual(s[1], Decimal("33.34")) self.assertEqual(s[2], Decimal("33.33")) self.assertEqual(s[3], Decimal("33.33"))
def testPercentage1(self): d = { 1 : Decimal("10.00"), 2 : Decimal("10.00"), 3 : Decimal("10.00"), } s = Discount.apply_percentage(d, Decimal("10.00")) self.assertEqual(s[1], Decimal("1.00")) self.assertEqual(s[2], Decimal("1.00")) self.assertEqual(s[3], Decimal("1.00"))
def testLargeThirds(self): d = { 1 : Decimal("100.00"), 2 : Decimal("100.00"), 3 : Decimal("100.00"), } s = Discount.apply_even_split(d, Decimal("100.00")) self.assertEqual(s[1], Decimal("33.34")) self.assertEqual(s[2], Decimal("33.33")) self.assertEqual(s[3], Decimal("33.33"))
def invitees_changed(sender, **kwargs): inviteReward = kwargs['instance'] if kwargs['action'] not in ('post_add', 'post_clear', 'post_remove'): return allowedUses = 1 from decimal import Decimal invitationRewardExpirationDays = 365 threshold_invitees_numbers = [2,4] amounts = [Decimal("100.0"), Decimal("200.0")] minOrders = [Decimal("500.0"), Decimal("1000.0")] invitor = inviteReward.invitor invitees_number = inviteReward.invitees.count() for i in range(0,len(threshold_invitees_numbers)): threshold_invitees_number = threshold_invitees_numbers[i] if invitees_number == threshold_invitees_number: amount = amounts[i] minOrder = minOrders[i] from product.models import Discount from satchmo_store.shop.models import Config shop_config = Config.objects.get_current() import uuid import datetime u = uuid.uuid4() discountCode = u.bytes.encode("base64")[:20] startDate = datetime.date.today() endDate = datetime.date.today() + datetime.timedelta(days=invitationRewardExpirationDays) discount = Discount(site = shop_config.site , description = 'Invitation reward of user(id:%i): %s' %(invitor.user.id, invitor.user.username) , code = discountCode , startDate = startDate , endDate = endDate , allValid = True , allowedUses = allowedUses , amount = amount , minOrder = minOrder , active = True) discount.save() send_reward_email(invitor.email, invitor.first_name, invitor.last_name, invitees_number = invitees_number, discount = discount) inviteReward.sent = True
def testEvenSplitOneTooSmall(self): """Test when one of the items is maxed, but others are OK""" d = { 1: Decimal("10.00"), 2: Decimal("5.00"), 3: Decimal("10.00"), 4: Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("23.00")) self.assertEqual(s[1], Decimal("6.00")) self.assertEqual(s[2], Decimal("5.00")) self.assertEqual(s[3], Decimal("6.00")) self.assertEqual(s[4], Decimal("6.00"))
def testEvenSplitEqual(self): """Test when amount is exactly equal""" d = { 1: Decimal("10.00"), 2: Decimal("10.00"), 3: Decimal("10.00"), 4: Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("40.00")) self.assertEqual(s[1], Decimal("10.00")) self.assertEqual(s[2], Decimal("10.00")) self.assertEqual(s[3], Decimal("10.00")) self.assertEqual(s[4], Decimal("10.00"))
def testEvenSplitTooMuch(self): """Test when amount is greater than total""" d = { 1: Decimal("10.00"), 2: Decimal("10.00"), 3: Decimal("10.00"), 4: Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("50.00")) self.assertEqual(s[1], Decimal("10.00")) self.assertEqual(s[2], Decimal("10.00")) self.assertEqual(s[3], Decimal("10.00")) self.assertEqual(s[4], Decimal("10.00"))
def testEvenSplit1(self): """Simple split test""" d = { 1: Decimal("10.00"), 2: Decimal("10.00"), 3: Decimal("10.00"), 4: Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("16.00")) self.assertEqual(s[1], Decimal("4.00")) self.assertEqual(s[2], Decimal("4.00")) self.assertEqual(s[3], Decimal("4.00")) self.assertEqual(s[4], Decimal("4.00"))
def assert_apply_even_split(self, input_str, amount_str, expect_str): """ Method which simplifies many similar tests to be written more compact on one line Example: the following line does the same as the method ``testEvenSplit1``. > > > self.assert_apply_even_split('10 10 10 10', '16', '4.00 4.00 4.00 4.00') """ ddd = input_str.split() dd = map(lambda x: Decimal(str(x)).quantize(Decimal("0.01")), ddd) d = dict(enumerate(dd)) amount = Decimal(str(amount_str)).quantize(Decimal("0.01")) s = Discount.apply_even_split(d, amount) self.assertEqual(s.keys(), d.keys()) output_str = ' '.join(map(lambda (k, v): str(v), sorted(s.items()))) self.assertEqual(output_str, expect_str)
def testEvenSplitOneTooSmall(self): """Test when one of the items is maxed, but others are OK""" d = { 1 : Decimal("10.00"), 2 : Decimal("5.00"), 3 : Decimal("10.00"), 4 : Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("23.00")) self.assertEqual(s[1], Decimal("6.00")) self.assertEqual(s[2], Decimal("5.00")) self.assertEqual(s[3], Decimal("6.00")) self.assertEqual(s[4], Decimal("6.00"))
def testEvenSplitEqual(self): """Test when amount is exactly equal""" d = { 1 : Decimal("10.00"), 2 : Decimal("10.00"), 3 : Decimal("10.00"), 4 : Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("40.00")) self.assertEqual(s[1], Decimal("10.00")) self.assertEqual(s[2], Decimal("10.00")) self.assertEqual(s[3], Decimal("10.00")) self.assertEqual(s[4], Decimal("10.00"))
def testEvenSplitTooMuch(self): """Test when amount is greater than total""" d = { 1 : Decimal("10.00"), 2 : Decimal("10.00"), 3 : Decimal("10.00"), 4 : Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("50.00")) self.assertEqual(s[1], Decimal("10.00")) self.assertEqual(s[2], Decimal("10.00")) self.assertEqual(s[3], Decimal("10.00")) self.assertEqual(s[4], Decimal("10.00"))
def testEvenSplit1(self): """Simple split test""" d = { 1 : Decimal("10.00"), 2 : Decimal("10.00"), 3 : Decimal("10.00"), 4 : Decimal("10.00"), } s = Discount.apply_even_split(d, Decimal("16.00")) self.assertEqual(s[1], Decimal("4.00")) self.assertEqual(s[2], Decimal("4.00")) self.assertEqual(s[3], Decimal("4.00")) self.assertEqual(s[4], Decimal("4.00"))
def _activate(self, user): today = datetime.utcnow().date() code = "%x%x%x%x%x" % uuid1().fields[0:5] discount = Discount( site=self.site, description=_(u"Invite discount " u"for %(last_name)s %(first_name)s") % { 'last_name': user.last_name, 'first_name': user.first_name }, code=code, active=True, amount=self.amount, percentage=self.percentage, automatic=False, allowedUses=1, minOrder=self.minOrder, startDate=today, endDate=today + timedelta(days=self.daysValid), shipping=self.shipping, allValid=self.allValid) discount.save() for product in self.valid_products.all(): discount.valid_products.add(product) for category in self.valid_categories.all(): discount.valid_categories.add(category) discount.save() discount_amount = None if self.amount is not None: discount_amount = moneyfmt(self.amount) else: discount_amount = "%s%%" % self.percentage send_store_mail(subject=_(u"A discount coupon for you"), context={ 'first_name': user.first_name, 'invites_number': self.invites_target, 'discount_amount': discount_amount, 'coupon_code': discount.code, 'end_date': discount.endDate }, template='invites/mail/discount.txt', recipients_list=[user.email], template_html='invites/mail/discount.html') # create new InviterDiscount inviter_discount = InviterDiscount(user=user, coupon=discount, template=self) inviter_discount.save() return True
def create_code(self): discounts = Discount.objects.all() code = self._code_build() while len(discounts.filter(code=code)): #print "found matching code %s" % code code = self._code_build() d = Discount() d.site = Site.objects.all()[0] d.description = u"Discount for %s" % self.name d.code = code d.active = True d.percentage = 100 d.allowedUses = 1 d.allValid = False d.startDate = datetime.now() d.endDate = datetime.now() + timedelta(days=30) d.shipping = 'FREE' d.save() # All free products for p in ProductAttribute.objects.filter( option__name='Free', value="YES" ): d.valid_products.add( p.product ) d.save() self.discount_code = code return code