pre_sale.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. promotion = product.get_promotion
  16. special_price = SpecialPrice.find_by(customer_id: customer_id, product_id: product_id)
  17. discount_per_unit =
  18. if promotion.present?
  19. self.promotion_id = promotion.id
  20. (promotion.percent / 100) * unit_price
  21. elsif special_price.present?
  22. self.special_price_id = special_price.id
  23. special_price.get_unit_discount(unit_price)
  24. else
  25. 0
  26. end
  27. self.unit_price_w_discount = (unit_price - discount_per_unit).round(2)
  28. self.discount = (discount_per_unit * quantity).round(2)
  29. self.amount = (quantity * unit_price_w_discount)
  30. haggle_discount =
  31. if haggle > 0
  32. haggle.round(2)
  33. elsif haggle_percent > 0
  34. ((haggle_percent.to_f / 100) * amount).round(2)
  35. else
  36. 0
  37. end
  38. if haggle_discount.present?
  39. self.discount += haggle_discount
  40. self.amount -= haggle_discount
  41. end
  42. self.tax = 0.0
  43. if product.include_sale_tax?
  44. self.tax = ((PosConfig.first.tax_percent / 100) * amount).round(2)
  45. end
  46. self.total = amount + tax
  47. true
  48. end
  49. end