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
@@ -0,0 +1,433 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "date"
5
+ require "json"
6
+
7
+ module CashCtrl
8
+ class Resource
9
+ def self.attribute(name, type: :string, readonly: false)
10
+ attributes[name.to_sym] = { type: type, readonly: readonly }
11
+
12
+ define_method(name) do
13
+ @attributes[name.to_sym]
14
+ end
15
+
16
+ return if readonly
17
+
18
+ define_method("#{name}=") do |value|
19
+ write_attribute(name, value)
20
+ end
21
+ end
22
+
23
+ def self.attributes
24
+ @attributes ||= {}
25
+ end
26
+
27
+ def self.belongs_to(name, resource:, foreign_key: :"#{name}_id")
28
+ associations[name.to_sym] = { resource: resource, foreign_key: foreign_key.to_sym }
29
+
30
+ define_method(name) do
31
+ read_association(name.to_sym)
32
+ end
33
+
34
+ define_method("#{name}=") do |record|
35
+ write_association(name.to_sym, record)
36
+ end
37
+ end
38
+
39
+ def self.associations
40
+ @associations ||= {}
41
+ end
42
+
43
+ def self.has_many(name, resource:, foreign_key:)
44
+ define_method(name) do
45
+ Resources.const_get(resource).with_client(association_client).where(foreign_key => id)
46
+ end
47
+ end
48
+
49
+ def self.nested(name, model:)
50
+ define_method(name) do
51
+ read_nested(name.to_sym, model)
52
+ end
53
+ end
54
+
55
+ def self.inherited(subclass)
56
+ super
57
+
58
+ subclass.instance_variable_set(:@attributes, attributes.dup)
59
+ subclass.instance_variable_set(:@associations, associations.dup)
60
+ end
61
+
62
+ def self.from_response(response, resource: nil)
63
+ return nil if response.nil?
64
+
65
+ data = response.is_a?(Hash) && response.key?("data") ? response["data"] : response
66
+ new(data, resource: resource)
67
+ end
68
+
69
+ attribute :id, type: :integer, readonly: true
70
+ attribute :created, type: :datetime, readonly: true
71
+ attribute :created_by, type: :string, readonly: true
72
+ attribute :last_updated, type: :datetime, readonly: true
73
+ attribute :last_updated_by, type: :string, readonly: true
74
+
75
+ attr_reader :raw_data, :response
76
+ attr_accessor :resource
77
+
78
+ def initialize(attributes = {}, resource: nil, client: nil)
79
+ @resource = resource
80
+ @client = client
81
+ @destroyed = false
82
+ @saved_changes = {}
83
+ load_attributes(attributes)
84
+ end
85
+
86
+ def [](key)
87
+ snake_key = underscore(key.to_s).to_sym
88
+ @attributes.key?(snake_key) ? @attributes[snake_key] : @extra_attributes[snake_key]
89
+ end
90
+
91
+ def []=(key, value)
92
+ write_attribute(key, value)
93
+ end
94
+
95
+ def attributes
96
+ @attributes.merge(@extra_attributes).transform_keys(&:to_s)
97
+ end
98
+
99
+ def assign_attributes(new_attributes)
100
+ new_attributes.each do |key, value|
101
+ if self.class.associations.key?(key.to_sym)
102
+ write_association(key.to_sym, value)
103
+ else
104
+ write_attribute(key, value)
105
+ end
106
+ end
107
+ end
108
+
109
+ alias attributes= assign_attributes
110
+
111
+ def changed?
112
+ @changes.any?
113
+ end
114
+
115
+ def changed
116
+ @changes.keys.map(&:to_s)
117
+ end
118
+
119
+ def changes
120
+ @changes.transform_keys(&:to_s)
121
+ end
122
+
123
+ def saved_changes
124
+ @saved_changes.transform_keys(&:to_s)
125
+ end
126
+
127
+ def attribute_changed?(name)
128
+ @changes.key?(underscore(name.to_s).to_sym)
129
+ end
130
+
131
+ def attribute_was(name)
132
+ snake_key = underscore(name.to_s).to_sym
133
+ @changes.key?(snake_key) ? @changes[snake_key].first : self[snake_key]
134
+ end
135
+
136
+ def errors
137
+ @errors ||= Errors.new
138
+ end
139
+
140
+ def association(name)
141
+ options = self.class.associations.fetch(name.to_sym) { raise ArgumentError, "Unknown association: #{name}" }
142
+ Resources.const_get(options[:resource]).with_client(association_client)
143
+ end
144
+
145
+ def preload_association(name, record)
146
+ @association_cache[name.to_sym] = [self[self.class.associations[name.to_sym][:foreign_key]].to_i, record]
147
+ end
148
+
149
+ def association_client
150
+ @client || resource&.client || CashCtrl.client
151
+ end
152
+
153
+ def children
154
+ @children ||= Array(@extra_attributes[:children]).map { |child| self.class.new(child, resource: resource) }
155
+ end
156
+
157
+ def new_record?
158
+ id.nil?
159
+ end
160
+
161
+ def persisted?
162
+ !new_record? && !destroyed?
163
+ end
164
+
165
+ def destroyed?
166
+ @destroyed
167
+ end
168
+
169
+ def save
170
+ save!
171
+ rescue ValidationError
172
+ false
173
+ end
174
+
175
+ def save!
176
+ errors.clear
177
+ response = bound_resource.save_record(self)
178
+ @attributes[:id] = response["insertId"].to_i if new_record? && response["insertId"]
179
+ @response = response
180
+ changes_applied
181
+ true
182
+ rescue ValidationError => e
183
+ errors.add_api_errors(e)
184
+ e.record = self
185
+ raise
186
+ end
187
+
188
+ def update(new_attributes = {}, **kwargs)
189
+ assign_attributes(new_attributes.merge(kwargs))
190
+ save
191
+ end
192
+
193
+ def update!(new_attributes = {}, **kwargs)
194
+ assign_attributes(new_attributes.merge(kwargs))
195
+ save!
196
+ end
197
+
198
+ def destroy
199
+ errors.clear
200
+ @response = bound_resource.delete(id)
201
+ @destroyed = true
202
+ self
203
+ rescue ValidationError => e
204
+ errors.add_api_errors(e)
205
+ false
206
+ end
207
+
208
+ def reload
209
+ fresh = bound_resource.read(id: id)
210
+ load_attributes(fresh.raw_data)
211
+ errors.clear
212
+ self
213
+ end
214
+
215
+ def attach(*files)
216
+ raise Error, "#{bound_resource.class.name} does not support attachments" unless bound_resource.respond_to?(:attach)
217
+
218
+ @response = bound_resource.attach(id, *files)
219
+ reload
220
+ end
221
+
222
+ def api_attributes
223
+ writable = self.class.attributes.reject { |_name, meta| meta[:readonly] }.keys
224
+ values = @attributes.slice(*writable).merge(@extra_attributes.slice(*@changes.keys))
225
+
226
+ values
227
+ .reject { |name, value| value.nil? && !@changes.key?(name) }
228
+ .to_h { |name, value| [camelize(name.to_s), serialize_value(value)] }
229
+ end
230
+
231
+ def to_h
232
+ result = {}
233
+
234
+ @attributes.each do |key, value|
235
+ result[camelize(key.to_s)] = value
236
+ end
237
+
238
+ @extra_attributes.each do |key, value|
239
+ result[camelize(key.to_s)] = value
240
+ end
241
+
242
+ result
243
+ end
244
+
245
+ def to_json(*)
246
+ to_h.to_json(*)
247
+ end
248
+
249
+ def inspect
250
+ attrs = @attributes.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
251
+
252
+ "#<#{self.class.name} #{attrs}>"
253
+ end
254
+
255
+ def ==(other)
256
+ return true if equal?(other)
257
+
258
+ other.is_a?(self.class) && !id.nil? && id == other.id
259
+ end
260
+
261
+ alias eql? ==
262
+
263
+ def hash
264
+ [self.class, id].hash
265
+ end
266
+
267
+ def method_missing(method_name, *args)
268
+ name = method_name.to_s
269
+ snake_key = name.delete_suffix("=").to_sym
270
+
271
+ if name.end_with?("_changed?") && attribute?(name.delete_suffix("_changed?"))
272
+ attribute_changed?(name.delete_suffix("_changed?"))
273
+ elsif name.end_with?("_was") && attribute?(name.delete_suffix("_was"))
274
+ attribute_was(name.delete_suffix("_was"))
275
+ elsif name.end_with?("=")
276
+ write_attribute(snake_key, args.first)
277
+ elsif @extra_attributes.key?(snake_key)
278
+ coerce_dynamic_value(@extra_attributes[snake_key])
279
+ elsif @original_attributes.key?(snake_key)
280
+ coerce_dynamic_value(@original_attributes[snake_key])
281
+ end
282
+ end
283
+
284
+ def respond_to_missing?(method_name, include_private = false)
285
+ name = method_name.to_s.delete_suffix("=").delete_suffix("_changed?").delete_suffix("_was")
286
+ attribute?(name) || super
287
+ end
288
+
289
+ private
290
+
291
+ def load_attributes(attributes)
292
+ @children = nil
293
+ @association_cache = {}
294
+ @nested_cache = {}
295
+ @raw_data = attributes.dup.freeze
296
+ @attributes = {}
297
+ @changes = {}
298
+ @original_attributes = attributes.transform_keys { |k| underscore(k.to_s).to_sym }
299
+
300
+ self.class.attributes.each do |name, meta|
301
+ @attributes[name] = coerce_value(@original_attributes[name], meta[:type])
302
+ end
303
+
304
+ @extra_attributes = @original_attributes.reject { |key, _value| self.class.attributes.key?(key) }
305
+ end
306
+
307
+ def write_attribute(key, value)
308
+ snake_key = underscore(key.to_s).to_sym
309
+ meta = self.class.attributes[snake_key]
310
+ value = coerce_value(value, meta[:type]) if meta
311
+ original = @changes.key?(snake_key) ? @changes[snake_key].first : self[snake_key]
312
+
313
+ if original == value
314
+ @changes.delete(snake_key)
315
+ else
316
+ @changes[snake_key] = [original, value]
317
+ end
318
+
319
+ if meta
320
+ @attributes[snake_key] = value
321
+ else
322
+ @extra_attributes[snake_key] = value
323
+ end
324
+ end
325
+
326
+ def read_association(name)
327
+ id = self[self.class.associations[name][:foreign_key]].to_i
328
+ return nil if id.zero?
329
+
330
+ cached = @association_cache[name]
331
+ return cached.last if cached && cached.first == id
332
+
333
+ record = begin
334
+ association(name).find(id)
335
+ rescue NotFoundError
336
+ nil
337
+ end
338
+
339
+ @association_cache[name] = [id, record]
340
+ record
341
+ end
342
+
343
+ def write_association(name, record)
344
+ write_attribute(self.class.associations[name][:foreign_key], record.is_a?(Resource) ? record.id : record)
345
+ @association_cache[name] = [record.id, record] if record.is_a?(Resource)
346
+ end
347
+
348
+ def read_nested(name, model)
349
+ raw = self[name]
350
+ raw = JSON.parse(raw) if raw.is_a?(String)
351
+ cached = @nested_cache[name]
352
+ return cached.last if cached && cached.first.equal?(raw)
353
+
354
+ records = Array(raw).map do |item|
355
+ item.is_a?(Resource) ? item : Models.const_get(model).new(item, client: association_client)
356
+ end
357
+ @nested_cache[name] = [raw, records]
358
+ records
359
+ end
360
+
361
+ def attribute?(name)
362
+ snake_key = name.to_sym
363
+ self.class.attributes.key?(snake_key) || @extra_attributes.key?(snake_key)
364
+ end
365
+
366
+ def changes_applied
367
+ @saved_changes = @changes
368
+ @changes = {}
369
+ end
370
+
371
+ def bound_resource
372
+ @resource || raise(Error, "#{self.class.name} is not bound to a resource")
373
+ end
374
+
375
+ def serialize_value(value)
376
+ case value
377
+ when nil then ""
378
+ when DateTime, Time then value.strftime("%Y-%m-%d %H:%M:%S")
379
+ when Date then value.iso8601
380
+ when BigDecimal then value.to_s("F")
381
+ when LocalizedString then value.raw
382
+ when Array then value.map { |item| item.is_a?(Resource) ? item.to_h : item }
383
+ else value
384
+ end
385
+ end
386
+
387
+ def coerce_dynamic_value(value)
388
+ return value unless value.is_a?(String)
389
+
390
+ LocalizedString.localized?(value) ? LocalizedString.parse(value) : value
391
+ end
392
+
393
+ def coerce_value(value, type)
394
+ return nil if value.nil?
395
+
396
+ case type
397
+ when :integer
398
+ value.to_i
399
+ when :decimal, :float
400
+ value.is_a?(BigDecimal) ? value : BigDecimal(value.to_s)
401
+ when :boolean
402
+ [true, "true", 1].include?(value)
403
+ when :date
404
+ value.is_a?(Date) ? value : Date.parse(value.to_s)
405
+ when :datetime
406
+ case value
407
+ when DateTime then value
408
+ when Time then value.to_datetime
409
+ else DateTime.parse(value.to_s)
410
+ end
411
+ when :json
412
+ value.is_a?(String) ? JSON.parse(value) : value
413
+ when :localized
414
+ LocalizedString.parse(value)
415
+ when :string
416
+ # Auto-detect localized strings
417
+ LocalizedString.localized?(value) ? LocalizedString.parse(value) : value
418
+ else
419
+ value
420
+ end
421
+ rescue ArgumentError, JSON::ParserError
422
+ value
423
+ end
424
+
425
+ def underscore(string)
426
+ string.gsub(/([A-Z])/, '_\1').sub(/^_/, "").downcase
427
+ end
428
+
429
+ def camelize(string)
430
+ string.split("_").each_with_index.map { |part, index| index.zero? ? part : part.capitalize }.join
431
+ end
432
+ end
433
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ class Account
6
+ class Bank < Base
7
+ include Attachments
8
+
9
+ api_path "account/bank"
10
+ model_class Models::Bank
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ class Account
6
+ class Category < Base
7
+ api_path "account/category"
8
+ model_class Models::Category
9
+
10
+ def tree(id = nil)
11
+ tree_collection(client.get("#{api_path}/tree.json", api_params(id: id)))
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ class Account
6
+ class CostCenter
7
+ class Category < Base
8
+ api_path "account/costcenter/category"
9
+ model_class Models::Category
10
+
11
+ def tree(id = nil)
12
+ tree_collection(client.get("#{api_path}/tree.json", api_params(id: id)))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ class Account
6
+ class CostCenter < Base
7
+ include Attachments
8
+
9
+ api_path "account/costcenter"
10
+ model_class Models::CostCenter
11
+
12
+ def balance(id:, date: nil)
13
+ params = { id: id }
14
+ params[:date] = date if date
15
+ result = client.get_raw("#{api_path}/balance", params)
16
+ BigDecimal(result.to_s)
17
+ end
18
+
19
+ def categorize(ids:, target:)
20
+ ids_param = Array(ids).join(",")
21
+ client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ class Account < Base
6
+ include Attachments
7
+
8
+ api_path "account"
9
+ model_class Models::Account
10
+
11
+ def find_by_number(number)
12
+ all.find { |account| account.number.to_s == number.to_s }
13
+ end
14
+
15
+ def balance(id:, date: nil)
16
+ params = { id: id }
17
+ params[:date] = date if date
18
+ result = client.get_raw("#{api_path}/balance", params)
19
+ BigDecimal(result.to_s)
20
+ end
21
+
22
+ def categorize(ids:, target:)
23
+ ids_param = Array(ids).join(",")
24
+ client.post("#{api_path}/categorize.json", ids: ids_param, target: target)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CashCtrl
4
+ module Resources
5
+ module Attachments
6
+ def update_attachments(id:, file_ids:)
7
+ client.post("#{api_path}/update_attachments.json", id: id, fileIds: csv(file_ids))
8
+ end
9
+
10
+ def add_attachments(id:, file_ids:)
11
+ response = client.get("#{api_path}/read.json", id: id)
12
+ existing = Array(response.dig("data", "attachments")).map { |attachment| attachment["fileId"] }
13
+
14
+ update_attachments(id: id, file_ids: (existing + Array(file_ids)).uniq)
15
+ end
16
+
17
+ def attach(id, *files)
18
+ add_attachments(id: id, file_ids: files.flatten.map { |file| attachment_file_id(file) })
19
+ end
20
+
21
+ private
22
+
23
+ def attachment_file_id(file)
24
+ case file
25
+ when Integer then file
26
+ when Models::File then file.id
27
+ else File.with_client(client).upload(file)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end