Skip to content

Commit

Permalink
create report in csv in bling order item
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 22, 2024
1 parent 94adc45 commit 2420bc8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
69 changes: 65 additions & 4 deletions app/controllers/bling_order_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
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]
decorates_assigned :bling_order_item

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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/views/bling_order_items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1><%= t('activerecord.models.bling_order_items.other') %></h1>
</div>
<div class="d-flex justify-content-end">
<% 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" %>
</div>
</div>

Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,10 @@

resources :warehouses

resources :bling_order_items do
collection do
get :export_csv
end
end

end

0 comments on commit 2420bc8

Please sign in to comment.