objets_metier_rs 1.0.2

Bibliothèque Rust moderne et sûre pour l'API COM Objets Métier Sage 100c - Production Ready
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use crate::com::{FromDispatchNew, SafeDispatch, SafeVariant};
use crate::errors::SageResult;
use windows::Win32::System::Com::IDispatch;

/// Wrapper pour l'objet Tiers de Sage 100c (IBOTiers3)
#[derive(Debug)]
pub struct Tiers {
    pub dispatch: IDispatch,
}

impl Tiers {
    /// Crée un SafeDispatch temporaire pour les appels
    fn dispatch(&self) -> SafeDispatch<'_> {
        SafeDispatch::new(&self.dispatch)
    }

    /// Accède à l'IDispatch sous-jacent
    ///
    /// Utile pour passer le tiers à d'autres méthodes COM comme SetDefaultClient
    pub fn instance(&self) -> &IDispatch {
        &self.dispatch
    }

    // Propriétés principales des tiers

    /// Récupère le numéro du tiers
    pub fn ct_num(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Num", &[])?
            .to_string()
    }

    /// Récupère l'intitulé du tiers
    pub fn ct_intitule(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Intitule", &[])?
            .to_string()
    }

    /// Récupère le type du tiers (Client, Fournisseur, Salarié, Autre)
    pub fn ct_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_Type", &[])?
            .to_i32()
    }

    /// Récupère la qualité du tiers
    pub fn ct_qualite(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Qualite", &[])?
            .to_string()
    }

    /// Récupère le classement du tiers
    pub fn ct_classement(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Classement", &[])?
            .to_string()
    }

    /// Récupère les commentaires
    pub fn ct_commentaire(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Commentaire", &[])?
            .to_string()
    }

    /// Récupère le numéro SIREN
    pub fn ct_identifiant(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Identifiant", &[])?
            .to_string()
    }

    /// Récupère le numéro SIRET
    pub fn ct_siret(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Siret", &[])?
            .to_string()
    }

    /// Récupère l'APE/NAF
    pub fn ct_ape(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_Ape", &[])?
            .to_string()
    }

    /// Récupère la date de création
    pub fn ct_date_create(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_DateCreate", &[])?
            .to_string()
    }

    /// Récupère le statut actif/inactif
    pub fn ct_sommeil(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("CT_Sommeil", &[])?
            .to_bool()
    }

    // Informations adresse principale

    /// Récupère l'adresse principale (objet adresse)
    pub fn adresse_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("Adresse", &[])
    }

    // Propriétés comptables

    /// Récupère le compte collectif
    pub fn ct_compte_collectif(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CT_CompteCollectif", &[])?
            .to_string()
    }

    /// Récupère la période de facturation
    pub fn ct_periode(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_Periode", &[])?
            .to_i32()
    }

    /// Récupère le solde comptable
    pub fn ct_solde(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_Solde", &[])?
            .to_f64()
    }

    /// Récupère l'encours autorisé
    pub fn ct_encours(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_Encours", &[])?
            .to_f64()
    }

    /// Récupère l'assurance crédit
    pub fn ct_assurance(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_Assurance", &[])?
            .to_f64()
    }

    /// Récupère les conditions de règlement
    pub fn ct_condition_reglement(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_ConditionReglement", &[])?
            .to_i32()
    }

    /// Récupère le nombre de jours
    pub fn ct_nb_jour(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_NbJour", &[])?
            .to_i32()
    }

    /// Récupère le jour de règlement
    pub fn ct_jour_reglement(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_JourReglement", &[])?
            .to_i32()
    }

    /// Récupère la date de fin de mois
    pub fn ct_jour_net(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_JourNet", &[])?
            .to_i32()
    }

    /// Récupère la condition de règlement le
    pub fn ct_condition_reglement_le(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_ConditionReglementLe", &[])?
            .to_i32()
    }

    /// Récupère le taux d'escompte
    pub fn ct_taux_escompte(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_TauxEscompte", &[])?
            .to_f64()
    }

    /// Récupère le taux de retard
    pub fn ct_taux_retard(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_TauxRetard", &[])?
            .to_f64()
    }

    /// Récupère le taux de risque
    pub fn ct_taux_risque(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CT_TauxRisque", &[])?
            .to_f64()
    }

    /// Récupère si tiers soumis à la TVA
    pub fn ct_tva_encaissement(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("CT_TvaEncaissement", &[])?
            .to_bool()
    }

    /// Récupère le régime de TVA
    pub fn ct_tva_regime(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CT_TvaRegime", &[])?
            .to_i32()
    }

    // Relations avec autres objets

    /// Récupère le tiers payeur (raw variant)
    pub fn tiers_payeur_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("TiersPayeur", &[])
    }

    /// Récupère la devise (raw variant)
    pub fn devise_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("Devise", &[])
    }

    /// Récupère la catégorie comptable (raw variant)
    pub fn categorie_comptable_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch()
            .call_method_by_name("CategorieComptable", &[])
    }

    /// Récupère le mode de règlement (raw variant)
    pub fn reglement_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("Reglement", &[])
    }

    /// Récupère la banque principale (raw variant)
    pub fn banque_principale_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("BanquePrincipale", &[])
    }

    /// Récupère le transporteur (raw variant)
    pub fn transporteur_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("Transporteur", &[])
    }

    /// Récupère le représentant (raw variant)
    pub fn representant_raw(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("Representant", &[])
    }

    // Méthodes de calcul et d'information

    /// Calcule le chiffre d'affaires pour une période
    pub fn ca_periode(&self, date_debut: &str, date_fin: &str) -> SageResult<f64> {
        let debut = SafeVariant::from_string(date_debut);
        let fin = SafeVariant::from_string(date_fin);
        self.dispatch()
            .call_method_by_name("CAPeriode", &[debut, fin])?
            .to_f64()
    }

    /// Calcule le chiffre d'affaires pour l'exercice N
    pub fn ca_n(&self) -> SageResult<f64> {
        self.dispatch().call_method_by_name("CA_N", &[])?.to_f64()
    }

    /// Calcule le chiffre d'affaires pour l'exercice N-1
    pub fn ca_n_1(&self) -> SageResult<f64> {
        self.dispatch().call_method_by_name("CA_N_1", &[])?.to_f64()
    }

    /// Calcule le chiffre d'affaires pour l'exercice N-2
    pub fn ca_n_2(&self) -> SageResult<f64> {
        self.dispatch().call_method_by_name("CA_N_2", &[])?.to_f64()
    }

    /// Récupère le solde au jour J
    pub fn solde_jour_j(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("SoldeJourJ", &[])?
            .to_f64()
    }

    /// Récupère le solde échu
    pub fn solde_echu(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("SoldeEchu", &[])?
            .to_f64()
    }

    /// Récupère le solde non échu
    pub fn solde_non_echu(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("SoldeNonEchu", &[])?
            .to_f64()
    }

    /// Récupère l'encours
    pub fn encours(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("Encours", &[])?
            .to_f64()
    }

    /// Récupère l'encours en devise
    pub fn encours_devise(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("EncoursDevise", &[])?
            .to_f64()
    }

    /// Récupère la date du dernier règlement
    pub fn date_dernier_reglement(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("DateDernierReglement", &[])?
            .to_string()
    }

    /// Récupère le montant du dernier règlement
    pub fn montant_dernier_reglement(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("MontantDernierReglement", &[])?
            .to_f64()
    }

    /// Récupère la date de la dernière facture
    pub fn date_derniere_facture(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("DateDerniereFacture", &[])?
            .to_string()
    }

    /// Récupère le montant de la dernière facture
    pub fn montant_derniere_facture(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("MontantDerniereFacture", &[])?
            .to_f64()
    }

    // ==================== SETTERS POUR PROPRIÉTÉS ====================

    /// Définit le numéro du tiers
    /// Note: seulement pour les objets non persistants
    pub fn set_ct_num(&self, numero: &str) -> SageResult<()> {
        let numero_variant = SafeVariant::from_string(numero);
        self.dispatch()
            .call_property_put("CT_Num", &[numero_variant])?;
        Ok(())
    }

    /// Définit l'intitulé du tiers
    pub fn set_ct_intitule(&self, intitule: &str) -> SageResult<()> {
        let intitule_variant = SafeVariant::from_string(intitule);
        self.dispatch()
            .call_property_put("CT_Intitule", &[intitule_variant])?;
        Ok(())
    }

    /// Définit le type du tiers
    /// 0 = Client, 1 = Fournisseur, 2 = Salarié, 3 = Autre
    pub fn set_ct_type(&self, type_tiers: i32) -> SageResult<()> {
        let type_variant = SafeVariant::from_i32(type_tiers);
        self.dispatch()
            .call_property_put("CT_Type", &[type_variant])?;
        Ok(())
    }

    /// Définit la qualité du tiers
    pub fn set_ct_qualite(&self, qualite: &str) -> SageResult<()> {
        let qualite_variant = SafeVariant::from_string(qualite);
        self.dispatch()
            .call_property_put("CT_Qualite", &[qualite_variant])?;
        Ok(())
    }

    /// Définit le classement du tiers
    pub fn set_ct_classement(&self, classement: &str) -> SageResult<()> {
        let classement_variant = SafeVariant::from_string(classement);
        self.dispatch()
            .call_property_put("CT_Classement", &[classement_variant])?;
        Ok(())
    }

    /// Définit les commentaires
    pub fn set_ct_commentaire(&self, commentaire: &str) -> SageResult<()> {
        let commentaire_variant = SafeVariant::from_string(commentaire);
        self.dispatch()
            .call_property_put("CT_Commentaire", &[commentaire_variant])?;
        Ok(())
    }

    /// Définit le numéro SIREN
    pub fn set_ct_identifiant(&self, identifiant: &str) -> SageResult<()> {
        let identifiant_variant = SafeVariant::from_string(identifiant);
        self.dispatch()
            .call_property_put("CT_Identifiant", &[identifiant_variant])?;
        Ok(())
    }

    /// Définit le numéro SIRET
    pub fn set_ct_siret(&self, siret: &str) -> SageResult<()> {
        let siret_variant = SafeVariant::from_string(siret);
        self.dispatch()
            .call_property_put("CT_Siret", &[siret_variant])?;
        Ok(())
    }

    /// Définit l'APE/NAF
    pub fn set_ct_ape(&self, ape: &str) -> SageResult<()> {
        let ape_variant = SafeVariant::from_string(ape);
        self.dispatch()
            .call_property_put("CT_Ape", &[ape_variant])?;
        Ok(())
    }

    /// Définit le statut actif/inactif (en sommeil)
    pub fn set_ct_sommeil(&self, sommeil: bool) -> SageResult<()> {
        let sommeil_variant = SafeVariant::from_bool(sommeil);
        self.dispatch()
            .call_property_put("CT_Sommeil", &[sommeil_variant])?;
        Ok(())
    }

    /// Définit le compte collectif
    pub fn set_ct_compte_collectif(&self, compte: &str) -> SageResult<()> {
        let compte_variant = SafeVariant::from_string(compte);
        self.dispatch()
            .call_property_put("CT_CompteCollectif", &[compte_variant])?;
        Ok(())
    }

    /// Définit la période de facturation
    pub fn set_ct_periode(&self, periode: i32) -> SageResult<()> {
        let periode_variant = SafeVariant::from_i32(periode);
        self.dispatch()
            .call_property_put("CT_Periode", &[periode_variant])?;
        Ok(())
    }

    /// Définit l'encours autorisé
    pub fn set_ct_encours(&self, encours: f64) -> SageResult<()> {
        let encours_variant = SafeVariant::R8(encours);
        self.dispatch()
            .call_property_put("CT_Encours", &[encours_variant])?;
        Ok(())
    }

    /// Définit l'assurance crédit
    pub fn set_ct_assurance(&self, assurance: f64) -> SageResult<()> {
        let assurance_variant = SafeVariant::R8(assurance);
        self.dispatch()
            .call_property_put("CT_Assurance", &[assurance_variant])?;
        Ok(())
    }

    /// Définit les conditions de règlement
    /// 0 = Comptant, 1 = À crédit
    pub fn set_ct_condition_reglement(&self, condition: i32) -> SageResult<()> {
        let condition_variant = SafeVariant::from_i32(condition);
        self.dispatch()
            .call_property_put("CT_ConditionReglement", &[condition_variant])?;
        Ok(())
    }

    /// Définit le nombre de jours
    pub fn set_ct_nb_jour(&self, nb_jour: i32) -> SageResult<()> {
        let nb_jour_variant = SafeVariant::from_i32(nb_jour);
        self.dispatch()
            .call_property_put("CT_NbJour", &[nb_jour_variant])?;
        Ok(())
    }

    /// Définit le jour de règlement
    pub fn set_ct_jour_reglement(&self, jour: i32) -> SageResult<()> {
        let jour_variant = SafeVariant::from_i32(jour);
        self.dispatch()
            .call_property_put("CT_JourReglement", &[jour_variant])?;
        Ok(())
    }

    /// Définit la date de fin de mois
    pub fn set_ct_jour_net(&self, jour_net: i32) -> SageResult<()> {
        let jour_variant = SafeVariant::from_i32(jour_net);
        self.dispatch()
            .call_property_put("CT_JourNet", &[jour_variant])?;
        Ok(())
    }

    /// Définit la condition de règlement le
    pub fn set_ct_condition_reglement_le(&self, le: i32) -> SageResult<()> {
        let le_variant = SafeVariant::from_i32(le);
        self.dispatch()
            .call_property_put("CT_ConditionReglementLe", &[le_variant])?;
        Ok(())
    }

    /// Définit le taux d'escompte
    pub fn set_ct_taux_escompte(&self, taux: f64) -> SageResult<()> {
        let taux_variant = SafeVariant::R8(taux);
        self.dispatch()
            .call_property_put("CT_TauxEscompte", &[taux_variant])?;
        Ok(())
    }

    /// Définit le taux de retard
    pub fn set_ct_taux_retard(&self, taux: f64) -> SageResult<()> {
        let taux_variant = SafeVariant::R8(taux);
        self.dispatch()
            .call_property_put("CT_TauxRetard", &[taux_variant])?;
        Ok(())
    }

    /// Définit le taux de risque
    pub fn set_ct_taux_risque(&self, taux: f64) -> SageResult<()> {
        let taux_variant = SafeVariant::R8(taux);
        self.dispatch()
            .call_property_put("CT_TauxRisque", &[taux_variant])?;
        Ok(())
    }

    /// Définit si tiers soumis à la TVA sur encaissement
    pub fn set_ct_tva_encaissement(&self, tva: bool) -> SageResult<()> {
        let tva_variant = SafeVariant::from_bool(tva);
        self.dispatch()
            .call_property_put("CT_TvaEncaissement", &[tva_variant])?;
        Ok(())
    }

    /// Définit le régime de TVA
    pub fn set_ct_tva_regime(&self, regime: i32) -> SageResult<()> {
        let regime_variant = SafeVariant::from_i32(regime);
        self.dispatch()
            .call_property_put("CT_TvaRegime", &[regime_variant])?;
        Ok(())
    }

    // Informations libres

    /// Récupère une information libre par index (1-based)
    pub fn info_libre(&self, index: i16) -> SageResult<String> {
        let idx = SafeVariant::from_i16(index);
        self.dispatch()
            .call_method_by_name("InfoLibre", &[idx])?
            .to_string()
    }

    /// Définit une information libre par index
    pub fn set_info_libre(&self, index: i16, valeur: &str) -> SageResult<()> {
        let idx = SafeVariant::from_i16(index);
        let val = SafeVariant::from_string(valeur);
        self.dispatch()
            .call_method_by_name("InfoLibre", &[idx, val])?;
        Ok(())
    }

    // Méthodes communes héritées de IBIPersistObject

    /// Vérifie si l'objet est persistant
    pub fn is_persistant(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("IsPersistant", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Vérifie si l'objet a été modifié
    pub fn is_modified(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("IsModified", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Initialise les valeurs par défaut
    pub fn set_default(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("SetDefault", &[])?;
        Ok(())
    }

    /// Sauvegarde l'objet dans la base de données
    pub fn write(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Write", &[])?;
        Ok(())
    }

    /// Sauvegarde l'objet avec les valeurs par défaut
    pub fn write_default(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("WriteDefault", &[])?;
        Ok(())
    }

    /// Relit l'objet depuis la base de données
    pub fn read(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Read", &[])?;
        Ok(())
    }

    /// Supprime l'objet de la base de données
    pub fn remove(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Remove", &[])?;
        Ok(())
    }

    /// Rafraîchit l'objet si nécessaire
    pub fn refresh(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Refresh", &[])?;
        Ok(())
    }
}

// Implémentation de traits utiles
impl Clone for Tiers {
    fn clone(&self) -> Self {
        Self {
            dispatch: self.dispatch.clone(),
        }
    }
}

impl PartialEq for Tiers {
    fn eq(&self, other: &Self) -> bool {
        // Comparaison basée sur le numéro de tiers
        match (self.ct_num(), other.ct_num()) {
            (Ok(num1), Ok(num2)) => num1 == num2,
            _ => false,
        }
    }
}

// Implémentation du trait FromDispatchNew pour permettre l'utilisation avec TypedComCollection
impl FromDispatchNew for Tiers {
    fn from_dispatch_new(dispatch: IDispatch) -> SageResult<Self> {
        Ok(Self { dispatch })
    }
}