class PrePurchase < ActiveRecord::Base
	belongs_to :supplier
	belongs_to :user
	belongs_to :pointsale
	belongs_to :product
	belongs_to :warehouse	

	validates :supplier_id , :presence => { message: "Debe seleccionar proveedor para la compra." }, :on => [:create, :update]
	validates :product_id , :presence => { message: "Debe seleccionar producto para la compra." }, :on => [:create, :update]
    validates :pointsale_id , presence: { message: "Debe seleccionar un almacén o un punto de venta." }, :if => Proc.new {|c| c.warehouse_id.blank?} , :on => [:create, :update]
    validates :warehouse_id , presence: { message: "Debe seleccionar un almacén o un punto de venta." }, :if => Proc.new {|c| c.pointsale_id.blank?} , :on => [:create, :update]

	def get_totals
		if self.exchange.present? 
			self.price_base = self.product.price_base_dollars
		else
			self.price_base = self.product.price_base
		end
		self.price_base = self.price_base.blank? ? 0 : self.price_base
		self.amount = self.quantity * self.price_base
		self.tax = 0.0
		if self.product.include_purchase_tax == 1 && self.exchange.blank?
			tax_percent = PosConfig.first.tax_percent
	        self.tax = (((tax_percent / 100)  * self.amount) / ((100 + tax_percent) / 100)).round(2)	
		end
		self.total = self.amount
		return true
	end 
end
