Thursday, October 19, 2017

X++ class that fetches the data from tables in a different Database and create Payment Journals in AX

Class Declaration:

class LanCreatePaymentJournal_B2B extends RunBaseBatch
{

LedgerjournalTable      LedgerjournalTable;
}



Method lanB2BPaymentJournal:

void lanB2BPaymentJournal()
{

    LedgerjournalTrans      LedgerjournalTrans;
    LedgerjournalCheckPost  LedgerjournalCheckPost;
    CustInvoiceJour custInvoiceJour;
    LedgerJournalEngine_CustPayment     ledgerJournalEngine;
    ResultSet               ResultSet;
    str                     CurCode[], PayStatus[],  InvoiceID[],CardConnectRef[], strdate;
    real Amt[], invoiceamt;
    int                     i = 0;
    int                     cnt = 1;
    Ledgerjournalname       ledgerjournalname;
    CustTransOpen                   custTransOpen;
    CustTrans                       custTrans;
    SpecTransManager                specTransManager;

    NumberSeq               numberSeq;
    CustPaymModeTable _custPaymModeTable;
    CustInvoiceId _invoiceId;
    boolean journalexist = false;


;

    try
    {
    //call the method to get the data from Payment tables
    ResultSet = this.lanReadPaymentTables();

    //Loop through resultset from the above method
    while(ResultSet.next())
    {
        i++;
       // assign resultset values to the arrays
         Amt[i]    = ResultSet.getReal(3);
         PayStatus[i]    = strlrtrim(ResultSet.getString(4));
         CurCode[i]    = strlrtrim(ResultSet.getString(6));
         CardConnectRef[i]    = strlrtrim(ResultSet.getString(7));
         InvoiceID[i]    = strlrtrim(ResultSet.getString(12));

    }
    ResultSet.close();
    ResultSet = null;

    if (i == 0)
        return;

    cnt = i;

      //get the payment method record to get offset Ledger dimension
          select * from _custPaymModeTable
               where _custPaymModeTable.PaymMode == 'MCV';
        // loop through each record from the resultset and create Payment Journals
    for (i = 1; i <= cnt; i++)
    {

           _invoiceId = InvoiceID[i];
            invoiceamt = Amt[i];

            // get the customer invoice journal record
            select * from custInvoiceJour
                    where custInvoiceJour.InvoiceId == _invoiceId;
                   // && custInvoiceJour.InvoiceAmount == invoiceamt;

                select firstonly custTransOpen
                    join custTrans
                    where  custTransOpen.AccountNum  == custTrans.AccountNum
                        && custTransOpen.RefRecId    == custTrans.RecId
                        && custTrans.Invoice         == custInvoiceJour.InvoiceId
                        && custTrans.AccountNum      == custInvoiceJour.InvoiceAccount
                        && custTrans.TransDate       == custInvoiceJour.InvoiceDate;

        if(custTransOpen)
            {

                // create journal
                if(journalexist == false) // Only one Journal Header per mulitple invoices
                {
                     info("Payment Journal Creation to be started");
                ttsBegin;
                    LedgerjournalTable.clear();
                    ledgerjournalname = LedgerJournalName::find('ARCCP');
                    ledgerJournalTable.initFromLedgerJournalName(ledgerjournalname.JournalName);
                    ledgerJournalTable.JournalNum = NumberSeq::newGetNum(LedgerParameters::numRefJournalNum()).num();
                    LedgerjournalTable.Name = strFmt("%1 Credit Card Payment" , systemDateGet());
                    ledgerJournalTable.OffsetAccountType = _custPaymModeTable.AccountType;
                    ledgerJournalTable.parmOffsetLedgerDimension(_custPaymModeTable.offsetLedgerDimension());
                         if (ledgerJournalTable.validateWrite())
                            {
                                ledgerJournalTable.insert();
                                journalexist = true;
                            }
                            else
                            {
                                throw error("Journal header");
                            }
                ttsCommit;
                }

                ttsBegin;
                // create journal Lines
                LedgerjournalTrans.clear();
                ledgerJournalEngine = new LedgerJournalEngine_CustPayment();
                LedgerjournalTrans.initValue();
                ledgerJournalEngine.newJournalActive(ledgerJournalTable);
                ledgerJournalEngine.initValue(LedgerjournalTrans);
                ledgerJournalEngine.initFromCustTable(LedgerjournalTrans);
                numberSeq                               =   NumberSeq::newGetVoucherFromId((ledgerjournalname.NumberSequenceTable));
                LedgerjournalTrans.Voucher              =   numberSeq.voucher();
                LedgerjournalTrans.JournalNum           =   LedgerjournalTable.JournalNum;
                LedgerjournalTrans.accountName();
                LedgerjournalTrans.CurrencyCode         =   CurCode[i];

                LedgerjournalTrans.ExchRate             =   Currency::exchRate(LedgerjournalTrans.CurrencyCode);
                LedgerjournalTrans.AccountType          =   LedgerJournalACType::Cust;
                LedgerjournalTrans.parmAccount(custInvoiceJour.InvoiceAccount,LedgerjournalTrans.AccountType);
                LedgerjournalTrans.Txt                  =   CardConnectRef[i];
                LedgerjournalTrans.MarkedInvoice   = _invoiceId;

                LedgerjournalTrans.AmountCurCredit      =   custTransOpen.AmountCur;
                LedgerjournalTrans.TransDate            =   systemDateGet();
                LedgerjournalTrans.PaymReference = CardConnectRef[i];
                LedgerJournalTrans.OffsetTxt = 'Credit Card Payment';
                LedgerJournalTrans.BankTransType = _custPaymModeTable.BankTransType;
                LedgerJournalTrans.DocumentDate = systemDateGet();
                LedgerjournalTrans.SettleVoucher        = SettlementType::SelectedTransact;

                LedgerjournalTrans.OffsetAccountType    =   LedgerJournalACType::Bank;
                LedgerjournalTrans.OffsetLedgerDimension=    _custPaymModeTable.offsetLedgerDimension();
                LedgerjournalTrans.DefaultDimension     =   CustTable::find(custInvoiceJour.InvoiceAccount).DefaultDimension;
                LedgerjournalTrans.OffsetDefaultDimension =  CustTable::find(custInvoiceJour.InvoiceAccount).DefaultDimension;
                LedgerjournalTrans.LedgerDimension      =   DimensionStorage::getDynamicAccount(custInvoiceJour.InvoiceAccount, LedgerJournalACType::Cust);

                ledgerJournalEngine.currencyModified(LedgerjournalTrans);

                 if (!LedgerjournalTrans.parmOffsetLedgerDimension())
                    {
                        throw info("@SYS122148");
                    }

                 ledgerJournalEngine.initOffsetDefaultDimension(LedgerjournalTrans);
                 LedgerjournalTrans.PaymMode = 'MCV';
                 LedgerjournalTrans.insert();
                 this. lanUpdatePaymentTables(_invoiceId,CardConnectRef[i]);
                ttsCommit;

                // mark the transaction for settlement
                 specTransManager = SpecTransManager::construct(LedgerjournalTrans);

                specTransManager.insert(
                    custTransOpen.company(),
                    custTransOpen.TableId,
                    custTransOpen.RecId,
                    custTransOpen.AmountCur,
                    custInvoiceJour.CurrencyCode);

                ttsBegin;
                LedgerjournalTrans.reread();
                LedgerjournalTrans.MarkedInvoice        = custTrans.Invoice;
                LedgerjournalTrans.MarkedInvoiceCompany = custTrans.company();
                LedgerjournalTrans.MarkedInvoiceRecId   = custTransOpen.RecId;
                LedgerjournalTrans.SettleVoucher        = SettlementType::SelectedTransact;
                LedgerjournalTrans.update();
                ttsCommit;


       }
     }
    }
      catch (Exception::Error)
        {
            ttsabort;
            ttsBegin;
              ledgerJournalTable.delete(); // delete the jorunal header on error
            ttsCommit;
            throw Exception::Error;
        }

}

