Class: Invoice

Inherits:
ApplicationRecord show all
Includes:
CustomFields, LocalizeInput
Defined in:
app/models/invoice.rb

Instance Attribute Summary collapse

Attributes included from CustomFields

#custom_fields

Instance Method Summary collapse

Methods included from LocalizeInput

parse

Instance Attribute Details

#delete_attachmentObject

Returns the value of attribute delete_attachment.



19
20
21
# File 'app/models/invoice.rb', line 19

def delete_attachment
  @delete_attachment
end

Instance Method Details

#expected_amountObject



45
46
47
48
49
# File 'app/models/invoice.rb', line 45

def expected_amount
  return net_amount unless orders.any?

  orders_sum + orders_transport_sum
end

#net_amountObject

Amount without deposit



29
30
31
# File 'app/models/invoice.rb', line 29

def net_amount
  amount - deposit + deposit_credit
end

#orders_sumObject



33
34
35
36
37
38
39
# File 'app/models/invoice.rb', line 33

def orders_sum
  orders
    .joins(order_articles: [:article_price])
    .sum('COALESCE(order_articles.units_received, order_articles.units_billed, order_articles.units_to_order)' \
      + '* article_prices.unit_quantity' \
      + '* ROUND((article_prices.price + article_prices.deposit) * (100 + article_prices.tax) / 100, 2)')
end

#orders_transport_sumObject



41
42
43
# File 'app/models/invoice.rb', line 41

def orders_transport_sum
  orders.sum(:transport)
end

#user_can_edit?(user) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/invoice.rb', line 24

def user_can_edit?(user)
  user.role_finance? || (user.role_invoices? && !paid_on && created_by.try(:id) == user.id)
end

#valid_attachmentsObject (protected)

validates that the attachments are jpeg, png or pdf



54
55
56
57
58
# File 'app/models/invoice.rb', line 54

def valid_attachments
  attachments.each do |attachment|
    errors.add(:attachments, I18n.t('model.invoice.invalid_mime', mime: attachment.content_type)) unless attachment.content_type.in?(%w[image/jpeg image/png application/pdf])
  end
end