-
Notifications
You must be signed in to change notification settings - Fork 104
/
articles.rb
126 lines (106 loc) · 4.02 KB
/
articles.rb
1
2
3
4
5
6
7
8
9
10
11
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
41
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
# Copyright (c) 2012-2017, Jungwacht Blauring Schweiz. This file is part of
# hitobito and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito.
module Export::Pdf::Invoice
class Articles < Section
attr_reader :reminder
def render # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
reminder = invoice.payment_reminders.last
move_cursor_to 510
font_size(12) { text title(reminder) }
pdf.move_down 8
text invoice.description
if reminder
pdf.move_down 8
font_size(10) { text reminder.text }
end
pdf.move_down 10
pdf.font_size(8) { articles_table }
total_box unless invoice.hide_total?
pdf.move_down 4
font_size(8) { text invoice.payment_information }
end
private
def title(reminder)
reminder ? "#{reminder.title} - #{invoice.title}" : invoice.title
end
def articles_table
table(articles,
header: true,
column_widths: { 0 => 290, 1 => 40, 2 => 50, 3 => 50, 4 => 50 },
cell_style: { borders: [:bottom],
border_color: 'CCCCCC',
border_width: 0.5,
padding: [2, 0, 2, 0],
inline_format: true })
end
def articles
[
[I18n.t('activerecord.models.invoice_article.one'),
align_right(I18n.t('activerecord.attributes.invoice_item.count')),
align_right(I18n.t('activerecord.attributes.invoice_item.unit_cost')),
align_right(I18n.t('activerecord.attributes.invoice_item.cost')),
align_right(I18n.t('activerecord.attributes.invoice_item.vat_rate'))]
] + article_data
end
def article_data
invoice_items.collect do |it|
[
"<b>#{it.name}</b>\n#{it.description}",
align_right(it.count.to_s),
align_right(helper.number_to_currency(it.unit_cost, unit: '')),
align_right(helper.number_to_currency(it.cost, unit: '')),
align_right(helper.number_to_percentage(it.vat_rate, precision: 1))
]
end
end
def total_box # rubocop:disable Metrics/MethodLength
bounding_box([0, cursor], width: bounds.width) do
font_size(8) do
data = total_data
payments = invoice.payments
pdf.table data, position: :right,
column_widths: { 0 => 100 },
cell_style: { borders: [],
border_color: 'CCCCCC',
border_width: 0.5 } do
last_row_index = data.size.pred
rows(0..last_row_index).padding = [2, 0]
row(last_row_index).font_style = :bold
total_row_index = payments.any? ? last_row_index - payments.count.succ : last_row_index
row(total_row_index).font_style = :bold
row(last_row_index).borders = [:bottom, :top]
row(last_row_index).padding = [5, 0]
row(last_row_index).column(0).padding = [5, 15, 5, 0]
column(1).align = :right
end
end
end
end
def total_data
decorated = invoice.decorate
vat_row = if invoice.calculated[:vat].nonzero?
[I18n.t('invoices.pdf.total_vat'), decorated.vat]
end
payment_data = if invoice.payments.any?
payment_rows +
[[I18n.t('invoices.pdf.amount_open'), decorated.amount_open]]
end
[
[I18n.t('invoices.pdf.cost'), decorated.cost],
vat_row,
[I18n.t('invoices.pdf.total'), decorated.total],
*payment_data
].compact
end
def payment_rows
@payment_rows ||= invoice.payments.map do |p|
[I18n.t('invoices.pdf.payment'), invoice.decorate.format_currency(p.amount)]
end
end
def align_right(content)
pdf.make_cell(content: content, align: :right)
end
end
end