class CreditProduct < ActiveRecord::Base
  belongs_to :devise
  
  def price_ttc
    self.price_ht * self.tva
  end
  
  
  def price_ht_final(annonce_account)
    if self.devise_id == 1 and annonce_account.credits.between(Date.today, Date.today).boughts.where("orders.devise_id != 1").count == 0
      self.price_ht - annonce_account.credits.between(Date.today, Date.today).boughts.sum("orders.price_ht")
    else
      self.price_by_credits_ht * nbr_credits_final(annonce_account)
      
    end
  
  end
  
  def price_ttc_final(annonce_account)
    self.price_ht_final(annonce_account)* self.tva
    
  end
  
  def nbr_credits_final(annonce_account)
    self.nbr_credits - annonce_account.credits_boughts(Date.today)
  end
  
  def binary_points_final(annonce_account)
    self.binary_points - annonce_account.credits.between(Date.today, Date.today).boughts.sum("orders.binary_points")
  end
  
  def nbr_parts_final(annonce_account)
    self.parts - annonce_account.credits.between(Date.today, Date.today).boughts.sum("orders.nbr_parts")
  end
  
  def price_by_credits_ht
    self.price_ht/self.nbr_credits
  end
  
  def price_by_credits_ttc
    price_by_credits_ht * self.tva
  end
  
  def tva
    1.2
  end

end

