Showing posts with label service. Show all posts
Showing posts with label service. Show all posts

Thursday, May 4, 2017

MICROSOFT DYNAMICS AX 2012 : CODE TO GET ON HAND OF AN ITEM STOCK COUNT

MICROSOFT DYNAMICS AX 2012 : GET ON HAND OF AN ITEM STOCK COUNT
static void stockOnHand(Args _args)
{
    InventOnhand        inventOnHand;
    InventDim           inventDim;
    InventDimParm       inventDimParm;
    InventQty           stockOnHand;
    ItemId              itemid = "00001aTestInter";
 
    inventDim.InventSiteId = "SITE"; // site
    inventDim.InventLocationId = "WAREHOUSE"; // warehouse
 
 
    inventDimParm.initFromInventDim(inventDim);
 
    inventOnHand = InventOnhand::newParameters(itemid,inventDim,inventDimParm);
 
    stockOnHand = inventOnHand.availPhysical();
 
    info (strFmt(" Stock on Hand for Item %1 is %2",itemid,stockOnHand));
 
}

MICROSOFT DYNAMICS AX 2012 : CODE TO DEPLOY SERVICE FROM AX

DEPLOY AX SERVICES FROM CODE

Below is the script for automatically deploy or undeploy  AIF service.
In this example update the port with current company and deploy the service.
static void AIFservices(Args _args)
{
    AifPort  port;
    AifPortName portName = "servicename";
    AifPortManager::undeployPort(portName);
    port = AifPort::find(portName,true);

    if(port)
    {
        ttsBegin;
        if(port.Company != curext() )
        {
            port.Company = curext();
            port.update();
        }
        ttsCommit;
    }
    AifPortManager::deployPort(portName);
}

Saturday, April 16, 2016

Sales Order Creation Service : AX 2012 X++ code

Sales Order Creation in AX with Config

