14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/contract_ltd.rb', line 14
def self.invoice
if ARGV.empty?
puts <<-EOB
Create a new invoice.slim template:
invoice new
Generate a timesheet (CSV) and invoice (PDF) from existing invoice.slim template.
invoice [-f] [-p] -n [-x|-i [day[am|pm]],[...]]
-f - overwrite existing timesheet/invoice
-n - generate timesheet/invoice for n months ago (-0 to -12)
-x - specify days to exclude (sat/sun excluded anyway)
-i - specify days to include
am|pm - just the morning or afternoon was taken off (half a day)
EOB
exit
end
@days_are_included = ARGV.delete('-i')
@days_are_excluded = ARGV.delete('-x')
if @days_are_included && @days_are_excluded
puts "Days can only be included or excluded. Not both. Specify -i or -x."
exit 1
end
@overwrite = ARGV.delete('-f')
month = (0..12).detect {|n| ARGV.delete("-#{n}") }
if ARGV.size > 0
@days = Hash[*(ARGV.first.split(',').map do |day|
[day.to_i, day =~ /am|pm/ ? 0.5 : 1.0]
end.flatten)]
else
@days = []
end
date = month.month.ago.beginning_of_month.to_date
if File.exist?(timesheet_filename(date)) || File.exist?(invoice_filename(date))
if @overwrite
FileUtils.rm_f(timesheet_filename(date))
FileUtils.rm_f(invoice_filename(date))
else
puts "Existing timesheet and/or invoice detected for #{date}"
puts "Use -f to overwrite"
exit 1
end
end
total_days = write_timesheet(date)
write_pdf(Invoice.new(date, total_days))
end
|