Sunday, July 1, 2018

Package and Engine





Step 1
%Select(business_unit) SELECT business_unit FROM PS_NTT_GY_INT_RUN WHERE OPRID = %OperatorId AND RUN_CNTL_ID = %RunControl

Step 2

/****************************** Billing and Payment Interface Program **************************/

import NTT_GY_TEST_RESP:TRIGGER_IB_MSG;
import NTT_GY_TEST_RESP:Rec_Insert;
import NTT_GY_TEST_RESP:Rec_Update;
import NTT_GY_TEST_RESP:*;


Component Record &rec;
Component File &LOGFILE;
Local Record &recline;
Local SQL &sql, &sqlline;
Component number &r_counter, &count_retry;
Component number &count_sent;

&rec = CreateRecord(Record.BI_HDR);
&recline = CreateRecord(Record.BI_LINE);

&PRCSINSTANCE = NTT_GY_INTF_AET.PROCESS_INSTANCE.Value;
&sql = CreateSQL("%Selectall(:1) where bill_status = 'INV' and FINAL_CURCNV_FLG ='Y' and   (business_unit,invoice) not in(select business_unit,invoice from ps_ntt_bi_intfc where edi_sent_flg in('X','H', 'E','S')) order by business_unit,invoice", &rec);
Local Record &recTgt;
&recTgt = CreateRecord(Record.NTT_BI_INTFC);

&LOGFILE = GetFile("INTFC_LOG.log", "w", "a", %FilePath_Relative);

Local number &count_error;
Local number &retry_Count;
Local Message &MSG;
SQLExec("select 'X' from PS_NTT_BI_INTFC where edi_sent_flg = 'X'", &Xflag);

