rconomic 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +40 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +14 -3
  5. data/CHANGELOG.md +28 -0
  6. data/Gemfile +5 -4
  7. data/Guardfile +9 -4
  8. data/README.md +23 -4
  9. data/Rakefile +5 -5
  10. data/gemfiles/Gemfile.ruby-2.0 +14 -0
  11. data/lib/economic/account.rb +3 -3
  12. data/lib/economic/cash_book.rb +4 -7
  13. data/lib/economic/cash_book_entry.rb +4 -24
  14. data/lib/economic/company.rb +61 -0
  15. data/lib/economic/creditor.rb +4 -5
  16. data/lib/economic/creditor_contact.rb +5 -6
  17. data/lib/economic/creditor_entry.rb +1 -3
  18. data/lib/economic/current_invoice.rb +14 -21
  19. data/lib/economic/current_invoice_line.rb +4 -13
  20. data/lib/economic/debtor.rb +10 -11
  21. data/lib/economic/debtor_contact.rb +7 -8
  22. data/lib/economic/debtor_entry.rb +1 -3
  23. data/lib/economic/endpoint.rb +36 -6
  24. data/lib/economic/entity/handle.rb +54 -29
  25. data/lib/economic/entity/mapper.rb +1 -1
  26. data/lib/economic/entity.rb +26 -29
  27. data/lib/economic/entry.rb +1 -2
  28. data/lib/economic/invoice.rb +15 -8
  29. data/lib/economic/order.rb +2 -4
  30. data/lib/economic/product.rb +73 -0
  31. data/lib/economic/proxies/account_proxy.rb +2 -4
  32. data/lib/economic/proxies/actions/debtor_contact/all.rb +63 -0
  33. data/lib/economic/proxies/actions/find_by_ci_number.rb +1 -3
  34. data/lib/economic/proxies/actions/find_by_date_interval.rb +3 -6
  35. data/lib/economic/proxies/actions/find_by_handle_with_number.rb +1 -1
  36. data/lib/economic/proxies/actions/find_by_name.rb +1 -1
  37. data/lib/economic/proxies/actions/find_by_number.rb +2 -4
  38. data/lib/economic/proxies/actions/find_by_telephone_and_fax_number.rb +1 -3
  39. data/lib/economic/proxies/cash_book_entry_proxy.rb +19 -22
  40. data/lib/economic/proxies/cash_book_proxy.rb +5 -10
  41. data/lib/economic/proxies/company_proxy.rb +9 -0
  42. data/lib/economic/proxies/creditor_contact_proxy.rb +2 -2
  43. data/lib/economic/proxies/creditor_entry_proxy.rb +8 -14
  44. data/lib/economic/proxies/creditor_proxy.rb +8 -11
  45. data/lib/economic/proxies/current_invoice_line_proxy.rb +2 -2
  46. data/lib/economic/proxies/current_invoice_proxy.rb +3 -3
  47. data/lib/economic/proxies/debtor_contact_proxy.rb +6 -2
  48. data/lib/economic/proxies/debtor_entry_proxy.rb +6 -10
  49. data/lib/economic/proxies/debtor_proxy.rb +40 -55
  50. data/lib/economic/proxies/entity_proxy.rb +15 -15
  51. data/lib/economic/proxies/entry_proxy.rb +10 -16
  52. data/lib/economic/proxies/invoice_proxy.rb +3 -3
  53. data/lib/economic/proxies/order_proxy.rb +3 -3
  54. data/lib/economic/proxies/product_proxy.rb +11 -0
  55. data/lib/economic/session.rb +48 -21
  56. data/lib/economic/support/string.rb +5 -5
  57. data/lib/rconomic/version.rb +1 -1
  58. data/lib/rconomic.rb +39 -34
  59. data/lib/savon_ext/request.rb +17 -0
  60. data/rconomic.gemspec +8 -7
  61. data/spec/economic/account_spec.rb +3 -3
  62. data/spec/economic/cash_book_entry_spec.rb +26 -3
  63. data/spec/economic/cash_book_spec.rb +7 -7
  64. data/spec/economic/company_spec.rb +20 -0
  65. data/spec/economic/creditor_contact_spec.rb +4 -5
  66. data/spec/economic/creditor_entry_spec.rb +20 -0
  67. data/spec/economic/creditor_spec.rb +3 -3
  68. data/spec/economic/current_invoice_line_spec.rb +3 -3
  69. data/spec/economic/current_invoice_spec.rb +19 -19
  70. data/spec/economic/debtor_contact_spec.rb +4 -5
  71. data/spec/economic/debtor_entry_spec.rb +1 -1
  72. data/spec/economic/debtor_spec.rb +13 -13
  73. data/spec/economic/endpoint_spec.rb +27 -11
  74. data/spec/economic/entity/handle_spec.rb +10 -23
  75. data/spec/economic/entity/mapper_spec.rb +26 -30
  76. data/spec/economic/entity_spec.rb +36 -31
  77. data/spec/economic/entry_spec.rb +1 -1
  78. data/spec/economic/invoice_spec.rb +49 -7
  79. data/spec/economic/order_spec.rb +4 -4
  80. data/spec/economic/product_spec.rb +72 -0
  81. data/spec/economic/proxies/account_proxy_spec.rb +74 -0
  82. data/spec/economic/proxies/actions/debtor_contact/all_spec.rb +26 -0
  83. data/spec/economic/proxies/actions/find_by_name_spec.rb +6 -7
  84. data/spec/economic/proxies/cash_book_entry_proxy_spec.rb +27 -29
  85. data/spec/economic/proxies/cash_book_proxy_spec.rb +14 -22
  86. data/spec/economic/proxies/company_proxy_spec.rb +47 -0
  87. data/spec/economic/proxies/creditor_contact_proxy_spec.rb +11 -5
  88. data/spec/economic/proxies/creditor_entry_proxy_spec.rb +12 -12
  89. data/spec/economic/proxies/creditor_proxy_spec.rb +15 -15
  90. data/spec/economic/proxies/current_invoice_line_proxy_spec.rb +10 -4
  91. data/spec/economic/proxies/current_invoice_proxy_spec.rb +37 -19
  92. data/spec/economic/proxies/debtor_contact_proxy_spec.rb +8 -4
  93. data/spec/economic/proxies/debtor_entry_proxy_spec.rb +21 -11
  94. data/spec/economic/proxies/debtor_proxy_spec.rb +81 -40
  95. data/spec/economic/proxies/entry_proxy_spec.rb +17 -14
  96. data/spec/economic/proxies/invoice_proxy_spec.rb +24 -10
  97. data/spec/economic/proxies/order_proxy_spec.rb +20 -10
  98. data/spec/economic/proxies/product_proxy_spec.rb +86 -0
  99. data/spec/economic/session_spec.rb +74 -35
  100. data/spec/fixtures/account_get_all/multiple.xml +15 -0
  101. data/spec/fixtures/account_get_all/none.xml +8 -0
  102. data/spec/fixtures/account_get_all/single.xml +12 -0
  103. data/spec/fixtures/account_get_data_array/multiple.xml +69 -0
  104. data/spec/fixtures/company_get/success.xml +10 -0
  105. data/spec/fixtures/company_get_data/success.xml +32 -0
  106. data/spec/fixtures/current_invoice_line_create_from_data/success.xml +6 -37
  107. data/spec/fixtures/debtor_create_from_data/success.xml +5 -52
  108. data/spec/fixtures/debtor_get_orders/none.xml +9 -0
  109. data/spec/fixtures/product_create_from_data/success.xml +37 -0
  110. data/spec/fixtures/product_find_by_number/found.xml +10 -0
  111. data/spec/fixtures/product_find_by_number/not_found.xml +6 -0
  112. data/spec/fixtures/product_get_all/multiple.xml +15 -0
  113. data/spec/fixtures/product_get_all/single.xml +12 -0
  114. data/spec/fixtures/product_get_data/success.xml +37 -0
  115. data/spec/fixtures/product_get_data_array/multiple.xml +69 -0
  116. data/spec/spec_helper.rb +6 -9
  117. data/spec/support/factories.rb +14 -14
  118. metadata +54 -9