Method lanReadPaymentTables:

//this method reads the data from Payments and PaymentInvoices tables in different database
//stores the output to Resultset

public ResultSet lanReadPaymentTables()
{
    str         sql;
    str         strSQTYPE;
    ResultSet   ResultSet;
    int         conlength;
    int         counter = 0;
    str         dbServer;
    str         dbName;

//getting server details form parameters table

    dbServer = lanParameters::Find().LAN_ManualPackDbServer;
    if (dbServer == '')
        dbServer = '';

    dbName = lanParameters::Find().Lan_B2BAPIDB;
    if (dbName == '')
        dbName = '';

    sql =  "";
    sql += strFmt('SELECT * FROM [%1].[%2].[dbo].[Payments] P ', dbServer, dbName);
    sql +=    strfmt('inner join [%1].[%2].[dbo].[PaymentsInvoices] PI ', dbServer, dbName);
    sql +=    'on PI.PaymentId = P.ID ';
    sql += strfmt('WHERE P.AXStatus = %1%2%3 ', "'", counter, "'");
    sql +=    strfmt('AND PI.AXInvoiceStatus = %1%2%3 ', "'", counter, "'");

    ResultSet = QueryHelperRunOnServer::executeSQLQuery(sql);

    return ResultSet;

}

Method lanUpdatePaymentTables:

//this method update the data on Payments and PaymentInvoices tables in different database

public void lanUpdatePaymentTables(CustInvoiceId _invoiceId1, str CardConnectRef)
{
    str         sql,sql1;
    str         strSQTYPE;
    int   ResultSet1;
    int         conlength;
    int         counter;
    str         dbServer;
    str         dbName;
    int AXstatus = 1;

    dbServer = lanParameters::Find().LAN_ManualPackDbServer;
    if (dbServer == '')
        dbServer = '';

    dbName = lanParameters::Find().Lan_B2BAPIDB;
    if (dbName == '')
        dbName = '';

   // update AXStatus value on the payments table
    sql += strFmt('Update [%1].[%2].[dbo].[Payments] set AXStatus = %3 ', dbServer, dbName, AXstatus);
    sql += strfmt('WHERE CardConnectRetRef  = %1%2%3 ', "'", CardConnectRef, "'");

      // update AXInvoiceStatus value on the paymentsInvoices table
       sql1 += strfmt("UPDATE [%1].[%2].[dbo].[PaymentsInvoices]  set AXInvoiceStatus = %3", dbServer, dbName, AXstatus);
     sql1 += strfmt("where InvoiceID  = %1%2%3 ", "'", _invoiceId1, "'");


    ResultSet1 = QueryHelperRunOnServer::executeSQLUpdate(sql);
    ResultSet1 = QueryHelperRunOnServer::executeSQLUpdate(sql1);



}

Method Run:

void run()
{

    info(StrFmt("%1 %2", "Start-time: ", time2str(timenow(),1,2)));

  this.lanB2BPaymentJournal();

    info(StrFmt("End-time: %1",  time2str(timenow(),1,2)));

}

Method Void:

static void main(Args args)
{
    LanCreatePaymentJournal_B2B   LanCreatePaymentJournal_B2B;
    ;

    LanCreatePaymentJournal_B2B = new LanCreatePaymentJournal_B2B();

    if (LanCreatePaymentJournal_B2B.prompt())
    {
        LanCreatePaymentJournal_B2B.run();
    }
}


No comments:

Post a Comment