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 Compte Général de Sage 100c (IBOCompteG3)
#[derive(Debug)]
pub struct CompteG {
    pub dispatch: IDispatch,
}

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

    /// Récupère le numéro du compte général
    pub fn cg_num(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CG_Num", &[])?
            .to_string()
    }

    /// Récupère l'intitulé du compte général
    pub fn cg_intitule(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CG_Intitule", &[])?
            .to_string()
    }

    /// Récupère le type du compte général
    pub fn cg_type(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_Type", &[])?
            .to_i32()
    }

    /// Récupère le niveau du compte général
    pub fn cg_niveau(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_Niveau", &[])?
            .to_i32()
    }

    /// Récupère le sens du solde
    pub fn cg_sens_solde(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_SensSolde", &[])?
            .to_i32()
    }

    /// Récupère le solde initial
    pub fn cg_solde_initial(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CG_SoldeInitial", &[])?
            .to_f64()
    }

    /// Récupère le solde actuel
    pub fn cg_solde_actuel(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CG_SoldeActuel", &[])?
            .to_f64()
    }

    // Propriétés supplémentaires basées sur la documentation

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

    /// Récupère le compte de regroupement
    pub fn cg_compte_regroup(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CG_CompteRegroup", &[])?
            .to_string()
    }

    /// Récupère le code analytique
    pub fn cg_analytique(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_Analytique", &[])?
            .to_i32()
    }

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

    /// Récupère l'information sur l'interrogation tiers
    pub fn cg_interrogation_tiers(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_InterrogationTiers", &[])?
            .to_i32()
    }

    /// Récupère l'information sur le lettrage
    pub fn cg_lettrage(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("CG_Lettrage", &[])?
            .to_i32()
    }

    /// Récupère l'information sur la saisie analytique
    pub fn cg_saisie_analytique(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("CG_SaisieAnalytique", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Récupère l'information sur la saisie des échéances
    pub fn cg_saisie_echeance(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("CG_SaisieEcheance", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Récupère l'information sur la saisie des quantités
    pub fn cg_saisie_quantite(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("CG_SaisieQuantite", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Récupère le taux de taxe
    pub fn cg_taux_taxe(&self) -> SageResult<f64> {
        self.dispatch()
            .call_method_by_name("CG_TauxTaxe", &[])?
            .to_f64()
    }

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

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

    /// Récupère les informations de classification
    pub fn cg_classement(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("CG_Classement", &[])?
            .to_string()
    }

    // ==================== SETTERS POUR PROPRIÉTÉS ====================

    /// Définit le numéro du compte général
    /// Note: seulement pour les objets non persistants
    pub fn set_cg_num(&self, num: &str) -> SageResult<()> {
        let num_variant = SafeVariant::from_string(num);
        self.dispatch()
            .call_property_put("CG_Num", &[num_variant])?;
        Ok(())
    }

    /// Définit l'intitulé du compte
    pub fn set_cg_intitule(&self, intitule: &str) -> SageResult<()> {
        let intitule_variant = SafeVariant::from_string(intitule);
        self.dispatch()
            .call_property_put("CG_Intitule", &[intitule_variant])?;
        Ok(())
    }

    /// Définit le type du compte
    /// 0 = Détail, 1 = Total
    pub fn set_cg_type(&self, type_compte: i32) -> SageResult<()> {
        let type_variant = SafeVariant::from_i32(type_compte);
        self.dispatch()
            .call_property_put("CG_Type", &[type_variant])?;
        Ok(())
    }

    /// Définit le niveau du compte
    pub fn set_cg_niveau(&self, niveau: i32) -> SageResult<()> {
        let niveau_variant = SafeVariant::from_i32(niveau);
        self.dispatch()
            .call_property_put("CG_Niveau", &[niveau_variant])?;
        Ok(())
    }

    /// Définit le sens du solde
    /// 0 = Pas de sens, 1 = Débiteur, 2 = Créditeur
    pub fn set_cg_sens_solde(&self, sens: i32) -> SageResult<()> {
        let sens_variant = SafeVariant::from_i32(sens);
        self.dispatch()
            .call_property_put("CG_SensSolde", &[sens_variant])?;
        Ok(())
    }

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

    /// Définit le compte de regroupement
    pub fn set_cg_compte_regroup(&self, compte_regroup: &str) -> SageResult<()> {
        let compte_variant = SafeVariant::from_string(compte_regroup);
        self.dispatch()
            .call_property_put("CG_CompteRegroup", &[compte_variant])?;
        Ok(())
    }

    /// Définit le code analytique
    /// 0 = Aucun, 1 = Au débit, 2 = Au crédit, 3 = Toujours
    pub fn set_cg_analytique(&self, analytique: i32) -> SageResult<()> {
        let analytique_variant = SafeVariant::from_i32(analytique);
        self.dispatch()
            .call_property_put("CG_Analytique", &[analytique_variant])?;
        Ok(())
    }

    /// Définit le code journal analytique
    pub fn set_cg_journala(&self, journala: &str) -> SageResult<()> {
        let journala_variant = SafeVariant::from_string(journala);
        self.dispatch()
            .call_property_put("CG_JournalA", &[journala_variant])?;
        Ok(())
    }

    /// Définit l'interrogation tiers
    /// 0 = Aucun, 1 = Client, 2 = Fournisseur, 3 = Salarié
    pub fn set_cg_interrogation_tiers(&self, interrogation: i32) -> SageResult<()> {
        let interrogation_variant = SafeVariant::from_i32(interrogation);
        self.dispatch()
            .call_property_put("CG_InterrogationTiers", &[interrogation_variant])?;
        Ok(())
    }

    /// Définit le lettrage
    /// 0 = Interdit, 1 = Autorisé, 2 = Obligatoire
    pub fn set_cg_lettrage(&self, lettrage: i32) -> SageResult<()> {
        let lettrage_variant = SafeVariant::from_i32(lettrage);
        self.dispatch()
            .call_property_put("CG_Lettrage", &[lettrage_variant])?;
        Ok(())
    }

    /// Définit la saisie analytique
    pub fn set_cg_saisie_analytique(&self, saisie: bool) -> SageResult<()> {
        let saisie_variant = SafeVariant::from_bool(saisie);
        self.dispatch()
            .call_property_put("CG_SaisieAnalytique", &[saisie_variant])?;
        Ok(())
    }

    /// Définit la saisie des échéances
    pub fn set_cg_saisie_echeance(&self, saisie: bool) -> SageResult<()> {
        let saisie_variant = SafeVariant::from_bool(saisie);
        self.dispatch()
            .call_property_put("CG_SaisieEcheance", &[saisie_variant])?;
        Ok(())
    }

    /// Définit la saisie des quantités
    pub fn set_cg_saisie_quantite(&self, saisie: bool) -> SageResult<()> {
        let saisie_variant = SafeVariant::from_bool(saisie);
        self.dispatch()
            .call_property_put("CG_SaisieQuantite", &[saisie_variant])?;
        Ok(())
    }

    /// Définit le taux de taxe
    pub fn set_cg_taux_taxe(&self, taux: f64) -> SageResult<()> {
        let taux_variant = SafeVariant::R8(taux);
        self.dispatch()
            .call_property_put("CG_TauxTaxe", &[taux_variant])?;
        Ok(())
    }

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

    /// Définit le reporting
    pub fn set_cg_reporting(&self, reporting: &str) -> SageResult<()> {
        let reporting_variant = SafeVariant::from_string(reporting);
        self.dispatch()
            .call_property_put("CG_Reporting", &[reporting_variant])?;
        Ok(())
    }

    /// Définit le classement
    pub fn set_cg_classement(&self, classement: &str) -> SageResult<()> {
        let classement_variant = SafeVariant::from_string(classement);
        self.dispatch()
            .call_property_put("CG_Classement", &[classement_variant])?;
        Ok(())
    }

    // Méthodes communes héritées de IBIPersistObject

    /// Vérifie si l'objet est persistant
    pub fn is_persistant(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("IsPersistant", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Vérifie si l'objet a été modifié
    pub fn is_modified(&self) -> SageResult<bool> {
        let result = self
            .dispatch()
            .call_method_by_name("IsModified", &[])?
            .to_i32()?;
        Ok(result != 0)
    }

    /// Sauvegarde l'objet dans la base de données
    pub fn write(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Write", &[])?;
        Ok(())
    }

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

    /// Relit l'objet depuis la base de données
    pub fn read(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Read", &[])?;
        Ok(())
    }

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

    /// Initialise les valeurs par défaut
    pub fn set_default(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("SetDefault", &[])?;
        Ok(())
    }

    /// Rafraîchit l'objet si nécessaire
    pub fn refresh(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Refresh", &[])?;
        Ok(())
    }
}

// Implémentation de traits utiles
impl Clone for CompteG {
    fn clone(&self) -> Self {
        Self {
            dispatch: self.dispatch.clone(),
        }
    }
}

impl PartialEq for CompteG {
    fn eq(&self, other: &Self) -> bool {
        // Comparaison basée sur le numéro de compte
        match (self.cg_num(), other.cg_num()) {
            (Ok(num1), Ok(num2)) => num1 == num2,
            _ => false,
        }
    }
}

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