Wednesday, June 14, 2023

Custom class to handle financial Dimensions using x++

 Custom class to handle financial Dimensions using x++ 


/// <summary>
/// Custom class to handle dimensions
/// </summary>
class DimensionHelper
{
    /// To Get the default dimension value by using default dimension id
    public static DimensionValue getDefaultDimensionValue(str dimName, 
                                                        DimensionDefault defaultDimension)
    {
        DimensionAttributeValueSetStorage   dimStorage;
        Counter                             i;
        str                                 returnValue;

        dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimension);

        for (i=1 ; i<= dimStorage.elements() ; i++)
        {
            if (DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == _dimName)
            {
                returnValue = dimStorage.getDisplayValueByIndex(i);
                break;
            }
        }
        return returnValue;
    }

    /// Static method to get default dimension
    public static DimensionDefault getDefaultDim(container conAttr, container conValue)
    {
        DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();

        int                     i;
        DimensionAttribute      dimensionAttribute;
        DimensionAttributeValue dimensionAttributeValue;
        str                     dimValue;

        if((conLen(conAttr) <= 0) || (conLen(conValue) <= 0))
        {
            throw error("@SYS313886");
        }

        for (i = 1; i <= conLen(conAttr); i++)
        {
            dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));
            
            if (dimensionAttribute.RecId == 0)
            {
                continue;
            }
            
            dimValue = conPeek(conValue,i);
            
            if (dimValue != "")
            {
                dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(
                                                                    dimensionAttribute, dimValue, false, true);
                valueSetStorage.addItem(dimensionAttributeValue);
            }
        }

        return valueSetStorage.save();
    }

    /// Static method to get default dimension
    public static DimensionDynamicAccount createLedgerDimFromMainAccount(container conAttr, container conValue, 
                                                                            Name mainAccount, LedgerJournalACType accountType) 
    {
        DimensionDefault        dimensionDefault;
        DimensionDynamicAccount dimensionDynamicAccount;
        LedgerDimensionBase     ledgerDimBase;
        LedgerDimensionAccount  ledgerDim;

        dimensionDefault        = DimensionHelper::getDefaultDim(conAttr, conValue);
        dimensionDynamicAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(mainAccount, accountType);
        ledgerDim               = LedgerDimensionFacade::serviceCreateLedgerDimension(dimensionDynamicAccount);

        ledgerDimBase = LedgerDimensionFacade::serviceCreateLedgerDimForDefaultDim(dimensionDefault, ledgerDim);

        return ledgerDimBase;
    }

    /// Generate ledger dimension 
    public DimensionDynamicAccount generateLedgerDimension(container    conData, MainAccountNum  mainAccount)
    {
        int                                 hierarchyCount;
        int                                 hierarchyIdx;
        LedgerRecId                         ledgerRecId;
        MainAccount                         mainAccount;
        RefRecId                            recordvalue;
        DimensionAttribute                  dimensionAttribute;
        DimensionAttributeValue             dimensionAttributeValue;
        DimensionSetSegmentName             DimensionSet;
        DimensionStorage                    dimStorage;
        LedgerAccountContract               LedgerAccountContract = new LedgerAccountContract();
        dimensionAttributeValueCombination  dimensionAttributeValueCombination;
        DimensionAttributeValueContract     ValueContract;
        List                                valueContracts;
        
        valueContracts          = new List(Types::Class);
        mainAccount             = MainAccount::findByMainAccountId(_mainAccount);
        recordvalue             = DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
        hierarchyCount          = DimensionHierarchy::getLevelCount(recordvalue);
        DimensionSet            = DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);

        for(hierarchyIdx = 1; hierarchyIdx <= hierarchyCount; hierarchyIdx++)
        {
            if (hierarchyIdx == 1)
            {
                continue;
            }

            dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx], 
                                                                            false, SystemParameters::getSystemLanguageId());
            if (dimensionAttribute)
            {
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,
                                                                                            conPeek(_conData,hierarchyIdx));
                if (dimensionAttributeValue)
                {
                    ValueContract = new DimensionAttributeValueContract();
                    ValueContract.parmName(dimensionAttribute.Name) ;
                    ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                    valueContracts.addEnd(ValueContract);
                }
            }
        }

        LedgerAccountContract.parmMainAccount(_mainAccount);
        LedgerAccountContract.parmValues(valueContracts);

        dimStorage                          = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination  = DimensionAttributeValueCombination::find(dimStorage.save());
        ledgerRecId                         = dimensionAttributeValueCombination.RecId;

        return ledgerRecId;
    }
}


Keep Daxing!!

No comments:

Post a Comment