cashctrl 0.0.1 → 0.2.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +21 -0
  3. data/README.md +831 -14
  4. data/cashctrl.gemspec +1 -1
  5. data/lib/cashctrl/client.rb +219 -0
  6. data/lib/cashctrl/collection.rb +78 -0
  7. data/lib/cashctrl/configuration.rb +30 -0
  8. data/lib/cashctrl/error.rb +29 -0
  9. data/lib/cashctrl/errors.rb +76 -0
  10. data/lib/cashctrl/localized_string.rb +115 -0
  11. data/lib/cashctrl/models/account.rb +39 -0
  12. data/lib/cashctrl/models/article.rb +48 -0
  13. data/lib/cashctrl/models/asset.rb +37 -0
  14. data/lib/cashctrl/models/attachment.rb +14 -0
  15. data/lib/cashctrl/models/bank.rb +18 -0
  16. data/lib/cashctrl/models/category.rb +13 -0
  17. data/lib/cashctrl/models/certificate_document.rb +14 -0
  18. data/lib/cashctrl/models/certificate_template.rb +13 -0
  19. data/lib/cashctrl/models/cost_center.rb +17 -0
  20. data/lib/cashctrl/models/currency.rb +18 -0
  21. data/lib/cashctrl/models/custom_field.rb +18 -0
  22. data/lib/cashctrl/models/custom_field_group.rb +12 -0
  23. data/lib/cashctrl/models/file.rb +20 -0
  24. data/lib/cashctrl/models/fiscal_period.rb +16 -0
  25. data/lib/cashctrl/models/fiscal_period_task.rb +14 -0
  26. data/lib/cashctrl/models/history.rb +19 -0
  27. data/lib/cashctrl/models/insurance_type.rb +13 -0
  28. data/lib/cashctrl/models/journal.rb +65 -0
  29. data/lib/cashctrl/models/journal_import.rb +18 -0
  30. data/lib/cashctrl/models/journal_import_entry.rb +20 -0
  31. data/lib/cashctrl/models/journal_item.rb +20 -0
  32. data/lib/cashctrl/models/loader.rb +55 -0
  33. data/lib/cashctrl/models/location.rb +22 -0
  34. data/lib/cashctrl/models/order.rb +53 -0
  35. data/lib/cashctrl/models/order_book_entry.rb +17 -0
  36. data/lib/cashctrl/models/order_document.rb +15 -0
  37. data/lib/cashctrl/models/order_item.rb +23 -0
  38. data/lib/cashctrl/models/order_layout.rb +18 -0
  39. data/lib/cashctrl/models/order_payment.rb +16 -0
  40. data/lib/cashctrl/models/person.rb +78 -0
  41. data/lib/cashctrl/models/report.rb +15 -0
  42. data/lib/cashctrl/models/report_collection.rb +13 -0
  43. data/lib/cashctrl/models/report_element.rb +16 -0
  44. data/lib/cashctrl/models/rounding.rb +16 -0
  45. data/lib/cashctrl/models/salary_book_entry.rb +16 -0
  46. data/lib/cashctrl/models/salary_certificate.rb +16 -0
  47. data/lib/cashctrl/models/salary_document.rb +15 -0
  48. data/lib/cashctrl/models/salary_field.rb +16 -0
  49. data/lib/cashctrl/models/salary_layout.rb +13 -0
  50. data/lib/cashctrl/models/salary_payment.rb +16 -0
  51. data/lib/cashctrl/models/salary_setting.rb +12 -0
  52. data/lib/cashctrl/models/salary_statement.rb +19 -0
  53. data/lib/cashctrl/models/salary_status.rb +12 -0
  54. data/lib/cashctrl/models/salary_sum.rb +14 -0
  55. data/lib/cashctrl/models/salary_template.rb +12 -0
  56. data/lib/cashctrl/models/salary_type.rb +14 -0
  57. data/lib/cashctrl/models/sequence_number.rb +13 -0
  58. data/lib/cashctrl/models/setting.rb +33 -0
  59. data/lib/cashctrl/models/tax.rb +18 -0
  60. data/lib/cashctrl/models/text.rb +13 -0
  61. data/lib/cashctrl/models/title.rb +13 -0
  62. data/lib/cashctrl/models/unit.rb +11 -0
  63. data/lib/cashctrl/relation.rb +271 -0
  64. data/lib/cashctrl/resource.rb +433 -0
  65. data/lib/cashctrl/resources/account/bank.rb +14 -0
  66. data/lib/cashctrl/resources/account/category.rb +16 -0
  67. data/lib/cashctrl/resources/account/cost_center/category.rb +18 -0
  68. data/lib/cashctrl/resources/account/cost_center.rb +26 -0
  69. data/lib/cashctrl/resources/account.rb +28 -0
  70. data/lib/cashctrl/resources/attachments.rb +32 -0
  71. data/lib/cashctrl/resources/base.rb +237 -0
  72. data/lib/cashctrl/resources/currency.rb +17 -0
  73. data/lib/cashctrl/resources/custom_field/group.rb +16 -0
  74. data/lib/cashctrl/resources/custom_field.rb +18 -0
  75. data/lib/cashctrl/resources/file/category.rb +16 -0
  76. data/lib/cashctrl/resources/file.rb +82 -0
  77. data/lib/cashctrl/resources/fiscal_period/task.rb +20 -0
  78. data/lib/cashctrl/resources/fiscal_period.rb +54 -0
  79. data/lib/cashctrl/resources/history.rb +26 -0
  80. data/lib/cashctrl/resources/import_session.rb +43 -0
  81. data/lib/cashctrl/resources/inventory/article/category.rb +18 -0
  82. data/lib/cashctrl/resources/inventory/article/import.rb +13 -0
  83. data/lib/cashctrl/resources/inventory/article.rb +19 -0
  84. data/lib/cashctrl/resources/inventory/asset/category.rb +18 -0
  85. data/lib/cashctrl/resources/inventory/asset.rb +19 -0
  86. data/lib/cashctrl/resources/inventory/unit.rb +12 -0
  87. data/lib/cashctrl/resources/journal/import/entry.rb +33 -0
  88. data/lib/cashctrl/resources/journal/import.rb +16 -0
  89. data/lib/cashctrl/resources/journal.rb +56 -0
  90. data/lib/cashctrl/resources/location.rb +10 -0
  91. data/lib/cashctrl/resources/order/book_entry.rb +12 -0
  92. data/lib/cashctrl/resources/order/category.rb +21 -0
  93. data/lib/cashctrl/resources/order/document.rb +30 -0
  94. data/lib/cashctrl/resources/order/layout.rb +12 -0
  95. data/lib/cashctrl/resources/order/payment.rb +36 -0
  96. data/lib/cashctrl/resources/order.rb +47 -0
  97. data/lib/cashctrl/resources/organization.rb +13 -0
  98. data/lib/cashctrl/resources/person/category.rb +16 -0
  99. data/lib/cashctrl/resources/person/import.rb +11 -0
  100. data/lib/cashctrl/resources/person/title.rb +12 -0
  101. data/lib/cashctrl/resources/person.rb +21 -0
  102. data/lib/cashctrl/resources/report/collection.rb +40 -0
  103. data/lib/cashctrl/resources/report/element.rb +48 -0
  104. data/lib/cashctrl/resources/report.rb +34 -0
  105. data/lib/cashctrl/resources/rounding.rb +10 -0
  106. data/lib/cashctrl/resources/salary/book_entry.rb +12 -0
  107. data/lib/cashctrl/resources/salary/category.rb +16 -0
  108. data/lib/cashctrl/resources/salary/certificate/document.rb +47 -0
  109. data/lib/cashctrl/resources/salary/certificate/template.rb +18 -0
  110. data/lib/cashctrl/resources/salary/certificate.rb +20 -0
  111. data/lib/cashctrl/resources/salary/document.rb +42 -0
  112. data/lib/cashctrl/resources/salary/field.rb +24 -0
  113. data/lib/cashctrl/resources/salary/insurance/type.rb +14 -0
  114. data/lib/cashctrl/resources/salary/layout.rb +12 -0
  115. data/lib/cashctrl/resources/salary/payment.rb +36 -0
  116. data/lib/cashctrl/resources/salary/setting.rb +12 -0
  117. data/lib/cashctrl/resources/salary/statement.rb +42 -0
  118. data/lib/cashctrl/resources/salary/status.rb +16 -0
  119. data/lib/cashctrl/resources/salary/sum.rb +12 -0
  120. data/lib/cashctrl/resources/salary/template.rb +16 -0
  121. data/lib/cashctrl/resources/salary/type.rb +17 -0
  122. data/lib/cashctrl/resources/sequence_number.rb +14 -0
  123. data/lib/cashctrl/resources/setting.rb +34 -0
  124. data/lib/cashctrl/resources/tax.rb +10 -0
  125. data/lib/cashctrl/resources/text.rb +10 -0
  126. data/lib/cashctrl/schema.yml +712 -0
  127. data/lib/cashctrl/version.rb +2 -2
  128. data/lib/cashctrl.rb +125 -3
  129. data/lib/generators/model_generator.rb +104 -0
  130. data/sig/cashctrl.rbs +1 -1
  131. metadata +126 -2
