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/README.md CHANGED
@@ -1,39 +1,856 @@
1
- # Cashctrl
1
+ # CashCtrl Ruby
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cashctrl`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A Ruby gem to interact with the [CashCtrl](https://cashctrl.com) Swiss accounting software API.
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "cashctrl"
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ And then execute:
12
14
 
13
15
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ bundle install
15
17
  ```
16
18
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ Or install it yourself as:
18
20
 
19
21
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ gem install cashctrl
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ ### Global Configuration
28
+
29
+ ```ruby
30
+ CashCtrl.configure do |config|
31
+ config.org = "your-organization"
32
+ config.api_key = ENV["CASHCTRL_API_KEY"]
33
+ config.language = "en"
34
+ end
35
+ ```
36
+
37
+ - `org` - Your CashCtrl subdomain
38
+ - `api_key` - Your API key
39
+ - `language` - Optional: `en`, `de`, `fr`, `it` (default: `en`)
40
+
41
+ ### Per-Client Configuration
42
+
43
+ For multi-tenant applications or testing:
44
+
45
+ ```ruby
46
+ client = CashCtrl::Client.new(
47
+ org: "other-organization",
48
+ api_key: "different-api-key",
49
+ language: "de"
50
+ )
21
51
  ```
22
52
 
23
53
  ## Usage
24
54
 
