mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
Update invoice
This commit is contained in:
@@ -27,7 +27,7 @@ const validate = (method) => {
|
||||
};
|
||||
|
||||
// POSTING INVOICE
|
||||
const generateInvoice = async (req, res) => {
|
||||
const apiGenerateInvoice = async (req, res) => {
|
||||
|
||||
try {
|
||||
// generate unique code
|
||||
@@ -59,94 +59,12 @@ const generateInvoice = async (req, res) => {
|
||||
// time
|
||||
let invoiceDate = moment(req.body.trx_date).format("YYYY-MM-DD")
|
||||
|
||||
let invoice = []
|
||||
|
||||
await knex.transaction(async trx => {
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`);
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} execute payment.sp_payment_req_topup_online(${invoiceDate})`);
|
||||
|
||||
invoice = await model.generateInvoice(invoiceDate, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
|
||||
|
||||
if(invoice.length > 0) for (const key in invoice) {
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
|
||||
|
||||
let postingEt = await model.insertPostingEt(invoice[key], trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} success insert posting et : ${JSON.stringify(postingEt)}`)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
if(invoice.length > 0){
|
||||
|
||||
winston.logger.info(`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'
|
||||
|
||||
for (const key in invoice) {
|
||||
|
||||
let xmlHeader = {
|
||||
doc_header_text : invoice[key].doc_header_text ? invoice[key].doc_header_text : "",
|
||||
ba: invoice[key].ba ? invoice[key].ba : "",
|
||||
doc_date : invoice[key].doc_date ? invoice[key].doc_date : "",
|
||||
posting_date : invoice[key].posting_date ? invoice[key].posting_date : "",
|
||||
fisc_year : invoice[key].fisc_year ? invoice[key].fisc_year : "",
|
||||
fisc_period : invoice[key].fisc_period ? invoice[key].fisc_period: "",
|
||||
doc_type: invoice[key].doc_type ? invoice[key].doc_type : "",
|
||||
c_invoice_no : invoice[key].c_invoice_no ? invoice[key].c_invoice_no : ""
|
||||
}
|
||||
|
||||
let xmlItem = ['S', 'H'].map(item => {
|
||||
return {
|
||||
c_account_code_topup : item == 'S' ? invoice[key].c_account_code_topup : "",
|
||||
c_account_topup : item == 'H' ? invoice[key].c_account_topup : "",
|
||||
ba : invoice[key].ba,
|
||||
amount : invoice[key].amount,
|
||||
posting_date : invoice[key].posting_date,
|
||||
c_invoice_no : invoice[key].c_invoice_no,
|
||||
item_text : `${item == 'S' ? "DEBIT" : "KREDIT"} INVOICE CMS ${invoice[key].doc_header_text}`,
|
||||
de_cre : item
|
||||
}
|
||||
});
|
||||
|
||||
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`);
|
||||
|
||||
const response = await helper.postingSAP(url, xmlRequest)
|
||||
|
||||
winston.logger.info(`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
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
|
||||
|
||||
const cms = await model.setReturnSAP(invoice[key], resSAP, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// })
|
||||
let invoice = await _generateInvoice(invoiceDate, uniqueCode)
|
||||
|
||||
result = {
|
||||
code: "200",
|
||||
message: "Succes posting",
|
||||
data: invoice,
|
||||
data: invoice
|
||||
};
|
||||
|
||||
return res.status(200).json(result);
|
||||
@@ -170,7 +88,7 @@ const generateInvoice = async (req, res) => {
|
||||
};
|
||||
|
||||
// RETRY POSTING INVOICE
|
||||
const postingInvoice = async (req, res) => {
|
||||
const apiPostingInvoice = async (req, res) => {
|
||||
try {
|
||||
// generate unique code
|
||||
uniqueCode = helper.getUniqueCode()
|
||||
@@ -200,70 +118,16 @@ const postingInvoice = async (req, res) => {
|
||||
// return uniqueCode
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RE-TRY POSTING INVOICE`)
|
||||
|
||||
await knex.transaction(async trx => {
|
||||
const invoice = await _postingInvoice(uniqueCode)
|
||||
|
||||
const postingEt = await model.getPostingEt(trx)
|
||||
result = {
|
||||
code: "200",
|
||||
message: "Succes",
|
||||
data: invoice,
|
||||
}
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(postingEt)}`)
|
||||
return res.status(200).json(result);
|
||||
|
||||
for (const item of postingEt) {
|
||||
|
||||
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)
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}`);
|
||||
|
||||
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 response = await helper.postingSAP(url, xmlRequest)
|
||||
|
||||
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
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
|
||||
|
||||
const cms = await model.setReturnSAP({c_invoice_no : item.reference}, resSAP, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
|
||||
// })
|
||||
}
|
||||
|
||||
result = {
|
||||
code: "200",
|
||||
message: "Succes posting",
|
||||
data: postingEt,
|
||||
}
|
||||
return res.status(200).json(result);
|
||||
})
|
||||
} catch (error) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
@@ -281,8 +145,161 @@ const postingInvoice = async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
const _generateInvoice = async (invoiceDate, uniqueCode) => {
|
||||
|
||||
let invoice = []
|
||||
|
||||
await knex.transaction(async trx => {
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`);
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} execute payment.sp_payment_req_topup_online(${invoiceDate})`);
|
||||
|
||||
invoice = await model.generateInvoice(invoiceDate, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
|
||||
|
||||
if(invoice.length > 0) for (const key in invoice) {
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
|
||||
|
||||
let postingEt = await model.insertPostingEt(invoice[key], trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} success insert posting et : ${JSON.stringify(postingEt)}`)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
if(invoice.length > 0){
|
||||
|
||||
winston.logger.info(`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'
|
||||
|
||||
for (const key in invoice) {
|
||||
|
||||
let xmlHeader = {
|
||||
doc_header_text : invoice[key].doc_header_text ? invoice[key].doc_header_text : "",
|
||||
ba: invoice[key].ba ? invoice[key].ba : "",
|
||||
doc_date : invoice[key].doc_date ? invoice[key].doc_date : "",
|
||||
posting_date : invoice[key].posting_date ? invoice[key].posting_date : "",
|
||||
fisc_year : invoice[key].fisc_year ? invoice[key].fisc_year : "",
|
||||
fisc_period : invoice[key].fisc_period ? invoice[key].fisc_period: "",
|
||||
doc_type: invoice[key].doc_type ? invoice[key].doc_type : "",
|
||||
c_invoice_no : invoice[key].c_invoice_no ? invoice[key].c_invoice_no : ""
|
||||
}
|
||||
|
||||
let xmlItem = ['S', 'H'].map(item => {
|
||||
return {
|
||||
c_account_code_topup : item == 'S' ? invoice[key].c_account_code_topup : "",
|
||||
c_account_topup : item == 'H' ? invoice[key].c_account_topup : "",
|
||||
ba : invoice[key].ba,
|
||||
amount : invoice[key].amount,
|
||||
posting_date : invoice[key].posting_date,
|
||||
c_invoice_no : invoice[key].c_invoice_no,
|
||||
item_text : `${item == 'S' ? "DEBIT" : "KREDIT"} INVOICE CMS ${invoice[key].doc_header_text}`,
|
||||
de_cre : item
|
||||
}
|
||||
});
|
||||
|
||||
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`);
|
||||
|
||||
const response = await helper.postingSAP(url, xmlRequest)
|
||||
|
||||
winston.logger.info(`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
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
|
||||
|
||||
const cms = await model.setReturnSAP(invoice[key], resSAP, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return invoice
|
||||
|
||||
}
|
||||
|
||||
const _postingInvoice = async (uniqueCode) => {
|
||||
let postingEt = []
|
||||
await knex.transaction(async trx => {
|
||||
|
||||
postingEt = await model.getPostingEt(trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(postingEt)}`)
|
||||
|
||||
for (const item of postingEt) {
|
||||
|
||||
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)
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}`);
|
||||
|
||||
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 response = await helper.postingSAP(url, xmlRequest)
|
||||
|
||||
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
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
|
||||
|
||||
const cms = await model.setReturnSAP({c_invoice_no : item.reference}, resSAP, trx)
|
||||
|
||||
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
|
||||
// })
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return postingEt
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
postingInvoice,
|
||||
generateInvoice
|
||||
apiPostingInvoice,
|
||||
apiGenerateInvoice,
|
||||
_postingInvoice,
|
||||
_generateInvoice
|
||||
};
|
||||
@@ -7,14 +7,14 @@ router.post(
|
||||
"/generate-invoice",
|
||||
// auth.authenticateToken,
|
||||
controller.validate("generateInvoice"),
|
||||
controller.generateInvoice
|
||||
controller.apiGenerateInvoice
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/posting-invoice",
|
||||
// controller.validate("postingInvoice"),
|
||||
// auth.authenticateToken,
|
||||
controller.postingInvoice
|
||||
controller.apiPostingInvoice
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -58,30 +58,17 @@ app.listen(PORT, () => {
|
||||
|
||||
// winston.logger.info(`Response : ${response()}`);
|
||||
|
||||
// SCHEDULER FOR POSTING INVOICE
|
||||
// const invoiceControler = require("./application/modules/invoice/invoice.controller");
|
||||
// cron.schedule('*/10 * * * * *', async () => { // Every minute offset 30 seconds
|
||||
|
||||
// try {
|
||||
// console.log("first")
|
||||
// let as = await invoiceControler.generateInvoice()
|
||||
// console.log(as)
|
||||
// } catch (error) {
|
||||
// winston.logger.info(`[${moment().format('YYYY-MM-DD hh:mm:ss')}] : err`, error);
|
||||
// }
|
||||
|
||||
// });
|
||||
// SCHEDULER FOR GENERATE INVOICE
|
||||
const {_generateInvoice, _postingInvoice } = require("./application/modules/invoice/invoice.controller");
|
||||
cron.schedule('0 1 * * *', async () => {
|
||||
let uniqueCode = helper.getUniqueCode
|
||||
const ivoiceDate = moment().format("YYYY-MM-DD")
|
||||
const generate = await _generateInvoice(ivoiceDate, uniqueCode)
|
||||
winston.logger.info(`SCHEDULER-FB70 GENERATE INVOICE : ${JSON.stringify(generate)}`);
|
||||
});
|
||||
|
||||
// const soap = `<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'><SOAP:Header/><SOAP:Body><ns0:ZFMCMS_FI_001.Response xmlns:ns0='urn:sap-com:document:sap:rfc:functions'><E_DOCUMENTNUMBER/><E_DOCUMENTYEAR>0000</E_DOCUMENTYEAR><C_T_RETURN><item><TYPE>E</TYPE><DOC_NUMBER>$</DOC_NUMBER><MESSAGE_1>Error in document: BKPFF $ KAQCLNT100</MESSAGE_1><MESSAGE_2/><MESSAGE_3/><MESSAGE_4/><MESSAGE_5/><MESSAGE_6/><MESSAGE_7/><MESSAGE_8/><MESSAGE_9/><MESSAGE_10/></item><item><TYPE>E</TYPE><DOC_NUMBER>$</DOC_NUMBER><MESSAGE_1>Period 01 is not allowed</MESSAGE_1><MESSAGE_2/><MESSAGE_3/><MESSAGE_4/><MESSAGE_5/><MESSAGE_6/><MESSAGE_7/><MESSAGE_8/><MESSAGE_9/><MESSAGE_10/></item><item><TYPE>E</TYPE><DOC_NUMBER>$</DOC_NUMBER><MESSAGE_1>Period 01 is not valid</MESSAGE_1><MESSAGE_2/><MESSAGE_3/><MESSAGE_4/><MESSAGE_5/><MESSAGE_6/><MESSAGE_7/><MESSAGE_8/><MESSAGE_9/><MESSAGE_10/></item></C_T_RETURN></ns0:ZFMCMS_FI_001.Response></SOAP:Body></SOAP:Envelope>`
|
||||
// `
|
||||
// let resSAP = helper.getXMLData({status: 200}, soap)
|
||||
|
||||
// winston.logger.info(`SCHEDULER-FB70 RESPONSE posting to SAP ${JSON.stringify(resSAP)}`);
|
||||
|
||||
// const invoiceSch = require("./application/modules/invoice/invoice.model");
|
||||
// const postingEt = async () => {
|
||||
// // const rs = await invoiceSch.getPostingEt()
|
||||
// const rs = await invoiceControler.postingInvoice()
|
||||
// console.log("Posting ET : ", rs)
|
||||
// }
|
||||
// postingEt()
|
||||
cron.schedule('0 2 * * *', async () => {
|
||||
let uniqueCode = helper.getUniqueCode
|
||||
const posting = await _postingInvoice(uniqueCode)
|
||||
winston.logger.info(`SCHEDULER-FB70 POSTING INVOICE : ${JSON.stringify(posting)}`);
|
||||
});
|
||||
Reference in New Issue
Block a user