Skip to content

Instantly share code, notes, and snippets.

@guipdutra
Created June 5, 2013 16:05
Show Gist options
  • Save guipdutra/5715093 to your computer and use it in GitHub Desktop.
Save guipdutra/5715093 to your computer and use it in GitHub Desktop.
class AccountingAccount < Accounting::Model
attr_accessible :account_plan_configuration_id, :function,
:title, :nature_balance, :nature_information, :contract_id,
:nature_balance_variation, :bookkeeping, :surplus_indicator,
:movimentation_kind, :debt_type, :detailing_required_thirteenth,
:bank_account_id, :retention_kind, :retention_subtype,
:regulatory_act_id, :parent_id, :child_code
attr_accessor :child_code
has_enumeration_for :nature_balance
has_enumeration_for :nature_information
has_enumeration_for :nature_balance_variation
has_enumeration_for :movimentation_kind
has_enumeration_for :bookkeeping, :create_helpers => true
has_enumeration_for :retention_kind
has_enumeration_for :retention_subtype
has_enumeration_for :debt_type, :with => AccountingAccountDebtType
belongs_to :account_plan_configuration
belongs_to :bank_account
belongs_to :regulatory_act
belongs_to :contract
belongs_to :parent, :class_name => 'AccountingAccount'
has_many :accounting_account_extra_budget_revenues, :dependent => :restrict
has_many :extra_budget_revenues, :dependent => :restrict
has_many :pledges, :dependent => :restrict
has_many :accountancy_movements, :dependent => :restrict
delegate :mask, :to => :account_plan_configuration, :allow_nil => true
delegate :account_number, :bank, :balance, :digit, :description,
:to => :bank_account, :allow_nil => true, :prefix => true
delegate :act_number, :signature_date, :to => :regulatory_act, :allow_nil => true, :prefix => true
delegate :contract_number, :to => :contract, :allow_nil => true
delegate :creditors, :signature_date, :to => :contract, :allow_nil => true, :prefix => true
validates :account_plan_configuration, :child_code, :function, :title,
:nature_balance, :nature_information, :nature_balance_variation,
:movimentation_kind, :bookkeeping, :presence => true
validates :title, :uniqueness => true
validates :retention_kind, :presence => true, :if => :retention_subtype_present?
validates :contract, :presence => true, :if => :debt_type?
validate :code_must_match_account_plan_configuration_mask
validate :code_uniqueness
validate :retention_subtype_belongs_to_retention_kind
before_validation :fill_code
before_save :unless_debt_type_clean_regulatory_act_and_contract
orderize :code
def self.filter
query = scoped
query = query.where { code.eq(options[:code]) } if options[:code].present?
query = query.where { code.eq(options[:title]) } if options[:title].present?
query
end
scope :analyticals, where { bookkeeping.eq(Bookkeeping::ANALYTIC) }
scope :funded_debt, where { debt_type.not_eq(nil) }
scope :term, lambda { |q|
where {
(code.like("#{q}%") | title.like("#{q}%")) & (code.like("%00"))
}
}
scope :with_nature_information, lambda { |value|
where { nature_information.eq(value) if value.present? }
}
def to_s
"#{ code } - #{ title }"
end
def nature_info_is_patrimonial?
if nature_information == 'patrimonial'
if surplus_indicator == ''
errors.add(:surplus_indicator, :blank)
end
end
end
def filled_masked_code
splitted_masked_code_filled.join('.')
end
def child_mask
splitted_masked_code_unfilled.join('.').gsub('0', '9')
end
def balance
credit_accountancy_movements.sum(:amount) - debit_accountancy_movements.sum(:amount)
end
protected
def unless_debt_type_clean_regulatory_act_and_contract
unless debt_type?
self.regulatory_act = nil
self.contract = nil
end
end
def retention_subtype_belongs_to_retention_kind
return unless retention_kind.present? && retention_subtype.present?
allowed_subtypes = {
RetentionKind::DEPOSITS_AND_CONSIGNMENTS => [
RetentionSubtype::INSS,
RetentionSubtype::RPPS,
RetentionSubtype::IRRF,
RetentionSubtype::ISSQN,
],
RetentionKind::TREASURY_DEBT => [
RetentionSubtype::ARO
],
RetentionKind::CURRENT_ASSETS => [
RetentionSubtype::CITY_COUNCIL_TWELFTH,
RetentionSubtype::RETURN_OF_CASH_TO_PREFECTURE,
RetentionSubtype::FINANCIAL_SUPPORT_COVERAGE_FOR_RPPS_FINANCIAL_FAILURE,
RetentionSubtype::FINANCIAL_CONTRIBUTION_TO_RPPS_FINANTIAL_RESERVE,
RetentionSubtype::OTHER_FINANCIAL_CONTRIBUTION_TO_RPPS,
RetentionSubtype::FINANCIAL_SUPPORT_COVERAGE_FOR_RPPS_FINANCIAL_DEFICIT,
RetentionSubtype::FINANCIAL_SUPPORT_COVERAGE_OR_AMORTIZATION_FOR_RPPS_ACTUARIAL_DEFICIT
],
}
unless allowed_subtypes[retention_kind].include? retention_subtype
errors.add(:retention_subtype, :invalid_retention_subtype_for_retention_kind,
:subtype => retention_subtype, :kind => retention_kind)
end
end
def retention_subtype_present?
retention_subtype.present?
end
def splitted_masked_code
return [] unless code.present?
code.split('.')
end
def splitted_masked_code_filled
splitted_masked_code.select { |level| level.to_i > 0 }
end
def splitted_masked_code_unfilled
splitted_masked_code.select { |level| level.to_i == 0 }
end
def fill_code
self.code = if parent.present?
[parent.filled_masked_code, child_code].join('.')
else
child_code
end
end
def code_must_match_account_plan_configuration_mask
return unless account_plan_configuration && code
errors.add(:child_code, :invalid) unless code.to_s.match mask_to_regexp
end
def mask_to_regexp
/\A#{(mask.to_s.each_char.collect { |char| character_map[char] || "\\#{char}" }).join}\z/
end
def character_map
{ "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" }
end
def code_uniqueness
return unless code
accounting_accounts = AccountingAccount.where { |klass| klass.code.eq(code) }
accounting_accounts = accounting_accounts.where { |klass| klass.id.not_eq(id) } if id?
if accounting_accounts.any?
errors.add(:child_code, :taken)
end
end
def debit_accountancy_movements
accountancy_movements.where :movement_type => AccountancyMovementType::DEBIT
end
def credit_accountancy_movements
accountancy_movements.where :movement_type => AccountancyMovementType::CREDIT
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment