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 Nature de Compte de Sage 100c (IBONatureCompte3)
///
/// Les natures de compte permettent de classifier les comptes généraux
/// selon leur nature comptable (Actif, Passif, Charge, Produit, etc.).
/// Utilisées pour la génération des états financiers et bilans.
#[derive(Debug)]
pub struct NatureCompte {
    pub dispatch: IDispatch,
}

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

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

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

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

    /// Récupère le code de la nature de compte
    pub fn nc_code(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("NC_Code", &[])?
            .to_string()
    }

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

    // ==================== PROPRIÉTÉS DE CLASSIFICATION ====================

    /// Récupère le type de nature (0=Bilan, 1=Gestion)
    pub fn nc_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("NC_Type", &[])?
            .to_i32()
    }

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

    /// Récupère la classe (Actif, Passif, Charge, Produit)
    /// 0=Actif, 1=Passif, 2=Charge, 3=Produit
    pub fn nc_classe(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("NC_Classe", &[])?
            .to_i32()
    }

    /// Définit la classe
    pub fn set_nc_classe(&self, classe: i32) -> SageResult<()> {
        let param = SafeVariant::I4(classe);
        self.dispatch()
            .call_method_by_name("SetNC_Classe", &[param])?;
        Ok(())
    }

    /// Récupère le numéro de nature
    pub fn nc_numero(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("NC_Numero", &[])?
            .to_i32()
    }

    /// Définit le numéro de nature
    pub fn set_nc_numero(&self, numero: i32) -> SageResult<()> {
        let param = SafeVariant::I4(numero);
        self.dispatch()
            .call_method_by_name("SetNC_Numero", &[param])?;
        Ok(())
    }

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

    /// Récupère la position d'affichage dans les états
    pub fn nc_position(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("NC_Position", &[])?
            .to_i32()
    }

    /// Définit la position d'affichage
    pub fn set_nc_position(&self, position: i32) -> SageResult<()> {
        let param = SafeVariant::I4(position);
        self.dispatch()
            .call_method_by_name("SetNC_Position", &[param])?;
        Ok(())
    }

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

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

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

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

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

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

    /// Retourne une description formatée de la nature de compte
    pub fn description(&self) -> SageResult<String> {
        let intitule = self.nc_intitule()?;
        let type_val = self.nc_type()?;
        let classe = self.nc_classe()?;

        let type_str = match type_val {
            0 => "Bilan",
            1 => "Gestion",
            _ => "Inconnu",
        };

        let classe_str = match classe {
            0 => "Actif",
            1 => "Passif",
            2 => "Charge",
            3 => "Produit",
            _ => "Autre",
        };

        Ok(format!(
            "Nature Compte: {} ({}, {})",
            intitule, type_str, classe_str
        ))
    }
}

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

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

        // Propriétés principales
        // nc_intitule() / set_nc_intitule()
        // nc_code() / set_nc_code()

        // Propriétés de classification
        // nc_type() / set_nc_type()
        // nc_classe() / set_nc_classe()
        // nc_numero() / set_nc_numero()

        // Propriétés d'affichage
        // nc_position() / set_nc_position()
        // nc_niveau() / set_nc_niveau()

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