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 Analyse (Plan d'Analyse) de Sage 100c (IBPAnalyse3)
///
/// Les plans d'analyse permettent de définir les axes d'analyse pour la comptabilité analytique.
/// Chaque plan regroupe des sections analytiques et permet une ventilation spécifique
/// des charges et produits (par projet, service, centre de coût, etc.).
#[derive(Debug)]
pub struct Analyse {
    pub dispatch: IDispatch,
}

impl Analyse {
    /// 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 plan d'analyse (1 à 10)
    pub fn an_numero(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("AN_Numero", &[])?
            .to_i32()
    }

    /// Définit le numéro du plan d'analyse
    pub fn set_an_numero(&self, numero: i32) -> SageResult<()> {
        let param = SafeVariant::I4(numero);
        self.dispatch()
            .call_method_by_name("SetAN_Numero", &[param])?;
        Ok(())
    }

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

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

    /// Récupère l'abrégé du plan d'analyse
    pub fn an_abrege(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("AN_Abrege", &[])?
            .to_string()
    }

    /// Définit l'abrégé du plan d'analyse
    pub fn set_an_abrege(&self, abrege: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(abrege);
        self.dispatch()
            .call_method_by_name("SetAN_Abrege", &[param])?;
        Ok(())
    }

    // ==================== PROPRIÉTÉS DE CONFIGURATION ====================

    /// Récupère le type d'analyse (0=Libre, 1=Obligatoire, 2=Interdite)
    pub fn an_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("AN_Type", &[])?
            .to_i32()
    }

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

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

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

    /// Récupère la longueur du code section
    pub fn an_longueur(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("AN_Longueur", &[])?
            .to_i32()
    }

    /// Définit la longueur du code section
    pub fn set_an_longueur(&self, longueur: i32) -> SageResult<()> {
        let param = SafeVariant::I4(longueur);
        self.dispatch()
            .call_method_by_name("SetAN_Longueur", &[param])?;
        Ok(())
    }

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

    /// Vérifie si le plan d'analyse est actif
    pub fn an_actif(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("AN_Actif", &[])?
            .to_bool()
    }

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

    /// Vérifie si la saisie est protégée
    pub fn an_protege(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("AN_Protege", &[])?
            .to_bool()
    }

    /// Définit la protection de la saisie
    pub fn set_an_protege(&self, protege: bool) -> SageResult<()> {
        let param = SafeVariant::from_bool(protege);
        self.dispatch()
            .call_method_by_name("SetAN_Protege", &[param])?;
        Ok(())
    }

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

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

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

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

    /// Retourne une description formatée du plan d'analyse
    pub fn description(&self) -> SageResult<String> {
        let numero = self.an_numero()?;
        let intitule = self.an_intitule()?;
        let type_val = self.an_type()?;
        let type_str = match type_val {
            0 => "Libre",
            1 => "Obligatoire",
            2 => "Interdite",
            _ => "Inconnu",
        };
        Ok(format!(
            "Plan Analyse {} - {} (Type: {})",
            numero, intitule, type_str
        ))
    }
}

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

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

        // Propriétés principales
        // an_numero() / set_an_numero()
        // an_intitule() / set_an_intitule()
        // an_abrege() / set_an_abrege()

        // Propriétés de configuration
        // an_type() / set_an_type()
        // an_niveau_max() / set_an_niveau_max()
        // an_longueur() / set_an_longueur()

        // Propriétés d'état
        // an_actif() / set_an_actif()
        // an_protege() / set_an_protege()

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