Class: Pdf::Document::Check

Inherits:
BaseService
  • Object
show all
Includes:
Base
Defined in:
app/services/pdf/document/check.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

MICR_FONT_PATH =
'data/fonts/GnuMICR.ttf'.freeze
CHECK_FONT =
'MICR'.freeze

Constants included from Base

Base::FONT, Base::NIMBUS_SANS_PATH, Base::NIMBUS_SANS_PATH_BOLD, Base::WY_LOGO_PATH

Instance Method Summary collapse

Constructor Details

#initialize(check:, outgoing_payment:) ⇒ Check

Returns a new instance of Check.



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
# File 'app/services/pdf/document/check.rb', line 12

def initialize(check:, outgoing_payment:)
  @check ||= check
  @outgoing_payment ||= outgoing_payment
  @outgoing_payment = @check&.outgoing_payment if outgoing_payment.nil?
  @outgoing_payment_items ||= @outgoing_payment_items
  @outgoing_payment_items ||= check&.outgoing_payment&.outgoing_payment_items
  @payee ||= @payee
  @check&.outgoing_payment&.outgoing_payment_items&.group_by(&:payee)&.each { |p, opi| payee = p } if @payee.nil?
  @company = @outgoing_payment&.company
  @company_address = @company&.address
  @bank_account = @outgoing_payment&.
  @bank = @bank_account&.bank
  @bank_address = @bank&.address
  @payee_address = @outgoing_payment.address.nil? ? OutgoingPayment.billing_address(@payee, @outgoing_payment_items) || @check.address.to_s : @check.address.to_s
  @payee_name = if @outgoing_payment.address.nil? || @outgoing_payment.address.company_name_override.nil?
                  @check.payee
                else
                  @outgoing_payment&.address&.company_name_override.to_s
                end
  @total_gross = @outgoing_payment_items&.sum { |pi| pi.gross_amount }
  @total_payment = @outgoing_payment_items&.sum { |pi| pi.amount }
  @check_number = @check.check_number.to_s.rjust(5, '0')
  @amount = @outgoing_payment_items.sum { |pi| pi.amount }
  @mod_amount = Monetize.parse(@amount).format(symbol: false)
  @mod_amount = "$#{@mod_amount.rjust(13, '*')}"
  @amount_in_words = Check.number_as_sentence(@amount).ljust(100, '*')
  @font_path = Rails.root.join('data/fonts/GnuMICR.ttf')
  @base64_font = Base64.strict_encode64(File.read(@font_path))
end

Instance Method Details

#callObject



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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/services/pdf/document/check.rb', line 42

