class Purchase < ActiveRecord::Base ## Associaciones belongs_to :supplier belongs_to :pointsale belongs_to :warehouse belongs_to :user has_many :cash_registers_moves has_many :purchase_details has_many :inventories_moves has_many :products, :through => :purchase_details ##--- Llevar registro de Actividad del usuario audited enum status: [:notpaid, :cancelled, :paid, :parcial] accepts_nested_attributes_for :purchase_details ##--- Validaciones previas de guardar validates :supplier_id, presence: { message: "Debe seleccionar el proveedor." } validates :pointsale_id, presence: { message: "Debe seleccionar un almacén o un punto de venta." }, if: Proc.new { |c| c.warehouse_id.blank? } validates :warehouse_id, presence: { message: "Debe seleccionar un almacén o un punto de venta." }, if: Proc.new { |c| c.pointsale_id.blank? } validates :exchange, presence: { message: "Debe capturar el tipo de cambio." }, if: Proc.new { |c| c.is_in_dollars? } ##--- Tipo de vistas / consultas scope :notpaid, -> { where(status: 0) } scope :paid, -> { where(status: 2) } scope :cancelled, -> { where(status: 1) } scope :active, -> { where.not(status: 1) } def code_with_price "Codigo: #{purchase_code} - $ #{total}" end end