smc_cet_financials_backend/app/services/probe42lineitem.mapping.helper.js
2023-09-29 16:17:57 +05:30

151 lines
5.8 KiB
JavaScript

const { sequelize, Financials , Itemsmaster , Entities , Entityfinyear} = require('../models')
const { Op } = require("sequelize");
const { logMsg } = require('../services/logger.js');
function lineItemFetching(financials_data)
{
try
{
return new Promise((resolve, reject) => {
let data = [];
financials_data.forEach((value, index, array) => {
if(value.nature == "STANDALONE" && index == 0 || index == 1 || index == 2)
{
let object = {};
current_year_in_object = value.year;
fin_data_year = current_year_in_object.substring(0, 4);
let year = {"year": fin_data_year };
// get balance sheet line items here
let balance_sheet = value.bs;
for (const [bs_key, bs_value ] of Object.entries(balance_sheet)) {
if(bs_key === "assets" || bs_key === "liabilities" || bs_key === "subTotals")
{
Object.assign(object, year, bs_value)
}
} // end of balance_sheet object for loop
// get profit and loss line items here
let profit_and_loss = value.pnl;
for (const [pnl_key, pnl_value ] of Object.entries(profit_and_loss)) {
if(pnl_key === "lineItems" || pnl_key === "subTotals")
{
Object.assign(object, year, pnl_value)
}
} // end of profit_and_loss object for loop
setTimeout(() => { data.push(object); }, 100);
} // end of if condition
}); // end of financials data foreach
setTimeout(() => {
resolve(data);
}, 500);
});
}
catch (error)
{
console.log(error);
}
}
function lineItemMapping(financials_lineitems_data_object,entityid)
{
try
{
return new Promise((resolve, reject) => {
Itemsmaster.findAll({where : { pvtltditemkey: {[Op.ne]: 'NULL'} } , attributes: [ 'id','pvtltditemkey' ] })
.then(ItemsmasterData => {
let data = [];
year = financials_lineitems_data_object.year;
ItemsmasterData.forEach((element , key) => {
for (const [key, item_value ] of Object.entries(financials_lineitems_data_object)) {
remove_underscore = key.replace(/_/g, "");
converted_item_key = remove_underscore.toLowerCase();
if(element.dataValues.pvtltditemkey === converted_item_key)
{
let obj = {entityid : entityid ,itemid : element.dataValues.id , value : item_value , year : year};
data.push(obj);
}
}
});
setTimeout(() => { resolve(data); }, 500);
});
});
}
catch (error)
{
console.log(error);
}
}
function lineItemUpdate(update_array_data)
{
try
{
entityid = update_array_data[0].entityid;
fy = update_array_data[0].year;
var LineItemUpdate = new Promise((resolve, reject) => {
update_array_data.forEach((value,index) => {
if(value.value != null)
{
// Replace this with the INR value you want to convert
lakhsValue = convertINRtoLakhs(value.value);
Financials.update( { inr_value:value.value,value: lakhsValue , is_editable : 0 } , { where: { entity_id: value.entityid , itemid: value.itemid , fy:value.year} })
.then(data => { logMsg.info("update success : "+ data); })
.catch(err => { logMsg.info("update failed : " + err); });
}
if (index === update_array_data.length -1) { resolve(value.entityid); }
});
});
LineItemUpdate.then((entityid)=>{
//Entities.update({ "probe42_data_status": 0 },{ where: { id: entityid }});
Entityfinyear.update({ probe42_data_status : 1 } , {where : {entityid:entityid , FY :fy}})
.then(async (data) => {
logMsg.info("Probe42 data updated for EntityId = "+entityid+" Year = "+fy);
// Entityfinyear.findAll({where : {entityid:entityid},rew:true})
// .then(async (EntityfinyearData) =>{
// let count = 0;
// for(const value of EntityfinyearData)
// {
// count = count + value.probe42_data_status;
// }
// if(EntityfinyearData.length == count)
// {
// Entities.update({ "probe42_data_status": 0 },{ where: { id: entityid }});
// }
// else if(count >=1)
// {
// Entities.update({ "probe42_data_status": 3 },{ where: { id: entityid }});
// }
// });
await Entities.update({ "probe42_data_status": 0 },{ where: { id: entityid }});
}).catch(err => { logMsg.info("Probe42 data update failed for EntityId = "+entityid+" Year = "+fy); });
});
}
catch (error)
{
console.log(error);
}
}
function convertINRtoLakhs(inrValue) {
return inrValue / 100000;
}
module.exports = {
lineItemFetching : lineItemFetching ,
lineItemMapping : lineItemMapping ,
lineItemUpdate :lineItemUpdate
}