require 'Date'

def get_last_day_of_month(y,m)
	return Date.new(y.to_i,m.to_i,-1).day.to_s
end

def receivables_helper(receiving_date, cutoff_date)
	# 変数を囲むダブルクォートをとる
	receiving_date.gsub!(/\"/,'')
	cutoff_date.gsub!(/\"/,'')
	(y,m,d) = receiving_date.split("-")
	if(cutoff_date == '99')
		dd = get_last_day_of_month(y,m)
	else
		dd = cutoff_date
	end
	return '"' + y + '-' + m + '-' + dd + '"'
end

csv = '/tmp/receivings.csv'

open(csv) do |file|
  while line = file.gets()
    array = line.split(',')
    array[8] = receivables_helper(array[8],array[10])
    puts array.inject{|r,i| r + "," + i}
  end
end
