Thursday, April 21, 2016

Class to Create Item Group include Main Account in AX 2012 R3

CreateInventGroup::Create(itemGroupx,itemGroupNamex,inventoryAccx,expenseAccx,revenueAccx,interUnitAccx);

==============

class CreateInventGroup
{
}


public static void Create(
ItemGroupId _itemGroupId,
Name _itemGroupName,
MainAccountNum _inventoryAcc,
MainAccountNum _expenseyAcc,
MainAccountNum _revenueAcc,
MainAccountNum _interUnitAcc,
MainAccountNum _wipAcc=''
)
{
    InventPosting                       _inventPosting;
    InventItemGroup                     _itemGroup0,_itemGroup;
    InventItemGroupForm                 _itemGroupForm;

    InventAccountType                   inventAcctTypeEnum;
    InventPostingItemCode               inventItemCodeEnum;
    container                           conCOAInventory,conCOAExpense,conCOARevenue,conCOAInterunit,conCOAWIP;
    ItemGroupId                         itemGroupx;
    Name                                itemGroupNamex,invttypex;
    MainAccountNum                      inventoryAccx,expenseAccx,revenueAccx,interUnitAccx,wipAccx;
    int                                 i,counterx;
    ;

    itemGroupx                          = _itemGroupId;
    itemGroupNamex                      = _itemGroupName;
    inventoryAccx                       = _inventoryAcc;
    expenseAccx                         = _expenseyAcc;
    revenueAccx                         = _revenueAcc;
    interUnitAccx                       = _interUnitAcc;
    wipAccx                             = _wipAcc;

    conCOAInventory     =  ['Inventory, issue',
                            'Inventory, receipt',
                            'Product receipt',
                            'Production, issue',
                            'Production, picking list',
                            'Production, receipt',
                            'Production, report as finished',
                            'Purchase, inventory receipt',
                            'Sales order, issue',
                            'Order, packing slip'];

    conCOAExpense       =  ['Inventory, loss',
                            'Inventory, profit',
                            'Purchase expenditure for expense',
                            'Purchase expenditure for product',
                            'Sales order, consumption',
                            'Sales, packing slip offset'];

    conCOARevenue       =  ['Sales order, revenue'];

    conCOAInterunit     =  ['Inventory inter-unit payable','Inventory inter-unit receivable'];

    conCOAWIP           = [ 'Production, issue offset',
                            'Production, picking list offset',
                            'Production, receipt offset',
                            'Production, report as finished offset'];

    _itemGroup0 = InventItemGroup::find(itemGroupx);

    if(!_itemGroup0)
    {
        ttsBegin;
        _itemGroup.clear();
        _itemGroup.ItemGroupId     = itemGroupx;
        _itemGroup.Name            = itemGroupNamex;
        _itemGroup.validateWrite();
        _itemGroup.insert();
        ttsCommit;

        if(inventoryAccx)
        {
            for (i=1; i <= conLen(conCOAInventory); i++)
            {
                invttypex = conPeek(conCOAInventory, i);

                ttsBegin;
                _inventPosting.clear();
                _inventPosting.initValue();
                _inventPosting.InventAccountType = str2enum(inventAcctTypeEnum,invttypex);
                _inventPosting.ItemCode = str2enum(inventItemCodeEnum,'Group');
                _inventPosting.ItemRelation = itemGroupx;
                _inventPosting.CustVendCode = TableGroupAll::All;
                _inventPosting.LedgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(inventoryAccx);
                _inventPosting.validateWrite();
                _inventPosting.insert();
                ttsCommit;
                DIP_CreateInventGroup::CreateUpdateGroupForm(itemGroupx,_inventPosting.LedgerDimension,invttypex);
            }//for
        }// if(inventoryAccx)

        if(expenseAccx)
        {
            for (i=1; i <= conLen(conCOAExpense); i++)
            {
                invttypex = conPeek(conCOAExpense, i);

                ttsBegin;
                _inventPosting.clear();
                _inventPosting.initValue();
                _inventPosting.InventAccountType = str2enum(inventAcctTypeEnum,invttypex);
                _inventPosting.ItemCode = str2enum(inventItemCodeEnum,'Group');
                _inventPosting.ItemRelation = itemGroupx;
                _inventPosting.CustVendCode = TableGroupAll::All;
                _inventPosting.LedgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(expenseAccx);
                _inventPosting.validateWrite();
                _inventPosting.insert();
                ttsCommit;
                DIP_CreateInventGroup::CreateUpdateGroupForm(itemGroupx,_inventPosting.LedgerDimension,invttypex);
            }//for
        }// if(expenseAccx)

        if(revenueAccx)
        {
            for (i=1; i <= conLen(conCOARevenue); i++)
            {
                invttypex = conPeek(conCOARevenue, i);

                ttsBegin;
                _inventPosting.clear();
                _inventPosting.initValue();
                _inventPosting.InventAccountType = str2enum(inventAcctTypeEnum,invttypex);
                _inventPosting.ItemCode = str2enum(inventItemCodeEnum,'Group');
                _inventPosting.ItemRelation = itemGroupx;
                _inventPosting.CustVendCode = TableGroupAll::All;
                _inventPosting.LedgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(revenueAccx);
                _inventPosting.validateWrite();
                _inventPosting.insert();
                ttsCommit;
                DIP_CreateInventGroup::CreateUpdateGroupForm(itemGroupx,_inventPosting.LedgerDimension,invttypex);
            }//for
        }// if(revenueAccx)

        if(interUnitAccx)
        {
            for (i=1; i <= conLen(conCOAInterunit); i++)
            {
                invttypex = conPeek(conCOAInterunit, i);

                ttsBegin;
                _inventPosting.clear();
                _inventPosting.initValue();
                _inventPosting.InventAccountType = str2enum(inventAcctTypeEnum,invttypex);
                _inventPosting.ItemCode = str2enum(inventItemCodeEnum,'Group');
                _inventPosting.ItemRelation = itemGroupx;
                _inventPosting.CustVendCode = TableGroupAll::All;
                _inventPosting.LedgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(interUnitAccx);
                _inventPosting.validateWrite();
                _inventPosting.insert();
                ttsCommit;
                DIP_CreateInventGroup::CreateUpdateGroupForm(itemGroupx,_inventPosting.LedgerDimension,invttypex);
            }//for
        }// if(interUnitAccx)

        if(wipAccx)
        {
            for (i=1; i <= conLen(conCOAWIP); i++)
            {
                invttypex = conPeek(conCOAWIP, i);

                ttsBegin;
                _inventPosting.clear();
                _inventPosting.initValue();
                _inventPosting.InventAccountType = str2enum(inventAcctTypeEnum,invttypex);
                _inventPosting.ItemCode = str2enum(inventItemCodeEnum,'Group');
                _inventPosting.ItemRelation = itemGroupx;
                _inventPosting.CustVendCode = TableGroupAll::All;
                _inventPosting.LedgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(wipAccx);
                _inventPosting.validateWrite();
                _inventPosting.insert();
                ttsCommit;
                DIP_CreateInventGroup::CreateUpdateGroupForm(itemGroupx,_inventPosting.LedgerDimension,invttypex);
            }//for
        }// if(interUnitAccx)

    } //if(!_itemGroup0)

}


