ari/jobsworth

View on GitHub
app/controllers/billing_controller.rb

Summary

Maintainability
A
35 mins
Test Coverage
# encoding: UTF-8
require 'csv'

# Massage the WorkLogs in different ways, saving reports for later access
# as well as CSV downloading.
#
class BillingController < ApplicationController

  def index
    @tags = Tag.top_counts(current_user.company)
    @users = User.order('name').where('users.company_id = ?', current_user.company_id).joins('INNER JOIN project_permissions ON project_permissions.user_id = users.id')
    @custom_attributes = current_user.company.custom_attributes.by_type('WorkLog')
    options = params[:report]
    if options
      @worklog_report = WorklogReport.new(self, options)
      @title = @worklog_report.make_billing_title(params[:report][:rows],
                                                  params[:report][:columns]) if params[:report][:type] == '1'
      @column_headers = @worklog_report.column_headers
      @column_totals = @worklog_report.column_totals
      @rows = @worklog_report.rows
      @row_totals = @worklog_report.row_totals
      @total = @worklog_report.total
      @generated_report = @worklog_report.generated_report
    end

    if @column_headers.nil? or @column_headers.length <= 1
      flash[:alert] = t('flash.alert.empty_report') if params[:report]
    end

    render :layout => 'basic'
  end

  def get_csv
    @report = GeneratedReport.where('user_id = ? AND company_id = ?', current_user.id, current_user.company_id).find(params[:id])
    if @report
      send_data(@report.report,
                :type => 'text/csv; charset=utf-8; header=present',
                :filename => @report.filename)
    else
      redirect_to :index
    end
  end
end