Tuesday, June 11, 2019

OverRide Print Mgmt Email setting for To/CC setting in D365 FO


final class SAN_PrintMgmtDelegatesHandler
{
    /// <summary>
    /// Delegate handler for the getEmailAddressDelegate method of the <c>PrintMgmtDocType</c> class.
    /// </summary>
    /// <param name = "_docType"><c>PrintMgmtDocumentType</c> enumeration value.</param>
    /// <param name = "_purpose"><c>SrsPrintDestinationToken</c> object.</param>
    /// <param name = "_jour"><c>Common</c> object containing journal record.</param>
    /// <param name = "_printMgmtPrintDestinationTokens"><c>PrintMgmtPrintDestinationTokens</c> object.</param>
    /// <param name = "_result">The <c>EventHandlerResult</c> object.</param>
    [SubscribesTo(classstr(PrintMgmtDocType), delegatestr(PrintMgmtDocType, getEmailAddressDelegate))]
    public static void getEmailAddressDelegateHandler(PrintMgmtDocumentType _docType, SrsPrintDestinationToken _purpose, Common _jour, PrintMgmtPrintDestinationTokens _printMgmtPrintDestinationTokens, EventHandlerResult _result)
    {
        if(_docType == PrintMgmtDocumentType::PurchaseOrderRequisition)
        {
            _result.result(""); //TODO ur Logic
        }
        else
        {
            _result.result(SAN_PrintMgmtDelegatesHandler::getEmailAddress(_docType, _purpose, _jour, _printMgmtPrintDestinationTokens));
        }
    }

    private static str getEmailAddress(PrintMgmtDocumentType _docType, SrsPrintDestinationToken _purpose, Common _jour, PrintMgmtPrintDestinationTokens _printMgmtPrintDestinationTokens)
    {
        PrintMgmtPrintDestinationPartyType partyType;
        CustVendAC ac;
        DirPartyRecId partyRecId;
        CompanyId acCompany;

        if (!_printMgmtPrintDestinationTokens)
        {
            throw error(strFmt("@SYS318601", @"_printMgmtPrintDestinationTokens"));
        }

        if (_jour.TableId == tableNum(CustQuotationJour) && _jour.(fieldNum(CustQuotationJour, BusRelAccount)))
        {
            partyRecId = smmBusRelTable::find(_jour.(fieldNum(CustQuotationJour, BusRelAccount))).Party;
        }
        else
        {
            [partyType, ac] = SAN_PrintMgmtDelegatesHandler::getDestinationPartyTypeAndId(_docType, _jour);

            if (SAN_PrintMgmtDelegatesHandler::hasDataAreaIdField(_jour))
            {
                partyRecId = SAN_PrintMgmtDelegatesHandler::getPartyRecId(partyType, ac, _jour.DataAreaId);
            }
            else
            {
                partyRecId = SAN_PrintMgmtDelegatesHandler::getPartyRecId(partyType, ac);
            }
        }
       
        return _printMgmtPrintDestinationTokens.getEmailAddressForParty(partyType, partyRecId, _purpose);
    }

    private static boolean hasDataAreaIdField(Common _common)
    {
        boolean hasDataAreaId;

        if (_common)
        {
            DictTable dictTable = new DictTable(_common.TableId);
            hasDataAreaId = dictTable && dictTable.dataPrCompany();
        }

        return hasDataAreaId;
    }

    private static container getDestinationPartyTypeAndId(PrintMgmtDocumentType _docType, Common _jour)
    {
        // NOTE: mappings from _jour to a specific table are found in the CustVendAccountMap
        // The table mapped is the same table referenced in method getQueryTableId()
        CustVendAC getAccount(Common _common)
        {
            CustVendAccountMap accountMap = _common;

            return accountMap.Account;
        }

        container fiscalDocument_BRParty(FiscalDocument_BR _fiscalDocument_BR)
        {
            switch (_fiscalDocument_BR.FiscalDocumentAccountType)
            {
                case CustVendType_BR::Customer:
                    return [PrintMgmtPrintDestinationPartyType::Customer, _fiscalDocument_BR.FiscalDocumentAccountNum];
                case CustVendType_BR::Vendor:
                    return [PrintMgmtPrintDestinationPartyType::Vendor, _fiscalDocument_BR.FiscalDocumentAccountNum];
                default:
                    return [PrintMgmtPrintDestinationPartyType::Unknown, _fiscalDocument_BR.FiscalDocumentAccountNum];
            }
        }

        container bankChequeTable_ACParty(BankChequeTable _bankChequeTable)
        {
            switch (_bankChequeTable.RecipientType)
            {
                case BankChequeRecipientType::Cust:
                    return [PrintMgmtPrintDestinationPartyType::Customer, _bankChequeTable.RecipientAccountNum];
                case BankChequeRecipientType::Vend:
                    return [PrintMgmtPrintDestinationPartyType::Vendor, _bankChequeTable.RecipientAccountNum];
                default:
                    return [PrintMgmtPrintDestinationPartyType::Unknown, _bankChequeTable.RecipientAccountNum];
            }
        }

        switch (_docType)
        {
            case PrintMgmtDocumentType::ComplementaryInvoice_BR:
            case PrintMgmtDocumentType::TransferOrderFiscalDoc_BR:
                return fiscalDocument_BRParty(_jour);

            case PrintMgmtDocumentType::BankCheque:
                return bankChequeTable_ACParty(_jour);

            default:
                switch (SAN_PrintMgmtDelegatesHandler::getPartyType(_docType, _jour))
                {
                    case PrintMgmtPrintDestinationPartyType::Customer:
                        return [PrintMgmtPrintDestinationPartyType::Customer, getAccount(_jour)];
                    case PrintMgmtPrintDestinationPartyType::Vendor:
                        return [PrintMgmtPrintDestinationPartyType::Vendor, getAccount(_jour)];
                }
        }

        return conNull();
    }