25
- TODO: Write usage instructions here
55
+ ### Two API Styles
56
+
57
+ The gem supports two usage patterns:
58
+
59
+ #### 1. Class Methods (uses global configuration)
60
+
61
+ **List all accounts:**
62
+
63
+ ```ruby
64
+ CashCtrl::Account.list
65
+ ```
66
+
67
+ **Find a specific account:**
68
+
69
+ ```ruby
70
+ CashCtrl::Account.find(1)
71
+ ```
72
+
73
+ **Create an account:**
74
+
75
+ ```ruby
76
+ account = CashCtrl::Account.create(number: "1000", name: "Cash", category_id: 1)
77
+ account.persisted? # => true
78
+ account.id
79
+ ```
80
+
81
+ `create` returns the new record, read back from CashCtrl. If CashCtrl rejects it, you get the unsaved record with `errors` filled. `create!` raises `CashCtrl::ValidationError` instead. Resources that have no `read` endpoint return the API response.
82
+
83
+ **Update an account:**
84
+
85
+ ```ruby
86
+ CashCtrl::Account.update(1, name: "Updated Name")
87
+ ```
88
+
89
+ `update` reads the record first and sends your changes merged over its current values, so fields you leave out keep their value. It returns the reloaded record, or the record with `errors` if CashCtrl rejected it. `update!` raises `CashCtrl::ValidationError` instead. To send exactly the fields you pass and nothing else, use `replace`:
90
+
91
+ ```ruby
92
+ CashCtrl::Account.replace(1, number: "1000", name: "Cash", category_id: 1)
93
+ ```
94
+
95
+ **Delete accounts:**
96
+
97
+ ```ruby
98
+ CashCtrl::Account.delete(1, 2, 3) # one delete.json request
99
+ CashCtrl::Account.delete([1, 2, 3])
100
+ CashCtrl::Account.destroy(1, 2) # loads and destroys each record
101
+ CashCtrl::Account.where(category_id: 9).delete_all
102
+ CashCtrl::Account.where(category_id: 9).destroy_all
103
+ ```
104
+
105
+ `delete` and `delete_all` send the ids in a single request and return the API response. `destroy` and `destroy_all` return the destroyed records, with `errors` filled for any that CashCtrl refused to delete.
106
+
107
+ Attributes and filters can be passed in snake_case. They are converted to CashCtrl's camelCase names before the request is sent, and camelCase keys are passed through unchanged. Keys inside nested arrays and hashes (like order `items`) are sent as given.
108
+
109
+ #### 2. Client Instance (custom configuration)
110
+
111
+ ```ruby
112
+ client = CashCtrl::Client.new(org: "myorg", api_key: "key")
113
+
114
+ client.accounts.list
115
+ client.accounts.find(1)
116
+ client.accounts.create(number: "1000", name: "Cash", category_id: 1)
117
+ client.people.list
118
+ client.orders.list
119
+ client.salary_statements.list
120
+ ```
121
+
122
+ Every resource has an accessor named after its constant: `CashCtrl::Account::CostCenter` is `client.account_cost_centers`, `CashCtrl::Inventory::Article` is `client.inventory_articles`, `CashCtrl::Person` is `client.people`. The full list is in `CashCtrl::Client::RESOURCES`.
123
+
124
+ #### Querying
125
+
126
+ `all`, `where`, `order`, `limit` and `offset` return a lazy, chainable `CashCtrl::Relation`. Nothing is requested until you enumerate it, and `all` fetches every page.
127
+
128
+ ```ruby
129
+ CashCtrl::Journal.all
130
+ CashCtrl::Journal.where(fiscal_period_id: 3).order(date_added: :desc).limit(20)
131
+ CashCtrl::Person.where(last_name: "Lovelace").where(category_id: 3).order(:first_name)
132
+ CashCtrl::Person.offset(100).limit(50).to_a
133
+ ```
134
+
135
+ `where` sends list parameters like `query`, `category_id`, `fiscal_period_id`, `only_active` and `columns` (and `type` for orders and order categories) as they are. Every other condition becomes an equality filter in CashCtrl's filter JSON. Pass `filter:` with a hash for more equality filters or with an array of raw CashCtrl filters:
136
+
137
+ ```ruby
138
+ CashCtrl::Person.where(filter: [{ comparison: "like", field: "firstName", value: "Ad" }])
139
+ ```
140
+
141
+ **Finding records:**
142
+
143
+ ```ruby
144
+ CashCtrl::Account.find(1) # raises CashCtrl::NotFoundError when missing
145
+ CashCtrl::Account.find(1, 2, 3) # => [account, account, account]
146
+ CashCtrl::Person.find_by(last_name: "Lovelace")
147
+ CashCtrl::Person.find_by!(last_name: "Lovelace")
148
+ ```
149
+
150
+ **Calculations and batches:**
151
+
152
+ ```ruby
153
+ CashCtrl::Account.first
154
+ CashCtrl::Account.first(5)
155
+ CashCtrl::Account.last
156
+ CashCtrl::Journal.where(fiscal_period_id: 3).count # uses the total from list.json
157
+ CashCtrl::Person.exists?(last_name: "Lovelace")
158
+ CashCtrl::Account.pluck(:number, :name)
159
+
160
+ CashCtrl::Journal.find_each(batch_size: 200) { |entry| puts entry.title }
161
+ CashCtrl::Journal.in_batches(of: 200) { |entries| puts entries.size }
162
+ ```
163
+
164
+ The same methods work on client accessors: `client.accounts.where(category_id: 1).first`.
165
+
166
+ `list` still sends a single `list.json` request and returns a `CashCtrl::Collection` with `total`, `summary` and `properties`.
167
+
168
+ ### Core Resources
169
+
170
+ #### Accounts
171
+
172
+ **List accounts:**
173
+
174
+ ```ruby
175
+ CashCtrl::Account.list
176
+ ```
177
+
178
+ **Find account by number:**
179
+
180
+ ```ruby
181
+ CashCtrl::Account.find_by_number("1000")
182
+ ```
183
+
184
+ **Get account balance:**
185
+
186
+ ```ruby
187
+ CashCtrl::Account.balance(id: 1, date: "2024-12-31")
188
+ ```
189
+
190
+ **Account categories:**
191
+
192
+ ```ruby
193
+ CashCtrl::Account::Category.list
194
+ CashCtrl::Account::Category.tree
195
+ CashCtrl::Account::Category.tree(4) # the tree below category 4
196
+ ```
197
+
198
+ `tree` returns a collection of typed records. Each node has `children`, which are typed records as well:
199
+
200
+ ```ruby
201
+ CashCtrl::Account::Category.tree.each do |category|
202
+ puts category.name
203
+ category.children.each { |child| puts " #{child.name}" }
204
+ end
205
+ ```
206
+
207
+ **Bank accounts:**
208
+
209
+ ```ruby
210
+ CashCtrl::Account::Bank.list
211
+ CashCtrl::Account::Bank.create(name: "Main Bank", account_id: 1)
212
+ ```
213
+
214
+ **Cost centers:**
215
+
216
+ ```ruby
217
+ CashCtrl::Account::CostCenter.list
218
+ CashCtrl::Account::CostCenter.balance(id: 1)
219
+ ```
220
+
221
+ #### Persons (Contacts)
222
+
223
+ ```ruby
224
+ CashCtrl::Person.list
225
+ CashCtrl::Person.find(1)
226
+ CashCtrl::Person.create(first_name: "John", last_name: "Doe", is_customer: true)
227
+ CashCtrl::Person.categorize(ids: [1, 2], target: 5)
228
+ ```
229
+
230
+ **Person categories:**
231
+
232
+ ```ruby
233
+ CashCtrl::Person::Category.list
234
+ CashCtrl::Person::Category.tree
235
+ ```
236
+
237
+ **Import people from a vCard, Excel or CSV file:**
238
+
239
+ ```ruby
240
+ file_id = CashCtrl::File.upload("contacts.vcf")
241
+ import_id = CashCtrl::Person::Import.create(file_id: file_id)["insertId"]
242
+ CashCtrl::Person::Import.preview(id: import_id)
243
+ CashCtrl::Person::Import.execute(id: import_id)
244
+ ```
245
+
246
+ Excel and CSV imports need a column mapping first (`mapping(id:, mapping: [{ from: "Name", to: "name" }])`, field names from `mapping_combo`). Articles work the same way through `CashCtrl::Inventory::Article::Import`.
247
+
248
+ **Titles (Mr., Mrs., etc.):**
249
+
250
+ ```ruby
251
+ CashCtrl::Person::Title.list
252
+ ```
253
+
254
+ #### Orders (Invoices, Quotes, etc.)
255
+
256
+ ```ruby
257
+ CashCtrl::Order.list
258
+ CashCtrl::Order.find(1)
259
+ CashCtrl::Order.create(
260
+ associate_id: 1,
261
+ category_id: 1,
262
+ items: [{ accountId: 100, name: "Service", unitPrice: 100 }]
263
+ )
264
+ CashCtrl::Order.update_status(ids: [1, 2], status_id: 16)
265
+ CashCtrl::Order.continue(ids: [1], category_id: 4, date: "2028-01-01")
266
+ CashCtrl::Order.dossier(id: 1)
267
+ CashCtrl::Order.dossier_add(group_id: 7, ids: [10, 20])
268
+ CashCtrl::Order.update_attachments(id: 1, file_ids: [5, 6])
269
+ ```
270
+
271
+ **Order documents (PDF generation):**
272
+
273
+ ```ruby
274
+ CashCtrl::Order::Document.find(1)
275
+ CashCtrl::Order::Document.read_pdf(ids: [1, 2])
276
+ CashCtrl::Order::Document.read_zip(ids: [1, 2])
277
+ CashCtrl::Order::Document.mail(
278
+ order_ids: [1],
279
+ mail_from: "billing@example.com",
280
+ mail_to: "client@example.com",
281
+ mail_subject: "Your invoice",
282
+ mail_text: "Please find your invoice attached."
283
+ )
284
+ ```
285
+
286
+ **Order payment files (pain.001):**
287
+
288
+ ```ruby
289
+ CashCtrl::Order::Payment.download(order_ids: [1, 2], date: "2028-01-31")
290
+ ```
291
+
292
+ **Order categories:**
293
+
294
+ ```ruby
295
+ CashCtrl::Order::Category.list
296
+ ```
297
+
298
+ #### Journal Entries
299
+
300
+ ```ruby
301
+ CashCtrl::Journal.list
302
+ CashCtrl::Journal.find(1)
303
+ ```
304
+
305
+ **Create with account IDs:**
306
+
307
+ ```ruby
308
+ CashCtrl::Journal.create(
309
+ debit_id: 1,
310
+ credit_id: 2,
311
+ amount: 100.00,
312
+ date_added: "2024-01-15",
313
+ title: "Payment received"
314
+ )
315
+ ```
316
+
317
+ **Create with account numbers:**
318
+
319
+ ```ruby
320
+ CashCtrl::Journal.create(
321
+ debit: "1000",
322
+ credit: "2000",
323
+ amount: 100.00,
324
+ date_added: "2024-01-15",
325
+ title: "Payment received"
326
+ )
327
+ ```
328
+
329
+ **Create with Account objects:**
330
+
331
+ ```ruby
332
+ cash_account = CashCtrl::Account.find_by_number("1000")
333
+ bank_account = CashCtrl::Account.find_by_number("1020")
334
+
335
+ CashCtrl::Journal.create(
336
+ debit: cash_account,
337
+ credit: bank_account,
338
+ amount: 500.00,
339
+ date_added: "2024-01-15",
340
+ title: "Cash deposit"
341
+ )
342
+ ```
343
+
344
+ #### Currencies
345
+
346
+ ```ruby
347
+ CashCtrl::Currency.list
348
+ CashCtrl::Currency.find(1)
349
+ CashCtrl::Currency.exchange_rate(from: "CHF", to: "EUR", date: "2024-01-15")
350
+ ```
351
+
352
+ #### Tax Rates
353
+
354
+ ```ruby
355
+ CashCtrl::Tax.list
356
+ CashCtrl::Tax.find(1)
357
+ CashCtrl::Tax.create(name: "VAT 8.1%", percentage: 8.1)
358
+ ```
359
+
360
+ #### Fiscal Periods
361
+
362
+ ```ruby
363
+ CashCtrl::FiscalPeriod.list
364
+ CashCtrl::FiscalPeriod.current
365
+ CashCtrl::FiscalPeriod.result(id: 1)
366
+ CashCtrl::FiscalPeriod.switch(id: 2)
367
+ CashCtrl::FiscalPeriod.complete(id: 1)
368
+ CashCtrl::FiscalPeriod.book_depreciations(id: 1, depreciation: [40, 41])
369
+ CashCtrl::FiscalPeriod.book_exchangediff(id: 1, exchange_diff: [{ accountId: 12, currencyRate: 0.95 }])
370
+ CashCtrl::FiscalPeriod.complete_months(id: 1, months: ["2028-01", "2028-02"])
371
+ ```
372
+
373
+ **Get journals for the current fiscal period**
374
+
375
+ ```ruby
376
+ period = CashCtrl::FiscalPeriod.current
377
+ journals = CashCtrl::Journal.where(fiscal_period_id: period.id)
378
+ ```
379
+
380
+ #### Reports
381
+
382
+ ```ruby
383
+ CashCtrl::Report.tree
384
+ ```
385
+
386
+ **Report collections:**
387
+
388
+ ```ruby
389
+ CashCtrl::Report::Collection.find(1)
390
+ CashCtrl::Report::Collection.meta(id: 1, fiscal_period: 25)
391
+ CashCtrl::Report::Collection.download_pdf(id: 1, start_date: "2028-01-01", end_date: "2028-06-30")
392
+ CashCtrl::Report::Collection.download_annual_report(fiscal_period_id: 25)
393
+ ```
394
+
395
+ **Report elements:**
396
+
397
+ ```ruby
398
+ CashCtrl::Report::Element.find(1)
399
+ CashCtrl::Report::Element.data(id: 1, fiscal_period: 25)
400
+ CashCtrl::Report::Element.download_xlsx(id: 1)
401
+ CashCtrl::Report::Element.download_xml(id: 1)
402
+ ```
403
+
404
+ The report methods take `start_date`, `end_date`, `fiscal_period`, `language` and `sort`.
405
+
406
+ #### Inventory
407
+
408
+ **Articles:**
409
+
410
+ ```ruby
411
+ CashCtrl::Inventory::Article.list
412
+ CashCtrl::Inventory::Article.create(name: "Product", sales_price: 50.00)
413
+ ```
414
+
415
+ **Fixed assets:**
416
+
417
+ ```ruby
418
+ CashCtrl::Inventory::Asset.list
419
+ ```
420
+
421
+ **Units:**
422
+
423
+ ```ruby
424
+ CashCtrl::Inventory::Unit.list
425
+ ```
426
+
427
+ #### Salary
428
+
429
+ ```ruby
430
+ CashCtrl::Salary::Statement.calculate(person_id: 5, template_id: 2, date: "2028-01-31")
431
+ CashCtrl::Salary::Statement.update_status(ids: [1, 2], status_id: 3)
432
+ CashCtrl::Salary::Document.read_pdf(ids: [1, 2])
433
+ CashCtrl::Salary::Document.mail(
434
+ statement_ids: [1, 2],
435
+ mail_from: "hr@example.com",
436
+ mail_subject: "Your payslip",
437
+ mail_text: "Please find your payslip attached."
438
+ )
439
+ CashCtrl::Salary::Certificate::Document.read_pdf(ids: [7])
440
+ CashCtrl::Salary::Payment.download(statement_ids: [1, 2], date: "2028-01-25")
441
+ ```
442
+
443
+ #### Organization
444
+
445
+ ```ruby
446
+ logo = CashCtrl::Organization.logo # PNG, GIF or JPEG bytes
447
+ ```
448
+
449
+ #### Files
450
+
451
+ ```ruby
452
+ client.files.list
453
+ client.files.get(id: 1)
454
+
455
+ file_id = client.files.upload("receipts/2026-11-invoice.pdf")
456
+ file_id = client.files.upload(io, name: "receipt.jpg", category_id: 3)
457
+
458
+ client.journals.update_attachments(id: 123, file_ids: [file_id])
459
+ ```
460
+
461
+ `upload` runs CashCtrl's three upload steps (prepare, PUT to the write URL, persist) and returns the new file ID. `update_attachments` replaces all attachments of the entry. Use `add_attachments` to keep the existing ones:
462
+
463
+ ```ruby
464
+ CashCtrl::Journal.add_attachments(id: 123, file_ids: [file_id])
465
+ CashCtrl::Journal.attach(123, "receipts/2026-11-invoice.pdf")
466
+ ```
467
+
468
+ ### Typed Models & Collections
469
+
470
+ API responses are automatically converted to typed Ruby objects.
471
+
472
+ **List returns a Collection:**
473
+
474
+ ```ruby
475
+ accounts = CashCtrl::Account.list
476
+ accounts.class
477
+ accounts.total
478
+ accounts.each { |a| puts a.name }
479
+ ```
480
+
481
+ **Individual records are typed models:**
482
+
483
+ ```ruby
484
+ account = accounts.first
485
+ account.class
486
+ account.id
487
+ account.name
488
+ account.number
489
+ ```
490
+
491
+ **Read also returns typed models:**
492
+
493
+ ```ruby
494
+ account = CashCtrl::Account.find(1)
495
+ account.class
496
+ ```
497
+
498
+ #### Type Coercion
499
+
500
+ Fields are automatically converted to appropriate Ruby types.
501
+
502
+ **DateTime fields:**
503
+
504
+ ```ruby
505
+ account = CashCtrl::Account.find(1)
506
+ account.created
507
+ account.last_updated
508
+ ```
509
+
510
+ **BigDecimal for monetary values:**
511
+
512
+ ```ruby
513
+ account.opening_amount
514
+ account.end_amount
515
+ ```
516
+
517
+ **Booleans:**
518
+
519
+ ```ruby
520
+ account.is_inactive
521
+ ```
522
+
523
+ **Date fields:**
524
+
525
+ ```ruby
526
+ person = CashCtrl::Person.find(1)
527
+ person.date_birth
528
+ ```
529
+
530
+ #### Accessing Raw API Response
531
+
532
+ The original record data is always available, and records returned from `create`, `update` and `save` also keep the raw API response:
533
+
534
+ ```ruby
535
+ account = CashCtrl::Account.find(1)
536
+ account.raw_data # => { "id" => 1, "number" => "1000", ... }
537
+
538
+ account = CashCtrl::Account.create(number: "1000", name: "Cash", category_id: 1)
539
+ account.response # => { "success" => true, "insertId" => 1, "message" => "..." }
540
+ ```
541
+
542
+ ### Working with Records
543
+
544
+ Records know the resource and client that loaded them, so they can save, update, destroy and reload themselves.
545
+
546
+ **Build and save a new record:**
547
+
548
+ ```ruby
549
+ person = CashCtrl::Person.new(first_name: "Ada", last_name: "Lovelace")
550
+ person.new_record? # => true
551
+ person.save # => true
552
+ person.id
553
+ person.persisted? # => true
554
+ ```
555
+
556
+ **Change a loaded record:**
557
+
558
+ ```ruby
559
+ person = CashCtrl::Person.find(1)
560
+ person.first_name = "Grace"
561
+ person.save
562
+
563
+ person.update(last_name: "Hopper")
564
+ person.reload
565
+ person.destroy
566
+ person.destroyed? # => true
567
+ ```
568
+
569
+ `client.people.build(first_name: "Ada")` builds a record bound to a specific client.
570
+
571
+ **Attributes and dirty tracking:**
572
+
573
+ ```ruby
574
+ person.assign_attributes(first_name: "Grace", notes: "Admiral")
575
+ person.attributes # => { "id" => 1, "first_name" => "Grace", ... }
576
+ person.changed? # => true
577
+ person.changed # => ["first_name", "notes"]
578
+ person.changes # => { "first_name" => ["Ada", "Grace"], "notes" => [nil, "Admiral"] }
579
+ person.first_name_changed? # => true
580
+ person.first_name_was # => "Ada"
581
+ person.save
582
+ person.saved_changes
583
+ ```
584
+
585
+ **Validation errors:**
586
+
587
+ `save` and `update` return `false` when CashCtrl rejects the record and fill `errors` from CashCtrl's error list. `save!` and `update!` raise `CashCtrl::ValidationError` instead.
588
+
589
+ ```ruby
590
+ person = CashCtrl::Person.new
591
+ person.save # => false
592
+ person.errors[:first_name] # => ["This field cannot be empty."]
593
+ person.errors.full_messages # => ["First name This field cannot be empty."]
594
+
595
+ begin
596
+ person.save!
597
+ rescue CashCtrl::ValidationError => e
598
+ e.record
599
+ e.errors
600
+ end
601
+ ```
602
+
603
+ **Attachments:**
604
+
605
+ Records of resources with attachments (accounts, banks, cost centers, articles, assets, journal entries, orders, people and salary statements) can attach files. Paths and IO objects are uploaded first; file IDs and `CashCtrl::Models::File` records are attached as they are. Existing attachments are kept.
606
+
607
+ ```ruby
608
+ journal = CashCtrl::Journal.find(123)
609
+ journal.attach("receipts/2026-11-invoice.pdf", 42)
610
+ ```
611
+
612
+ ### Associations
613
+
614
+ Foreign keys have readers that load the associated record on first access, from the same client, and keep it until the key changes or the record is reloaded. An empty or missing key returns `nil`.
615
+
616
+ ```ruby
617
+ journal = CashCtrl::Journal.find(9)
618
+ journal.debit # => #<CashCtrl::Models::Account number: "6500", ...>
619
+ journal.credit
620
+ journal.associate # => the person, or nil
621
+ journal.currency
622
+ journal.tax
623
+
624
+ order = CashCtrl::Order.find(3)
625
+ order.associate
626
+ order.category # bound to CashCtrl::Order::Category
627
+
628
+ person = CashCtrl::Person.find(7)
629
+ person.category # bound to CashCtrl::Person::Category
630
+ person.superior
631
+ ```
632
+
633
+ Categories are bound to the category resource of their record, so `person.category.children` and `person.category.save` use the person categories.
634
+
635
+ **Assigning records:**
636
+
637
+ ```ruby
638
+ journal.debit = CashCtrl::Account.find_by_number("6500") # sets debit_id
639
+ journal.save
640
+
641
+ CashCtrl::Order.create(associate: ada, category: invoices, date: "2028-01-15", items: [...])
642
+ CashCtrl::Order.where(associate: ada)
643
+ ```
644
+
645
+ **Nested records:**
646
+
647
+ Journal and order items and attachments are typed records with their own associations:
648
+
649
+ ```ruby
650
+ journal.items.each do |item|
651
+ puts "#{item.account.number} #{item.debit} #{item.credit} #{item.description}"
652
+ end
653
+
654
+ order.items.first.unit_price
655
+ order.items.first.account
656
+ order.items.first.unit
657
+
658
+ journal.attachments.first.name
659
+ journal.attachments.first.file # => the CashCtrl::Models::File
660
+ ```
661
+
662
+ **Has many:**
663
+
664
+ ```ruby
665
+ person.orders # => CashCtrl::Order.where(associate_id: person.id)
666
+ person.orders.where(category: invoices).count
667
+ person.journals.where(fiscal_period_id: 25)
668
+ ```
669
+
670
+ **Avoiding a request per record:**
671
+
672
+ `includes` loads the associations of every record in the relation up front. Accounts, taxes, currencies, units and categories come from a single list request; other records are loaded once per distinct id.
673
+
674
+ ```ruby
675
+ CashCtrl::Journal.where(fiscal_period_id: 25).includes(:debit, :credit).each do |journal|
676
+ puts "#{journal.debit.number} -> #{journal.credit.number} #{journal.amount}"
677
+ end
678
+
679
+ CashCtrl::Order.includes(:associate, :category).limit(20)
680
+ ```
681
+
682
+ Updating a record resends its items as CashCtrl returned them. To replace them, assign new items as hashes or `CashCtrl::Models::JournalItem` / `CashCtrl::Models::OrderItem` records.
683
+
684
+ ### Localized Strings (i18n)
685
+
686
+ CashCtrl returns multi-language strings in XML format. These are automatically parsed into `LocalizedString` objects.
687
+
688
+ **Localized fields return LocalizedString objects:**
689
+
690
+ ```ruby
691
+ account = CashCtrl::Account.find(1)
692
+ account.category_display.class
693
+ ```
694
+
695
+ **Access specific languages:**
696
+
697
+ ```ruby
698
+ account.category_display.de
699
+ account.category_display.en
700
+ account.category_display.fr
701
+ account.category_display.it
702
+ ```
703
+
704
+ **`to_s` returns the value for the current locale:**
705
+
706
+ ```ruby
707
+ account.category_display.to_s
708
+ ```
709
+
710
+ **Get all translations as a hash:**
711
+
712
+ ```ruby
713
+ account.category_display.to_h
714
+ ```
715
+
716
+ The locale is determined by (in order of priority):
717
+ 1. `I18n.locale` if I18n is available
718
+ 2. `CashCtrl.configuration.language`
719
+ 3. Falls back to `"en"`
720
+
721
+ ### Export Formats
722
+
723
+ Many list endpoints support CSV, Excel, and PDF exports:
724
+
725
+ ```ruby
726
+ client.accounts.list_csv
727
+ client.accounts.list_xlsx
728
+ client.accounts.list_pdf
729
+ ```
730
+
731
+ ### Error Handling
732
+
733
+ ```ruby
734
+ begin
735
+ CashCtrl::Account.find(999999)
736
+ rescue CashCtrl::AuthenticationError => e
737
+ puts "Invalid API key"
738
+ rescue CashCtrl::NotFoundError => e
739
+ puts "Resource not found"
740
+ rescue CashCtrl::ValidationError => e
741
+ puts "Validation failed"
742
+ puts e.errors
743
+ rescue CashCtrl::RateLimitError => e
744
+ puts "Too many requests"
745
+ rescue CashCtrl::ServerError => e
746
+ puts "Server error (5xx)"
747
+ rescue CashCtrl::Error => e
748
+ puts "Generic error"
749
+ end
750
+ ```
751
+
752
+ ### All Available Resources
753
+
754
+ | Resource | Class |
755
+ |----------|-------|
756
+ | Accounts | `CashCtrl::Account` |
757
+ | Account Banks | `CashCtrl::Account::Bank` |
758
+ | Account Categories | `CashCtrl::Account::Category` |
759
+ | Cost Centers | `CashCtrl::Account::CostCenter` |
760
+ | Currencies | `CashCtrl::Currency` |
761
+ | Custom Fields | `CashCtrl::CustomField` |
762
+ | Files | `CashCtrl::File` |
763
+ | Fiscal Periods | `CashCtrl::FiscalPeriod` |
764
+ | History | `CashCtrl::History` |
765
+ | Inventory Articles | `CashCtrl::Inventory::Article` |
766
+ | Inventory Assets | `CashCtrl::Inventory::Asset` |
767
+ | Inventory Units | `CashCtrl::Inventory::Unit` |
768
+ | Journal | `CashCtrl::Journal` |
769
+ | Locations | `CashCtrl::Location` |
770
+ | Orders | `CashCtrl::Order` |
771
+ | Organization (logo) | `CashCtrl::Organization` |
772
+ | Order Categories | `CashCtrl::Order::Category` |
773
+ | Order Documents | `CashCtrl::Order::Document` |
774
+ | Persons | `CashCtrl::Person` |
775
+ | Person Categories | `CashCtrl::Person::Category` |
776
+ | Person Titles | `CashCtrl::Person::Title` |
777
+ | Reports | `CashCtrl::Report` |
778
+ | Report Collections | `CashCtrl::Report::Collection` |
779
+ | Report Elements | `CashCtrl::Report::Element` |
780
+ | Roundings | `CashCtrl::Rounding` |
781
+ | Salary (all sub-resources) | `CashCtrl::Salary::*` |
782
+ | Sequence Numbers | `CashCtrl::SequenceNumber` |
783
+ | Settings | `CashCtrl::Setting` |
784
+ | Tax Rates | `CashCtrl::Tax` |
785
+ | Text Templates | `CashCtrl::Text` |
786
+
787
+ ## API Documentation
788
+
789
+ For full API documentation, see the [CashCtrl API Reference](https://app.cashctrl.com/static/help/en/api/index.html).
26
790
 
27
791
  ## Development
28
792
 
29
793
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
794
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
795
+ To install this gem onto your local machine, run `bundle exec rake install`.
796
+
797
+ ### Model Generator
798
+
799
+ Model classes are auto-generated from `lib/cashctrl/schema.yml`. The schema defines attributes and their types for each resource.
800
+
801
+ **Schema format:**
802
+
803
+ ```yaml
804
+ account:
805
+ attributes:
806
+ id:
807
+ type: integer
808
+ readonly: true
809
+ number:
810
+ type: string
811
+ name:
812
+ type: string
813
+ opening_amount:
814
+ type: decimal
815
+ created:
816
+ type: datetime
817
+ readonly: true
818
+ is_inactive:
819
+ type: boolean
820
+ belongs_to:
821
+ category: { resource: Account::Category }
822
+ nested:
823
+ attachments: { model: attachment }
824
+
825
+ person:
826
+ has_many:
827
+ orders: { resource: Order, foreign_key: associate_id }
828
+ ```
829
+
830
+ `belongs_to` takes the resource under `CashCtrl::Resources` and uses `<name>_id` as the foreign key. `has_many` names the resource and the foreign key on its records. `nested` types an array attribute, such as journal `items`, with another model from the schema.
831
+
832
+ **Supported types:**
833
+
834
+ - `string` - Plain strings (auto-detects localized XML strings)
835
+ - `integer` - Integer values
836
+ - `decimal` - BigDecimal for monetary values
837
+ - `boolean` - Boolean values
838
+ - `date` - Date objects
839
+ - `datetime` - DateTime objects
840
+ - `json` - JSON parsed objects
841
+
842
+ **Regenerate models after schema changes:**
843
+
844
+ ```bash
845
+ ruby lib/generators/model_generator.rb
846
+ ```
847
+
848
+ This generates model classes in `lib/cashctrl/models/` with proper type coercion.
32
849
 
33
850
  ## Contributing
34
851
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/cashctrl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/marcoroth/cashctrl/blob/main/CODE_OF_CONDUCT.md).
852
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/cashctrl-ruby.
36
853
 
37
- ## Code of Conduct
854
+ ## License
38
855
 
39
- Everyone interacting in the Cashctrl project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/marcoroth/cashctrl/blob/main/CODE_OF_CONDUCT.md).
856
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).