Class: Dorsale::BillingMachine::InvoiceSingleVatPdf

Inherits:
Prawn::Document
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, Alexandrie::Prawn, AllHelpers
Defined in:
app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb

Direct Known Subclasses

InvoiceMultipleVatPdf, QuotationSingleVatPdf

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UsersHelper

#avatar_img

Methods included from Flyboy::ApplicationHelper

#folder_color, #show_tasks_summary, #task_color, #tasks_for

Methods included from Alexandrie::AttachmentsHelper

#attachment_form_for, #attachments_for, #attachments_list_for

Methods included from TextHelper

#currency, #date, #euros, #hours, #info, #number, #percentage, #text2html

Methods included from RoutesHelper

#engine_polymorphic_path

Methods included from PaginationHelper

#paginate

Methods included from LinkHelper

#email_link, #icon_link_to, #tel_link, #twitter_link, #web_link

Methods included from FormHelper

#form_buttons, #horizontal_form_for, #search_form

Methods included from FiltersHelper

#dorsale_time_periods_for_select, #filter_buttons, #filter_reset_button, #filter_submit_button

Methods included from ContextHelper

#actions_for, #context_icon, #context_info, #context_title, #render_contextual

Methods included from CommentsHelper

#comment_form_for, #comments_for, #comments_list_for

Methods included from Dorsale::ButtonHelper

#complete_button, #copy_button, #create_button, #delete_button, #dorsale_button, #download_button, #lock_button, #read_button, #snooze_button, #unlock_button, #update_button

Methods included from Alexandrie::Prawn

#render_with_attachments

Constructor Details

#initialize(main_document) ⇒ InvoiceSingleVatPdf

Returns a new instance of InvoiceSingleVatPdf.



31
32
33
34
35
36
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 31

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

Instance Attribute Details

#main_documentObject (readonly)

Returns the value of attribute main_document.



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

def main_document
  @main_document
end

Instance Method Details

#bm_currency(n) ⇒ Object



17
18
19
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 17

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

#buildObject



57
58
59
60
61
62
63
64
65
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 57

def build
  repeat :all do
    build_header
    build_footer
  end

  build_middle
  build_page_numbers
end

#build_bank_informationsObject



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 397

def build_bank_informations
  top = bounds.top - products_table_height - 20.mm
  height = 1.cm
  width  = 7.5.cm

  bounding_box [bounds.left, top], height: height, width: width do
    draw_bounds_debug
    font_size 9 do
      text "#{main_document.t(:iban)} : #{@id_card.iban}"           if @id_card.iban.present?
      text "#{main_document.t(:bic_swift)} : #{@id_card.bic_swift}" if @id_card.bic_swift.present?
    end
  end
end

#build_commentsObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 361

def build_comments
  return if main_document.comments.blank?

  top    = bounds.top - products_table_height - 35.mm
  height = top - bounds.bottom
  width  = 10.cm

  text_box main_document.comments,
    :at       => [bounds.left, top],
    :height   => height,
    :width    => width,
    :overflow => :shrink_to_fit,
    :size     => 9
end

#build_contactObject



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

def build_contact
  top    = bounds.top - 4.cm
  left   = bounds.left
  width  = bounds.width / 2 - 1.1.cm
  height = 2.5.cm

  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug
    text contact_content, inline_format: true, size: 8
  end
end

#build_customerObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 191

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

  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug
    stroke do
      fill_color LIGHT_GREY
      fill_rounded_rectangle [cursor-bounds.height,cursor], bounds.width, bounds.height, 0
      fill_color BLACK
    end

    bounding_box [bounds.left + padding, bounds.top - padding], height: bounds.height - padding, width: bounds.width - padding do
      text customer_content
    end
  end
end

#build_expiryObject



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

def build_expiry
end


411
412
413
414
415
416
417
418
419
420
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 411

def build_footer
  top = bounds.bottom + footer_height

  bounding_box [0, top], width: bounds.width, height: footer_height do
    draw_bounds_debug
    build_footer_top
    build_footer_line
    build_footer_bottom
  end
end


463
464
465
466
467
468
469
470
471
472
473
474
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 463

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

  text_box footer_bottom_content,
    :at       => [bounds.left, top],
    :height   => height,
    :width    => width,
    :overflow => :shrink_to_fit,
    :size     => 9
end


439
440
441
442
443
444
445
446
447
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 439

def build_footer_line
  # Center line between the two footer parts
  n = footer_top_height + (footer_height - footer_top_height - footer_bottom_height) / 2
  move_down n
  stroke do
    horizontal_rule
    line_width 1
  end
end


426
427
428
429
430
431
432
433
434
435
436
437
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 426

def build_footer_top
  top    = bounds.top
  height = footer_top_height
  width  = bounds.width

  text_box footer_top_content,
    :at       => [bounds.left, top],
    :height   => height,
    :width    => width,
    :overflow => :shrink_to_fit,
    :size          => 9
end

#build_headerObject



80
81
82
83
84
85
86
87
88
89
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 80

def build_header
  bounding_box [0, bounds.top], width: bounds.width, height: header_height do
    draw_bounds_debug
    build_title
    
    build_contact
    build_subject
    build_customer
  end
end

#build_logoObject



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

def 
  return if logo_path.nil?

  height = logo_height
  width  = logo_width

  bounding_box [bounds.left, bounds.top], width: width, height: height do
    draw_bounds_debug
    image logo_path, fit: [width, height]
  end
end

#build_middleObject

def build_customer



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 212

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

  bounding_box [left, top], width: width, height: height do
    build_table
    build_total
    build_payment_conditions
    build_bank_informations
    build_expiry
    build_comments
  end
