class PShipTourTruckSheetLine < ActiveRecord::Base
  belongs_to :p_ship_tour_truck
  belongs_to :p_sheet_line
  
  validates :qte_shiped, :presence => true, :if => :qte_needed?
  
  validates :raison, :presence => true, :if => :raison_needed?
  
  attr_accessor :to_return
  
  
  def qte_needed?
    if self.to_return
      true 
    else
      false
    end
  end
  
  
  def raison_needed?
    if self.canceled
      true
    else
      false
    end
  end
  
  before_validation do 
    if self.canceled
      self.qte_shiped = 0
    
    elsif self.qte_needed?
      if self.qte_shiped.to_f == 0.0
        errors.add(:qte_shiped, 'La quantité doit être supérieure à 0') 
      end
    end
  end
  
  
end
