reports_controller.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class ReportsController < ApplicationController
  2. ##--- Breadcrum_rails
  3. add_breadcrumb "Reporte de mínimos y máximos", :min_max_path, only: :min_max
  4. add_breadcrumb "Reporte de compras y egresos por mes", :purchases_per_month_path, only: :purchases_per_month
  5. def min_max
  6. @pointsales = Pointsale.activos
  7. respond_to do |format|
  8. if params[:pointsale_id].present?
  9. @products = AvailableProduct.joins(:product, :categories).activos.where(pointsale_id: params[:pointsale_id]).where("available_products.stock <= available_products.stock_min and available_products.stock_max > ?", 0)
  10. @products = get_products_category(@products, params[:category], params[:subcategory]) if params[:category].present?
  11. format.js
  12. else
  13. @products = AvailableProduct.where(pointsale_id: @pointsales.first.id).where("available_products.stock <= available_products.stock_min and available_products.stock_min > ?", 0)
  14. format.html
  15. end
  16. end
  17. end
  18. def get_products_category(products, category, subcategory)
  19. ids =
  20. if subcategory.present?
  21. subcategory
  22. else
  23. category = Category.find(category)
  24. [category.id, category.children.ids].flatten
  25. end
  26. products.where(categories: { id: ids })
  27. end
  28. def purchases_per_month
  29. if params[:start_date].present? && params[:end_date].present?
  30. @start_date = DateTime.parse(params[:start_date]).in_time_zone(Time.zone).beginning_of_day + 1.days
  31. @end_date = DateTime.parse(params[:end_date]).in_time_zone(Time.zone).end_of_day + 1.days
  32. @purchases = Purchase.includes(:purchase_details).where(created_at: @start_date..@end_date).order("id DESC")
  33. @expenses = Expense.activos.where(created_at: @start_date..@end_date).order("id DESC")
  34. unless params[:location] == "Todos"
  35. location = params[:location].slice(2..-1)
  36. @purchases =
  37. if params[:location][0] == "P"
  38. @purchases.where(pointsale_id: location)
  39. elsif params[:location][0] == "W"
  40. @purchases.where(warehouse_id: location)
  41. end
  42. end
  43. @total_prods = 0
  44. @purchases.each do |purchase|
  45. @total_prods += purchase.purchase_details.sum(:quantity)
  46. end
  47. respond_to do |format|
  48. format.js
  49. end
  50. end
  51. end
  52. end