Permalink
Browse files

update cal price

  • Loading branch information...
1 parent a0aa746 commit b68451388c6dd981ef4425946df322009e3924ae @zhendi committed Jul 17, 2012
Showing with 36 additions and 23 deletions.
  1. +35 −22 app/controllers/saasy_simple/subscriptions_controller.rb
  2. +1 −1 lib/SaasySimple/version.rb
@@ -37,28 +37,41 @@ def deactivate
private
def cal_price(user)
- al = []
- site_price = 5
- user_price = 2.5
- gateway_price = 10
- device_price = 4.5
- message_price = 0.03
-
- user_count = User.where(:created_by=>current_user.id).count + 1
- gateway_count, device_count, message_count, site_count = 0, 0, 0, 0
- UserGateway.where(:user_id=>user.id).each do |ug|
- site = ug["zone"].blank? ? ug["mac"] : ug["zone"]
- next if al.include?(site)
- site_count += 1
- al << site
- gateway_count += 1
- registered_gateway = RegisteredGateway.where(:mac=>ug['mac']).first
- latest_timestamp = registered_gateway.devices.where(:timestamp.exists=>true).order_by([[:timestamp, -1]]).limit(1).first.timestamp rescue nil
- latest_timestamp = Time.parse(latest_timestamp).advance(:minutes => -5).strftime("%Y%m%d%H%M%S") if latest_timestamp
- device_count += registered_gateway.devices.where(:timestamp.gte => latest_timestamp).count
- message_count += Message.where(:mac=>ug['mac']).count
- end
- site_count*site_price + user_count*user_price + gateway_count*gateway_price + device_count*device_price + message_count*message_price
+ if user["subscription"].blank? or user["subscription"]["price"].blank?
+ al = []
+ site_price = 5
+ user_price = 2.5
+ gateway_price = 10
+ device_price = 4.5
+
+ user_count = User.where(:created_by=>user.id).count + 1
+ gateway_count, device_count, message_count, site_count = 0, 0, 0, 0
+ UserGateway.where(:user_id=>user.id).each do |ug|
+ site = ug["zone"].blank? ? ug["mac"] : ug["zone"]
+ next if al.include?(site)
+ site_count += 1
+ al << site
+ gateway_count += 1
+ registered_gateway = RegisteredGateway.where(:mac=>ug['mac']).first
+ latest_timestamp = registered_gateway.devices.where(:timestamp.exists=>true).order_by([[:timestamp, -1]]).limit(1).first.timestamp rescue nil
+ latest_timestamp = Time.parse(latest_timestamp).advance(:minutes => -5).strftime("%Y%m%d%H%M%S") if latest_timestamp
+ device_count += registered_gateway.devices.where(:timestamp.gte => latest_timestamp).count
+ end
+ user_count = user_count > 3 ? user_count-3 : 0
+ device_count = device_count > 5 ? device_count-5 : 0
+ site_count = site_count > 1 ? site_count-1 : 0
+ gateway_count = gateway_count > 1 ? gateway_count-1 : 0
+ price = 10 + site_count*site_price + user_count*user_price + gateway_count*gateway_price + device_count*device_price
+ user["subscription"] = {"user_count"=>user_count, "device_count"=>device_count, "site_count"=>site_count, "gateway_count"=>gateway_count, "price"=>price}
+ user.save
+ else
+ if current_user['additional_subscription'].blank?
+ price = current_user["subscription"]["price"]
+ else
+ price = current_user['additional_subscription']['additional_price']
+ end
+ end
+ price*12
end
end
end
@@ -1,3 +1,3 @@
module SaasySimple
- VERSION = "0.2.1"
+ VERSION = "0.2.2"
end

0 comments on commit b684513

Please sign in to comment.