Add possibility to add an attachment to an invoice #345
This commit is contained in:
parent
8d5467ab7c
commit
749791bb7a
14 changed files with 75 additions and 1 deletions
|
|
@ -62,6 +62,13 @@ class Finance::InvoicesController < ApplicationController
|
|||
redirect_to finance_invoices_url
|
||||
end
|
||||
|
||||
def attachment
|
||||
@invoice = Invoice.find(params[:invoice_id])
|
||||
type = MIME::Types[@invoice.attachment_mime].first
|
||||
filename = "invoice_#{@invoice.id}_attachment.#{type.preferred_extension}"
|
||||
send_data(@invoice.attachment_data, :filename => filename, :type => type)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_invoice
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
require 'filemagic'
|
||||
|
||||
class Invoice < ActiveRecord::Base
|
||||
|
||||
belongs_to :supplier
|
||||
|
|
@ -7,12 +9,27 @@ class Invoice < ActiveRecord::Base
|
|||
|
||||
validates_presence_of :supplier_id
|
||||
validates_numericality_of :amount, :deposit, :deposit_credit
|
||||
validate :valid_attachment
|
||||
|
||||
scope :unpaid, -> { where(paid_on: nil) }
|
||||
|
||||
attr_accessor :delete_attachment
|
||||
|
||||
# Replace numeric seperator with database format
|
||||
localize_input_of :amount, :deposit, :deposit_credit
|
||||
|
||||
def attachment=(incoming_file)
|
||||
self.attachment_data = incoming_file.read
|
||||
self.attachment_mime = FileMagic.new(FileMagic::MAGIC_MIME).buffer(self.attachment_data)
|
||||
end
|
||||
|
||||
def delete_attachment=(value)
|
||||
if value == '1'
|
||||
self.attachment_data = nil
|
||||
self.attachment_mime = nil
|
||||
end
|
||||
end
|
||||
|
||||
def user_can_edit?(user)
|
||||
user.role_finance? || (user.role_invoices? && !self.paid_on && self.created_by.id == user.id)
|
||||
end
|
||||
|
|
@ -21,4 +38,15 @@ class Invoice < ActiveRecord::Base
|
|||
def net_amount
|
||||
amount - deposit + deposit_credit
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@
|
|||
= f.input :amount, as: :string
|
||||
= f.input :deposit, as: :string
|
||||
= f.input :deposit_credit, as: :string
|
||||
= f.input :attachment, as: :file, hint: t('.attachment_hint')
|
||||
= f.input :delete_attachment, as: :boolean
|
||||
= f.input :note
|
||||
.form-actions
|
||||
= f.submit class: 'btn'
|
||||
|
|
|
|||
|
|
@ -58,6 +58,10 @@
|
|||
%p
|
||||
%b= heading_helper(Invoice, :total) + ':'
|
||||
= number_to_currency total
|
||||
%p
|
||||
%b= heading_helper(Invoice, :attachment) + ':'
|
||||
- if @invoice.attachment_data
|
||||
= link_to t('ui.download'), finance_invoice_attachment_path(@invoice)
|
||||
%p
|
||||
%b= heading_helper(Invoice, :note) + ':'
|
||||
=h @invoice.note
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue