Module: ReimbursementsHelper

Defined in:
app/helpers/reimbursements_helper.rb

Overview

Helpers for reimbursements' views

Instance Method Summary collapse

Instance Method Details

#can_read_pdf_for?(reimb) ⇒ Boolean

Checks whether the current user should be authorized to read the pdf version of a reimbursement, since the pdf version includes information about the user profile and bank information.

Parameters:

Returns:

  • (Boolean)

    true if authorized



190
191
192
# File 'app/helpers/reimbursements_helper.rb', line 190

def can_read_pdf_for?(reimb)
  can?(:read, reimb) && can?(:read, reimb.user.profile) && (reimb..nil? || can?(:read, reimb.))
end

#check_request_bank_value(account, field) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/reimbursements_helper.rb', line 162

def check_request_bank_value(, field)
  is_iban = .format == 'iban'

  case field.to_s.to_sym
  when :name
    .bank_name
  when :holder
    .holder
  when :iban
    is_iban ? .iban : ''
  when :bic
    is_iban ? .bic : ''
  when :national_code
    is_iban ? '' : .national_bank_code
  when :national_account
    is_iban ? '' : .
  when :country
    is_iban ? '' : country_label(.country_code)
  when :postal_address
    is_iban ? '' : .bank_postal_address
  end
end

Outputs a link to the reimbursement's check request only if everything related to check requests is configured

Parameters:

Returns:

  • (String)

    HTML output



113
114
115
116
117
118
119
# File 'app/helpers/reimbursements_helper.rb', line 113

def check_request_link(reimbursement)
  return '' if Rails.configuration.site['check_request_layout'].blank?
  return '' if Rails.configuration.site['check_request_template'].blank?

  url = check_request_request_reimbursement_path(reimbursement.request)
  link_to t(:check_request), url, class: 'btn btn-default'
end

#check_request_reimb_value(reimb, field) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/reimbursements_helper.rb', line 137

def check_request_reimb_value(reimb, field)
  case field.to_sym
  when :amount
    expenses_sum(reimb, :authorized)
  when :full_name
    reimb.user.profile.full_name
  when :date
    l(Date.today)
  when :postal_address
    reimb.user.profile.postal_address
  when :city
    reimb.user.profile.location
  when :country
    country_label(reimb.user.profile.country_code)
  when :zip_code
    reimb.user.profile.zip_code
  when :phone_number
    reimb.user.profile.phone_number
  when :label
    reimb.label
  when :email
    reimb.user.email
  end
end

#check_request_value(reimb, field) ⇒ String

Outputs the value for the given reimbursement of one of the check request's fields

