From 2420bc85e59f4ddb094c6155193131985e2ac97c Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Sun, 22 Sep 2024 20:53:49 -0300 Subject: [PATCH] create report in csv in bling order item --- .../bling_order_items_controller.rb | 69 +++++++++++++++++-- app/views/bling_order_items/index.html.erb | 2 +- config/routes.rb | 6 ++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/app/controllers/bling_order_items_controller.rb b/app/controllers/bling_order_items_controller.rb index b810d3ab..119e7063 100644 --- a/app/controllers/bling_order_items_controller.rb +++ b/app/controllers/bling_order_items_controller.rb @@ -1,5 +1,7 @@ +require 'csv' + class BlingOrderItemsController < ApplicationController - before_action :default_initial_date, :disable_initial_date, :default_final_date + before_action :set_filter_params include Pagy::Backend inherit_resources actions :all, except: %i[new create] @@ -7,7 +9,16 @@ class BlingOrderItemsController < ApplicationController def index authorize Customer - index! + bling_order_items = filtered_bling_order_items + + respond_to do |format| + format.html do + @pagy, @bling_order_items = pagy(bling_order_items) + end + format.csv do + send_data generate_csv(bling_order_items), filename: "bling_order_items_report_#{Date.today}.csv" + end + end end def show @@ -43,6 +54,20 @@ def destroy end end + def export_csv + authorize Customer + @bling_order_items = BlingOrderItem.where(account_id: current_tenant) + .by_status(params['status'] || BlingOrderItemStatus::ALL) + .date_range(params['initial_date'], params['final_date']) + .by_store(params['store_id'] || BlingOrderItemStore::ALL) + + respond_to do |format| + format.csv do + send_data generate_csv(@bling_order_items), filename: "bling_order_items_report_#{Date.today}.csv" + end + end + end + protected def collection @@ -62,8 +87,25 @@ def permitted_params private + def set_filter_params + @default_status_filter = params['status'] || BlingOrderItemStatus::ALL + @default_store_filter = params['store_id'] || BlingOrderItemStore::ALL + @default_initial_date = params['initial_date'].presence + @default_final_date = params['final_date'].presence || Date.today + @default_initial_date = nil if params['disable_initial_date'].present? + end + + def filtered_bling_order_items + items = BlingOrderItem.where(account_id: current_tenant) + .by_status(@default_status_filter) + .by_store(@default_store_filter) + + items = items.date_range(@default_initial_date, @default_final_date) if @default_initial_date.present? + items + end + def default_initial_date - @default_initial_date = params['initial_date'] || Date.today + @default_initial_date = params['initial_date'].presence || Date.today end # Dashboard has links to each status. From its point of view, it does not @@ -73,6 +115,25 @@ def disable_initial_date end def default_final_date - @default_final_date = params['final_date'] || Date.today + @default_final_date = params['final_date'].presence || Date.today + end + + def generate_csv(items) + CSV.generate(headers: true) do |csv| + csv << ['ID', 'Bling Order ID', 'Bling ID', 'Status', 'Store', 'Marketplace Code', 'Date', 'Value'] + + items.each do |item| + csv << [ + item.id, + item.bling_order_id, + item.bling_id, + item.situation_id_humanize, + item.store_id_humanize, + item.marketplace_code_id, + item.date.strftime(t('date.formats.default')), + item.value + ] + end + end end end diff --git a/app/views/bling_order_items/index.html.erb b/app/views/bling_order_items/index.html.erb index 450a749f..b41f8f3b 100644 --- a/app/views/bling_order_items/index.html.erb +++ b/app/views/bling_order_items/index.html.erb @@ -3,7 +3,7 @@

<%= t('activerecord.models.bling_order_items.other') %>

- <% button_to "Faça algo útil", '#', class: "btn btn-success", method: :get %> + <%= link_to "Exportar CSV", bling_order_items_path(format: :csv, status: @default_status_filter, store_id: @default_store_filter, initial_date: @default_initial_date, final_date: @default_final_date, disable_initial_date: params[:disable_initial_date]), class: "btn btn-success" %>
diff --git a/config/routes.rb b/config/routes.rb index 66ec21d6..2d1b5385 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -148,4 +148,10 @@ resources :warehouses + resources :bling_order_items do + collection do + get :export_csv + end + end + end