def test_20_shipping_discount(self): """Test applying discounts to shipping too""" order_processors = plata.settings.PLATA_ORDER_PROCESSORS[:] plata.settings.PLATA_ORDER_PROCESSORS[-2] = 'plata.shop.processors.FixedAmountShippingProcessor' p1 = self.create_product() p2 = self.create_product() order = self.create_order() normal1 = order.modify_item(p1, 3, recalculate=False) normal2 = order.modify_item(p2, 5) self.assertAlmostEqual(order.total, Decimal('639.20') + 8) discount = Discount.objects.create( type=Discount.AMOUNT_INCL_TAX, code='d2', name='d2', value=Decimal('640.00'), is_active=True) order.add_discount(discount) self.assertAlmostEqual(order.total, Decimal('7.20')) discount.value = Decimal('650.00') order.add_discount(discount) self.assertAlmostEqual(order.total, Decimal('0.00')) plata.settings.PLATA_ORDER_PROCESSORS = order_processors[:]
def test_11_multiple_discounts(self): """Test behavior of orders with more than one discount""" order = self.create_order() product = self.create_product() order.modify_item(product, 3) order.recalculate_total() self.assertAlmostEqual(order.total, Decimal('239.70')) order.add_discount(Discount.objects.create( type=Discount.PERCENTAGE, name='Percentage', code='perc20', value=Decimal('20.00'), is_active=True, )) order.recalculate_total() self.assertAlmostEqual(order.total, Decimal('239.70') / 5 * 4) order.add_discount(Discount.objects.create( type=Discount.AMOUNT_INCL_TAX, name='Amount incl. tax', code='amount_incl_20', value=Decimal('20.00'), is_active=True, )) order.recalculate_total() self.assertAlmostEqual(order.total, (Decimal('239.70') - 20) / 5 * 4)
def test_18_amount_discount_incl_tax(self): """Test discount amounts specified with tax included""" p1 = self.create_product() p2 = self.create_product() price = p1.get_price(currency='ASDF') price.tax_class = self.tax_class_germany price.save() order = self.create_order() order.currency = 'ASDF' order.save() normal1 = order.modify_item(p1, 3) normal2 = order.modify_item(p2, 5) order.recalculate_total() #self.assertAlmostEqual(order.total, Decimal('598.84')) # We use ROUND_HALF_UP now self.assertAlmostEqual(order.total, Decimal('598.85')) discount = Discount.objects.create( type=Discount.AMOUNT_INCL_TAX, code='asdf', name='Amount discount', value=Decimal('50.00'), is_active=True) order.add_discount(discount) order.recalculate_total() # Exact values after usage of different tax rates in same order #self.assertAlmostEqual(order.total, Decimal('548.84')) # We use ROUND_HALF_UP now self.assertAlmostEqual(order.total, Decimal('548.85')) self.assertAlmostEqual(order.discount, Decimal('50.00'))
def test_14_invoice2009_0170_0002_test(self): """Reproduce invoide of a deployed metronom installation""" order = self.create_order() p = self.create_product() p.prices.all().delete() p.prices.create( _unit_price=1, tax_included=False, currency=order.currency, tax_class=self.tax_class, ) order.modify_item(p, 952) order.modify_item(p, 120) discount = Discount.objects.create( type=Discount.AMOUNT_EXCL_TAX, name='Discount', value=532, code='1234code', ) order.add_discount(discount) order.recalculate_total() plata.settings.PLATA_PRICE_INCLUDES_TAX = False self.assertAlmostEqual(order.subtotal, Decimal('1072.00')) self.assertAlmostEqual(order.discount, Decimal('532.00')) self.assertAlmostEqual(order.items_tax, Decimal('41.04')) self.assertAlmostEqual(order.total, Decimal('581.04')) plata.settings.PLATA_PRICE_INCLUDES_TAX = True
def test_07_order_amount_discount(self): """Test a simple amount discount""" order = self.create_order() p1 = self.create_product() p2 = self.create_product() normal1 = order.modify_item(p1, 3) normal2 = order.modify_item(p2, 5) order.recalculate_total() self.assertAlmostEqual(order.total, Decimal('639.20')) discount = Discount.objects.create( type=Discount.AMOUNT_INCL_TAX, code='asdf', name='Amount discount', value=Decimal('50.00'), is_active=True) order.add_discount(discount) order.recalculate_total() discounted1 = order.modify_item(p1, 0) discounted2 = order.modify_item(p2, 0) tax_factor = Decimal('1.076') item_price_incl_tax = Decimal('79.90') item_price_excl_tax = item_price_incl_tax / tax_factor self.assertAlmostEqual(order.total, Decimal('639.20') - Decimal('50.00')) self.assertAlmostEqual(normal1.unit_price, discounted1.unit_price) self.assertAlmostEqual(normal2.unit_price, discounted2.unit_price) self.assertAlmostEqual(normal1.unit_price, item_price_incl_tax) self.assertEqual(normal1.line_item_discount, 0) self.assertEqual(normal2.line_item_discount, 0) self.assertAlmostEqual(discounted1.line_item_discount, Decimal('50.00') / 8 * 3) self.assertAlmostEqual(discounted2.line_item_discount, Decimal('50.00') / 8 * 5) self.assertAlmostEqual(discounted1.discounted_subtotal, order.total / 8 * 3) self.assertAlmostEqual(discounted2.discounted_subtotal, order.total / 8 * 5) plata.settings.PLATA_PRICE_INCLUDES_TAX = False order.recalculate_total() discounted1 = order.modify_item(p1, 0) discounted2 = order.modify_item(p2, 0) self.assertAlmostEqual(order.total, Decimal('639.20') - Decimal('50.00')) self.assertAlmostEqual(discounted1.unit_price, item_price_excl_tax) self.assertAlmostEqual(discounted1.line_item_discount, discount.value / tax_factor / 8 * 3) self.assertAlmostEqual(order.total, discounted1.discounted_subtotal + discounted2.discounted_subtotal + order.items_tax) plata.settings.PLATA_PRICE_INCLUDES_TAX = True
def test_12_order4567_test(self): """Reproduce order ID 4567 of a deployed satchmo shop installation""" order = self.create_order() p1 = self.create_product() p1.name = 'Kleid' p1.save() p1.prices.all().delete() p1.prices.create( _unit_price=160, tax_included=True, currency=order.currency, tax_class=self.tax_class, ) p2 = self.create_product() p2.prices.all().delete() p2.prices.create( _unit_price=280, tax_included=True, currency=order.currency, tax_class=self.tax_class, ) c = Category.objects.create( name='category', slug='category', ) p1.categories.add(c) order.modify_item(p1, 1) order.modify_item(p2, 1) self.assertAlmostEqual(order.total, Decimal('440.00')) discount = Discount( type=Discount.PERCENTAGE, name='Sonderrabatt Kleid', value=Decimal('20.00'), code='1234code', ) discount.config = {'only_categories': {'categories': [c.pk]}} discount.save() order.add_discount(discount) order.recalculate_total() self.assertAlmostEqual(order.total, 408) self.assertAlmostEqual(order.subtotal, 440) self.assertAlmostEqual(order.discount, 32)
def test_13_order4206_test(self): """Reproduce order ID 4206 of a deployed satchmo shop installation""" order = self.create_order() p1 = self.create_product() p1.name = 'Venice' p1.save() p1.prices.all().delete() p1.prices.create( _unit_price=170, tax_included=True, currency=order.currency, tax_class=self.tax_class, ) p2 = self.create_product() p2.prices.all().delete() p2.prices.create( _unit_price=Decimal('40.80'), tax_included=True, currency=order.currency, tax_class=self.tax_class, ) order.modify_item(p1, 1) order.modify_item(p2, 1) c = Category.objects.create( name='category', slug='category', ) p1.categories.add(c) discount = Discount( type=Discount.AMOUNT_INCL_TAX, name='Sonderrabatt Venice', value=Decimal('20.00'), code='1234code', ) discount.config = {'only_categories': {'categories': [c.pk]}} discount.save() order.add_discount(discount) order.recalculate_total() self.assertAlmostEqual(order.total, Decimal('190.80')) self.assertAlmostEqual(order.subtotal, Decimal('210.80')) self.assertAlmostEqual(order.discount, 20)
def test_06_order_percentage_discount(self): """Test a simple percentage discount""" order = self.create_order() p1 = self.create_product() p2 = self.create_product() order.modify_item(p1, 3) order.modify_item(p2, 5) discount = Discount.objects.create( is_active=False, type=Discount.PERCENTAGE, code='asdf', name='Percentage discount', value=30) self.assertRaises(ValidationError, lambda: order.add_discount(discount)) discount.is_active = True discount.save() order.add_discount(discount) order.recalculate_total() tax_factor = Decimal('1.076') item_price_incl_tax = Decimal('79.90') item_price_excl_tax = item_price_incl_tax / tax_factor order.recalculate_total() item = order.modify_item(p1, relative=0) item2 = order.modify_item(p2, relative=0) self.assertAlmostEqual(item.unit_price, item_price_incl_tax) self.assertAlmostEqual(item.line_item_discount, item_price_incl_tax * 3 * Decimal('0.30')) self.assertAlmostEqual(order.total, item.discounted_subtotal + item2.discounted_subtotal) plata.settings.PLATA_PRICE_INCLUDES_TAX = False order.recalculate_total() item = order.modify_item(p1, 0) item2 = order.modify_item(p2, 0) self.assertAlmostEqual(item.unit_price, item_price_excl_tax) self.assertAlmostEqual(item.line_item_discount, item_price_excl_tax * 3 * Decimal('0.30')) self.assertAlmostEqual(order.total, item.discounted_subtotal + item2.discounted_subtotal + order.items_tax) plata.settings.PLATA_PRICE_INCLUDES_TAX = True
def test_15_remaining_discount(self): """Test determination of remaining discount amount""" order = self.create_order() product = self.create_product() order.modify_item(product, 1) self.assertAlmostEqual(order.total, Decimal('79.90')) order.add_discount(Discount.objects.create( type=Discount.AMOUNT_INCL_TAX, name='Discount', value='100', code='1234code', )) self.assertAlmostEqual(order.subtotal, Decimal('79.90')) self.assertAlmostEqual(order.discount, Decimal('79.90')) self.assertAlmostEqual(order.total, 0) self.assertAlmostEqual(order.discount_remaining, Decimal('20.10') / Decimal('1.076'))
def test_09_selective_discount(self): """Test applying discounts with product restriction""" p1 = self.create_product() p2 = self.create_product() p2.name = 'Discountable' p2.save() c = Category.objects.create( name='category', slug='category', is_active=True, is_internal=True, ) p2.categories.add(c) d = Discount( type=Discount.PERCENTAGE, name='Some discount', code='asdf', value=Decimal('30'), is_active=True, ) d.config = {'only_categories': {'categories': [c.pk]}} d.save() order = self.create_order() order.modify_item(p1, 3) order.modify_item(p2, 2) order.add_discount(d) order.recalculate_total() # Test that only one order item has its discount applied self.assertEqual(Product.objects.all().count(), 2) self.assertEqual(order.items.count(), 2) self.assertEqual(1, len([item for item in order.items.all() if item._line_item_discount])) self.assertEqual(Category.objects.active().count(), 1) self.assertEqual(Category.objects.public().count(), 0) self.assertEqual(unicode(Category.objects.create( name='blaa', slug='blaa', parent=c)), 'category - blaa')