DSA Utill d365fo x++

DSA Utill d365fo x++

Code:

class DSAUtil
{


    

    public static Common findRecordByValue(TableId _tableId, FieldId _fieldId, anytype _value, boolean _forUpdate = false)
    {
        Common      common;
        DictTable   dictTable;
        ;
        dictTable = new DictTable(_tableId);
        common = dictTable.makeRecord();
 
        common.selectForUpdate(_forUpdate);
 
        select common
        where common.(_fieldId) == _value;
 
        return common;
    }

    public static void refreshCaller(FormRun _fRun)
    {
        FormRun callerForm;
        ;

        callerForm = _fRun.args().caller();

        if(callerForm)
        {
            DSAUtil::refreshForm(callerForm);
        }
    }

    public static void refreshForm(FormRun _fRun)
    {
        FormDataSource fds;
        ;

        fds = _fRun.dataSource();

        DSAUtil::refreshDataSource(fds);
    }

    public static void refreshDataSource(FormDataSource _fds)
    {
        FormDataSource fds;
        ;

        fds = _fds;

        if(fds)
        {
            fds.refresh();
            fds.reread();
            fds.research(true);
        }
    }

    public static void showWarning(boolean _show, str _warning)
    {
        if(_show)
            warning(_warning);
    }

    public static void showError(boolean _show, str _error)
    {
        if(_show)
            error(_error);
    }

    public static void throwError(boolean _throw, str _error)
    {
        if(_throw)
            throw error(_error);
    }

