def update(self, force_add=False): req = self.req name = get_string(req.GET.get('n')) order = get_integer(req.GET.get('i')) is_required = get_integer(req.GET.get('ir')) group_help = self.get_str('gh', False, '-', 2000) metric = self.get_str('mt', max_len=255) cr = self.get_bool('cr', False, False) old = self.get_single_ext() if not name: raise RequestProcessException(_('please enter name')) if not order: raise RequestProcessException(_('please enter view order')) if force_add: if old or CustomOptionGroup.objects.filter( name__iexact=name).exists(): raise RequestProcessException(_('item exists')) if not old: old = CustomOptionGroup() old.name = name old.view_order = order old.is_required = is_required == 1 old.group_help = group_help old.can_recharge = cr old.metric = metric old.save() return old.ext
def update(self, force_add=False): s = self.get_single_ext(True) s_name = self.get_str('n', True, max_len=255) s_description = self.get_str('d', True, 500) group_type = self.get_int('gt', False, 1) is_shown = self.get_int('isn', False) sgm = ServiceGroupManagement(self.req) sgm.pk_name = 'gn' service_group = sgm.get_single_pk(False) try: s.description = s_description s.name = s_name s.group_type = group_type if is_shown: s.is_visible = True else: s.is_visible = False s.save() if not service_group: return s.pk if ServiceGroup.objects.filter(service=s.pk).exists(): sg = ServiceGroup.objects.get(service=s.pk) else: sg = ServiceGroup() sg.service = s sg.group = service_group sg.save() return s.pk except Exception as e: logger.error(e.message) raise RequestProcessException(e.message)
def get_send_history(self, raise_error=False): x = self.get_single_ext(True) if not x.fk_dedicated_invoice_send_history_invoice.exists(): if raise_error: raise RequestProcessException(_('no history found')) else: return DedicatedInvoiceSendHistory.objects.none() return DedicatedInvoiceSendHistory.objects.filter(invoice=x.pk)
def update(self, force_add=False): req = self.store name = get_string(req.get('n')) old = self.get_single_ext() if not name: raise RequestProcessException(_('please enter name')) if not old: old = DedicatedInvoiceType() old.name = name old.save()
def update(self, force_add=False): post = self.store x = self.get_single_ext() px = self.__process_params__() # print px total_price = 0 service_list = [] for ix in px.keys(): s = DedicatedInvoiceService() p = get_integer(px.get(ix).get('mx')) * get_integer( px.get(ix).get('px')) if p < 1: self.error(_('please enter price'), True) total_price += p s.period = px.get(ix).get('mx') s.price = px.get(ix).get('px') sm = DedicateServiceManager(self.req, store=self.store) sm.pk_name = 'sx' + ix s.service = sm.get_single_ext(True) service_list.append(s) description = get_string(post.get('d')) user = validate_user(get_integer(post.get('u'))) discount = get_integer(self.store.get('dp')) total_price = total_price - discount if total_price - discount < 1: self.error(_('please correct the discount price'), True) if not description: description = '-' if not user: raise RequestProcessException(_('invalid user')) if not x: x = DedicatedInvoice() type_m = DedicatedInvoiceTypeManager(self.req, target=DedicatedInvoiceType, store=self.store) type_m.pk_name = 'ity' invoice_type_ = type_m.get_single_ext(True) x.user = user x.send_type = None x.description = description x.price = total_price x.tax = total_price * float(read_config('invoice_tax', 0.09)) x.discount = discount x.send_date = None x.invoice_type = invoice_type_ x.creator_id = self.requester.pk x.save() for s in service_list: s.invoice = x DedicatedInvoiceService.objects.bulk_create(service_list) return x
def create_invoice(self): d = self.get_single_ext(True) store = self.store if d.system_invoice_number > 0: raise RequestProcessException(_('invoice generation done before')) bank_ref = get_string(store.get('rf')) comment = get_string(store.get('c')) pay_time = parse_date_from_str(store.get('pd')) if not bank_ref: raise RequestProcessException(_('please enter bank ref code')) if not comment: comment = '' if not pay_time: pay_time = datetime.today() i = Invoice() ins = InvoiceService() ins.content_object = d ins.service_type = 5 ins.save() i.comment = comment i.create_time = datetime.today() i.debit_price = 0 i.dynamic_discount = 0 i.extra_data = 0 i.is_paid = True i.paid_online = False i.pay_time = pay_time i.price = d.price i.ref_number = bank_ref i.service = ins i.service_text = _('dedicate service') i.user_id = d.user_id i.comment = self.requester.username i.save() d.system_invoice_number = i.pk d.save() self.__update_state__(d, 7) return i.pk
def update(self, force_add=False): user = self.get_target_user() fv = FloatValidator(self.req, store=self.store) fv.validate() service_time = self.get_int('iMon', False, 1) if service_time < 1: service_time = 1 # Negative Time Validation! res = fv.get_all() if not len(res[0]): raise RequestProcessException(_('nothing to calculate')) name = get_string(self.store.get('t_name')) if not name: name = fv.service.name ut = None if force_add: ut = self.get_single_ext() if not ut: ut = UserFloatTemplate() ut.user = user ut.service_id = fv.service.pk ut.create_date = now() ut.service_period = service_time ut.name = name ut.final_price = res[2] ut.is_system = not force_add # Used to determine the status of template! this is not a system template ut.save() # option_list = [] ut.fk_float_template_template.all().delete() for r in res[0]: if not hasattr(r.option, 'group'): continue # ft = FloatTemplate.objects.filter(option=r.option.pk).first() # if not ft: ft = FloatTemplate() ft.option_id = r.option.pk ft.price = r.price ft.total_price = r.total_price ft.value = r.value ft.template_id = ut.pk # option_list.append(ft) ft.save() # FloatTemplate.objects.bulk_create(option_list) utx = AssignedUserTemplate.objects.filter(user=user.pk, template=ut.pk).first() if not utx: utx = AssignedUserTemplate() utx.user = user utx.template = ut utx.save() return ut
def validate(self): sm = BasicServiceManager(self.req) sm.pk_name = 's' sm.set_post() options = self.req.POST.keys() option_list = [] for o in options: x = get_integer(o) if x: option_list.append(x) try: service = sm.get_single_ext(True) self.service = service all_service_options = service.fk_service_options_service.values_list( 'option__pk', flat=True) all_options = CustomOption.objects.filter( pk__in=all_service_options, group__is_required=True, group__is_deleted=False) errors = {} group_passed = [] current_options = [] current_user = self.get_target_user() state = UserServiceStatus(current_user.pk) if state.can_recharge_float: current_options = UserServiceState.objects.filter( user=current_user.pk).values_list('option__group__pk', flat=True) for a in all_options: if a.pk not in option_list: if a.group_id not in group_passed: if a.group_id not in current_options: errors.update({a.group_id: a.group.name}) else: group_passed.append(a.group_id) if a.group_id in errors: del errors[a.group_id] if len(errors) > 0: raise RequestProcessException( _('please select one option : ') + errors.values()[0]) except RequestProcessException as e: raise e
def calculate_formula_for_service(service_id, selected_option={}, service_period=1, is_recharge=False): if not BasicService.objects.filter(pk=service_id).exists(): return 0 bs = BasicService.objects.get(pk=service_id) options = bs.fk_service_options_service.filter( option_id__in=selected_option.keys()).order_by( '-option__group__view_order') selected_vars = options.values_list('option__var_name', flat=True) service_options = bs.fk_service_options_service.values_list('option__pk', flat=True) selected_package = bs.fk_service_options_service.filter( option__package__gt=0, option__in=selected_option.keys()) sys_vars = {} option_price = [] final_price = 0.0 base_price = float(read_config('service_base_price', 600)) base_ratio = bs.base_ratio if selected_package.exists(): # sys_vars['selected_package'] = selected_package package_amount = selected_package.aggregate( amount=Sum('option__package')).get('amount', 0) / 1024 # sys_vars['selected_amount'] = package_amount else: package_amount = 0 # sys_vars['selected_amount'] = package_amount service_index = bs.service_index service_price = 0 for o in options: option = o.option if option.is_custom_value: # Bug fix for custom values m = get_integer(selected_option.get( option.pk)) # Skip if selected value is 0 if m < option.custom_value_min and m != -1: raise RequestProcessException( _('please select more value for') + ' ' + option.name) if m == 0: sys_vars[option.var_name] = None continue sys_vars[option.var_name] = m exec option.var_name + "=" + str(m) else: sys_vars[option.var_name] = o.option_id exec option.var_name + '=' + str(o.option_id) try: exec bs.formula.formula except Exception as e: print e.message or e.args for o in options: option = o.option try: single_option_price = eval(option.var_name + '_price') except NameError: single_option_price = 0 try: total_option_price = eval(option.var_name + '_total') except NameError: total_option_price = 0 if option.is_custom_value: option_value = eval(option.var_name) else: option_value = 0 option_price.append( OptionPrice(option, single_option_price, total_option_price, option_value)) # final_price += total_option_price # while counter < 3: # sys_vars['current_round'] = counter # for o in options: # option = o.option # if option.is_custom_value: # Bug fix for custom values # m = get_integer(selected_option.get(option.pk)) # Skip if selected value is 0 # if m < option.custom_value_min and m != -1: # raise RequestProcessException(_('please select more value for') + ' ' + option.name) # if m == 0: # sys_vars[option.var_name] = None # continue # sys_vars[option.var_name] = m # exec option.var_name+"="+str(m) # else: # sys_vars[option.var_name] = o.option_id # exec option.var_name+'='+str(o.option_id) # rendered = render_formula(bs.formula.formula, # selected_vars, bs.pk, service_options, sys_vars, # o.option_id).strip() # sys_vars[option.var_name] = None # exec option.var_name+"_price=0" # if rendered: # if not rendered.strip(): # rendered = "0" # code = parser.expr(rendered).compile() # res = int(eval(code)) # res = math.ceil(res/100.0)*100 # Round up the price to 00 # if option.is_custom_value and counter == 2: # only run when round counter was 2. fix base price bug # value = get_integer(selected_option.get(option.pk)) # price = res * value # else: # price = res # value = 0 # if counter == 2: # If Round Counter was 2 check for min and max. this will fix base price bug # if price > o.option.max_value > 0: # price = o.option.max_value # res = price # elif price < o.option.min_value: # price = o.option.min_value # res = price # exec option.var_name+"_price="+str(price) # # if counter == 0: # if price > 0: # base_price = price # sys_vars['base_price'] = base_price # elif counter == 1: # service_price += price # sys_vars['service_price'] = service_price # else: # if is_recharge and selected_option.get(option.pk) == -1: # continue # op = OptionPrice(o.option, res, price, value) # option_price.append(op) # final_price += price # else: # op = OptionPrice(o.option, 0, 0, 0) # option_price.append(op) # counter += 1 # # print time.time() - x if not is_recharge: option_price.append( OptionPrice(bs, service_price, service_price, bs.pk)) return option_price, final_price, final_price * service_period # return option_price, final_price, final_price return option_price, final_price, final_price