From 647e71e8d6e01b37d09d7a9a773883e49dea7ed5 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Wed, 22 Jul 2020 00:20:43 +0200 Subject: [PATCH] fonctions pour export des utilisateurs en CSV --- .../admin/p_customers_controller.rb | 80 +++++++++++++++++-- app/controllers/application_controller.rb | 2 +- app/models/p_customer.rb | 45 ++++++++++- .../admin/p_customers/_p_customer.html.haml | 31 ++++++- app/views/admin/p_customers/index.html.haml | 12 +-- .../_qi_ordered_table_search_footer.html.haml | 7 +- 6 files changed, 156 insertions(+), 21 deletions(-) diff --git a/app/controllers/admin/p_customers_controller.rb b/app/controllers/admin/p_customers_controller.rb index 1ba3926..038f577 100644 --- a/app/controllers/admin/p_customers_controller.rb +++ b/app/controllers/admin/p_customers_controller.rb @@ -298,13 +298,83 @@ class Admin::PCustomersController < ApplicationController @p_customers = @p_customers.distinct - @p_customers = @p_customers.limit(50) + + + + + respond_to do |format| + format.html{ + + + + params[:per_page] = params[:per_page] || 50 + + per_page = params[:per_page] + page = (params[:page] and params[:page] != "") ? params[:page] : 1 + @p_customers = @p_customers.page(page).per(per_page) + + } + + format.csv { + @headers = [] + @columns = [] + PCustomer.qi_table_order.each do |key, value| + + if value.instance_of? Hash + name = value[:name] + else + name = value + end + + if name != "Actions" + @headers << name.to_s + @columns << key + end + + end + + + + @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv| + csv << @headers + + @p_customers.each do |p_customer| + line = [] + + @columns.each do |column| + + if (p_customer.respond_to?("csv_"+column.to_s)) + line << p_customer.send("csv_"+column.to_s) + elsif (p_customer.respond_to?(column)) + if p_customer.send(column.to_s).class.to_s == "BigDecimal" + line << p_customer.send(column.to_s).to_s.gsub('.', ',') + else + line << p_customer.send(column.to_s) + end + else + line << column.to_s + end + + end + + csv << line + end + + + end + + + @data_to_send = @csv + + send_data @data_to_send, :filename => "export-csv.csv" #, :type => 'text/csv; charset=iso-8859-1; header=present' + + + + } + + end - params[:per_page] = params[:per_page] || 50 - per_page = params[:per_page] - page = (params[:page] and params[:page] != "") ? params[:page] : 1 - @p_customers = @p_customers.page(page).per(per_page) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 064af1f..e264cac 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception - + require "csv" before_action :set_locale diff --git a/app/models/p_customer.rb b/app/models/p_customer.rb index 8c7335a..c0b3627 100644 --- a/app/models/p_customer.rb +++ b/app/models/p_customer.rb @@ -255,8 +255,17 @@ class PCustomer < ApplicationRecord :cc_show_name => {:name => "Nom", :reorder => true}, :mlm_token => {:name => "Code ambassadeur", :reorder => true}, :enabled => "Actif ?", + :test_user => "Utilisateur test ?", :parent => "Ambassadeur", - :particular => "Ville", + + + + :particular_cp => "Code postal", + :particular_city => "Ville", + :particular_country => "Pays", + + :email => "Email", + :tel => "Téléphone", :actions => "Actions" } @@ -549,5 +558,39 @@ class PCustomer < ApplicationRecord end + + + def csv_parent + if self.parent + self.parent.mlm_token.to_s+" - "+self.show_name.to_s + end + end + + + def csv_mlm_token + self.mlm_token if self.ambassadeur + + end + + + def csv_tel + self.particular.tel if self.particular + end + + + def csv_particular_cp + self.particular.cp if self.particular + end + + def csv_particular_city + self.particular.city if self.particular + end + + def csv_particular_country + self.particular.country if self.particular + end + + + end diff --git a/app/views/admin/p_customers/_p_customer.html.haml b/app/views/admin/p_customers/_p_customer.html.haml index 723d01e..b73f2e8 100644 --- a/app/views/admin/p_customers/_p_customer.html.haml +++ b/app/views/admin/p_customers/_p_customer.html.haml @@ -5,6 +5,11 @@ %td -if p_customer.enabled Oui + + -tr[:test_user] = capture do + %td + -if p_customer.test_user + Oui -tr[:parent] = capture do %td @@ -17,6 +22,12 @@ %td -if p_customer.ambassadeur =p_customer.mlm_token + + + -tr[:tel] = capture do + %td + -if p_customer.particular + =p_customer.particular.tel @@ -27,12 +38,26 @@ -tr[:p_customer_cat] = capture do %td= p_customer.p_customer_cat.name if p_customer.p_customer_cat - -tr[:particular] = capture do + -tr[:particular_country] = capture do + %td + -if p_customer.particular + = p_customer.particular.country + + -tr[:particular_cp] = capture do %td -if p_customer.particular = p_customer.particular.cp - = p_customer.particular.city - = p_customer.particular.country + + + + -tr[:particular_city] = capture do + %td + -if p_customer.particular + = p_customer.particular.city + + + + -tr[:actions] = capture do %td.actions diff --git a/app/views/admin/p_customers/index.html.haml b/app/views/admin/p_customers/index.html.haml index d54c87e..3ddee93 100644 --- a/app/views/admin/p_customers/index.html.haml +++ b/app/views/admin/p_customers/index.html.haml @@ -113,17 +113,9 @@ - =link_to ic(:search)+" Rechercher", "#", :class => "btn btn-default btn-qi-search", :onclick => "$(this).closest('form').submit();$(this).html('...');return false;" - - - .results_count - ="(#{@p_customers.total_count}) résultat(s)" - - ="-" - - =select_tag "search[per_page]", options_for_select([50,100,200,500], params[:search][:per_page]) - résultats par page + =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_customers, :csv => true} + diff --git a/app/views/qi/_qi_ordered_table_search_footer.html.haml b/app/views/qi/_qi_ordered_table_search_footer.html.haml index edd4dfe..e92811f 100644 --- a/app/views/qi/_qi_ordered_table_search_footer.html.haml +++ b/app/views/qi/_qi_ordered_table_search_footer.html.haml @@ -1,6 +1,7 @@ =hidden_field_tag :column, params[:column] =hidden_field_tag :direction, params[:direction] + =link_to ic(:search)+" Rechercher", "#", :class => "btn btn-default btn-qi-search", :onclick => "$(this).closest('form').submit();$(this).html('...');return false;" @@ -10,4 +11,8 @@ ="-" =select_tag "search[per_page]", options_for_select([50,100,200,500], params[:search][:per_page]) - résultats par page \ No newline at end of file + résultats par page + + -if csv = csv || nil and csv + ="-" + =link_to "export csv", params.permit!.merge(:format => :csv)