@@ -1,7 +1,6 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
-
5
4
  # Represents a debtor in E-conomic.
6
5
  #
7
6
  # API documentation: http://www.e-conomic.com/apidocs/Documentation/T_Economic_Api_IDebtor.html
@@ -53,7 +52,7 @@ module Economic
53
52
  :balance
54
53
 
55
54
  def handle
56
- @handle || Handle.new({:number => @number})
55
+ @handle || Handle.new(:number => @number)
57
56
  end
58
57
 
59
58
  # Provides access to the current invoices for Debtor - ie invoices that
@@ -64,27 +63,27 @@ module Economic
64
63
 
65
64
  # Returns the Debtors contacts
66
65
  def contacts
67
- return [] if self.handle.empty?
68
- @contacts ||= DebtorProxy.new(self).get_debtor_contacts(self.handle)
66
+ return [] if handle.empty?
67
+ @contacts ||= DebtorProxy.new(self).get_debtor_contacts(handle)
69
68
  end
70
69
 
71
70
  def invoices
72
- return [] if self.handle.empty?
73
- @invoices ||= DebtorProxy.new(self).get_invoices(self.handle)
71
+ return [] if handle.empty?
72
+ @invoices ||= DebtorProxy.new(self).get_invoices(handle)
74
73
  end
