Wednesday, November 30, 2016

Get Aging bucket in ax 2012

//Get Aging bucket in ax 2012

static void Fcc_CheckBucketSplitUp(Args _args)
{
    CustVendReportName       _agingPeriodDefinition = "3";
    TransDate                _zeroDate = today();
    QueryRun                    queryRun;
    QueryBuildDataSource        qbds;
    TmpStatPer                  tmpStat;
    TransDate                   datePointer;
    StatRepIntervalLine         statRepIntervalLine;
    StatRepInterval             statRepInterval;
    RecordInsertList            recordInsertList;
    int                         bucket;
    ForwardBackwardPrinting     direction;
    Fcc_inventoryAgingPeriodScaleTmp    agingPeriodTmp;
    int i;
    Query                       query = new Query();
    TmpStatPer                  tmpStatPer = TmpStatPer::createDefName(_agingPeriodDefinition,_zeroDate); // Need to modify if aging bucket table is customize means..Table TmpStatPer Method CreateDefName for Newly created table

    void addLine(TransDate _transDate, str _description,  ItemAgingIndicator _indicator, int _bucketNumber)
    {
        agingPeriodTmp.TransDate            = _transDate;
        agingPeriodTmp.BucketDescription    = _description;
        agingPeriodTmp.AgingIndicator       = _indicator;
        agingPeriodTmp.BucketNumber         = _bucketNumber;
        recordInsertList.add(agingPeriodTmp);
    }
    delete_from agingPeriodTmp;
    direction = (select PrintDirection from statRepInterval where statRepInterval.Name == _agingPeriodDefinition).PrintDirection;
    qbds = query.addDataSource(tableNum(TmpStatPer));
    recordInsertList = new RecordInsertList(tableNum(Fcc_InventoryAgingPeriodScaleTmp), true, true, true, false, true, agingPeriodTmp);
    switch (direction)
    {
        case ForwardBackwardPrinting::Forward:
            qbds.addSortField(fieldNum(TmpStatPer, StartDate), SortOrder::Ascending);
            bucket = 1;
            break;
        case ForwardBackwardPrinting::Backward:
            qbds.addSortField(fieldNum(TmpStatPer, StartDate), SortOrder::Descending);
            bucket = int642int((select count(RecId) from statRepIntervalLine where statRepIntervalLine.Name == _agingPeriodDefinition).RecId);
            break;
    }
    queryRun = new QueryRun(query);
    queryRun.setCursor(tmpStatPer);
    while (queryRun.next())
    {
        tmpStat = queryRun.getNo(1) as TmpStatPer;
        if (tmpStat.StartDate == dateNull())
        {
            addLine(tmpStat.StartDate, tmpStat.Description, tmpStat.InvoiceQty, bucket);
            addLine(tmpStat.EndDate, tmpStat.Description, tmpStat.InvoiceQty, bucket);
        }
        if (tmpStat.StartDate != dateNull() && tmpStat.EndDate != dateMax())
        {
            for (datePointer = tmpStat.StartDate; datePointer <= tmpStat.EndDate; datePointer++)
            {
                // means that if we order buckets in aging period definition ASC from the past to future - each bucket will have a corresponding int, to be used afterwards as bucket number.
                addLine(datePointer, tmpStat.Description, tmpStat.InvoiceQty, bucket);
            }
        }
        if (tmpStat.EndDate == dateMax())
        {
            addLine(tmpStat.StartDate, tmpStat.Description, tmpStat.InvoiceQty, bucket);
            addLine(tmpStat.EndDate, tmpStat.Description, tmpStat.InvoiceQty, bucket);
        }
        if (direction == ForwardBackwardPrinting::Forward)
        {
            bucket++;
        }
        else
        {
            bucket--;
        }
    }
    recordInsertList.insertDatabase();
    //Deletion and updation of aging bucket
    ttsBegin;
    for(i=1;i<=6;i++)
    {
        agingPeriodTmp.clear();
        select forUpdate agingPeriodTmp order by RecId Asc
            where agingPeriodTmp.BucketNumber == i;
        if(agingPeriodTmp)
        {
            agingPeriodTmp.Updated = NoYes::Yes;
            agingPeriodTmp.Update();
        }
        agingPeriodTmp.clear();
        select forUpdate agingPeriodTmp order by RecId Desc
            where agingPeriodTmp.BucketNumber == i;
        if(agingPeriodTmp)
        {
            agingPeriodTmp.Updated = NoYes::Yes;
            agingPeriodTmp.Update();
        }
    }
    delete_from agingPeriodTmp where agingPeriodTmp.Updated == NoYes::No;
    ttsCommit;
}

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...