class WorkingTimes::Invoice
Class about creating invoice This class is designed to build data_dir/invoices/current_term/invoice.tex from data_dir/invoices/template.tex
Constants
- WDAYS
Attributes
company[R]
path_template[R]
salary_per_hour[R]
tax_rate[R]
Public Class Methods
new()
click to toggle source
path_template
: String salary_per_hour
: Integer tax_rate
: Float company : String
# File lib/working_times/invoice.rb, line 17 def initialize h_invoice_info = invoice_info @path_template = h_invoice_info['template'] @salary_per_hour = h_invoice_info['salaryPerHour'] @tax_rate = h_invoice_info['taxRate'] @company = h_invoice_info['company'] end
Public Instance Methods
build()
click to toggle source
# File lib/working_times/invoice.rb, line 32 def build puts 'Currently, it is not available to build pdf with latexmk.' puts 'Wait for new version!' end
generate()
click to toggle source
# File lib/working_times/invoice.rb, line 26 def generate create_dir_invoice_current_term makeup_worktable generate_invoice_from_template end
Private Instance Methods
beginning_of_month(rfc3339)
click to toggle source
# File lib/working_times/invoice.rb, line 88 def beginning_of_month(rfc3339) Date.parse(rfc3339).beginning_of_month end
calculate_worktime(st_rfc3339, fi_rfc3339, rest_sec)
click to toggle source
# File lib/working_times/invoice.rb, line 96 def calculate_worktime(st_rfc3339, fi_rfc3339, rest_sec) Time.parse(fi_rfc3339) - Time.parse(st_rfc3339) - rest_sec.to_i end
create_dir_invoice_current_term()
click to toggle source
# File lib/working_times/invoice.rb, line 39 def create_dir_invoice_current_term FileUtils.mkdir_p(dir_invoice_current_term) end
format_worktable_row(date, comment = nil, started_at = nil, finished_at = nil, rest = nil, worktime = nil)
click to toggle source
date : Date
comment : String started_at, finished_at : String (RFC3339) rest, worktime : Integer
# File lib/working_times/invoice.rb, line 104 def format_worktable_row(date, comment = nil, started_at = nil, finished_at = nil, rest = nil, worktime = nil) "#{date.to_s(:jp_date)} & " \ "#{WDAYS[date.wday]} & " \ "#{comment || '-'} & " \ "#{started_at.nil? ? '-' : Time.parse(started_at).to_s(:only_hm)} & " \ "#{finished_at.nil? ? '-' : Time.parse(finished_at).to_s(:only_hm)} & " \ "#{rest.nil? ? '-' : parse_second_to_hm_str(rest.to_i)} & " \ "#{worktime.nil? ? '-' : parse_second_to_hm_str(worktime)} \\\\ \\hline" end
generate_invoice_from_template()
click to toggle source
# File lib/working_times/invoice.rb, line 67 def generate_invoice_from_template template = File.readlines(path_template) File.open(path_invoice_current_term, 'w') do |f| template.each do |t| t.gsub!(/##COMPANY##/, company) t.gsub!(/##WORKTIME##/, parse_second_to_hh_str(@allworktime_on_sec)) t.gsub!(/##ACTUALWORKTIME##/, parse_second_to_hm_str(@allworktime_on_sec)) t.gsub!(/##SALARYPERHOUR##/, salary_per_hour.to_s) t.gsub!(/##SALARY##/, salary.to_s) t.gsub!(/##TAXRATE##/, "#{(tax_rate * 100).to_i}\\%") t.gsub!(/##TAX##/, tax.to_s) t.gsub!(/##SALARYWITHTAX##/, (salary + tax).to_s) # on gsub, '\' means 'replace sub string'. # so we should use block if work as expected t.gsub!(/##WORKTABLE##/) { @worktable.join("\n") } f.write(t) end end end
makeup_worktable()
click to toggle source
# File lib/working_times/invoice.rb, line 43 def makeup_worktable @worktable = [ '\hline', '日付 & 曜日 & 内容 & 出勤 & 退勤 & 休憩 & 労働時間 \\\\ \hline\hline' ] @allworktime_on_sec = 0.0 working_times = CSV.open(path_current_term, headers: true) row = working_times.first date_itr = beginning_of_month(row['started_at']) date_itr.upto(date_itr.end_of_month) do |date| if row.nil? @worktable << format_worktable_row(date) elsif same_day?(row['started_at'], date.rfc3339) worktime = calculate_worktime(row['started_at'], row['finished_at'], row['rest_sec']) @allworktime_on_sec += worktime @worktable << format_worktable_row(date, row['comment'], row['started_at'], row['finished_at'], row['rest_sec'], worktime) row = working_times.readline else @worktable << format_worktable_row(date) end end end
parse_second_to_hh_str(second)
click to toggle source
# File lib/working_times/invoice.rb, line 122 def parse_second_to_hh_str(second) (second / 3600).to_i.to_s end
parse_second_to_hm_str(second)
click to toggle source
second : Float
# File lib/working_times/invoice.rb, line 115 def parse_second_to_hm_str(second) second = second.to_i h = second / 3600 m = (second - 3600 * h) / 60 "#{h}:#{m.to_s.rjust(2, '0')}" end
salary()
click to toggle source
calculate with 'hour' only
# File lib/working_times/invoice.rb, line 127 def salary @salary ||= @allworktime_on_sec.to_i / 3600 * salary_per_hour end
same_day?(one_rfc3339, another_rfc3339)
click to toggle source
# File lib/working_times/invoice.rb, line 92 def same_day?(one_rfc3339, another_rfc3339) Date.parse(one_rfc3339) == Date.parse(another_rfc3339) end
tax()
click to toggle source
# File lib/working_times/invoice.rb, line 131 def tax @tax ||= (salary * tax_rate).to_i end