75
74
 
76
75
  def orders
77
- return [] if self.handle.empty?
78
- @orders ||= DebtorProxy.new(self).get_orders(self.handle)
76
+ return [] if handle.empty?
77
+ @orders ||= DebtorProxy.new(self).get_orders(handle)
79
78
  end
80
79
 
81
80
  protected
82
81
 
83
82
  def fields
84
- to_hash = Proc.new { |handle| handle.to_hash }
83
+ to_hash = proc { |handle| handle.to_hash }
85
84
  [
86
85
  ["Handle", :handle, to_hash, :required],
87
- ["Number", :handle, Proc.new { |h| h.number }, :required],
86
+ ["Number", :handle, proc { |h| h.number }, :required],
88
87
  ["DebtorGroupHandle", :debtor_group_handle, to_hash],
89
88
  ["Name", :name, nil, :required],
90
89
  ["VatZone", :vat_zone, nil, :required],
@@ -1,7 +1,6 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
-
5
4
  # Represents a debtor contact.
6
5
  #
7
6
  # API documentation: http://www.e-conomic.com/apidocs/Documentation/T_Economic_Api_IDebtorContact.html
@@ -45,7 +44,7 @@ module Economic
45
44
  end
46
45
 
47
46
  def handle
48
- @handle || Handle.new({:id => @id})
47
+ @handle || Handle.new(:id => @id)
49
48
  end
50
49
 
51
50
  protected
@@ -53,17 +52,17 @@ module Economic
53
52
  # Returns the field rules to use when mapping to SOAP data
54
53
  def fields
55
54
  [
56
- ["Handle", :handle, Proc.new { |v| v.to_hash }, :required],
57
- ["Id", :handle, Proc.new { |v| v.id }, :required],
58
- ["DebtorHandle", :debtor, Proc.new { |v| v.handle.to_hash }],
55
+ ["Handle", :handle, proc { |v| v.to_hash }, :required],
56
+ ["Id", :handle, proc { |v| v.id }, :required],
57
+ ["DebtorHandle", :debtor, proc { |v| v.handle.to_hash }],
59
58
  ["Name", :name, nil, :required],
60
59
  ["Number", :number],
61
60
  ["TelephoneNumber", :telephone_number],
62
61
  ["Email", :email],
63
62
  ["Comments", :comments],
64
63
  ["ExternalId", :external_id],
65
- ["IsToReceiveEmailCopyOfOrder", :is_to_receive_email_copy_of_order, Proc.new { |v| v || false }, :required],
66
- ["IsToReceiveEmailCopyOfInvoice", :is_to_receive_email_copy_of_invoice, Proc.new { |v| v || false }, :required]
64
+ ["IsToReceiveEmailCopyOfOrder", :is_to_receive_email_copy_of_order, proc { |v| v || false }, :required],
65
+ ["IsToReceiveEmailCopyOfInvoice", :is_to_receive_email_copy_of_invoice, proc { |v| v || false }, :required]
67
66
  ]
68
67
  end
69
68
  end
@@ -1,7 +1,6 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
-
5
4
  # Represents a debtor entry in E-conomic.
6
5
  #
7
6
  # API documentation: http://www.e-conomic.com/apidocs/Documentation/T_Economic_Api_IDebtorEntry.html
@@ -22,5 +21,4 @@ module Economic
22
21
  :remainder,
23
22
  :remainder_default_currency
24
23
  end
25
-
26
24
  end
@@ -8,6 +8,18 @@ class Economic::Endpoint
8
8
  def_delegator "client.globals", :log_level, :log_level=
9
9
  def_delegator "client.globals", :log, :log=
10
10
 
11
+ # Create a new Endpoint
12
+ #
13
+ # Economic::Session uses this internally
14
+ #
15
+ # ==== Attributes
16
+ #
17
+ # * +app_identifier+ - A string identifiying your application, as described in http://techtalk.e-conomic.com/e-conomic-soap-api-now-requires-you-to-specify-a-custom-x-economicappidentifier-header/
18
+ #
19
+ def initialize(app_identifier = nil)
20
+ @app_identifier = app_identifier
21
+ end
22
+
11
23
  # Invokes soap_action on the API endpoint with the given data.
12
24
  #
13
25
  # Returns a Hash with the resulting response from the endpoint as a Hash.
@@ -30,12 +42,16 @@ class Economic::Endpoint
30
42
  #
31
43
  # Cached on class-level to avoid loading the big WSDL file more than once (can
32
44
  # take several hundred megabytes of RAM after a while...)
33
- def client
34
- @@client ||= Savon.client do
35
- wsdl File.expand_path(File.join(File.dirname(__FILE__), "economic.wsdl"))
36
- log false
37
- log_level :info
45
+ #
46
+ # If you need to refresh the cached client and return a newly built instance,
47
+ # set force_new_instance to true
48
+ def client(force_new_instance: false)
49
+ reset_client if force_new_instance
50
+ options = client_options
51
+ if @app_identifier
52
+ options[:headers] = {"X-EconomicAppIdentifier" => @app_identifier}
38
53
  end
54
+ @@client ||= Savon.client(options)
39
55
  end
40
56
 
41
57
  # Returns the E-conomic API action name to call
@@ -50,7 +66,17 @@ class Economic::Endpoint
50
66
 
51
67
  def class_name_without_modules(entity_class)
52
68
  class_name = entity_class.to_s
53
- class_name.split('::').last
69
+ class_name.split("::").last
70
+ end
71
+
72
+ def client_options
73
+ {
74
+ :wsdl => File.expand_path(
75
+ File.join(File.dirname(__FILE__), "economic.wsdl")
76
+ ),
77
+ :log => false,
78
+ :log_level => :info
79
+ }
54
80
  end
55
81
 
56
82
  def extract_result_from_response(response, soap_action)
@@ -76,4 +102,8 @@ class Economic::Endpoint
76
102
  locals
77
103
  )