public static void CreateUpdateGroupForm(
ItemGroupId _itemGroupIdx,
LedgerDimensionDefaultAccount _ledgerDimDefaultx,
Name _inventAcctTypex
)
{
    InventItemGroupForm                 _itemGroupForm,_itemGroupForm1;
    InventAccountType                   inventAcctTypeEnum;
    ItemGroupLedgerDimensionGroup       itemGroupDimGroup;
    ;

    select * from _itemGroupForm
      where _itemGroupForm.ItemGroupId == _itemGroupIdx
         &&  _itemGroupForm.InventAccountType == str2enum(inventAcctTypeEnum,_inventAcctTypex);

        switch(_inventAcctTypex)
        {
            Case    'Inventory, issue',
                    'Inventory, receipt',
                    'Inventory, loss',
                    'Inventory, profit',
                    'Inventory inter-unit payable',
                    'Inventory inter-unit receivable':

                itemGroupDimGroup = ItemGroupLedgerDimensionGroup::Inventory;
            break;

            Case    'Production, issue',
                    'Production, picking list',
                    'Production, receipt',
                    'Production, report as finished',
                    'Production, issue offset',
                    'Production, picking list offset',
                    'Production, receipt offset',
                    'Production, report as finished offset':

                itemGroupDimGroup = ItemGroupLedgerDimensionGroup::Production;
            break;

            Case    'Sales order, issue',
                    'Order, packing slip',
                    'Sales order, consumption',
                    'Sales, packing slip offset',
                    'Sales order, revenue':

                itemGroupDimGroup = ItemGroupLedgerDimensionGroup::Sales;
            break;

            Case    'Purchase, inventory receipt',
                    'Product receipt',
                    'Purchase expenditure for expense',
                    'Purchase expenditure for product':

                itemGroupDimGroup = ItemGroupLedgerDimensionGroup::Purchase;
            break;
        }//switch


    if(!_itemGroupForm)
    {
        ttsBegin;
        _itemGroupForm.clear();
        _itemGroupForm.initValue();
        _itemGroupForm.InventAccountType = str2enum(inventAcctTypeEnum,_inventAcctTypex);
        _itemGroupForm.ItemGroupId = _itemGroupIdx;
        _itemGroupForm.LedgerDimension = _ledgerDimDefaultx;
        _itemGroupForm.LedgerDimensionGroup = itemGroupDimGroup;
        _itemGroupForm.validateWrite();
        _itemGroupForm.insert();
        ttsCommit;
    }
    else
    {
        while select forupdate _itemGroupForm1
          where _itemGroupForm1.ItemGroupId == _itemGroupIdx
             &&  _itemGroupForm1.InventAccountType == str2enum(inventAcctTypeEnum,_inventAcctTypex)
        {
            ttsBegin;
            _itemGroupForm1.LedgerDimension = _ledgerDimDefaultx;
            _itemGroupForm1.LedgerDimensionGroup = itemGroupDimGroup;
            _itemGroupForm1.validateWrite();
            _itemGroupForm1.update();
            ttsCommit;
        }
    }
}



No comments:

Post a Comment