Create Product variants In ax 2012

 Creation of product Variants in Ax 2012 

Public void prodVariants(RefRecId _oldrecid, RefRecId _newRecid)

{


    select ProductDimensionGroup from ecoResProductDimensionGroupProductloc

         where ecoResProductDimensionGroupProductloc.Product == _oldrecid

            join ecoResProductDimensionGroup

                where ecoResProductDimensionGroup.RecId==ecoResProductDimensionGroupProductloc.ProductDimensionGroup;


    if (ecoResProductDimensionGroup.Name=="Config")

    {

        while select productMasterConfiguration

            where productMasterConfiguration.ConfigProductMaster== _oldrecid


        {

            productMasterConfigurationVariants = this.copyProdDimensionConfig(_newRecid , productMasterConfiguration);

            ecoResConfiguration = EcoResConfiguration::find(productMasterConfigurationVariants.Configuration);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                                ecoResColor.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);

        }


    }

    if (ecoResProductDimensionGroup.Name=="Size")

    {

        while select ecoResProductMasterSize

              where ecoResProductMasterSize.SizeProductMaster == _oldrecid

        {

            ecoResProductMasterSizeVariants = this.copyProdDimensionsSIze(_newRecid , ecoResProductMasterSize);

            ecoResSize = EcoResSize::find(ecoResProductMasterSizeVariants.Size);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                                ecoResColor.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);

        }

    }

    if (ecoResProductDimensionGroup.Name=="color")

    {

        while select ecoResProductMastercolor

            where ecoResProductMastercolor.ColorProductMaster == _oldrecid

        {

            ecoResProductMastercolorVariants = this.copyProdDimensionColur(_newRecid ,ecoResProductMastercolor);

            ecoResColor = EcoResColor::find(ecoResProductMastercolorVariants.Color);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                                ecoResColor.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);

        }


    }

    if (ecoResProductDimensionGroup.Name=="Style")

    {

        while select ecoResProductMasterStyle

            where  ecoResProductMasterStyle.StyleProductMaster == _oldrecid

        {

            ecoResProductMasterStyleVariants = this.copyProdDimensionsStyle(_newRecid ,ecoResProductMasterStyle);

            ecoResStyle = EcoResStyle::find(ecoResProductMasterStyleVariants.Style);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                             ecoResColor.Name,ecoResStyle.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);

        }


    }

    if (ecoResProductDimensionGroup.Name=="SizeCol")

    {

         while select ecoResProductMasterSize

             where ecoResProductMasterSize.SizeProductMaster == _oldrecid

                join ecoResProductMastercolor

                    where ecoResProductMastercolor.ColorProductMaster==ecoResProductMasterSize.SizeProductMaster

        {

            ecoResProductMasterSizeVariants = this.copyProdDimensionsSIze(_newRecid , ecoResProductMasterSize);

            ecoResSize = EcoResSize::find(ecoResProductMasterSizeVariants.Size);

            ecoResProductMastercolorVariants = this.copyProdDimensionColur(_newRecid ,ecoResProductMastercolor);

            ecoResColor = EcoResColor::find(ecoResProductMastercolorVariants.Color);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                             ecoResColor.Name,ecoResStyle.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);

        }


    }

    if (ecoResProductDimensionGroup.Name=="ColorStyle")

    {

        while select ecoResProductMastercolor

            where ecoResProductMastercolor.ColorProductMaster == _oldrecid

                join ecoResProductMasterStyle

                    where ecoResProductMasterStyle.StyleProductMaster ==ecoResProductMastercolor.ColorProductMaster

        {


            ecoResProductMastercolorVariants = this.copyProdDimensionColur(_newRecid ,ecoResProductMastercolor);

            ecoResColor = EcoResColor::find(ecoResProductMastercolorVariants.Color);


            ecoResProductMasterStyleVariants = this.copyProdDimensionsStyle(_newRecid ,ecoResProductMasterStyle);

            ecoResStyle = EcoResStyle::find(ecoResProductMasterStyleVariants.Style);

            conVariants  = EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name,ecoResSize.Name,

                                                                                             ecoResColor.Name,ecoResStyle.Name);

            this.addVariantsToReleasedProduct(_newRecid,conVariants);


        }

    }


}




private void addVariantsToReleasedProduct(RefRecId _productMasterRecId,

                                            container  _con)

