Showing posts with label PurchTable. Show all posts
Showing posts with label PurchTable. Show all posts

Saturday, December 17, 2016

Dynamics AX 2012 Create Purchase Order Through Code in X++ Vikas Mehta

How to Create Purchase Order Through Code in X++

static void PurchOrderCreate(Args _args)
{
    NumberSeq numberSeq;
    PurchTable purchTable;
    PurchLine purchLine;
    ttsBegin;
    numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
    numberSeq.used();
    purchTable.PurchId = numberSeq.num();
    purchTable.initValue();
    purchTable.initFromVendTable(VendTable::find('8009'));
    if (!purchTable.validateWrite())
    {
        throw Exception::Error;
    }
    purchTable.insert();
    purchLine.PurchId = purchTable.PurchId;
    purchLine.ItemId = 'A0001';
    purchLine.createLine(true, true, true, true, true, true);
    ttsCommit;
    info(strFmt("Purchase order '%1' has been created",purchTable.PurchId));
}

Saturday, April 16, 2016

PURCHASE ORDER INTEGRATION AX 2012 X++ CUSTOM WEB SERVICE

PURCHASE ORDER INTEGRATION AX 2012 
X++ CUSTOM WEB SERVICE

public void sendPOtoCRM()
{

    compPurchaseOrdertoCRMUat.PurchaseOrderReference.PurchaseOrderClient    service2;
    PurchTable                  purchTable;
    PurchLine                   purchLine,purchLineupd;
    VendPurchOrderJour          vendPurchOrderJour,vendPurchOrderJourupd;
    InventDim                   inventDim;
    ConfigChoice                configChoice;
    InventTable                 inventTable;
    str                         _success;
    TaxTable                    taxtable;
    int                         qty;
    str                         component,sfilepath,sfilename,sfileext,sfullname;
    container                   conTax,conChar,concharges,confiledetails;
    int                         i,j,k,f;
    System.Exception            ex;
    System.Type                 type;
    str                         taxinfo,taxcomp,taxvalue,charname,charvalue,markupAmount;
    str                         conchargesinfo,contaxinfo,concharinfo,strfiledetails;
    TmpTaxWorkTrans             tmpTax;
    PurchTotals                 purchTotals;
    PurchLine_IN                purchLine_In;
    SalesTaxFormTypes_IN        salesTaxFormTypes_IN;
    Transtxt                    transTxt;
    MarkupAmount                markupAmounttest;
    MarkupTrans                 markupTrans;
    DocuRef                     docuRef;
    DocuValue                   docuValue;
    VendPackingSlipTrans        tempVPST;
    compProductMasterDefault     prodMasterDef;
    //changes for multiple PO lines
    TaxTmpWorkTransForm         taxTmpWorkTransForm;
    TaxPurch                    taxPurch;

    select * from prodMasterDef;

   while select purchLine
            join purchTable  where  purchTable.PurchId == purchLine.PurchId && purchLine.comp_IsPurchlineSend == NoYes::No
           // join vendPurchOrderJour where vendPurchOrderJour.PurchId == purchTable.PurchId
    {

     try
       {
            type    = CLRInterop::getType('compPurchaseOrdertoCRMUat.PurchaseOrderReference.PurchaseOrderClient');
            service2 = AifUtil::createServiceClient(type);

            //purchTable  = PurchTable::find(_purchTable.PurchId);
            purchTotals = purchTotals::newPurchTable(purchTable);

           select reverse vendPurchOrderJour where vendPurchOrderJour.PurchId == purchTable.PurchId;

         //   select purchLine where purchLine.PurchId == purchTable.PurchId;
            select purchLine_In where purchLine_In.PurchLine == purchLine.recid;
            select salesTaxFormTypes_IN where salesTaxFormTypes_IN.RecId == purchLine_In.SalesTaxFormTypes_IN;
            select inventDim where inventDim.inventDimId == purchLine.InventDimId;

            purchTotals.calc();
            tmpTax.setTmpData(purchTotals.tax().tmpTaxWorkTrans());

             // Showing taxes with tax value
             conTax = conNull();
             while   select tmpTax where tmpTax.sourcerecid ==purchline.recid && tmpTax.sourcetableid == purchline.tableid
            {
                i++;
                select taxtable where taxtable.taxcode == tmpTax.TaxCode;
                component   = TaxComponentTable_IN::find(taxtable.TaxComponentTable_IN).Name;
                taxcomp     = component;
                taxvalue    = num2str(abs(tmpTax.TaxAmount/(purchline.PurchQty)),0,2,1,2); //changes
                conTax      = conins(conTax,i,taxcomp+':'+taxvalue);
            }
                contaxinfo = con2StrUnlimited(conTax,'|');
                i=0;

               // send characteristics value
                conchar = connull();
             while select configChoice  where configChoice.ConfigId == InventDim.configId && configChoice.ItemId == purchLine.itemid
            {
                inventTable.clear();
                select inventTable where inventTable.ItemId == configChoice.chosenitemid;
                j++;
                charname    = inventTable.comp_CharacteristicName;
                charvalue   = inventTable.comp_CharacteristicValue;
                conchar     = conIns(conchar,j,charname+':'+charvalue);
            }
               concharinfo = con2StrUnlimited(conchar,'|');
               j =0;

                concharges = conNull();
            while select * from MarkupTrans  where MarkupTrans.TransTableId == purchline.TableId
                                                && MarkupTrans.TransRecId == purchline.RecId
            {
                k++;
                transTxt     = MarkUpTrans.Txt;
                if(transTxt == "Cost of FOC Product")
                    markupAmount =  num2str((MarkUpTrans.Value*purchline.PurchQty),0,2,1,2);
                else
                    markupAmount =  num2str((abs(MarkUpTrans.Value)),0,2,1,2);
                //markupAmount =  num2str((MarkUpTrans.Value*purchline.PurchQty),0,2,1,2);  changes sameer

                concharges = conins(concharges,k,transTxt+':'+markupAmount);
            }

               conchargesinfo = con2StrUnlimited(concharges,'|');
               k=0;


                confiledetails = conNull();
           while select docuRef where  docuRef.refrecid == purchTable.RecId && docuRef.RefCompanyId == purchTable.dataAreaId
                                    && docuRef.RefTableId == purchTable.TableId
            {
                f++;
                select docuValue where docuValue.RecId == docuRef.ValueRecId;
                sfilename = docuValue.FileName;
                sfilepath = docuRef.path();
                sfileext  = docuValue.FileType;
                sfullname = sfilepath + sfilename + "." +sfileext;
                confiledetails = conins(confiledetails,f,sfullname+':'+enum2Value(docuRef.comp_FileOrderType)+':'+enum2value(docuRef.comp_FileType));
               // info(strFmt("%1\%2 %3 %4 %5",docuRef.path(),docuValue.FileName,docuRef.comp_FileOrderType,docuRef.comp_FileType));
            }
               strfiledetails = con2StrUnlimited(confiledetails,'|');
               f=0;

           _success = service2.Synchronize_PurchaseOrder(PurchTable.purchid,
           VendPurchOrderJour.PurchOrderDate,enum2str(PurchTable.DocumentState),inventdim.InventSiteId,inventdim.InventLocationId,purchLine.ItemId,
           concharinfo,real2int(purchLine.PurchQty),purchLine.PurchPrice,purchTable.CurrencyCode,purchTable.payment,purchTable.DlvTerm,
           purchTable.comp_IncoTerms,purchTable.comp_DoAmount,purchTable.comp_DoDate,conchargesinfo,conTaxinfo,purchTable.comp_ManualExcise,
        str2int(purchTable.comp_ExciseDutyRefund),VendPurchOrderJour.Amount,purchTable.comp_FocInformation,purchTable.comp_FinancerUnderHyp,
        purchLine.TaxGroup,purchTable.OrderAccount,strfiledetails,SalesTaxFormTypes_IN.FormType,enum2str(purchTable.comp_SalesTypeExciseDuty),enum2str(purchLine.comp_WarrantyCode));

           info(strFmt("%1-%2-%3-%4-%5-%6-%7-%8-%9-%10-%11-%12-%13-%14-%15-%16-%17-%18-%19-%20-%21-%22-%23-%24-%25-%26-%27-%28-%29",PurchTable.purchid,
           VendPurchOrderJour.PurchOrderDate,enum2str(PurchTable.DocumentState),inventdim.InventSiteId,inventdim.InventLocationId,purchLine.ItemId,
           concharinfo,real2int(purchLine.PurchQty),purchLine.PurchPrice,purchTable.CurrencyCode,purchTable.payment,purchTable.DlvTerm,
           purchTable.comp_IncoTerms,purchTable.comp_DoAmount,purchTable.comp_DoDate,conchargesinfo,conTaxinfo,purchTable.comp_ManualExcise,
        str2int(purchTable.comp_ExciseDutyRefund),VendPurchOrderJour.Amount,purchTable.comp_FocInformation,purchTable.comp_FinancerUnderHyp,
        purchLine.TaxGroup,purchTable.OrderAccount,strfiledetails,SalesTaxFormTypes_IN.FormType,enum2str(purchTable.comp_SalesTypeExciseDuty),enum2str(purchLine.comp_WarrantyCode)));
                  if(_success != "Success")
                {
                    error(strFmt("%1 for PO %2",_success,purchTable.PurchId));
                }
                else
                {
                    ttsBegin;
                    select forUpdate purchLineupd where purchLineupd.PurchId == purchTable.PurchId && purchLineupd.ItemId == purchLine.ItemId;
                    purchLineupd.comp_IsPurchlineSend = NoYes::Yes;
                    purchLineupd.update();
                    ttsCommit;
                    info(strFmt(" %1 is created in CRM",purchTable.PurchId));
                }
        }//try

        catch(Exception::CLRError)
        {
            ex = CLRInterop::getLastException();
            info(CLRInterop::getAnyTypeForObject(ex.ToString()));
        }
        comp_RetailExceptionActivityLog::logEvent(strFmt("Purch Order Creation %1:",PurchTable.purchid),infolog.export());
    }//while
    //comp_RetailExceptionActivityLog::logEvent('Purch Order Creation:',infolog.export());
  //  comp_RetailExceptionActivityLog::logEvent(strFmt("Purch Order Creation %1:",PurchTable.purchid),infolog.export());


}

