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 Journal Analytique de Sage 100c (IBOJournalA3)
///
/// Les journaux analytiques permettent de gérer la comptabilité analytique
/// et de répartir les charges et produits sur différents axes d'analyse.
#[derive(Debug)]
pub struct JournalA {
    pub dispatch: IDispatch,
}

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

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

    /// Récupère le code du journal analytique
    pub fn ja_code(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("JA_Code", &[])?
            .to_string()
    }

    /// Définit le code du journal analytique
    pub fn set_ja_code(&self, code: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(code);
        self.dispatch()
            .call_method_by_name("SetJA_Code", &[param])?;
        Ok(())
    }

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

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

    /// Récupère le type du journal analytique
    pub fn ja_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("JA_Type", &[])?
            .to_i32()
    }

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

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

    /// Récupère le numéro du plan analytique associé
    pub fn ja_plan(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("JA_Plan", &[])?
            .to_i32()
    }

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

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

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

    // ==================== PROPRIÉTÉS DE NUMÉROTATION ====================

    /// Récupère le compteur de pièces
    pub fn ja_compteur(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("JA_Compteur", &[])?
            .to_i32()
    }

    /// Définit le compteur de pièces
    pub fn set_ja_compteur(&self, compteur: i32) -> SageResult<()> {
        let param = SafeVariant::I4(compteur);
        self.dispatch()
            .call_method_by_name("SetJA_Compteur", &[param])?;
        Ok(())
    }

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

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

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

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

    /// Retourne une description formatée du journal analytique
    pub fn description(&self) -> SageResult<String> {
        let code = self.ja_code()?;
        let intitule = self.ja_intitule()?;
        let type_val = self.ja_type()?;
        let type_str = match type_val {
            0 => "Général",
            1 => "Achat",
            2 => "Vente",
            3 => "Trésorerie",
            _ => "Autre",
        };
        Ok(format!(
            "Journal Analytique {} - {} (Type: {})",
            code, intitule, type_str
        ))
    }
}

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

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

        // Propriétés principales
        // ja_code() / set_ja_code()
        // ja_intitule() / set_ja_intitule()
        // ja_type() / set_ja_type()

        // Propriétés de configuration
        // ja_plan() / set_ja_plan()
        // ja_actif() / set_ja_actif()

        // Propriétés de numérotation
        // ja_compteur() / set_ja_compteur()

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