78
104
  end
105
+
106
+ def reset_client
107
+ @@client = nil
108
+ end
79
109
  end
@@ -8,14 +8,14 @@ class Economic::Entity
8
8
 
9
9
  def self.id_properties
10
10
  {
11
- :code => 'Code',
12
- :id => 'Id',
13
- :id1 => 'Id1',
14
- :id2 => 'Id2',
15
- :name => 'Name',
16
- :number => 'Number',
17
- :serial_number => 'SerialNumber',
18
- :vat_code => 'VatCode'
11
+ :code => "Code",
12
+ :id => "Id",
13
+ :id1 => "Id1",
14
+ :id2 => "Id2",
15
+ :name => "Name",
16
+ :number => "Number",
17
+ :serial_number => "SerialNumber",
18
+ :vat_code => "VatCode"
19
19
  }
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ class Economic::Entity
23
23
  id_properties.keys
24
24
  end
25
25
 
26
- attr_accessor *supported_keys
26
+ attr_accessor(*supported_keys)
27
27
 
28
28
  # Returns true if Handle hasn't been initialized with any values yet. This
29
29
  # usually happens when the handle is constructed for an entity whose id
@@ -36,10 +36,10 @@ class Economic::Entity
36
36
  verify_sanity_of_arguments!(hash)
