diff --git a/app/controllers/financial.controller.js b/app/controllers/financial.controller.js index 57688ad..bbb080e 100644 --- a/app/controllers/financial.controller.js +++ b/app/controllers/financial.controller.js @@ -65,95 +65,99 @@ exports.MenuList = (req, res) => { var EntityInsert = new Promise((resolve, reject) => { // insert pro42data into entities table using using foreach Pro42Data.forEach((Pro42Datavalue, key,array)=>{ - // check if the Pro42Data (entity) already inserted - Entities.findOne({ where: { losid : Pro42Datavalue.losid , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.cin } }).then(Entity => { - if(Entity) - { - // skip inserting - if (key === array.length -1) {resolve();} - } - else - { - - Entities.create({ "losid":Pro42Datavalue.losid,"pan":Pro42Datavalue.pan,"cin":Pro42Datavalue.cin,"bizname":Pro42Datavalue.bizname,"entity_type":Pro42Datavalue.entity_type,"activity_group":Pro42Datavalue.activity_group,"biz_act_desc":Pro42Datavalue.biz_act_desc,"createdby":Pro42Datavalue.createdby}) - .then(Entitydata => { - - - //process instance Id update - const cin = Entitydata.dataValues.cin; - const entityid = Entitydata.dataValues.id; - const user = "SMC"; - processInstanceId = camundaHelper.startFinancialsProcessInstance(Entitydata.dataValues.losid,Entitydata.dataValues.pan,user); - processInstanceId.then(function(InstanceId){ - Entities.update({ "process_instance_id":InstanceId },{ where: { id: entityid }}); - }); - - // call and get financial year wise data for entity (probe42 api call) - financials_data_api_response = ApiCallHelper.probe42ApiCall(cin); - financials_data_api_response.then(function(result){ - //filter lineitems from api responce and construct to needed format - lineItem_data = Probe42DataMappingHelper.lineItemFetching(result); - lineItem_data.then(function(lineItem_result){ - - //create financial table and entityfinyear table record here - lineItem_result.forEach((value, key)=>{ - - Entityfinyear.create({ "losid":Entitydata.dataValues.losid,"entityid":Entitydata.dataValues.id,"FY":value.year,"st_type": 1 }) - .then(Entityfinyearsdata => { + + // check if the Pro42Data (entity) already inserted + Entities.findOne({ where: { losid : Pro42Datavalue.losid , pan : Pro42Datavalue.pan , cin : Pro42Datavalue.cin } }).then(Entity => { + if(Entity) + { + // skip inserting + if (key === array.length -1) {resolve();} + } + else + { + + Entities.create({ "losid":Pro42Datavalue.losid,"pan":Pro42Datavalue.pan,"cin":Pro42Datavalue.cin,"bizname":Pro42Datavalue.bizname,"entity_type":Pro42Datavalue.entity_type,"activity_group":Pro42Datavalue.activity_group,"biz_act_desc":Pro42Datavalue.biz_act_desc,"createdby":Pro42Datavalue.createdby}) + .then(Entitydata => { + + + //process instance Id update + const cin = Entitydata.dataValues.cin; + const entityid = Entitydata.dataValues.id; + const user = "SMC"; + processInstanceId = camundaHelper.startFinancialsProcessInstance(Entitydata.dataValues.losid,Entitydata.dataValues.pan,user); + processInstanceId.then(function(InstanceId){ + Entities.update({ "process_instance_id":InstanceId },{ where: { id: entityid }}); + }); + + // call and get financial year wise data for entity (probe42 api call) + financials_data_api_response = ApiCallHelper.probe42ApiCall(cin); + financials_data_api_response.then(function(result){ + //filter lineitems from api responce and construct to needed format + lineItem_data = Probe42DataMappingHelper.lineItemFetching(result); + lineItem_data.then(function(lineItem_result){ - // create skeletion - // generate financials data array - let fin_data_array = []; - for (let i = 1; i < 141; i++) - { - if(i != 124 && i != 125 && i != 130) - { - fin_data_obj = { "entity_id":Entityfinyearsdata.dataValues.entityid,"st_type":Entityfinyearsdata.dataValues.st_type,"itemid":i,"fy":Entityfinyearsdata.dataValues.FY,"value":0,"is_active":1,"createdby":4} - fin_data_array.push(fin_data_obj); - } - } //console.log("------"+entity_id +"---"+ fy) - //bulk financials data create - Financials.bulkCreate(fin_data_array).then(data=>{}).catch(err=>(console.log(err))); - - console.log("working") - setTimeout(() => { - lineItem_result.forEach((value,index,array) => { - // mapping of probe42 api lineItem with itemMaster primary key - final_array = Probe42DataMappingHelper.lineItemMapping(value,entityid); - final_array.then(function(update_array_data){ - // update mapped lineitem value here - Probe42DataMappingHelper.lineItemUpdate(update_array_data); - });//end of lineItemUpdate helper call - });//end of lineItem mapping with itemMaster primary key helper call - }, 500); + //create financial table and entityfinyear table record here + lineItem_result.forEach((value, key)=>{ + Entityfinyear.create({ "losid":Entitydata.dataValues.losid,"entityid":Entitydata.dataValues.id,"FY":value.year,"st_type": 1 }) + .then(Entityfinyearsdata => { + + // create skeletion with itemmaster data (itemmaster(id) == financial(itemid)) + Itemsmaster.findAll({ where :{is_active : 1},attributes: ['id'] , order: [['id', 'ASC']] }) + .then(ItemsmasterData => { + //construct lineitem array for bulk insert using itemmaster primarykey (id) + var SkeletonInsert = new Promise((resolve, reject) => { + let fin_data_array = []; + ItemsmasterData.forEach( (itemvalue , index , itemvaluearray) => { + fin_data_obj = { "entity_id":Entityfinyearsdata.dataValues.entityid,"st_type":Entityfinyearsdata.dataValues.st_type,"itemid":itemvalue.dataValues.id,"fy":Entityfinyearsdata.dataValues.FY,"value":0,"is_active":1,"createdby":4} + fin_data_array.push(fin_data_obj); + if (index === itemvaluearray.length -1) { resolve(fin_data_array); } + }) + }); + SkeletonInsert.then((Query_data)=>{ + Financials.bulkCreate(Query_data).then(data=>{ + //line item data update + lineItem_result.forEach((value,index,array) => { + // mapping of probe42 api lineItem with itemMaster primary key + final_array = Probe42DataMappingHelper.lineItemMapping(value,entityid); + final_array.then(function(update_array_data){ + // update mapped lineitem value here + Probe42DataMappingHelper.lineItemUpdate(update_array_data); + });//end of lineItemUpdate helper call + });//end of lineItem mapping with itemMaster primary key helper call + + }).catch(err=>(console.log(err))); //skeleton bulk create query + }); + + }).catch(err=>(console.log(err))); //itemmaster query + + + + }).catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); // end of entity fin year table data insert + + });//end of foreach - }).catch(err => { + });//end of lineItem filter from api response helper call + });//end of probe42 api call + + + + // resolve here + if (key === array.length -1) { resolve(); } + + }).catch(err => { res.status(404).send({'status':404, message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); - }); // end of entity fin year table data insert - - });//end of foreach + }); // end of entity insert - - - });//end of lineItem filter from api response helper call - });//end of probe42 api call - - - - - if (key === array.length -1) {resolve();} - - }).catch(err => { - res.status(404).send({'status':404, - message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); - }); // end of entity insert - } //end of else condition - - }); // end of (losid,pan,cin) combination checking entity data exist or not + } //end of else condition + + }); // end of (losid,pan,cin) combination checking entity data exist or not }); // end of pro42 data foreach