{

    EcoResProductReleaseManagerBase     releaseManager;

    Name                                configId;


 //   i=1;

    if (_productMasterRecId)

    {

        ecoResDistinctProductVariant    = EcoResProductVariantManager::findDistinctProductVariant(_productMasterRecId,_con);


                if (!ecoresdistinctproductvariant)

                {

                    ecoresproductvariantmanager::createproductvariant(_productMasterRecId,

                                                                    ecoresproduct::find(_productMasterRecId).searchname + ecoresconfiguration.name,

                                                                    _con);

                    //info("product variants created");

        //con = _con;

        //while (i<=conLen(con))

        //{

            //configId = conPeek(con,i);

                   }


          while  select ecoResDistinctProductVariant

                where ecoResDistinctProductVariant.ProductMaster == _productMasterRecId //&&

                //ecoResDistinctProductVariant.DisplayProductNumber == EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions(ecoResProductMaster.productNumber(),

           {                                                                                          //EcoResProductVariantDimValue::getDimensionValuesContainer(configId));

                if (ecoResDistinctProductVariant && !ecoResDistinctProductVariant.isReleased())

                {

                    releaseManager = EcoResProductReleaseManagerBase::newFromProduct(ecoResDistinctProductVariant);

                    releaseManager.release();

                }

           // i++;

           }

        //info('Product variants released');

    }

}


Create Purchase Order In ax 2012

  public PurchIdBase createPO(ItemId _itemid,Vendaccount _vendaccount,Inventsiteid _id,Inventlocationid _whs,

                                PurchPrice _price,Weight _grosswieght,Weight _netweight,purchunit _uom)

 {


    NumberSeq                             numberSeq;

    PurchFormLetter                       purchFormLetter;


    PurchLine                             purchLine;

    InventDim                             inventDimloc;

    InventItemPrice                         inventItemPriceLoc;

    PurchId                               purchid;


    ttsBegin;

    numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());

    numberSeq.used();

    purchTable.PurchId = numberSeq.num();

    purchTable.initValue();

    purchTable.OrderAccount= _vendaccount;

    purchTable.initFromVendTable(VendTable::find(_vendaccount));

    if (!purchTable.validateWrite())

    {

        throw Exception::Error;

    }

    purchTable.insert();

    purchLine.clear();

    purchLine.PurchId = purchTable.PurchId;

    purchLine.ItemId = _itemid;

    inventDimloc.clear();

    inventDimloc.InventSiteId = _id;

    inventDimloc.InventLocationId =_whs;

    purchLine.InventDimId=InventDim::findOrCreate(inventDimloc).inventDimId;


    inventItemPriceLoc = InventItemPrice::findCurrent(_itemid,CostingVersionPriceType::Cost,inventDimloc.inventDimId,

                                                      systemDateGet(),_id,InventItemCostingType::Standard);


    //purchLine.RFGrossWeight = _grosswieght;

    purchLine.RFNetWeight = _netweight;

    purchLine.PurchUnit = _uom;


    purchLine.createLine(true, true, true, true, true, true);

            // get random amount nubmer

            // purchLine.LineAmount = purchLine.calcLineAmount();

    purchLine.selectForUpdate(true);

    purchLine.PurchPrice = _price;

    purchLine.PurchQty = _grosswieght;

    purchLine.update();

    ttsCommit;

    info(strFmt("<%1> Purchase order  has been created",purchTable.PurchId));


     //po confirmation

    ttsBegin;

    purchTable = PurchTable::find(purchTable.PurchId);

    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);

    purchFormLetter.update(purchTable, strFmt("Inv_%1", purchTable.PurchId));


    ttsCommit;


       //PO Product Receipt

    //ttsBegin;

    //purchFormLetter = purchFormLetter::construct(DocumentStatus::PackingSlip);

    //purchFormLetter.update(purchtable,

    //"PO_"+purchTable.PurchId, // Invoice Number

    //systemdateget()); // Transaction date

     //Info(strFmt("%1 Product reciept recived with no. %1","PO_"+purchTable.PurchId ));

    //ttsCommit;


    return purchid;

 }

Reverse Product Reciept In Ax 2012

 void clicked()

