Skip to content

Commit

Permalink
#9 add total sum to reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsehau Chao committed Nov 8, 2018
1 parent e15e6a4 commit 2743201
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,41 @@ def service_income(year)
def ordered
Report.order(employee_id: :asc, year: :asc, month: :asc)
end

def sum_by_festival(year, festival)
Report
.salary_income(year)
.where(festival_type: festival)
.sum(:festival_bonus)
.to_i
end

def sum_by_employee_salary(year:, employee_id:)
Report
.salary_income(year)
.where(employee_id: employee_id)
.sum_amount
end

def sum_by_employee_service(year:, employee_id:)
Report
.service_income(year)
.where(employee_id: employee_id)
.sum_amount
end

def sum_by_month(year:, month:, tax_code:)
Report
.where(tax_code: tax_code, year: year, month: month)
.sum_amount
end

def sum_amount
Report
.pluck("SUM(amount), SUM(correction), SUM(subsidy_income) * -1")
.flatten
.reduce(0) { |sum, column| sum + column.to_i }
end
end

def adjusted_amount
Expand Down
13 changes: 13 additions & 0 deletions app/views/reports/salary.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
%th 端午
%th 中秋
%th 年終
%th 總計

- @report.each do |row|
%tr
Expand All @@ -25,3 +26,15 @@
%td= number_with_delimiter row[:festival_bonus][:dragonboat]
%td= number_with_delimiter row[:festival_bonus][:midautumn]
%td= number_with_delimiter row[:festival_bonus][:newyear]
%td= number_with_delimiter Report.sum_by_employee_salary(year: params[:year], employee_id: row[:employee][:id])

%tr
%td 總計
%td.hide
%td.hide
- 1.upto(12) do |month|
%td= number_with_delimiter Report.sum_by_month(year: params[:year], month: month, tax_code: 50)
%td= number_with_delimiter Report.sum_by_festival(params[:year], :dragonboat)
%td= number_with_delimiter Report.sum_by_festival(params[:year], :midautumn)
%td= number_with_delimiter Report.sum_by_festival(params[:year], :newyear)
%td
10 changes: 10 additions & 0 deletions app/views/reports/service.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@
%th.hide 戶籍地址
- 1.upto(12) do |month|
%th= "#{month}"
%th 總計
- @report.each do |row|
%tr
%td= link_to row[:employee][:name], employee_path(row[:employee][:id])
%td.hide= row[:employee][:id_number]
%td.hide= row[:employee][:address]
- 12.times do |month|
= render "cell", cell: row[:income][month]
%td= number_with_delimiter Report.sum_by_employee_service(year: params[:year].to_i, employee_id: row[:employee][:id])
%tr
%td 總計
%td.hide
%td.hide
%td= number_with_delimiter Report.sum_by_month(year: (params[:year].to_i - 1), month: 12, tax_code: "9a")
- 1.upto(11) do |month|
%td= number_with_delimiter Report.sum_by_month(year: params[:year], month: month, tax_code: "9a")
%td

0 comments on commit 2743201

Please sign in to comment.