def get_estimated_usage(self, service: Service, usage_settings: UsageSettings) -> EstimatedUsage: LOG.debug( '{} get dynamic price per second called for service {}:{}'.format( self.module_name, service.id, service, )) return EstimatedUsage()
def __compute_client_usage(self) -> ClientUsage: # compute total unpaid usage for all services associated with the client LOG.debug('Computing total usage for client {}'.format(self.client)) total_unpaid_usage = Decimal(0) total_estimated_usage = EstimatedUsage() usage_settings = UsageSettings(billing_settings=self.billing_settings) for service in self.client.services.all(): billing_module = module_factory.get_module_instance( service=service) # get unpaid usage from billing module unpaid_usage = billing_module.get_unpaid_usage(service) total_unpaid_usage += unpaid_usage.total_cost if service.status == ServiceStatus.active: # get unpaid usage from service if service.next_due_date is not None and service.next_due_date < self.reference_datetime: total_unpaid_usage += service.get_fixed_price() # see if we need to get estimated usage from billing module if not service.is_price_overridden: # get estimated usage from billing module estimated_usage = billing_module.get_estimated_usage( service, usage_settings=usage_settings) total_estimated_usage += estimated_usage # add service static price if needed if service.get_fixed_price() > 0: service_fixed_usage = EstimatedUsage.create_for_fixed_price( fixed_price=service.get_fixed_price(), cycle_end_date=service.next_due_date, get_next_end_date=lambda previous_end_date, s=service: s.get_next_due_date(previous_end_date), usage_settings=usage_settings) total_estimated_usage += service_fixed_usage return ClientUsage(total_unpaid_usage, total_estimated_usage)
def get_estimated_usage(self, service: Service, usage_settings: UsageSettings) -> EstimatedUsage: LOG.debug('{} get_estimated_usage called for service {}:{}'.format( self.module_name, service.id, service)) estimated_usage = EstimatedUsage() for reseller_client in filter_queryset_for_client( queryset=Client.objects, client=service.client).all(): for client_service in reseller_client.services.all(): service_module = module_factory.get_module_instance( service=client_service, reseller_usage=True) estimated_usage = estimated_usage + service_module.get_estimated_usage( service=client_service, usage_settings=usage_settings, ) return estimated_usage
def __init__(self, unpaid_usage: Decimal = Decimal(0), estimated_usage: EstimatedUsage = None): self.unpaid_usage = unpaid_usage self.estimated_usage = estimated_usage if estimated_usage is not None else EstimatedUsage( )
def get_estimated_usage(service: Service, usage_settings: UsageSettings) -> EstimatedUsage: try: service_dynamic_usage = service.service_dynamic_usage except Exception as e: del e LOG.error( 'Get estimated usage called for client without client billing {0}, aborting' .format(service.client)) return EstimatedUsage() if service_dynamic_usage is None: LOG.error( 'Get estimated usage called for client without client billing {0}, aborting' .format(service.client)) return EstimatedUsage() active_resources = get_active_resources( usage=service_dynamic_usage.get_usage(), accounting_end=service_dynamic_usage.updated_at) usage_cycle = UsageCycle(Decimal(0), service_dynamic_usage.end_date, service_dynamic_usage.get_next_billing_date, usage_settings=usage_settings) for active_resource in active_resources: # we are only interested in resources that apply for a billing cycle if active_resource['price_details']['time_unit'] == 'bc': usage_cycle.add_fixed_price(Decimal(active_resource['price'])) else: unit_price = active_resource['price_details']['unit_price'] quantity = active_resource['attribute_value'] price_seconds = utils.time_unit_seconds( active_resource['price_details']['time_unit']) start_time = active_resource['start'] price_per_second = utils.cdecimal(unit_price * quantity / price_seconds, q='0.00000000000001', rounding='ROUND_DOWN') usage_cycle.add_dynamic_resource(price_per_second=price_per_second, price_per_unit=unit_price * quantity, seconds_per_unit=price_seconds, start_datetime=start_time) # Calculate modifiers prices also for modifier in active_resource.get('modifiers', []): mod_time_unit = modifier['price_details']['time_unit'] if mod_time_unit == 'bc': usage_cycle.add_fixed_price(Decimal(modifier['price'])) else: mod_price_seconds = utils.time_unit_seconds(mod_time_unit) mod_unit_price = modifier['price_details']['unit_price'] mod_price_per_second = utils.cdecimal( mod_unit_price / mod_price_seconds, q='0.00000000000001', rounding='ROUND_DOWN') usage_cycle.add_dynamic_resource( price_per_second=mod_price_per_second, price_per_unit=mod_unit_price, seconds_per_unit=mod_price_seconds, start_datetime=start_time) # add price per second estimated_usage = EstimatedUsage() estimated_usage.add_usage_cycle(usage_cycle) return estimated_usage
def get_estimated_usage(self, service: Service, usage_settings: UsageSettings) -> EstimatedUsage: return EstimatedUsage()