Monday 7 May 2018

Sales Order Importing and Updating in Ax 2012

public void DataImport()
{
    InventDim                       inventDim;
    InventTable                     inventTable;
    EcoResProduct                   ecoResProduct;
    EcoResProductSubtype            ecoResProductSubtype;

    Na_Creatingproductitem    productitem = new Na_Creatingproductitem();


    str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
        {
            switch(_cv.variantType())
            {
                case (COMVariantType::VT_BSTR):
                    return _cv.bStr();
                case (COMVariantType::VT_R4):
                    return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
                case (COMVariantType::VT_R8):
                    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
                case (COMVariantType::VT_DECIMAL):
                    return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
                case (COMVariantType::VT_DATE):
                    return date2str(_cv.date(),123,2,1,2, 1,4);
                case (COMVariantType::VT_EMPTY):
                    return "";
                default:
                    throw error(strfmt("@SYS26908",_cv.variantType()));
            }
            return "";
        }


        application = SysExcelApplication::construct();
        workbooks = application.workbooks();

        try
        {
            workbooks.open(filename);
        }
        catch (Exception::Error)
        {
            throw error("File cannot be opened.");
        }
        workbook = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet = worksheets.itemFromNum(1);
        cells = worksheet.cells();
        row = 1;


        do
        {
            row++;

            salesorderid    = cells.item(row, 1).value().bStr();
            custAccount     = cells.item(row, 2).value().bStr();
            custName        = cells.item(row, 3).value().bStr();
            custGroup       = COMVariant2Str(cells.item(row, 4).value());
            currency        = cells.item(row, 5).value().bStr();
            itemId          = COMVariant2Str(cells.item(row, 6).value());
            itemName        = cells.item(row, 7).value().bStr();
            searchname      = cells.item(row, 8).value().bStr();
            salesqty        = any2int(cells.item(row,9).value().toString());
            unitprice       = int2str(real2int(cells.item(row,10).value().double()));
            site            = int2str(real2int(cells.item(row,11).value().double()));
            warehouse       = int2str(real2int(cells.item(row,12).value().double()));
            Deliveryname    = cells.item(row, 13).value().bStr();
            Address         = cells.item(row, 14).value().bStr();
            ProductSubtype  = cells.item(row, 15).value().bStr();
            productType     = cells.item(row, 16).value().bStr();

            select salestable where salestable.SalesId  == salesorderid;
            if(salestable.SalesId  == salesorderid &&  salestable.SalesStatus == SalesStatus::Backorder)
            {
                select forupdate dirPartyTable12 join custTab  where custTab.AccountNum == custAccount
                                                               && dirPartyTable12.RecId == custTab.Party;
                if(dirPartyTable12.RecId)
                {
                    ttsBegin;
                    dirPartyTable12.Name        = custName;
                    dirPartyTable12.update();
                    ttsCommit;
                }
                select forupdate postalAddress join location join dirPartyTable12 join  custTab where custTab.AccountNum == custAccount
                                                                                                && custTab.Party == dirPartyTable12.RecId
                                                                                                && Location.RecId == dirPartyTable12.PrimaryAddressLocation
                                                                                                && postalAddress.Location == location.RecId;

                if(postalAddress.RecId)
                {
                    ttsBegin;
                    postalAddress.Address      = Address;
                    postalAddress.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                    postalAddress.update();
                    ttsCommit;
                }
                if(Location.RecId)
                {
                    ttsBegin;
                    Location.Description       = Deliveryname;
                    Location.selectForUpdate(true);
                    Location.update();
                    ttsCommit;
                }

                custTab.CustGroup       = custGroup;
                custTab.Currency        = currency;
                custTab.InventSiteId    = site;
                custTab.InventLocation  = warehouse;
                ttsBegin;
                custTab.selectForUpdate(true);
                custTab.update();
                ttsCommit;

                salestable.CustAccount      = custTab.AccountNum;
                salestable.SalesName        = dirPartyTable12.Name;
                salestable.CustGroup        = custTab.CustGroup;
                salestable.CurrencyCode     = custTab.Currency;
                salestable.InventSiteId     = site;
                salestable.InventLocationId = warehouse;
                salestable.SalesGroup       = "01";
                salesTable.initValue(SalesType::Sales);
                salesTable.initFromCustTable();
                ttsBegin;
                salesTable.selectForUpdate(true);
                salestable.update();
                ttsCommit;

                salesLine.clear();
                salesLine.initValue(salesTable.SalesType);
                salesLine.initFromSalesTable(salesTable);
                salesLine.ItemId = itemId;
                salesLine.InventDimId       = inventDim.inventDimId;
                salesLine.SalesQty          = salesqty;
                salesLine.SalesUnit         = unitprice;
                ttsBegin;
                salesLine.selectForUpdate(true);
                salesLine.update();
                ttsCommit;

                info("Updated Successfully");
            }
        else
        {
         //num = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
        //salesTable.SalesId = num.num();
            select custTab where custTab.AccountNum == custAccount;
            if( custTab.AccountNum == custAccount)
            {
                info("Record Is updated");
            }
            else
            {
                ttsBegin;
                dirPartyTable12.clear();
                dirPartyTable12.initValue();
                dirPartyTable12.Name             = custName;
                dirPartyTable12.insert();

                location.clear();
                location.initValue();
                location.Description            = Deliveryname;
                location.IsPostalAddress        = NoYes::Yes;
                location.insert();

                postalAddress.clear();
                postalAddress.initValue();

                postalAddress.Address           = Address;
                postalAddress.CountryRegionId   = "IND";
                postalAddress.Street            ="Kukatpally";
                postalAddress.State             ="Telanaga";
                postalAddress.Location          = location.RecId;
                postalAddress.insert();

                partyLocation.clear();
                partyLocation.initValue();
                partyLocation.Party             = dirPartyTable12.RecId;
                partyLocation.Location          = location.RecId;
                partyLocation.IsPostalAddress   = NoYes::Yes;
                partyLocation.IsPrimary         = NoYes::Yes;
                partyLocation.insert();

                partyLocationRole.clear();
                partyLocationRole.initValue();
                partyLocationRole.LocationRole  = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Delivery).RecId;
                partyLocationRole.PartyLocation = partyLocation.RecId;
                partyLocationRole.insert();

                custTab.clear();
                custTab.initValue();
                custTab.AccountNum              = custAccount;
                custTab.CustGroup               = custGroup;
                custTab.Currency                = currency;
                custTab.InventSiteId            = site;
                custTab.InventLocation          = warehouse;
                custTab.Party                   = dirPartyTable12.RecId;
                custTab.insert();

                salestable.SalesId              = salesorderid;
                salestable.CustAccount          = custTab.AccountNum;
                salestable.SalesName            = dirPartyTable12.Name;
                salestable.CustGroup            = custTab.CustGroup;
                salestable.CurrencyCode         = custTab.Currency;
                salestable.InventSiteId         = site;
                salestable.InventLocationId     = warehouse;
                salestable.SalesGroup           = "01";
                salesTable.initValue(SalesType::Sales);
                salesTable.initFromCustTable();
                salesTable.insert();

                select inventTable where inventTable.ItemId == itemId;
                if(!inventTable.RecId)
                {
                    if(ecoResProductSubtype::ProductMaster == str2enum(ecoResProductSubtype,ProductSubtype))
                    {
                        this.Create_ProductMaster(itemId,itemName,searchname,name,productType);//to call the Class item master method.
                    }
                    else if(ecoResProductSubtype::Product == str2enum(ecoResProductSubtype,ProductSubtype))
                    {
                        this.Create_Product(itemId,itemName,searchname,name,productType);//to call the Class Create_Product method.
                    }
                    else
                    {
                        Box::info("Record not Exist");
                    }
                    salesLine.clear();
                    salesLine.initValue(salesTable.SalesType);
                    salesLine.initFromSalesTable(salesTable);
                    salesLine.SalesId = salestable.SalesId ;
                    salesLine.ItemId = itemId;
                    salesLine.initFromInventTable(InventTable::find(itemId));
                    salesLine.InventDimId  = InventDim::findOrCreate(inventDim).inventDimId;
                    salesLine.InventDimId  = inventDim.inventDimId;
                    salesLine.SalesQty     = salesqty;
                    salesLine.SalesUnit    = unitprice;
                    SalesLine.DeliveryName = location.Description;
                    salesLine.createLine(true,  true, true, true,true,true );
                    info("Record Is Inserted");
                }
                else
                {
                    salesLine.clear();
                    salesLine.initValue(salesTable.SalesType);
                    salesLine.initFromSalesTable(salesTable);
                    salesLine.SalesId = salestable.SalesId ;
                    salesLine.ItemId  = itemId;
                    salesLine.initFromInventTable(InventTable::find(itemId));
                    salesLine.InventDimId  = InventDim::findOrCreate(inventDim).inventDimId;
                    salesLine.InventDimId  = inventDim.inventDimId;
                    salesLine.SalesQty     = salesqty;
                    salesLine.SalesUnit    = unitprice;
                    SalesLine.DeliveryName = location.Description;
                    salesLine.createLine(true,  true, true, true,true,true );
                    info("Record Is Inserted");
                }
                ttsCommit;
            }
        }
            type = cells.item(row+1, 1).value().variantType();
     }
            while (type != COMVariantType::VT_EMPTY);
            workbooks.close();
            application.quit();
            info("Done!");
            //super();
}

No comments:

Post a Comment