1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

full subscriptions export

This commit is contained in:
Sylvain 2016-07-07 15:31:22 +02:00
parent eec004d1ba
commit 8e8bd3f9eb
9 changed files with 73 additions and 10 deletions

View File

@ -531,6 +531,11 @@ See https://angular-ui.github.io/bootstrap/#uibdateparser-s-format-codes for a l
**BEWARE**: years format with less than 4 digits will result in problems because the system won't be able to distinct dates with the same less significant digits, eg. 50 could mean 1950 or 2050.
EXCEL_DATE_FORMAT
Date format for dates shown in exported Excel files (eg. statistics)
See https://support.microsoft.com/en-us/kb/264372 for a list a available formats.
<a name="i18n-apply"></a>
#### Applying changes

View File

@ -274,7 +274,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state",
##
# Run the current elastic query on the server and return the result as an Excel file
# Open a modal dialog asking the user for details about exporting the statistics tables to an excel file
##
$scope.exportToExcel = ->
options =
@ -532,6 +532,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state",
Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$uibModalInstance', 'dates', 'query', 'key', 'CSRF'
, ($scope, $uibModalInstance, dates, query, key, CSRF) ->
## Retrieve Anti-CSRF tokens from cookies
CSRF.setMetaTags()
## Bindings for date range
@ -546,6 +547,7 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u
## Form action on the above URL
$scope.method = "post"
## Anti-CSRF token to inject into the download form
$scope.csrfToken = angular.element('meta[name="csrf-token"]')[0].content
## Binding of the export type (global / current)
@ -590,6 +592,10 @@ Application.Controllers.controller 'ExportStatisticsController', [ '$scope', '$u
##
# Callback when exchanging the export type between 'global' and 'current view'
# Adjust the query and the requesting url according to this type.
##
$scope.setRequest = ->
if $scope.export.type == 'global'
$scope.actionUrl = '/stats/global/export'

View File

@ -67,7 +67,7 @@
<input name="authenticity_token" type="hidden" ng-value="csrfToken"/>
<input name="_method" type="hidden" ng-value="method"/>
<input name="body" type="hidden" ng-value="query"/>
<input type="submit" class="btn btn-info" value="{{ 'export' | translate }}"/>
<input type="submit" class="btn btn-info" value="{{ 'export' | translate }}" formtarget="_blank"/>
</form>
<button class="btn btn-default" ng-click="cancel()" translate>{{ 'cancel' }}</button>
</div>

View File

@ -25,6 +25,10 @@ class API::StatisticsController < API::ApiController
@results['hits']['hits'].concat(scroll_res['hits']['hits'])
scroll_id = scroll_res['_scroll_id']
end
ids = @results['hits']['hits'].map { |u| u['_source']['userId'] }
@users = User.includes(:profile).where(:id => ids)
type_key = query['query']['bool']['must'][0]['term']['type'].to_s
@subtypes = StatisticType.find_by(key: type_key, statistic_index_id: StatisticIndex.find_by(es_type_key: '#{path}').id).statistic_sub_types
render xlsx: 'export_#{path}.xlsx', filename: "#{path}.xlsx"
end
}

View File

@ -52,6 +52,17 @@ module ApplicationHelper
if (bool) then return :true else return :false end
end
def get_item(array, id, key = nil)
array.each do |i|
if key.nil?
return i if i.id == id
else
return i if i[key] == id
end
end
end
private
## inspired by gems/actionview-4.2.5/lib/action_view/helpers/translation_helper.rb

View File

