Friday 14 April 2017

Purchase Requision through X++ in Ax 2012

Header

private PurchReqId createPurchReqHeader()
{
    PurchReqTable                   purchReqTable;
    ;

    ttsbegin;
    purchReqTable.clear();
    purchReqTable.initValue();
    purchReqTable.PurchReqId        = NumberSeq::newGetNum(PurchReqTable::numRefPurchReqId()).num();
    purchReqTable.PurchReqName      = description;
    purchReqTable.TransDate         = requestedDate;
    purchReqTable.Originator        = HcmWorker::findByPersonnelNumber(workerId).RecId;
    if(purchReqTable.validateWrite())
    {
        purchReqTable.insert();
    }
    ttsCommit;

    return purchReqTable.PurchReqId;

}


Lines

private void createPurchReqLines(PurchReqId                      _purchReqId,
                                 MaterialIndentParmLines     _materialIndentParmLine)
{
    PurchReqLine                        purchReqLine;
    InventDim                           inventDim;
    EcoResCategory                      category;
    PurchReqLineTaxExtensionIN          purchReqLineTaxExtensionIN;
    MaterialIndentTransTable        materialIndentTranstable;
    MaterialIndentLine              materialIndentLine;
    RecId                               recId;
    ;

    // Multi lines created
    purchReqLine.clear();
    purchReqLine.initValue();

    inventdim.InventSiteId              =   dfSite.value();
    inventdim.InventLocationId          =   dfWarehouse.value();
    purchReqLine.InventDimId            =   InventDim::findOrCreate(InventDim).InventDimId;
    purchReqLine.initFromPurchReqTable(PurchReqTable::find(_purchReqId));

    purchReqLine.ItemId                 = _materialIndentParmLine.ItemId;
    purchReqLine.BuyingLegalEntity      = CompanyInfo::find().RecId;
    purchReqLine.InventDimIdDataArea    = curext();

    select * from    category
        where category.Name == _materialIndentParmLine.ProcurementCategory;
    purchReqLine.ProcurementCategory    =  category.RecId;
    purchReqLine.Requisitioner          = HcmWorker::findByPersonnelNumber(workerId).RecId;
    purchReqLine.Name                   = _materialIndentParmLine.ItemName;
    purchReqLine.PurchQty               = _materialIndentParmLine.Qty;
    purchReqLine.PurchUnitOfMeasure     = UnitOfMeasure::findBySymbol(_materialIndentParmLine.UnitOfMeasureSymbol).RecId;
    purchReqLine.CurrencyCode           = CompanyInfo::standardCurrency();
    purchReqLine.TransDate              = today();
    //purchReqLine.initTransDate();
    purchReqLine.initFromEcoResCategory(category.RecId);
    purchReqLine.RequiredDate           = today();
    purchReqLine.PriceUnit              = 1;
    purchReqLine.ItemIdDataArea         = curext();
    purchReqLine.PurchReqTable           = PurchReqTable::findPurchReqId(_purchReqId).RecId;
    purchReqLine.editItemName(true,_materialIndentParmLine.ItemName);

    if(purchReqLine.validateWrite())
    {
        //purchReqLine.insert();
        purchReqLine.createLine(true, true);
        if(SysCountryRegionCode::isLegalEntityInCountryRegion([#isoIN]))
        {
            ttsBegin;
            purchReqLineTaxExtensionIN.selectForUpdate(true);
            purchReqLineTaxExtensionIN.TaxModelDocLineExtensionIN::init(purchReqLine);
            purchReqLineTaxExtensionIN.insert();


            _materialIndentparmline.selectForUpdate(true);
            _materialIndentparmline.MaterialIndentLineStatus    = MaterialIndentLineStatus::PR;
            _materialIndentparmline.update();

            // Updates remaining qty to material indent line and changes the status to submitted if qty = 0
            select forUpdate materialIndentLine
                where materialIndentLine.RecId == _materialIndentparmline.MaterialIndentLinesRecId;

            materialIndentLine.Qty  = _materialIndentparmline.RemainingQty;
            if(_materialIndentparmline.RemainingQty == 0)
            {
                materialIndentLine.MaterialIndentLineStatus = MaterialIndentHeaderStatus::Completed;
            }
            materialIndentLine.update();

            //inserts record in MaterialIndentTrans

            recId = materialIndentTranstable.findbyparmIdMaterialIndentId(_materialIndentparmline.MaterialIndentparmTable, _materialIndentparmline.MaterialIndentId).RecId;
            if(!recId)
            {
                recId = this.insertTransTable(_materialIndentparmline, _purchReqId);
            }
            this.insertTransLines(_materialIndentparmline, recId);
            materialIndentId = _materialIndentparmline.MaterialIndentId;
            }
            ttsCommit;
        info(strFmt("Purchase requisition is created - %1 for an item - %2.", _purchReqId, purchReqLine.ItemId),"", SysInfoAction_Formrun::newFormname(formStr(PurchReqTableListPage)));

    }
}

No comments:

Post a Comment