2009-01-08 16:33:27 +01:00
|
|
|
class Invoice < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :supplier
|
2016-02-17 16:11:01 +01:00
|
|
|
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
|
2016-02-25 12:56:34 +01:00
|
|
|
has_many :deliveries
|
|
|
|
has_many :orders
|
2009-01-08 16:33:27 +01:00
|
|
|
|
|
|
|
validates_presence_of :supplier_id
|
2012-04-20 18:33:47 +02:00
|
|
|
validates_numericality_of :amount, :deposit, :deposit_credit
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :unpaid, -> { where(paid_on: nil) }
|
2009-01-10 19:36:58 +01:00
|
|
|
|
2012-04-20 18:33:47 +02:00
|
|
|
# Replace numeric seperator with database format
|
|
|
|
localize_input_of :amount, :deposit, :deposit_credit
|
2009-01-29 21:28:22 +01:00
|
|
|
|
2016-02-17 21:07:35 +01:00
|
|
|
def user_can_edit?(user)
|
|
|
|
user.role_finance? || (user.role_invoices? && !self.paid_on && self.created_by.id == user.id)
|
|
|
|
end
|
|
|
|
|
2009-01-29 21:28:22 +01:00
|
|
|
# Amount without deposit
|
|
|
|
def net_amount
|
|
|
|
amount - deposit + deposit_credit
|
|
|
|
end
|
2009-01-08 16:33:27 +01:00
|
|
|
end
|