pointsale.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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." },
  35. length: {
  36. maximum: 3, too_long: "El maximo de caracteres debe ser %{count}.",
  37. minimum: 3, too_short: "El minimo de caracteres debe ser %{count}."
  38. },
  39. uniqueness: { message: "El prefijo ya fue utilizado, favor de especificar otro." }
  40. #validates_attachment_content_type :img_pointsale, :content_type => /\Aimage\/.*\Z/
  41. ##--- Tipo de vistas / consultas
  42. scope :vigentes, -> { where("status != 0").order(" status ASC, name ASC") }
  43. scope :activos, -> { where( "status = 1").order(" name ASC") }
  44. scope :ignore_current, ->(pointsale_id) { where.not(id: pointsale_id) }
  45. # def has_stock
  46. def total_products(pointsale_id)
  47. unless pointsale_id.nil?
  48. Pointsale.find(pointsale_id).products.where("stock > 0").sum(:stock).to_i
  49. end
  50. end
  51. def self.get_pointsale(id, option)
  52. if option == "open_cash_register"
  53. Pointsale.open_cash_registers.find(id)
  54. end
  55. end
  56. def get_open_cash_register
  57. # OpenCashRegister.where(:status => 0, :user_id => self.id)
  58. self.open_cash_registers.where(:status => 0).last
  59. end
  60. def self.delete_duplicates_available
  61. ActiveRecord::Base.connection.exec_query('SELECT DISTINCT (a.product_id), a.pointsale_id,
  62. COUNT (a.product_id) AS productos FROM available_products AS a WHERE
  63. (SELECT COUNT (b.product_id) FROM available_products as b
  64. WHERE b.product_id = a.product_id AND b.pointsale_id = a.pointsale_id
  65. GROUP BY b.product_id, b.pointsale_id ) > 1 GROUP BY a.product_id, a.pointsale_id
  66. ORDER BY productos DESC').each do | row |
  67. availables = AvailableProduct.where(:product_id => row["product_id"], :pointsale_id => row["pointsale_id"]).order('stock desc')
  68. con_mayor_stock = availables[0]
  69. diferentes = 0
  70. availables.each do |ap|
  71. if ap.id != con_mayor_stock.id
  72. if ap.stock == 0 || ap.stock == con_mayor_stock.stock
  73. ap.destroy
  74. end
  75. end
  76. end
  77. end
  78. return 'termine carnalit0'
  79. end
  80. end