Parameters:

  • reimb (Reimbursement)

    the reimbursement

  • field (#to_sym)

    the name of one of the fields defined in check requests

Returns:

  • (String)

    value to show in the resulting pdf



127
128
129
130
131
132
133
134
135
# File 'app/helpers/reimbursements_helper.rb', line 127

def check_request_value(reimb, field)
  if field.to_s.start_with?('bank_')
     = reimb.
    field = field[5..-1].to_sym
    check_request_bank_value(, field)
  else
    check_request_reimb_value(reimb, field)
  end
end

#manual_authorized_amount_is_needed_for?(reimb) ⇒ Boolean

Checks whether some manual adjustment of 'authorized amount' is required for a reimbursement

Parameters:

Returns:

  • (Boolean)

    true unless all authorized values can be automatically set



198
199
200
# File 'app/helpers/reimbursements_helper.rb', line 198

def manual_authorized_amount_is_needed_for?(reimb)
  reimb.expenses.any? { |r| !r.authorized_can_be_calculated? }
end

#reimbursement_acceptance_file(reimbursement) ⇒ String

Outputs the download link to acceptance_file, with instructions to attach it if needed

Parameters:

Returns:

  • (String)

    HTML output



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
# File 'app/helpers/reimbursements_helper.rb', line 39

def reimbursement_acceptance_file(reimbursement)
  out = if reimbursement.acceptance_file.blank?
          t('show_for.blank').html_safe
        else
          link_to(reimbursement.acceptance_file.file.filename,
                  request_reimbursement_acceptance_path(reimbursement.request))
        end

  links = []
  links << link_to(t(:pdf_format), request_reimbursement_path(reimbursement.request, format: :pdf)) if can_read_pdf_for?(reimbursement) && reimbursement.editable?
  links << link_to(t(:send_reimbursement_acceptance), new_request_reimbursement_acceptance_path(reimbursement.request), remote: true) if can? :submit, reimbursement

  info = if can? :submit, reimbursement
           t(:reimbursement_acceptance_intro).html_safe
         else
           info = if reimbursement.acceptance_file.blank?
                    t(:reimbursement_acceptance_blank).html_safe
                  else
                    t(:reimbursement_acceptance_update).html_safe
                  end
         end
  info << (:p, links.join(' | ').html_safe, class: 'text-right') unless links.empty?
  out << (:div, info, class: 'alert alert-info') unless info.empty?
  out
end

#reimbursement_attachments(reimbursement) ⇒ String

Outputs a comma-separated list of attachments for a given reimbursement, including the download link

Parameters:

Returns:

  • (String)

    HTML output



12
13
14
15
16
17
18
19
20
# File 'app/helpers/reimbursements_helper.rb', line 12

def reimbursement_attachments(reimbursement)
  if reimbursement.attachments.empty?
    I18n.t('show_for.blank')
  else
    req = reimbursement.request
    links = reimbursement.attachments.map { |a| link_to(a.title, request_reimbursement_attachment_path(req, a)) }
    links.join(', ').html_safe
  end
end

Outputs a comma-separated list of links for a given reimbursement,

Parameters:

Returns:

  • (String)

    HTML output



26
27
28
29
30
31
32
# File 'app/helpers/reimbursements_helper.rb', line 26

def reimbursement_links(reimbursement)
  if reimbursement.links.empty?
    I18n.t('show_for.blank')
  else
    reimbursement.links.map { |a| link_to(a.title, a.url) }.join(', ').html_safe
  end
end

#reimbursement_payments(reimbursement) ⇒ String

Outputs a table containing payments, with edit and destroy links where allowed

Parameters:

Returns:

  • (String)

    HTML output



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
# File 'app/helpers/reimbursements_helper.rb', line 69

def reimbursement_payments(reimbursement)
  payments = reimbursement.payments.order(:date).accessible_by(current_ability)
  if payments.empty?
    (:p, I18n.t('show_for.blank'))
  else
    header = (:th, Payment.human_attribute_name(:date))
    header << (:th, Payment.human_attribute_name(:method))
    header << (:th, Payment.human_attribute_name(:subject))
    header << (:th, Payment.human_attribute_name(:amount), class: 'text-right')
    header << (:th, '')

    req = reimbursement.request
    rows = payments.map do |payment|
      p = (:td, l(payment.date, format: :long))
      p << (:td, payment.method)
      p << (:td, payment.subject)
      p << (:td, number_to_currency(payment.amount, unit: payment.currency), class: 'text-right')
      links = []
      links << link_to(t('helpers.links.edit'), edit_request_reimbursement_payment_path(req, payment)) if can? :update, payment
      if can? :destroy, payment
        links << link_to(t('helpers.links.destroy'),
                         request_reimbursement_payment_path(req, payment),
                         data: { confirm: t('helpers.links.confirm') }, method: :delete)
      end
      links << link_to(Payment.human_attribute_name(:file), file_request_reimbursement_payment_path(req, payment)) if can?(:update, payment) && !payment.file.blank?
      p << if links.empty?
             (:td, '')
           else
             (:td, "#{payment.code} (#{links.join(' | ')})".html_safe)
           end
      (:tr, p.html_safe)
    end

    (:table,
                (:thead, (:tr, header.html_safe)) + (:tbody, rows.join('').html_safe),
                class: 'table table-condensed', id: 'reimbursement-payments')
  end
end