This commit is contained in:
2022-12-01 17:56:39 +07:00
6 changed files with 268 additions and 119 deletions
@@ -37,7 +37,7 @@ const apiGenerateCahsIn = async (req, res) => {
// log info
winston.logger.info(
`${uniqueCode} REQUEST generate invoice: ${JSON.stringify(req.body)}`
`${uniqueCode} REQUEST generate cashin: ${JSON.stringify(req.body)}`
);
// check validator
@@ -51,7 +51,7 @@ const apiGenerateCahsIn = async (req, res) => {
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE generate invoice: ${JSON.stringify(result)}`
`${uniqueCode} RESPONSE generate cashin: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
@@ -167,6 +167,8 @@ const _generateCashIn = async (uniqueCode) => {
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} cash in ${code} data : ${JSON.stringify(dataCahsIn)}`)
const cashIn = await model.insertCashIn(trx, dataCahsIn)
const paymentyHistory = await model.writePaynentHistory(dataCahsIn, "SUCCESSFULLY", 0, trx)
// Insert cahs in detail
let data = []
const reserv = await model.getReserve(trx, item.c_partner_code, item.d_payment)
+25 -22
View File
@@ -154,28 +154,6 @@ const setReturnSAP = async (code, resSAP, trx) => {
}
const writePaynentHistory = async (data, trx) => {
console.log(data)
const result = await trx('log.t_log_invoice').insert({
i_log_name : 102,
d_log : trx.raw('NOW()'),
c_invoice_no : data.c_invoice_no,
c_partner_name : data.c_partner_name,
j_new_data : JSON.stringify(data),
d_payment_date: trx.raw('NOW()'),
c_note : 'SUCCESSFULLY',
c_status : 'S',
d_created : trx.raw('NOW()'),
m_amount : data.amount,
m_mdr_amount : 0,
m_mdr_net : data.amount
}, ['i_log_name', 'c_note'])
return result
}
const getRecon = async (trx) => {
let sql = `SELECT
@@ -303,9 +281,11 @@ const getPartnerRecon = async(trx) => {
.leftJoin('public.t_m_partner as tmp', function () {
this.on('tmp.c_code', '=', 'tdrb.c_partner_code')
})
.where("tdrb.c_status", "=", "A")
.where("tdi.c_status", "!=", "X")
.where("tdpr.c_status", "=", "S")
// .where("tdi.c_return_sap", "LIKE", "S-%")
.whereNotNull("tdi.c_return_sap")
.whereNotNull("tdrb.c_load_balance_no")
.whereRaw(`tdrb.c_reservation_no in (SELECT c_reservation_no FROM payment.t_d_payment_recon where c_status = 'S' ) AND tdrb.c_reservation_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`)
@@ -353,6 +333,29 @@ const insertCashIn = async (trx, data) => {
return result[0]
}
const writePaynentHistory = async (data, message, i_log_name, trx) => {
console.log(data)
const result = await trx('log.t_log_invoice').insert({
// i_log_name : 102,
i_log_name : 119,
d_log : trx.raw('NOW()'),
c_invoice_no : data.c_cash_in_no,
c_partner_name : data.c_partner_name,
j_new_data : JSON.stringify(data),
d_payment_date: trx.raw('NOW()'),
c_note : message,
c_status : 'S',
d_created : trx.raw('NOW()'),
m_amount : data.amount,
m_mdr_amount : 0,
m_mdr_net : data.amount
}, ['i_log_name', 'c_note'])
return result
}
module.exports = {
generateInvoice,
insertPostingEt,
@@ -163,7 +163,7 @@ const apiPostingInvoice = async (req, res) => {
const _generateInvoice = async (invoiceDate, uniqueCode, repost) => {
let invoice = [];
const collection = []
await knex.transaction(async (trx) => {
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`);
@@ -181,19 +181,6 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => {
if (invoice.length > 0)
for (const key in invoice) {
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} insert payment history et : ${JSON.stringify(
invoice
)}`
);
let history = await model.writePaynentHistory(invoice[key], trx);
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} result insert payment history et : ${JSON.stringify(
history
)}`
);
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify(
@@ -216,8 +203,40 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => {
`SCHEDULER-FB70-${uniqueCode} preparing posting FB70 to SAP`
);
const url =
"http://piqas.kereta-api.co.id:51500/XISOAPAdapter/MessageServlet?senderParty=&senderService=KCI_ETICKETING_2&receiverParty=&receiverService=&interface=SI_KCI_CMS_FI01&interfaceNamespace=urn:Posting_ETicketing_2";
const credUsername = await model.getParameterSetting(knex, "SAP_AUTH_USERNAME")
const credPassword = await model.getParameterSetting(knex, "SAP_AUTH_PASSWORD")
const parameter = await model.getParameterSetting(knex, "SAP_FI_URL_FOR_FB70")
let prepare = {
status : true,
message: "Success",
}
if(!credUsername) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_USERNAME",
}
}
if(!credPassword) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
if(!parameter) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
const credential = {
username: credUsername.c_value,
password: credPassword.c_value
}
for (const key in invoice) {
let xmlHeader = {
@@ -263,98 +282,161 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => {
`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`
);
const response = await helper.postingSAP(url, xmlRequest);
let response = {}
if(prepare.status){
winston.logger.debug(
`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${
invoice[key].c_invoice_no
} to SAP : ${JSON.stringify(response)}`
);
response = await helper.postingSAP(parameter.c_value, xmlRequest, credential);
winston.logger.debug(
`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${
invoice[key].c_invoice_no
} to SAP : ${JSON.stringify(response)}`
);
}
// new transaction
await knex.transaction(async (trx) => {
const resSAP = helper.getXMLData(response);
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 2`);
// UPDATE CMS
let resSAP
if(prepare.status){
resSAP = helper.getXMLData(response);
}else {
resSAP = {
type: 'E',
message : prepare?.message || "FAILED GET PARAMETER SETTING",
data : {}
}
}
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(
resSAP
)}`
);
const cms = await model.setReturnSAP(invoice[key], resSAP, trx);
const cms = await model.setReturnSAP(invoice[key], resSAP, trx, true);
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`
);
collection.push(cms.invoice[0])
});
}
}
return invoice;
return collection;
};
const _postingInvoice = async (uniqueCode) => {
let postingEt = [];
const collection = []
postingEt = await model.getPostingEt(knex);
winston.logger.info(
`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(
postingEt
)}`
);
const credUsername = await model.getParameterSetting(knex, "SAP_AUTH_USERNAME")
const credPassword = await model.getParameterSetting(knex, "SAP_AUTH_PASSWORD")
const parameter = await model.getParameterSetting(knex, "SAP_FI_URL_FOR_FB70")
let prepare = {
status : true,
message: "Success",
}
if(!credUsername) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_USERNAME",
}
}
if(!credPassword) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
if(!parameter) {
prepare = {
status : false,
message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
const credential = {
username: credUsername.c_value,
password: credPassword.c_value
}
for (const item of postingEt) {
await knex.transaction(async (trx) => {
let xmlHeader = {
doc_header_text: item.doc_header_text ? item.doc_header_text : "",
ba: item.ba ? item.ba : "",
doc_date: item.doc_date ? item.doc_date : "",
posting_date: item.posting_date ? item.posting_date : "",
fisc_year: item.fiscal_year ? item.fiscal_year : "",
fisc_period: item.fis_period ? item.fis_period : "",
doc_type: item.doc_type ? item.doc_type : "",
c_invoice_no: item.reference ? item.reference : "",
};
let xmlItem = ["S", "H"].map((key) => {
return {
c_account_code_topup: key == "S" ? item.customer : "",
c_account_topup: key == "H" ? item.account : "",
ba: item.ba,
amount: item.amount,
posting_date: item.posting_date,
c_invoice_no: item.reference,
item_text: key == "S" ? item.text1 : item.text2,
de_cre: key,
let response = {}
let resSAP = {}
if(prepare.status){
let xmlHeader = {
doc_header_text: item.doc_header_text ? item.doc_header_text : "",
ba: item.ba ? item.ba : "",
doc_date: item.doc_date ? item.doc_date : "",
posting_date: item.posting_date ? item.posting_date : "",
fisc_year: item.fiscal_year ? item.fiscal_year : "",
fisc_period: item.fis_period ? item.fis_period : "",
doc_type: item.doc_type ? item.doc_type : "",
c_invoice_no: item.reference ? item.reference : "",
};
});
let xmlItem = ["S", "H"].map((key) => {
return {
c_account_code_topup: key == "S" ? item.customer : "",
c_account_topup: key == "H" ? item.account : "",
ba: item.ba,
amount: item.amount,
posting_date: item.posting_date,
c_invoice_no: item.reference,
item_text: key == "S" ? item.text1 : item.text2,
de_cre: key,
};
});
let xmlRequest = helper.generateXMLRequest(
"ZFMCMS_FI_001",
xmlHeader,
xmlItem
);
let xmlRequest = helper.generateXMLRequest(
"ZFMCMS_FI_001",
xmlHeader,
xmlItem
);
winston.logger.info(
`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}`
);
winston.logger.info(
`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}`
);
response = await helper.postingSAP(parameter.c_value, xmlRequest, credential);
winston.logger.debug(
`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${
item.reference
} to SAP : ${JSON.stringify(response)}`
);
const url =
"http://piqas.kereta-api.co.id:51500/XISOAPAdapter/MessageServlet?senderParty=&senderService=KCI_ETICKETING_2&receiverParty=&receiverService=&interface=SI_KCI_CMS_FI01&interfaceNamespace=urn:Posting_ETicketing_2";
resSAP = helper.getXMLData(response);
const response = await helper.postingSAP(url, xmlRequest);
}else{
resSAP = {
type: 'E',
message : prepare?.message || "FAILED GET PARAMETER SETTING",
data : {}
}
winston.logger.info(
`SCHEDULER-RETRY-${uniqueCode} RESPONSE posting ${
item.reference
} to SAP : ${JSON.stringify(response)}`
);
// new transaction
// await knex.transaction(async trx => {
const resSAP = helper.getXMLData(response);
}
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} TRANSACTION 2`);
// UPDATE CMS
@@ -364,21 +446,25 @@ const _postingInvoice = async (uniqueCode) => {
)}`
);
const invoice = await model.getInvoice(item.reference, trx)
const cms = await model.setReturnSAP(
{ c_invoice_no: item.reference },
invoice,
resSAP,
trx
trx,
false
);
winston.logger.info(
`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`
);
collection.push(cms.invoice[0])
})
}
}
// });
return postingEt;
return collection;
};
module.exports = {
@@ -85,15 +85,15 @@ const getPostingEt = async(trx) => {
}
const setReturnSAP = async (invoice, resSAP, trx) => {
const setReturnSAP = async (invoice, resSAP, trx, posting = false) => {
let t_invoice = {}
let t_load_balance = {}
let posting_et = {}
if(resSAP.type == 'S'){
// update invoice
t_invoice = await trx('payment.t_d_invoice').update({
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
}, ['c_invoice_no','c_return_sap'])
c_return_sap: "S-"+resSAP.data.E_DOCUMENTNUMBER
}, ['c_invoice_no','c_return_sap','m_amount'])
.where({
c_invoice_no: invoice.c_invoice_no
});
@@ -113,6 +113,18 @@ const setReturnSAP = async (invoice, resSAP, trx) => {
reference: invoice.c_invoice_no
})
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} insert payment history et : ${JSON.stringify(
invoice
)}`
);
if(posting) {
await writePaynentHistory(invoice, "SUCCESSFULLY", 102, trx)
} else {
await writePaynentHistory(invoice, "SUCCESS TO RETRY SAP", 118, trx)
}
}else {
let text = resSAP.message;
@@ -137,7 +149,7 @@ const setReturnSAP = async (invoice, resSAP, trx) => {
// update invoice
t_invoice = await trx('payment.t_d_invoice').update({
c_return_sap: doc_number
}, ['c_invoice_no','c_return_sap'])
}, ['c_invoice_no','c_return_sap','m_amount'])
.where({
c_invoice_no: invoice.c_invoice_no
});
@@ -147,7 +159,13 @@ const setReturnSAP = async (invoice, resSAP, trx) => {
c_return_sap: doc_number
}, ['c_load_balance_no', 'c_return_sap'])
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${invoice.c_invoice_no}')`)
if(posting) {
await writePaynentHistory(invoice, "SUCCESSFULLY", 102, trx)
} else {
await writePaynentHistory(invoice, "SUCCESS TO RETRY SAP", 118, trx)
}
}else{
// update posting et
posting_et = await trx('public.posting_et').update({
@@ -158,6 +176,19 @@ const setReturnSAP = async (invoice, resSAP, trx) => {
}, ['file_name','status0']).where({
reference: invoice.c_invoice_no
})
// update invoice
t_invoice = await trx('payment.t_d_invoice').select(['c_invoice_no','c_return_sap','m_amount'])
.where({
c_invoice_no: invoice.c_invoice_no
});
if(posting) {
await writePaynentHistory(invoice, `SUCCESS WITH SAP ERR: ${resSAP.message}`,102, trx)
} else {
await writePaynentHistory(invoice, `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`,118, trx)
}
}
}
@@ -172,16 +203,17 @@ const setReturnSAP = async (invoice, resSAP, trx) => {
}
const writePaynentHistory = async (data, trx) => {
const writePaynentHistory = async (data, message, i_log_name, trx) => {
console.log(data)
const result = await trx('log.t_log_invoice').insert({
i_log_name : 102,
// i_log_name : 102,
i_log_name : i_log_name,
d_log : trx.raw('NOW()'),
c_invoice_no : data.c_invoice_no,
c_partner_name : data.c_partner_name,
j_new_data : JSON.stringify(data),
d_payment_date: trx.raw('NOW()'),
c_note : 'SUCCESSFULLY',
c_note : message,
c_status : 'S',
d_created : trx.raw('NOW()'),
m_amount : data.amount,
@@ -194,10 +226,37 @@ const writePaynentHistory = async (data, trx) => {
}
const getParameterSetting = async(trx, code) => {
const result = await trx('public.t_m_global_parameter_sap AS tmgps').select([
"c_value"
])
.where('c_code', '=', code)
.where('c_status', '=', "A")
.first()
return result
}
const getInvoice = async(code, trx) => {
const result = await trx('payment.t_d_invoice').select([
"*"
])
.where('c_invoice_no', '=', code)
.first()
return result
}
module.exports = {
generateInvoice,
insertPostingEt,
setReturnSAP,
getPostingEt,
writePaynentHistory
writePaynentHistory,
getParameterSetting,
getInvoice
};