Skip to content

Commit

Permalink
Add CurrentInvoiceProxy to Debtor allowing us to to debtor.current_in…
Browse files Browse the repository at this point in the history
…voices.build and get a CurrentInvoice initialized with required values from debtor
  • Loading branch information
koppen committed Jul 13, 2011
1 parent 2bd3bdb commit d0541cc
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 22 deletions.
7 changes: 0 additions & 7 deletions README.md
Expand Up @@ -32,17 +32,10 @@ Usage example

# Create invoice for debtor:
invoice = economic.current_invoices.build

invoice.debtor_handle = { :number => debtor.number }
invoice.debtor_name = 'Bob'
invoice.date = Time.now
invoice.due_date = Time.now + 15
invoice.term_of_payment_handle = { :id => 1 }

invoice.currency_handle = { :code => 'DKK' }
invoice.exchange_rate = 100
invoice.is_vat_included = false
invoice.layout_handle = { :id => 16 }

invoice_line = Economic::CurrentInvoiceLine.new
invoice_line.description = 'Line on invoice'
Expand Down
39 changes: 33 additions & 6 deletions lib/economic/current_invoice_proxy.rb
@@ -1,16 +1,43 @@
module Economic
class CurrentInvoiceProxy
attr_reader :session
attr_reader :owner

def initialize(session)
@session = session
def initialize(owner)
@owner = owner
end

def session
owner.session
end

# Returns a new, unpersisted Economic::CurrentInvoice
def build(values = {})
invoice = Economic::CurrentInvoice.new(values)
invoice.session = session
def build(properties = {})
invoice = Economic::CurrentInvoice.new(:session => session)

initialize_properties_with_values_from_owner(invoice) if owner.is_a?(Debtor)
invoice.update_properties(properties)
invoice.partial = false

invoice
end

private

# Initialize properties in invoice with values from owner
def initialize_properties_with_values_from_owner(invoice)
invoice.debtor_handle = owner.handle

invoice.debtor_name ||= owner.name
invoice.debtor_address ||= owner.address
invoice.debtor_postal_code ||= owner.postal_code
invoice.debtor_city ||= owner.city

invoice.term_of_payment_handle ||= owner.term_of_payment_handle
invoice.layout_handle ||= owner.layout_handle
invoice.currency_handle ||= owner.currency_handle

invoice
end

end
end
5 changes: 5 additions & 0 deletions lib/economic/debtor.rb
Expand Up @@ -5,6 +5,11 @@ module Economic
class Debtor < Entity
has_properties :handle, :number, :debtor_group_handle, :name, :vat_zone, :currency_handle, :price_group_handle, :is_accessible, :ean, :public_entry_number, :email, :telephone_and_fax_number, :website, :address, :postal_code, :city, :country, :credit_maximum, :vat_number, :county, :ci_number, :term_of_payment_handle, :layout_handle, :attention_handle, :your_reference_handle, :our_reference_handle, :balance

# Provides access to the current invoices - ie invoices that haven't yet been booked
def current_invoices
@current_invoices ||= CurrentInvoiceProxy.new(self)
end

protected

def build_soap_data
Expand Down
5 changes: 5 additions & 0 deletions lib/economic/session.rb
Expand Up @@ -50,5 +50,10 @@ def request(action, &block)
{}
end
end

# Returns self - used by proxies to access the session of their owner
def session
self
end
end
end
2 changes: 1 addition & 1 deletion spec/economic/current_invoice_line_spec.rb
@@ -1,7 +1,7 @@
require './spec/spec_helper'

describe Economic::CurrentInvoiceLine do
let(:session) { stub_session }
let(:session) { make_session }
subject { (l = Economic::CurrentInvoiceLine.new).tap { l.session = session } }

it "inherits from Economic::Entity" do
Expand Down
30 changes: 29 additions & 1 deletion spec/economic/current_invoice_proxy_spec.rb
@@ -1,7 +1,7 @@
require './spec/spec_helper'

describe Economic::CurrentInvoiceProxy do
let(:session) { stub_session }
let(:session) { make_session }
subject { Economic::CurrentInvoiceProxy.new(session) }

describe "new" do
Expand All @@ -18,5 +18,33 @@
it "assigns the session to the CurrentInvoice" do
subject.build.session.should === session
end

