martes, 21 de agosto de 2012

That company container you have always been looking for


// Inner function to return a list of non virtual companies
// --------------------------------------------------------
container companyContainer(NoYes _includeVirtual = NoYes::No)
{
    DataArea            dataarea;
    container           retVal;
    ;
    while select id, isVirtual from dataarea index hint id
    {
        if (_includeVirtual == NoYes::No && dataarea.isVirtual == NoYes::Yes)
        {
            continue;
        }
        retVal += dataarea.id;
    }
    return retVal;
}

I have used the above inner function in two Jobs now, placed in the function declaration section.  Example usage:
static void tutorial_Job_companyCurrency(Args _args)
{
    container               conComps,
                            conComp;
    Counter                 cntComps;
    DataAreaId              dataareaId;
    CurrencyCode            currencyCompany;

    container companyContainer(NoYes _includeVirtual = NoYes::No)
    {
        DataArea            dataarea;
        container           retVal;
        ;
        while select id, isVirtual from dataarea index hint id
        {
            if (_includeVirtual == NoYes::No && dataarea.isVirtual == NoYes::Yes)
            {
                continue;
            }
            retVal += dataarea.id;
        }
        return retVal;
    }
    ;

    conComps        = companyContainer();

    for (cntComps=1; cntComps <= conlen(conComps); cntComps++)
    {
        dataareaId  = conpeek(conComps, cntComps);
        conComp     = [dataareaId];

        currencyCompany     = (select crosscompany : conComp CompanyInfo).CurrencyCode;
        warning(strfmt("Company: %1; Currency: %2", dataareaId, currencyCompany));
    }
}

Finally, below is an example when the customers table is shared across companies, using virtual companies in the query.
    DataArea            dataArea;
    VirtualDataAreaList virtualDataAreaList, 
                        virtualDataAreaListNow;
    ;
    select virtualDataAreaListNow
        where virtualDataAreaListNow.id == curext();

    while select dataArea
        where dataArea.isVirtual == false
//            && dataArea.Id != curext()
        join virtualDataAreaList
            where virtualDataAreaList.id == dataArea.id
            &&    virtualDataAreaList.virtualDataArea == virtualDataAreaListNow.virtualDataArea
    {
        changecompany(dataArea.Id)
        {
            info(strFmt('%1: %2', dataArea.Id, CustTable::find('CL000000').openBalanceMST()));
        }
    }

Let's put that in a tidy container:
static void BigJob(Args _args)
    container                   conCompanys;

    container companyContainer()
    {
        DataArea            dataarea;
        container           retVal;
        VirtualDataAreaList virtualDataAreaList,
                            virtualDataAreaListNow;
        ;
        select virtualDataAreaListNow
            where virtualDataAreaListNow.id == curext();

        while select dataArea
            where dataArea.isVirtual == false
            join virtualDataAreaList
                where virtualDataAreaList.id == dataArea.id
                &&    virtualDataAreaList.virtualDataArea == virtualDataAreaListNow.virtualDataArea
        {
            retVal += dataarea.id;
        }
        return retVal;
    }
    ;
    conCompanys = companyContainer();
    // Do work
}

No hay comentarios:

Publicar un comentario