pointsale.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. class Pointsale < ActiveRecord::Base
  2. ##--- Associaciones
  3. has_and_belongs_to_many :expensesconcepts
  4. has_and_belongs_to_many :products, join_table: :available_products
  5. has_many :purchases
  6. has_many :pre_purchases
  7. has_many :suppliers, through: :purchases
  8. has_many :inventories
  9. has_many :users
  10. has_many :credits
  11. has_many :credit_payments
  12. has_many :cash_registers
  13. has_many :open_cash_registers, through: :cash_registers
  14. has_many :cash_outs, through: :open_cash_registers
  15. has_many :sales, through: :open_cash_registers
  16. has_many :expenses, through: :open_cash_registers
  17. has_many :sales_details, through: :sales
  18. has_many :product_wastes
  19. has_many :transfers
  20. has_many :sellers
  21. has_many :products_returns
  22. has_many :sales_details, through: :sales
  23. accepts_nested_attributes_for :users
  24. validates_associated :users, on: :create
  25. # has_attached_file :img_pointsale, :styles => { :medium => "250x80>", :thumb => "50x50>" }, :default_url => "/images/:style/missing.png"
  26. enum status: [:erased, :active, :inactive]
  27. attr_accessor :skip_name_validation
  28. attr_accessor :skip_products_validation
  29. mount_uploader :img_pointsale, ImageUploader
  30. ##--- Llevar registro de Actividad del usuario
  31. audited
  32. ##--- Validaciones previas de guardar
  33. validates :name, presence: { message: "Debe capturar el nombre del punto de venta." }, unless: :skip_name_validation
  34. validates :prefix, presence: { message: "Debe indicar el prefijo del punto de venta." }, length: { maximum: 3, too_long: "El maximo de caracteres debe ser %{count}.", minimum: 3, too_short: "El minimo de caracteres debe ser %{count}." }, uniqueness: { message: "El prefijo ya fue utilizado, favor de especificar otro." }
  35. # validates_attachment_content_type :img_pointsale, :content_type => /\Aimage\/.*\Z/
  36. ##--- Tipo de vistas / consultas
  37. scope :vigentes, -> { where("status != 0").order("status ASC, name ASC") }
  38. scope :activos, -> { where("status = 1").order("name ASC") }
  39. scope :ignore_current, ->(pointsale_id) { where.not(id: pointsale_id) }
  40. # def has_stock
  41. def total_products(pointsale_id)
  42. unless pointsale_id.nil?
  43. Pointsale.find(pointsale_id).products.where("stock > 0").sum(:stock).to_i
  44. end
  45. end
  46. def self.get_pointsale(id, option)
  47. if option == "open_cash_register"
  48. Pointsale.open_cash_registers.find(id)
  49. end
  50. end
  51. def get_open_cash_register
  52. open_cash_registers.where(status: 0).last
  53. end
  54. def can_open_cash_register?
  55. crs = cash_registers.activos
  56. if crs.size == 1
  57. # solo hay una caja, checar si esta abierta
  58. # si esta abierta entonces, regresar false, para que no puedan abrirla otra vez
  59. !open_cash_registers.abiertas.any?
  60. else
  61. # mas de una caja, checar si hay alguna que se pueda abrir
  62. opened_cash_registers = OpenCashRegister.where(cash_register_id: crs.pluck(:id), status: 0)
  63. # si el punto de venta tiene 2 cajas y nomas esta abierta una, regresar true para que puedan abrir la otra
  64. crs.size > opened_cash_registers.size ? true : false
  65. end
  66. end
  67. def self.delete_duplicates_available
  68. ActiveRecord::Base.connection.exec_query('SELECT DISTINCT (a.product_id), a.pointsale_id,
  69. COUNT (a.product_id) AS productos FROM available_products AS a WHERE
  70. (SELECT COUNT (b.product_id) FROM available_products as b
  71. WHERE b.product_id = a.product_id AND b.pointsale_id = a.pointsale_id
  72. GROUP BY b.product_id, b.pointsale_id ) > 1 GROUP BY a.product_id, a.pointsale_id
  73. ORDER BY productos DESC').each do |row|
  74. availables = AvailableProduct.where(product_id: row["product_id"], pointsale_id: row["pointsale_id"]).order('stock desc')
  75. con_mayor_stock = availables[0]
  76. diferentes = 0
  77. availables.each do |ap|
  78. next unless ap.id != con_mayor_stock.id
  79. if ap.stock.zero? || ap.stock == con_mayor_stock.stock
  80. ap.destroy
  81. end
  82. end
  83. end
  84. 'termine carnalit0'
  85. end
  86. end