    public static void allowEditFieldsOnFormDS(FormDataSource _dataSource)
    {
        var dictTable = new DictTable(_dataSource.table());
        int         cx, idx;

        for (cx = 1; cx <= dictTable.fieldCnt(); cx ++)
        {
            DictField dictField = dictTable.fieldObject(dictTable.fieldCnt2Id(cx));

            if (! dictField.isSystem())
            {
                for (idx = 1; idx <= dictField.arraySize(); idx++)
                {
                    FormDataObject dataObject = _dataSource.object(fieldId2Ext(dictField.id(), idx));
                    if (dataObject)
                    {
                        switch(_dataSource.table())
                        {
                            case tableNum(SalesTable):
                            {
                                if(dictField.id() == fieldNum(SalesTable, ShippingDateConfirmed) || dictField.id() == fieldNum(SalesTable, ReceiptDateConfirmed))
                                {
                                    dataObject.allowEdit(true);
                                }
                                else
                                {
                                    dataObject.allowEdit(false);
                                }
                                break;
                            }
                        case tableNum(SalesLine):
                            if(dictField.id() == fieldNum(SalesLine, ShippingDateConfirmed) || dictField.id() == fieldNum(SalesLine, ShippingDateConfirmed))
                            {
                                dataObject.allowEdit(true);
                            }
                            else
                            {
                                dataObject.allowEdit(false);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    public static InventQtyAvailPhysical availPhysical(InventSum _inventSum, FormDataSource _inventDim_ds)
    {
        InventAvailabilityFormCache inventAvailabilityFormCache;
        inventAvailabilityFormCache = InventAvailabilityFormCache::construct();
        return inventAvailabilityFormCache.availabilityFromSumJoinedWithDim(_inventSum, _inventDim_ds).availPhysical();
    }

    // Find on hand inventory given location and item
    public str onHandInventory()
    {
        InventSum                   inventSum;
        InventSum                   locFinder;
        InventDim                   inventDim;
        InventDim                   inventDimCriteria;
        InventDimParm               inventDimParmCriteria;
        InventDimParm               inventDimParmGroupBy;
        Query                       q;
        QueryBuildDataSource        qbds, inventSumQbds, invetbatchQbds;
        QueryRun                    qr;
        str                         locations;
        int                         counter = 0;

        inventDimParmGroupBy.ItemIdFlag = true;  // group by item
        //inventDimParmGroupBy.WMSLocationIdFlag = true; // group by location
        //inventDimParmGroupBy.InventLocationIdFlag = true;

        q = InventSum::newQuery(q, '', inventDimCriteria, inventDimParmCriteria, inventDimParmGroupBy);

        qbds = q.dataSourceTable(tableNum(InventSum));

        qbds.addRange(fieldNum(InventSum, AvailPhysical)).value(SysQuery::valueNot(0));
        //qbds.addRange(fieldNum(InventSum, ItemId)).value(_itemId);

 

        /*if (_site)
        {
            qbds.addRange(fieldNum(InventSum, InventSiteId)).value(_site);
        }*/

 

        qr = new QueryRun(q);

        // Build string used to show inventory at each location, limited to 3 lines
        //while (qr.next() && counter < 3)
        // Now limit is not required
        while (qr.next())
        {
            inventSum = qr.get(tableNum(InventSum));
            inventDim = qr.get(tableNum(InventDim));
            inventSumQbds   =   q.dataSourceTable(tableNum(InventSum));

            invetbatchQbds = inventSumQbds.addDataSource(tableNum(InventBatch));
            invetbatchQbds.addLink(fieldNum(InventSum, InventBatchId), fieldNum(inventBatch, InventBatchId));
            invetbatchQbds.joinMode(JoinMode::InnerJoin);

            invetbatchQbds.addSortField(fieldNum(inventBatch,ItemId));
            invetbatchQbds.orderMode(OrderMode::GroupBy);
            invetbatchQbds.addSortField(fieldNum(inventBatch, inventBatchId));
            invetbatchQbds.orderMode(OrderMode::GroupBy);

            //select firstonly1 InventLocationId, wmsLocationId
            //    from locFinder
            //    where locFinder.ItemId == _itemId
            //    &&    locFinder.AvailPhysical == inventSum.AvailPhysical
            //    &&    locFinder.InventLocationId != ""
            //    &&    locFinder.wmsLocationId == inventDim.wMSLocationId
            //    &&    locFinder.InventSiteId == _site;

            locations += strFmt("@DSALabel:DSA001470", inventDim.InventLocationId, inventDim.wmsLocationId, inventSum.AvailPhysical);
            counter++;

        }

        info(locations);

        return locations;
    }

    /// <summary>
    /// Converts amount from accounting currency to given currency
    /// </summary>
    /// <param name = "_toCurrency">Currency to be converted into</param>
    /// <param name = "fromAmount">The amount to be converted</param>
    /// <returns>Converted amount</returns>
    public static AmountMST getExchangeRateValue(CurrencyCode _toCurrency, AmountCur fromAmount)
    {
        CurrencyCode            fromCurrency;
        AmountMST               exchangeRateAmount;
        CurrencyExchangeHelper  CurrencyExchangeHelper;

        fromCurrency = Ledger::find(Ledger::current()).AccountingCurrency;

        if (fromCurrency == _toCurrency)
        {
            return fromAmount;
        }

        currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());

        exchangeRateAmount = currencyExchangeHelper.calculateCurrencyToCurrency(fromCurrency, _toCurrency, fromAmount, true);

        return exchangeRateAmount;
    }

    public static str getItemDescriptionForReports(ItemId  _itemId, InventDimId _inventDimId, DSAOrderType _type, str headerID)
    {
        str                         concatenatedStr;
        InventTable                 inventTable = inventTable::find(_itemId);
        SalesTable                  salesTable;
        CompanyInfo                 compInfo = CompanyInfo::find();
        PurchTable                  purchTable;
        ProjTable                   projTable;
        InventDim                   inventDim = inventDim::find(_inventDimId);
        EcoResProduct               ecoResProduct = ecoResProduct::find(inventTable.Product);
        EcoResProductName           ecoResProductName = EcoResProductTranslation::getNameOrDefaultName(ecoResProduct.RecId, SystemParameters::getSystemLanguageId());
        EcoResProductTranslation    ecoResProductTransalation;
        //inventTable.productDescription(CompanyInfo::find().LanguageId, _inventDimId);
        InventDimCombination        inventDimCombination;
        CustVendExternalItem        custVendExternalItem;
        ;
        select * from ecoResProductTransalation where ecoResProductTransalation.Product == ecoResProduct.RecId;


        switch (_type)
        {
            case   DSAOrderType::Sales:
                {
                    salesTable  =   salesTable::find(headerID);

                    select * from custVendExternalItem where custVendExternalItem.ItemId == _itemId
                    && custVendExternalItem.CustVendRelation == salesTable.CustAccount
                    && custVendExternalItem.ModuleType == ModuleInventPurchSalesVendCustGroup::Cust;

                    //concatenatedStr = inventTable.NameAlias + '\n ' + ecoResProductName + '\n ' +
                    //    ecoResProduct.productDescription(SystemParameters::getSystemLanguageId()) + '\n '
                    //    + inventTable.ZAArabicDescription + '\n ' 
                    //    + custVendExternalItem.ExternalItemTxt;
                    concatenatedStr = inventDim.configId + '\n' + inventDim.InventStyleId + '\n' +
                                      ecoResProductName + '\n' +
                                      inventTable.DSAArabicDescription + '\n' +
                                      ecoResProduct.productDescription(SystemParameters::getSystemLanguageId()) + '\n' +
                                      custVendExternalItem.ExternalItemTxt;
                }
            case    DSAOrderType::Purchase:
            {
                purchTable  =   PurchTable::find(headerID);

                select * from custVendExternalItem where custVendExternalItem.ItemId == _itemId
                    && custVendExternalItem.CustVendRelation == purchTable.OrderAccount
                        && custVendExternalItem.ModuleType == ModuleInventPurchSalesVendCustGroup::Vend;

                concatenatedStr = inventDim.configId + '\n' + inventDim.InventStyleId + '\n' +
                                  ecoResProductName + '\n' +
                                  inventTable.DSAArabicDescription + '\n' +
                                  ecoResProduct.productDescription(SystemParameters::getSystemLanguageId()) + '\n' +
                                  custVendExternalItem.ExternalItemTxt;
            }
        }

        return concatenatedStr;
    }

    public static Query lookupQuery(str _securityRoleAotName, RefRecId _hcmWorkerRecID)
    {
        HcmWorker                       hcmWorker = HcmWorker::find(_hcmWorkerRecID);
        DSATeamMemberSecurityRoleMA     teamMemberSecurityRole;
        DirPartyRelationship            dirPartyRelationShip;
        OMTeam                          OmTeam;
        CustParameters                  custParameters = CustParameters::find();
        //SecurityUserRole                securityUserRole;
        //SecurityRole                    securityRole;
        Query                           query;
        QueryBuildDataSource            qbds;
        QueryBuildRange                 qbr;
        QueryBuildDataSource            qbdsJoin;
        QueryBuildDataSource            qbdsJoin1;
        QueryBuildDataSource            qbdsJoin2;
        QueryBuildDataSource            qbdsJoin3;
        str                             range;

        if(hcmWorker)
        {
            query = new Query();
            qbds = query.addDataSource(tableNum(hcmWorker));
            qbdsJoin = qbds.addDataSource(tableNum(DirPartyRelationship));
            qbdsJoin1 = qbdsJoin.addDataSource(tableNum(OMTeam));
            qbdsJoin2 = qbdsJoin.addDataSource(tableNum(DSATeamMemberSecurityRoleMA));
            qbdsJoin3 = qbdsJoin2.addDataSource(tableNum(SecurityRole));

            qbdsJoin.addLink(fieldNum(hcmWorker, Person), fieldNum(DirPartyRelationship, ChildParty));
            qbdsJoin1.addLink(fieldNum(DirPartyRelationship, ParentParty), fieldNum(OMTeam, RecId));
            qbdsJoin2.addLink(fieldNum(DirPartyRelationship, RecId), fieldNum(DSATeamMemberSecurityRoleMA, DirPartyRelationShipRefRec));
            qbdsJoin3.addLink(fieldNum(DSATeamMemberSecurityRoleMA, SecurityRoleRefRec), fieldNum(SecurityRole, Recid));
        

            qbds.addRange(fieldNum(HcmWorker, RecId)).value(queryValue(_hcmWorkerRecID));
            qbdsJoin3.addRange(fieldNum(SecurityRole,AotName)).value(queryValue(_securityRoleAotName));
            qbdsJoin1.addRange(fieldNum(OMTeam, TeamMembershipCriterion)).value(queryValue(CustParameters.DSATeamType));

            qbds.joinMode(JoinMode::ExistsJoin);
            qbdsJoin.joinMode(JoinMode::ExistsJoin);
            qbdsJoin1.joinMode(JoinMode::ExistsJoin);
            qbdsJoin2.joinMode(JoinMode::ExistsJoin);
            qbdsJoin3.joinMode(JoinMode::ExistsJoin);
        }
        return query;
    }

}

Comments

Popular posts from this blog

D365FO – AX – X++ –Refresh, Reread, Research, and ExecuteQuery

Create Inventory Journal through Code in D365FO X++

SalesLine Reservation in D365fo x++