end

#build_page_numbersObject



476
477
478
479
480
481
482
483
484
485
486
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 476

def build_page_numbers
  height = 5.mm
  top    = bounds.bottom + height
  width  = bounds.width

  bounding_box [0, top], height: height, width: bounds.width do
    float do
      number_pages "page <page>/<total>", align: :right, size: 9
    end
  end
end

#build_payment_conditionsObject



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 376

def build_payment_conditions
  return if main_document.payment_term.blank?

  top    = bounds.top - products_table_height - 5.mm
  height = 15.mm
  width  = 7.5.cm

  txt = "<b>#{main_document.t(:payment_terms)}</b> : #{main_document.payment_term}"

  text_box txt,
    :at            => [bounds.left, top],
    :height        => height,
    :width         => width,
    :overflow      => :shrink_to_fit,
    :inline_format => true,
    :size          => 9
end

#build_subjectObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 153

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

  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug

    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



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

def build_table
  left   = bounds.left
  top    = bounds.top
  width  = bounds.width - left
  height = products_table_height

  # Empty table to draw lines
  bounding_box [left, top], width: width, height: 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: 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 # bounding_box

  # products table
  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug

    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 == 0 ? :left : :right }
    end # table
  end # bounding_box
end

#build_titleObject



91
92
93
94
95
96
97
98
99
100
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 91

def build_title
  top    = bounds.top - 1.5.cm
  left   = bounds.left
  width  = bounds.width
  height = 1.cm
  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug
    text "<b>#{main_document.t}#{main_document.tracking_id}</b>", inline_format: true, size: 20, align: :center
  end
end

#build_totalObject

build_table



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 297

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

  bounding_box [left, top], width: width, height: height do
    draw_bounds_debug

    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.to_s} #{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_address_lineObject



120
121
122
123
124
125
126
127
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 120

def contact_address_line
  [
    @id_card.address1,
    @id_card.address2,
    @id_card.zip,
    @id_card.city,
  ].select(&:present?).join(", ")
end

#contact_contentObject



129
130
131
132
133
134
135
136
137
138
139
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 129

def contact_content
  content = []
  content << "<b>#{@id_card.entity_name}</b>"                                           if @id_card.entity_name.present?
  content << "<b>#{contact_address_line}</b>"                                           if contact_address_line.present?
  content << " "
  content << "<b>#{main_document.t(:your_contact)} : #{@id_card.contact_full_name}</b>" if @id_card.contact_full_name.present?
  content << "<b>#{main_document.t(:contact_phone)} : </b> #{@id_card.contact_phone}"   if @id_card.contact_phone.present?
  content << "<b>#{main_document.t(:contact_fax)} : </b> #{@id_card.contact_fax}"       if @id_card.contact_fax.present?
  content << "<b>#{main_document.t(:contact_email)} : </b> #{@id_card.contact_email}"   if @id_card.contact_email.present?
  content.join("\n")
end

#customer_contentObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 173

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

#draw_bounds_debugObject



488
489
490
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 488

def draw_bounds_debug
  transparent(0.5) { stroke_bounds } if DEBUG
end

#first_column_widthObject



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

def first_column_width;  7.6.cm; end


449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 449

def footer_bottom_content
  infos_text = []
  infos_text << @id_card.entity_name                                                                                if @id_card.entity_name.present?
  infos_text << "#{main_document.t(:info_phone)} : #{@id_card.contact_phone}"                                       if @id_card.contact_phone.present?
  infos_text << "#{main_document.t(:info_fax)} : #{@id_card.contact_fax}"                                           if @id_card.contact_fax.present?
  infos_text << "#{@id_card.contact_email}"                                                                         if @id_card.contact_email.present?
  infos_text << "#{@id_card.legal_form.to_s}"                                                                       if @id_card.legal_form.present?
  infos_text << "#{main_document.t(:capital)} : #{bm_currency @id_card.capital}"                                          if @id_card.capital.present?
  infos_text << "#{main_document.t(:registration)} : #{@id_card.registration_city} #{@id_card.registration_number}" if @id_card.registration_number.present?
  infos_text << "#{main_document.t(:siret)} : #{@id_card.siret}"                                                    if @id_card.siret.present?
  infos_text << "#{main_document.t(:intracommunity_vat)} : #{@id_card.intracommunity_vat}"                          if @id_card.intracommunity_vat.present?
  infos_text = infos_text.join(" - ")
end


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

def footer_bottom_height;  15.mm; end


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

def footer_height;         40.mm; end


422
423
424
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 422

def footer_top_content
  @id_card.custom_info_1.to_s
end


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

def footer_top_height;     15.mm; end

#fourth_column_widthObject



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

def fourth_column_width; 2.9.cm; end

#has_advanceObject



21
22
23
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 21

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

#has_discountObject



25
26
27
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 25

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

#header_heightObject



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

def header_height;         90.mm; end

#last_column_widthObject



53
54
55
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 53

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

#logo_heightObject



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

def logo_height;           32.mm; end

#logo_pathObject



102
103
104
105
106
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 102

def logo_path
  main_document.id_card..path
rescue NoMethodError
  nil
end

#logo_widthObject



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

def logo_width;            50.mm; end

#middle_heightObject



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

def middle_height;         14.cm; end

#products_table_heightObject



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

def products_table_height; 90.mm; end

#second_column_widthObject



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

def second_column_width; 2.4.cm; end

#setupObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/pdfs/dorsale/billing_machine/invoice_single_vat_pdf.rb', line 67

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

  font("BryantPro")
  font_size 10
end

#third_column_widthObject



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

def third_column_width;  2.5.cm; end