def call
  composer = build_composer(margin: [105, 20, 35, 20])

  define_styles(composer)

  l = composer.document.layout
  page = composer.document.pages[0]
  canvas = page.canvas
  page_width  = page.box(:media).width
  page_height = page.box(:media).height

  logo_path = Rails.public_path.join('images/pdf/wylogo-bw.png').to_s

  check_number_text = @check_number
  font_name = "#{FONT} bold"
  font_size = 12

  canvas.font(font_name, size: font_size)

  text_width = text_width(composer, check_number_text, 12).round

  x_position = page_width - 35 - text_width
  y_position = page_height - 30

  canvas.image(logo_path, at: [50, page_height - 85], width: 48)
  canvas.font("#{FONT} bold", size: 12)
  canvas.text(@company&.legal_name.to_s, at: [110, page_height - 52])
  canvas.font(FONT, size: 12)
  canvas.text("#{@company_address&.street1} #{@company_address&.street2}", at: [110, page_height - 65])
  canvas.text("#{@company_address&.city} #{@company_address&.state_code} #{@company_address&.zip}", at: [110, page_height - 77])
  canvas.font("#{FONT} bold", size: 10)
  canvas.text(@bank&.name.to_s, at: [310, page_height - 30])
  canvas.font(FONT, size: 10)
  canvas.text("#{@bank_address&.street1} #{@bank_address&.street2}", at: [310, page_height - 41])
  canvas.text("#{@bank_address&.city} #{@bank_address&.state_code} #{@bank_address&.zip}", at: [310, page_height - 52])
  canvas.text(@bank&.phone_number.to_s, at: [310, page_height - 63])
  canvas.text(@bank&.bank_identifier.to_s, at: [310, page_height - 74])
  canvas.font("#{FONT} bold", size: 12)
  canvas.text(@check_number, at: [x_position, y_position])
  canvas.font(FONT, size: 12)
  canvas.text('DATE', at: [page_width - 143, page_height - 45])
  canvas.font(FONT, size: 11)
  canvas.text(@check&.date&.to_fs(:crm_default).to_s, at: [page_width - 92, page_height - 44])
  canvas.line_width(1) do
    canvas.line(page_width - 110, page_height - 47, page_width - 35, page_height - 47).stroke
  end

  payee_line1_cells = [
    [
      { content: l.text('PAY TO THE ORDER OF', style: { font_size: 12 }), col_span: 5 },
      { content: l.text(@payee_name, style: { font: "#{FONT} bold", font_size: 12 }), col_span: 11 },
      { content: l.text(@mod_amount, style: { font: "#{FONT} bold", font_size: 12, text_align: :right }), col_span: 3 }
    ]
  ]

  block = ->(_total_rows) do
    ->(cell) do
      cell.style.border = if cell.column.zero?
                            { width: [0, 0, 0, 0] }
                          else
                            { width: [0, 0, 1, 0] }
                          end

      cell.style.padding = [0, 0, -1, 0]
    end
  end

  composer.table(payee_line1_cells,
                 cell_style: block.call(payee_line1_cells.size), width: 558)

  payee_line2_cells = [
    [
      { content: l.text(@amount_in_words, style: { font: "#{FONT} bold", font_size: 11 }), col_span: 23 },
      { content: l.text('DOLLARS', style: { font: FONT, font_size: 12, text_align: :left }), col_span: 3 }
    ]
  ]

  block = ->(_total_rows) do
    ->(cell) do
      cell.style.border = if cell.column.zero?
                            { width: [0, 0, 1, 0] }
                          else
                            { width: [0, 0, 0, 0] }
                          end

      cell.style.padding = [1, 0, -1, 0]
    end
  end

  composer.table(payee_line2_cells,
                 cell_style: block.call(payee_line2_cells.size), style: { padding: [5, 0, 0, 0] })

  composer.text("\n")

  payee_address_lines = []
  payee_address_lines << @payee_name
  if @payee_address && @payee_address != ''
    if @payee_address.respond_to?(:street1)
      payee_address_lines << @payee_address.street1.to_s
      payee_address_lines << @payee_address.street2.to_s if @payee_address.street2.present?
      payee_address_lines << "#{@payee_address.city}, #{@payee_address.state_code} #{@payee_address.zip}".strip
      payee_address_lines << @payee_address.country_printable_name.to_s
    else
      full_address = @payee_address.to_s
      payee_name = @payee_name.to_s.strip

      address_body_string = if full_address.include?(payee_name)
                              full_address.sub(payee_name, '').strip
                            else
                              full_address.strip
                            end

      address_parts = address_body_string.split(',').map(&:strip)

      if ISO3166::Country.find_country_by_any_name(address_parts.last)
        combined = "#{address_parts[-3]}, #{address_parts[-2]}"
        formatted_parts = address_parts[0..-4] + [combined, address_parts[-1]]
      else
        combined = "#{address_parts[-2]}, #{address_parts[-1]}"
        formatted_parts = address_parts[0..-3] + [combined]
      end

      payee_address_lines += formatted_parts
    end

  end

  payee_address_text = payee_address_lines.compact.select { |x| x.length > 0 }.join("\n")

  composer.container(width: 572, style: { padding: [12, 0, 0, 0] }) do |container|
    container.container(width: 286, style: { align: :left, position: :float }) do |left_container|
      left_container.container(width: 286, style: { align: :right, position: :float }) do |text_box|
        text_box.text(payee_address_text, style: { font: FONT, font_size: 10, padding: [0, 0, 0, 40] })
      end
    end
    container.container(width: 286, style: { align: :right, position: :float }) do |right_container|
      right_container.image(Rails.root.join('extras/signature.jpg').to_s, width: 120, align: :center, style: { border: { width: [0, 0, 1, 0] }, padding: [-5, 0, 5, 0] })
    end
  end

  composer.text("\n")

  composer.text("C000#{@check_number}C A#{@bank.routing_number}A #{@bank_account.}C", style: { font: CHECK_FONT, font_size: 14, align: :left, padding: [15, 0, 0, 0] })

  composer.text("\n\n")

  payment_item_rows = @outgoing_payment_items.map do |pi|
    [
      { content: l.text(pi&.invoice_number.to_s), col_span: 2 },
      { content: l.text(pi&.voucher_number.to_s), col_span: 1 },
      { content: l.text(pi&.invoice_date&.to_fs(:crm_default).to_s), col_span: 1 },
      { content: l.text(truncate(pi&.remark.to_s, length: 35)), col_span: 3 },
      { content: l.text(ActionController::Base.helpers.number_to_currency(pi&.gross_amount.to_s, unit: @outgoing_payment.currency_symbol), style: { text_align: :right }), col_span: 1 },
      { content: l.text(ActionController::Base.helpers.number_to_currency(pi&.amount.to_s, unit: @outgoing_payment.currency_symbol), style: { text_align: :right }), col_span: 1 }
    ]
  end

  payment_item_rows << [
    { content: nil, col_span: 2 }, { content: nil, col_span: 1 }, { content: nil, col_span: 1 }, { content: nil, col_span: 3 },
    { content: l.text(ActionController::Base.helpers.number_to_currency(@total_gross.to_s, unit: @outgoing_payment.currency_symbol), style: { text_align: :right }), col_span: 1 },
    { content: l.text(ActionController::Base.helpers.number_to_currency(@total_payment.to_s, unit: @outgoing_payment.currency_symbol), style: { text_align: :right }), col_span: 1 }
  ]

  payment_header = ->(tb) {
    [[
      { content: l.text('INVOICE', style: :regularNimBold), col_span: 2 },
      { content: l.text('VOUCHER', style: :regularNimBold), col_span: 1 },
      { content: l.text('DATE', style: :regularNimBold), col_span: 1 },
      { content: l.text('REMARK', style: :regularNimBold), col_span: 3 },
      { content: l.text('GROSS', style: :regularNimBold), col_span: 1 },
      { content: l.text('PAYMENT', style: :regularNimBold), col_span: 1 }
    ]]
  }

  composer.table(payment_item_rows, header: payment_header, cell_style: { padding: [2, 2], border: { width: 0 } }, style: { padding: [10, 0, 0, 0] })

  if @outgoing_payment_items.length > 12
    composer.page_style(:content, page_size: :Letter, orientation: :portrait) do |_, style|
      style.frame = style.create_frame(canvas.context, [25, 20, 35, 20])
    end

    composer.new_page(:content)
  end

  composer.text("\n")

  carbon_copy_details = [
    [
      { content: l.text("CHECK # #{@check_number}", style: { font: "#{FONT} bold" }), col_span: 16 }
    ],
    [
      { content: l.text('Bank: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@bank.name, style: { font: FONT, font_size: 10 }), col_span: 11 }
    ],
    [
      { content: l.text('Check: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@check_number, style: { font: FONT, font_size: 10 }), col_span: 11 }
    ],
    [
      { content: l.text('Date: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@check.date.to_fs(:crm_default), style: { font: FONT, font_size: 10 }), col_span: 11 }
    ],
    [
      { content: l.text('Amount: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(ActionController::Base.helpers.number_to_currency(@amount, unit: @outgoing_payment.currency_symbol), style: { font: FONT, font_size: 10 }), col_span: 11 }
    ],
    [
      { content: l.text('BANK ACCOUNT', style: { font: "#{FONT} bold" }), col_span: 16 }
    ],
    [
      { content: l.text('Account: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@bank_account., style: { font: FONT, font_size: 10 }), col_span: 11 }
    ],
    [
      { content: l.text('Routing: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@bank.routing_number, style: :regularNim), col_span: 11 }
    ],
    [
      { content: l.text('BENEFICIARY', style: { font: "#{FONT} bold" }), col_span: 16 }
    ],
    [
      { content: l.text('Name: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(@payee_name, style: :regularNim), col_span: 11 }
    ],
    [
      { content: l.text('Address: ', style: :regularNimBold), col_span: 5 },
      { content: l.text(payee_address_lines.slice(1..).compact.join("\n"), style: { font: FONT, font_size: 10, line_height: 10 }), col_span: 11 }
    ]
  ]

  carbon_copy_items_header = ->(tb) {
    [[
      { content: l.text('VOUCHER', style: :regularNimBold), col_span: 3 },
      { content: l.text('INVOICE', style: :regularNimBold), col_span: 11 },
      { content: l.text('PAYMENT', style: { font: "#{FONT} bold", font_size: 10, text_align: :left }), col_span: 3 }
    ]]
  }

  carbon_copy_items = @outgoing_payment_items.map do |pi|
    [
      { content: l.text(pi.voucher_number.to_s, style: { font: FONT, font_size: 10 }), col_span: 3 },
      { content: l.text(pi.invoice_number(with_prefix: true), style: { font: FONT, font_size: 10 }), col_span: 11 },
      { content: l.text(ActionController::Base.helpers.number_to_currency(pi.amount, unit: @outgoing_payment.currency_symbol), style: { font: FONT, font_size: 10, text_align: :right }), col_span: 3 }
    ]
  end

  carbon_copy_items << [
    { content: l.text('TOTAL:', style: { font: "#{FONT} bold", font_size: 10, text_align: :right }), col_span: 14 },
    { content: l.text(ActionController::Base.helpers.number_to_currency(@total_payment, unit: @outgoing_payment.currency_symbol), style: { font: FONT, font_size: 10, text_align: :right }), col_span: 3 }
  ]

  composer.container do |container|
    container.container(style: { valign: @outgoing_payment_items.length > 12 ? :top : :bottom }) do |bottom_container|
      bottom_container.table(
        [
          [
            l.table(
              carbon_copy_details,
              cell_style: { border: { width: 0 }, padding: [1, 0] }
            ),
            l.table(
              carbon_copy_items,
              header: carbon_copy_items_header,
              cell_style: { border: { width: 0 }, padding: [1, 0] }
            )
          ]
        ],
        column_widths: [-2, -3],
        cell_style: { padding: [2, 2], border: { width: 0 } },
        style: { padding: [0, 0, 70, 0] }
      )
    end
  end

  buffer = StringIO.new
  composer.write(buffer, optimize: true)
  Result.new(pdf: buffer.string, file_name: @check.file_name)
end

#configure_document(document) ⇒ Object



321
322
323
324
# File 'app/services/pdf/document/check.rb', line 321

def configure_document(document)
  super
  document.config['font.map'][CHECK_FONT] = { none: MICR_FONT_PATH }
end

#define_styles(composer) ⇒ Object



326
327
328
329
# File 'app/services/pdf/document/check.rb', line 326

def define_styles(composer)
  composer.style(:regularNim, font: FONT, font_size: 10)
  composer.style(:regularNimBold, font: "#{FONT} bold", font_size: 10)
end

#text_width(composer, text, font_size = 12) ⇒ Object

block = lambda do |total_rows|
lambda do |cell|
cell.style.border = {
color: 'CDCDCD',
width: if cell.row == total_rows - 1
[0.5, 0, 0.5, 0]
else
[0.5, 0, 0, 0]
end
}
end
end



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'app/services/pdf/document/check.rb', line 344

def text_width(composer, text, font_size = 12)
  return 0 if text.nil?

  font_size = font_size.to_f

  layout = HexaPDF::Layout::TextLayouter.new
  layout.style.font = FONT
  layout.style.font_size = font_size

  text_fragments = composer.document.layout.text_fragments(text, style: { font: FONT, font_size: font_size })

  text_box = HexaPDF::Layout::TextBox.new(items: text_fragments)

  frame_width = 1000
  frame = HexaPDF::Layout::Frame.new(0, 1000, frame_width, 5000)

  result = frame.fit(text_box)

  result.box.width
end

#truncate(text, length:) ⇒ Object



365
366
367
368
369
# File 'app/services/pdf/document/check.rb', line 365

def truncate(text, length:)
  return text if text.length <= length

  "#{text[0...length]}..."
end