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::{SafeDispatch, SafeVariant};
use crate::errors::SageResult;
use windows::Win32::System::Com::IDispatch;

/// ✅ IMPLÉMENTATION ENRICHIE - Version Phase 6
///
/// Wrapper pour unités de mesure (IBSCIALUnite3)
///
/// Les unités de mesure permettent de définir les unités de vente, d'achat
/// et de stock des articles :
/// - Pièce (PCE, U, etc.)
/// - Poids (kg, g, t, etc.)
/// - Longueur (m, cm, mm, etc.)
/// - Volume (L, mL, m³, etc.)
/// - Surface (m², etc.)
///
/// # Exemple
/// ```no_run
/// use objets_metier_rs::wrappers::cial::CialApplication;
///
/// let cial_app = CialApplication::new("Objets100c.CIAL")?;
/// let factory = cial_app.factory_unite()?;
///
/// let unite = factory.create()?;
/// unite.set_intitule("Kilogramme")?;
/// unite.set_abreviation("kg")?;
/// unite.set_coef_conversion(1.0)?;
/// unite.write()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Unite {
    pub(crate) dispatch: IDispatch,
}

impl Unite {
    fn dispatch(&self) -> SafeDispatch<'_> {
        SafeDispatch::new(&self.dispatch)
    }

    // ═══════════════════════════════════════════════════════════════════════
    // PROPRIÉTÉS DE BASE (2 propriétés)
    // ═══════════════════════════════════════════════════════════════════════

    /// Identifiant unique (cbMarq)
    pub fn cbmarq(&self) -> SageResult<i32> {
        self.dispatch().call_method_by_name("cbMarq", &[])?.to_i32()
    }

    /// Intitulé de l'unité
    pub fn intitule(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("Intitule", &[])?
            .to_string()
    }

    // ═══════════════════════════════════════════════════════════════════════
    // PROPRIÉTÉS DE CONFIGURATION (5 propriétés)
    // ═══════════════════════════════════════════════════════════════════════

    /// Abréviation de l'unité (ex: "kg", "m", "L")
    pub fn abreviation(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("Abreviation", &[])?
            .to_string()
    }

    /// Coefficient de conversion (par rapport à l'unité de base)
    pub fn coef_conversion(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CoefConversion", &[])?
            .to_f64()
    }

    /// Unité de base (pour conversions)
    pub fn unite_base(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("UniteBase", &[])?
            .to_string()
    }

    /// Nombre de décimales pour affichage
    pub fn nb_decimales(&self) -> SageResult<i16> {
        self.dispatch()
            .call_method_by_name("NbDecimales", &[])?
            .to_short()
    }

    /// Type d'unité (0=Standard, 1=Poids, 2=Volume, 3=Longueur, 4=Surface)
    pub fn type_unite(&self) -> SageResult<i16> {
        self.dispatch()
            .call_method_by_name("TypeUnite", &[])?
            .to_short()
    }

    // ═══════════════════════════════════════════════════════════════════════
    // SETTERS (6 setters)
    // ═══════════════════════════════════════════════════════════════════════

    /// Modifie l'intitulé de l'unité
    pub fn set_intitule(&self, value: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(value);
        self.dispatch().call_property_put("Intitule", &[param])?;
        Ok(())
    }

    /// Modifie l'abréviation
    pub fn set_abreviation(&self, abrev: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(abrev);
        self.dispatch().call_property_put("Abreviation", &[param])?;
        Ok(())
    }

    /// Modifie le coefficient de conversion
    pub fn set_coef_conversion(&self, coef: f64) -> SageResult<()> {
        let param = SafeVariant::from_f64(coef);
        self.dispatch()
            .call_property_put("CoefConversion", &[param])?;
        Ok(())
    }

    /// Modifie l'unité de base
    pub fn set_unite_base(&self, unite: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(unite);
        self.dispatch().call_property_put("UniteBase", &[param])?;
        Ok(())
    }

    /// Modifie le nombre de décimales
    pub fn set_nb_decimales(&self, nb: i16) -> SageResult<()> {
        let param = SafeVariant::from_i16(nb);
        self.dispatch().call_property_put("NbDecimales", &[param])?;
        Ok(())
    }

    /// Modifie le type d'unité
    pub fn set_type_unite(&self, type_u: i16) -> SageResult<()> {
        let param = SafeVariant::from_i16(type_u);
        self.dispatch().call_property_put("TypeUnite", &[param])?;
        Ok(())
    }

    // ═══════════════════════════════════════════════════════════════════════
    // MÉTHODES STANDARD (3 méthodes)
    // ═══════════════════════════════════════════════════════════════════════

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

    /// Enregistre avec valeurs par défaut
    pub fn write_default(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("WriteDefault", &[])?;
        Ok(())
    }

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