37
37
  hash = prepare_hash_argument(hash) unless hash.is_a?(self.class)
38
38
 
39
- [:code, :name, :vat_code].each do |key|
39
+ [:code, :name, :vat_code, :number].each do |key|
40
40
  instance_variable_set("@#{key}", hash[key]) if hash[key]
41
41
  end
42
- [:id, :id1, :id2, :number, :serial_number].each do |key|
42
+ [:id, :id1, :id2, :serial_number].each do |key|
43
43
  instance_variable_set("@#{key}", hash[key].to_i) if hash[key]
44
44
  end
45
45
  end
@@ -48,7 +48,7 @@ class Economic::Entity
48
48
  only_keys = [only_keys].flatten
49
49
  only_keys.each_with_object({}) do |key, hash|
50
50
  property = id_properties[key]
51
- value = self.send(key)
51
+ value = send(key)
52
52
  next if value.blank?
53
53
  hash[property] = value
54
54
  end
@@ -59,44 +59,69 @@ class Economic::Entity
59
59
  end
60
60
 
61
61
  def ==(other)
62
- return true if self.object_id == other.object_id
62
+ return true if object_id == other.object_id
63
63
  return false if other.nil?
64
64
  return false if empty? || (other.respond_to?(:empty?) && other.empty?)
65
65
  return false unless other.respond_to?(:id) && other.respond_to?(:number)
66
- self.id == other.id && self.number == other.number && self.id1 == other.id1 && self.id2 == other.id2
66
+ id == other.id &&
67
+ number == other.number &&
68
+ id1 == other.id1 &&
69
+ id2 == other.id2 &&
70
+ name == other.name
67
71
  end
68
72
 
69
73
  private
70
74
 
75
+ def handleish?(object)
76
+ return false if object.nil?
77
+ return true if object.is_a?(self.class)
78
+ return true if object.is_a?(Hash)
79
+ false
80
+ end
81
+
71
82
  def id_properties
72
83
  self.class.id_properties
73
84
  end
74
85
 
86
+ def verify_all_keys_are_known(hash)
87
+ if hash.respond_to?(:keys)
88
+ unknown_keys = hash.keys - id_properties.keys - id_properties.values
89
+ raise(
90
+ ArgumentError,
91
+ "Unknown keys in handle: #{unknown_keys.inspect}"
92
+ ) unless unknown_keys.empty?
93
+ end
94
+ end
95
+
75
96
  # Raises exceptions if hash doesn't contain values we can use to construct a
76
97
  # new handle
77
98
  def verify_sanity_of_arguments!(hash)
78
- return if hash.is_a?(self.class)
99
+ verify_usability_for_handle(hash)
100
+ verify_all_keys_are_known(hash)
101
+ end
79
102
 
80
- if hash.nil? || (!hash.respond_to?(:to_i) && (!hash.respond_to?(:keys) && !hash.respond_to?(:values)))
81
- raise ArgumentError.new("Expected Number, Hash or Economic::Entity::Handle - got #{hash.inspect}")
82
- end
103
+ def verify_usability_for_handle(hash)
104
+ return if handleish?(hash)
83
105
 
84
- if hash.respond_to?(:keys)
85
- unknown_keys = hash.keys - id_properties.keys - id_properties.values
86
- raise ArgumentError.new("Unknown keys in handle: #{unknown_keys.inspect}") unless unknown_keys.empty?
87
- end
106
+ raise(
107
+ ArgumentError,
108
+ "Expected Hash or Economic::Entity::Handle - got #{hash.inspect}"
109
+ )
88
110
  end
89
111
 
90
112
  # Examples
91
113
  #
