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 Compte de Reporting de Sage 100c (IBOCompteR3)
///
/// Les comptes de reporting permettent de regrouper et analyser les comptes généraux
/// selon différents axes de consolidation et de reporting financier.
#[derive(Debug)]
pub struct CompteR {
    pub dispatch: IDispatch,
}

impl CompteR {
    /// 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 compte de reporting
    pub fn cr_num(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CR_Num", &[])?
            .to_string()
    }

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

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

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

    /// Récupère le type du compte de reporting (0=Détail, 1=Total)
    pub fn cr_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CR_Type", &[])?
            .to_i32()
    }

    /// Définit le type du compte de reporting
    pub fn set_cr_type(&self, type_val: i32) -> SageResult<()> {
        let param = SafeVariant::I4(type_val);
        self.dispatch()
            .call_method_by_name("SetCR_Type", &[param])?;
        Ok(())
    }

    /// Récupère le niveau hiérarchique du compte
    pub fn cr_niveau(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CR_Niveau", &[])?
            .to_i32()
    }

    /// Définit le niveau hiérarchique
    pub fn set_cr_niveau(&self, niveau: i32) -> SageResult<()> {
        let param = SafeVariant::I4(niveau);
        self.dispatch()
            .call_method_by_name("SetCR_Niveau", &[param])?;
        Ok(())
    }

    // ==================== PROPRIÉTÉS DE REGROUPEMENT ====================

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

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

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

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

    // ==================== PROPRIÉTÉS D'ÉTAT ====================

    /// Vérifie si le compte est actif
    pub fn cr_actif(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("CR_Actif", &[])?
            .to_bool()
    }

    /// Définit l'état actif du compte
    pub fn set_cr_actif(&self, actif: bool) -> SageResult<()> {
        let param = SafeVariant::from_bool(actif);
        self.dispatch()
            .call_method_by_name("SetCR_Actif", &[param])?;
        Ok(())
    }

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

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

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

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

    /// Retourne une description formatée du compte de reporting
    pub fn description(&self) -> SageResult<String> {
        let numero = self.cr_num()?;
        let intitule = self.cr_intitule()?;
        let type_val = self.cr_type()?;
        let type_str = match type_val {
            0 => "Détail",
            1 => "Total",
            _ => "Inconnu",
        };
        Ok(format!(
            "Compte Reporting {} - {} (Type: {})",
            numero, intitule, type_str
        ))
    }
}

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

#[cfg(test)]
mod tests {
    #[test]
    fn test_compte_r_properties() {
        // Test de documentation des propriétés disponibles

        // Propriétés principales
        // cr_num() / set_cr_num()
        // cr_intitule() / set_cr_intitule()
        // cr_type() / set_cr_type()
        // cr_niveau() / set_cr_niveau()

        // Propriétés de regroupement
        // cr_compte_rattach() / set_cr_compte_rattach()
        // cr_classement() / set_cr_classement()

        // Propriétés d'état
        // cr_actif() / set_cr_actif()

        // Méthodes
        // write() - Sauvegarde
        // remove() - Suppression
        // description() - Description formatée
    }
}