Thursday, January 10, 2013

Ax 2009 Item on hand

  while select sum(PostedQty),sum(Received),sum(Deducted), sum(Registered),sum(Picked) from inventSum group by itemId
            where inventSum.ItemId == _itemId && inventSum.Closed == NoYes::No
            join inventDim group by InventSiteId, InventLocationId,wMSLocationId where
                inventDim.inventDimId == inventSum.InventDimId
 {
  inventQtyPhysicalOnhand = inventSum.PostedQty + inventSum.Received - inventSum.Deducted + inventSum.Registered - inventSum.Picked;
  
 }

Ax 2012 inventory movement posting

static void hks_InventMovementJournalPosting(Args _args)
{
    InventJournalTable  inventJournalTable;
    InventJournalTrans  inventJournalTrans;
    InventJournalTableData  inventJournalTableData;
    InventJournalTransData  inventJournalTransData;
    InventTable             inventTable;
    InventDim               inventDim;
    InventJournalCheckPost  inventJournalCheckPost = new InventJournalCheckPost();
    container               ledgerDim;
    ;
    inventJournalTableData = JournalTableData::newTable(inventJournalTable);    
    
    //Init Journal table
    inventJournalTable.clear();
    inventJournalTable.initValue();
    inventJournalTable.JournalId = inventJournalTableData.nextJournalId();
    inventJournalTable.JournalType = InventJournalType::Movement;
    inventJournalTable.JournalNameId = inventJournalTableData.journalStatic().standardJournalNameId(inventJournalTable.JournalType);
    inventJournalTableData.initFromJournalName(inventJournalTableData.journalStatic().findJournalName(inventJournalTable.JournalNameId));
    inventJournalTable.insert();
    
    //Init JournalTrans
    inventJournalTransData = inventJournalTableData.journalStatic().newJournalTransData(inventJournalTrans,inventJournalTableData);
    select firstonly inventTable where inventTable.ItemId == '1000';
    inventJournalTrans.clear();
    inventJournalTrans.initFromInventJournalTable(inventJournalTable);
    inventJournalTrans.TransDate = systemdateget() + 1 div 2;   
    inventJournalTrans.initFromInventTable(inventTable, false, false, false);
    inventJournalTrans.Qty = 10;
    inventJournalTrans.CostAmount = 100;
    
    //Dimensions
    inventDim.InventLocationId = "21";
    inventDim.InventSiteId = InventLocation::find(inventDim.InventLocationId).InventSiteId;
    inventJournalTrans.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
    
    //set the offset account
    ledgerDim = ["Goodwill",'1110000'];
    inventJournalTrans.LedgerDimension =  AxdDimensionUtil::getLedgerAccountId(ledgerDim);
    
    inventJournalTransData.create();
    
    if (InventJournalCheckPost::newPostJournal(inventJournalTable).validate())
        InventJournalCheckPost::newPostJournal(inventJournalTable).run();
    
    info (inventJournalTable.JournalId);

    
    

}

Ax 2009 Get primary Address

 
//This method tries to get the primary address (Ex: customer address). 
//If not found, tries get the address based on the address Type (Ex: delivery address), 
//else tries to find the first available address.

Private Address address(DirPartyId _partyId, AddressType _addressType)
{
    Address         address;
    DirPartyTable   dirPartyTable;
    DirParty        party;
    ;

    while select  address
        join dirPartyTable
            where dirPartyTable.RecId == address.AddrRecId
            &&    dirPartyTable.TableId == address.AddrTableId
            &&    dirPartyTable.PartyId == _partyId
    {
        party = DirParty::constructFromCommon(dirPartyTable);
        party.initializeAddressInfo(DateTimeUtil::getSystemDateTime());
        if (party)
        {
            if (party.getDirPartyAddress().getAddressRecId()==address.RecId)
                return address;
        }
    }

    if (!address)
    {
        select firstonly address  where address.type == _addressType
            join recId from dirPartyTable
            where dirPartyTable.RecId == address.AddrRecId
            &&    dirPartyTable.TableId == address.AddrTableId
            &&    dirPartyTable.PartyId == _partyId;
    }
    if (!address)
        select firstonly address
            join recId from dirPartyTable
                where dirPartyTable.RecId == address.AddrRecId
                &&    dirPartyTable.TableId == address.AddrTableId
                &&    dirPartyTable.PartyId == _partyId;

    return address;
}

Ax 2009 Set Child Form access based on caller

 
//The following code is a generic code which can be used in scenario when based on certain condition of in main form, the associated forms should be view only.
Example if the item master is approved then the child forms should be only view only (default setting, transaction text etc) else should work as per standard Ax user permissions.

\Classes\SysSetupFormRun
public void init()
{
    this.AA_setCallerFormAccess();
}


