class PFournisseurDoc < ApplicationRecord
  belongs_to :p_fournisseur
  
  
  belongs_to :p_fournisseur_order
  
  
  belongs_to :p_fournisseur_doc_type
  
  
  has_one :price_line_block, :as => :price_lineable
  
  accepts_nested_attributes_for :price_line_block
  
  has_many :stock_blocks, :as => :stockable
  
  has_many :stock_lines, :through => :stock_blocks
  
  def accounting_zone_name
    self.p_fournisseur.accounting_zone.name if self.p_fournisseur and self.p_fournisseur.accounting_zone
  end
  
  def accounting_zone_id
    self.p_fournisseur.accounting_zone_id
  end
  
  
  before_save do
    if !self.date
      if self.created_at
        self.date = self.created_at
      else
        self.date = Time.now
      end
    end
    
    if self.p_fournisseur_doc_type
      self.movement_type = self.p_fournisseur_doc_type.movement_type
      self.accounting = self.p_fournisseur_doc_type.movement_type
    end
  end
  
  
end
