Skip to content

Instantly share code, notes, and snippets.

@tinnguyenz
Created October 24, 2014 04:41
Show Gist options
  • Save tinnguyenz/046bb5bc44cee112bd0e to your computer and use it in GitHub Desktop.
Save tinnguyenz/046bb5bc44cee112bd0e to your computer and use it in GitHub Desktop.
class FinancialStatement < ActiveRecord::Base
# Financial statement does not use task anymore. This association will be removed.
belongs_to :task
# Phase actually is a FinancialStatement in the client sheet.
belongs_to :phase, :class_name => "FinancialStatement",
:foreign_key => "phase_id",
:conditions => ["sheet_type = ?", "ClientSheet"]
belongs_to :sheet, :polymorphic => true
# Specific sheet_type
# Remember to include the condition: ["financial_statements.sheet_type = ?", 'ServiceSheet'] when use this virtual association.
belongs_to :service_sheet, :foreign_key => "sheet_id"
# Remember to include the condition: ["financial_statements.sheet_type = ?", 'ClientSheet'] when use this virtual association.
belongs_to :client_sheet, :foreign_key => "sheet_id"
belongs_to :client
# Only PDT
has_many :billing_plans, :dependent => :destroy
has_many :orders, :dependent => :destroy
accepts_nested_attributes_for :billing_plans, :allow_destroy => true,
:reject_if => proc {|attrs| attrs['name'].blank? and attrs['total'].to_i == 0 and
attrs['billing_num'].blank? and attrs['bank_account_num'].blank? and
attrs['_destroy'].to_i != 1 }
accepts_nested_attributes_for :orders, :allow_destroy => true,
:reject_if => proc {|attrs| attrs['name'].blank? and attrs['total'] and
attrs['client_address'].blank? and attrs['client_name'].blank? and
attrs['_destroy'].to_i != 1}
include ::Util::CurrenyConfig
config_curreny_attributes(:turnover)
# VALIDATION
# Only for PRJ project
validates :name, :presence => true, :uniqueness => {:scope => [:sheet_id, :sheet_type]}, :if => :is_in_prj_project?
# CALLBACK
before_validation :normalize
def self.total_turnover(sheet_id, sheet_type, task_id)
self.scoped_by_sheet_id_and_sheet_type_and_task_id(sheet_id, sheet_type, task_id).sum(:turnover)
end
def is_in_prj_project?
return (self.sheet_type == "ClientSheet" and self.sheet and self.sheet.project.is_a?(PrjProject))
end
def normalize
if self.name
self.name.strip!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment