cashctrl 0.1.0 → 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 (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +418 -136
  3. data/cashctrl.gemspec +1 -1
  4. data/lib/cashctrl/client.rb +106 -103
  5. data/lib/cashctrl/collection.rb +6 -5
  6. data/lib/cashctrl/configuration.rb +1 -1
  7. data/lib/cashctrl/error.rb +3 -1
  8. data/lib/cashctrl/errors.rb +76 -0
  9. data/lib/cashctrl/localized_string.rb +2 -2
  10. data/lib/cashctrl/models/account.rb +5 -1
  11. data/lib/cashctrl/models/article.rb +9 -1
  12. data/lib/cashctrl/models/asset.rb +6 -1
  13. data/lib/cashctrl/models/attachment.rb +14 -0
  14. data/lib/cashctrl/models/bank.rb +3 -1
  15. data/lib/cashctrl/models/category.rb +1 -1
  16. data/lib/cashctrl/models/certificate_document.rb +1 -1
  17. data/lib/cashctrl/models/certificate_template.rb +1 -1
  18. data/lib/cashctrl/models/cost_center.rb +3 -1
  19. data/lib/cashctrl/models/currency.rb +1 -1
  20. data/lib/cashctrl/models/custom_field.rb +1 -1
  21. data/lib/cashctrl/models/custom_field_group.rb +1 -1
  22. data/lib/cashctrl/models/file.rb +2 -1
  23. data/lib/cashctrl/models/fiscal_period.rb +1 -1
  24. data/lib/cashctrl/models/fiscal_period_task.rb +1 -1
  25. data/lib/cashctrl/models/history.rb +1 -1
  26. data/lib/cashctrl/models/insurance_type.rb +1 -1
  27. data/lib/cashctrl/models/journal.rb +10 -1
  28. data/lib/cashctrl/models/journal_import.rb +1 -1
  29. data/lib/cashctrl/models/journal_import_entry.rb +1 -1
  30. data/lib/cashctrl/models/journal_item.rb +20 -0
  31. data/lib/cashctrl/models/loader.rb +3 -0
  32. data/lib/cashctrl/models/location.rb +1 -1
  33. data/lib/cashctrl/models/order.rb +8 -1
  34. data/lib/cashctrl/models/order_book_entry.rb +1 -1
  35. data/lib/cashctrl/models/order_document.rb +1 -1
  36. data/lib/cashctrl/models/order_item.rb +23 -0
  37. data/lib/cashctrl/models/order_layout.rb +1 -1
  38. data/lib/cashctrl/models/order_payment.rb +1 -1
  39. data/lib/cashctrl/models/person.rb +7 -1
  40. data/lib/cashctrl/models/report.rb +1 -1
  41. data/lib/cashctrl/models/report_collection.rb +1 -1
  42. data/lib/cashctrl/models/report_element.rb +1 -1
  43. data/lib/cashctrl/models/rounding.rb +2 -1
  44. data/lib/cashctrl/models/salary_book_entry.rb +1 -1
  45. data/lib/cashctrl/models/salary_certificate.rb +2 -1
  46. data/lib/cashctrl/models/salary_document.rb +1 -1
  47. data/lib/cashctrl/models/salary_field.rb +1 -1
  48. data/lib/cashctrl/models/salary_layout.rb +1 -1
  49. data/lib/cashctrl/models/salary_payment.rb +1 -1
  50. data/lib/cashctrl/models/salary_setting.rb +1 -1
  51. data/lib/cashctrl/models/salary_statement.rb +3 -1
  52. data/lib/cashctrl/models/salary_status.rb +1 -1
  53. data/lib/cashctrl/models/salary_sum.rb +1 -1
  54. data/lib/cashctrl/models/salary_template.rb +1 -1
  55. data/lib/cashctrl/models/salary_type.rb +1 -1
  56. data/lib/cashctrl/models/sequence_number.rb +1 -1
  57. data/lib/cashctrl/models/setting.rb +1 -1
  58. data/lib/cashctrl/models/tax.rb +2 -1
  59. data/lib/cashctrl/models/text.rb +1 -1
  60. data/lib/cashctrl/models/title.rb +1 -1
  61. data/lib/cashctrl/models/unit.rb +1 -1
  62. data/lib/cashctrl/relation.rb +271 -0
  63. data/lib/cashctrl/resource.rb +300 -57
  64. data/lib/cashctrl/resources/account/bank.rb +3 -13
  65. data/lib/cashctrl/resources/account/category.rb +3 -9
  66. data/lib/cashctrl/resources/account/cost_center/category.rb +3 -9
  67. data/lib/cashctrl/resources/account/cost_center.rb +3 -25
  68. data/lib/cashctrl/resources/account.rb +4 -30
  69. data/lib/cashctrl/resources/attachments.rb +32 -0
  70. data/lib/cashctrl/resources/base.rb +160 -84
  71. data/lib/cashctrl/resources/currency.rb +1 -10
  72. data/lib/cashctrl/resources/custom_field/group.rb +3 -11
  73. data/lib/cashctrl/resources/custom_field.rb +3 -15
  74. data/lib/cashctrl/resources/file/category.rb +4 -10
  75. data/lib/cashctrl/resources/file.rb +44 -28
  76. data/lib/cashctrl/resources/fiscal_period/task.rb +2 -12
  77. data/lib/cashctrl/resources/fiscal_period.rb +17 -59
  78. data/lib/cashctrl/resources/history.rb +3 -21
  79. data/lib/cashctrl/resources/import_session.rb +43 -0
  80. data/lib/cashctrl/resources/inventory/article/category.rb +3 -9
  81. data/lib/cashctrl/resources/inventory/article/import.rb +13 -0
  82. data/lib/cashctrl/resources/inventory/article.rb +3 -18
  83. data/lib/cashctrl/resources/inventory/asset/category.rb +3 -9
  84. data/lib/cashctrl/resources/inventory/asset.rb +3 -18
  85. data/lib/cashctrl/resources/inventory/unit.rb +1 -1
  86. data/lib/cashctrl/resources/journal/import/entry.rb +1 -22
  87. data/lib/cashctrl/resources/journal/import.rb +1 -7
  88. data/lib/cashctrl/resources/journal.rb +19 -57
  89. data/lib/cashctrl/resources/location.rb +1 -1
  90. data/lib/cashctrl/resources/order/book_entry.rb +1 -1
  91. data/lib/cashctrl/resources/order/category.rb +4 -15
  92. data/lib/cashctrl/resources/order/document.rb +13 -21
  93. data/lib/cashctrl/resources/order/layout.rb +1 -1
  94. data/lib/cashctrl/resources/order/payment.rb +9 -29
  95. data/lib/cashctrl/resources/order.rb +21 -68
  96. data/lib/cashctrl/resources/organization.rb +13 -0
  97. data/lib/cashctrl/resources/person/category.rb +3 -9
  98. data/lib/cashctrl/resources/person/import.rb +11 -0
  99. data/lib/cashctrl/resources/person/title.rb +1 -1
  100. data/lib/cashctrl/resources/person.rb +4 -23
  101. data/lib/cashctrl/resources/report/collection.rb +10 -42
  102. data/lib/cashctrl/resources/report/element.rb +14 -46
  103. data/lib/cashctrl/resources/report.rb +4 -30
  104. data/lib/cashctrl/resources/rounding.rb +1 -1
  105. data/lib/cashctrl/resources/salary/book_entry.rb +1 -1
  106. data/lib/cashctrl/resources/salary/category.rb +3 -9
  107. data/lib/cashctrl/resources/salary/certificate/document.rb +14 -39
  108. data/lib/cashctrl/resources/salary/certificate/template.rb +2 -8
  109. data/lib/cashctrl/resources/salary/certificate.rb +2 -12
  110. data/lib/cashctrl/resources/salary/document.rb +14 -34
  111. data/lib/cashctrl/resources/salary/field.rb +3 -17
  112. data/lib/cashctrl/resources/salary/insurance/type.rb +1 -1
  113. data/lib/cashctrl/resources/salary/layout.rb +1 -1
  114. data/lib/cashctrl/resources/salary/payment.rb +9 -29
  115. data/lib/cashctrl/resources/salary/setting.rb +1 -1
  116. data/lib/cashctrl/resources/salary/statement.rb +19 -54
  117. data/lib/cashctrl/resources/salary/status.rb +3 -11
  118. data/lib/cashctrl/resources/salary/sum.rb +1 -1
  119. data/lib/cashctrl/resources/salary/template.rb +2 -8
  120. data/lib/cashctrl/resources/salary/type.rb +1 -8
  121. data/lib/cashctrl/resources/sequence_number.rb +1 -7
  122. data/lib/cashctrl/resources/setting.rb +6 -24
  123. data/lib/cashctrl/resources/tax.rb +1 -1
  124. data/lib/cashctrl/resources/text.rb +1 -1
  125. data/lib/cashctrl/schema.yml +111 -0
  126. data/lib/cashctrl/version.rb +2 -2
  127. data/lib/cashctrl.rb +35 -25
  128. data/lib/generators/model_generator.rb +19 -7
  129. data/sig/cashctrl.rbs +1 -1
  130. metadata +12 -2
data/README.md CHANGED
@@ -27,7 +27,7 @@ gem install cashctrl
27
27
  ### Global Configuration
28
28
 
29
29
  ```ruby
30
- Cashctrl.configure do |config|
30
+ CashCtrl.configure do |config|
31
31
  config.org = "your-organization"
32
32
  config.api_key = ENV["CASHCTRL_API_KEY"]
33
33
  config.language = "en"
@@ -43,7 +43,7 @@ end
43
43
  For multi-tenant applications or testing:
44
44
 
45
45
  ```ruby
46
- client = Cashctrl::Client.new(
46
+ client = CashCtrl::Client.new(
47
47
  org: "other-organization",
48
48
  api_key: "different-api-key",
49
49
  language: "de"
@@ -61,175 +61,255 @@ The gem supports two usage patterns:
61
61
  **List all accounts:**
62
62
 
63
63
  ```ruby
64
- Cashctrl::Account.list
64
+ CashCtrl::Account.list
65
65
  ```
66
66
 
67
- **Read a specific account:**
67
+ **Find a specific account:**
68
68
 
69
69
  ```ruby
70
- Cashctrl::Account.read(id: 1)
70
+ CashCtrl::Account.find(1)
71
71
  ```
72
72
 
73
73
  **Create an account:**
74
74
 
75
75
  ```ruby
76
- Cashctrl::Account.create(number: "1000", name: "Cash", categoryId: 1)
76
+ account = CashCtrl::Account.create(number: "1000", name: "Cash", category_id: 1)
77
+ account.persisted? # => true
78
+ account.id
77
79
  ```
78
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
+
79
83
  **Update an account:**
80
84
 
81
85
  ```ruby
82
- Cashctrl::Account.update(id: 1, name: "Updated Name")
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)
83
93
  ```
84
94
 
85
95
  **Delete accounts:**
86
96
 
87
97
  ```ruby
88
- Cashctrl::Account.delete(ids: [1, 2, 3])
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
89
103
  ```
90
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
+
91
109
  #### 2. Client Instance (custom configuration)
92
110
 
93
111
  ```ruby
94
- client = Cashctrl::Client.new(org: "myorg", api_key: "key")
112
+ client = CashCtrl::Client.new(org: "myorg", api_key: "key")
95
113
 
96
114
  client.accounts.list
97
- client.accounts.read(id: 1)
98
- client.accounts.create(number: "1000", name: "Cash", categoryId: 1)
99
- client.persons.list
115
+ client.accounts.find(1)
116
+ client.accounts.create(number: "1000", name: "Cash", category_id: 1)
117
+ client.people.list
100
118
  client.orders.list
119
+ client.salary_statements.list
101
120
  ```
102
121
 
103
- #### Convenience Methods
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
104
125
 
105
- All resources support these shortcuts and aliases:
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.
106
127
 
107
128
  ```ruby
108
- Cashctrl::Account.all # alias for .list
109
- Cashctrl::Account.find(1) # alias for .read(id: 1)
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
+ ```
110
134
 
111
- Cashctrl::Account.first # alias for .list.first
112
- Cashctrl::Account.last # alias for .list.last
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:
113
136
 
114
- client.accounts.all
115
- client.accounts.find(1)
116
- client.orders.first
137
+ ```ruby
138
+ CashCtrl::Person.where(filter: [{ comparison: "like", field: "firstName", value: "Ad" }])
117
139
  ```
118
140
 
119
- ### Core Resources
141
+ **Finding records:**
120
142
 
121
- #### Accounts
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
+ ```
122
149
 
123
- **List accounts:**
150
+ **Calculations and batches:**
124
151
 
125
152
  ```ruby
126
- Cashctrl::Account.list
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 }
127
162
  ```
128
163
 
129
- **Get first or last account:**
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:**
130
173
 
131
174
  ```ruby
132
- Cashctrl::Account.first
133
- Cashctrl::Account.last
175
+ CashCtrl::Account.list
134
176
  ```
135
177
 
136
178
  **Find account by number:**
137
179
 
138
180
  ```ruby
139
- Cashctrl::Account.find_by_number("1000")
181
+ CashCtrl::Account.find_by_number("1000")
140
182
  ```
141
183
 
142
184
  **Get account balance:**
143
185
 
144
186
  ```ruby
145
- Cashctrl::Account.balance(id: 1, date: "2024-12-31")
187
+ CashCtrl::Account.balance(id: 1, date: "2024-12-31")
146
188
  ```
147
189
 
148
190
  **Account categories:**
149
191
 
150
192
  ```ruby
151
- Cashctrl::Account::Category.list
152
- Cashctrl::Account::Category.tree
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
153
205
  ```
154
206
 
155
207
  **Bank accounts:**
156
208
 
157
209
  ```ruby
158
- Cashctrl::Account::Bank.list
159
- Cashctrl::Account::Bank.create(name: "Main Bank", accountId: 1)
210
+ CashCtrl::Account::Bank.list
211
+ CashCtrl::Account::Bank.create(name: "Main Bank", account_id: 1)
160
212
  ```
161
213
 
162
214
  **Cost centers:**
163
215
 
164
216
  ```ruby
165
- Cashctrl::Account::CostCenter.list
166
- Cashctrl::Account::CostCenter.balance(id: 1)
217
+ CashCtrl::Account::CostCenter.list
218
+ CashCtrl::Account::CostCenter.balance(id: 1)
167
219
  ```
168
220
 
169
221
  #### Persons (Contacts)
170
222
 
171
223
  ```ruby
172
- Cashctrl::Person.list
173
- Cashctrl::Person.read(id: 1)
174
- Cashctrl::Person.create(firstName: "John", lastName: "Doe", isCustomer: true)
175
- Cashctrl::Person.categorize(ids: [1, 2], target: 5)
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)
176
228
  ```
177
229
 
178
230
  **Person categories:**
179
231
 
180
232
  ```ruby
181
- Cashctrl::Person::Category.list
182
- Cashctrl::Person::Category.tree
233
+ CashCtrl::Person::Category.list
234
+ CashCtrl::Person::Category.tree
183
235
  ```
184
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
+
185
248
  **Titles (Mr., Mrs., etc.):**
186
249
 
187
250
  ```ruby
188
- Cashctrl::Person::Title.list
251
+ CashCtrl::Person::Title.list
189
252
  ```
190
253
 
191
254
  #### Orders (Invoices, Quotes, etc.)
192
255
 
193
256
  ```ruby
194
- Cashctrl::Order.list
195
- Cashctrl::Order.read(id: 1)
196
- Cashctrl::Order.create(
197
- associateId: 1,
198
- categoryId: 1,
199
- items: [{ accountId: 100, name: "Service", unitPrice: 100 }].to_json
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 }]
200
263
  )