[AifCollectionTypeAttribute('return', Types::String), SysEntryPointAttribute(true)]
public str CreateSalesOrder(SalesIdBase _salesId,CustAccount _customerId,InventSiteId _site,TaxGroup _taxGroup,
                      )
{
    str                                     log;
    str                                     log_1,log_2,log_3,log_4,log_5,log_6,log_7,log_8,log_9;
    Inventsite                              site;
    InventLocation                          ware;
    SalesTable                              header,salesTable,salesTablechk;
    SalesLine                               line_1,line_2;
    SalesLine_IN                            line_In,line_Inc;
    NoYes                                   noyes;
    str                                     component;
    container                               con,_conchar,_concharvalue;
    int                                     i,j,configCount,k,counter;
    MarkupTable                             markTab;
    MarkupTrans                             trans;
    EcoResProductMaster                     ecoResProductMaster;
    EcoResConfiguration                     ecoResConfiguration;
    EcoResProductMasterConfiguration        ecoResProductMasterConfiguration;
    EcoResDistinctProductVariant            ecoResDistinctProductVariant;
    EcoResProductVariantConfiguration       ecoResProductVariantConfiguration;
    EcoResconfigurationName                 _name;
    container                               prodDimensions;
    RefRecId                                ecoResDistinctProductVariantRecId;
    ConfigChoice                            config,configrec;
    int                                     newconfigrec;
    ConfigGroup                             configGroup;
    ConfigGroupId                           groupId;
    TmpConfigId                             tempConfig;
    TmpConfigValue                          tempConfigValue;
    EcoResProductReleaseManagerBase         releaseManager;
    InventDim                               dim;
    InventDimCombination                    comb;
   // AxSalesTable                            axsalesTable;
    SalesFormLetter                         salesFormLetter;
    CompanyInfo                             info;
    DirPartyLocation                        dirPartyLocation;
    TaxInformation_IN                       taxInformation;
    compProductMasterDefault                 prodMasterdef;
    _conchar        = str2con(_charname,',');
    _concharvalue   = str2con(_charvalue,',');
    select * from prodMasterdef;
   changeCompany(prodMasterdef.DealerCompany)
    {
        select salesid from salesTablechk where salesTablechk.salesid ==_salesId;
  if(!salesTablechk.RecId && InventSite::exist(_site) && InventLocation::exist(_warehouse) && InventTable::exist(_item) && SalesTaxFormTypes_IN::exist(_saleTypeSalesTax)  &&  TaxGroupHeading::exist(_taxGroup))
    {
        configcount++;
        ttsBegin;
        //creating header
        header.clear();
        header.SalesId = _salesId;
        header.SalesType = SalesType::Sales;
        header.initValue();
        header.CustAccount = _customerId;
        header.InvoiceAccount = _customerId;
        header.initFromCustTable();
        header.InventSiteId = _site;
        header.InventLocationId = _warehouse;
        header.Payment = _paymTerm;
        header.ShippingDateRequested = _deliveryDate;
        header.CurrencyCode = _currency;
        header.LanguageId =  systemparameters::getSystemLanguageId();
        header.comp_SoldToParty = _soldToParty ;
        header.comp_ShipToParty = _shipToParty;
        header.comp_BilltoAddress = _billTo;
        header.comp_ShiptoAddress = _shipTo;
        header.DlvTerm = _delTerm;
        header.CustGroup = CustTable::find(_customerId).CustGroup;
        header.TaxGroup   =  _taxGroup;
        header.CustomerRef   = _ref;
        header.PurchorderFormNum = _custReq;
        header.comp_OrderType = _orderType;
        header.comp_OrderCat = _orderCategory;
        header.comp_SaleTypeExciseDuty = _saleTypeExcise;
        header.comp_E1Sale = str2enum(noyes,_e1Sale);
        header.comp_Domestic = str2enum(NoYes,_domestic);
        header.comp_Export = str2enum(NoYes,_export);
        header.comp_IncoTerm2 = _incoTerm;
        header.comp_FOCSpacesOffered = _FOCspaces;
        header.comp_FocInformation = _FOCInformation;
        header.comp_Financer = _financer;
        header.insert();
        line_1.initFromSalesTable(header,true);
        line_1.ItemId = _item;
        //line_1.QtyOrdered = _qty;
        line_1.SalesQty = _qty;
        line_1.initFromSalesLine_IN(line_1);
       // line_1.initFromSalesLineView_IN(
        //changes start
        select count(RecId) from configrec where configrec.ItemId == _item;
        {
            newconfigrec = int642int(configrec.RecId) + 1;
        }
       
        //changes foc product
        if(!conLen(_conChar)==0 && strLen(_charname) != 0)
        {
        _name = this.Config(_conChar,_conCharValue,_item);
        if(_name == '')
        {
                //creating configuration on basis of characteristic name and values
            //    _name = 'second';
                   _name = int2str(newconfigrec);
                 ecoResConfiguration = EcoResConfiguration::findByName(_name);
                if (!ecoResConfiguration)
                {
                    ecoResConfiguration.clear();
                    ecoResConfiguration.initValue();
                    ecoResConfiguration.Name = _name;
                    ecoResConfiguration.insert();
                }
                select ecoResProductMaster where ecoResProductMaster.DisplayProductNumber == _item;
                 Select firstonly ecoResProductMasterConfiguration where ecoResProductMasterConfiguration.ConfigProductMaster == ecoResProductMaster.RecId
                        && ecoResProductMasterConfiguration.Configuration == ecoResConfiguration.RecId;
                if (!ecoResProductMasterConfiguration)
                {
                    ecoResProductMasterConfiguration.clear();
                    ecoResProductMasterConfiguration.initValue();
                    ecoResProductMasterConfiguration.Configuration = ecoResConfiguration.RecId;
                    ecoResProductMasterConfiguration.ConfigProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, ConfigId));
                    ecoResProductMasterConfiguration.ConfigProductMaster = ecoResProductMaster.RecId;
                    ecoResProductMasterConfiguration.insert();
                }
                select tempConfig ;
                tempconfig.ConfigId =  ecoResConfiguration.Name;
                tempConfig.doInsert();
               for (i=1; i <= conlen(_conChar); i++)
                {
                   tempConfigValue.clear();
                   tempConfigValue.ConfigGroupId = conPeek(_conChar,i) ;
                   tempConfigValue.ItemId = conPeek(_conChar,i)+'_'+ conPeek(_conCharValue,i);
                   tempConfigValue.ConfigId = ecoResConfiguration.Name;
                   tempConfigValue.GroupNum = counter;
                   tempConfigValue.doInsert();
                   Counter++;
                   config.clear();
                   config.initValue();
                   config.initFromTmpConfigValue(tempConfigValue);
                   config.ItemId = _item;
                   config.ConfigId = ecoResConfiguration.Name;
                   config.Autogenerated = NoYes::No;
                   config.insert();
                }
                prodDimensions = EcoResProductVariantDimValue::getDimensionValuesContainer(_name);
                //Create Product search name
                ecoResDistinctProductVariant.DisplayProductNumber = EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions('PC104',prodDimensions);
               //Create Product variant with Product and dimensions provided
                ecoResDistinctProductVariantRecId = EcoResProductVariantManager::createProductVariant(ecoResProductMasterConfiguration.ConfigProductMaster,ecoResDistinctProductVariant.DisplayProductNumber,prodDimensions);
                //Find newly created 4 Variant
                ecoResDistinctProductVariant = ecoResDistinctProductVariant::find(ecoResDistinctProductVariantRecId);
                //Now release the Product variant
               releaseManager = EcoResProductReleaseManagerBase::newFromProduct(ecoResDistinctProductVariant);
               releaseManager.release();
               dim.configId = _name;
        }
         else
            {
                dim.configId = _name;
            }
        }
        // dim.configId = InventTable::find(line.ItemId).StandardConfigId;
        dim.InventSiteId = _site;
        dim.InventLocationId = _warehouse;
       // dim.configId = _name;
        dim = InventDim::findOrCreate(dim);
        dim.write();
        //creating line
        line_1.InventDimId = dim.inventDimId;
        comb = InventDimCombination::findByInventDim(line_1.ItemId,dim);
        line_1.RetailVariantId = comb.RetailVariantId  ;
        line_1.createLine(true,true,true,false,true,true,false,false,'',true,true,false,null,0,'');
        select forUpdate line_2 where line_2.SalesId == line_1.SalesId;
        if(line_2.RecId)
        {
           line_2.SalesPrice = _price;
        }
        line_2.modifiedField(fieldnum(SalesLine,SalesPrice));
        line_2.update();
        //allocating charges
        if(_FOCSpares != 0 )
        {
            markTab = MarkupTable::find((MarkupModuleType::Cust),'1');
            trans.clear();
            trans.initFromSalesLine(line_1);
            trans.initFromMarkupTable(marktab);
            trans.MarkupCategory = MarkupCategory::Pcs;
            trans.Value = _FOCSpares/_qty;
            trans.LineNum = k;
            trans.insert();
            k++;
        }
        info = CompanyInfo::findByCompany_IN(curext());
        dirPartyLocation = DirPartyLocation::findPrimaryPartyLocation(info.RecId);
        if (dirPartyLocation)
        {
           line_In.CompanyLocation_IN = LogisticsLocation::find(dirPartyLocation.Location).RecId;
             select firstonly RecId from taxInformation
            where taxInformation.RegistrationLocation == LogisticsLocation::find(dirPartyLocation.Location).RecId
                    && taxInformation.IsPrimary == NoYes::Yes;
        }
       select forupdate line_IN where line_In.SalesLine == line_2.RecId;
        if(line_In.RecId)
        {
            line_In.CompanyLocation_IN  = LogisticsLocation::find(dirPartyLocation.Location).RecId;
            line_In.TaxInformation = taxInformation.RecId;
            line_In.SalesTaxFormTypes_IN = SalesTaxFormTypes_IN::findbyFormType(_saleTypeSalesTax).RecId;
            line_In.AssessableValue_IN = line_2.getMiscChargesAmount_IN();
            line_In.ExciseRecordType_IN = ExciseRecordType_IN::RG23D;
            line_In.ExciseType_IN = ExciseType_IN::Trader;
            line_IN.update();
        }
        //confirming Sales Order
        salesformletter = SalesFormletter::construct(DocumentStatus::Confirmation);
        salesTable = SalesTable::find(_salesId);
        salesFormLetter.update(salesTable);
        ttscommit;
        log = strfmt('Success : Sales Order Created successfully');
        k = 1;
    }
      else
        {
            if(salesTablechk.RecId)
             {
                log_9 = strfmt('Saled Order %1 already exist',_salesId) ;
            }
            if(!InventSite::exist(_site))
            {
                log_1 = strfmt('Site %1 is not valid for SO %2',_site,_salesId) ;
            }
            if(!InventLocation::exist(_warehouse))
            {
                log_2 = strFmt('Warehouse %1 is not valid for SO %2',_warehouse,_salesId) ;
            }
            if(!InventTable::exist(_item))
            {
                log_3 = strFmt('Item %1 is not valid for SO %2',_item,_salesId) ;
            }
            if(!SalesTaxFormTypes_IN::exist(_saleTypeSalesTax))
            {
                log_4 = strFmt('SalesTax FormType %1 is not valid for SO %2',_saleTypeSalesTax,_salesId) ;
            }
            if(!TaxGroupHeading::exist(_taxGroup))
            {
                log_5 = strFmt('Tax Group %1 is not valid for SO %2',_taxGroup,_salesId) ;
            }
            log = strFmt("%1/n%2/n%3/n%4/n%5/n%6",log_9,log_1,log_2,log_3,log_4,log_5);
        }
        info(strFmt("SO Integration : %1 - %2 ",_salesId,log));
        comp_RetailExceptionActivityLog::logEvent('Sales Order Creation:',infolog.export());
     return log ;
    }//change company
    }



Config Creation:
config creation
public str config(container _conChar,Container _conCharValue,ItemId _item)
{
    ConfigChoice            choice,_config;
    int                     length,i,choice_1,con,l;
    ItemId                  chosenItem;
    boolean                 configexist;
    ConfigIdStandard        configname;
    container               conTax ;
    length = conLen(_conChar);
    while  select ConfigId from _config    group by _config.ConfigId  where  _config.itemid == _item
    {
        choice_1 = 0;
        for(i=1;i<=length;i++)
        {
            chosenItem = conPeek(_conChar,i)+'_'+ conPeek(_conCharValue,i);
            select choice where choice.ConfigId ==  _config.ConfigId
                            &&  choice.ChosenItemId == chosenItem;
            if(choice)
            {
            choice_1++;
            }
        }
        if(choice_1 == length)
        {
            configexist = true;
            configname = choice.ConfigId;
        }
        if(configexist == true)
        {
            break ;
        }
    }
    return configname;
}

Related Posts Plugin for WordPress, Blogger...