pre_sale.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. class PreSale < ActiveRecord::Base
  2. belongs_to :customer
  3. belongs_to :user
  4. belongs_to :open_cash_register
  5. belongs_to :product
  6. belongs_to :special_price
  7. has_one :cash_register, :through => :open_cash_register
  8. has_one :pointsale, :through => :cash_register
  9. enum sale_type: [ :credit, :cash, :reserved ]
  10. validates :customer_id , :presence => { message: "Debe seleccionar cliente para la venta." }, :on => [:create, :update]
  11. validates :open_cash_register_id , :presence => { message: "Debe seleccionar caja registradora." }, :on => [:create, :update]
  12. validates :sale_type, presence: { message: "Debe seleccionar tipo de venta." }, :on => [:create, :update]
  13. # before_save :get_totals
  14. def get_totals
  15. special_price = SpecialPrice.find_by(:customer_id => self.customer_id, :product_id => self.product_id)
  16. unless special_price.blank?
  17. self.special_price_id = special_price.id
  18. discount_per_unit = special_price.get_unit_discount(self.unit_price)
  19. self.discount = discount_per_unit * self.quantity
  20. end
  21. self.discount += self.haggle
  22. self.amount = self.quantity * self.unit_price
  23. self.tax = 0.0
  24. if self.product.include_sale_tax == 1
  25. self.tax = ((PosConfig.first.tax_percent / 100) * self.amount).round(2)
  26. end
  27. self.total = (self.amount - self.discount) + self.tax
  28. return true
  29. end
  30. end