def test_package_creation_does_not_increase_price_for_cheaper_1_day_long_old_package_in_the_same_day( self): old_component_price = 10 new_component_price = old_component_price + 5 start_date = timezone.datetime(2014, 2, 26, tzinfo=pytz.UTC) package_change_date = start_date end_of_the_month = core_utils.month_end(package_change_date) with freeze_time(start_date): old_package = invoices_fixtures.create_package( component_price=old_component_price) customer = old_package.tenant.service_project_link.project.customer with freeze_time(package_change_date): old_package.delete() new_template = invoices_fixtures.create_package_template( component_price=new_component_price) new_package = packages_factories.OpenStackPackageFactory( template=new_template, tenant__service_project_link__project__customer=customer, ) old_components_price = old_package.template.price * ( package_change_date - start_date).days second_component_usage_days = invoices_utils.get_full_days( package_change_date, end_of_the_month) new_components_price = new_package.template.price * second_component_usage_days expected_price = old_components_price + new_components_price # assert self.assertEqual(invoices_models.Invoice.objects.count(), 1) self.assertEqual(Decimal(expected_price), invoices_models.Invoice.objects.first().price)
def test_package_creation_increases_price_from_old_package_if_it_is_more_expensive_in_the_end_of_the_month( self): old_component_price = 15 new_component_price = old_component_price - 5 start_date = timezone.datetime(2014, 2, 20, tzinfo=pytz.UTC) package_change_date = timezone.datetime(2014, 2, 27, tzinfo=pytz.UTC) end_of_the_month = core_utils.month_end(package_change_date) with freeze_time(start_date): old_package = invoices_fixtures.create_package( component_price=old_component_price) tenant = old_package.tenant with freeze_time(package_change_date): old_package.delete() new_template = invoices_fixtures.create_package_template( component_price=new_component_price) new_package = packages_factories.OpenStackPackageFactory( template=new_template, tenant=tenant, ) old_components_price = old_package.template.price * ( (package_change_date - start_date).days + 1) second_component_usage_days = invoices_utils.get_full_days( package_change_date, end_of_the_month) - 1 new_components_price = new_package.template.price * second_component_usage_days expected_price = old_components_price + new_components_price # assert self.assertEqual(invoices_models.Invoice.objects.count(), 1) self.assertEqual(Decimal(expected_price), invoices_models.Invoice.objects.first().price)
def serialize_resource_limit_period(period): billing_periods = get_full_days(period['start'], period['end']) return { 'start': period['start'].isoformat(), 'end': period['end'].isoformat(), 'quantity': period['quantity'], 'billing_periods': billing_periods, 'total': str(period['quantity'] * billing_periods), }
def test_existing_invoice_is_updated_on_resource_creation(self): start_date = timezone.datetime(2014, 2, 27, tzinfo=pytz.UTC) end_date = core_utils.month_end(start_date) usage_days = utils.get_full_days(start_date, end_date) month_days = monthrange(start_date.year, start_date.month)[1] factor = quantize_price(decimal.Decimal(usage_days) / month_days) with freeze_time(start_date): invoice = factories.InvoiceFactory(customer=self.fixture.customer) self.resource.set_state_ok() self.resource.save() self.assertEqual(models.Invoice.objects.count(), 1) self.assertTrue(invoice.items.filter(resource=self.resource).exists()) expected_price = self.plan_component.price * factor self.assertEqual(invoice.price, Decimal(expected_price))
def test_invoice_item_with_daily_price(self): start_date = timezone.datetime(2017, 7, 14) end_date = timezone.datetime(2017, 7, 31, 23, 59, 59) resource = self.fixture.resource self.fixture.plan.unit = common_mixins.UnitPriceMixin.Units.PER_DAY self.fixture.plan.save() with freeze_time(start_date): resource.set_state_ok() resource.save() expected_price = (utils.get_full_days(start_date, end_date) * self.fixture.plan_component.price) invoice_item = models.InvoiceItem.objects.get( resource=self.fixture.resource) self.assertEqual(invoice_item.price, expected_price)
def test_invoice_price_is_not_changed_after_a_while_if_resource_is_deleted( self): start_date = timezone.datetime(2014, 2, 27, tzinfo=pytz.UTC) end_date = core_utils.month_end(start_date) usage_days = utils.get_full_days(start_date, end_date) month_days = monthrange(start_date.year, start_date.month)[1] factor = quantize_price(decimal.Decimal(usage_days) / month_days) with freeze_time(start_date): self.resource.set_state_ok() self.resource.save() self.assertEqual(models.Invoice.objects.count(), 1) invoice = models.Invoice.objects.first() with freeze_time(end_date): self.resource.set_state_terminating() self.resource.save() self.resource.set_state_terminated() self.resource.save() expected_price = self.plan_component.price * factor self.assertEqual(invoice.price, Decimal(expected_price))
def usage_days(item): full_days = utils.get_full_days(item.start, item.end) return full_days
def get_total_quantity(cls, unit, value, start, end): if unit == invoice_models.InvoiceItem.Units.PER_DAY: return value * get_full_days(start, end) return value