PURCHASE ORDER CATEGORY PRODUCT RECEIPT CALLER CLASS

Purchase category

1.       PO invoice and product receipt

Changes have been done in the forms clicked ok methods of post and close ok
Blow is code for product receipt
// po category start

    VendPackingSlipJour         vendPackingSlipJour;
    Vik_PurchaseOrderCategory   poCategory;
    int                         daydiff,prodreceiptcount;
    PurchTable                  purchTableLocal;

    if(curext() == 'SDS')
    {
        if( classidget(purchFormLetter) == classnum(PurchFormLetter_PackingSlip)) //&& (purchTable.Vik_PurchaseCategory != Vik_PurchaseCategory::None))
        {
            select purchTableLocal where purchTableLocal.PurchId == purchFormLetter.purchTable().PurchId;
            select poCategory where poCategory.Vik_PurchaseCategory == purchTableLocal.Vik_PurchaseCategory;
            select count(RecId) from vendPackingSlipJour where vendPackingSlipJour.PurchId == purchTableLocal.PurchId;

            prodreceiptcount = int642int(vendPackingSlipJour.RecId);

            if(prodreceiptcount > poCategory.DeliveryTimes)
                throw error(strFmt(" The Product receipt for Purchase Order %1 has exceeded for the category %2",purchTableLocal.PurchId,purchTableLocal.Vik_PurchaseCategory));
        }

    }
 //   po category end