void AA_setCallerFormAccess()
{
    FormDataSource formDataSource,formDataSourceChild;
    Object         formObject;
    Object         caller;
    FormRun        callerForm;
    Common         common;
    int            dsCnt;
    ;

    //info(this.args().menuItemName());    
    if (this.args().menuItemName()  != "InventTxt" &&
        this.args().menuItemName()  != "InventPackagingUnit" &&
        this.args().menuItemName()  != "PriceDiscTable_PricePurch" &&
        this.args().menuItemName()  != "PriceDiscTable_LineDiscPurch" &&
        this.args().menuItemName()  != "PriceDiscTable_MultiLineDiscPurch" &&
        this.args().menuItemName()  != "PriceDiscTable_EndDiscPurch" &&
        this.args().menuItemName()  != "SuppItemTable_Purch"
        )
            return;

    formDataSource = this.objectSet();
    if (formDataSource && 
        formDataSource.formRun().args().menuItemType() == MenuItemtype::Display)
    {
        caller = formDataSource.formRun().args().caller();
        if (!caller)
            return;
            
        callerForm = caller;
        if (callerForm.name() != formstr(InventTable))
            return;

        for (dsCnt =  1; dsCnt <= formDataSource.query().dataSourceCount(); dsCnt++)
        {
            formDataSourceChild = 
                         this.objectSet(formDataSource.query().dataSourceNo(dsCnt).name());
            if (formDataSourceChild)
            {
                formDataSourceChild.allowCreate(false);
                formDataSourceChild.allowDelete(false);
                formDataSourceChild.allowEdit(false);
            }
        }
    }
}

Ax 2009 Enable disable field controls

 
The following code can be used to enable all disable field controls in a group control.
Add the field to a container which needs to be enabled are disabled and call the generic function. In the below example, Name is a group control and 2 fields in this control needs to be enabled.

    //[TabPage:General]\[Group:Column1]\[Group:Name]
    Name.enabled(true);
    conItemChildNames = connull();
    conItemChildNames = ["Name_AA_MasterkeyType","Name_M_txtDefault"];
    element.AA_enableDisableFieldControls(Name,conItemChildNames,true);
    conItemChildNames = connull();

void AA_enableDisableFieldControls(FormGroupControl _formGroupControl, 
container _conChildNames, boolean _enableControl = false)
{
    FormGroupControl    formGroupControl;
    Object              fat;
    Object              child;
    Object              bchild;
    int                 controlCount;
    int                 j;
    ;


    formGroupControl = _formGroupControl;
    fat = form.design().control(formGroupControl.id());
    controlCount = fat.controlcount();
    for (j = 1; j <= controlCount; j++)
    {

        bchild  = fat.controlNum(j);
        child = element.design().control(bchild.id());
        if(!conFind(_conChildNames,child.name()))
        {
            child.enabled(_enableControl);
        }
    }
}

Ax 2012 LedgerJournalPosting

 
static void hks_LedgerJournalPosting(Args _args)
{
    LedgerJournalName          ledgerJournalName;
    LedgerJournalTable         ledgerJournalTable;
    LedgerJournalTrans         ledgerJournalTrans;
    NumberSeq                  numberSeq;
    LedgerJournalCheckPost     ledgerJournalCheckPost; 
    container                  ledgerDim;
;


    select ledgerJournalName where ledgerJournalName.JournalType == 
    LedgerJournalType::Daily && ledgerJournalName.JournalName == "GenJrn"; 

    ledgerJournalTable.JournalName = ledgerJournalName.JournalName; 
    ledgerJournalTable.initFromLedgerJournalName();
    ledgerJournalTable.insert(); 

    //NumberSequenceTable::find(ledgerJournalTable.NumberSequenceTable).NumberSequence
    numberSeq = numberSeq::newGetVoucherFromCode(NumberSequenceTable::find           
    (ledgerJournalName.NumberSequenceTable).NumberSequence, 
    NumberSeqScopeFactory::createDefaultScope(),true);

    ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum; 
    ledgerJournalTrans.AccountType = LedgerJournalACType::Ledger;
    ledgerDim = ["Goodwill","1110000"];
    ledgerJournalTrans.LedgerDimension = AxdDimensionUtil::getLedgerAccountId(ledgerDim);
    ledgerJournalTrans.CurrencyCode = "USD";
    ledgerJournalTrans.ExchRate = Currency::exchRate(ledgerJournalTrans.CurrencyCode);
    ledgerJournalTrans.AmountCurDebit = 10;
    ledgerJournalTrans.TransDate = today();
    ledgerJournalTrans.Txt = "hks test";
    ledgerDim = ["Petty Cash","110180"];
    ledgerJournalTrans.OffsetLedgerDimension = AxdDimensionUtil::getLedgerAccountId
                                               (ledgerDim);
    ledgerJournalTrans.Voucher = numberSeq.voucher();
    ledgerJournalTrans.insert();
    numberSeq.used();

    info(ledgerJournalTable.JournalNum);

    ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable
                             (ledgerJournalTable,true);
    ledgerJournalCheckPost.run();

}