From ed3b4a7b9ebd6eb08dce8a21a49cf80a3174a32a Mon Sep 17 00:00:00 2001 From: sriramv Date: Tue, 21 Sep 2021 15:30:15 +0530 Subject: [PATCH] fin_summary:GWM --- app/controllers/financial.controller.js | 56 +++++++++++++++++++++---- app/routes/routes.js | 2 +- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/app/controllers/financial.controller.js b/app/controllers/financial.controller.js index 4fec31c..9c17dc9 100644 --- a/app/controllers/financial.controller.js +++ b/app/controllers/financial.controller.js @@ -99,14 +99,15 @@ exports.FinantialsSummary = async (req, res) => { const Year_2 = data[0][0].Y2; const Year_3 = data[0][0].Y3; const Year_4 = data[0][0].Y4; - const years_data = data[0][0]; + //const years_data = data[0][0]; add_new_FY_flag = financialControllerHelper.addNewFyFlag(Year_1); if(Year_4 == null) { + const three_years_data = {"losid": data[0][0].losid,"entityid": data[0][0].entityid,"Y1": data[0][0].Y1,"Y2": data[0][0].Y2,"Y3": data[0][0].Y3,"Y1_st_type_id":data[0][0].Y1_st_type_id,"Y1_st_type_name": data[0][0].Y1_st_type_name,"Y2_st_type_id":data[0][0].Y2_st_type_id,"Y2_st_type_name": data[0][0].Y2_st_type_name,"Y3_st_type_id": data[0][0].Y3_st_type_id,"Y3_st_type_name": data[0][0].Y3_st_type_name} sequelize.query("Select a.entity_id,x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext ,a.itemid,e.itemtext as items,e.formula,e.is_calculated,a.st_type as Y1_st_type,a.fy as Y1,a.id as Y1_id ,a.value as Y1_value ,b.st_type as Y2_st_type,b.fy as Y2, b.id Y2_id ,b.value Y2_value ,c.st_type as Y3_st_type,c.fy as Y3,c.id as Y3_id ,c.value Y3_value , g.remarks , g.id as remarks_id FROM financials a LEFT JOIN ( SELECT id,entity_id,itemid,remarks FROM fin_remarks WHERE is_active = 1) g ON a.entity_id = g.entity_id and a.itemid = g.itemid JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN ( SELECT id,entity_id, itemid, value,fy,is_active,st_type FROM financials WHERE fy = "+Year_3+" and entity_id ="+id+" and is_active = 1) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN ( SELECT id,entity_id, itemid, value,fy,is_active,st_type FROM financials WHERE fy = "+Year_2+" and entity_id = "+id+" and is_active = 1) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = "+Year_1+" and a.entity_id = "+id+" and a.is_active = 1 order by e.statement asc , e.section asc , e.id asc") .then(result => { - const arr = {"years" : years_data , "summary" : result } - res.send({'status':200,'message':"success",'data':{"years" : years_data ,"add_new_finyear_flag" : add_new_FY_flag, "summary" : result } }); + + res.send({'status':200,'message':"success",'data':{"years" : three_years_data ,"add_new_finyear_flag" : add_new_FY_flag, "summary" : result } }); }).catch(err => { res.status(500).send({'status':404, message: err.message || "Some error occurred while retrieving tutorials." ,'data': "No data" }); @@ -114,10 +115,11 @@ exports.FinantialsSummary = async (req, res) => { } else { + const four_years_data = {"losid": data[0][0].losid,"entityid": data[0][0].entityid,"Y1": data[0][0].Y1,"Y2": data[0][0].Y2,"Y3": data[0][0].Y3,"Y4": data[0][0].Y4,"Y1_st_type_id":data[0][0].Y1_st_type_id,"Y1_st_type_name": data[0][0].Y1_st_type_name,"Y2_st_type_id":data[0][0].Y2_st_type_id,"Y2_st_type_name": data[0][0].Y2_st_type_name,"Y3_st_type_id": data[0][0].Y3_st_type_id,"Y3_st_type_name": data[0][0].Y3_st_type_name,"Y4_st_type_id":data[0][0].Y4_st_type_id,"Y4_st_type_name":data[0][0].Y4_st_type_name} sequelize.query("Select a.entity_id,x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext ,a.itemid,e.itemtext as items,e.formula,e.is_calculated,a.st_type as Y1_st_type,a.fy as Y1,a.id as Y1_id ,a.value as Y1_value ,b.st_type as Y2_st_type,b.fy as Y2, b.id Y2_id ,b.value Y2_value ,c.st_type as Y3_st_type,c.fy as Y3,c.id as Y3_id ,c.value Y3_value ,d.st_type as Y4_st_type,d.fy as Y4,d.id as Y4_id ,d.value Y4_value , g.remarks , g.id as remarks_id FROM financials a LEFT JOIN ( SELECT id,entity_id,itemid,remarks FROM fin_remarks WHERE is_active = 1) g ON a.entity_id = g.entity_id and a.itemid = g.itemid JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN ( SELECT id,entity_id, itemid, value,fy,is_active,st_type FROM financials WHERE fy = "+Year_4+" and entity_id ="+id+" and is_active = 1) d ON a.entity_id = d.entity_id and a.itemid = d.itemid LEFT JOIN ( SELECT id,entity_id, itemid, value,fy,is_active,st_type FROM financials WHERE fy = "+Year_3+" and entity_id ="+id+" and is_active = 1) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN ( SELECT id,entity_id, itemid, value,fy,is_active,st_type FROM financials WHERE fy = "+Year_2+" and entity_id = "+id+" and is_active = 1) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = "+Year_1+" and a.entity_id = "+id+" and a.is_active = 1 order by e.statement asc , e.section asc , e.id asc") .then(result => { - const arr = {"years" : years_data , "summary" : result } - res.send({'status':200,'message':"success",'data':{"years" : years_data ,"add_new_finyear_flag" : add_new_FY_flag, "summary" : result } }); + + res.send({'status':200,'message':"success",'data':{"years" : four_years_data ,"add_new_finyear_flag" : add_new_FY_flag, "summary" : result } }); }).catch(err => { res.status(500).send({'status':404, message: err.message || "Some error occurred while retrieving tutorials." ,'data': "No data" }); @@ -390,10 +392,46 @@ exports.createFinancialYear = async (req, res) => { exports.testwo = async (req, res) => { - req_obj ={"losid":"TEST123"} - const testhree_fn = testhree(req_obj) - console.log(testhree_fn) - + // const fin_summary = await Financials.findAll({ + // attributes: [['fy','Y1'] ,'itemid',[sequelize.fn('sum', sequelize.col('value')), 'Y1_value'],],group: ['fy','itemid'], + // where: { fy : 2019 , entity_id : [6,7] , is_active : 1 } + // }, + // ); + const losid = req.query.losid; + const years_data = await Entityfinyears.findAll({where : { losid } , attributes: ['Y1','Y2','Y3','Y4'] , group: ['Y1','Y2','Y3','Y4'] }); + console.log(years_data); + Entityfinyears.findAll({where : { losid } , attributes: ['entityid','Y1','Y2','Y3','Y4'] }) + .then(EntityfinyearsData =>{ + let data=[]; + EntityfinyearsData.forEach((value, key)=>{ + obj_data = value.dataValues.entityid; + data.push(obj_data); + }); // end of EntityfinyearsData foreach + const IN = data.join(); + sequelize.query("SELECT x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext,a.itemid,e.itemtext as items,a.fy as Y1,SUM(a.value) as Y1_value ,b.fy as Y2,SUM(b.value) as Y2_value,c.fy as Y3,SUM(c.value) as Y3_value FROM financials a JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2019' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2020' and entity_id IN ("+IN+") and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = '2021' and a.entity_id IN ("+IN+") and is_active = 1 GROUP BY a.itemid,a.fy,a.is_active,b.fy,c.fy,x.id,x.statement,y.id,y.section,e.subsection,z.subsection,z.itemtext,e.itemtext,e.statement,e.section,e.id order by e.statement asc , e.section asc , e.id asc") + .then(sum_data =>{ + res.send({'status':200,message: "success" ,'data': years_data }); + }) + .catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); + + //res.send({'status':200,message: "success" ,'data': data }); + }) + .catch(err => { + res.status(404).send({'status':404, + message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + }); + // sequelize.query("SELECT x.id as statement_id,x.statement as statement,y.id as section_id,y.section as section,e.subsection as subsection_id,z.subsection as subsection , z.itemtext as subsection_itemtext,a.itemid,e.itemtext as items,a.fy as Y1,SUM(a.value) as Y1_value ,b.fy as Y2,SUM(b.value) as Y2_value,c.fy as Y3,SUM(c.value) as Y3_value FROM financials a JOIN ( SELECT id,itemtext,statement,section,subsection,formula,is_calculated FROM itemsmaster ) e ON a.itemid = e.id JOIN ( SELECT id, statement FROM statementsmaster ) x ON e.statement = x.id JOIN ( SELECT id, section FROM sectionsmaster ) y ON e.section = y.id LEFT JOIN ( SELECT id, subsection , itemtext FROM subsectionsmaster ) z ON e.subsection = z.id LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2019' and entity_id IN (6,7) and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) c ON a.entity_id = c.entity_id and a.itemid = c.itemid LEFT JOIN(SELECT entity_id,itemid,fy,value FROM financials WHERE fy = '2020' and entity_id IN (6,7) and is_active = 1 GROUP BY itemid,fy,is_active,value,entity_id) b ON a.entity_id = b.entity_id and a.itemid = b.itemid WHERE a.fy = '2021' and a.entity_id IN (6,7) and is_active = 1 GROUP BY a.itemid,a.fy,a.is_active,b.fy,c.fy,x.id,x.statement,y.id,y.section,e.subsection,z.subsection,z.itemtext,e.itemtext,e.statement,e.section,e.id order by e.statement asc , e.section asc , e.id asc") + // .then(data =>{ + // res.send({'status':200,message: "success" ,'data': data }); + // }) + // .catch(err => { + // res.status(404).send({'status':404, + // message: err.message || "Some error occurred while insertinging statements." ,'data': "No data" }); + // }); + }; function testhree(req , res) { console.log( new Date().getFullYear()) diff --git a/app/routes/routes.js b/app/routes/routes.js index 6698188..c77a034 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -9,7 +9,7 @@ module.exports = app => { - + /** * @swagger * /api/FinantialsSummary: