Class: Dorsale::BillingMachine::InvoiceSingleVatPdf

Inherits:
ApplicationPdf
  • Object
show all
Includes:
Alexandrie::Prawn::RenderWithAttachments
Defined in:
app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb

Direct Known Subclasses

InvoiceMultipleVatPdf, QuotationSingleVatPdf

Constant Summary collapse

DEBUG =
false
BLACK =
"000000"
WHITE =
"FFFFFF"
GREY =
"808080"
LIGHT_GREY =
"C0C0C0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationPdf

#open!

Methods included from PrawnHelpers

#bb, #btb, #draw_debug_bounds!, #draw_debug_bounds?, #get_image, #page_height, #page_width, #placeholder, #t, #tb

Methods included from UsersHelper

#avatar_img, #default_avatar_url

Methods included from ExpenseGun::ApplicationHelper

#expense_states_for_filters_select

Methods included from Flyboy::ApplicationHelper

#flyboy_reminder_type_units_for_select, #flyboy_reminder_types_for_select, #flyboy_status_for_filters_select, #flyboy_tasks_owners_for_filters_select, #flyboy_tasks_owners_for_select, #flyboy_tasks_tags_for_filters_select, #flyboy_tasks_tags_for_select, #show_tasks_summary, #task_term_values_for_select, #tasks_for

Methods included from CustomerVault::ApplicationHelper

#customer_vault_activity_types_for_select, #customer_vault_event_actions_for_filter_select, #customer_vault_event_contact_types_for_filter_select, #customer_vault_link_form_path, #customer_vault_origins_for_select, #customer_vault_tag_list, #new_event_for, #person_address_blank?, #person_related_people_blank?, #person_social_blank?, #person_types_for_filter_select

Methods included from ApplicationHelper

#billing_machine_invoices_chart, #billing_machine_payment_status_for_filter_select, #billing_machine_quotation_states_for_filter_select, #billing_machine_quotation_states_for_select, #quotation_state_classes

Methods included from Alexandrie::AttachmentsHelper

#attachments_for

Methods included from CommentsHelper

#comments_for, #new_comment_for, #truncate_comment_text, #truncate_comments_in_this_page?

Methods included from ContextHelper

#actions_for, #render_dorsale_page

Constructor Details

#initialize(main_document) ⇒ InvoiceSingleVatPdf

Returns a new instance of InvoiceSingleVatPdf.



13
14
15
16
17
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 13

def initialize(main_document)
  super(page_size: "A4", margin: 1.cm)
  @main_document = main_document
  setup
end

Instance Attribute Details

#main_documentObject (readonly)

Returns the value of attribute main_document.



11
12
13
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 11

def main_document
  @main_document
end

Instance Method Details

#attachmentsObject



19
20
21
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 19

def attachments
  @main_document.try(:attachments) || []
end

#bm_currency(n) ⇒ Object



23
24
25
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 23

def bm_currency(n)
  currency(n, Dorsale::BillingMachine.default_currency)
end

#buildObject



51
52
53
54
55
56
57
58
59
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 51

def build
  repeat :all do
    build_header
    build_footer
  end

  build_middle
  build_page_numbers
end

#build_contact_infosObject



111
112
113
114
115
116
117
118
119
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 111

def build_contact_infos
  top    = bounds.top - 4.cm
  width  = (bounds.width / 2) - 1.1.cm
  height = contact_infos_height

  bb top: top, width: width, height: height do
    tb contact_infos_content.to_s, size: 9
  end
end

#build_customerObject



156
157
158
159
160
161
162
163
164
165
166
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 156

def build_customer
  top     = bounds.top - 4.cm
  left    = (bounds.width / 2) + 1.1.cm
  width   = (bounds.width / 2) - 1.1.cm
  height  = 4.5.cm
  padding = 3.mm

  bb top: top, left: left, height: height, width: width, padding: padding, background: LIGHT_GREY do
    tb customer_content.to_s
  end
end

#build_document_infosObject



331
332
333
334
335
336
337
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 331

def build_document_infos
  top    = bounds.top - products_table_height - 5.mm
  height = middle_height - products_table_height - 5.mm
  width  = 10.cm

  btb document_infos_content, top:, height:, width:
end


339
340
341
342
343
344
345
346
347
348
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 339

def build_footer
  top    = bounds.bottom + footer_height
  height = footer_height

  bb top: top, height: height do
    build_footer_top
    build_footer_line
    build_footer_bottom
  end
end


370
371
372
373
374
375
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 370

