adsenseheader

Monday, November 4, 2019

Additional voucher creation for an Procurement item, Correct and Cancel Product receipt for Purchase Order in D365 for opeartions

Hi Guys,

Below code shall help in  creating additional Vouchers for a Procurement item and also correcting and canceling the Product receipt.

Distributes amount based on the Accounting distribution setup on the Purchase order line


[ExtensionOf(classStr(PurchPackingSlipJournalPost))]
final class ABC_PurchPackingSlipJournalPost_Extension
{
    protected void postSingleLine(boolean _countryRegion_RU,
                                  boolean _hasQualityOrder,
                                  boolean _isExciseEnableIN,
                                  boolean _isVATEnableIN,
                                  boolean _isCustomsEnableIN,
                                  boolean _isConfigurationkeyRetailEnabled)
    {
        LedgerDimensionAccount          ledgerDimensionId;
        AccountingDistribution          accountingDistribution;
        LedgerDimensionDefaultAccount   ledgerDefaultAccount;
        LedgerDimensionDefaultAccount   ledgerDefaultAccountOffset;
        DimensionDefault                defaultDim;
        Amount                          gstAmount;
        Amount                          creditAmount;
        Amount                          debitAmount;
        LedgerVoucher                   ledgerVoucher;
        LedgerVoucherTransObject        ledgerVoucherTransObject;
        CurrencyCode                    standardCurrencyCode;
        LedgerVoucherObject             ledgerVoucherObject;
        CurrencyExchangeHelper          currencyExchHelper;

        xyz_POItemFeeTable              POItemFeeTable;
        PurchLine                       purchLineLocal = this.sourceLine();

        next postSingleline (_countryRegion_RU,     _hasQualityOrder, _isExciseEnableIN, _isVATEnableIN, _isCustomsEnableIN,  _isConfigurationkeyRetailEnabled);

        PurchParmLine                   purchParmLine = this.purchParmLine;
        PurchParmLine                   purchParmLineloc = this.purchParmLine;
        SourceDocumentLine              sourceDocumentLine;
        Qty                             res = purchParmLineloc.purchQtyCorrection();
        AccountingDistribution          accDist;
        AccountingDistribution          revAccDist;
        SourceDocumentLine              sourceDocLine;
        PurchTable                      purchTable = PurchTable::find(purchline.PurchId);
        AllocationFactor                allocationFactor;

        while select forupdate RecId from sourceDocLine
                where
                    sourceDocLine.SourceDocumentHeader == purchTable.SourceDocumentHeader
            join forupdate accDist
                where
                    accDist.SourceDocumentLine == sourceDocLine.RecId &&
                    accDist.ReferenceRole != AccountingDistributionReferenceRole::Reversing
            notexists join revAccDist
                where
                    (   revAccDist.ReferenceDistribution == accDist.RecId &&
                        revAccDist.SourceDocumentLine == sourceDocLine.RecId &&
                        revAccDist.ReferenceRole == AccountingDistributionReferenceRole::Reversing
                    )
                    ||
                    (
                        revAccDist.ReferenceDistribution == accDist.RecId &&
                        revAccDist.SourceDocumentLine == sourceDocLine.RecId &&
                        revAccDist.ReferenceRole == AccountingDistributionReferenceRole::Adjusting
                    )
        {
            if (accDist.AllocationFactor == 1.0)
            {
                continue;
            }

            allocationFactor += accDist.AllocationFactor;
         
            if (purchLineLocal.ProcCatRecoverable_xyz== NoYes::Yes)
            {
                Amount exclGst =   xyz_ItemFeeTable::findByPurchLine(purchLineLocal.recId).RecoveryAmountExclGST;
                if (this.versioningUpdateType == VersioningUpdateType::Correction)
                {
                    gstAmount = ((exclGst /purchLineLocal.PurchQty ) * res) * accDist.AllocationFactor;
                    creditAmount = -decRound(gstAmount,2);
                    debitAmount  = creditAmount * -1;
                }

                else if (this.versioningUpdateType == VersioningUpdateType::Cancel)
                {
                    gstAmount = xyz_ItemFeeTable::findByPurchLine(purchLineLocal.recId).RecoveryAmountExclGST / purchLineLocal.PurchQty;
                    creditAmount = -(gstAmount * res) * accDist.AllocationFactor;
                    creditAmount = decRound(creditAmount,2);
                    debitAmount  = creditAmount * -1;
                }
                else
                {
                    gstAmount = 0 ;
                    creditAmount = ((xyz_ItemFeeTable::findByPurchLine(purchLineLocal.recId).RecoveryAmountExclGST /purchLineLocal.PurchQty ) * purchParmLineloc.ReceiveNow) * accDist.AllocationFactor  ;
                    creditAmount = decRound(creditAmount,2);
                    debitAmount  = -creditAmount;
                }
         

                ledgerDefaultAccount = InventPosting::accountItemLedgerDimensionFromParameters(InventPostingAccountItemLedgerDimensionParameters::newFromParameters(
                                                                                        InventAccountType::RevenueAccrual_xyz,
                                                                                        purchLineLocal.ItemId,
                                                                                        purchLineLocal.inventTable().itemGroupId(),
                                                                                        purchLineLocal.ProcurementCategory,
                                                                                        purchLineLocal.VendAccount,
                                                                                        purchLineLocal.VendGroup,
                                                                                        purchLineLocal.TaxGroup
                                                                                        ));
                ledgerDimensionId = LedgerDimensionFacade::serviceCreateLedgerDimension(ledgerDefaultAccount);

                ledgerVoucher = this.ledgerVoucher;
         
                ledgerVoucherObject = this.findOrCreateLedgerVoucherObject(this.getVoucher(), this.updateDate());
         
                standardCurrencyCode = CompanyInfoHelper::standardCurrency();
                currencyExchHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());

                ledgerVoucherTransObject =  LedgerVoucherTransObject::newTransactionAmountDefault(ledgerVoucherObject  ,
                                                                                                    LedgerPostingType::RevenueAccrual_xyz,
                                                                                                    ledgerDimensionId,
                                                                                                    standardCurrencyCode,
                                                                                                    debitAmount,
                                                                                                    currencyExchHelper);

                ledgerVoucher.addTrans(ledgerVoucherTransObject);

                //for credit account
                ledgerDefaultAccount = InventPosting::accountItemLedgerDimensionFromParameters(InventPostingAccountItemLedgerDimensionParameters::newFromParameters(
                                                                                        InventAccountType::RevenuePurchReceipt_xyz,
                                                                                        purchLineLocal.ItemId,
                                                                                        purchLineLocal.inventTable().itemGroupId(),
                                                                                        purchLineLocal.ProcurementCategory,
                                                                                        purchLineLocal.VendAccount,
                                                                                        purchLineLocal.VendGroup,
                                                                                        purchLineLocal.TaxGroup
                                                                                        ));

                defaultDim = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(accDist.ledgerdimension);

                ledgerDimensionId = LedgerDimensionFacade::serviceCreateLedgerDimension(ledgerDefaultAccount);
                ledgerDefaultAccountOffset = LedgerDimensionFacade::serviceMergeLedgerDimensions(ledgerDimensionId,accDist.ledgerdimension);
                ledgerVoucherTransObject =  LedgerVoucherTransObject::newTransactionAmountDefault(ledgerVoucherObject,
                                                                                                    LedgerPostingType::RevenuePurchReceipt_xyz,
                                                                                                    ledgerDefaultAccountOffset,
                                                                                                    standardCurrencyCode,
                                                                                                    creditAmount,
                                                                                                    currencyExchHelper);
                ledgerVoucherObject.addTrans(ledgerVoucherTransObject);
                ledgerVoucherObject.parmVoucherCheck(false);
            }

            if (allocationFactor == 1.0)
            {
                break ;
            }
        }
    }

}


Thanks,
Pradeep

No comments:

Post a Comment