reports_controller.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. add_breadcrumb "Reporte de utilidades", :utilities_report_path, only: :utilities_report
  6. def min_max
  7. @pointsales = Pointsale.activos
  8. respond_to do |format|
  9. if params[:pointsale_id].present?
  10. @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)
  11. @products = get_products_category(@products, params[:category], params[:subcategory]) if params[:category].present?
  12. format.js
  13. else
  14. @products = AvailableProduct.where(pointsale_id: @pointsales.first.id).where("available_products.stock <= available_products.stock_min and available_products.stock_min > ?", 0)
  15. format.html
  16. end
  17. end
  18. end
  19. def get_products_category(products, category, subcategory)
  20. ids =
  21. if subcategory.present?
  22. subcategory
  23. else
  24. category = Category.find(category)
  25. [category.id, category.children.ids].flatten
  26. end
  27. products.where(categories: { id: ids })
  28. end
  29. def purchases_per_month
  30. if params[:start_date].present? && params[:end_date].present?
  31. @start_date = DateTime.parse(params[:start_date]).in_time_zone(Time.zone).beginning_of_day + 1.days
  32. @end_date = DateTime.parse(params[:end_date]).in_time_zone(Time.zone).end_of_day + 1.days
  33. @purchases = Purchase.includes(:purchase_details).where(created_at: @start_date..@end_date).order("id DESC")
  34. @expenses = Expense.activos.where(created_at: @start_date..@end_date).order("id DESC")
  35. unless params[:location] == "Todos"
  36. location = params[:location].slice(2..-1)
  37. @purchases =
  38. if params[:location][0] == "P"
  39. @purchases.where(pointsale_id: location)
  40. elsif params[:location][0] == "W"
  41. @purchases.where(warehouse_id: location)
  42. end
  43. end
  44. @total_prods = 0
  45. @total_purchased = 0
  46. @total_dollars = 0
  47. @purchases.each do |purchase|
  48. next if purchase.cancelled?
  49. @total_prods += purchase.purchase_details.sum(:quantity)
  50. if purchase.exchange.present?
  51. @total_dollars += purchase.total
  52. else
  53. @total_purchased += purchase.total
  54. end
  55. end
  56. respond_to do |format|
  57. format.js
  58. end
  59. end
  60. end
  61. def utilities_report
  62. if params[:start_date].present? && params[:end_date].present?
  63. @start_date = DateTime.parse(params[:start_date]).in_time_zone(Time.zone).beginning_of_day + 1.days
  64. @end_date = DateTime.parse(params[:end_date]).in_time_zone(Time.zone).end_of_day + 1.days
  65. @sales =
  66. if params[:pointsale].blank?
  67. Sale.joins(:products, products: :categories).where.not(status: 1).where(created_at: @start_date..@end_date).order("id DESC")
  68. else
  69. Pointsale.find(params[:pointsale]).sales.joins(:products, products: :categories).where.not(status: 1).where(created_at: @start_date..@end_date).order("id DESC")
  70. end
  71. categories = Category.find(params[:subcategory].present? ? params[:subcategory] : params[:category]) if params[:category].present?
  72. @sales = @sales.where(categories: { id: [categories.id, categories.children.ids].flatten }) if categories.present?
  73. @total_prods = 0
  74. @total_utility = 0
  75. @sales.each do |sale|
  76. @total_prods += sale.sales_details.sum(:quantity)
  77. cost = 0
  78. sale.sales_details.each do |detail|
  79. cost += (detail.product.price_base * detail.quantity)
  80. end
  81. utility = sale.total - cost
  82. @total_utility += utility
  83. end
  84. respond_to do |format|
  85. format.js
  86. end
  87. end
  88. end
  89. end