def get_initial(self): data = get_billing_plan_mapping(self.request, self.kwargs['id']) projects = {} for project in get_projects(self.request): projects[str(project.id)] = project plans = {} for plan in get_plans(self.request): plans[str(plan['id'])] = plan # insert required data project = projects.get(str(data['user'])) data['user_name'] = project.name if project else '!ERR: ' + data['user'] plan = plans.get(str(data['plan_id'])) if plan: data['plan_name'] = plan['name'] data['service_type'] = plan['service_type'] else: data['plan_name'] = '!ERR: plan: ' + str(plan_map['id']) data['service_type'] = None disc_map = data.get('discount_mapping', None) if disc_map: data['discount'] = disc_map['discount_id'] data['apply_type'] = disc_map['apply_type'] data['apply_interval'] = disc_map['apply_interval'] data['apply_amt'] = disc_map['apply_amt'] return data
def get_context_data(self, **kwargs): context = super(UserInvoiceDetailsView, self).get_context_data(**kwargs) id = self.kwargs['invoice_id'] #Commented the below line to make display in sync with admin >> billing >> invoice display #context['invoice'] = get_invoice(self.request, id, verbose=False) context['invoice'] = get_invoice(self.request, id, verbose=True) #context['invoice'] = get_user_inv_details(self.request, id, verbose=True) plans = {} for plan in get_plans(self.request): plans[plan['id']] = plan #Same as Admin/Invoice Details View page discounts = {} for discount in get_discounts(self.request): discounts[discount['id']] = discount for item in context['invoice']['items']: #item['plan'] = plans.get(item['plan_id'])['name'] item['bill_start_date'] = item['bill_start_date'].split(' ')[0] item['bill_end_date'] = item['bill_end_date'].split(' ')[0] disc = discounts.get(item.get('discount_id')) if disc: if disc['discount_type_code'] == 'percentage': item['discount'] = str(disc.get('amt')) + '%' elif disc['discount_type_code'] == 'override': item['discount'] = '$' + str(disc.get('amt')) else: item['discount'] = '-' return context
def get_data(self): data = get_discount_mappings(self.request) projects = dict([(str(p.id), p) for p in get_projects(self.request)]) disc_types = dict([(str(dt['id']), dt) for dt in get_discount_types(self.request)]) discounts = dict([(str(d['id']), d) for d in get_discounts(self.request)]) plans = dict([(str(p['id']), p) for p in get_plans(self.request)]) plan_mappings = dict([(str(pm['id']), pm) for pm in get_billing_plan_mappings(self.request)]) for item in data: project = projects.get(str(item['user'])) item['user_name'] = project.name if project else '!ERR: ' + item['user'] if item['map_object'] == 'user_plan': plan_map = plan_mappings.get(item['ref_id']) if plan_map: plan = plans.get(str(plan_map['plan_id'])) item['plan_name'] = plan['name'] if plan else '!ERR: plan: ' + str(plan_map['id']) else: '!ERR: plan mapping: ' + item['ref_id'] disc = discounts.get(str(item['discount_id'])) disc_type = disc_types.get(str(item['discount_type_id'])) item['discount_type'] = disc_type['name'] if disc_type else '!ERR: ' + str(item['discount_type_id']) item['discount_amt'] = str(disc['amt']) + '%' if disc else '!ERR: ' + str(item['discount_id']) item['apply_type'] = apply_intervals.get(item['apply_type']) or '!ERR: ' + item['apply_type'] if item['apply_amt']: item['discount_amt'] = item['apply_amt'] return data
def get_plans_data(self): try: data = get_plans(self.request, verbose=True) for item in data: item['rate'] = "%.2f" % (item.get('rate') or 0.0) item['setup_fee'] = "%.2f" % (item.get('setup_fee') or 0.00) return data except Exception: self._has_more = False error_message = _('Unable to get plans') exceptions.handle(self.request, error_message) return []
def get_avbl_user_plans(req, verbose=False): """Get all the plans under the current billing type""" user_id = req.user.tenant_id plan_list = get_plans(req) #user_sub_plan_list = get_user_sub_plans(req) user_billing_details = get_user_billing_type(req) if user_billing_details: #Billing type code should already be present for a user billing_type_code = user_billing_details[0]['type_code'] #Display only additional services for rab billing type if billing_type_code == 'rab': #print "Entering rab section" rab_billing_typeId = user_billing_details[0]['billing_type'] data = filter( lambda x: (x['billing_type'] == None and x['service_name'] != 'SetupFee'), plan_list) if billing_type_code == 'payg': #print "Entering payg section" payg_billing_typeId = user_billing_details[0]['billing_type'] data = filter( lambda x: ((x['billing_type'] == payg_billing_typeId or x['billing_type'] == None) and (x['service_name'] != 'SetupFee')), plan_list) for item in data: if billing_type_code == 'payg': item['billing_type'] = 'Usage Based Billing' elif billing_type_code == 'rab': item['billing_type'] = 'Resource Allocation Based Billing' else: item['billing_type'] = 'NA' else: data = [] """ for item in data: billing_type_id = item['billing_type'] """ # filteredList = filter(lambda x: (x['billing_type'] == 1 and x['name'] not in keyVal2List), plan_list) return data
def populate_image_id_choices(self, request, context): choices = [] images = image_utils.get_available_images(request, context.get('project_id'), self._images_cache) # prepare account plan marks plans = dict([(plan['id'], plan) for plan in get_plans(request)]) marks = [] for plan_map in get_project_plan_mappings(request, context.get('project_id')): plan = plans.get(plan_map['plan_id']) if plan: mark = plan.get('metadata_mark') if mark: marks.append(mark.upper()) for image in images: # filter out image by `astute_plan_mark` if marks: plan_mark = image.properties.get('astute_plan_mark') if plan_mark and not plan_mark.upper() in marks: continue image.bytes = getattr(image, 'virtual_size', None) or image.size image.volume_size = max(image.min_disk, functions.bytes_to_gigabytes(image.bytes)) choices.append((image.id, image)) if context.get('image_id') == image.id and \ 'volume_size' not in context: context['volume_size'] = image.volume_size if choices: #choices.sort(key=lambda c: c[1].name) #Converting to lowercase for sorting in alphabetical order choices.sort(key=lambda c: c[1].name.lower()) choices.insert(0, ("", _("Select Image"))) else: choices.insert(0, ("", _("No images available"))) return choices
def get_initial(self): data = get_discount_mapping(self.request, self.kwargs['id']) projects = {} for project in get_projects(self.request): projects[str(project.id)] = project plans = {} for plan in get_plans(self.request): plans[str(plan['id'])] = plan plan_mappings = {} for plan_map in get_billing_plan_mappings(self.request): plan_mappings[str(plan_map['id'])] = plan_map # insert required data project = projects.get(str(data['user'])) data['user_id'] = data['user'] data['user_name'] = project.name if project else '!ERR: ' + data['user'] if data['map_object'] == 'user_plan': plan_map = plan_mappings.get(data['ref_id']) if plan_map: data['plan_mapping_id'] = plan_map['id'] plan = plans.get(str(plan_map['plan_id'])) data['plan_name'] = plan['name'] if plan else '!ERR: plan: ' + str(plan_map['id']) else: '!ERR: plan mapping: ' + data['ref_id'] return data
def export_as_csv(request, id): invoices = [] # # get list of invoice(s) for export # # export all (filtered) invoices account = request.session.get('admin_billing_invoices_filter_account') period_from = request.session.get( 'admin_billing_invoices_filter_period_from') period_to = request.session.get('admin_billing_invoices_filter_period_to') if id == '*': invoices = get_invoices(request, account=account, period_from=period_from, period_to=period_to) else: # export single invoice invoices.append(get_invoice(request, id)) # prepare required strings timestamp = datetime.now().strftime("%Y%m%d%H%M%S") seq_number = request.session.get( 'admin_billing_invoices_export_csv_seq_number') or 0 if seq_number >= 9999: seq_number = 0 seq_number += 1 request.session[ 'admin_billing_invoices_export_csv_seq_number'] = seq_number request.session.modified = True seq_number = "{:0>4}".format(seq_number) # prepare required data services = dict([(s['id'], s) for s in get_service_types(request)]) accounts = dict([(a['user'], a) for a in get_billing_type_mappings(request)]) plan_maps = dict([(pm['user'], pm) for pm in get_billing_plan_mappings(request)]) plans = dict([(p['id'], p) for p in get_plans(request)]) # # prepare file content # content = "" # title content += "Usage & Billing Report Reference numver: SGM1%s\n" % timestamp[ 0:6] content += "Period: %s to %s\n" % (period_from, period_to) content += "\n" # table header content += ",".join([ "Customer Account ID", "Service ID", "Customer Name", "Billing Period (From)", "Billing Period (To)", "Item Code", "Product Description", "Bill Description", "Unit", "Quantity", "Service Start Date", "Service Termination Date", "Quantity (after proration) or Usage cut off at 25th", "Unit Price according to product catalogue (SGD)", "Mothly Total before discount (SGD)", "Discount", "Billing Amount in M1 bill (SGD)", "\n" ]) #content += "\n" # table content for invoice in invoices: content += "\n" account = accounts.get(invoice['user']) for item in invoice['items']: if (item.get('qty') == 0) or (item.get('rate') == 0): discount = '0%' else: discount = str( round( item.get('discount_amt') / (item.get('qty') * item.get('rate')) * 100)) + '%' plan_map = plan_maps.get(invoice['user']) plan = plans.get(item['plan_id']) #yad: delete # plan = plans.get(plan_map and plan_map['plan_id']) service = services.get(plan and plan['service_type']) content += ",".join([ '"%s"' % (account and account['extra_fields'].get('crm_account_num') or '#%s' % invoice.get('user')), '"%s"' % (account and account['extra_fields'].get('service_id') or '#%s' % invoice.get('user')), '"%s"' % (account and account['extra_fields'].get('customer_name') or '#%s' % invoice.get('user')), invoice.get('inv_from') or '-', invoice.get('inv_to') or '-', plan and plan.get('code') or '-', #yad: delete # service and service.get('code') or '-', '"%s"' % plan and plan.get('description'), #yad: delete # '"%s"' % item.get('description'), item.get('description'), service and service.get('units') or '-', str(item.get('qty')) or '-', #yad: delete # plan_maps and plan_maps.get('created_on') or '-', # plan_maps and plan_maps.get('inactive_on') or '-', plan_map and plan_map.get('created_on') or '-', plan_map and plan_map.get('inactive_on') or '-', str(item.get('qty')) or '-', '$' + str(item.get('rate')) or '-', '$' + str(item.get('qty') * item.get('rate')) or '-', discount, '$' + str(item.get('total_amt')) or '-', '\n' ]) # # return data # response = HttpResponse(content, content_type='application/force-download') response[ 'Content-Disposition'] = 'attachment; filename=HDS_CHARGINGCDR_%s_%s.csv' % ( timestamp[:-2], seq_number) return response
def export_as_dat(request, id): invoices = [] # # get list of invoice(s) for export # account = request.session.get('admin_billing_invoices_filter_account') period_from = request.session.get( 'admin_billing_invoices_filter_period_from') period_to = request.session.get('admin_billing_invoices_filter_period_to') if id == '*': # export all (filtered) invoices invoices = get_invoices(request, account=account, period_from=period_from, period_to=period_to) #yad: delete #, # verbose=True) else: # export single invoice invoices.append(get_invoice(request, id)) # prepare required data accounts = {} for account in get_billing_type_mappings(request): accounts[account['user']] = account plans = {} for plan in get_plans(request): plans[plan['id']] = plan # prepare required strings timestamp = datetime.now().strftime("%Y%m%d%H%M%S") seq_number = request.session.get( 'admin_billing_invoices_export_dat_seq_number') or 0 if seq_number >= 9999: seq_number = 0 seq_number += 1 request.session[ 'admin_billing_invoices_export_dat_seq_number'] = seq_number request.session.modified = True seq_number = "{:0>4}".format(seq_number) # # prepare file content # content = "" # add header content += "{:3}{:14}{:4}{:7}{:16}{:5}{:3}{:8}{:4}\n".format( 'HDR', timestamp, '0', '0', 'HDS', 'V1.0', '', '', '') # add detail records records_sequence = 0 for invoice in invoices: account = accounts.get(invoice['user']) service_id = (account.get('extra_fields') or {}).get('service_id') or '' for item in invoice['items']: records_sequence += 1 plan = plans.get(item['plan_id']) content += "{:3}{:32}{:21}{:21}{:5}{:5}{:5}{:5}{:5}{:23}{:14}{:8}{:8}{:3}{:2}{:{lenght}}{:{lenght}}{:8}{:8}{:10}{:5}{:1}\n".format( 'SUB', '{:0>12}_{}'.format( invoice['id'], invoice['inv_date'].replace( '-', '').replace(' ', '').replace(':', ''))[:32], '6596800000', service_id[:21], #yad: delete # invoice['inv_code'][:21], '221', '5712', '10', '10', '238', '6596800000', datetime.now().strftime("%Y%m%d%H%M%S"), #yad: delete # timestamp, '1', str(item['total_amt'] * 100).split('.')[0][:8], #yad: delete # str(int(float(item['total_amt']) * 100))[:8], # ??? #yad: delete # int(float(invoice['total_amt']) * 100), # ??? '0', '20', plan and plan.get('description').encode('utf8')[:50], plan and plan.get('description').encode('utf8')[:50], #yad: delete # item['description'].encode('utf8'), # item['description'].encode('utf8'), item['bill_start_date'].split(' ')[0].replace('-', ''), item['bill_end_date'].split(' ')[0].replace('-', ''), #yad: delete # invoice['notes'], # invoice['notes'], # invoice['inv_from'].split(' ')[0].replace('-', ''), # invoice['inv_to'].split(' ')[0].replace('-', ''), '', '', '', lenght=50 + len(plan.get('description').encode('utf8')) - len(plan.get('description'))) # add trailer content += "{:3}{:14}{:4}{:7}{:16}{:5}{:8}{:10}{:10}{:42}\n".format( 'TRL', timestamp, '0', str(records_sequence)[:7], 'HDS', 'V1.0', seq_number[:8], '', '', '') # # return data # response = HttpResponse(content, content_type='application/force-download') response[ 'Content-Disposition'] = 'attachment; filename=HDS_CHARGINGCDR_%s_%s.dat' % ( timestamp[:-2], seq_number) return response
def get_context_data(self, **kwargs): context = super(CreateBillingPlanMappingView, self).get_context_data(**kwargs) context['submit_url'] = reverse(self.submit_url) context['plans'] = get_plans(self.request) return context