class ReportsController < ApplicationController ##--- Breadcrum_rails add_breadcrumb "Reporte de mínimos y máximos", :min_max_path, only: :min_max add_breadcrumb "Reporte de compras y egresos por mes", :purchases_per_month_path, only: :purchases_per_month add_breadcrumb "Reporte de utilidades", :utilities_report_path, only: :utilities_report def min_max @pointsales = Pointsale.activos respond_to do |format| if params[:pointsale_id].present? @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) @products = get_products_category(@products, params[:category], params[:subcategory]) if params[:category].present? format.js else @products = AvailableProduct.where(pointsale_id: @pointsales.first.id).where("available_products.stock <= available_products.stock_min and available_products.stock_min > ?", 0) format.html end end end def get_products_category(products, category, subcategory) ids = if subcategory.present? subcategory else category = Category.find(category) [category.id, category.children.ids].flatten end products.where(categories: { id: ids }) end def purchases_per_month if params[:start_date].present? && params[:end_date].present? @start_date = DateTime.parse(params[:start_date]).in_time_zone(Time.zone).beginning_of_day + 1.days @end_date = DateTime.parse(params[:end_date]).in_time_zone(Time.zone).end_of_day + 1.days @purchases = Purchase.includes(:purchase_details).where(created_at: @start_date..@end_date).order("id DESC") @expenses = Expense.activos.where(created_at: @start_date..@end_date).order("id DESC") unless params[:location] == "Todos" location = params[:location].slice(2..-1) @purchases = if params[:location][0] == "P" @purchases.where(pointsale_id: location) elsif params[:location][0] == "W" @purchases.where(warehouse_id: location) end end @total_prods = 0 @total_purchased = 0 @total_dollars = 0 @purchases.each do |purchase| next if purchase.cancelled? @total_prods += purchase.purchase_details.sum(:quantity) if purchase.exchange.present? @total_dollars += purchase.total else @total_purchased += purchase.total end end respond_to do |format| format.js end end end def utilities_report if params[:start_date].present? && params[:end_date].present? @start_date = DateTime.parse(params[:start_date]).in_time_zone(Time.zone).beginning_of_day + 1.days @end_date = DateTime.parse(params[:end_date]).in_time_zone(Time.zone).end_of_day + 1.days @sales = if params[:pointsale].blank? Sale.joins(:products, products: :category).where.not(status: 1).where(created_at: @start_date..@end_date).order("id DESC") else Pointsale.find(params[:pointsale]).sales.joins(:products, products: :category).where.not(status: 1).where(created_at: @start_date..@end_date).order("id DESC") end categories = Category.find(params[:subcategory].present? ? params[:subcategory] : params[:category]) if params[:category].present? @sales = @sales.where(categories: { id: [categories.id, categories.children.ids].flatten }) if categories.present? @total_prods = 0 @total_utility = 0 @sales.each do |sale| @total_prods += sale.sales_details.sum(:quantity) cost = 0 sale.sales_details.each do |detail| cost += !detail.product.price_base.nil? ? (detail.product.price_base * detail.quantity) : 0 end utility = sale.total - cost @total_utility += utility end respond_to do |format| format.js end end end end