def build_footer_bottom
  height = footer_bottom_height
  top    = bounds.bottom + height + footer_pagination_height

  btb footer_bottom_content, top:, height:, size: 9
end


359
360
361
362
363
364
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 359

def build_footer_line
  top = bounds.top - footer_top_height - ((footer_height - footer_top_height - footer_bottom_height - footer_pagination_height) / 2)
  stroke do
    horizontal_line bounds.left, bounds.right, at: top
  end
end


355
356
357
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 355

def build_footer_top
  btb footer_top_content, height: footer_top_height, size: 9
end

#build_headerObject



74
75
76
77
78
79
80
81
82
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 74

def build_header
  bb height: header_height do
    build_title
    
    build_contact_infos
    build_subject
    build_customer
  end
end

#build_logoObject



98
99
100
101
102
103
104
105
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 98

def 
  height = logo_height
  width  = logo_width

  bb width: width, height: height do
    image logo_path, fit: [width, height] if logo_path
  end
end

#build_middleObject

def build_customer



168
169
170
171
172
173
174
175
176
177
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 168

def build_middle
  top    = bounds.top - header_height
  height = middle_height

  bb top: top, height: height do
    build_table
    build_total
    build_document_infos
  end
end

#build_page_numbersObject



377
378
379
380
381
382
383
384
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 377

def build_page_numbers
  height = footer_pagination_height
  top    = bounds.bottom + height

  bb top: top, height: height do
    number_pages "page <page>/<total>", align: :right, size: 9
  end
end

#build_subjectObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 121

def build_subject
  top    = bounds.top - 7.5.cm
  width  = (bounds.width / 2) - 1.1.cm
  height = 15.mm

  bb top: top, width: width, height: height do
    if main_document.label.present?
      text "<b>#{main_document.t(:label)} : </b> #{main_document.label}", inline_format: true
    end

    if main_document.date.present?
      move_down 3.mm
      text "<b>#{main_document.t(:date)} : </b> #{date main_document.date}", inline_format: true
    end
  end
end

#build_tableObject



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
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 187

def build_table
  height = products_table_height

  # Empty table to draw lines
  bb height: products_table_height do
    repeat :all do
      float do
        table [["", "", "", "", ""]],
          :column_widths => [
            first_column_width,
            second_column_width,
            third_column_width,
            fourth_column_width,
            last_column_width,
          ],
          :cell_style => {height:} \
        do
          row(0).style       :text_color => BLACK
          row(0).style       :font_style => :bold
          column(0).style    :align      => :left
          column(1..4).style :align      => :right
        end # table
      end # float
    end # repeat all
  end # bb

  # Products table
  bb height: height do
    table_products = [
      [
        main_document.t(:designation).mb_chars.upcase.to_s,
        main_document.t(:quantity).mb_chars.upcase.to_s,
        main_document.t(:unit).mb_chars.upcase.to_s,
        main_document.t(:unit_price).mb_chars.upcase.to_s,
        main_document.t(:line_total).mb_chars.upcase.to_s,
      ],
    ]

    main_document.lines.each do |line|
      table_products.push [
        line.label,
        number(line.quantity).gsub(",00", "").gsub(".00", ""),
        line.unit,
        bm_currency(line.unit_price),
        bm_currency(line.total),
      ]
    end

    table table_products,
      :column_widths => [
        first_column_width,
        second_column_width,
        third_column_width,
        fourth_column_width,
        last_column_width,
      ],
      :header => true,
      :cell_style => {border_width: 0} \
    do
      row(0).font_style = :bold
      row(0).border_width =
        1,
        cells.style { |c| c.align = c.column.zero? ? :left : :right }
    end # table
  end # bb
end

#build_titleObject



84
85
86
87
88
89
90
91
92
93
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 84

def build_title
  top    = bounds.top - 1.5.cm
  height = 1.cm

  title = "<b>#{main_document.t}#{main_document.tracking_id}</b>"

  bb top: top, height: height do
    tb title, size: 20, align: :center
  end
end

#build_totalObject

build_table



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
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 254