92
- # prepare_hash_argument(12) #=> {:id => 12}
93
- # prepare_hash_argument(:id => 12) #=> {:id => 12}
94
- # prepare_hash_argument('Id' => 12) #=> {:id => 12}
95
- # prepare_hash_argument('Id' => 12, 'Number' => 13) #=> {:id => 12, :number => 13}
114
+ # prepare_hash_argument(:id => 12)
115
+ # #=> {:id => 12}
116
+ #
117
+ # prepare_hash_argument('Id' => 12)
118
+ # #=> {:id => 12}
119
+ #
120
+ # prepare_hash_argument('Id' => 12, 'Number' => 13)
121
+ # #=> {:id => 12, :number => 13}
96
122
  def prepare_hash_argument(hash)
97
- hash = {:id => hash.to_i} if hash.respond_to?(:to_i) unless hash.blank?
98
- hash[:id] ||= hash['Id']
99
- hash[:number] ||= hash['Number']
123
+ hash[:id] ||= hash["Id"]
124
+ hash[:number] ||= hash["Number"]
100
125
  hash
101
126
  end
102
127
  end
@@ -26,7 +26,7 @@ module Economic
26
26
  end
27
27
  end
28
28
 
29
- return data
29
+ data
30
30
  end
31
31
 
32
32
  private
@@ -1,6 +1,6 @@
1
- require 'economic/endpoint'
2
- require 'economic/entity/handle'
3
- require 'economic/entity/mapper'
1
+ require "economic/endpoint"
2
+ require "economic/entity/handle"
3
+ require "economic/entity/mapper"
4
4
 
5
5
  module Economic
6
6
  class Entity
@@ -33,7 +33,7 @@ module Economic
33
33
  # Create a property getter that loads the full Entity from the API if
34
34
  # necessary
35
35
  def property_reader(property)
36
- define_method "#{property}" do
36
+ define_method property.to_s do
37
37
  value = instance_variable_get("@#{property}")
38
38
  if value.nil? && partial? && persisted?
39
39
  instance_variable_get("@#{property}")
@@ -72,7 +72,7 @@ module Economic
72
72
 
73
73
  # Returns the class used to instantiate a proxy for Entity
74
74
  def proxy
75
- class_name = name.split('::').last
75
+ class_name = name.split("::").last
76
76
  proxy_class_name = "#{class_name}Proxy"
77
77
  Economic.const_get(proxy_class_name)
78
78
  end
@@ -83,7 +83,7 @@ module Economic
83
83
  # Entity.key #=> :entity
84
84
  # CurrentInvoice.key #=> :current_invoice
85
85
  def key
86
- key = self.name
86
+ key = name
87
87
  key = Economic::Support::String.demodulize(key)
88
88
  key = Economic::Support::String.underscore(key)
89
89
  key.intern
@@ -91,7 +91,7 @@ module Economic
91
91
  end
92
92
 
93
93
  def handle
94
- @handle || Handle.build({:number => @number, :id => @id})
94
+ @handle || Handle.build(:number => @number, :id => @id)
95
95
  end
96
96
 
97
97
  def handle=(handle)
@@ -108,22 +108,23 @@ module Economic
108
108
  # Updates Entity with its data from the API
109
109
  def get_data
110
110
  response = proxy.get_data(handle)
111
- self.update_properties(response)
111
+ update_properties(response)
112
112
  self.partial = false
113
113
  self.persisted = true
114
114
  end
115
115
 
116
+ # Get default Entity with its number from the API
117
+ def get
118
+ proxy.get
119
+ end
120
+
116
121
  # Returns the number of Entity. This does not trigger a load from the API
117
122
  # even if Entity is partial
118
- def number
119
- @number
120
- end
123
+ attr_reader :number
121
124
 
122
125
  # Returns the id of Entity. This does not trigger a load from the API even
123
126
  # if Entity is partial
124
- def id
125
- @id
126
- end
127
+ attr_reader :id
127
128
 
128
129
  # Returns true if CurrentInvoiceLine has been persisted in e-conomic
129
130
  def persisted?
@@ -144,8 +145,8 @@ module Economic
144
145
  end
145
146
 
146
147
  def inspect
147
- props = self.class.properties.collect { |p| "#{p}=#{self.send(p).inspect}" }
148
- "#<#{self.class}:#{self.object_id} partial=#{partial?}, persisted=#{persisted?}, #{props.join(', ')}>"
148
+ props = self.class.properties.collect { |p| "#{p}=#{send(p).inspect}" }
149
+ "#<#{self.class}:#{object_id} partial=#{partial?}, persisted=#{persisted?}, #{props.join(', ')}>"
149
150
  end
