This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoice.rb
executable file
·231 lines (171 loc) · 5.84 KB
/
invoice.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env ruby
# This program reads simple YAML documents and generates
# nice looking PDF invoices as output.
#
# Author:: Zane Ashby (mailto:[email protected])
# Copyright:: Copyright (c) 2012 Zane Ashby
# License:: GPLv3
require 'prawn'
require 'prawn/measurement_extensions'
require 'prawn/table'
require 'yaml'
# Add to this class a quick method to add thousands
# separators to a number, for use in currency output.
class String
def add_thousands_separator
number = self.to_s.reverse
number.gsub!(/(\d\d\d)(?=\d)(?!\d*\.)/) do |match|
$1 + ','
end
number.to_s.reverse
end
end
# The main class that reads in the config and each
# invoice document and generates the PDF layout.
class Invoice
def initialize(invoice, output = nil)
# Load config
@config = YAML::load(File.read('config.yml'))
@file_hash = parse_filename(invoice)
# Output to the same name as invoice with pdf extension
@output = @file_hash[:template] + ".pdf"
# Load invoice
@invoice = YAML::load(File.read(@file_hash[:filename]))
@invoice['to'] = @config['clients'][@file_hash[:client]] unless @invoice['to']
#rescue
#$stderr.puts "Couldn't load or parse: #{$1}"
end
def generate
config = @config
invoice = @invoice
file_hash = @file_hash
# Set a currency symbol from config or $ by default
currency_symbol = config['currency_symbol'] || '$'
info = {
Title: "#{file_hash[:title]} Invoice",
Author: config['name']
}
Prawn::Document.generate(@output, info: info) do
default_leading 3.mm
image config['logo']['path'], at: [bounds.width - config['logo']['width'], bounds.height], width: config['logo']['width']
text "Invoice".upcase, size: 36
transparent(0.6) do
text file_hash[:date].strftime('%d %B %Y'), size: 14
text "Invoice No. <b>#{file_hash[:date].strftime '%Y%m%d'}-#{file_hash[:shortcode]}</b>", inline_format: true
end
move_down 1.cm
# To
text invoice['to'].first, style: :bold, size: 16
invoice['to'].drop(1).each do |line|
text line
end
move_down 1.cm
total = 0.0
header = [
{ content: '<b>Item</b>', align: :left },
{ content: '<b>Rate</b>', align: :right },
{ content: '<b>Units</b>', align: :right }
]
if config['line_total']
header << { content: '<b>Total</b>', align: :right }
end
cells = [header]
invoice['items'].each do |item|
line_total = item['rate'] * item['units']
total += line_total
cell = [
item['description'],
{ content: sprintf('%s%.2f', currency_symbol, item['rate']).add_thousands_separator, align: :right },
{ content: item['units'].to_s, align: :right }
]
if config['line_total']
cell << { content: sprintf('%s%.2f', currency_symbol, line_total), align: :right }
end
cells << cell
end
if config['line_total']
column_widths = [360, 60, 60, 60]
else
column_widths = [420, 60, 60]
end
table cells,
header: true,
column_widths: column_widths,
row_colors: ['fcfcfc', 'eeeeee'],
cell_style: {
borders: [:bottom],
inline_format: true,
overflow: :shrink_to_fit}
move_down 1.cm
# Calculate discount
discount = invoice['discount'] || 0.0
total_discount = total * (discount / 100.0)
# Calculate tax
tax = 0.0
if config['tax'] && config['tax']['percentage']
tax = config['tax']['percentage']
end
total_tax = total * (tax / 100.0)
# Remove any amount paid so far
paid = invoice['paid'] || 0.0
# Calculate net
net = total
net = net - total_discount
net = net - paid
net = net + total_tax
text "Total: #{sprintf('%s%.2f', currency_symbol, total).add_thousands_separator}", size: 13, align: :right
if discount != 0.0
text "Discount: #{discount}%", size: 13, align: :right
end
if tax != 0.0
text "#{config['tax']['name']}: #{sprintf('%s%.2f', currency_symbol, total_tax).add_thousands_separator}", size: 13, align: :right
end
if paid != 0.0
text "Amount Paid: #{sprintf('%s%.2f', currency_symbol, paid).add_thousands_separator}", size: 13, align: :right
end
text "Amount Due: #{sprintf('%s%.2f', currency_symbol, net).add_thousands_separator}", size: 14, align: :right, style: :bold
move_down 2.cm
span 420, position: :center do
text config['note'], inline_format: true, align: :center, leading: 1.mm, size: 13
end
bounding_box([0, 1.5.cm], width: bounds.width, height: 2.cm) do
transparent(0.6) do
text config['footer'], inline_format: true, align: :center, size: 13
end
end
end
end
# Parse a filename like "path/to/year-month-day-clientname-title.yml:shortcode"
# and return a hash containing :filename, :template, :shortcode, :date, :client, :title.
def parse_filename(filename)
shortcode = '1'
parts = filename.split(':')
if parts[1]
shortcode = parts[1]
end
filename = parts[0]
basename = File.basename(filename)
filename_no_ext = File.basename(basename, File.extname(basename))
filename_no_ext =~ /(\d+)-(\d+)-(\d+)-(\w+)-([a-zA-Z\-]+)/
{
filename: filename,
template: filename_no_ext,
shortcode: shortcode,
date: Date.new($1.to_i, $2.to_i, $3.to_i),
client: $4,
title: $5
}
end
end
if __FILE__ == $0
# Being run as a command-line utility.
if ARGV.length == 0
$stderr.puts "Usage: invoice <path> [path ...]"
exit 1
end
ARGV.each do |source|
puts "Processing #{source}."
invoice = Invoice.new source
invoice.generate
end
end