objets_metier_rs 1.0.2

Bibliothèque Rust moderne et sûre pour l'API COM Objets Métier Sage 100c - Production Ready
use crate::com::{FromDispatchNew, SafeDispatch, SafeVariant};
use crate::errors::SageResult;
use windows::Win32::System::Com::IDispatch;

/// Wrapper pour l'objet TiersAutre de Sage 100c (IBOTiersAutre3)
///
/// Représente un tiers de type Autre dans Sage 100c.
/// Utilisé pour les tiers qui ne sont ni clients, ni fournisseurs, ni salariés.
#[derive(Debug)]
pub struct TiersAutre {
    pub dispatch: IDispatch,
}

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

    // ==================== PROPRIÉTÉS PRINCIPALES ====================

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

    /// Définit le numéro du tiers autre
    pub fn set_ct_num(&self, numero: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(numero);
        self.dispatch().call_method_by_name("SetCT_Num", &[param])?;
        Ok(())
    }

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

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

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

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

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

    // ==================== MÉTHODES IBIPersistObject ====================

    /// Sauvegarde les modifications du tiers autre
    pub fn write(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Write", &[])?;
        Ok(())
    }

    /// Supprime le tiers autre
    pub fn remove(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Remove", &[])?;
        Ok(())
    }

    // ==================== MÉTHODES DE DESCRIPTION ====================

    /// Retourne une description formatée du tiers autre
    pub fn description(&self) -> SageResult<String> {
        let numero = self.ct_num()?;
        let intitule = self.ct_intitule()?;
        Ok(format!("TiersAutre: {} - {}", numero, intitule))
    }
}

impl FromDispatchNew for TiersAutre {
    fn from_dispatch_new(dispatch: IDispatch) -> SageResult<Self> {
        Ok(Self { dispatch })
    }
}