{

    super();


    //select  PurchId from purchtable

        //join purchline

             //where purchline.ItemId==RentalLinesIntake.Rentalreference // itemid

             //

        //join PackingSlipId from vendPackingSlipJour

            //where vendPackingSlipJour.PurchId == purchtable.PurchId;

    select PurchId from purchline

        where purchline.ItemId == RentalLinesIntake.Rentalreference;

    info(purchline.PurchId);

    if (purchline.PurchId)

    {

        select PackingSlipId from vendPackingSlipJour

            where vendPackingSlipJour.PurchId == purchline.PurchId;


         createRentalProductmaster.reverseProductReciept(purchline.PurchId,vendPackingSlipJour.PackingSlipId);


    }

}

note : class createRentalProductmaster/ reverseProductReciept

public void reverseProductReciept(PurchId _purchid ,ProductReceiptId _productReceiptId)

{

    VendPackingSlipJour  vendPackingSlipJour;

    PurchFormLetter      purchFormLetter;

    purchTableRevPr = PurchTable::find(_purchid);


    //Retrieve existing vend packingslip

    select firstOnly vendPackingSlipJour

    where vendPackingSlipJour.PackingSlipId == _productReceiptId;


    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);


    //If the correct VersioningUpdateType is not set

    //the system will try to create a new packingslip

    PurchFormLetter.parmVersioningUpdateType(VersioningUpdateType::Cancel);

    PurchFormLetter.parmCallerTable(vendPackingSlipJour);


    //The rows bellow are very important. Not really sure why

    PurchFormLetter.allowEmptyTable(true);

    PurchFormLetter.initAllowEmptyTable(true);

    PurchFormLetter.multiForm(true);

    purchFormLetter.update( purchTableRevPr,     // Purchase record buffer

        vendPackingSlipJour.PackingSlipId,  // Packingslip Number

        VendPackingSlipJour.DeliveryDate,   // Transaction Date

        PurchUpdate::ReceiveNow,            // Quantity update

        AccountOrder::None,

        NoYes::No,

        NoYes::No,

        NoYes::Yes);


    //if (purchTable.PurchStatus==PurchStatus::Backorder)

    //{

        //info(strFmt(" product reciept -%1 has reversed ",_productReceiptId));

//

    //}

    //else

    //{

        //info("product reciept reversal failed");

    //}




}


Reverse PO in ax 2012

  A simple way to reverse Po in ax 2012



void clicked()

{

    PurchLine purchLineloc ;

    Purchtable purchtableloc;

    super();

    select PurchId from purchline

        where purchline.ItemId == RentalLinesIntake.Rentalreference; // itemid

    purchLineloc = PurchLine::find(purchline.purchid);



    if (purchLineloc)

    {

        purchLineloc.selectForUpdate(true);

        ttsBegin;

        purchLineloc.RemainInventPhysical  = 0;



        purchLineloc.RemainPurchPhysical   = 0;


        purchLineloc.PurchStatus           = PurchStatus::Canceled;


        purchLineloc.update();

        ttsCommit;


        InventMovement::bufferSetRemainQty(purchLineloc);


        purchtableloc = PurchTable::find(purchLineloc.PurchId,true);

        purchtableloc.delete();

        info("done");


    }



}

filter and Pass current record to the another form using Listpage in ax 2012

 to pass records  In lispage interaction class in selection changed method we can wright as follows


public void selectionChanged()

{

    Query _query;



    this.listPage().actionPaneControlParameters(formControlStr(RentalJobListPage,Intake),rentalord.RentalOrderid);

    this.listPage().actionPaneControlParameters(formControlStr(RentalJobListPage,OutTakeRentals),rentalord.RentalOrderid);


    super();


}

To filter records in listpage 


public void initializeQuery(Query _query)

{

    Query query= new Query();

    QueryBuildRange qbr,qbr1;

    QueryBuildDataSource    qbds,qbds1;


    if (this.listPage().listPageArgs().externalRecord())

    {

        rentalord = this.listPage().listPageArgs().externalRecord();

        rentalJobHeaders=RentalJobHeaders::findbyRentalorderId(rentalord.RentalOrderid);

        qbds = query.addDataSource(tableNum(RentalOrders));

        qbr= qbds.addRange(fieldNum(RentalOrders,RentalOrderid));


        if (rentalJobHeaders.Jobid)

        {

            qbr.value(rentalord.RentalOrderid);

        }

        else

        {

            qbr.value(SysQuery::valueEmptyString());

        }

    }


    super(_query);



}