Wednesday, January 25, 2023

Business event class

// Business event Contract

[DataContractAttribute]

final class XYZ_ICProdRcptLineContract

{

  SalesOrderedQty             salesQty;

  SalesStatus                 salesLineStatus;

  SalesId                     salesId;

  ItemId                      itemId;

  InventTransId               icInventTransId;


    [DataMember("SalesQty")]

    public SalesOrderedQty parmSalesQty(SalesOrderedQty _salesQty = salesQty)

    {

      salesQty = _salesQty;

      return salesQty;

    }


    [DataMember("IntercompanyInventTransId")]

    public InventTransId parmICInventTransId(InventTransId _icInventTransId = icInventTransId)

    {

      icInventTransId = _icInventTransId;

      return icInventTransId;

    }


    [DataMember("SalesStatus")]

    public SalesStatus parmSalesLineStatus(SalesStatus _salesLineStatus = salesLineStatus)

    {

      salesLineStatus = salesLineStatus;

      return salesLineStatus;

    }


    [DataMember("SalesId")]

    public SalesId parmSalesId(SalesId _salesId = salesId)

    {

      salesId = _salesId;

      return salesId;

    }


    [DataMember("ItemNumber")]

    public ItemId parmItemNumber(ItemId _itemId = itemId)

    {

      itemId = _itemId;

      return itemId;

    }


}


// Request class

final class XYZ_ICProdRcptPostingRequest

{

    DataAreaId                  legalEntity;

    SalesId                     salesId;

    PurchId                     purchId;

    InterCompanyPurchId         icPurchId;

    SalesStatus                 salesStatus;

    List                        salesLineList;

    PackingSlipId               packingSlipId;


    [DataMember("PackingslipId")]

    public packingSlipId parmpackingSlipId(PackingSlipId _packingSlipId = packingSlipId)

    {

      packingSlipId = _packingSlipId;

      return packingSlipId;

    }


    [DataMember("PurchId")]

    public PurchId parmPurchId(PurchId _purchId = purchId)

    {

      purchId = _purchId;

      return purchId;

    }

    [DataMember("SalesId")]

    public SalesId parmSalesId(SalesId _salesId = salesId)

    {

      salesId = _salesId;


      return salesId;

    }


   [DataMember("DataAreaId")]

    public DataAreaId parmDataArea(DataAreaId _legalEntity = legalEntity)

    {

      legalEntity = _legalEntity;

      return legalEntity;

    }


    [DataMember("SalesLine"),

    AifCollectionType('return', Types::Class, classStr(XYZ_ICProdRcptLineContract)),

    AifCollectionType('_salesLine', Types::Class, classStr(XYZ_ICProdRcptLineContract))]

    public List parmSalesLine(List _salesLine  =  salesLineList)

    {

      salesLineList   = _salesLine;

      return  salesLineList;

    }

    public static XYZ_ICProdRcptPostingRequest construct()

    {

      return new XYZ_ICProdRcptPostingRequest();

    }

}


// Response class

[DataContractAttribute]

final class XYZ_ICProdRcptPostingResponse

{

    private boolean     success;

    private str         errorMessage;

    List                valueList;

    PackingSlipId       packingSlipId;


    [DataMember('SalesOrderNum')]

    public PackingSlipId parmpackingSlipId(SalesId _packingSlipId = packingSlipId)

    {

      packingSlipId = _packingSlipId;

      return packingSlipId;

    }


    [DataMember("ErrorMessage")]

    public str parmErrorMessage(str _value = errorMessage)

    {

      errorMessage = _value;

      return errorMessage;

    }


    [DataMember("Success")]

    public Boolean parmSuccess(Boolean _value = success)

    {

      success = _value;

      return success;

    }


    [DataMember("Value"),

    AifCollectionType("Header",Types::Class,classStr(XYZ_ICProdRcptPostingRequest)),

    AifCollectionType("return",Types::Class,classStr(XYZ_ICProdRcptPostingResponse))]

    public List parmValue(List _value = valueList)

    {

      valueList = _value;

      return valueList;

    }

    public static XYZ_ICProdRcptPostingResponse construct()

    {

      return new XYZ_ICProdRcptPostingResponse();

    }

}


// service class

final class XYZ_ICProdRcptPostingService

{

  PurchTable                          purchTable;

  PurchLine                           purchLine;

  XYZ_ICProdRcptPostingRequest        req;

  XYZ_ICProdRcptLineContract          lineContract;

  XYZ_ICProdRcptPostingResponse       response;

  ListEnumerator                      leHeader;

  ListEnumerator                      lineList;

  PurchFormLetter                     purchFormLetter;

  PurchFormletterParmData             purchFormLetterParmData;

  PurchParmUpdate                     purchParmUpdate;

  PurchParmTable                      purchParmTable;

  PurchParmLine                       purchParmLine;

  VendPackingSlipJour                 vendPackingSlipJour;

  public XYZ_ICProdRcptPostingResponse postProductReceipt(XYZ_ICProdRcptPostingRequest            request)

