Create and Post Pending vendor invoice using X++

 

The class below will be used to create and post pending vendor invoices along with the project information. In my case, I retrieved the data from a staging table and created the pending vendor invoices.

public class CreatePendingVendorInvoice
{
    VendInvoiceInfoTable    vendInvoiceInfoTable;
    ProjParameters          projParameters;
    str                     ItemCompany;
    str                     ItemProjId;
    RecId                   ItemProjDimension;

    /// <summary>
    /// This method will be used to create the Pending vendor invoice header
    /// </summary>
    /// <param name = "_stagingTrans"></param>
    public void createPendingVendorInvoiceHeader(DaxStagingTrans    _stagingTrans)
    {
        projParameters = ProjParameters::find();

        NumberSeq   numberSeq = NumberSeq::newGetNum(ProjParameters::invoiceId());

        vendInvoiceInfoTable.clear();
        vendInvoiceInfoTable.initValue();

        vendInvoiceInfoTable.DocumentOrigin          = DocumentOrigin::Manual;
        vendInvoiceInfoTable.InvoiceAccount          = this.getVendorAccount();
        vendInvoiceInfoTable.defaultRow(null, null, true);

        vendInvoiceInfoTable.Num                     = numberSeq.num();
        vendInvoiceInfoTable.VendInvoiceSaveStatus   = VendInvoiceSaveStatus::Pending;
        vendInvoiceInfoTable.DocumentDate            = _stagingTrans.JournalDate;
        vendInvoiceInfoTable.ReceivedDate            = _stagingTrans.JournalDate;
        vendInvoiceInfoTable.TransDate               = _stagingTrans.JournalDate;
        vendInvoiceInfoTable.LastMatchVariance       = LastMatchVarianceOptions::OK;
        vendInvoiceInfoTable.RequestStatus           = VendInvoiceRequestStatus::Approved;
        vendInvoiceInfoTable.insert();

        this.createPendingVendorInvoiceLine(_stagingTrans);
    }

    /// <summary>
    /// This method used to create the Pending Vendor incvoice lines
    /// </summary>
    /// <param name = "_stagingTrans">DaxStagingTrans</param>
    public void createPendingVendorInvoiceLine(DaxStagingTrans    _stagingTrans)
    {
        VendInvoiceInfoLine     vendInvoiceInfoLine;

        vendInvoiceInfoLine.clear();
        vendInvoiceInfoLine.initValue();
        vendInvoiceInfoLine.DeliveryName        = vendInvoiceInfoTable.DeliveryName;
        vendInvoiceInfoLine.TableRefId          = vendInvoiceInfoTable.TableRefId;
        vendInvoiceInfoLine.currencyCode        = vendInvoiceInfoTable.CurrencyCode;
        vendInvoiceInfoLine.LineNum             = 1;
        vendInvoiceInfoLine.InvoiceAccount      = vendInvoiceInfoTable.InvoiceAccount;
        vendInvoiceInfoLine.OrderAccount        = vendInvoiceInfoTable.OrderAccount;
        vendInvoiceInfoLine.ProcurementCategory = projParameters.ANTHProcurCategory;
        vendInvoiceInfoLine.modifiedField(fieldNum(VendInvoiceInfoLine, ProcurementCategory));
        vendInvoiceInfoLine.ReceiveNow          = 1;
        vendInvoiceInfoLine.PurchUnit           = projParameters.ANTHPurchUnit;
        vendInvoiceInfoLine.DocumentOrigin      = DocumentOrigin::Manual;

        container           conAttribute            = ANTHConcurCreateGeneralJournalService::getFDFromParameters();
        container           convalue                = this.getProjectDimensions(conAttribute, _stagingTrans);

        vendInvoiceInfoLine.DefaultDimension    = ANTHConcurCreateGeneralJournalService::createDefaultDimension(conAttribute, convalue);
        vendInvoiceInfoLine.insert();

        if (vendInvoiceInfoLine)
        {
            VendInvoiceInfoLine_Project   vendInvoiceInfoLine_Project;

            vendInvoiceInfoLine_Project.VendInvoiceInfoLineRefRecId = vendInvoiceInfoLine.RecId;
            vendInvoiceInfoLine_Project.ProjDataAreaId              = ItemCompany;
            vendInvoiceInfoLine_Project.ProjId                      = ItemProjId;
            vendInvoiceInfoLine_Project.ProjCategoryId              = _stagingTrans.Expensecategory;
            vendInvoiceInfoLine_Project.ProjLinePropertyId          = CreatePendingVendorInvoice::findLineProperty();
            vendInvoiceInfoLine_Project.TransDate                   = _stagingTrans.JournalDate;
            vendInvoiceInfoLine_Project.ProjSalesUnitId             = UnitOfMeasure::findBySymbol(projParameters.ANTHPurchUnit).RecId;
            vendInvoiceInfoLine_Project.ProjSalesCurrencyId         = _stagingTrans.Currency;
            vendInvoiceInfoLine_Project.TransferCurrency            = _stagingTrans.Currency;
            vendInvoiceInfoLine_Project.TransferPrice               = _stagingTrans.Costamount;
            vendInvoiceInfoLine_Project.ANTHConcurTransactionID     = _stagingTrans.ConcurTransactionID;
            vendInvoiceInfoLine_Project.ProjTaxGroupId              = '';
            vendInvoiceInfoLine_Project.ProjTaxItemGroupId          = '';
            vendInvoiceInfoLine_Project.insert();
        }
    }

