Skip to content

Instantly share code, notes, and snippets.

@nicolasmarcal
Created July 16, 2015 11:39
Show Gist options
  • Save nicolasmarcal/2bbcbaa65df18e46d1ef to your computer and use it in GitHub Desktop.
Save nicolasmarcal/2bbcbaa65df18e46d1ef to your computer and use it in GitHub Desktop.
# -*- encoding : utf-8 -*-
[Date.parse("2015-05-01"), Date.parse("2015-04-01")].each do |date|
Octopus.each do
Location.bioritmo_or_franchising.each do |location|
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => "Geral")
sheet1.row(0).concat %w{CONSULTOR VENDAS META PORCENTAGEM COMISSÃO}
goals = location.goals.sales.find(:all, :conditions => { :date => date })
location_goal = Goal.new(:location_id => location.id, :kind => :sales, :date => date)
results = goals.group_by(&:user)
total = { :current_value => 0, :final => 0 }
index = 0
results.each do |user, goals|
goal = goals.first
total[:current_value] += goal.current_value
total[:final] += goal.final
sheet1.row(index + 1).concat [user.name, goal.current_value, goal.final, "#{goal.real_percentage}%", "#{goal.commission_amount.round(2)}"]
index += 1
results = goal.commission_statement
user_sheet = book.create_worksheet(:name => user.name)
user_sheet.row(0).concat %w{Plano Matrícula Parcelas Valor PrimeiraVisita? InvestimentoInicial ReavaliaçãoMédica Cadastro? Agendamento? Bonus Total}
results.each_with_index do |result, result_index|
user_sheet.row(result_index + 1).concat [ result[:plan], result[:person_id], result[:installments], "#{result[:commission].round(2)}", I18n.t("#{result[:first_visit]}_option"), "#{result[:initial_investment].round(2)}", result[:medical_revaluation], I18n.t("#{result[:complete_registration]}_option"), I18n.t("#{result[:valid_appointment]}_option"), "#{result[:bonus]}", "#{result[:total].round(2)}" ]
end
user_sheet.row(results.count + 2).concat ["Total:", "#{results.sum{|r| r[:total]}.round(2) }"]
end
real_percentage = (total[:current_value].to_f * 100 / total[:final]).round rescue 0
percentage = real_percentage > 100 ? 100 : real_percentage
sheet1.row(results.size + 2).concat ["Total:", "#{total[:current_value].round(2)}", "#{total[:final].round(2)}", "#{percentage}%"]
Dir.mkdir("/tmp/comissoes/") unless Dir.exist?("/tmp/comissoes/")
book.write("/tmp/comissoes/COMISSIOES_#{location.name}_#{date}.xls")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment