Skip to content

Commit

Permalink
add statements
Browse files Browse the repository at this point in the history
  • Loading branch information
anquinn committed May 24, 2021
1 parent fe57c7b commit 355cdd4
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,50 @@ You can also use line_items to flexibly generate and display the table with item
)
```

## Statements

Statements follow the exact same set of steps as receipts, with a few minor changes and have a few extra arguments you can use:

* `issue_date` - Date the invoice was issued

* `start_date` - The start date of the statement period

* `start_date` - The end date of the statement period

* `bill_to` - A string or Array of lines with account details

You can also use line_items to flexibly generate and display the table with items in it, including subtotal, taxes, and total amount.

```ruby
Receipts::Statement.new(
id: "123",
issue_date: Date.today,
start_date: Date.today - 30,
end_date: Date.today,
status: "<b><color rgb='#5eba7d'>PAID</color></b>",
bill_to: [
"GoRails, LLC",
"123 Fake Street",
"New York City, NY 10012",
nil,
"[email protected]",
],
company: {
name: "GoRails, LLC",
address: "123 Fake Street\nNew York City, NY 10012",
email: "[email protected]",
logo: File.expand_path("./examples/gorails.png")
},
line_items: [
["<b>Item</b>", "<b>Unit Cost</b>", "<b>Quantity</b>", "<b>Amount</b>"],
["GoRails Subscription", "$19.00", "1", "$19.00"],
[nil, nil, "Subtotal", "$19.00"],
[nil, nil, "Tax Rate", "0%"],
[nil, nil, "Total", "$19.00"],
],
)
```

## Contributing

1. Fork it ( https://github.com/excid3/receipts/fork )
Expand Down
1 change: 1 addition & 0 deletions lib/receipts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "receipts/version"
require "receipts/receipt"
require "receipts/invoice"
require "receipts/statement"
117 changes: 117 additions & 0 deletions lib/receipts/statement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'prawn'
require 'prawn/table'

module Receipts
class Statement < Prawn::Document
attr_reader :attributes, :id, :company, :custom_font, :line_items, :logo, :message, :product, :subheading, :bill_to, :issue_date, :start_date, :end_date

def initialize(attributes)
@attributes = attributes
@id = attributes.fetch(:id)
@company = attributes.fetch(:company)
@line_items = attributes.fetch(:line_items)
@custom_font = attributes.fetch(:font, {})
@message = attributes.fetch(:message) { default_message }
@subheading = attributes.fetch(:subheading) { default_subheading }
@bill_to = Array(attributes.fetch(:bill_to)).join("\n")
@issue_date = attributes.fetch(:issue_date)
@start_date = attributes.fetch(:start_date)
@end_date = attributes.fetch(:end_date)

super(margin: 0)

setup_fonts if custom_font.any?
generate
end

private

def default_message
"For questions, contact us anytime at <color rgb='326d92'><link href='mailto:#{company.fetch(:email)}?subject=Charge ##{id}'><b>#{company.fetch(:email)}</b></link></color>."
end

def default_subheading
"STATEMENT #%{id}"
end

def setup_fonts
font_families.update "Primary" => custom_font
font "Primary"
end

def generate
bounding_box [0, 792], width: 612, height: 792 do
bounding_box [85, 792], width: 442, height: 792 do
header
charge_details
footer
end
end
end

def header
move_down 60

logo = company[:logo]

if logo.nil?
move_down 32
elsif logo.is_a?(String)
image open(logo), height: 32
else
image logo, height: 32
end

move_down 8
label (subheading % {id: id})

move_down 10

# Cache the Y value so we can have both boxes at the same height
top = y
bounding_box([0, y], width: 200) do
move_down 5
text_box bill_to, at: [0, cursor], width: 200, height: 75, inline_format: true, size: 10, leading: 4, overflow: :shrink_to_fit

end

bounding_box([250, top], width: 200) do
label "STATEMENT DATE"

move_down 5
text issue_date.to_s, inline_format: true, size: 12, leading: 4

move_down 10
label "STATEMENT PERIOD"

move_down 5
text "#{start_date.to_s} - #{end_date.to_s}", inline_format: true, size: 12, leading: 4
end
end

def charge_details
move_down 30

borders = line_items.length - 2

table(line_items, width: bounds.width, cell_style: { border_color: 'cccccc', inline_format: true }) do
cells.padding = 12
cells.borders = []
row(0..borders).borders = [:bottom]
end
end

def footer
move_down 30
text message, inline_format: true, size: 12, leading: 4

move_down 30
text company.fetch(:name), inline_format: true
text "<color rgb='888888'>#{company.fetch(:address)}</color>", inline_format: true
end

def label(text)
text "<color rgb='a6a6a6'>#{text}</color>", inline_format: true, size: 8
end
end
end

0 comments on commit 355cdd4

Please sign in to comment.