Skip to content

Instantly share code, notes, and snippets.

@aribeiro
Forked from wilkerlucio/payment_request.rb
Created December 7, 2010 18:23
Show Gist options
  • Save aribeiro/732180 to your computer and use it in GitHub Desktop.
Save aribeiro/732180 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'activesupport'
require 'chronic'
require 'optparse'
DAY_VALUE = 0 # regular value per day
WDAY_REGULAR = 1..5 # range for regular days 1..5 means from monday to friday
EXTRAS_VALUES = {
:h => 0, # value for an extra day with half time
:f => 0 # value for an full extra day
}
options = {:billing_date => Date.current}
optparse = OptionParser.new do |opts|
# to generate charge for extra days you can use the -e option, examples:
#
# ./payment_request -e 10f,11h
#
# example above will charge day 10 for full day extra and day 11 as half day,
# note these days should be in the range of current charge time, otherwise
# they will be ignored
opts.on('-e', '--extra EXTRA') do |extra|
options[:extra] = extra
end
opts.on('-b', '--billing BILLING_DATE') do |billing|
billing_date = Chronic.parse(billing)
options[:billing_date] = Chronic.parse(billing) if billing_date
end
end
optparse.parse!
extras = []
if options[:extra]
options[:extra].split(",").each do |extra|
if extra =~ /(\d+)(f|h)/
extras << [$1.to_i, $2]
end
end
end
def compute_start_finish(start, finish)
start_print = start.strftime("%b %d")
end_print = finish.strftime(start.month == finish.month ? "%d" : "%b %d")
[start_print, end_print]
end
def compute_segment(segment)
start = segment.first[0]
finish = segment.last[0]
ammount = segment.sum { |s| s[1] }
sl, fl = compute_start_finish(start, finish)
["#{sl} ~ #{fl} - regular time: R$#{ammount}", ammount]
end
start_date = Chronic.parse("1 week before last monday", :now => options[:billing_date])
end_date = start_date + 2.weeks - 1.days
start_date = start_date.to_date
end_date = end_date.to_date
last_received = nil
segments = []
current_segment = []
(start_date..end_date).each do |date|
if WDAY_REGULAR.include?(date.wday)
current_segment << [date, DAY_VALUE]
elsif extras.map {|e| e[0]}.include? date.day
segments << compute_segment(current_segment) if current_segment.length > 0
current_segment = []
extra = extras.find { |e| date.day == e[0] }
value = EXTRAS_VALUES[extra[1].to_sym]
segments << [date.strftime("%b %d") + " - extra time: R$#{value}", value]
else
segments << compute_segment(current_segment) if current_segment.length > 0
current_segment = []
end
end
segments << compute_segment(current_segment) if current_segment.length > 0
total_ammount = segments.sum { |s| s[1] }
start_date_print, end_date_print = compute_start_finish(start_date, end_date)
output = <<EOS
Hi Laura
Im requesting the payment for the period of #{start_date_print} to #{end_date_print}.
#{segments.map {|s| s[0]}.join("\n")}
total: R$#{total_ammount}
EOS
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment