Class: Invoice

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
CustomFields
Defined in:
app/models/invoice.rb

Instance Attribute Summary collapse

Attributes included from CustomFields

#custom_fields

Instance Method Summary collapse

Instance Attribute Details

#delete_attachmentObject

Returns the value of attribute delete_attachment


17
18
19
# File 'app/models/invoice.rb', line 17

def delete_attachment
  @delete_attachment
end

Instance Method Details

#attachment=(incoming_file) ⇒ Object


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

def attachment=(incoming_file)
  self.attachment_data = incoming_file.read
  # allow to soft-fail when FileMagic isn't present and removed from Gemfile (e.g. Heroku)
  self.attachment_mime = defined?(FileMagic) ? FileMagic.new(FileMagic::MAGIC_MIME).buffer(self.attachment_data) : 'application/octet-stream'
end

#net_amountObject

Amount without deposit


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

def net_amount
  amount - deposit + deposit_credit
end

#user_can_edit?(user) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/models/invoice.rb', line 35

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

#valid_attachmentObject (protected)


46
47
48
49
50
51
52
53
# File 'app/models/invoice.rb', line 46

def valid_attachment
  if attachment_data
    mime = MIME::Type.simplified(attachment_mime)
    unless ['application/pdf', 'image/jpeg'].include? mime
      errors.add :attachment, I18n.t('model.invoice.invalid_mime', :mime => mime)
    end
  end
end