it "should not build a partial CurrentInvoice" do
subject.build.should_not be_partial
end

context "when owner is a Debtor" do
let(:debtor) { make_debtor(:session => session) }
subject { debtor.current_invoices }

it "should use the Debtors session" do
subject.build.session.should == debtor.session
end

it "should initialize with values from Debtor" do
invoice = subject.build

invoice.debtor_name.should == debtor.name
invoice.debtor_address.should == debtor.address
invoice.debtor_postal_code.should == debtor.postal_code
invoice.debtor_city.should == debtor.city

invoice.debtor_handle.should == debtor.handle
invoice.term_of_payment_handle.should == debtor.term_of_payment_handle
invoice.layout_handle.should == debtor.layout_handle
invoice.currency_handle.should == debtor.currency_handle
end
end
end

end
2 changes: 1 addition & 1 deletion spec/economic/current_invoice_spec.rb
@@ -1,7 +1,7 @@
require './spec/spec_helper'

describe Economic::CurrentInvoice do
let(:session) { stub_session }
let(:session) { make_session }
subject { (i = Economic::CurrentInvoice.new).tap { i.session = session } }

it "inherits from Economic::Entity" do
Expand Down
2 changes: 1 addition & 1 deletion spec/economic/debtor_proxy_spec.rb
@@ -1,7 +1,7 @@
require './spec/spec_helper'

describe Economic::DebtorProxy do
let(:session) { stub_session }
let(:session) { make_session }
subject { Economic::DebtorProxy.new(session) }

describe "new" do
Expand Down
20 changes: 18 additions & 2 deletions spec/economic/debtor_spec.rb
@@ -1,8 +1,8 @@
require './spec/spec_helper'

describe Economic::Debtor do
let(:session) { stub_session }
subject { Economic::Debtor.new }
let(:session) { make_session }
subject { Economic::Debtor.new(:session => session) }

it "inherits from Economic::Entity" do
Economic::Debtor.ancestors.should include(Economic::Entity)
Expand All @@ -24,4 +24,20 @@
end
end
end

describe ".current_invoices" do
it "returns an CurrentInvoiceProxy" do
subject.current_invoices.should be_instance_of(Economic::CurrentInvoiceProxy)
end

it "memoizes the proxy" do
subject.current_invoices.should === subject.current_invoices
end

it "should store the session" do
subject.session.should_not be_nil
subject.current_invoices.session.should == subject.session
end
end

end
2 changes: 1 addition & 1 deletion spec/economic/entity_spec.rb
Expand Up @@ -7,7 +7,7 @@ def existing_method; end
end

describe Economic::Entity do
let(:session) { stub_session }
let(:session) { make_session }

describe "class methods" do
subject { SpecEntity }
Expand Down
7 changes: 6 additions & 1 deletion spec/economic/session_spec.rb
Expand Up @@ -32,6 +32,12 @@
end
end

describe ".session" do
it "returns self" do
subject.session.should === subject
end
end

describe "current_invoices" do
it "returns an CurrentInvoiceProxy" do
subject.current_invoices.should be_instance_of(Economic::CurrentInvoiceProxy)
Expand All @@ -42,7 +48,6 @@
end
end


describe "debtors" do
it "returns a DebtorProxy" do
subject.debtors.should be_instance_of(Economic::DebtorProxy)
Expand Down
27 changes: 26 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -31,6 +31,31 @@ def response
end
end

def stub_session
def make_session
Economic::Session.new(123456, 'api', 'passw0rd')
end

def make_debtor(properties = {})
debtor = Economic::Debtor.new

# Assign specified properties
properties.each { |key, value|
debtor.send("#{key}=", value)
}

# Use defaults for the rest of the properties
debtor.session ||= make_session
debtor.handle ||= { :number => 42 }
debtor.number ||= 42
debtor.debtor_group_handle || { :number => 1 }
debtor.name ||= 'Bob'
debtor.vat_zone ||= 'HomeCountry' # HomeCountry, EU, Abroad
debtor.currency_handle ||= { :code => 'DKK' }
debtor.price_group_handle ||= { :number => 1 }
debtor.is_accessible ||= true
debtor.ci_number ||= '12345678'
debtor.term_of_payment_handle ||= { :id => 1 }
debtor.layout_handle ||= { :id => 16 }

debtor
end

0 comments on commit d0541cc

Please sign in to comment.