def gen_invoice(uid, credit, days, lock_mode=False): """ Generate Invoice @param lock_mode: bool @param uid: user id @type uid: str|int @param credit: credit used to charge @type credit: int @param days: charged days @type days: int @return: Invoice id @rtype: int """ current_service = UserCurrentService.objects.get(user=uid) prices = Utils.get_service_price(current_service) package_price = prices.meg service_price = days * prices.day ti = TempInvoice() ti.credit = credit ti.credit_price = credit * package_price ti.days = days ti.days_price = service_price ti.user_id = uid ti.save() i = Invoice() ic = InvoiceService() ic.content_object = ti ic.service_type = 6 exp_date = date_add_days_aware(current_service.expire_date, days) ic.expire_date = exp_date ic.save() i.user_id = uid i.comment = _('temp charge') i.create_time = now() if lock_mode: user_debit = UserDebit.objects.filter(user=uid).first() if user_debit: i.debit_price = user_debit.amount else: i.debit_price = 0 else: i.debit_price = 0 i.dynamic_discount = 0 i.extra_data = 0 # i.price = (ti.days_price + ti.credit_price) i.tax = (ti.days_price + ti.credit_price) * float( read_config('invoice_tax', 0.09)) i.price = (ti.days_price + ti.credit_price) + i.tax i.service_text = _('temp charge invoice') i.service = ic i.save() return i.pk
def get_max_charges(uid): """ Get the max allowed changes for user @param uid: user id @type uid: str @return: (credit, days) @rtype: (int, int) """ state = UserServiceStatus(uid) data = TempChargeState.objects.filter( user=uid).first() # only 1 Hit to DB! cs = state.current_service is_float_mode = read_config('service_temp_float', '0') == '1' if cs: x = Utils.get_service_price(cs) if data: credit = data.credit if state.is_unlimited: credit = 0 days = data.days if not is_float_mode: temp_days = int(read_config('service_temp_time', 2)) temp_amount = int(read_config('service_temp_amount', 1024)) if days > temp_days: days = temp_days # TempChargeManagement.update_state(uid, 0, days, True, True) if credit > temp_amount: # TempChargeManagement.update_state(uid, credit, 0, True, True) credit = temp_amount else: if state.is_limited: credit = int(read_config('service_temp_amount', 1024)) else: credit = 0 days = int(read_config('service_temp_time', 2)) TempChargeManagement.update_state(uid, credit, days, True, True) if not state.account_expired: days = 0 if state.credit > 100: credit = 0 if data.total_count > 3: return 0, 0, 0, 0 return credit, days, x.day, x.package else: return 0, 0, 0, 0