data/cashctrl.gemspec CHANGED
@@ -4,7 +4,7 @@ require_relative "lib/cashctrl/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "cashctrl"
7
- spec.version = Cashctrl::VERSION
7
+ spec.version = CashCtrl::VERSION
8
8
  spec.authors = ["Marco Roth"]
9
9
  spec.email = ["marco.roth@intergga.ch"]
10
10
 
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "date"
5
+ require "faraday"
6
+ require "json"
7
+
8
+ module CashCtrl
9
+ class Client
10
+ attr_reader :config
11
+
12
+ def initialize(org: nil, api_key: nil, language: "en")
13
+ @config = Configuration.new
14
+ @config.org = org
15
+ @config.api_key = api_key
16
+ @config.language = language
17
+ end
18
+
19
+ def connection
20
+ @connection ||= build_connection
21
+ end
22
+
23
+ MAX_REDIRECTS = 5
24
+
25
+ def get(path, params = {})
26
+ response = connection.get(path, prepare_params(params))
27
+ handle_response(response)
28
+ end
29
+
30
+ def post(path, params = {})
31
+ response = connection.post(path, prepare_params(params))
32
+ handle_response(response)
33
+ end
34
+
35
+ def get_raw(path, params = {})
36
+ response = raw_connection.get(path, prepare_params(params))
37
+ response = follow_redirects(response)
38
+ handle_raw_response(response)
39
+ end
40
+
41
+ def put_raw(url, body, content_type: "application/octet-stream")
42
+ response = Faraday.put(url, body, "Content-Type" => content_type)
43
+ return response if response.success?
44
+
45
+ raise Error.new("Upload failed: #{response.status}", response: response)
46
+ end
47
+
48
+ RESOURCES = {
49
+ accounts: "Account",
50
+ account_banks: "Account::Bank",
51
+ account_categories: "Account::Category",
52
+ account_cost_centers: "Account::CostCenter",
53
+ account_cost_center_categories: "Account::CostCenter::Category",
54
+ currencies: "Currency",
55
+ custom_fields: "CustomField",
56
+ custom_field_groups: "CustomField::Group",
57
+ files: "File",
58
+ file_categories: "File::Category",
59
+ fiscal_periods: "FiscalPeriod",
60
+ fiscal_period_tasks: "FiscalPeriod::Task",
61
+ history: "History",
62
+ inventory_articles: "Inventory::Article",
63
+ inventory_article_categories: "Inventory::Article::Category",
64
+ inventory_article_imports: "Inventory::Article::Import",
65
+ inventory_assets: "Inventory::Asset",
66
+ inventory_asset_categories: "Inventory::Asset::Category",
67
+ inventory_units: "Inventory::Unit",
68
+ journals: "Journal",
69
+ journal_imports: "Journal::Import",
70
+ journal_import_entries: "Journal::Import::Entry",
71
+ locations: "Location",
72
+ orders: "Order",
73
+ order_book_entries: "Order::BookEntry",
74
+ order_categories: "Order::Category",
75
+ order_documents: "Order::Document",
76
+ order_layouts: "Order::Layout",
77
+ order_payments: "Order::Payment",
78
+ organization: "Organization",
79
+ people: "Person",
80
+ person_categories: "Person::Category",
81
+ person_imports: "Person::Import",
82
+ person_titles: "Person::Title",
83
+ reports: "Report",
84
+ report_collections: "Report::Collection",
85
+ report_elements: "Report::Element",
86
+ roundings: "Rounding",
87
+ salary_book_entries: "Salary::BookEntry",
88
+ salary_categories: "Salary::Category",
89
+ salary_certificates: "Salary::Certificate",
90
+ salary_certificate_documents: "Salary::Certificate::Document",
91
+ salary_certificate_templates: "Salary::Certificate::Template",
92
+ salary_documents: "Salary::Document",
93
+ salary_fields: "Salary::Field",
94
+ salary_insurance_types: "Salary::Insurance::Type",
95
+ salary_layouts: "Salary::Layout",
96
+ salary_payments: "Salary::Payment",
97
+ salary_settings: "Salary::Setting",
98
+ salary_statements: "Salary::Statement",
99
+ salary_statuses: "Salary::Status",
100
+ salary_sums: "Salary::Sum",
101
+ salary_templates: "Salary::Template",
102
+ salary_types: "Salary::Type",
103
+ sequence_numbers: "SequenceNumber",
104
+ settings: "Setting",
105
+ taxes: "Tax",
106
+ texts: "Text",
107
+ }.freeze
108
+
109
+ RESOURCES.each do |name, resource|
110
+ define_method(name) do
111
+ Resources.const_get(resource).with_client(self)
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def prepare_params(params)
118
+ params = params.transform_values { |value| encode_param(value) }
119
+ params = params.merge(lang: config.language) if config.language
120
+ params
121
+ end
122
+
123
+ def encode_param(value)
124
+ case value
125
+ when Array, Hash then value.to_json
126
+ when BigDecimal then value.to_s("F")
127
+ when DateTime, Time then value.strftime("%Y-%m-%d %H:%M:%S")
128
+ when Date then value.iso8601
129
+ else value
130
+ end
131
+ end
132
+
133
+ def follow_redirects(response)
134
+ MAX_REDIRECTS.times do
135
+ break unless [301, 302, 303, 307, 308].include?(response.status) && response.headers["location"]
136
+
137
+ response = Faraday.get(response.headers["location"])
138
+ end
139
+
140
+ response
141
+ end
142
+
143
+ def build_connection
144
+ config.validate!
145
+
146
+ Faraday.new(url: config.base_url) do |f|
147
+ f.request :url_encoded
148
+ f.request :authorization, :basic, config.api_key, ""
149
+ f.response :json, content_type: /\bjson$/
150
+ f.adapter Faraday.default_adapter
151
+ end
152
+ end
153
+
154
+ def raw_connection
155
+ @raw_connection ||= begin
156
+ config.validate!
157
+
158
+ Faraday.new(url: config.base_url) do |f|
159
+ f.request :url_encoded
160
+ f.request :authorization, :basic, config.api_key, ""
161
+ f.adapter Faraday.default_adapter
162
+ end
163
+ end
164
+ end
165
+
166
+ def handle_response(response)
167
+ case response.status
168
+ when 200
169
+ handle_success_response(response)
170
+ when 401
171
+ raise AuthenticationError.new("Invalid API key", response: response)
172
+ when 403
173
+ raise PermissionError.new("API key lacks permission for this request", response: response)
174
+ when 404
175
+ raise NotFoundError.new("Resource not found", response: response)
176
+ when 429
177
+ raise RateLimitError.new("Rate limit exceeded", response: response)
178
+ when 500..599
179
+ raise ServerError.new("Server error: #{response.status}", response: response)
180
+ else
181
+ raise Error.new("Unexpected response: #{response.status}", response: response)
182
+ end
183
+ end
184
+
185
+ def handle_success_response(response)
186
+ body = response.body
187
+
188
+ return body unless body.is_a?(Hash)
189
+
190
+ if body["success"] == false
191
+ errors = body["errors"] || []
192
+ message = body["message"] || body["errorMessage"] ||
193
+ errors.map { |e| [e["field"], e["message"]].compact.join(": ") }.join(", ")
194
+ raise ValidationError.new(message, errors: errors, response: response)
195
+ end
196
+
197
+ body
198
+ end
199
+
200
+ def handle_raw_response(response)
201
+ case response.status
202
+ when 200
203
+ response.body
204
+ when 401
205
+ raise AuthenticationError.new("Invalid API key", response: response)
206
+ when 403
207
+ raise PermissionError.new("API key lacks permission for this request", response: response)
208
+ when 404
209
+ raise NotFoundError.new("Resource not found", response: response)
210
+ when 429
211
+ raise RateLimitError.new("Rate limit exceeded", response: response)
212
+ when 500..599
213
+ raise ServerError.new("Server error: #{response.status}", response: response)
214
+ else
215
+ raise Error.new("Unexpected response: #{response.status}", response: response)
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ class Collection
5
+ include Enumerable
6
+
7
+ attr_reader :data, :total, :summary, :properties, :resource_class
8
+
9
+ def self.from_response(response, resource_class: Resource, resource: nil)
10
+ data = response.is_a?(Array) ? response : (response["data"] || [])
11
+
12
+ new(
13
+ data: data,
14
+ total: response.is_a?(Hash) ? response["total"] : nil,
15
+ summary: response.is_a?(Hash) ? response["summary"] : nil,
16
+ properties: response.is_a?(Hash) ? response["properties"] : nil,
17
+ resource_class: resource_class,
18
+ resource: resource
19
+ )
20
+ end
21
+
22
+ def initialize(data:, total: nil, summary: nil, properties: nil, resource_class: Resource, resource: nil)
23
+ @resource_class = resource_class
24
+ @data = data.map { |item| resource_class.from_response(item, resource: resource) }
25
+ @total = total || @data.size
26
+ @summary = summary
27
+ @properties = properties
28
+ end
29
+
30
+ def each(&)
31
+ @data.each(&)
32
+ end
33
+
34
+ def size
35
+ @data.size
36
+ end
37
+
38
+ alias length size
39
+ alias count size
40
+
41
+ def empty?
42
+ @data.empty?
43
+ end
44
+
45
+ def first
46
+ @data.first
47
+ end
48
+
49
+ def last
50
+ @data.last
51
+ end
52
+
53
+ def [](index)
54
+ @data[index]
55
+ end
56
+
57
+ def to_a
58
+ @data.dup
59
+ end
60
+
61
+ def to_h
62
+ {
63
+ "data" => @data.map(&:to_h),
64
+ "total" => @total,
65
+ "summary" => @summary,
66
+ "properties" => @properties,
67
+ }
68
+ end
69
+
70
+ def to_json(*)
71
+ to_h.to_json(*)
72
+ end
73
+
74
+ def inspect
75
+ "#<#{self.class.name} total=#{@total} size=#{size} resource_class=#{@resource_class.name}>"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ class Configuration
5
+ attr_accessor :org, :api_key, :language
6
+
7
+ def initialize
8
+ @org = ENV.fetch("CASHCTRL_ORG", nil)
9
+ @api_key = ENV.fetch("CASHCTRL_API_KEY", nil)
10
+ @language = ENV.fetch("CASHCTRL_LANGUAGE", "en")
11
+ end
12
+
13
+ def base_url
14
+ raise ConfigurationError, "org must be configured" if org.nil? || org.empty?
15
+
16
+ "https://#{org}.cashctrl.com/api/v1"
17
+ end
18
+
19
+ def valid?
20
+ !org.nil? && !org.empty? && !api_key.nil? && !api_key.empty?
21
+ end
22
+
23
+ def validate! # rubocop:disable Naming/PredicateMethod
24
+ raise ConfigurationError, "org must be configured" if org.nil? || org.empty?
25
+ raise ConfigurationError, "api_key must be configured" if api_key.nil? || api_key.empty?
26
+
27
+ true
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ class Error < StandardError
5
+ attr_reader :response
6
+
7
+ def initialize(message = nil, response: nil)
8
+ @response = response
9
+ super(message)
10
+ end
11
+ end
12
+
13
+ class ConfigurationError < Error; end
14
+ class AuthenticationError < Error; end
15
+ class PermissionError < Error; end
16
+ class NotFoundError < Error; end
17
+ class RateLimitError < Error; end
18
+ class ServerError < Error; end
19
+
20
+ class ValidationError < Error
21
+ attr_reader :errors
22
+ attr_accessor :record
23
+
24
+ def initialize(message = nil, errors: [], response: nil)
25
+ @errors = errors
26
+ super(message, response: response)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ class Errors
5
+ include Enumerable
6
+
7
+ def initialize
8
+ @messages = {}
9
+ end
10
+
11
+ def add(attribute, message)
12
+ (@messages[attribute.to_sym] ||= []) << message
13
+ end
14
+
15
+ def add_api_errors(error)
16
+ error.errors.each do |entry|
17
+ field = entry["field"].to_s.gsub(/([A-Z])/) { "_#{Regexp.last_match(1).downcase}" }
18
+ add(field.empty? ? :base : field, entry["message"])
19
+ end
20
+
21
+ add(:base, error.message) if error.errors.empty? && error.message
22
+ end
23
+
24
+ def [](attribute)
25
+ @messages.fetch(attribute.to_sym, [])
26
+ end
27
+
28
+ def messages
29
+ @messages.transform_values(&:dup)
30
+ end
31
+
32
+ alias to_h messages
33
+
34
+ def include?(attribute)
35
+ @messages.key?(attribute.to_sym)
36
+ end
37
+
38
+ def each(&)
39
+ @messages.each { |attribute, messages| messages.each { |message| yield attribute, message } }
40
+ end
41
+
42
+ def full_messages
43
+ map { |attribute, message| full_message(attribute, message) }
44
+ end
45
+
46
+ def full_messages_for(attribute)
47
+ self[attribute].map { |message| full_message(attribute.to_sym, message) }
48
+ end
49
+
50
+ def size
51
+ @messages.values.sum(&:size)
52
+ end
53
+
54
+ alias count size
55
+
56
+ def empty?
57
+ @messages.empty?
58
+ end
59
+
60
+ def clear
61
+ @messages.clear
62
+ end
63
+
64
+ def inspect
65
+ "#<#{self.class.name} #{@messages.inspect}>"
66
+ end
67
+
68
+ private
69
+
70
+ def full_message(attribute, message)
71
+ return message if attribute == :base
72
+
73
+ "#{attribute.to_s.tr("_", " ").capitalize} #{message}"
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rexml/document"
4
+
5
+ module CashCtrl
6
+ class LocalizedString
7
+ LOCALIZED_PATTERN = %r{<values>.*?</values>}m
8
+
9
+ attr_reader :translations, :raw, :prefix
10
+
11
+ def self.localized?(value)
12
+ return false unless value.is_a?(String)
13
+
14
+ value.match?(LOCALIZED_PATTERN)
15
+ end
16
+
17
+ def self.parse(value)
18
+ return value unless localized?(value)
19
+
20
+ new(value)
21
+ end
22
+
23
+ def initialize(xml_string)
24
+ @translations = {}
25
+ @raw = xml_string
26
+ @prefix = ""
27
+
28
+ parse_xml(xml_string)
29
+ end
30
+
31
+ def [](locale)
32
+ value = @translations[locale.to_s.downcase]
33
+ return nil unless value
34
+
35
+ @prefix.empty? ? value : "#{@prefix}#{value}"
36
+ end
37
+
38
+ def to_s
39
+ current_locale = if defined?(I18n) && I18n.respond_to?(:locale)
40
+ I18n.locale.to_s.downcase
41
+ else
42
+ CashCtrl.configuration&.language&.downcase || "en"
43
+ end
44
+
45
+ value = @translations[current_locale] || @translations["en"] || @translations.values.first
46
+ return @raw unless value
47
+
48
+ @prefix.empty? ? value : "#{@prefix}#{value}"
49
+ end
50
+
51
+ def to_h
52
+ @translations.dup
53
+ end
54
+
55
+ def inspect
56
+ if @prefix.empty?
57
+ "#<#{self.class.name} #{@translations.inspect}>"
58
+ else
59
+ "#<#{self.class.name} prefix=#{@prefix.inspect} #{@translations.inspect}>"
60
+ end
61
+ end
62
+
63
+ def ==(other)
64
+ case other
65
+ when LocalizedString
66
+ @translations == other.translations
67
+ when String
68
+ to_s == other
69
+ else
70
+ false
71
+ end
72
+ end
73
+
74
+ alias eql? ==
75
+
76
+ def hash
77
+ @translations.hash
78
+ end
79
+
80
+ def method_missing(method_name, *args)
81
+ locale = method_name.to_s.downcase
82
+
83
+ if @translations.key?(locale)
84
+ value = @translations[locale]
85
+ @prefix.empty? ? value : "#{@prefix}#{value}"
86
+ else
87
+ super
88
+ end
89
+ end
90
+
91
+ def respond_to_missing?(method_name, include_private = false)
92
+ @translations.key?(method_name.to_s.downcase) || super
93
+ end
94
+
95
+ private
96
+
97
+ def parse_xml(xml_string)
98
+ match = xml_string.match(LOCALIZED_PATTERN)
99
+
100
+ return unless match
101
+
102
+ @prefix = xml_string[0...match.begin(0)]
103
+ values_xml = match[0]
104
+
105
+ doc = REXML::Document.new(values_xml)
106
+
107
+ doc.root&.elements&.each do |element|
108
+ locale = element.name.downcase
109
+ @translations[locale] = element.text || ""
110
+ end
111
+ rescue REXML::ParseException
112
+ @translations["raw"] = xml_string
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Account < Resource
8
+ attribute :category_id, type: :integer
9
+ attribute :category_display, type: :string, readonly: true
10
+ attribute :account_class, type: :string
11
+ attribute :tax_id, type: :integer
12
+ attribute :tax_name, type: :string, readonly: true
13
+ attribute :currency_id, type: :integer
14
+ attribute :currency_code, type: :string
15
+ attribute :number, type: :string
16
+ attribute :name, type: :string
17
+ attribute :custom, type: :string
18
+ attribute :notes, type: :string
19
+ attribute :attachment_count, type: :integer, readonly: true
20
+ attribute :allocation_count, type: :integer, readonly: true
21
+ attribute :cost_center_ids, type: :string
22
+ attribute :cost_center_numbers, type: :string, readonly: true
23
+ attribute :opening_amount, type: :decimal
24
+ attribute :end_amount, type: :decimal, readonly: true
25
+ attribute :target_min, type: :decimal
26
+ attribute :target_max, type: :decimal
27
+ attribute :target_display, type: :string, readonly: true
28
+ attribute :default_currency_opening_amount, type: :decimal, readonly: true
29
+ attribute :default_currency_end_amount, type: :decimal, readonly: true
30
+ attribute :is_inactive, type: :boolean
31
+ attribute :is_in_salary_type, type: :boolean, readonly: true
32
+ attribute :is_bank_account, type: :boolean, readonly: true
33
+ belongs_to :category, resource: "Account::Category"
34
+ belongs_to :tax, resource: "Tax"
35
+ belongs_to :currency, resource: "Currency"
36
+ nested :attachments, model: "Attachment"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Article < Resource
8
+ attribute :category_id, type: :integer
9
+ attribute :category_name, type: :string, readonly: true
10
+ attribute :nr, type: :string
11
+ attribute :name, type: :string
12
+ attribute :description, type: :string
13
+ attribute :unit_id, type: :integer
14
+ attribute :unit_name, type: :string, readonly: true
15
+ attribute :unit_price, type: :decimal
16
+ attribute :sales_account_id, type: :integer
17
+ attribute :sales_account_name, type: :string, readonly: true
18
+ attribute :purchase_account_id, type: :integer
19
+ attribute :purchase_account_name, type: :string, readonly: true
20
+ attribute :sales_tax_id, type: :integer
21
+ attribute :sales_tax_name, type: :string, readonly: true
22
+ attribute :purchase_tax_id, type: :integer
23
+ attribute :purchase_tax_name, type: :string, readonly: true
24
+ attribute :currency_id, type: :integer
25
+ attribute :currency_code, type: :string, readonly: true
26
+ attribute :last_purchase_price, type: :decimal, readonly: true
27
+ attribute :last_sales_price, type: :decimal, readonly: true
28
+ attribute :bin_location, type: :string
29
+ attribute :stock, type: :decimal
30
+ attribute :min_stock, type: :decimal
31
+ attribute :custom, type: :string
32
+ attribute :notes, type: :string
33
+ attribute :attachment_count, type: :integer, readonly: true
34
+ attribute :is_stock_article, type: :boolean
35
+ attribute :is_purchase_price_gross, type: :boolean
36
+ attribute :is_sales_price_gross, type: :boolean
37
+ attribute :is_inactive, type: :boolean
38
+ belongs_to :category, resource: "Inventory::Article::Category"
39
+ belongs_to :unit, resource: "Inventory::Unit"
40
+ belongs_to :sales_account, resource: "Account"
41
+ belongs_to :purchase_account, resource: "Account"
42
+ belongs_to :sales_tax, resource: "Tax"
43
+ belongs_to :purchase_tax, resource: "Tax"
44
+ belongs_to :currency, resource: "Currency"
45
+ nested :attachments, model: "Attachment"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Asset < Resource
8
+ attribute :category_id, type: :integer
9
+ attribute :category_name, type: :string, readonly: true
10
+ attribute :nr, type: :string
11
+ attribute :name, type: :string
12
+ attribute :date_acquired, type: :date
13
+ attribute :value_acquisition, type: :decimal
14
+ attribute :value_current, type: :decimal, readonly: true
15
+ attribute :value_depreciation, type: :decimal, readonly: true
16
+ attribute :value_salvage, type: :decimal
17
+ attribute :depreciation_type, type: :string
18
+ attribute :depreciation_rate, type: :decimal
19
+ attribute :useful_life, type: :integer
20
+ attribute :account_asset_id, type: :integer
21
+ attribute :account_asset_name, type: :string, readonly: true
22
+ attribute :account_depreciation_id, type: :integer
23
+ attribute :account_depreciation_name, type: :string, readonly: true
24
+ attribute :account_expense_id, type: :integer
25
+ attribute :account_expense_name, type: :string, readonly: true
26
+ attribute :custom, type: :string
27
+ attribute :notes, type: :string
28
+ attribute :attachment_count, type: :integer, readonly: true
29
+ attribute :is_inactive, type: :boolean
30
+ belongs_to :category, resource: "Inventory::Asset::Category"
31
+ belongs_to :account_asset, resource: "Account"
32
+ belongs_to :account_depreciation, resource: "Account"
33
+ belongs_to :account_expense, resource: "Account"
34
+ nested :attachments, model: "Attachment"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from schema.yml. Do not edit manually.
4
+
5
+ module CashCtrl
6
+ module Models
7
+ class Attachment < Resource
8
+ attribute :file_id, type: :integer
9
+ attribute :name, type: :string
10
+ attribute :mime_type, type: :string
11
+ belongs_to :file, resource: "File"
12
+ end
13
+ end
14
+ end