This commit is contained in:
Nuranggi Hermawan
2022-12-04 10:24:24 +07:00
parent af90fa7681
commit cec49dccc8
3 changed files with 150 additions and 125 deletions
@@ -178,154 +178,173 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => {
invoice
)}`
);
if (invoice.length > 0) {
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 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 = {
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} preparing posting FB70 to SAP`
);
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")
for (const key in invoice) {
let prepare = {
status : true,
cms : true,
message: "Success",
}
if(!credUsername) {
prepare = {
status : false,
cms : false,
message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_USERNAME",
}
}
if(!credPassword) {
prepare = {
status : false,
cms : false,
message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
if(!parameter) {
prepare = {
status : false,
cms : false,
message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_PASSWORD",
}
}
const credential = {
username: credUsername.c_value,
password: credPassword.c_value
}
winston.logger.debug(
`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`
);
let response = {}
if(prepare.status){
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) => {
let resSAP
if(prepare.status){
resSAP = helper.getXMLData(response);
}else {
resSAP = {
type: 'E',
message : prepare?.message || "FAILED GET PARAMETER SETTING",
data : {}
if(invoice[key].c_account_code_topup == null || invoice[key].c_account_topup == null) {
prepare = {
status : false,
cms : false,
message: "[CMS] FAILED GET PARTNER ACCOUNT CODE TOPUP / ACCOUNT TOPUP",
}
}
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(
resSAP
)}`
if(invoice[key].c_account_code_topup == "" || invoice[key].c_account_topup == "") {
prepare = {
status : false,
cms : false,
message: "[CMS] FAILED GET PARTNER ACCOUNT CODE TOPUP / ACCOUNT TOPUP",
}
}
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
);
const cms = await model.setReturnSAP(invoice[key], resSAP, trx, true);
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`
`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`
);
let response = {}
collection.push(cms.invoice[0])
if(prepare.status){
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) => {
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, true);
winston.logger.info(
`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`
);
collection.push(cms.invoice[0])
// });
}
});
}
}
});
return collection;
};