Fix the late fee calculations
[cs356-p2-videostore.git] / app / models / game.rb
index efdb260..a2d324a 100644 (file)
@@ -1,9 +1,44 @@
 class Game < Rentable
-  validates_presence_of :game_genre
-  validates_presence_of :platform
+  has_many :game_genres
+  has_many :game_platforms
+
+  validates_presence_of :game_genre_id
+  validates_presence_of :game_platform_id
+
+  def genre
+    GameGenre.find_by_id(game_genre_id)
+  end
+
+  def platform
+    GamePlatform.find_by_id(game_platform_id)
+  end
 
   def calculated_price
-    # FIXME: generate this based on day of week, newrelase
-    return 11
+    # FIXME: generate this based on day of week, newrelease
+    day_of_week = Time.now.to_date.wday
+    base_fee = GamePolicy.find_by_day(day_of_week).fee
+
+    # Check for newrelease
+    newrelease_fee = newrelease ? GamePolicy.find_by_day(8).fee : 0.00
+
+    return base_fee + newrelease_fee
   end
+
+  def due_date
+    # FIXME: generate this based on the day of week, newrelease
+    # NOTE: a Date.wday will tell you the day of week (0-6, meaning Sunday-Saturday)
+    day_of_week = Time.now.to_date.wday
+    base_period = GamePolicy.find_by_day(day_of_week).period
+    newrelease_period = newrelease ? GamePolicy.find_by_day(8).period : 0
+
+    period = base_period + newrelease_period
+    return Time.now.advance(:days => period).to_date
+  end
+
+  protected
+  def validate
+    errors.add(:game_genre_id, "does not exist in the database") if genre.nil?
+    errors.add(:game_platform_id, "does not exist in the database") if platform.nil?
+  end
+
 end