def setUp(self): self.client_price = ClientPriceFactory( product=ProductFactory(name='product', model_number='001'), client=ClientFactory(name='Central hospital'), unit_cost=18000, system_cost=9000) self.unit_cost_discount_1 = DiscountFactory( cost_type=UNIT_COST, order=1, client_price=self.client_price, percent=15, name='Bulk', apply_type=PRE_DOCTOR_ORDER) self.unit_cost_discount_2 = DiscountFactory( cost_type=UNIT_COST, order=2, client_price=self.client_price, percent=10, name='CCO') self.system_cost_discount_1 = DiscountFactory( cost_type=SYSTEM_COST, order=4, client_price=self.client_price, percent=15, name='Bulk', apply_type=PRE_DOCTOR_ORDER) self.system_cost_discount_2 = DiscountFactory( cost_type=SYSTEM_COST, order=2, client_price=self.client_price, percent=10, name='CCO')
class DiscountTestCase(TestCase): def setUp(self): self.discount = DiscountFactory() def test_discount_to_string(self): self.assertEqual(str(self.discount), self.discount.name) def test_is_valid_property(self): self.assertFalse(self.discount.is_valid()) today = datetime.now().date() self.discount.start_date = today + timedelta(days=5) self.assertFalse(self.discount.is_valid(purchased_date=today)) discount = DiscountFactory(end_date=date(2018, 5, 6)) self.assertFalse(discount.is_valid(purchased_date=today)) def test_display_name(self): spring_discount = DiscountFactory(name='Spring bulk', discount_type=PERCENT_DISCOUNT, percent=10, value=20) autumn_discount = DiscountFactory(name='Autumn bulk', discount_type=VALUE_DISCOUNT, percent=10, value=20) self.assertEqual(spring_discount.display_name, 'Spring bulk -10%') self.assertEqual(autumn_discount.display_name, 'Autumn bulk -$20')
def setUp(self): self.client = ClientFactory(name='UVMC') manufacturer = ManufacturerFactory(name='Medtronic') self.product = ProductFactory(model_number='SESR01', manufacturer=manufacturer) client_price = ClientPriceFactory(product=self.product, client=self.client) self.bulk_discount = DiscountFactory(name='Bulk', client_price=client_price, apply_type=PRE_DOCTOR_ORDER, cost_type=UNIT_COST, discount_type=PERCENT_DISCOUNT, percent=10, value=0, order=1) self.discount_2 = DiscountFactory(client_price=client_price, apply_type=ON_DOCTOR_ORDER, cost_type=SYSTEM_COST) self.device = self.product.device_set.get(client=self.client) self.device.hospital_number = '70669' self.device.save() self.item = ItemFactory(device=self.device, serial_number='PJN7204267', cost_type=SYSTEM_COST, discounts=[self.discount_2]) self.xls_file_path = os.path.join(FIXTURE_DIR, 'items.xls') self._prepare_xls_file()
def test_update_cost(self): client = ClientFactory() product = ProductFactory() discount_1 = DiscountFactory(cost_type=UNIT_COST, value=10, discount_type=VALUE_DISCOUNT, apply_type=PRE_DOCTOR_ORDER) discount_2 = DiscountFactory(cost_type=SYSTEM_COST, percent=20, discount_type=PERCENT_DISCOUNT, apply_type=ON_DOCTOR_ORDER) client_price = ClientPriceFactory(product=product, client=client, unit_cost=100, system_cost=150, discounts=[discount_1, discount_2]) device = client.device_set.get(product=product) unit_item = ItemFactory(device=device, cost_type=UNIT_COST, purchased_date=date(2017, 9, 8)) system_item = ItemFactory(device=device, cost_type=SYSTEM_COST, is_used=True, rep_case=RepCaseFactory(procedure_date=date(2018, 7, 9))) item = ItemFactory(cost=1000) unit_item.update_cost(discounts=[]) system_item.update_cost(discounts=[]) item.update_cost(discounts=[]) self.assertEqual((unit_item.cost, unit_item.saving), (client_price.unit_cost, 0)) self.assertEqual((system_item.cost, system_item.saving), (client_price.system_cost, 0)) self.assertEqual((item.cost, item.saving), (item.cost, 0)) unit_item.update_cost(discounts=[discount_1, discount_2]) system_item.update_cost(discounts=[discount_1, discount_2]) item.update_cost(discounts=[discount_1, discount_2]) self.assertEqual((unit_item.cost, unit_item.saving), (Decimal('90.00'), 0)) self.assertEqual((system_item.cost, system_item.saving), (Decimal('120.00'), 30)) self.assertEqual((item.cost, item.saving), (Decimal('1000.00'), 0))
def setUp(self): super().setUp() self.category = CategoryFactory() self.client_1 = ClientFactory() AccountFactory.create(user=self.user, client=self.client_1) self.product_1, self.product_2 = ProductFactory.create_batch(2, category=self.category) self.feature_1 = FeatureFactory(name='Wireless', product=self.product_2, shared_image=SharedImageFactory(image=ImageField(filename='wireless.jpg'))) self.feature_2, self.feature_3 = FeatureFactory.create_batch(2, product=self.product_2) price_1 = ClientPriceFactory(client=self.client_1, product=self.product_1, unit_cost=200, system_cost=300) price_2 = ClientPriceFactory(client=self.client_1, product=self.product_2, unit_cost=250, system_cost=300) ClientPriceFactory(client=self.client_1, unit_cost=250, system_cost=300, product=ProductFactory(enabled=False),) self.discount_1 = DiscountFactory( client_price=price_1, discount_type=VALUE_DISCOUNT, value=50, name='CCO', order=1, percent=0, apply_type=ON_DOCTOR_ORDER, cost_type=UNIT_COST, shared_image=SharedImageFactory(image=ImageField(filename='CCO.png')) ) self.discount_2 = DiscountFactory( client_price=price_2, discount_type=PERCENT_DISCOUNT, percent=10, cost_type=SYSTEM_COST, order=2, apply_type=ON_DOCTOR_ORDER, name='Repless', shared_image=SharedImageFactory(image=ImageField(filename='repless.png')) ) self.discount_3 = DiscountFactory( client_price=price_1, discount_type=PERCENT_DISCOUNT, value=0, name='Bulk', order=2, percent=15, apply_type=PRE_DOCTOR_ORDER, cost_type=UNIT_COST, shared_image=None) device = self.client_1.device_set.get(product=self.product_1) ItemFactory(device=device, discounts=[self.discount_3], purchase_type=BULK_PURCHASE, is_used=False, cost_type=UNIT_COST) self.path = reverse('api:hospital:device:products', args=(self.client_1.id, self.category.id,))
def test_saving_by_categories_queryset(self): RepCaseFactory(client=self.client_1, physician=self.physician_1, items=[self.item_1, self.item_2]) RepCaseFactory(client=self.client_1, physician=self.physician_3, items=[self.item_4]) self.assertCountEqual( self.client_1.items.used_by_client( self.client_1).saving_by_categories(), [{ 'category_id': self.item_1.device.product.category.id, 'category_name': self.item_1.device.product.category.name, 'saving': 0, 'spend': self.item_1.cost + self.item_4.cost, }, { 'category_id': self.item_2.device.product.category.id, 'category_name': self.item_2.device.product.category.name, 'saving': 0, 'spend': self.item_2.cost, }]) bulk_discount = DiscountFactory(apply_type=PRE_DOCTOR_ORDER, discount_type=VALUE_DISCOUNT, value=10, cost_type=UNIT_COST) cco_discount = DiscountFactory(apply_type=ON_DOCTOR_ORDER, discount_type=VALUE_DISCOUNT, value=15, cost_type=UNIT_COST) repless_discount = DiscountFactory(apply_type=ON_DOCTOR_ORDER, discount_type=VALUE_DISCOUNT, value=20, cost_type=UNIT_COST) self.item_1.discounts.set([bulk_discount, cco_discount]) self.item_2.discounts.set([bulk_discount, repless_discount]) self.item_4.discounts.set([cco_discount, repless_discount]) for item in [self.item_1, self.item_2, self.item_4]: ClientPriceFactory(client=self.client_1, product=item.device.product, unit_cost=item.cost) item.refresh_cost() item.save() self.assertCountEqual( self.client_1.items.used_by_client( self.client_1).saving_by_categories(), [{ 'category_id': self.item_1.device.product.category.id, 'category_name': self.item_1.device.product.category.name, 'saving': cco_discount.value * 2 + repless_discount.value, 'spend': self.item_1.cost + self.item_4.cost, }, { 'category_id': self.item_2.device.product.category.id, 'category_name': self.item_2.device.product.category.name, 'saving': repless_discount.value, 'spend': self.item_2.cost, }])
def test_is_valid_property(self): self.assertFalse(self.discount.is_valid()) today = datetime.now().date() self.discount.start_date = today + timedelta(days=5) self.assertFalse(self.discount.is_valid(purchased_date=today)) discount = DiscountFactory(end_date=date(2018, 5, 6)) self.assertFalse(discount.is_valid(purchased_date=today))
def test_display_name(self): spring_discount = DiscountFactory(name='Spring bulk', discount_type=PERCENT_DISCOUNT, percent=10, value=20) autumn_discount = DiscountFactory(name='Autumn bulk', discount_type=VALUE_DISCOUNT, percent=10, value=20) self.assertEqual(spring_discount.display_name, 'Spring bulk -10%') self.assertEqual(autumn_discount.display_name, 'Autumn bulk -$20')
def test_property_discounts_as_table(self): discount_1, discount_2, discount_3 = DiscountFactory.create_batch(3) item = ItemFactory(discounts=[discount_1, discount_2]) discounts_table = item.discounts_as_table self.assertTrue(discount_1.name in discounts_table) self.assertTrue(discount_2.name in discounts_table) self.assertFalse(discount_3.name in discounts_table)
def setUp(self): super().setUp() self.log_in_master_admin() self.client = ClientFactory() self.product = ProductFactory() ClientPriceFactory(client=self.client, product=self.product) device = self.client.device_set.get(product=self.product) self.discounts = DiscountFactory.create_batch(2) self.item = ItemFactory(device=device, purchase_type=BULK_PURCHASE, is_used=False, discounts=self.discounts)
def test_prefetch_price_with_discounts(self): client_1, client_2 = ClientFactory.create_batch(2) client_price_1 = ClientPriceFactory(product=self.product_1, client=client_1) client_price_2 = ClientPriceFactory(product=self.product_2, client=client_2) discount_1 = DiscountFactory(cost_type=UNIT_COST, client_price=client_price_1) discount_2 = DiscountFactory(cost_type=SYSTEM_COST, client_price=client_price_1) discount_3 = DiscountFactory(cost_type=SYSTEM_COST, client_price=client_price_2) ClientPriceFactory(product=self.product_1, client=ClientFactory()) products = Product.objects.prefetch_price_with_discounts( client_1).order_by('id') self.assertEqual(list(products), [self.product_1, self.product_2]) self.assertEqual(len(products[0].client_prices), 1) self.assertEqual(len(products[1].client_prices), 0) self.assertCountEqual(products[0].client_prices[0].unit_discounts, [discount_1]) self.assertCountEqual(products[0].client_prices[0].system_discounts, [discount_2]) products = Product.objects.prefetch_price_with_discounts( client_2).order_by('id') self.assertEqual(list(products), [self.product_1, self.product_2]) self.assertEqual(len(products[0].client_prices), 0) self.assertEqual(len(products[1].client_prices), 1) self.assertCountEqual(products[1].client_prices[0].unit_discounts, []) self.assertCountEqual(products[1].client_prices[0].system_discounts, [discount_3]) products = Product.objects.prefetch_price_with_discounts( ClientFactory()).order_by('id') self.assertEqual(list(products), [self.product_1, self.product_2]) self.assertEqual(len(products[0].client_prices), 0) self.assertEqual(len(products[1].client_prices), 0)
def test_session_authorized_staff_user(self): product_1, product_2 = ProductFactory.create_batch(2) discount_1, discount_3 = DiscountFactory.create_batch(2, cost_type=UNIT_COST) discount_2 = DiscountFactory(cost_type=SYSTEM_COST) ClientPriceFactory(client=self.hospital, product=product_1, discounts=[discount_1]) ClientPriceFactory(client=self.hospital, product=product_2, discounts=[discount_2, discount_3]) response = self.authorized_admin_client.get(self.path) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data, [ { 'id': product_1.id, 'discounts': { UNIT_COST: [{'id': discount_1.id, 'name': discount_1.name}], SYSTEM_COST: [] } }, { 'id': product_2.id, 'discounts': { UNIT_COST: [{'id': discount_3.id, 'name': discount_3.name}], SYSTEM_COST: [{'id': discount_2.id, 'name': discount_2.name}] } }, ])
def setUp(self): super().setUp() self.log_in_master_admin() self.product = ProductFactory(name='pacemaker') self.client_1, self.client_2 = ClientFactory.create_batch(2) client_price = ClientPriceFactory(product=self.product, client=self.client_1, unit_cost=1000, system_cost=1200) DiscountFactory(client_price=client_price, cost_type=UNIT_COST, name='CCO', order=1, percent=10) DiscountFactory(client_price=client_price, cost_type=UNIT_COST, name='Repless', order=2, percent=15) self.visit_reverse('admin:device_product_change', self.product.id) self.wait_for_element_contains_text( '.app-device.model-product #content h1', 'Change product')
def test_visit_item_details(self): discount = DiscountFactory() self.item_1.discounts.add(discount) self.visit_reverse('admin:hospital_item_change', self.item_1.id) self.wait_for_element_contains_text( '.model-item.change-form #content h1', 'Change item') self.wait_for_element_contains_text('.field-discounts_details', 'Discounts details:') self.assert_elements_count( '.field-discounts_details .item-discounts-table tbody tr', 1) self.wait_for_element_contains_text( '.field-discounts_details .item-discounts-table', discount.name) product_name = self.item_1.device.product.name self.find_link(product_name).click() self.wait_for_element_contains_text( '.model-product.change-form #content h1', 'Change product') self.assert_element_attribute_value('input#id_name', product_name)
def setUp(self): super().setUp() self.log_in_master_admin() self.client = ClientFactory(name='NSINC') self.product_1 = ProductFactory( name='Accolade VR', manufacturer=ManufacturerFactory(short_name='MDT')) self.product_2 = ProductFactory( name='Evera MRI XT', manufacturer=ManufacturerFactory(short_name='BIO')) ClientPriceFactory(client=self.client, product=self.product_1, system_cost=100, discounts=[ DiscountFactory(name='CCO', cost_type=SYSTEM_COST, discount_type=PERCENT_DISCOUNT, percent=20, order=2, apply_type=ON_DOCTOR_ORDER), DiscountFactory(name='Repless', cost_type=SYSTEM_COST, discount_type=VALUE_DISCOUNT, value=20, order=1, apply_type=ON_DOCTOR_ORDER), ]) bulk_discount = DiscountFactory(name='Bulk', cost_type=UNIT_COST, discount_type=VALUE_DISCOUNT, value=50, order=1, apply_type=PRE_DOCTOR_ORDER) ClientPriceFactory(client=self.client, product=self.product_2, unit_cost=200, discounts=[ DiscountFactory(name='Repless', cost_type=UNIT_COST, percent=10, order=1, apply_type=ON_DOCTOR_ORDER), bulk_discount ]) device = Device.objects.get(client=self.client, product=self.product_2) item = ItemFactory(device=device, is_used=False, serial_number='SER1234', discounts=[bulk_discount], cost_type=UNIT_COST, purchased_date=date(2018, 1, 1)) self.assertEqual(item.cost, 150) physician = AccountFactory( role=RoleFactory(priority=RolePriority.PHYSICIAN.value), client=self.client) self.rep_case = RepCaseFactory(client=self.client, owners=[physician], physician=physician, procedure_date=date(2018, 9, 10)) self.visit_reverse('admin:tracker_repcase_change', self.rep_case.id) self.wait_for_element_contains_text('#content h1', 'Change rep case')
def test_redeem(self): today = datetime.utcnow().date() unit_cost_discount = DiscountFactory(cost_type=UNIT_COST, order=3, client_price=self.client_price, discount_type=VALUE_DISCOUNT, value=688.50, name='Repless', apply_type=ON_DOCTOR_ORDER) system_repless_discount = DiscountFactory( cost_type=SYSTEM_COST, order=2, client_price=self.client_price, discount_type=PERCENT_DISCOUNT, percent=5, name='Repless', apply_type=ON_DOCTOR_ORDER) item = Item(purchased_date=today, rep_case=RepCase(procedure_date=today), cost_type=UNIT_COST) redemption = self.client_price.redeem( self.client_price.unit_cost_discounts, item) self.maxDiff = None self.assertDictEqual( redemption, { 'original_cost': 18000, 'total_cost': Decimal('13081.50'), 'point_of_sales_saving': Decimal('2218.50'), 'redeemed_discounts': [ { 'discount': self.unit_cost_discount_1, 'is_valid': True, 'value': Decimal('2700.00'), }, { 'discount': self.unit_cost_discount_2, 'is_valid': True, 'value': Decimal('1530.00'), }, { 'discount': unit_cost_discount, 'is_valid': True, 'value': Decimal('688.50'), }, ] }) item.cost_type = SYSTEM_COST redemption = self.client_price.redeem( self.client_price.system_cost_discounts, item) self.assertDictEqual( redemption, { 'original_cost': 9000, 'total_cost': Decimal('6502.50'), 'point_of_sales_saving': Decimal('1350.00'), 'redeemed_discounts': [ { 'discount': system_repless_discount, 'is_valid': True, 'value': Decimal('450.00'), }, { 'discount': self.system_cost_discount_2, 'is_valid': True, 'value': Decimal('900.00'), }, { 'discount': self.system_cost_discount_1, 'is_valid': True, 'value': Decimal('1147.50'), }, ] }) system_repless_discount.end_date = date(2017, 5, 6) system_repless_discount.save() redemption = self.client_price.redeem( self.client_price.system_cost_discounts, item) self.assertDictEqual( redemption, { 'original_cost': 9000, 'total_cost': Decimal('6885.00'), 'point_of_sales_saving': Decimal('900'), 'redeemed_discounts': [ { 'discount': system_repless_discount, 'is_valid': False, 'value': Decimal('450.00'), }, { 'discount': self.system_cost_discount_2, 'is_valid': True, 'value': Decimal('900.00'), }, { 'discount': self.system_cost_discount_1, 'is_valid': True, 'value': Decimal('1215.00'), }, ] })
def setUp(self): client = ClientFactory() product = ProductFactory() self.discount_1 = DiscountFactory(name='CCO', discount_type=PERCENT_DISCOUNT, percent=10, cost_type=UNIT_COST, order=1, apply_type=ON_DOCTOR_ORDER) self.discount_2 = DiscountFactory(name='Bulk', discount_type=VALUE_DISCOUNT, value=20, cost_type=SYSTEM_COST, order=1, apply_type=PRE_DOCTOR_ORDER) self.client_price = ClientPriceFactory( client=client, product=product, unit_cost=100, system_cost=120, discounts=[self.discount_1, self.discount_2]) device = client.device_set.get(product=product) today = datetime.utcnow().date() self.item_1 = ItemFactory( device=device, cost_type=UNIT_COST, rep_case=RepCaseFactory(procedure_date=today), is_used=True, discounts=[self.discount_1]) self.item_2, self.item_3 = ItemFactory.create_batch( 2, device=device, cost_type=SYSTEM_COST, is_used=False, discounts=[self.discount_2], purchased_date=today) self.rebate = RebateFactory(client=client, manufacturer=product.manufacturer) RebatableItemFactory(rebate=self.rebate, item_type=REBATED_ITEM, content_object=product) TierFactory(rebate=self.rebate, tier_type=SPEND, discount_type=PERCENT_DISCOUNT, percent=10, order=2) TierFactory(rebate=self.rebate, tier_type=PURCHASED_UNITS, discount_type=VALUE_DISCOUNT, value=20, lower_bound=2, upper_bound=10, order=2) TierFactory(rebate=self.rebate, tier_type=PURCHASED_UNITS, discount_type=VALUE_DISCOUNT, value=50, lower_bound=11, upper_bound=100, order=2)
def setUp(self): self.discount = DiscountFactory()