Browse Source

category mod

Jose Miguel Ledon Nieblas 8 years ago
parent
commit
68f3ef7186
1 changed files with 29 additions and 19 deletions
  1. 29 19
      app/models/category.rb

+ 29 - 19
app/models/category.rb

@@ -1,22 +1,22 @@
 class Category < ActiveRecord::Base
-	##--- Associaciones
-	has_and_belongs_to_many :products
-	belongs_to :parent, :class_name => "Category"
-  has_many :children, -> { where "status!= 0" }, :class_name => "Category", :foreign_key => 'parent_id'
+  ##--- Associaciones
+  has_and_belongs_to_many :products
+  belongs_to :parent, class_name: "Category"
+  has_many :children, -> { where "categories.status!= 0" }, class_name: "Category", foreign_key: 'parent_id'
 
-	enum status: [ :erased, :active, :inactive ]
+  enum status: [:erased, :active, :inactive]
 
   ##--- Llevar registro de Actividad del usuario
   audited
 
-	##--- Validaciones previas de guardar
-	validates :category , presence: { message: "Debe capturar el nombre de la línea de producto." }
+  ##--- Validaciones previas de guardar
+  validates :category, presence: { message: "Debe capturar el nombre de la línea de producto." }
 
-	##--- Tipo de vistas / consultas
-	scope :vigentes, -> { where( "status != 0").order(" status ASC, category ASC") }
-	scope :activos, -> { where( "status = 1").order(" category ASC") }
-	scope :activos_padre, -> { where( "status = 1 AND parent_id = 0").order(" category ASC") }
-	scope :get_parents, -> { where( "parent_id = 0 and status != 0").order(" category ASC") }
+  ##--- Tipo de vistas / consultas
+  scope :vigentes, -> { where("status != 0").order("status ASC, category ASC") }
+  scope :activos, -> { where("status = 1").order("category ASC") }
+  scope :activos_padre, -> { where("categories.status = 1 AND categories.parent_id = 0").order("categories.category ASC") }
+  scope :get_parents, -> { where("categories.parent_id = 0 and categories.status != 0").order("categories.category ASC") }
 
   def descendents
     self_and_descendents - [self]
@@ -30,8 +30,21 @@ class Category < ActiveRecord::Base
     where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id")
   end
 
+  def sales_by_category(pointsale_id)
+    @pointsale = Pointsale.find(pointsale_id)
+    @category_parents = Category.get_parents
+    @sales = Pointsale.find(pointsale_id).sales.activas
+    puts "PUNTO DE VENTA: #{@pointsale.name}"
+    puts "CATEGORIAS: #{@category_parents.count}"
+    puts "TOTAL DE VENTAS: #{@sales.count}"
+    @category.each do |category|
+      # category.children.each do |childre_of_category|
+      # end
+    end
+  end
+
   def self.tree_sql_for(instance)
-    tree_sql =  <<-SQL
+    tree_sql = <<-SQL
       WITH RECURSIVE search_tree(id, path) AS (
           SELECT id, ARRAY[id]
           FROM #{table_name}
@@ -46,10 +59,7 @@ class Category < ActiveRecord::Base
     SQL
   end
 
-	protected
-
-		before_save  do
-			self.parent_id = 0 if parent_id.nil?
-		end
-
+  before_save do
+    self.parent_id = 0 if parent_id.nil?
+  end
 end