Tuesday, 18 April 2017

Due Date calculation using x++ code in Ax 2012 R3

Due Date Calculation
Customers
//Added by Rajendra - Due date calculation on 18-04-2017
public TransDate HM_DueDate()
{
    ProjInvoiceJour   ProjInvoiceJour;
    str s;
   
    select ProjInvoiceJour where ProjInvoiceJour.ProjInvoiceId == custTrans.Invoice;
    if(ProjInvoiceJour.RecId)
    {
        s = strFmt('%1',ProjInvoiceJour.InvoiceDate + PaymTerm::find(custTable::find(custTrans.AccountNum).PaymTermId).NumOfDays);
    }
    else
    {
        if(custTrans.DocumentDate)
        {
            s = strFmt('%1',custTrans.DocumentDate + PaymTerm::find(custTable::find(custTrans.AccountNum).PaymTermId).NumOfDays);
        }
        else
        {
            s = strFmt('%1',datenull());
        }
    }
    return str2Date(s,213);
}


In General Journal due date calculation

ledgerJournalTrans.Payment      = termsofPayment;
if(ledgerJournalTrans.Payment)
{
   ledgerJournalTrans.Due = ledgerJournalEngine.getDueDateBaseDate(ledgerJournalTrans);
   ledgerJournalTrans.Due = PaymCalendarSourceLedgerJournalTrans::getUpdatedDueDate(ledgerJournalTrans,this.paymDayId(ledgerJournalTrans));
}

public PaymDayId paymDayId(LedgerJournalTrans _ledgerJournalTrans)
{
    PaymDayId   paymDayId;
    CompanyId   companyId;
    LedgerJournalEngine ledgerJournalEngine = new ledgerJournalEngine();
    ;

    if (xDataArea::exist(_ledgerJournalTrans.Company))
    {
        companyId =_ledgerJournalTrans.Company;
    }
    else
    {
        companyId = curext();
    }

    changecompany(companyId)
    {
        switch(_ledgerJournalTrans.AccountType)
        {
            case LedgerJournalACType::Cust :
                paymDayId = ledgerJournalEngine.findCustTable(_ledgerJournalTrans).PaymDayId;
                break;

            case LedgerJournalACType::Vend :
                paymDayId = _ledgerJournalTrans.findVendTable().PaymDayId;
                break;

            default :
                paymDayId = '';
        }
    }
    return paymDayId;

}

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)));

    }
}