pre_purchase.rb 1.2 KB

12345678910111213141516171819202122232425262728293031
  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. self.price_base =
  13. if exchange.present?
  14. product.price_base_dollars
  15. else
  16. product.price_base
  17. end
  18. self.price_base = 0 if price_base.nil?
  19. self.amount = quantity * price_base
  20. self.tax = 0.0
  21. if product.include_purchase_tax == 1 && exchange.blank?
  22. tax_percent = PosConfig.first.tax_percent
  23. self.tax = (((tax_percent / 100) * amount)).round(2)
  24. end
  25. self.total = tax + amount
  26. true
  27. end
  28. end