def build_total
  top    = bounds.top - products_table_height - 5.mm
  height = middle_height - products_table_height - 5.mm

  bb top: top, height: height do
    table_totals = [[]]

    if has_discount
      table_totals.push [
        main_document.t(:commercial_discount).mb_chars.upcase.to_s,
        bm_currency(-main_document.commercial_discount),
      ]
    end

    table_totals.push [
      main_document.t(:total_excluding_taxes).mb_chars.upcase.to_s,
      bm_currency(main_document.total_excluding_taxes),
    ]

    vat_rate = number(main_document.vat_rate)
    table_totals.push [
      "#{main_document.t(:vat).mb_chars.upcase} #{percentage vat_rate}",
      bm_currency(main_document.vat_amount),
    ]

    if has_advance
      table_totals.push [
        main_document.t(:advance).mb_chars.upcase.to_s,
        bm_currency(main_document.advance),
      ]

      table_totals.push [
        main_document.t(:total_including_taxes).mb_chars.upcase.to_s,
        bm_currency(main_document.balance),
      ]
    else
      table_totals.push [
        main_document.t(:total_including_taxes).mb_chars.upcase.to_s,
        bm_currency(main_document.total_including_taxes),
      ]
    end

    table table_totals,
      :column_widths => [fourth_column_width, last_column_width],
      :cell_style    => {border_width: [0, 1, 0, 0]},
      :position      => :right do
        row(-1).style :font_style => :bold
        column(0).padding_right = 0.2.cm
        row(-1).borders = [:top, :right]
        row(-1).border_width = 1
        cells.style do |c|
          c.align = :right
        end
      end

    stroke do
      rectangle [(bounds.right - fourth_column_width - last_column_width), bounds.top], (fourth_column_width + last_column_width), (bounds.top-cursor)
    end
  end
end

#contact_infos_contentObject



107
108
109
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 107

def contact_infos_content
  placeholder __method__
end

#contact_infos_heightObject



31
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 31

def contact_infos_height;  30.mm; end

#customer_contentObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 138

def customer_content
  return if main_document.customer.nil?

  content = []
  content << main_document.customer.name
  content << main_document.customer.address.street
  content << main_document.customer.address.street_bis
  content << "#{main_document.customer.address.zip} #{main_document.customer.address.city}"
  content << main_document.customer.address.country

  if main_document.customer.try(:european_union_vat_number).present?
    content << (main_document.customer.t(:european_union_vat_number) + " : ")
    content << main_document.customer.european_union_vat_number
  end

  content.select(&:present?).join("\n")
end

#document_infos_contentObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 315

def document_infos_content
  txt = []

  if main_document.try(:payment_term).present?
    txt << "<b>#{main_document.t :payment_term}</b> : #{main_document.payment_term}"
  end

  if main_document.try(:expires_at).present?
    txt << "<b>#{main_document.t :expires_at}</b> : #{date main_document.expires_at}"
  end

  txt << main_document.comments

  txt.select(&:present?).join("\n\n")
end

#first_column_widthObject



41
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 41

def first_column_width;  7.6.cm; end


366
367
368
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 366

def footer_bottom_content
  placeholder __method__
end


35
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 35

def footer_bottom_height;    15.mm; end


33
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 33

def footer_height;           40.mm; end


36
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 36

def footer_pagination_height; 5.mm; end

TODO



351
352
353
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 351

def footer_top_content
  placeholder __method__
end


34
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 34

def footer_top_height;       15.mm; end

#fourth_column_widthObject



44
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 44

def fourth_column_width; 2.9.cm; end

#has_advanceObject



179
180
181
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 179

def has_advance
  main_document.try(:advance) && main_document.advance.to_d != BigDecimal("0.0")
end

#has_discountObject



183
184
185
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 183

def has_discount
  main_document.try(:commercial_discount) && main_document.commercial_discount.to_d != BigDecimal("0.0")
end

#header_heightObject

rubocop:disable Style/SingleLineMethods



28
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 28

def header_height;         90.mm; end

#last_column_widthObject

rubocop:enable Style/SingleLineMethods



47
48
49
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 47

def last_column_width
  bounds.width - first_column_width - second_column_width - third_column_width - fourth_column_width
end

#logo_heightObject



29
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 29

def logo_height;           32.mm; end

#logo_pathObject



95
96
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 95

def logo_path
end

#logo_widthObject



30
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 30

def logo_width;            50.mm; end

#middle_heightObject



38
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 38

def middle_height;         14.cm; end

#products_table_heightObject



39
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 39

def products_table_height; 90.mm; end

#second_column_widthObject



42
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 42

def second_column_width; 2.4.cm; end

#setupObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 61

def setup
  font_root = ::Dorsale::Engine.root.join("app/assets/fonts")
  font_families.update( # rubocop:disable Rails/SaveBang
    "BryantPro" => {
      normal: "#{font_root}/BryantPro-Regular.ttf",
      bold:   "#{font_root}/BryantPro-Bold.ttf",
    },
  )

  font("BryantPro")
  font_size 10
end

#third_column_widthObject



43
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 43

def third_column_width;  2.5.cm; end