pre_purchase.rb 1.3 KB

1234567891011121314151617181920212223242526272829
  1. class PrePurchase < ActiveRecord::Base
  2. belongs_to :supplier
  3. belongs_to :user
  4. belongs_to :pointsale
  5. belongs_to :product
  6. belongs_to :warehouse
  7. validates :supplier_id , :presence => { message: "Debe seleccionar proveedor para la compra." }, :on => [:create, :update]
  8. validates :product_id , :presence => { message: "Debe seleccionar producto para la compra." }, :on => [:create, :update]
  9. 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]
  10. 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]
  11. def get_totals
  12. if self.exchange.present?
  13. self.price_base = self.product.price_base_dollars
  14. else
  15. self.price_base = self.product.price_base
  16. end
  17. self.price_base = self.price_base.blank? ? 0 : self.price_base
  18. self.amount = self.quantity * self.price_base
  19. self.tax = 0.0
  20. if self.product.include_purchase_tax == 1 && self.exchange.blank?
  21. tax_percent = PosConfig.first.tax_percent
  22. self.tax = (((tax_percent / 100) * self.amount) / ((100 + tax_percent) / 100)).round(2)
  23. end
  24. self.total = self.amount
  25. return true
  26. end
  27. end