Class: GoodReceipt::Receipt

Inherits:
Base
  • Object
show all
Defined in:
lib/good_receipt/receipt.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Receipt

Returns a new instance of Receipt.



7
8
9
10
11
# File 'lib/good_receipt/receipt.rb', line 7

def initialize(data)
  super()
  validate_data(data)
  @data = data
end

Instance Method Details

#generateObject



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
# File 'lib/good_receipt/receipt.rb', line 57

def generate
  # Render PDF
  report_header(
    'Receipt',
    {
      customer_name: @data[:customer_name],
      date: @data[:date]
    }
  )
  price_table(table_items)
  paid_line

  # Save PDF into the file
  path_name = "receipt-#{@data[:id]}.pdf"
  pdf.render_file path_name

  # Store PDF into the Google Cloud Storage
  storage = ::Google::Cloud::Storage.new(
    project_id: GoodReceipt.configuration.storage_project_id,
    credentials: GoodReceipt.configuration.storage_credentials
  )

  bucket = storage.bucket GoodReceipt.configuration.storage_bucket
  file = bucket.create_file path_name, "/#{@id}/receipt.pdf"
  file.acl.public!

  # Return the URL of the PDF
  file.url
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/good_receipt/receipt.rb', line 39

def paid_line
  pdf.table(
    [
      ['', 'PAID']
    ],
    width: pdf.bounds.width,
    cell_style: { border_width: 0 }
  ) do
    column(0).size = 40

    column(1).align = :right
    column(1).size = 70
    column(1).text_color = Constants::COLOR_GREEN
    column(1).font_style = :bold
    column(1).padding_right = 50
  end
end

#table_itemsObject



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
# File 'lib/good_receipt/receipt.rb', line 13

def table_items
  items = [
    ['Description', 'Qty', 'Unit Price', 'Line Total']
  ]

  @data[:line_items].each do |line_item|
    items << [line_item[:name], '', '', '']

    total = 0.0

    line_item[:items].each do |item|
      total += item[:price].to_f
      items << [item[:name], item[:quantity], "$#{item[:price]}",
                "$#{(item[:price] * item[:quantity]).to_f.round(2)}"]
    end

    items << ['', '', '', "$#{total.round(2)}"]
  end

  items << ['', '', 'Discount', "- $#{@data[:discount].to_f.round(2)}"] if @data[:discount]

  items << ['', '', 'Tax', "$#{@data[:tax]}"] if @data[:tax]

  items << ['', '', 'Total Price', "$#{@data[:total_price]}"]
end