def get_temp_rate_for_user(self, user_id): if not user_id: self.stderr.write('Invalid User ID') return print('Rate Calc For %s : %s%s' % (user_id[0], TempChargeManagement.calculate_temp_rate( user_id[0]), '%'))
def update_service_listener(sender, **kwargs): invoice = kwargs.get('invoice') if invoice is None: return if invoice.service.service_type == 1: from CRM.Core.TempChargeManagement import TempChargeManagement # Prevent Dependency Loop rate = TempChargeManagement.calculate_temp_rate(invoice.user_id) TempChargeManagement.update_state( invoice.user_id, 0, int(invoice.extra_data * 30 * rate) / 100, True, True) service = invoice.service.content_object current_service = UserCurrentService.objects.filter( user=invoice.user_id).first() res = ChargeBasicService( 1, invoice.user.fk_ibs_user_info_user.get().ibs_uid).update( current_service=current_service, service=service, days=invoice.extra_data, request=kwargs.get('request')) update_charge_state(invoice, not res.is_error, res.message) res2 = ChargePackage( 1, invoice.user.fk_ibs_user_info_user.get().ibs_uid).update( service=service, current_service=current_service, period=invoice.extra_data, amount=service.initial_package) update_charge_state(invoice, not res2.is_error, res2.message)
def update_package_listener(sender, **kwargs): invoice = kwargs.get('invoice') if invoice is None: return if invoice.service.service_type == 2: from CRM.Core.TempChargeManagement import TempChargeManagement # Prevent Dependency Loop try: rate = TempChargeManagement.calculate_temp_rate(invoice.user_id) TempChargeManagement.update_state( invoice.user_id, (int(invoice.service.content_object.amount) * rate) / 100, 0, False, True) except Exception as e: logger.error(e.args or e.message) current_service = UserCurrentService.objects.filter( user=invoice.user_id).first() package = invoice.service.content_object res = ChargePackage( 2, invoice.user.fk_ibs_user_info_user.get().ibs_uid).update( current_service=current_service, package=package) update_charge_state(invoice, not res.is_error, res.message)
def update(self, **kwargs): service = kwargs.get('service') current_service = kwargs.get('current_service') extra_data = kwargs.get('extra') if not isinstance(service, UserFloatTemplate): logger.error('UserFloatTemplate Type Expected Got %s' % type(service)) return self._error(1400, _('invalid float template type'), True) if not isinstance(current_service, UserCurrentService): logger.error('UserCurrentService Type Expected Got %s' % type(service)) return self._error(1401, _('invalid user current service type'), True) if not isinstance(extra_data, int) and not isinstance( extra_data, long): logger.error('int Type Expected got charge Got %s' % type(int)) return self._error(1402, _('invalid charge month type'), True) options = service.fk_float_template_template.all() if UserServiceState.objects.filter( option__var_name='transfer', user=current_service.user_id).first(): has_transfer = True else: has_transfer = False user_package = 0 ibs_service = 0 is_unlimited = 1 ip_pool = None ip_static = 0 from CRM.Core.ServiceManager import Utils from CRM.Core.TempChargeManagement import TempChargeManagement # Prevent Dependency Loop is_new_service = Utils.is_new_service(current_service.user_id, service) if extra_data > 0 and is_new_service: UserServiceState.objects.filter( user=current_service.user_id).delete() for o in options: option = o.option user_service_state = UserServiceState.objects.filter( option=option.pk, user=current_service.user_id).first() if not user_service_state: user_service_state = UserServiceState() user_service_state.option_id = option.pk user_service_state.value = o.value user_service_state.user_id = current_service.user_id user_service_state.current_value = o.value user_service_state.purchase_date = now() try: user_service_state.save() except Exception as e: logger.error(e.message or e.args) if option.var_name == 'package' or option.package > 0: user_package += o.value elif option.var_name == 'unlimited' or option.group_type == 2: is_unlimited = 2 elif option.pool_id is not None: ip_pool = option.pool elif option.fk_custom_option_group_map_option.filter( service=service.service.pk).exists(): ibs_service = option.fk_custom_option_group_map_option.filter( service=service.service.pk).all() elif option.var_name == 'ip': ip_static = True if not ibs_service: ibs_service = service.service.fk_basic_service_default_group_service.filter( group_type=is_unlimited).first().group else: ibs_service = ibs_service.filter( group__group_type=is_unlimited).first().group temp_package = 0 temp_days = 0 rate = TempChargeManagement.calculate_temp_rate( current_service.user_id) if user_package > 0: if extra_data == 0: tmp_can_transfer = True elif has_transfer and not is_new_service: tmp_can_transfer = True else: tmp_can_transfer = False fpd = FloatPackageDiscount.objects.filter( charge_amount=user_package).first() to_charge = user_package * 1024 * service.service_period if fpd is not None: to_charge += (fpd.extra_charge * (user_package * 1024)) / 100 pdu = FloatPackageDiscountUsage() pdu.user_id = current_service.user_id pdu.package_discount_id = fpd.pk pdu.save() if not self._update_package(int(to_charge), not tmp_can_transfer): logger.error('unable to charge service') return self._error(1403, _('unable to charge service'), True) temp_package = (int(user_package * 1024) * rate) / 100 if extra_data > 0: if ibs_service: charge_days = service.service_period * 30 if current_service.is_active: ds = FloatDiscount.objects.filter( charge_month=service.service_period).first() if ds: extra_days = ds.extra_charge charge_days += extra_days extra_package = (ds.extra_package * (int(user_package) * 1024)) / 100 if not self._update_package(int(extra_package)): logger.error( 'unable to add package discount for user %s ' % self.user_id) else: charge_days = int(read_config('service_test_time', 2)) user_tower = UserTower.objects.filter( user=current_service.user_id).first() if user_tower: if user_tower.tower.has_test: charge_days = int( read_config('service_tower_test', 30)) current_service.is_active = True if not self._update_user_group(ibs_service.ibs_name): logger.error('unable to change user group : %s' % self.user_id) return self._error(1410, _('unable to change user group'), True) if not self._update_expire_date(charge_days, is_new_service): logger.error('unable to change expire date : %s' % self.user_id) return self._error(1411, _('unable to change expire date'), True) temp_days = (int((service.service_period * 30)) * rate) / 100 ua = UserActiveTemplate.objects.filter( user=current_service.user_id).first() if not ua: ua = UserActiveTemplate() ua.user_id = current_service.user_id ua.template_id = service.pk ua.save() current_service.is_float = True current_service.service = ibs_service current_service.service_property = None current_service.save() # self._KILL_REQUEST = kwargs.get('request') # self._kill_user(current_service.user_id) if temp_package > 0 or temp_days > 0 and current_service.is_active: # Prevent Extra DB Hit TempChargeManagement.update_state( current_service.user_id, temp_package, temp_days, temp_days > 0 and temp_package > 0, True) else: logger.warning('Temp Update Was Skipped for %s' % self.user_id) if ip_static: configure_static_ip_address(current_service.user_id, service.service_period) if ip_pool: x0 = assign_ip_pool(user_id=current_service.user_id, pool_name=ip_pool.ibs_name) return self._error(0, _('operation done'))