mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-24 03:35:59 +08:00
117 lines
3.4 KiB
CoffeeScript
117 lines
3.4 KiB
CoffeeScript
{config, plan} = app
|
|
{mAccount, mBalance} = app.models
|
|
|
|
exports.cyclicalBilling = (callback) ->
|
|
mAccount.find
|
|
'attribute.plans.0':
|
|
$exists: true
|
|
.toArray (err, accounts) ->
|
|
async.each accounts, (account, callback) ->
|
|
exports.triggerBilling account, ->
|
|
callback()
|
|
, ->
|
|
callback()
|
|
|
|
exports.cyclicalBilling ->
|
|
setInterval ->
|
|
exports.cyclicalBilling ->
|
|
, config.billing.billing_cycle
|
|
|
|
# @param callback(account)
|
|
exports.triggerBilling = (account, callback) ->
|
|
forceLeaveAllPlans = (callback) ->
|
|
async.eachSeries account.billing.plans, (plan_name, callback) ->
|
|
mAccount.findOne {_id: account._id}, (err, account) ->
|
|
plan.leavePlan account, plan_name, callback
|
|
, ->
|
|
mAccount.findOne {_id: account._id}, (err, account) ->
|
|
callback account
|
|
|
|
is_force = do ->
|
|
if account.attribute.balance < config.billing.force_freeze.when_balance_below
|
|
return true
|
|
|
|
if Date.now() > account.attribute.arrears_at.getTime() + config.billing.force_freeze.when_arrears_above
|
|
return true
|
|
|
|
return false
|
|
|
|
async.each account.billing.plans, (plan_name, callback) ->
|
|
exports.generateBilling account, plan_name, is_force, (result) ->
|
|
callback null, result
|
|
|
|
, (err, result) ->
|
|
result = _.compact result
|
|
|
|
if _.isEmpty result
|
|
return callback account
|
|
|
|
modifier =
|
|
$set: {}
|
|
$inc:
|
|
'billing.balance': 0
|
|
|
|
for item in result
|
|
modifier.$set["billing.last_billing_at.#{item.name}"] = item.last_billing_at
|
|
modifier.$inc['billing.balance'] += item.amount_inc
|
|
|
|
if account.billing.balance > 0
|
|
if account.billing.arrears_at
|
|
modifier.$set['billing.arrears_at'] = null
|
|
else if account.billing.balance < 0
|
|
unless account.billing.arrears_at
|
|
modifier.$set['billing.arrears_at'] = new Date()
|
|
|
|
mAccount.findAndModify {_id: account._id}, null, modifier, {new: true}, (err, account) ->
|
|
mBalance.create account, 'billing', modifier.$inc['billing.balance'],
|
|
plans: _.indexBy result, 'name'
|
|
, ->
|
|
if is_force
|
|
return forceLeaveAllPlans callback
|
|
else
|
|
callback account
|
|
|
|
exports.generateBilling = (account, plan_name, is_force, callback) ->
|
|
plan_info = config.plans[plan_name]
|
|
|
|
unless plan_info.billing_by_time
|
|
return callback()
|
|
|
|
last_billing_at = account.billing.last_billing_at[plan_name]
|
|
|
|
if last_billing_at
|
|
next_billing_at = new Date last_billing_at.getTime() + plan_info.billing_by_time.unit * plan_info.billing_by_time.min_billing_unit
|
|
else
|
|
last_billing_at = new Date()
|
|
|
|
unless last_billing_at and next_billing_at > new Date()
|
|
return callback()
|
|
|
|
billing_unit_count = (Date.now() - last_billing_at.getTime()) / plan_info.billing_by_time.unit
|
|
|
|
if is_force
|
|
billing_unit_count = Math.ceil billing_unit_count
|
|
new_last_billing_at = new Date()
|
|
else
|
|
billing_unit_count = Math.floor billing_unit_count
|
|
new_last_billing_at = new Date last_billing_at.getTime() + billing_unit_count * plan_info.billing_by_time.unit
|
|
|
|
amount = billing_unit_count * plan_info.billing_by_time.price
|
|
|
|
callback
|
|
name: plan_name
|
|
billing_unit_count: billing_unit_count
|
|
last_billing_at: new_last_billing_at
|
|
amount_inc: -amount
|
|
|
|
exports.calcResourcesLimit = (plans) ->
|
|
limit = {}
|
|
|
|
for plan_name in plans
|
|
if config.plans[plan_name].resources
|
|
for k, v of config.plans[plan_name].resources
|
|
limit[k] ?= 0
|
|
limit[k] += v
|
|
|
|
return limit
|