-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
490 lines (437 loc) · 19.4 KB
/
Copy pathProgram.cs
File metadata and controls
490 lines (437 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using intacct_rest_api.Models;
using intacct_rest_api.Models.Batch;
using intacct_rest_api.Models.Bulk;
using intacct_rest_api.Models.Composite;
using intacct_rest_api.Models.Export;
using intacct_rest_api.Models.InvoiceCreate;
using intacct_rest_api.Models.BillLineUpdate;
using intacct_rest_api.Models.InvoiceLineUpdate;
using intacct_rest_api.Models.InvoiceUpdate;
using intacct_rest_api.Models.Query;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
// ========== 1. Configuration ==========
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var urlBase = "https://api.intacct.com/ia/api/v1/";
var idClient = config["IdClient"];
var secretClient = config["SecretClient"];
var utilisateur = config["Utilisateur"];
var intacctService = new IntacctService(urlBase, idClient!, secretClient!, utilisateur!);
// ========== 2. Auth : obtenir le token ==========
var reponseAuth = await intacctService.ObtenirToken();
if (!reponseAuth.IsSuccessful)
{
Console.WriteLine("Erreur lors de l'authentification : " + reponseAuth.Content);
return;
}
var token = new Token(reponseAuth);
Console.WriteLine("Token d'accès : " + token.access_token.Substring(0, 40) + "...");
Console.WriteLine("Date d'expiration : " + token.DateExpiration);
Console.WriteLine("Est expiré ? : " + token.EstExpire);
// Plus tard : rafraîchir / révoquer
// var reponseRafraichir = await intacctService.RafraichirToken(token.refresh_token);
// var revokeOk = await intacctService.RevokerToken(token.access_token);
// ========== Menu de démo ==========
Console.WriteLine("\nChoisissez le scénario à exécuter :");
Console.WriteLine("1 - Query + Export (bill)");
Console.WriteLine("2 - GET factures (liste)");
Console.WriteLine("3 - GET facture (détail)");
Console.WriteLine("4 - POST facture (création)");
Console.WriteLine("5 - PATCH facture (mise à jour)");
Console.WriteLine("6 - PATCH ligne de bill (mise à jour)");
Console.WriteLine("7 - PATCH ligne de facture (mise à jour)");
Console.WriteLine("8 - DELETE facture");
Console.WriteLine("9 - Tous les scénarios");
Console.WriteLine("10 - Batch mode (GET/POST/PATCH/DELETE)");
Console.WriteLine("11 - Bulk create (vendors)");
Console.WriteLine("12 - Bulk get result (statut + download)");
Console.WriteLine("13 - Composite (plusieurs requêtes en un appel)");
Console.Write("\nVotre choix (1/2/3/4/5/6/7/8/9/10/11/12/13) : ");
var choix = Console.ReadLine();
switch (choix)
{
case "1":
await RunQueryAndExportAsync(intacctService, token);
break;
case "2":
await RunGetInvoicesAsync(intacctService, token);
break;
case "3":
await RunGetInvoiceDetailAsync(intacctService, token);
break;
case "4":
await RunInvoiceCreateAsync(intacctService, token);
break;
case "5":
await RunInvoiceUpdateAsync(intacctService, token);
break;
case "6":
await RunBillLineUpdateAsync(intacctService, token);
break;
case "7":
await RunInvoiceLineUpdateAsync(intacctService, token);
break;
case "8":
await RunInvoiceDelete(intacctService, token);
break;
case "10":
await RunBatchAsync(intacctService, token);
break;
case "11":
await RunBulkAsync(intacctService, token);
break;
case "12":
Console.Write("JobId (ex. copié après option 11) : ");
var jobId = Console.ReadLine()?.Trim();
if (!string.IsNullOrEmpty(jobId))
await RunBulkGetResultAsync(intacctService, token, jobId);
else
Console.WriteLine("JobId vide, annulé.");
break;
case "13":
await RunCompositeAsync(intacctService, token);
break;
case "9":
await RunQueryAndExportAsync(intacctService, token);
await RunGetInvoicesAsync(intacctService, token);
await RunInvoiceDetailAfterListAsync(intacctService, token);
await RunInvoiceCreateAsync(intacctService, token);
await RunInvoiceUpdateAsync(intacctService, token);
await RunBillLineUpdateAsync(intacctService, token);
await RunInvoiceLineUpdateAsync(intacctService, token);
break;
default:
Console.WriteLine("\nChoix non reconnu, aucun scénario exécuté.");
break;
}
Console.WriteLine("\nTerminé. Appuyez sur Entrée pour fermer.");
Console.ReadLine();
// === Méthodes de démo ===
static async Task RunQueryAndExportAsync(IntacctService intacctService, Token token)
{
// ========== 3. Requête Query ==========
var queryObject = "accounts-payable/bill";
var queryFields = new List<string> { "id", "billNumber", "vendor.id", "vendor.name", "postingDate", "totalTxnAmount", "entity.id" };
var queryFilters = new List<Dictionary<string, object>>
{
Filter.GreaterThan("totalTxnAmount", "100"),
Filter.Between("postingDate", new DateTime(2025, 1, 1), new DateTime(2025, 1, 31))
};
var queryFilterExpression = FilterExpression.And(FilterExpression.Ref(0), FilterExpression.Ref(1));
var queryFilterExpressionString = FilterExpression.Build(queryFilters, queryFilterExpression);
var queryFilterParam = new FilterParameters { CaseSensitiveComparison = false, IncludePrivate = false };
var querySort = new List<Dictionary<string, string>> { new() { ["totalTxnAmount"] = "desc" } };
var queryRequest = new QueryRequest
{
Object = queryObject,
Fields = queryFields,
Filters = queryFilters,
FilterExpression = queryFilterExpressionString,
FilterParameters = queryFilterParam,
OrderBy = querySort,
Start = 1,
Size = 100
};
var reponseQuery = await intacctService.Query(queryRequest, token.access_token);
Console.WriteLine("\nRequête - Succès : " + reponseQuery.IsSuccessful);
// Désérialiser + afficher
var queryResponse = JsonConvert.DeserializeObject<QueryResponse>(reponseQuery.Content!);
Console.WriteLine("Résultats : " + queryResponse!.Result.Count);
if (queryResponse.Result.Count > 0)
Console.WriteLine("Premier : " + string.Join(", ", queryResponse.Result[0].Select(kv => kv.Key + "=" + kv.Value)));
// Export
var fileType = ExportFileType.Pdf;
var reponseExport = await intacctService.Export(queryRequest, fileType, token.access_token);
var nomFichier = $"{queryRequest.Object.Replace("/", "-")}-export-{DateTime.Now:ddMMyyyy-HHmmss}.pdf";
File.WriteAllBytes(Path.Combine("C:\\temp", nomFichier), reponseExport.RawBytes!);
Console.WriteLine("Fichier : C:\\temp\\" + nomFichier);
}
static async Task RunGetInvoicesAsync(IntacctService intacctService, Token token)
{
var reponse = await intacctService.GetInvoices(token.access_token);
var list = JsonConvert.DeserializeObject<InvoiceReferenceListResponse>(reponse.Content!);
foreach (var inv in list!.Result.Take(3))
Console.WriteLine($"key={inv.key}, id={inv.id}");
}
static async Task RunGetInvoiceDetailAsync(IntacctService intacctService, Token token)
{
var key = "11"; // key facture démo
var reponse = await intacctService.GetInvoiceByKey(key, token.access_token);
var detail = JsonConvert.DeserializeObject<InvoiceDetailResponse>(reponse.Content!);
var h = detail!.Invoice;
Console.WriteLine($"Facture {h.invoiceNumber}, client {h.customer.name}, total {h.totalTxnAmount}");
var l = h.lines[0];
Console.WriteLine($"Ligne 1 : {l.glAccount.id}, {l.txnAmount}, lieu {l.dimensions.location.id}");
}
static async Task RunInvoiceDetailAfterListAsync(IntacctService intacctService, Token token)
{
var list = JsonConvert.DeserializeObject<InvoiceReferenceListResponse>((await intacctService.GetInvoices(token.access_token)).Content!);
var key = list!.Result[0].key;
var detail = JsonConvert.DeserializeObject<InvoiceDetailResponse>((await intacctService.GetInvoiceByKey(key, token.access_token)).Content!);
var h = detail!.Invoice;
Console.WriteLine($"Facture {h.invoiceNumber}, total {h.totalTxnAmount}; ligne 1 key={h.lines[0].key}");
}
static async Task RunInvoiceCreateAsync(IntacctService intacctService, Token token)
{
// POST facture : on assigne explicitement .Id (Customer.Id, GlAccount.Id, Dimensions.Customer.Id, Dimensions.Location.Id).
var createRequest = new InvoiceCreate
{
customer = { id = "CL0170" },
invoiceDate = "2025-12-06",
dueDate = "2025-12-31",
lines =
[
new Line
{
txnAmount = "100",
glAccount = { id = "701000" },
dimensions =
{
customer = new IdRef { id = "CL0170" },
location = new IdRef { id = "DEMO_1" }
}
}
]
};
Console.WriteLine("Json => \n"+ JsonConvert.SerializeObject(createRequest, Formatting.Indented));
var reponse = await intacctService.CreateInvoice(createRequest, token.access_token);
Console.WriteLine("POST invoice - Succès : " + reponse.IsSuccessful);
}
static async Task RunInvoiceUpdateAsync(IntacctService intacctService, Token token)
{
var key = "11";
var updateRequest = new InvoiceUpdate
{
referenceNumber = "PO-UPDATED-99",
description = "Modifié par Atelier",
dueDate = "2026-01-15",
};
var reponse = await intacctService.UpdateInvoice(updateRequest, key, token.access_token);
Console.WriteLine("PATCH invoice - Succès : " + reponse.IsSuccessful);
}
static async Task RunBillLineUpdateAsync(IntacctService intacctService, Token token)
{
var lineKey = "3"; // key ligne bill démo
var updateRequest = new BillLineUpdate
{
txnAmount = "150.00",
memo = "Démo bill line",
dimensions = new BillLineDimensions
{
department = new IdRef { id = "922" },
location = new IdRef { id = "DEMO_1" }
}
};
var reponse = await intacctService.UpdateBillLine(updateRequest, lineKey, token.access_token);
Console.WriteLine("PATCH bill-line - Succès : " + reponse.IsSuccessful);
}
static async Task RunInvoiceLineUpdateAsync(IntacctService intacctService, Token token)
{
var lineKey = "11"; // key ligne facture démo
var updateRequest = new InvoiceLineUpdate
{
txnAmount = "150.00",
memo = "Démo invoice line"
};
var reponse = await intacctService.UpdateInvoiceLine(updateRequest, lineKey, token.access_token);
Console.WriteLine("PATCH invoice-line - Succès : " + reponse.IsSuccessful);
}
static async Task RunInvoiceDelete(IntacctService intacctService, Token token)
{
var key = "11";
var reponse = await intacctService.DeleteInvoice(key, token.access_token);
Console.WriteLine("DELETE invoice - Succès : " + reponse.IsSuccessful);
}
static async Task RunBatchAsync(IntacctService intacctService, Token token)
{
var objectPath = "objects/accounts-payable/vendor";
var suffix = DateTime.UtcNow.ToString("MMddHHmmss");
var newVendors = new List<object>
{
new Dictionary<string, object> { ["id"] = $"batchv1-{suffix}", ["name"] = $"Batch Vendor 1 {suffix}" },
new Dictionary<string, object> { ["id"] = $"batchv2-{suffix}", ["name"] = $"Batch Vendor 2 {suffix}" },
new Dictionary<string, object> { ["id"] = $"batchv3-{suffix}", ["name"] = $"Batch Vendor 3 {suffix}" }
};
Console.WriteLine("\n[BATCH] 1) POST create (3 vendors)");
var createRes = await intacctService.BatchCreate(objectPath, newVendors, token.access_token);
Console.WriteLine("HTTP status create : " + (int)createRes.StatusCode);
if (!createRes.IsSuccessful || string.IsNullOrWhiteSpace(createRes.Content))
{
Console.WriteLine("Batch create échec : " + createRes.Content);
return;
}
var created = JsonConvert.DeserializeObject<BatchResponse>(createRes.Content!);
PrintBatchSummary(created);
var keys = created!.Result.Where(x => !string.IsNullOrWhiteSpace(x.key)).Select(x => x.key!).ToList();
if (keys.Count == 0)
{
Console.WriteLine("Aucune key retournée, suite de la démo annulée.");
return;
}
Console.WriteLine("Keys créées : " + string.Join(", ", keys));
Console.WriteLine("\n[BATCH] 2) GET by keys");
var getRes = await intacctService.BatchGetByKeys(objectPath, keys, token.access_token);
Console.WriteLine("HTTP status get : " + (int)getRes.StatusCode);
Console.WriteLine("Longueur payload : " + (getRes.Content?.Length ?? 0));
Console.WriteLine("\n[BATCH] 3) PATCH non-atomic (mise à jour des noms)");
var patchItems = keys.Select((k, i) =>
{
var item = new BatchPatchItem { key = k };
item["name"] = $"Batch Vendor {i + 1} UPDATED {suffix}";
return item;
}).ToList();
var patchRes = await intacctService.BatchUpdate(objectPath, patchItems, token.access_token, atomic: false);
Console.WriteLine("HTTP status patch non-atomic : " + (int)patchRes.StatusCode);
if (!string.IsNullOrWhiteSpace(patchRes.Content))
PrintBatchSummary(JsonConvert.DeserializeObject<BatchResponse>(patchRes.Content!));
Console.WriteLine("\n[BATCH] 4) PATCH atomic (1 key invalide pour illustrer l'échec transactionnel)");
var invalidPatchItem = new BatchPatchItem { key = "999999999" };
invalidPatchItem["name"] = "Invalid key to force atomic error";
var atomicPatch = new List<BatchPatchItem>(patchItems)
{
invalidPatchItem
};
var atomicRes = await intacctService.BatchUpdate(objectPath, atomicPatch, token.access_token, atomic: true);
Console.WriteLine("HTTP status patch atomic : " + (int)atomicRes.StatusCode);
if (!string.IsNullOrWhiteSpace(atomicRes.Content))
PrintBatchSummary(JsonConvert.DeserializeObject<BatchResponse>(atomicRes.Content!));
Console.WriteLine("\n[BATCH] 5) DELETE by keys (non-atomic)");
var deleteRes = await intacctService.BatchDeleteByKeys(objectPath, keys, token.access_token, atomic: false);
Console.WriteLine("HTTP status delete : " + (int)deleteRes.StatusCode);
if (!string.IsNullOrWhiteSpace(deleteRes.Content))
PrintBatchSummary(JsonConvert.DeserializeObject<BatchResponse>(deleteRes.Content!));
else
Console.WriteLine("DELETE batch : payload vide (ex. 204 No Content).");
}
static void PrintBatchSummary(BatchResponse? response)
{
if (response == null)
{
Console.WriteLine("Réponse batch non désérialisable.");
return;
}
Console.WriteLine($"meta => totalCount={response.Meta.totalCount}, totalSuccess={response.Meta.totalSuccess}, totalError={response.Meta.totalError}");
foreach (var (item, idx) in response.Result.Select((x, i) => (x, i + 1)))
{
var status = item.Status?.ToString() ?? "n/a";
var key = string.IsNullOrWhiteSpace(item.key) ? "—" : item.key;
var id = string.IsNullOrWhiteSpace(item.id) ? "—" : item.id;
var message = item.Error?.message ?? "";
Console.WriteLine($" [{idx}] status={status}, key={key}, id={id}" + (string.IsNullOrWhiteSpace(message) ? "" : $", error={message}"));
}
}
static async Task RunBulkAsync(IntacctService intacctService, Token token)
{
var request = new BulkCreateRequest
{
objectName = "accounts-payable/vendor",
operation = "create",
jobFile = "file",
fileContentType = "json"
// callbackURL = "https://your-server.com/bulk/callback" // optionnel
};
var jsonBody = """
[
{"id":"vendor1","name":"Corner Library"},
{"id":"vendor2","name":"Just Picked"},
{"id":"vendor3","name":"Paper Goods"},
{"id":"vendor4","name":"Office Furnishings"},
{"id":"vendor5","name":"Gadget Pro"},
{"id":"vendor6","name":"Tech Solutions"},
{"id":"vendor7","name":"Home Essentials"},
{"id":"vendor8","name":"Garden Supplies"},
{"id":"vendor9","name":"Auto Parts Co."},
{"id":"vendor10","name":"Fashion Hub"}
]
""";
var createRes = await intacctService.BulkCreate(request, jsonBody, token.access_token);
if (!createRes.IsSuccessful)
{
Console.WriteLine("Bulk create échec : " + createRes.Content);
return;
}
var createData = JsonConvert.DeserializeObject<BulkCreateResponse>(createRes.Content!);
var jobId = createData!.Result.jobId;
Console.WriteLine("Bulk envoyé. jobId : " + jobId);
Console.WriteLine("Pour vérifier le statut et télécharger le résultat : option 11 avec ce jobId.");
}
/// <summary>
/// Démo simple : vérifier le statut d'un job bulk puis télécharger le résultat (option 11).
/// </summary>
static async Task RunBulkGetResultAsync(IntacctService intacctService, Token token, string jobId)
{
// 1. Statut
var statusRes = await intacctService.BulkStatus(jobId, token.access_token, download: false);
if (!statusRes.IsSuccessful)
{
Console.WriteLine("Bulk status échec : " + statusRes.Content);
return;
}
var statusData = JsonConvert.DeserializeObject<BulkStatusResponse>(statusRes.Content!);
Console.WriteLine("Statut : " + statusData!.Result.status + ", percentComplete : " + (statusData.Result.percentComplete?.ToString() ?? "—"));
// 2. Download
var downloadRes = await intacctService.BulkStatus(jobId, token.access_token, download: true);
if (!downloadRes.IsSuccessful)
{
Console.WriteLine("Bulk download échec : " + downloadRes.Content);
return;
}
var content = downloadRes.Content ?? "";
try
{
var parsed = JsonConvert.DeserializeObject(content);
content = JsonConvert.SerializeObject(parsed, Formatting.Indented);
}
catch { /* garder le contenu brut si pas du JSON */ }
Console.WriteLine("Résultat (download) :\n" + content);
}
/// <summary>
/// Démo composite : création de 2 factures en un seul appel (même modèle InvoiceCreate que POST facture).
/// </summary>
static async Task RunCompositeAsync(IntacctService intacctService, Token token)
{
var invoice1 = new InvoiceCreate
{
customer = { id = "CL0170" },
invoiceDate = "2025-12-06",
dueDate = "2025-12-31",
lines =
[
new Line
{
txnAmount = "100",
glAccount = { id = "701000" },
dimensions = { customer = new IdRef { id = "CL0170" }, location = new IdRef { id = "DEMO_1" } }
}
]
};
var invoice2 = new InvoiceCreate
{
customer = { id = "CL0170" },
invoiceDate = "2025-12-07",
dueDate = "2026-01-01",
lines =
[
new Line
{
txnAmount = "200",
glAccount = { id = "701000" },
dimensions = { customer = new IdRef { id = "CL0170" }, location = new IdRef { id = "DEMO_1" } }
}
]
};
var subRequests = new List<CompositeSubRequest>
{
new() { method = "POST", path = "/objects/accounts-receivable/invoice", body = invoice1 },
new() { method = "POST", path = "/objects/accounts-receivable/invoice", body = invoice2 }
};
var response = await intacctService.Composite(subRequests, token.access_token);
if (!response.IsSuccessful)
{
Console.WriteLine("Composite échec : " + response.Content);
return;
}
var composite = JsonConvert.DeserializeObject<CompositeResponse>(response.Content!);
Console.WriteLine("Composite réussi. totalSuccess=" + composite!.Meta.totalSuccess + ", totalError=" + composite.Meta.totalError);
Console.WriteLine("Réponse (ia::result) : " + (response.Content?.Length ?? 0) + " caractères.");
}