diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 1763bf5..df8359e 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -73,7 +73,12 @@ nav.navbar-static-top.navbar-inverse { color: red; } +// statistics view ul.statistics li{ padding-right: 10px; padding-bottom: 10px; +} + +#chart{ + clear: both; } \ No newline at end of file diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index eb4c03c..5c8de5c 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -21,6 +21,42 @@ class ReportsController < ApplicationController end # cada intercambio implica dos movimientos @num_swaps = (num_movements + current_organization.account.movements.count)/2 + # intercambios con el banco + @total_hours += current_organization.account.movements.map{ |a| (a.amount > 0)? a.amount : 0 }.inject(0,:+) + # periodo a mostrar actividades globales + ini, fin = params[:ini], params[:fin] + if ini.present? + # calculo numero de meses + num_months = (fin.to_date.year * 12 + fin.to_date.month) - (ini.to_date.year * 12 + ini.to_date.month) + 1 + date = ini.to_date + # vector para los meses de la gráfica ["Enero", "Febrero",...] + @months_names = [] + # y vectores con los datos para la gráfica + @user_reg_months = [] + @num_swaps_months = [] + @hours_swaps_months = [] + # valores por cada mes + num_months.times{ + @months_names.push(date.strftime("%B")) + @user_reg_months.push(@members.by_month(date).count) + # movimientos de los miembros en dicho mes + swaps_members = @members.map{|a| a.account.movements.by_month(date)} + # movimimentos del banco + swaps_organization = current_organization.account.movements.by_month(date) + # numero de movimientos totales + sum_swaps = (swaps_members.flatten.count + swaps_organization.count)/2 + @num_swaps_months.push(sum_swaps) + # horas intercambiadas + sum_hours = 0 + swaps_members.flatten.each do |s| + sum_hours += (s.amount > 0)? s.amount : 0 + end + sum_hours += swaps_organization.map{ |a| (a.amount > 0)? a.amount : 0 }.inject(0,:+) + sum_hours = sum_hours / 3600.0 if sum_hours > 0 + @hours_swaps_months.push(sum_hours) + date = date.next_month + } + end end end diff --git a/app/models/member.rb b/app/models/member.rb index cb56df8..5f9cd65 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -8,6 +8,8 @@ class Member < ActiveRecord::Base after_create :create_account before_validation :assign_registration_number, :on => :create + scope :by_month, ->(month) { where({ created_at: month.beginning_of_month..month.end_of_month})} + validates :organization_id, presence: true validates :member_uid, presence: true, diff --git a/app/models/movement.rb b/app/models/movement.rb index 397e6f4..ea5d23c 100644 --- a/app/models/movement.rb +++ b/app/models/movement.rb @@ -14,4 +14,6 @@ class Movement < ActiveRecord::Base end has_one :other_side, (->(self_) { where ["NOT movements.id = #{self_.id}"] }), through: :transfer, source: :movements + + scope :by_month, ->(month) { where({ created_at: month.beginning_of_month..month.end_of_month})} end diff --git a/app/views/reports/statistics.html.haml b/app/views/reports/statistics.html.haml index ec2abe5..6d4ee31 100644 --- a/app/views/reports/statistics.html.haml +++ b/app/views/reports/statistics.html.haml @@ -18,6 +18,11 @@ = t ".total_hours" .badge = seconds_to_hm @total_hours + %form.navbar-form.navbar-right + .form-group + %input.form-control{type: "date", placeholder: "mm/aaaa inicial", name: "ini", value: params[:ini]} + %input.form-control{type: "date", placeholder: "mm/aaaa final", name: "fin", value: params[:fin]} + %button.btn.btn-default{type: "submit"}= "Mostrar" #chart .panel.panel-default @@ -71,6 +76,54 @@ :javascript window.onload = function () { + //globales + $('#chart').highcharts({ + chart: { + type: 'column' + }, + title: { + text: 'Actividad del banco' + }, + subtitle: { + text: 'Por meses' + }, + xAxis: { + categories: #{@months_names.to_json} + }, + yAxis: { + min: 0, + title: { + text: '' + } + }, + tooltip: { + headerFormat: '{point.key}
| {series.name}: | ' + + '{point.y} |