201
- Cashctrl::Order.update_status(id: 1, status: "S")
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])
202
269
  ```
203
270
 
204
271
  **Order documents (PDF generation):**
205
272
 
206
273
  ```ruby
207
- Cashctrl::Order::Document.read(id: 1)
208
- Cashctrl::Order::Document.read_pdf(id: 1)
209
- Cashctrl::Order::Document.mail(id: 1, recipientEmail: "client@example.com")
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")
210
290
  ```
211
291
 
212
292
  **Order categories:**
213
293
 
214
294
  ```ruby
215
- Cashctrl::Order::Category.list
295
+ CashCtrl::Order::Category.list
216
296
  ```
217
297
 
218
298
  #### Journal Entries
219
299
 
220
300
  ```ruby
221
- Cashctrl::Journal.list
222
- Cashctrl::Journal.read(id: 1)
301
+ CashCtrl::Journal.list
302
+ CashCtrl::Journal.find(1)
223
303
  ```
224
304
 
225
305
  **Create with account IDs:**
226
306
 
227
307
  ```ruby
228
- Cashctrl::Journal.create(
229
- debitId: 1,
230
- creditId: 2,
308
+ CashCtrl::Journal.create(
309
+ debit_id: 1,
310
+ credit_id: 2,
231
311
  amount: 100.00,
232
- dateAdded: "2024-01-15",
312
+ date_added: "2024-01-15",
233
313
  title: "Payment received"
234
314
  )
235
315
  ```
@@ -237,11 +317,11 @@ Cashctrl::Journal.create(
237
317
  **Create with account numbers:**
238
318
 
239
319
  ```ruby
240
- Cashctrl::Journal.create(
320
+ CashCtrl::Journal.create(
241
321
  debit: "1000",
242
322
  credit: "2000",
243
323
  amount: 100.00,
244
- dateAdded: "2024-01-15",
324
+ date_added: "2024-01-15",
245
325
  title: "Payment received"
246
326
  )
247
327
  ```
@@ -249,14 +329,14 @@ Cashctrl::Journal.create(
249
329
  **Create with Account objects:**
250
330
 
251
331
  ```ruby
252
- cash_account = Cashctrl::Account.find_by_number("1000")
253
- bank_account = Cashctrl::Account.find_by_number("1020")
332
+ cash_account = CashCtrl::Account.find_by_number("1000")
333
+ bank_account = CashCtrl::Account.find_by_number("1020")
254
334
 
255
- Cashctrl::Journal.create(
335
+ CashCtrl::Journal.create(
256
336
  debit: cash_account,
257
337
  credit: bank_account,
258
338
  amount: 500.00,
259
- dateAdded: "2024-01-15",
339
+ date_added: "2024-01-15",
260
340
  title: "Cash deposit"
261
341
  )
262
342
  ```
@@ -264,70 +344,106 @@ Cashctrl::Journal.create(
264
344
  #### Currencies
265
345
 
266
346
  ```ruby
267
- Cashctrl::Currency.list
268
- Cashctrl::Currency.read(id: 1)
269
- Cashctrl::Currency.exchange_rate(from: "CHF", to: "EUR", date: "2024-01-15")
347
+ CashCtrl::Currency.list
348
+ CashCtrl::Currency.find(1)
349
+ CashCtrl::Currency.exchange_rate(from: "CHF", to: "EUR", date: "2024-01-15")
270
350
  ```
271
351
 
272
352
  #### Tax Rates
273
353
 
274
354
  ```ruby
275
- Cashctrl::Tax.list
276
- Cashctrl::Tax.read(id: 1)
277
- Cashctrl::Tax.create(name: "VAT 8.1%", percentage: 8.1)
355
+ CashCtrl::Tax.list
356
+ CashCtrl::Tax.find(1)
357
+ CashCtrl::Tax.create(name: "VAT 8.1%", percentage: 8.1)
278
358
  ```
279
359
 
280
360
  #### Fiscal Periods
281
361
 
282
362
  ```ruby
283
- Cashctrl::FiscalPeriod.list
284
- Cashctrl::FiscalPeriod.result(id: 1)
285
- Cashctrl::FiscalPeriod.switch(id: 2)
286
- Cashctrl::FiscalPeriod.complete(id: 1)
287
- Cashctrl::FiscalPeriod.book_depreciations(id: 1)
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)
288
378
  ```
289
379
 
290
380
  #### Reports
291
381
 
292
382
  ```ruby
293
- Cashctrl::Report.tree
383
+ CashCtrl::Report.tree
294
384
  ```
295
385
 
296
386
  **Report collections:**
297
387
 
298
388
  ```ruby
299
- Cashctrl::Report::Collection.read(id: 1)
300
- Cashctrl::Report::Collection.download_pdf(id: 1)
301
- Cashctrl::Report::Collection.download_annual_report(id: 1)
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)
302
393
  ```
303
394
 
304
395
  **Report elements:**
305
396
 
306
397
  ```ruby
307
- Cashctrl::Report::Element.read(id: 1)
308
- Cashctrl::Report::Element.data(id: 1)
309
- Cashctrl::Report::Element.download_xlsx(id: 1)
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)
310
402
  ```
311
403
 
404
+ The report methods take `start_date`, `end_date`, `fiscal_period`, `language` and `sort`.
405
+
312
406
  #### Inventory
313
407
 
314
408
  **Articles:**
315
409
 
316
410
  ```ruby