  {

      List            salesLineList = new List(Types::Class);

      //leHeader                      = header.getEnumerator();

      salesLineList                 = request.parmSalesLine();

      lineList                      = salesLineList.getEnumerator();

      lineList.reset();


      ttsbegin;

      changecompany(request.parmDataArea())

      {

            select purchTable where purchTable.PurchId == request.parmPurchId();

            /*

            //Validate if Purchase order is in Confirmed state.

            if(purchtable.DocumentState != VersioningDocumentState::Confirmed)

            {

              throw error(strFmt("The purchase order should be in confirmed state."));

            }

            */


            purchFormLetterParmData  = PurchFormletterParmData::newData(                                                          DocumentStatus::PackingSlip,

                                        VersioningUpdateType::Initial);

        

            purchFormLetterParmData.parmOnlyCreateParmUpdate(true);

            purchFormLetterParmData.createData(false);

        

            purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

        

            purchParmTable.clear();

            purchParmTable.TransDate                = SystemDateGet();

            purchParmTable.Ordering                 = DocumentStatus::PackingSlip;

            purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;

            purchParmTable.Num                      = request.parmpackingSlipId(); //packingSlipId;//To-Do                    check for number sequence

            purchParmTable.PurchId                  = purchTable.PurchId;

            purchParmTable.PurchName                = purchTable.PurchName;

            purchParmTable.DeliveryName             = purchTable.DeliveryName;

            purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;

            purchParmTable.OrderAccount             = purchTable.OrderAccount;

            purchParmTable.CurrencyCode             = purchTable.CurrencyCode;

            purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;

            purchParmTable.ParmId                   = purchParmUpdate.ParmId;

            purchParmTable.insert();

            

            while (lineList.moveNext())

            {

              lineContract =  lineList.current();


            // Set PurchParmLine table

                select purchLine

                    where purchLine.PurchId == purchTable.purchId

                        &&  purchLine.ItemId  == lineContract.parmItemNumber()

                            &&  purchLine.InterCompanyInventTransId == lineContract.parmICInventTransId();


                if (purchLine)

                {

                  purchParmLine.InitFromPurchLine(purchLine);

                  purchParmLine.ReceiveNow            = PurchLine.PurchQty;

                  purchParmLine.ParmId                = purchParmTable.ParmId;

                  purchParmLine.TableRefId            = purchParmTable.TableRefId;

                  purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);

                  purchParmLine.setLineAmount();

                  purchParmLine.insert();

                }

            }

        

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

            purchFormLetter.transDate(systemDateGet());

            purchFormLetter.proforma(false);

            purchFormLetter.specQty(PurchUpdate::All);

            purchFormLetter.purchTable(purchTable);

            //purchFormLetter.showQueryForm(false);


            // This is the ID we hard code as the product receipt ID, if we do the posting via UI

            // user would have the option to manually enter this value

            purchFormLetter.parmParmTableNum(purchParmTable.ParmId);

            purchFormLetter.parmId(purchParmTable.ParmId);

            purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());

            purchFormLetter.run();

            info(strFmt("Product Receipt posted successfully."));

      }

            ttscommit;    

            select vendPackingSlipJour where vendPackingSlipJour.PurchId == purchTable.PurchId; 

            response=    XYZ_ICProdRcptPostingResponse::construct();

            response.parmpackingSlipId(vendPackingSlipJour.PackingSlipId);

            return response;

  }


  public void purchaseOrderInvoice(XYZ_ICProdRcptPostingRequest _request)

  {

    DocumentStatus  documentStatus;

    PurchTable      purchTableLoc;


    if (_request.parmPurchId())

    {

      purchTable = PurchTable::find(_request.parmPurchId(), true);


      if (purchTable && purchTable.PurchStatus != PurchStatus::Invoiced)

      {

        purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);

        purchFormLetter.update(purchTable, "Inv_"+purchTable.PurchId, systemdateget());

      }


      select * from purchTableLoc

          where purchTableLoc.PurchId == _request.parmPurchId();

      if (purchTableLoc.RecId && purchTableLoc.DocumentStatus == DocumentStatus::Invoice)

      {

        documentStatus = purchTable.DocumentStatus;

        response.parmSuccess(true);

        response.parmPackingSlipId('');

        response.parmErrorMessage(strfmt("Posted invoiced journal for purchase order                                         %1",purchTableLoc.PurchId));

      }

      else

      {

        response.parmSuccess(false);

        response.parmPackingSlipId('');

        response.parmErrorMessage(strFmt("Failed to post invoice for purchase order %1",                                     purchTableLoc.PurchId));

      }

    }

   // return response;

  }

      public static XYZ_ICProdRcptPostingService construct()

      {

        return new XYZ_ICProdRcptPostingService();

      }


}


No comments:

Post a Comment

Build Explained

Useful Blogs. https://axtechsolutions.blogspot.com/2018/08/performing-builds-in-d365.html https://community.dynamics.com/blogs/post/?postid=...