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 Ecriture analytique de Sage 100c (IBOEcritureA3)
#[derive(Debug)]
pub struct EcritureA {
    pub dispatch: IDispatch,
}

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

    /// Récupère le numéro de l'écriture analytique
    pub fn ea_num(&self) -> SageResult<i32> {
        self.dispatch().call_method_by_name("EA_Num", &[])?.to_i32()
    }

    /// Récupère le numéro de l'écriture associée
    pub fn ea_ecriture_num(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("EA_EcritureNum", &[])?
            .to_i32()
    }

    /// Récupère le numéro de la ligne d'écriture associée
    pub fn ea_ecriture_ligne(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("EA_EcritureLigne", &[])?
            .to_i32()
    }

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

    /// Récupère le montant analytique
    pub fn ea_montant(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("EA_Montant", &[])?
            .to_f64()
    }

    /// Récupère la référence analytique
    pub fn ea_reference(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("EA_Reference", &[])?
            .to_string()
    }

    /// Récupère la date analytique
    pub fn ea_date(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("EA_Date", &[])?
            .to_string()
    }

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

    // ==================== SETTERS POUR PROPRIÉTÉS ====================
    // Note: ea_num, ea_ecriture_num, ea_ecriture_ligne sont en lecture seule (auto-générés)

    /// Définit le code analytique
    pub fn set_ea_code_ana(&self, code_ana: &str) -> SageResult<()> {
        let code_variant = SafeVariant::from_string(code_ana);
        self.dispatch()
            .call_property_put("EA_CodeAna", &[code_variant])?;
        Ok(())
    }

    /// Définit le montant analytique
    pub fn set_ea_montant(&self, montant: f64) -> SageResult<()> {
        let montant_variant = SafeVariant::R8(montant);
        self.dispatch()
            .call_property_put("EA_Montant", &[montant_variant])?;
        Ok(())
    }

    /// Définit la référence analytique
    pub fn set_ea_reference(&self, reference: &str) -> SageResult<()> {
        let reference_variant = SafeVariant::from_string(reference);
        self.dispatch()
            .call_property_put("EA_Reference", &[reference_variant])?;
        Ok(())
    }

    /// Définit la date analytique
    pub fn set_ea_date(&self, date: &str) -> SageResult<()> {
        let date_variant = SafeVariant::from_string(date);
        self.dispatch()
            .call_property_put("EA_Date", &[date_variant])?;
        Ok(())
    }

    /// Définit le libellé analytique
    pub fn set_ea_libelle(&self, libelle: &str) -> SageResult<()> {
        let libelle_variant = SafeVariant::from_string(libelle);
        self.dispatch()
            .call_property_put("EA_Libelle", &[libelle_variant])?;
        Ok(())
    }
}

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