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 produits/natures d'articles (IBSCIALProduit3)
///
/// Un produit définit la nature commerciale d'un article :
/// - Marchandise (achat/revente)
/// - Service
/// - Prestation
/// - Bien d'équipement
/// - etc.
///
/// # Exemple
/// ```no_run
/// use objets_metier_rs::wrappers::cial::CialApplication;
///
/// let cial_app = CialApplication::new("Objets100c.CIAL")?;
/// let factory = cial_app.factory_produit()?;
///
/// let produit = factory.create()?;
/// produit.set_intitule("Marchandise")?;
/// produit.set_type_produit(1)?;
/// produit.set_n_numero(1)?;
/// produit.write()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Produit {
    pub(crate) dispatch: IDispatch,
}

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

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

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

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

    /// Numéro du produit
    pub fn n_numero(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("nNumero", &[])?
            .to_i32()
    }

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

    /// Type de produit (0=Marchandise, 1=Service, 2=Bien d'équipement)
    pub fn type_produit(&self) -> SageResult<i16> {
        self.dispatch()
            .call_method_by_name("TypeProduit", &[])?
            .to_short()
    }

    /// Compte comptable vente
    pub fn compte_vente(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CompteVente", &[])?
            .to_string()
    }

    /// Compte comptable achat
    pub fn compte_achat(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CompteAchat", &[])?
            .to_string()
    }

    /// Compte comptable stock
    pub fn compte_stock(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CompteStock", &[])?
            .to_string()
    }

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

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

    /// Modifie le numéro du produit
    pub fn set_n_numero(&self, value: i32) -> SageResult<()> {
        let param = SafeVariant::I4(value);
        self.dispatch().call_property_put("nNumero", &[param])?;
        Ok(())
    }

    /// Modifie le type de produit
    pub fn set_type_produit(&self, type_p: i16) -> SageResult<()> {
        let param = SafeVariant::from_i16(type_p);
        self.dispatch().call_property_put("TypeProduit", &[param])?;
        Ok(())
    }

    /// Modifie le compte vente
    pub fn set_compte_vente(&self, compte: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(compte);
        self.dispatch().call_property_put("CompteVente", &[param])?;
        Ok(())
    }

    /// Modifie le compte achat
    pub fn set_compte_achat(&self, compte: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(compte);
        self.dispatch().call_property_put("CompteAchat", &[param])?;
        Ok(())
    }

    /// Modifie le compte stock
    pub fn set_compte_stock(&self, compte: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(compte);
        self.dispatch().call_property_put("CompteStock", &[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(())
    }
}