mirror of
https://git.coop/cotech/fund.git
synced 2025-10-04 03:54:33 +00:00
Generate invocies from the members.csv
This commit updates the previous template to inline the two images as data URIs and embed ERB template markers for the data. The ruby script added in this commit interpolates the data from the members.csv file into the template and generates PDFs using wkhtmltopdf.
This commit is contained in:
66
scripts/generate_invoices.rb
Normal file
66
scripts/generate_invoices.rb
Normal file
@ -0,0 +1,66 @@
|
||||
require 'csv'
|
||||
require 'erb'
|
||||
require 'date'
|
||||
|
||||
FIRST_INVOICE_NUMBER = 50
|
||||
|
||||
output_dir = File.join(File.dirname(__FILE__), "..", "invoices", "sent", "2022")
|
||||
|
||||
members_fn = File.read(File.join(File.dirname(__FILE__), "..", "members.csv"))
|
||||
members = CSV.parse(members_fn, headers: true)
|
||||
|
||||
template_fn = File.read(File.join(File.dirname(__FILE__), "..", "template", "template.html"))
|
||||
|
||||
class Invoice
|
||||
def initialize(data, number)
|
||||
@coop_name = data['coop_name']
|
||||
@contact_address = data['contact_address']
|
||||
@members = data['members']
|
||||
@number = number
|
||||
end
|
||||
|
||||
def date
|
||||
Date.today.strftime('%d %B %Y')
|
||||
end
|
||||
|
||||
def number
|
||||
"%04d" % @number
|
||||
end
|
||||
|
||||
def members
|
||||
@members.to_i
|
||||
end
|
||||
|
||||
def total
|
||||
members * 52
|
||||
end
|
||||
|
||||
def name
|
||||
@coop_name
|
||||
end
|
||||
|
||||
def basename
|
||||
name.downcase.gsub(' ', '_').gsub('.', '')
|
||||
end
|
||||
|
||||
def address
|
||||
@contact_address.gsub("//", "\n")
|
||||
end
|
||||
|
||||
def get_binding
|
||||
binding()
|
||||
end
|
||||
end
|
||||
|
||||
members.each_with_index do |member, index|
|
||||
invoice = Invoice.new(member, FIRST_INVOICE_NUMBER + index)
|
||||
renderer = ERB.new(template_fn)
|
||||
result = renderer.result(invoice.get_binding)
|
||||
|
||||
output_html_fn = File.join(output_dir, invoice.basename + '.html')
|
||||
output_pdf_fn = File.join(output_dir, invoice.basename + '.pdf')
|
||||
|
||||
File.write(output_html_fn, result)
|
||||
|
||||
system("wkhtmltopdf #{output_html_fn} #{output_pdf_fn}")
|
||||
end
|
Reference in New Issue
Block a user