@ -1,13 +1,26 @@
wb = xlsx_package.workbook
price = wb.styles.add_style :format_code => ''
bold = wb.styles.add_style :b => true
date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format
wb.add_worksheet(name: "Abonnements") do |sheet|
sheet.add_row ['Entrées', @results['hits']['total']]
sheet.add_row ["Chiffre d'affaires", @results['aggregations']['total_ca']['value']]
sheet.add_row ['Âge moyen', @results['aggregations']['average_age']['value']]
sheet.add_row ['Date', 'ID Utilisateur', 'Genre', 'Âge', 'Type', 'Groupe', "Chiffre d'affaire"]
wb.add_worksheet(name: t('export.subscriptions')) do |sheet|
sheet.add_row [t('export.entries'), @results['hits']['total']], :style => [bold, nil], :types => [:string, :float]
sheet.add_row [t('export.revenue'), @results['aggregations']['total_ca']['value']], :style => [bold, nil], :types => [:string, :float]
sheet.add_row [t('export.average_age'), @results['aggregations']['average_age']['value']], :style => [bold, nil], :types => [:string, :float]
sheet.add_row []
sheet.add_row [t('export.date'), t('export.user'), t('export.email'), t('export.phone'), t('export.gender'), t('export.age'), t('export.type'), t('export.group'), t('export.revenue')], :style => bold
@results['hits']['hits'].each do |hit|
sheet.add_row [hit['_source']['date'], hit['_source']['userId'], hit['_source']['gender'], hit['_source']['age'], hit['_source']['planId'], hit['_source']['groupName'], hit['_source']['ca']]
user = get_item(@users, hit['_source']['userId'])
sheet.add_row [
Date::strptime(hit['_source']['date'],'%Y-%m-%d'),
user.profile.full_name,
user.email, user.profile.phone,
t("export.#{hit['_source']['gender']}"),
hit['_source']['age'],
get_item(@subtypes, hit['_source']['subType'], 'key').label,
hit['_source']['groupName'],
hit['_source']['ca']
], :style => [date, nil, nil, nil, nil, nil, nil, nil],
:types => [:date, :string, :string, :string, :string, :integer, :string, :string, :float]
end
end

View File

@ -47,6 +47,7 @@ TIME_ZONE: 'Paris'
WEEK_STARTING_DAY: 'monday'
D3_DATE_FORMAT: '%d/%m/%y'
UIB_DATE_FORMAT: 'dd/MM/yyyy'
EXCEL_DATE_FORMAT: 'dd/mm/yyyy'
OPENLAB_APP_SECRET:
OPENLAB_APP_ID:

View File

@ -255,4 +255,23 @@ fr:
course: "Stage"
workshop: "Atelier"
account_creation: "Création de compte"
project_publication: "Publication de projet"
project_publication: "Publication de projet"
export:
# export des statistiques au format excel
subscriptions: "Abonnements"
entries: "Entrées"
revenue_: "Chiffre d'affaires"
average_age: "Âge moyen"
total: "Total"
date: "Date"
user: "Utilisateur"
email: "Courriel"
phone: "Téléphone"
gender: "Genre"
age: "Âge"
type: "Type"
group: "Groupe"
revenue: "Chiffre d'affaires"
male: "Homme"
female: "Femme"

View File

@ -21,6 +21,7 @@ development:
week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %>
d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %> # .dump is needed as the value may start by a '%', see https://github.com/tenderlove/psych/issues/75
uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %>
excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%>
rails_locale: <%= ENV["RAILS_LOCALE"] %>
moment_locale: <%= ENV["MOMENT_LOCALE"] %>
summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %>
@ -45,6 +46,7 @@ test:
week_starting_day: monday
d3_date_format: '%d/%m/%y'
uib_date_format: dd/MM/yyyy
excel_date_format: dd/mm/yyyy
rails_locale: en
moment_locale: en
summernote_locale: en-US
@ -76,6 +78,7 @@ staging:
week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %>
d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %>
uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %>
excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%>
rails_locale: <%= ENV["RAILS_LOCALE"] %>
moment_locale: <%= ENV["MOMENT_LOCALE"] %>
summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %>
@ -109,6 +112,7 @@ production:
week_starting_day: <%= ENV["WEEK_STARTING_DAY"] %>
d3_date_format: <%= ENV["D3_DATE_FORMAT"].dump %>
uib_date_format: <%= ENV["UIB_DATE_FORMAT"] %>
excel_date_format: <%= ENV["EXCEL_DATE_FORMAT"]%>
rails_locale: <%= ENV["RAILS_LOCALE"] %>
moment_locale: <%= ENV["MOMENT_LOCALE"] %>
summernote_locale: <%= ENV["SUMMERNOTE_LOCALE"] %>