Skip to content

Instantly share code, notes, and snippets.

@chadwtaylor
Created April 30, 2014 19:13
Show Gist options
  • Save chadwtaylor/c7738febf7e9d4afb032 to your computer and use it in GitHub Desktop.
Save chadwtaylor/c7738febf7e9d4afb032 to your computer and use it in GitHub Desktop.
Example files from our rails application
module Person::Accounting
def subledger
@subledger ||= Subledger.new self
end
class Subledger
def initialize(parent)
@parent = parent
end
def subledger_service
@subledger_service ||= SubledgerService.new
end
def ap_accounts
ap_keys.map {|k| subledger_service.new_or_create_ap_account(@parent, k[:key], k[:service_key], k[:description], k[:service_key_description])}
end
def ar_accounts
ar_keys.map {|k| subledger_service.new_or_create_ap_account(@parent, k[:key], k[:service_key], k[:description], k[:service_key_description])}
end
def ap_keys
[true, false].map {|is_royalty| ServiceType.all.map {|s| ap_key(s.subledger.ap_key, is_royalty)}}.flatten
end
def ar_keys
[true, false].map {|is_royalty| ServiceType.all.map {|s| ar_key(s.subledger.ar_key, is_royalty)}}.flatten
end
def ap_key(service, is_royalty)
base = [key, service[:key]]
base << (is_royalty ? "service" : "expense")
base << "ap"
desc = "#{@parent.full_name} - #{service[:description]} - #{(is_royalty ? "Service" : "Expense")}"
return {key: base.join(" ").to_url, description: desc, service_key: service[:key], service_key_description: service[:description]}
end
def ar_key(service, is_royalty)
base = [key, service[:key]]
base << (is_royalty ? "service" : "expense")
base << "ar"
desc = "#{@parent.full_name} - #{service[:description]} - #{(is_royalty ? "Service" : "Expense")}"
return {key: base.join(" ").to_url, description: desc, service_key: service[:key], service_key_description: service[:description] }
end
def external_account
ExternalAccountPerson.where(external_account_id:1, person_id:@parent.id).first_or_create
end
def key
ext = self.external_account
if not ext.account["person-key"]
x = @parent.full_name.downcase
x << " #{@parent.id}"
x = x.to_url
ext.account_add("person-key", x)
end
return ext.account["person-key"]
end
end
end
module SubledgerAPI
def self.new
Subledger.new :key_id => ENV['SUBLEDGER_KEY_ID'],
:identity_id => ENV['SUBLEDGER_IDENTITY_ID'],
:secret => ENV['SUBLEDGER_SECRET'],
:org_id => ENV['SUBLEDGER_ORG_ID'],
:book_id => ENV['SUBLEDGER_BOOK_ID']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment