pointsale.rb 3.9 KB

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