150
151
 
151
152
  # Persist the Entity to the API
@@ -156,7 +157,7 @@ module Economic
156
157
  # Deletes entity permanently from E-conomic.
157
158
  def destroy
158
159
  handleKey = "#{Support::String.camel_back(class_name)}Handle"
159
- response = request(:delete, {handleKey => handle.to_hash})
160
+ response = request(:delete, handleKey => handle.to_hash)
160
161
 
161
162
  @persisted = false
162
163
  @partial = true
@@ -168,15 +169,15 @@ module Economic
168
169
  def update_properties(hash)
169
170
  hash.each do |key, value|
170
171
  setter_method = "#{key}="
171
- if self.respond_to?(setter_method)
172
- self.send(setter_method, value)
172
+ if respond_to?(setter_method)
173
+ send(setter_method, value)
173
174
  end
174
175
  end
175
176
  end
176
177
 
177
178
  def ==(other)
178
179
  return false if other.nil?
179
- self.handle == other.handle && other.is_a?(self.class)
180
+ handle == other.handle && other.is_a?(self.class)
180
181
  end
181
182
 
182
183
  protected
@@ -190,9 +191,7 @@ module Economic
190
191
  end
191
192
 
192
193
  def create
193
- response = request(:create_from_data, {
194
- 'data' => build_soap_data
195
- })
194
+ response = request(:create_from_data, "data" => build_soap_data)
196
195
 
197
196
  if response
198
197
  @number = response[:number]
@@ -204,7 +203,7 @@ module Economic
204
203
  @persisted = true
205
204
  @partial = false
206
205
 
207
- return response
206
+ response
208
207
  end
209
208
 
210
209
  def defaults
@@ -212,14 +211,12 @@ module Economic
212
211
  end
213
212
 
214
213
  def update
215
- response = request(:update_from_data, {
216
- 'data' => build_soap_data
217
- })
214
+ response = request(:update_from_data, "data" => build_soap_data)
218
215
 
219
216
  @persisted = true
220
217
  @partial = false
221
218
 
222
- return response
219
+ response
223
220
  end
224
221
 
225
222
  # Returns Hash with the data structure to send to the API
@@ -245,7 +242,7 @@ module Economic
245
242
 
246
243
  def initialize_defaults
247
244
  defaults.each do |property_name, default_value|
248
- self.send("#{property_name}=", default_value)
245
+ send("#{property_name}=", default_value)
249
246
  end
250
247
  end
251
248
  end
@@ -1,7 +1,6 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
-
5
4
  # Represents an entry in E-conomic.
6
5
  #
7
6
  # API documentation: http://www.e-conomic.com/apidocs/Documentation/T_Economic_Api_IEntry.html
@@ -1,4 +1,4 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
4
  class Invoice < Entity
@@ -6,10 +6,10 @@ module Economic
6
6
  :net_amount,
7
7
  :vat_amount,
8
8
  :gross_amount,
9
+ :date,
9
10
  :due_date,
10
11
  :debtor_handle,
11
12
  :debtor_name,
12
- :debtor_name,
13
13
  :debtor_address,
14
14
  :debtor_postal_code,
15
15
  :debtor_city,
@@ -49,9 +49,18 @@ module Economic
49
49
  end
50
50
 
51
51
  def remainder
52
- request(:get_remainder, {
53
- "invoiceHandle" => handle.to_hash
54
- })
52
+ @remainder ||= request(:get_remainder, "invoiceHandle" => handle.to_hash).to_f
53
+ end
54
+
55
+ def days_past_due
56
+ days = Date.today - due_date.to_date
57
+ days > 0 ? days : 0
58
+ end
59
+
60
+ # Returns true if the due date has expired, and there is a remainder
61
+ # left on the invoice
62
+ def past_due?
63
+ days_past_due > 0 && remainder > 0
55
64
  end
56
65
 
57
66
  # Returns the PDF version of Invoice as a String.