    /// <summary>
    /// This method used to Post the Vendor invoice
    /// </summary>
    public void postInvoice()
    {
        PurchFormLetter         purchFormLetter;

        purchFormLetter = PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTable);
        purchFormLetter.purchParmUpdate(null);
        purchFormLetter.parmId('');
        purchFormLetter.initNewPurchParmUpdate();
        purchFormLetter.proforma(false);
        purchFormLetter.reArrangeNow(false);

        purchFormLetter.update(vendInvoiceInfoTable,
                                vendInvoiceInfoTable.Num,
                                purchFormLetter.transDate(),
                                PurchUpdate::All,
                                AccountOrder::None,
                                purchFormLetter.proforma(),
                                purchFormLetter.printFormLetter(),
                                false,
                                purchFormLetter.creditRemaining(),
                                conNull(),
                                true);

    }

    /// <summary>
    /// This method used to retrieve the Default Dimensions
    /// </summary>
    /// <param name = "_attribute">Container</param>
    /// <param name = "_stagingTrans">DaxStagingTrans</param>
    /// <returns>Container</returns>
    private container getProjectDimensions(container    _attribute, DaxStagingTrans _stagingTrans)
    {
        // DeptCC-ProfitCenter-PRODGRP-UnifiedProductID-Customer-Vendor
        return [_stagingTrans.Costcenter, projParameters.DAXProfitCenter,
                CreatePendingVendorInvoice::getDimensionValue(orderItemProjDimension, conPeek(_attribute, 3)),
                CreatePendingVendorInvoice::getDimensionValue(orderItemProjDimension, conPeek(_attribute, 4)),
                CreatePendingVendorInvoice::getDimensionValue(orderItemProjDimension, conPeek(_attribute, 5))];
    }

    /// <summary>
    /// Gets the display value from the Dimension Recid
    /// </summary>
    /// <param name = "_dimension">RecId</param>
    /// <param name = "_dimensionName">Str</param>
    /// <returns>DimensionValue</returns>
    public static DimensionValue getDimensionValue(RecId  _dimension, str _dimensionName)
    {
        DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage ;
        DimensionAttribute                  dimensionAttribute;
        DimensionValue                      dimensionValue;

        dimensionAttributeValueSetStorage = dimensionAttributeValueSetStorage::find(_dimension);

        dimensionAttribute  = dimensionAttribute::findbyname(_dimensionName);
        dimensionValue      = dimensionAttributeValueSetStorage.getDisplayValueByDimensionAttribute(dimensionAttribute.recId);

        return dimensionValue;
    }

    public void OrderDetails(container  _OrderCon)
    {
        [orderItemCompany, orderItemProjId, orderItemProjDimension] = _OrderCon;
    }

    public str getVendorAccount()
    {
        DirPartyView    partyView;
        DirPartyRecId   partyId = CompanyInfo::findDataArea(orderItemCompany).RecId;

        select firstonly AccountNum from partyView
            where partyView.Party == partyId && partyView.RoleType == DirPartyRoleType::Vendor && partyView.DataArea == curExt();

        return partyView.AccountNum;
    }

    public static CreatePendingVendorInvoice construct(container  _OrderCon)
    {
        CreatePendingVendorInvoice    concurCreatePendingVendorInvoice = new CreatePendingVendorInvoice();

        concurCreatePendingVendorInvoice.OrderDetails(_OrderCon);

        return concurCreatePendingVendorInvoice;
    }

    public static str findLineProperty()
    {
        ProjLineProperty    projLineProperty;

        select firstonly LinePropertyId from projLineProperty
            where projLineProperty.ToBeInvoiced == false;

        return projLineProperty.LinePropertyId;
    }

    public static ResourceView findResource(str _resourceId)
    {
        ResourceView resource;

        select firstonly resource
            where resource.ResourceId == _resourceId;

        return resource;
    }

}

Thanks !!

No comments:

Post a Comment