/* Select invoices which is not having any rows in Custom Billing Interface table for EDI flags = (X) Exit flag*/
If (&Xflag <> "X") Then
  
   SQLExec("select count(*) from ps_ntt_bi_intfc where  business_unit = :1 and  edi_sent_flg = 'E' and  to_char(datetime_sent, 'YYYY/MM/DD')  =to_char(sysdate, 'YYYY/MM/DD')", &count_e_flag, NTT_GY_INTF_AET.BUSINESS_UNIT.Value);
   /* Total errors should not be more than 6 for the current date*/
   If &count_e_flag <= 6 Then
      &LOGFILE.WriteLine("Billing Interface Summary");
      While &sql.Fetch(&rec)
        
         SQLExec("select nvl(retrycount,0) ,transaction_dtl from PS_NTT_BI_INTFC where business_unit = :1 and invoice = :2   ", &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value, &retry_Count, &trn_dtl);
         &r_counter = &retry_Count;
         /* Call Insert App Package for the transaction to be sent first time*/
         If (&retry_Count = 0) Then
            Local NTT_GY_TEST_RESP:Rec_Insert &INS = create NTT_GY_TEST_RESP:Rec_Insert();
           
            &MSG = &INS.insert_trigger(&rec, &PRCSINSTANCE, &retry_Count, &trn_dtl);
         End-If;
         /* Call Update App Package for the transaction to be sent second time*/
         If (&r_counter = 1) Then
            Local NTT_GY_TEST_RESP:Rec_Update &UPD = create NTT_GY_TEST_RESP:Rec_Update();
            &MSG = &UPD.update_trigger(&rec, &PRCSINSTANCE, &r_counter);
         End-If;
        
         Local NTT_GY_TEST_RESP:TRIGGER_IB_MSG &My_Nbr = create NTT_GY_TEST_RESP:TRIGGER_IB_MSG();
         Local Message &response;
         &response = &My_Nbr.call_service_operation(&MSG);
         &Error_string = &response.GetContentString();
        
         &xmlDoc_res = &response.GetXmlDoc();
         &rootNode_res = &xmlDoc_res.DocumentElement;
         SQLExec("update ps_NTT_BI_INTFC set transaction_title = :1 where business_unit = :2 and invoice = :3 ", &response.TransactionId, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
         If &response.ResponseStatus = %IB_Status_Success Then
            
            &xmlDoc_res = &response.GetXmlDoc();
            &rootNode_res = &xmlDoc_res.DocumentElement;
           
            If &Error_string <> Null Then
               &find_start_tag = Find("<", &Error_string);
               &find_end_tag = Find(">", &Error_string);
               If &find_start_tag > 0 And
                     &find_end_tag > 1 Then
                  &err_xml_doc = CreateXmlDoc("");
                  &ret = &err_xml_doc.ParseXmlString(&Error_string);
                  If &ret Then
                     &aNodeList = &err_xml_doc.GetElementsByTagName("SEND_NEXT_FLAG");
                     If (&aNodeList [1].NodeValue = "Y") Then
                       
                        SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'S',prcsinstance = :1 ,errormsg = ' ' ,error_descr = ' ' , error_cd = '0' where business_unit = :2 and invoice = :3 ", &PRCSINSTANCE, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                        &LOGFILE.WriteLine("Import Success for Business unit : " | &rec.BUSINESS_UNIT.Value | " ,Invoice " | &rec.INVOICE.Value);
                        &count_sent = &count_sent + 1;
                     End-If;
                  End-If;
               Else
                  &Error_string = Substring(&Error_string, 1, 250);
                  SQLExec("update PS_NTT_BI_INTFC set edi_sent_flg = 'R',errormsg = :1 ,error_descr = :2 , error_cd = : 3,prcsinstance =:4,retrycount = :5 where business_unit = :6 and Invoice = :7  ", &Error_string, "Invalid Response", "0", &PRCSINSTANCE, &r_counter + 1, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
               End-If;
              
            Else
               If (&rootNode_res = Null) Then
                 
                  SQLExec("update ps_NTT_BI_INTFC set errormsg = :1 ,error_descr = :2 , error_cd = : 3 where business_unit = :4 and invoice = :5 ", "No Response/Connetivity Issue", "No Response", "0", &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                  If (&r_counter = 0) Then
                     SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'R',prcsinstance =:1,retrycount = :2 where business_unit = :3 and invoice = :4  ", &PRCSINSTANCE, &r_counter + 1, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                     &count_retry = &count_retry + 1;
                     &LOGFILE.WriteLine("Import Failed for Business unit : " | &rec.BUSINESS_UNIT.Value | " ,Invoice " | &rec.INVOICE.Value);
                  End-If;
                  If (&r_counter = 1) Then
                     SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'E',retrycount = :1 ,prcsinstance = :2 where business_unit = :3 and invoice = :4  ", &r_counter + 1, &PRCSINSTANCE, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                     Local NTT_GY_TEST_RESP:Mail_Notification &Email = create NTT_GY_TEST_RESP:Mail_Notification();
                     &transactionid = &rec.BUSINESS_UNIT.Value | &rec.BILL_TO_CUST_ID.Value | &rec.INVOICE.Value;
                     &done = &Email.send_email(&rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value, &transactionid, "E");
                     If &done = True Then
                        &LOGFILE.WriteLine("Email Notified  for Invocie : " | &rec.INVOICE.Value | " In Error Status");
                     End-If;
                     If &done = False Then
                        &LOGFILE.WriteLine("Email Falied to send  for Invocie : " | &rec.INVOICE.Value | " In Error Status, Please check SMTP settings");
                     End-If;
                     &count_error = &count_error + 1;
                     Continue;
                  End-If;
               End-If;
            End-If;
         Else
           
            If &Error_string <> Null Then
              
               &find_start_tag = Find("<", &Error_string);
               &find_end_tag = Find(">", &Error_string);
              
               If &find_start_tag > 0 And
                     &find_end_tag > 1 Then
                 
                  &err_xml_doc = CreateXmlDoc("");
                  &ret = &err_xml_doc.ParseXmlString(&Error_string);
                  If &ret Then
                     &Error_Message = &err_xml_doc.GetElementsByTagName("MESSAGE");
                     &Error_descr = &err_xml_doc.GetElementsByTagName("ERROR");
                     &Error_code = &err_xml_doc.GetElementsByTagName("CODE");
                     &Error_Message_nodevalue = Substring(&Error_Message [1].nodevalue, 1, 250);
                     &Error_descr_nodevalue = Substring(&Error_descr [1].nodevalue, 1, 19);
                     
                     SQLExec("update ps_NTT_BI_INTFC set errormsg = :1 ,error_descr = :2 , error_cd = : 3 where business_unit = :4 and invoice = :5 ", &Error_Message_nodevalue, &Error_descr_nodevalue, &Error_code [1].nodevalue, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                  End-If;
               Else
                  &response_GenXMLString = Substring(&response.GenXMLString(), 1, 250);
                  SQLExec("update ps_NTT_BI_INTFC set errormsg = :1 ,error_descr = :2 , error_cd = : 3 where business_unit = :4 and invoice = :5 ", &response_GenXMLString, "Network Issue", "0", &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
               End-If;
               If (&r_counter = 0) Then
                  SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'R',prcsinstance =:1,retrycount = :2 where business_unit = :3 and invoice = :4  ", &PRCSINSTANCE, &r_counter + 1, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                  &count_retry = &count_retry + 1;
                  &LOGFILE.WriteLine("Import Failed for Business unit : " | &rec.BUSINESS_UNIT.Value | " ,Invoice " | &rec.INVOICE.Value);
               End-If;
               If (&r_counter = 1) Then
                  SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'E',retrycount = :1 ,prcsinstance = :2 where business_unit = :3 and invoice = :4  ", &r_counter + 1, &PRCSINSTANCE, &rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value);
                  Local NTT_GY_TEST_RESP:Mail_Notification &Email11 = create NTT_GY_TEST_RESP:Mail_Notification();
                  &transactionid = &rec.BUSINESS_UNIT.Value | &rec.BILL_TO_CUST_ID.Value | &rec.INVOICE.Value;
                  &done = &Email11.send_email(&rec.BUSINESS_UNIT.Value, &rec.INVOICE.Value, &transactionid, "E");
                  If &done = True Then
                     &LOGFILE.WriteLine("Email Notified  for Invocie : " | &rec.INVOICE.Value | " In Error Status");
                  End-If;
                  If &done = False Then
                     &LOGFILE.WriteLine("Email Falied to send  for Invocie : " | &rec.INVOICE.Value | " In Error Status, Please check SMTP settings");
                  End-If;
                  &count_error = &count_error + 1;
                  Continue;
               End-If;
            End-If;
         End-If;
      End-While;
   End-If;
Else
   &LOGFILE.WriteLine("No Invoice have been Picked as some invoice are marked in X flag, Please update that to R for Retry or H for Hold through Online page to process the new data.");
End-If;



&LOGFILE.WriteLine("Total :" | &count_sent | " invoice sent successfully.");
&LOGFILE.WriteLine("Total :" | &count_error | " invoice marked for error.");
&LOGFILE.WriteLine("Total :" | &count_retry | " marked for Retry.");
MessageBox(0, "", 0, 0, "Billing Interface Summary");
MessageBox(0, "", 0, 0, "Total Invoices :" | &count_sent | " invoice sent successfully.");
MessageBox(0, "", 0, 0, "Total Invoices :" | &count_error | " marked for error.");
MessageBox(0, "", 0, 0, "Total Invoices :" | &count_retry | " marked for Retry.");






Step 2 step 2:

/****************************** Billing and Payment Interface Program **************************/

import NTT_GY_TEST_RESP:*;

Component File &LOGFILE;

SQLExec("select count(*) from ps_ntt_bi_intfc where  business_unit = '751' and  edi_sent_flg = 'E' and  to_char(datetime_sent, 'YYYY/MM/DD')  =to_char(sysdate, 'YYYY/MM/DD')", &count_e_flag);

If &count_e_flag > 6 Then
   SQLExec("update ps_NTT_BI_INTFC set edi_sent_flg = 'X' where business_unit = '751' and edi_sent_flg='E' and  to_char(datetime_sent, 'YYYY/MM/DD')  =to_char(sysdate, 'YYYY/MM/DD')");
   Local Record &rec_inv;
   &rec_inv = CreateRecord(Record.NTT_BI_INTFC);
   &sql = CreateSQL("%selectall(NTT_BI_INTFC)  where business_unit = '751' and edi_sent_flg='X' and  to_char(datetime_sent, 'YYYY/MM/DD')  =to_char(sysdate, 'YYYY/MM/DD')");
   While &sql.fetch(&rec_inv)
      &Invoice_x = &rec_inv.INVOICE.Value | "," | &Invoice_x;
   End-While;
  
   Local NTT_GY_TEST_RESP:Mail_Notification &Email = create NTT_GY_TEST_RESP:Mail_Notification();
   &done = &Email.send_email(&rec_inv.BUSINESS_UNIT.Value, &Invoice_x, " ", "X");
   If &done = True Then
      &LOGFILE.WriteLine("Email Notified  for Invocie's : " | &Invoice_x | " In Invalid(X) Status");
      MessageBox(0, "", 0, 0, "Email Notified  for Invocie's : " | &Invoice_x | " In Invalid(X) Status");
   End-If;
   If &done = False Then
      &LOGFILE.WriteLine("Email Failed to send  for Invocie's : " | &Invoice_x | " In Invalid(X) Status, Please check SMTP settings");
      MessageBox(0, "", 0, 0, "Email Failed to send for Invocie's : " | &Invoice_x | " In Invalid(X) Status, Please check SMTP settings");
   End-If;
End-If;
SQLExec("commit");