Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create report in csv in bling order item #341

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading