class PCommercialObjectMonth < ApplicationRecord
  belongs_to :p_commercial
  belongs_to :p_commercial_objective

  before_validation :set_end_at

  validates :objective_percent, :presence => true
  validates :weight_objective, :presence => true

  def set_end_at
    self.end_at = self.start_at.end_of_month
  end


  def ca_total_ca
    (self.objective_percent.to_f * self.p_commercial_objective.total_ca) / 100 if self.p_commercial_objective
  end

  def ca_total_weight
    (self.weight_objective.to_f * self.p_commercial_objective.total_weight) / 100 if self.p_commercial_objective
  end

end
