|
|
@@ -30,17 +30,23 @@ class Category < ActiveRecord::Base
|
|
|
where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id")
|
|
|
end
|
|
|
|
|
|
- def sales_by_category(pointsale_id)
|
|
|
+ def self.sales_by_category(pointsale_id)
|
|
|
@pointsale = Pointsale.find(pointsale_id)
|
|
|
@category_parents = Category.get_parents
|
|
|
- @sales = Pointsale.find(pointsale_id).sales.activas
|
|
|
+ @sales_details = Pointsale.find(pointsale_id).sales_details.includes(:sale).where('sales.status != 1')
|
|
|
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
|
|
|
+ puts "TOTAL DE PRODUCTOS VENDIDOS: #{@sales_details.count}"
|
|
|
+
|
|
|
+ cats = Hash.new
|
|
|
+ @category_parents.each do |category|
|
|
|
+ products_ids = category.children.includes(:products).pluck('products.id')
|
|
|
+ prods_from_category = @sales_details.where('product_id in (?) ', products_ids).count
|
|
|
+ percent_of_this_cat = (prods_from_category * 100) / @sales_details.count
|
|
|
+ # puts "CATEGORIA: #{category.category} #{percent_of_this_cat}%"
|
|
|
+ cats[category.category.to_s] = percent_of_this_cat unless percent_of_this_cat.zero?
|
|
|
end
|
|
|
+ cats.sort_by { |_key, value| value }.reverse
|
|
|
end
|
|
|
|
|
|
def self.tree_sql_for(instance)
|