317
- Cashctrl::Resources::Inventory::Article.list
318
- Cashctrl::Resources::Inventory::Article.create(name: "Product", unitPrice: 50.00)
411
+ CashCtrl::Inventory::Article.list
412
+ CashCtrl::Inventory::Article.create(name: "Product", sales_price: 50.00)
319
413
  ```
320
414
 
321
415
  **Fixed assets:**
322
416
 
323
417
  ```ruby
324
- Cashctrl::Resources::Inventory::Asset.list
418
+ CashCtrl::Inventory::Asset.list
325
419
  ```
326
420
 
327
421
  **Units:**
328
422
 
329
423
  ```ruby
330
- Cashctrl::Resources::Inventory::Unit.list
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
331
447
  ```
332
448
 
333
449
  #### Files
@@ -335,8 +451,18 @@ Cashctrl::Resources::Inventory::Unit.list
335
451
  ```ruby
336
452
  client.files.list
337
453
  client.files.get(id: 1)
338
- client.files.prepare(mime_type: "application/pdf", name: "invoice.pdf")
339
- client.files.persist(ids: [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")
340
466
  ```
341
467
 
342
468
  ### Typed Models & Collections
@@ -346,7 +472,7 @@ API responses are automatically converted to typed Ruby objects.
346
472
  **List returns a Collection:**
347
473
 
348
474
  ```ruby
349
- accounts = Cashctrl::Account.list
475
+ accounts = CashCtrl::Account.list
350
476
  accounts.class
351
477
  accounts.total
352
478
  accounts.each { |a| puts a.name }
@@ -365,7 +491,7 @@ account.number
365
491
  **Read also returns typed models:**
366
492
 
367
493
  ```ruby
368
- account = Cashctrl::Account.read(id: 1)
494
+ account = CashCtrl::Account.find(1)
369
495
  account.class
370
496
  ```
371
497
 
@@ -376,7 +502,7 @@ Fields are automatically converted to appropriate Ruby types.
376
502
  **DateTime fields:**
377
503
 
378
504
  ```ruby
379
- account = Cashctrl::Account.read(id: 1)
505
+ account = CashCtrl::Account.find(1)
380
506
  account.created
381
507
  account.last_updated
382
508
  ```
@@ -397,19 +523,164 @@ account.is_inactive
397
523
  **Date fields:**
398
524
 
399
525
  ```ruby
400
- person = Cashctrl::Person.read(id: 1)
526
+ person = CashCtrl::Person.find(1)
401
527
  person.date_birth
402
528
  ```
403
529
 
404
530
  #### Accessing Raw API Response
405
531
 
406
- The original API response is always available:
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.
407
606
 
408
607
  ```ruby
409
- account = Cashctrl::Account.read(id: 1)
410
- account.raw_data
608
+ journal = CashCtrl::Journal.find(123)
609
+ journal.attach("receipts/2026-11-invoice.pdf", 42)
411
610
  ```
412
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
+
413
684
  ### Localized Strings (i18n)
414
685
 
415
686
  CashCtrl returns multi-language strings in XML format. These are automatically parsed into `LocalizedString` objects.
@@ -417,7 +688,7 @@ CashCtrl returns multi-language strings in XML format. These are automatically p
417
688
  **Localized fields return LocalizedString objects:**
418
689
 
419
690
  ```ruby
420
- account = Cashctrl::Account.read(id: 1)
691
+ account = CashCtrl::Account.find(1)
421
692
  account.category_display.class
422
693
  ```
423
694
 
@@ -444,7 +715,7 @@ account.category_display.to_h
444
715
 
445
716
  The locale is determined by (in order of priority):
446
717
  1. `I18n.locale` if I18n is available
447
- 2. `Cashctrl.configuration.language`
718
+ 2. `CashCtrl.configuration.language`
448
719
  3. Falls back to `"en"`
449
720
 
450
721
  ### Export Formats
@@ -461,19 +732,19 @@ client.accounts.list_pdf
461
732
 
462
733
  ```ruby
463
734
  begin
464
- Cashctrl::Account.read(id: 999999)
465
- rescue Cashctrl::AuthenticationError => e
735
+ CashCtrl::Account.find(999999)
736
+ rescue CashCtrl::AuthenticationError => e
466
737
  puts "Invalid API key"
467
- rescue Cashctrl::NotFoundError => e
738
+ rescue CashCtrl::NotFoundError => e
468
739
  puts "Resource not found"
469
- rescue Cashctrl::ValidationError => e
740
+ rescue CashCtrl::ValidationError => e
470
741
  puts "Validation failed"
471
742
  puts e.errors
472
- rescue Cashctrl::RateLimitError => e
743
+ rescue CashCtrl::RateLimitError => e
473
744
  puts "Too many requests"
474
- rescue Cashctrl::ServerError => e
745
+ rescue CashCtrl::ServerError => e
475
746
  puts "Server error (5xx)"
476
- rescue Cashctrl::Error => e
747
+ rescue CashCtrl::Error => e
477
748
  puts "Generic error"
478
749
  end
479
750
  ```
@@ -482,35 +753,36 @@ end
482
753
 
483
754
  | Resource | Class |
484
755
  |----------|-------|
485
- | Accounts | `Cashctrl::Account` |
486
- | Account Banks | `Cashctrl::Account::Bank` |
487
- | Account Categories | `Cashctrl::Account::Category` |
488
- | Cost Centers | `Cashctrl::Account::CostCenter` |
489
- | Currencies | `Cashctrl::Currency` |
490
- | Custom Fields | `Cashctrl::CustomField` |
491
- | Files | `Cashctrl::Resources::CashctrlFile` |
492
- | Fiscal Periods | `Cashctrl::FiscalPeriod` |
493
- | History | `Cashctrl::Resources::History` |
494
- | Inventory Articles | `Cashctrl::Resources::Inventory::Article` |
495
- | Inventory Assets | `Cashctrl::Resources::Inventory::Asset` |
496
- | Inventory Units | `Cashctrl::Resources::Inventory::Unit` |
497
- | Journal | `Cashctrl::Journal` |
498
- | Locations | `Cashctrl::Location` |
499
- | Orders | `Cashctrl::Order` |
500
- | Order Categories | `Cashctrl::Order::Category` |
501
- | Order Documents | `Cashctrl::Order::Document` |
502
- | Persons | `Cashctrl::Person` |
503
- | Person Categories | `Cashctrl::Person::Category` |
504
- | Person Titles | `Cashctrl::Person::Title` |
505
- | Reports | `Cashctrl::Report` |
506
- | Report Collections | `Cashctrl::Report::Collection` |
507
- | Report Elements | `Cashctrl::Report::Element` |
508
- | Roundings | `Cashctrl::Rounding` |
509
- | Salary (all sub-resources) | `Cashctrl::Resources::Salary::*` |
510
- | Sequence Numbers | `Cashctrl::SequenceNumber` |
511
- | Settings | `Cashctrl::Setting` |
512
- | Tax Rates | `Cashctrl::Tax` |
513
- | Text Templates | `Cashctrl::Text` |
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` |
514
786
 
515
787
  ## API Documentation
516
788
 
@@ -545,8 +817,18 @@ account:
545
817
  readonly: true
546
818
  is_inactive:
547
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 }
548
828
  ```
549
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
+
550
832
  **Supported types:**
551
833
 
552
834
  - `string` - Plain strings (auto-detects localized XML strings)
@@ -567,7 +849,7 @@ This generates model classes in `lib/cashctrl/models/` with proper type coercion
567
849
 
568
850
  ## Contributing
569
851
 
570
- Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/cashctrl-ruby. 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-ruby/blob/main/CODE_OF_CONDUCT.md).
852
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/cashctrl-ruby.
571
853
 
572
854
  ## License
573
855