@@ -62,9 +71,7 @@ module Economic
62
71
  # file << invoice.pdf
63
72
  # end
64
73
  def pdf
65
- response = request(:get_pdf, {
66
- "invoiceHandle" => handle.to_hash
67
- })
74
+ response = request(:get_pdf, "invoiceHandle" => handle.to_hash)
68
75
 
69
76
  Base64.decode64(response)
70
77
  end
@@ -1,4 +1,4 @@
1
- require 'economic/entity'
1
+ require "economic/entity"
2
2
 
3
3
  module Economic
4
4
  class Order < Entity
@@ -56,9 +56,7 @@ module Economic
56
56
  # file << invoice.pdf
57
57
  # end
58
58
  def pdf
59
- response = request(:get_pdf, {
60
- "orderHandle" => handle.to_hash
61
- })
59
+ response = request(:get_pdf, "orderHandle" => handle.to_hash)
62
60
 
63
61
  Base64.decode64(response)
64
62
  end
@@ -0,0 +1,73 @@
1
+ require "economic/entity"
2
+
3
+ module Economic
4
+ # Represents a product in E-conomic.
5
+ #
6
+ # API documentation: http://www.e-conomic.com/apidocs/Documentation/T_Economic_Api_IProduct.html
7
+ #
8
+ # Examples
9
+ #
10
+ # # Find a product:
11
+ # product = economic.products.find(1234)
12
+ #
13
+ # # Creating a product:
14
+ # product = economic.products.build
15
+ # product.number = 'ESC2014-LED-DISPLAY'
16
+ # product.product_group_handle = { :number => 1 }
17
+ # product.name = '100 meter LED display'
18
+ # product.sales_price = 999999
19
+ # product.cost_price = 100000
20
+ # product.recommended_price = 999999
21
+ # product.is_accessible = true
22
+ # product.volume = 1
23
+ # product.save
24
+ class Product < Entity
25
+ has_properties :number,
26
+ :product_group_handle,
27
+ :name,
28
+ :description,
29
+ :bar_code,
30
+ :sales_price,
31
+ :cost_price,
32
+ :recommended_price,
33
+ :unit_handle,
34
+ :is_accessible,
35
+ :volume,
36
+ :department_handle,
37
+ :distribution_key_handle,
38
+ :in_stock,
39
+ :on_order,
40
+ :ordered,
41
+ :available
42
+
43
+ def handle
44
+ @handle ||= Handle.new(:number => @number)
45
+ end
46
+
47
+ protected
48
+
49
+ def fields
50
+ to_hash = proc { |handle| handle.to_hash }
51
+ [
52
+ ["Handle", :handle, to_hash, :required],
53
+ ["Number", :handle, proc { |h| h.number }, :required],
54
+ ["ProductGroupHandle", :product_group_handle, to_hash],
55
+ ["Name", :name, nil, :required],
56
+ ["Description", :description, nil],
57
+ ["BarCode", :bar_code, nil],
58
+ ["SalesPrice", :sales_price, nil, :required],
59
+ ["CostPrice", :cost_price, nil, :required],
60
+ ["RecommendedPrice", :recommended_price, nil, :required],
61
+ ["UnitHandle", :unit_handle, to_hash],
62
+ ["IsAccessible", :is_accessible, nil, :required],
63
+ ["Volume", :volume, nil, :required],
64
+ ["DepartmentHandle", :department_handle, to_hash],
65
+ ["DistributionKeyHandle", :distribution_key_handle, to_hash],
66
+ ["InStock", :in_stock],
67
+ ["OnOrder", :on_order],
68
+ ["Ordered", :ordered],
69
+ ["Available", :available]
70
+ ]
71
+ end
72
+ end
73
+ end
@@ -1,11 +1,9 @@
1
- require 'economic/proxies/entity_proxy'
1
+ require "economic/proxies/entity_proxy"
2
2
 
3
3
  module Economic
4
4
  class AccountProxy < EntityProxy
5
5
  def find_by_name(name)
6
- response = request('FindByName', {
7
- 'name' => name
8
- })
6
+ response = request("FindByName", "name" => name)
9
7
 
10
8
  handle = response[:account_handle]
11
9