    private static DirPartyRecId getPartyRecId(PrintMgmtPrintDestinationPartyType _partyType, CustVendAC _ac, CompanyId _acCompany = curExt())
    {
        DirPartyRecId getCustParty(CustAccount _account)
        {
            CustTable custTable;

            changecompany (_acCompany)
            {
                select Party from custTable where custTable.AccountNum == _account;
            }

            return custTable.Party;
        }

        DirPartyRecId getVendParty(VendAccount _account)
        {
            VendTable vendTable;

            changecompany(_acCompany)
            {
                select Party from vendTable where vendTable.AccountNum == _account;
            }

            return vendTable.Party;
        }

        switch (_partyType)
        {
            case PrintMgmtPrintDestinationPartyType::Customer:
                return getCustParty(_ac);
            case PrintMgmtPrintDestinationPartyType::Vendor:
                return getVendParty(_ac);
            case PrintMgmtPrintDestinationPartyType::Unknown:
                return 0;
        }

        throw error(Error::wrongUseOfFunction(funcName()));
    }

    private static PrintMgmtPrintDestinationPartyType getPartyType(PrintMgmtDocumentType _docType, Common _jour = null)
    {
        PrintMgmtPrintDestinationPartyType fiscalDocument_BRParty(FiscalDocument_BR _fiscalDocument_BR)
        {
            switch (_fiscalDocument_BR.FiscalDocumentAccountType)
            {
                case CustVendType_BR::Customer:
                    return PrintMgmtPrintDestinationPartyType::Customer;
                case CustVendType_BR::Vendor:
                    return PrintMgmtPrintDestinationPartyType::Vendor;
            }

            return PrintMgmtPrintDestinationPartyType::Unknown;
        }

        PrintMgmtPrintDestinationPartyType bankChequeTable_ACParty(BankChequeTable _bankChequeTable)
        {
            switch (_bankChequeTable.RecipientType)
            {
                case BankChequeRecipientType::Cust:
                    return PrintMgmtPrintDestinationPartyType::Customer;
                case BankChequeRecipientType::Vend:
                    return PrintMgmtPrintDestinationPartyType::Vendor;
            }

            return PrintMgmtPrintDestinationPartyType::Unknown;
        }

        switch (_docType)
        {
            case PrintMgmtDocumentType::SIProjInvoice:
           case PrintMgmtDocumentType::SIProjInvoiceWithBR:
            case PrintMgmtDocumentType::SIManagedProjInvoice:
            case PrintMgmtDocumentType::ProjCustRetentionReleaseInvoice:
            case PrintMgmtDocumentType::ProjectInvoice:
            case PrintMgmtDocumentType::SalesOrderInvoice:
            case PrintMgmtDocumentType::SalesFreeTextInvoice:
            case PrintMgmtDocumentType::InventPickList:
            case PrintMgmtDocumentType::SalesOrderPackingSlip:
            case PrintMgmtDocumentType::SalesOrderConfirmation:
            case PrintMgmtDocumentType::Confirmation:
            case PrintMgmtDocumentType::ProjectQuotation:
            case PrintMgmtDocumentType::Quotation:
            case PrintMgmtDocumentType::SalesOrderInvoice4Paym_RU:
            case PrintMgmtDocumentType::SalesFreeTextInvoice4Paym_RU:
            case PrintMgmtDocumentType::WMSBillOfLading_RU:
            case PrintMgmtDocumentType::WMSBilOfLadingTranspDocument_RU:
            case PrintMgmtDocumentType::CustAccountStatement:
            case PrintMgmtDocumentType::CustCollectionLetter:
            case PrintMgmtDocumentType::CustInterestNote:
            case PrintMgmtDocumentType::SalesAgreementConfirmation:
            case PrintMgmtDocumentType::SalesAdvanceInvoice:
            case PrintMgmtDocumentType::CustPaymAdvice:
                return PrintMgmtPrintDestinationPartyType::Customer;

            case PrintMgmtDocumentType::PurchaseOrderInvoice:
            case PrintMgmtDocumentType::PurchaseOrderPackingSlip:
            case PrintMgmtDocumentType::PurchaseOrderReceiptsList:
            case PrintMgmtDocumentType::PurchaseOrderRequisition:
            case PrintMgmtDocumentType::PurchRFQ:
            case PrintMgmtDocumentType::PurchRFQAccept:
            case PrintMgmtDocumentType::PurchRFQReject:
            case PrintMgmtDocumentType::PurchRFQReturn:
            case PrintMgmtDocumentType::PurchOrderInvoice4Paym_RU:
            case PrintMgmtDocumentType::PurchAgreementConfirmation:
            case PrintMgmtDocumentType::PurchaseAdvanceInvoice:
            case PrintMgmtDocumentType::PurchaseOrderConfirmationRequest:
            case PrintMgmtDocumentType::VendPaymAdvice:
                return PrintMgmtPrintDestinationPartyType::Vendor;

            case PrintMgmtDocumentType::ComplementaryInvoice_BR:
            case PrintMgmtDocumentType::TransferOrderFiscalDoc_BR:
                return fiscalDocument_BRParty(_jour);

           case PrintMgmtDocumentType::BankCheque:
                return bankChequeTable_ACParty(_jour);
        }

        return PrintMgmtPrintDestinationPartyType::Unknown;
    }

}





No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...