Below is code for Invoice

void clicked()
{
    // po category start

    Vik_PurchaseOrderCategory   poCategory;
    int                         daydiff,prodreceiptcount;
    PurchTable                  purchTableLocal;



    if(curext() == 'SDS')
    {
        select purchTableLocal where purchTableLocal.PurchId == purchFormLetter.purchTable().PurchId;
        if( classidget(purchFormLetter) == classnum(PurchFormLetter_Invoice) && (purchTable.Vik_PurchaseCategory != Vik_PurchaseCategory::None))
        {
           
            select poCategory where poCategory.Vik_PurchaseCategory == purchTableLocal.Vik_PurchaseCategory;
          //  daydiff = purchTableLocal.createdDateTime - DateTimeUtil::getSystemDateTime();
            daydiff = DateTimeutil::getDifference(DateTimeUtil::getSystemDateTime(), purchtablelocal.createddatetime) / 86400;
            if(daydiff > poCategory.ExpireDays)
                throw error(strFmt(" The Purchase Order %1 has exceeded the expire days for the category %2",purchTableLocal.PurchId,purchTableLocal.Vik_PurchaseCategory));
        }

    }
 //   po category end
    // <GEERU>
    if (SysCountryRegionCode::isLegalEntityInCountryRegion([ #isoRU ]) && ! purchFormLetter.checkBeforePost())
    {
        return;
    }
    // </GEERU>
    super();
}

Related Posts Plugin for WordPress, Blogger...