def post(self): order_id = self.get_argument('order_id') tpl_id = self.get_argument('tpl_id') tpl = self.db.query(models.TrPrintTemplate).get(tpl_id) note = self.db.query( models.TrCustomerNote).filter_by(order_id=order_id).first() print_params = dict(title=u'宽带票据', note_id=note.note_id, pay_type=note.pay_type, pay_date=note.pay_date, expire_date=note.expire_date, customer_name=note.customer_cname, install_address=note.install_address, product_name=note.product_name, product_price=utils.fen2yuan(note.fee_price), remark=note.remark, account_number=note.account_number, mobile=note.mobile, product_num=note.order_num, fee_total=utils.fen2yuan(note.fee_total), charge_values=utils.safeunicode( note.charge_values), receiver=note.operator_name, customer_sign='') print_func = tpl.tpl_content.format(**print_params) self.render_json(func=print_func)
def get(self): order_id = self.get_argument('order_id') form = customer_forms.customer_order_check_form() order = self.db.query(models.TrCustomerOrder).get(order_id) form.order_id.set_value(order_id) form.order_fee.set_value(utils.fen2yuan(order.order_fee)) form.actual_fee.set_value(utils.fen2yuan(order.actual_fee)) return self.render('base_form.html', form=form)
def get(self): charge_code = self.get_argument('charge_code') form = charges_forms.charge_update_form() charge = self.db.query(models.TrCharges).get(charge_code) form.fill(charge) form.charge_value.set_value(utils.fen2yuan(charge.charge_value)) self.render('base_form.html', form=form)
def get(self): product_id = self.get_argument('product_id') pools = [('', '')] + [(p.pool_name, p.pool_name) for p in self.db.query(models.TrAddrPool)] charges = [(p.charge_code, p.charge_name) for p in self.db.query(models.TrCharges)] pcharges = self.db.query( models.TrProductCharges).filter_by(product_id=product_id) product = self.db.query(models.TrProduct).get(product_id) form = product_forms.product_update_form(product.product_policy, charges=charges, pools=pools) form.fill(product) form.product_charges.set_value([c.charge_code for c in pcharges]) form.addr_pool.set_value(self.get_addr_pool(product_id)) form.product_policy_name.set_value( product_forms.product_policy[product.product_policy]) form.fee_times.set_value(utils.sec2hour(product.fee_times)) form.fee_flows.set_value(utils.kb2gb(product.fee_flows)) form.input_max_limit.set_value(utils.bps2mbps(product.input_max_limit)) form.output_max_limit.set_value( utils.bps2mbps(product.output_max_limit)) form.fee_price.set_value(utils.fen2yuan(product.fee_price)) form.free_auth_uprate.set_value( utils.bps2mbps(product.free_auth_uprate)) form.free_auth_downrate.set_value( utils.bps2mbps(product.free_auth_downrate)) attrs = self.db.query(models.TrProductAttr).filter_by( product_id=product.id, attr_type=0) for attr in attrs: if hasattr(form, attr.attr_name): getattr(form, attr.attr_name).set_value(attr.attr_value) return self.render('product_form.html', form=form)
def event_ppmf_user_renew(self, account_number, product_id): with make_db(self.db) as db: try: account = db.query(models.TrAccount).get(account_number) product = db.query(models.TrProduct).get(product_id) if not product: logger.error(u'执行流量包月续费时,用户[%s]资费id[%s]不存在' % (account_number, product_id)) return if product.fee_price > account.balance: logger.error(u'执行流量包月续费时,用户[%s]余额不足' % account_number) return old_balance = account.balance old_flows = account.flow_length account.balance -= product.fee_price account.flow_length = product.fee_flows account.sync_ver = tools.gen_sync_ver() accept_log = models.TrAcceptLog() accept_log.id = utils.get_uuid() accept_log.accept_type = 'auto_renew' accept_log.accept_source = 'task' accept_log.account_number = account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = 'admin' accept_log.accept_desc = u'用户[%s]流量包月套餐续费, 续费前余额为(%s)元,流量为(%s)G,续费后余额为(%s)元,流量为(%s)G' % ( account_number, utils.fen2yuan(old_balance), utils.kb2gb(old_flows), utils.fen2yuan( account.balance), utils.kb2gb(account.flow_length)) accept_log.stat_year = accept_log.accept_time[0:4] accept_log.stat_month = accept_log.accept_time[0:7] accept_log.stat_day = accept_log.accept_time[0:10] accept_log.sync_ver = tools.gen_sync_ver() self.db.add(accept_log) self.db.commit() logger.info(u'用户[%s]流量包月套餐续费完成' % account_number, trace='event') except Exception as err: logger.exception(err) logger.error(u'用户[%s]流量包月套餐续费失败' % account_number, trace='event')
def get(self): pay_status = self.get_argument('pay_status', None) accept_type = self.get_argument('accept_type', None) stat_month = self.get_argument('stat_month', utils.get_currdate()[0:7]) _query = self.db.query( models.TrAcceptLog.operator_name, func.sum(models.TrCustomerOrder.actual_fee).label('fee_value'), func.count( models.TrCustomerOrder.order_id).label('dev_count')).filter( models.TrCustomer.customer_id == models.TrCustomerOrder.customer_id, models.TrAcceptLog.id == models.TrCustomerOrder.accept_id, models.TrCustomerOrder.stat_month == stat_month) if pay_status: _query = _query.filter( models.TrCustomerOrder.pay_status == int(pay_status)) if accept_type: _query = _query.filter( models.TrAcceptLog.accept_type == accept_type) _query = _query.group_by(models.TrAcceptLog.operator_name) _query = _query.order_by(models.TrAcceptLog.operator_name.asc()) userqry = _query.subquery() fee_total = self.db.query(func.sum(userqry.c.fee_value)).scalar() or 0 dev_total = self.db.query(func.sum(userqry.c.dev_count)).scalar() or 0 fee_data = [{ 'name': i.operator_name, 'y': float(utils.fen2yuan(i.fee_value)) } for i in _query] dev_data = [{ 'name': i.operator_name, 'y': float(i.dev_count) } for i in _query] self.render_json(code=0, msg='ok', fee_data=fee_data, dev_data=dev_data, dev_total=int(dev_total), fee_total=utils.fen2yuan(fee_total)) return
def calc_fee(self, product_id, months=0, days=0, old_expire=None, charge_fee=100): product = self.db.query(models.TrProduct).get(product_id) self_recharge_minfee = utils.yuan2fen( self.get_param_value('self_recharge_minfee', 100)) fee_value, expire_date = (None, None) if product.product_policy == PPDay: fee = decimal.Decimal(days) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(int(fee.to_integral_value())) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') expire_date = start_expire + datetime.timedelta(days=days) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == BODay: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') fee_value = utils.fen2yuan(product.fee_price) expire_date = start_expire + datetime.timedelta( days=product.fee_days) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy in (PPTimes, PPFlow): fee_value = charge_fee or self_recharge_minfee expire_date = MAX_EXPIRE_DATE elif product.product_policy in (BOTimes, BOFlows): fee_value = utils.fen2yuan(product.fee_price) expire_date = MAX_EXPIRE_DATE elif product.product_policy == PPMonth: fee = decimal.Decimal(months) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(int(fee.to_integral_value())) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') expire_date = utils.add_months(start_expire, int(months), days=0) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == BOMonth: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') fee_value = utils.fen2yuan(product.fee_price) expire_date = utils.add_months(start_expire, product.fee_months, days=0) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == PPMFlows: fee = decimal.Decimal(months) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(int(fee.to_integral_value())) expire_date = MAX_EXPIRE_DATE return (fee_value, expire_date)
def add_note(self, order_id): order = self.db.query(models.TrCustomerOrder).get(order_id) account = self.db.query(models.TrAccount).filter_by( account_number=order.account_number).first() product = self.db.query( models.TrProduct).filter_by(id=order.product_id).first() customer = self.db.query(models.TrCustomer).get(order.customer_id) opr = self.db.query(models.TrOperator).filter( models.TrCustomerOrder.accept_id == models.TrAcceptLog.id, models.TrAcceptLog.operator_name == models.TrOperator.operator_name, models.TrCustomerOrder.order_id == order_id).first() clogs = self.db.query(models.TrCharges.charge_name, models.TrCharges.charge_value).filter( models.TrCharges.charge_code == models.TrChargeLog.charge_code, models.TrChargeLog.order_id == order_id) charge_values = ', '.join([ '%s:%s' % (cname, utils.fen2yuan(cvalue)) for cname, cvalue in clogs ]) note = models.TrCustomerNote() note.id = utils.get_uuid() note.note_id = utils.gen_order_id() note.order_id = order.order_id note.customer_cname = customer.realname note.account_number = account.account_number note.mobile = customer.mobile note.install_address = account.install_address note.pay_type = u'现金' note.pay_date = utils.get_currdate() note.expire_date = account.expire_date note.order_num = 1 note.product_name = product.product_name note.fee_price = product.fee_price note.fee_total = order.actual_fee note.charge_values = charge_values note.operator_name = opr and opr.operator_desc or '' note.print_times = 0 note.remark = u'用户套餐续费' note.sync_ver = tools.gen_sync_ver() self.db.add(note) self.db.commit() return note
def batchadd(self, formdata, **kwargs): try: batch_bn = self.get_next_bn() batch_num = int(formdata.num) if batch_num > 100: raise ValueError(u'最大只能发行300个充值卡') if formdata.card_type not in ('product', 'balance', 'timelen', 'flowlen'): raise ValueError(u'无效的充值卡类型') for sn in range(1001, batch_num + 1001): vcard = models.TrValCard() vcard.card_code = '{0}{1}'.format(batch_bn, sn) vcard.card_type = formdata.card_type vcard.card_bn = batch_num vcard.card_pwd = self.aes.encrypt(tools.gen_random_num(7)) vcard.product_tag = '' vcard.credit = 0 vcard.flowlen = 0 vcard.timelen = 0 vcard.status = 0 vcard.expire_date = formdata.expire_date vcard.create_time = utils.get_currtime() if formdata.card_type == 'product': vcard.product_tag = formdata.product_tag if formdata.card_type == 'balance': vcard.credit = formdata.credit if formdata.card_type == 'flowlen': vcard.flowlen = formdata.flowlen if formdata.card_type == 'timelen': vcard.timelen = formdata.timelen vcard.fee_price = utils.fen2yuan(int(formdata.fee_price) * 100) self.db.add(vcard) self.add_oplog(u'批量发行充值卡:数量=%s' % batch_num) self.db.commit() return True except Exception as err: self.db.rollback() logger.exception(err) self.last_error = u'充值卡发行失败:%s' % utils.safeunicode(err) return False
def get(self): product_id = self.get_argument('product_id') form = product_ppmf_forms.ppmflow_product_update_form() product = self.db.query(models.TrProduct).get(product_id) form.fill(product) form.fee_flows.set_value(utils.kb2gb(product.fee_flows)) form.input_max_limit.set_value(utils.bps2mbps(product.input_max_limit)) form.output_max_limit.set_value( utils.bps2mbps(product.output_max_limit)) form.fee_price.set_value(utils.fen2yuan(product.fee_price)) form.free_auth_uprate.set_value( utils.bps2mbps(product.free_auth_uprate)) form.free_auth_downrate.set_value( utils.bps2mbps(product.free_auth_downrate)) attrs = self.db.query(models.TrProductAttr).filter_by( product_id=product.id, attr_type=0) for attr in attrs: if hasattr(form, attr.attr_name): getattr(form, attr.attr_name).set_value(attr.attr_value) return self.render('product_ppmf_form.html', form=form)
def post(self): form = customer_forms.customer_order_check_form() if not form.validates(source=self.get_params()): return self.render('base_form.html', form=form) audit_desc = utils.safeunicode(form.d.audit_desc) order = self.db.query(models.TrCustomerOrder).get(form.d.order_id) new_actual_fee = utils.yuan2fen(form.d.actual_fee) if order.actual_fee != new_actual_fee: audit_desc += u';调整费用 \xa5{} 为 \xa5{}'.format(utils.fen2yuan(order.actual_fee), form.d.actual_fee) checktime = utils.get_currtime() order.actual_fee = new_actual_fee order.pay_status = 2 order.check_time = checktime order.order_desc += u'; 对账审核时间:{}'.format(checktime) audit_desc += u'; 订单ID:{}'.format(order.order_id) order.stat_year = order.create_time[0:4] order.stat_month = order.create_time[0:7] order.stat_day = order.create_time[0:10] self.add_oplog(audit_desc) self.db.commit() self.redirect('/admin/customer/order')
def add(self, formdata, **kwargs): """用户开户 :param formdata: 用户开户参数表 :type formdata: dict formdata params: :param account_number: 用户账号 :type account_number: string :param billing_type: 计费模式,0 首次上线计费 1 立即计费 :type billing_type: int :param customer_id: 客户ID,16-32位字符串(可选) :type customer_id: string :param order_id: 交易ID,16-32位字符串(可选) :type order_id: string :param product_id: 订购资费ID :type product_id: string :param node_id: 用户区域ID :type node_id: string :param area_id: 用户社区ID(可选) :type area_id: string :param agency_id: 代理商ID(可选) :type agency_id: string :param realname: 用户姓名 :type realname: string :param password: 用户账号密码 :type password: string :param ip_address: 用户IP地址 :type ip_address: string :param idcard: 用户身份证号码(可选) :type idcard: string :param email: 用户电子邮箱(可选) :type email: string :param mobile: 用户手机号码(可选) :type mobile: string :param address: 用户地址(可选) :type address: string :param customer_desc: 用户描述备注(可选) :type customer_desc: string :param accept_source: 用户受理来源(可选) :type accept_source: string :param expire_date: 用户到期时间 yyyy-mm-dd :type expire_date: string :param months: 用户订购月数,预付费包月有效 :type days: int :param days: 用户订购天数,预付费包日有效 :type months: int :param fee_value: 交易费用 x.xx 元 :type fee_value: string :param giftflows: 赠送流量 x.xx GB :type giftflows: string :param giftdays: 赠送天数 :type giftdays: int :param charge_code: 收费项目编码 :type charge_code: string :param builder_name: 服务人员名 :type builder_name: string :param vcard_code: 充值卡 :type vcard_code: string :param vcard_pwd: 充值卡密码 :type vcard_pwd: string """ try: account_number = self.parse_arg(formdata, 'account_number', rule=rules.not_null) account_number = account_number.strip() formdata['account_number'] = account_number billing_type = int(formdata.get('billing_type', 1)) pay_status = int(formdata.get('pay_status', 1)) customer_id = self.parse_arg(formdata, 'customer_id', defval=utils.get_uuid()) order_id = self.parse_arg(formdata, 'order_id', defval=utils.get_uuid(), rule=rules.not_null) product_id = self.parse_arg(formdata, 'product_id', rule=rules.not_null) node_id = self.parse_arg(formdata, 'node_id', rule=rules.not_null) area_id = self.parse_arg(formdata, 'area_id', defval='') agency_id = self.parse_arg(formdata, 'agency_id', defval='') realname = self.parse_arg(formdata, 'realname', defval=account_number) realname = realname or account_number password = self.parse_arg(formdata, 'password', rule=rules.not_null) ip_address = self.parse_arg(formdata, 'ip_address', defval='') idcard = self.parse_arg(formdata, 'idcard', defval='') sex = self.parse_arg(formdata, 'sex', defval='1') age = self.parse_arg(formdata, 'age', defval='0') email = self.parse_arg(formdata, 'email', defval='') mobile = self.parse_arg(formdata, 'mobile', defval='') address = self.parse_arg(formdata, 'address', defval='') customer_desc = self.parse_arg(formdata, 'customer_desc', defval='') accept_source = self.parse_arg(formdata, 'accept_source', defval='console') expire_date = self.parse_arg(formdata, 'expire_date', rule=rules.is_date) months = self.parse_arg(formdata, 'months', defval='0', rule=rules.is_number) days = self.parse_arg(formdata, 'days', defval='0', rule=rules.is_number) fee_value = self.parse_arg(formdata, 'fee_value', rule=rules.is_rmb) giftflows = self.parse_arg(formdata, 'giftflows', defval='0', rule=rules.is_number3) giftdays = self.parse_arg(formdata, 'giftdays', defval='0', rule=rules.is_number) charge_code = self.parse_arg(formdata, 'charge_code', defval='') builder_name = self.parse_arg(formdata, 'builder_name', defval='') status = self.parse_arg(formdata, 'status', defval='1', rule=rules.is_number) wechat_oid = self.parse_arg(formdata, 'wechat_oid', defval='') vcard_code = self.parse_arg(formdata, 'vcard_code', defval='') vcard_pwd = self.parse_arg(formdata, 'vcard_pwd', defval='') if not self.check_data(formdata): return False product = self.db.query(models.TrProduct).get(product_id) vcard = None if vcard_code and vcard_pwd: vcard = self.db.query(models.TrValCard).get(vcard_code) if not self.check_vcard(vcard, vcard_pwd, product): return False if hasattr(self.operator, 'agency_id') and self.operator.agency_id is not None: agency_id = self.operator.agency_id customer = models.TrCustomer() customer.customer_id = customer_id customer.node_id = node_id customer.area_id = area_id customer.agency_id = agency_id customer.realname = utils.safeunicode(realname) customer.customer_name = account_number customer.password = md5(password.encode()).hexdigest() customer.idcard = idcard customer.sex = sex customer.age = age customer.email = email customer.mobile = mobile customer.address = address customer.create_time = utils.get_currtime() customer.update_time = utils.get_currtime() customer.email_active = 0 customer.mobile_active = 0 customer.active_code = utils.get_uuid() customer.customer_desc = customer_desc customer.wechat_oid = wechat_oid customer.sync_ver = tools.gen_sync_ver() self.db.add(customer) accept_log = models.TrAcceptLog() accept_log.id = utils.get_uuid() accept_log.accept_type = 'open' accept_log.accept_source = accept_source accept_log.account_number = account_number accept_log.accept_time = customer.create_time accept_log.operator_name = self.operator.operator_name accept_log.accept_desc = u'用户新开户:(%s)%s' % (customer.customer_name, customer.realname) accept_log.stat_year = accept_log.accept_time[0:4] accept_log.stat_month = accept_log.accept_time[0:7] accept_log.stat_day = accept_log.accept_time[0:10] accept_log.sync_ver = tools.gen_sync_ver() self.db.add(accept_log) order_fee = 0 balance = 0 expire_date = expire_date flow_length = 0 if product.product_policy == PPMonth: order_fee = decimal.Decimal( product.fee_price) * decimal.Decimal(months) order_fee = int(order_fee.to_integral_value()) if product.product_policy == PPDay: order_fee = decimal.Decimal( product.fee_price) * decimal.Decimal(days) order_fee = int(order_fee.to_integral_value()) elif product.product_policy == APMonth: order_fee = 0 elif product.product_policy in (BOMonth, BODay): order_fee = int(product.fee_price) elif product.product_policy == BOFlows: order_fee = int(product.fee_price) flow_length = int(product.fee_flows) order = models.TrCustomerOrder() order.order_id = order_id order.customer_id = customer.customer_id order.product_id = product.id order.account_number = account_number order.order_fee = order_fee order.actual_fee = utils.yuan2fen(fee_value) order.pay_status = pay_status order.accept_id = accept_log.id order.order_source = accept_log.accept_source order.create_time = customer.create_time order.order_desc = u'用户新开账号' order.stat_year = order.create_time[0:4] order.stat_month = order.create_time[0:7] order.stat_day = order.create_time[0:10] order.sync_ver = tools.gen_sync_ver() self.db.add(order) if vcard: vcard.status = 2 vcard.use_time = utils.get_currtime() vcard.customer_id = customer.customer_id if agency_id and pay_status == 1: agency = self.db.query(models.TrAgency).get(agency_id) if agency.amount < order.actual_fee: self.last_error = u'代理商预存款余额不足' return False agency_share = models.TrAgencyShare() agency_share.id = utils.get_uuid() agency_share.agency_id = agency_id agency_share.order_id = order.order_id agency_share.share_rate = agency.share_rate sfee = decimal.Decimal(order.actual_fee) * decimal.Decimal( agency.share_rate) / decimal.Decimal(100) sfee = int(sfee.to_integral_value()) agency_share.share_fee = sfee agency_share.create_time = utils.get_currtime() agency_share.sync_ver = tools.gen_sync_ver() self.db.add(agency_share) agency.amount -= order.actual_fee aorder = models.TrAgencyOrder() aorder.id = utils.get_uuid() aorder.agency_id = agency.id aorder.fee_type = 'cost' aorder.fee_value = -order.actual_fee aorder.fee_total = agency.amount aorder.fee_desc = u'用户 %s 开通扣费' % account_number aorder.create_time = agency_share.create_time aorder.sync_ver = tools.gen_sync_ver() self.db.add(aorder) agency.amount += agency_share.share_fee aorder2 = models.TrAgencyOrder() aorder2.id = utils.get_uuid() aorder2.agency_id = agency.id aorder2.fee_type = 'share' aorder2.fee_value = agency_share.share_fee aorder2.fee_total = agency.amount aorder2.fee_desc = u'用户 %s 开通分成(%s%%)' % (account_number, agency.share_rate) aorder2.create_time = agency_share.create_time aorder2.sync_ver = tools.gen_sync_ver() self.db.add(aorder2) charge_value = 0 if charge_code: charge_log = models.TrChargeLog() charge_log.id = utils.get_uuid() charge_log.order_id = order.order_id charge_log.charge_code = charge_code charge_log.operator_name = accept_log.operator_name charge_log.operate_time = order.create_time charge_log.sync_ver = tools.gen_sync_ver() self.db.add(charge_log) charge_value = int( self.db.query( models.TrCharges).get(charge_code).charge_value or 0) account = models.TrAccount() account.account_number = account_number account.ip_address = ip_address account.customer_id = customer.customer_id account.product_id = order.product_id account.install_address = customer.address account.mac_addr = '' account.password = self.aes.encrypt(password) account.status = status if billing_type == 0: account.status = UsrPreAuth if pay_status == 0: account.status = UsrPadding account.balance = balance - charge_value account.time_length = int(product.fee_times) account.flow_length = flow_length account.expire_date = expire_date account.user_concur_number = product.concur_number account.bind_mac = product.bind_mac account.bind_vlan = product.bind_vlan account.vlan_id1 = 0 account.vlan_id2 = 0 account.create_time = customer.create_time account.update_time = customer.create_time account.account_desc = customer.customer_desc account.sync_ver = tools.gen_sync_ver() self.db.add(account) if pay_status == 0: order.before_account_data = self.warp_account_data( account, status=UsrPadding) order.after_account_data = self.warp_account_data(account, status=1) order.order_desc = u'用户新开账号,赠送天数:%s, 收费项金额:%s' % ( giftdays, utils.fen2yuan(charge_value)) issues = None builder_phone = None if builder_name and pay_status == 1: builder_phone = self.db.query( models.TrBuilder.builder_phone).filter_by( builder_name=builder_name).scalar() issues = models.TrIssues() issues.id = utils.get_uuid() issues.account_number = account.account_number issues.issues_type = '0' issues.content = u'用户新开账号, 请前往安装' issues.builder_name = builder_name issues.status = 0 issues.operator_name = self.operator.operator_name issues.date_time = utils.get_currtime() issues.sync_ver = tools.gen_sync_ver() self.db.add(issues) opsdesc = u'用户新开账号 %s, 赠送天数:%s, 收费项金额:%s' % ( account.account_number, giftdays, utils.fen2yuan(charge_value)) self.add_oplog(opsdesc) self.db.commit() dispatch.pub(ACCOUNT_OPEN_EVENT, account.account_number, async=True) if issues and builder_phone: dispatch.pub(ISSUES_ASSIGN_EVENT, issues.account_number, builder_phone, async=True) self.update_routeros_sync_event(account_number, password, product_id, node_id) return True except Exception as err: self.db.rollback() traceback.print_exc() self.last_error = u'客户开户操作失败:%s' % utils.safeunicode(err) logger.error(self.last_error, tag='customer_add_error', username=formdata.get('account_number')) return False return
def order_calc(self, product_id, old_expire = None): months = int(self.get_argument('months', 0)) days = int(self.get_argument('days', 0)) product = self.db.query(models.TrProduct).get(product_id) fee_value, expire_date = (None, None) if product.product_policy in (BOTimes, BOFlows): fee_value = utils.fen2yuan(product.fee_price) expire_date = MAX_EXPIRE_DATE elif product.product_policy == PPMonth: fee = decimal.Decimal(months) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(int(fee.to_integral_value())) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime(old_expire, '%Y-%m-%d') expire_date = utils.add_months(start_expire, int(months), days=0) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == PPDay: fee = decimal.Decimal(days) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(int(fee.to_integral_value())) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime(old_expire, '%Y-%m-%d') expire_date = start_expire + datetime.timedelta(days=days) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == BOMonth: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime(old_expire, '%Y-%m-%d') if months > 0: mprice = self.get_product_attr(product.id, 'month_price') if mprice: mpricefee = decimal.Decimal(mprice.attr_value) else: mpricefee = decimal.Decimal(product.fee_price) / decimal.Decimal(product.fee_months) fee = decimal.Decimal(months) * mpricefee fee_value = utils.fen2yuan(int(fee.to_integral_value())) expire_date = utils.add_months(start_expire, int(months), days=0) expire_date = expire_date.strftime('%Y-%m-%d') else: fee_value = utils.fen2yuan(product.fee_price) expire_date = utils.add_months(start_expire, product.fee_months, days=0) expire_date = expire_date.strftime('%Y-%m-%d') elif product.product_policy == BODay: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime(old_expire, '%Y-%m-%d') if days > 0: dprice = self.get_product_attr(product.id, 'day_price') if dprice: dpricefee = decimal.Decimal(dprice.attr_value) else: dpricefee = decimal.Decimal(product.fee_price) / decimal.Decimal(product.fee_days) print 'dprice=', dpricefee fee = decimal.Decimal(days) * dpricefee fee_value = utils.fen2yuan(int(fee.to_integral_value())) expire_date = start_expire + datetime.timedelta(days=days) expire_date = expire_date.strftime('%Y-%m-%d') else: fee_value = utils.fen2yuan(product.fee_price) expire_date = start_expire + datetime.timedelta(days=product.fee_days) expire_date = expire_date.strftime('%Y-%m-%d') return (fee_value, expire_date)
def post(self, pid): try: openid = self.session.get('mps_openid', os.environ.get('DEV_OPEN_ID')) realname = self.get_argument('realname', '').strip() node_id = self.get_argument('node_id', '').strip() mobile = self.get_argument('mobile', '').strip() address = self.get_argument('address', '').strip() idcard = self.get_argument('idcard', '').strip() username = self.get_argument('username', '').strip() password = self.get_argument('password', '').strip() months = int(self.get_argument('months', '1')) days = int(self.get_argument('days', '1')) wechat_bind = int(self.get_argument('wechat_bind', '0')) product = self.db.query(models.TrProduct).filter_by(id=pid).first() if not product: return self.render('error.html', msg=u'资费不存在') if not realname: return self.render('wxorder_form.html', product=product, msg=u'姓名不能为空', **self.get_params()) if not rules.is_alphanum2(6, 16).valid(utils.safestr(username)): return self.render('wxorder_form.html', product=product, msg=u'用户名校验错误,必须是长度为6-16位的英文字符数字', **self.get_params()) if not rules.is_alphanum2(6, 16).valid(password): return self.render('wxorder_form.html', product=product, msg=u'密码校验错误,必须是6-16为的英文字符数字', **self.get_params()) if wechat_bind == 1 and self.db.query(models.TrCustomer).filter_by( wechat_oid=openid).count() > 0: return self.render('wxorder_form.html', product=product, msg=u'微信账号已绑定', **self.get_params()) fee_value, expire_date = self.calc_fee(product.id, months=months, charge_fee=utils.fen2yuan( product.fee_price)) order_id = tools.gen_num_id(16) formdata = Storage({'order_attach': 'neworder'}) formdata['wxpay_body'] = u'套餐订购:%s' % product.product_name formdata['openid'] = openid formdata['order_id'] = order_id formdata['node_id'] = node_id formdata['area_id'] = '' formdata['realname'] = realname formdata['idcard'] = idcard formdata['mobile'] = mobile formdata['address'] = address formdata['account_number'] = username formdata['password'] = password formdata['ip_address'] = '' formdata['product_id'] = product.id formdata['agency_id'] = '' formdata['charge_code'] = '' formdata['months'] = months formdata['days'] = days formdata['giftdays'] = 0 formdata['giftflows'] = 0 formdata['fee_value'] = fee_value formdata['expire_date'] = expire_date formdata['status'] = 1 formdata['builder_name'] = '' formdata['customer_desc'] = u'客户微信自助开户' formdata['billing_type'] = 1 formdata['accept_source'] = 'wechat' if wechat_bind == 1: formdata['wechat_oid'] = openid self.paycache.set(order_wxpaycaache_key(order_id), formdata, 28800) self.redirect('/mps/wxorder/pay/%s' % order_id) except Exception as err: logger.exception(err, trace='wechat') self.render('error.html', msg=u'套餐订购失败,请联系客服 %s' % repr(err))
def event_apm_user_billing(self, account_number): logger.info(u'用户[%s]后付费自动出账任务执行' % account_number) with make_db(self.db) as db: try: account = db.query(models.TrAccount).get(account_number) product = db.query(models.TrProduct).get(account.product_id) fee_precision = self.get_param_value('billing_fee_precision', 'fen') if not product: logger.error(u'执行后付费包月自动出账时,用户[%s]资费id[%s]不存在' % (account_number, product_id)) return accept_log = models.TrAcceptLog() accept_log.id = utils.get_uuid() accept_log.accept_type = 'apm_bill' accept_log.accept_source = 'task' accept_log.account_number = account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = 'admin' accept_log.accept_desc = u'用户[%s]后付费包月自动出账, ' % account_number accept_log.stat_year = accept_log.accept_time[0:4] accept_log.stat_month = accept_log.accept_time[0:7] accept_log.stat_day = accept_log.accept_time[0:10] accept_log.sync_ver = tools.gen_sync_ver() self.db.add(accept_log) order = models.TrCustomerOrder() order.order_id = utils.get_uuid() order.customer_id = account.customer_id order.product_id = product.id order.account_number = account_number order.order_fee = product.fee_price order_bill_fee = product.fee_price order_bill_days = 30 per_day_fee = decimal.Decimal(product.fee_price) / order_bill_days per_day_fee_yuan = utils.fen2yuan(int(per_day_fee.to_integral_value())) this_month_start_str = datetime.datetime.now().strftime('%Y-%m-01 %H:%M:%S') this_month_start = datetime.datetime.strptime(this_month_start_str, '%Y-%m-%d %H:%M:%S') pre_month_start_str = utils.add_months(datetime.datetime.now(), -1).strftime('%Y-%m-01 00:00:00') pre_month_start = datetime.datetime.strptime(pre_month_start_str, '%Y-%m-%d %H:%M:%S') pre_month_end = this_month_start - datetime.timedelta(days=1) pre_month_days = (pre_month_end - pre_month_start).days + 1 user_create_time = datetime.datetime.strptime(account.create_time, '%Y-%m-%d %H:%M:%S') if user_create_time > pre_month_start: order_bill_days = (pre_month_end - user_create_time).days per_day_fee = decimal.Decimal(product.fee_price) / decimal.Decimal(pre_month_days) per_day_fee_yuan = utils.fen2yuan(int(per_day_fee.to_integral_value())) order_bill_fee = int((per_day_fee * order_bill_days).to_integral_value()) if fee_precision == 'yuan': order_bill_fee = int((decimal.Decimal(order_bill_fee) / decimal.Decimal(100)).to_integral_value()) * 100 order.actual_fee = order_bill_fee order.pay_status = 0 order.accept_id = accept_log.id order.order_source = accept_log.accept_source order.create_time = account.create_time order.order_desc = u'用户后付费账单:{0}(日均价={1}/{2}) x {3}(使用天数) '.format(per_day_fee_yuan, utils.fen2yuan(product.fee_price), pre_month_days, order_bill_days) order.stat_year = order.create_time[0:4] order.stat_month = order.create_time[0:7] order.stat_day = order.create_time[0:10] order.sync_ver = tools.gen_sync_ver() self.db.add(order) self.db.commit() logger.info(u'用户[%s]后付费出账完成' % account_number, trace='event') except Exception as err: logger.exception(err) logger.error(u'用户[%s]后付费出账失败' % account_number, trace='event')
def cancel(self, formdata, **kwargs): """用户销户,将用户状态设置为销户状态,账号不可用,不删除用户数据,数据保留用做查询统计。 :param formdata: 用户销户参数表 :type formdata: dict formdata params: :param account_number: 用户账号 :type account_number: string :param operate_desc: 操作描述 :type operate_desc: string :param fee_value: 费用金额(元) x.xx :type fee_value: string """ try: account_number = self.parse_arg(formdata, 'account_number', rule=rules.not_null) fee_value = self.parse_arg(formdata, 'fee_value', rule=rules.is_rmb) operate_desc = self.parse_arg(formdata, 'operate_desc', defval='') account = self.db.query(models.TrAccount).get(account_number) if account.status != 1: raise ValueError(u'无效用户状态') accept_log = models.TrAcceptLog() accept_log.id = utils.get_uuid() accept_log.accept_type = 'cancel' accept_log.accept_source = 'console' accept_log.account_number = account_number accept_log.accept_time = utils.get_currtime() accept_log.operator_name = self.operator.operator_name accept_log.accept_desc = u'用户销户退费%s(元);%s' % ( fee_value, utils.safeunicode(operate_desc)) accept_log.stat_year = accept_log.accept_time[0:4] accept_log.stat_month = accept_log.accept_time[0:7] accept_log.stat_day = accept_log.accept_time[0:10] accept_log.sync_ver = tools.gen_sync_ver() self.db.add(accept_log) old_expire_date = account.expire_date order = models.TrCustomerOrder() order.id = utils.get_uuid() order.order_id = utils.gen_order_id() order.customer_id = account.customer_id order.product_id = account.product_id order.account_number = account_number order.order_fee = 0 order.actual_fee = -utils.yuan2fen(fee_value) order.pay_status = 1 order.order_source = 'console' order.accept_id = accept_log.id order.create_time = utils.get_currtime() order.order_desc = accept_log.accept_desc order.stat_year = order.create_time[0:4] order.stat_month = order.create_time[0:7] order.stat_day = order.create_time[0:10] order.sync_ver = tools.gen_sync_ver() self.db.add(order) account.status = 3 agency_id = self.db.query(models.TrCustomer.agency_id).filter_by( customer_id=account.customer_id).scalar() if agency_id: agency = self.db.query(models.TrAgency).get(agency_id) sfee = decimal.Decimal(order.actual_fee) * decimal.Decimal( agency.share_rate) / decimal.Decimal(100) sfee = int(sfee.to_integral_value()) if agency.amount < sfee: raise ValueError(u'代理商预存款余额不足') agency.amount -= -sfee aorder = models.TrAgencyOrder() aorder.id = utils.get_uuid() aorder.agency_id = agency.id aorder.fee_type = 'sharecost' aorder.fee_value = sfee aorder.fee_total = agency.amount aorder.fee_desc = u'用户 %s 销户退费分摊(%s%%) %s 元' % ( account_number, agency.share_rate, utils.fen2yuan(aorder.fee_value)) aorder.create_time = utils.get_currtime() order.sync_ver = tools.gen_sync_ver() self.db.add(aorder) self.db.commit() dispatch.pub(ACCOUNT_CHANNEL_EVENT, account.account_number, async=True) dispatch.pub(redis_cache.CACHE_DELETE_EVENT, account_cache_key(account.account_number), async=True) for online in self.db.query(models.TrOnline).filter_by( account_number=account.account_number): dispatch.pub(UNLOCK_ONLINE_EVENT, online.account_number, online.nas_addr, online.acct_session_id, async=True) return True except Exception as err: self.db.rollback() self.last_error = u'用户销户失败:%s, %s' % (utils.safeunicode( err.message), err.__class__) logger.error(self.last_error, tag='account_cancel_error', username=formdata.get('account_number')) return False
def post(self): months = int(self.get_argument('months', 0)) days = int(self.get_argument('days', 0)) product_id = self.get_argument('product_id', '') old_expire = self.get_argument('old_expire', '') giftdays = int(self.get_argument('giftdays', 0)) product = self.db.query(models.TrProduct).get(product_id) charge_code = self.get_argument('charge_code', '') charge_value = 0 if charge_code: charge_value = self.db.query( models.TrCharges).get(charge_code).charge_value if product.product_policy in (BOTimes, BOFlows): fee_value = utils.fen2yuan(product.fee_price + charge_value) return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=MAX_EXPIRE_DATE)) if product.product_policy == PPMonth: fee = decimal.Decimal(months) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan( int(fee.to_integral_value()) + charge_value) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') expire_date = utils.add_months(start_expire, int(months), days=giftdays) expire_date = expire_date.strftime('%Y-%m-%d') return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=expire_date)) if product.product_policy == PPDay: fee = decimal.Decimal(days) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan( int(fee.to_integral_value()) + charge_value) start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') expire_date = start_expire + datetime.timedelta(days=days) expire_date = expire_date.strftime('%Y-%m-%d') return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=expire_date)) if product.product_policy == APMonth: fee = decimal.Decimal(months) * decimal.Decimal(product.fee_price) fee_value = utils.fen2yuan(charge_value) return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=MAX_EXPIRE_DATE)) if product.product_policy == BOMonth: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') fee_value = utils.fen2yuan(product.fee_price + charge_value) expire_date = utils.add_months(start_expire, product.fee_months, days=giftdays) expire_date = expire_date.strftime('%Y-%m-%d') return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=expire_date)) if product.product_policy == BODay: start_expire = datetime.datetime.now() if old_expire: start_expire = datetime.datetime.strptime( old_expire, '%Y-%m-%d') fee_value = utils.fen2yuan(product.fee_price + charge_value) expire_date = start_expire + datetime.timedelta( days=product.fee_days + giftdays) expire_date = expire_date.strftime('%Y-%m-%d') return self.render_json(code=0, data=dict(policy=product.product_policy, fee_value=fee_value, expire_date=expire_date))