
--
-- Table structure for table `regions`
--

CREATE TABLE `regions` (
  `R_ID` int(11) NOT NULL AUTO_INCREMENT,
  `REGION_NAME` varchar(45) DEFAULT NULL,
  `PARENT` varchar(45) DEFAULT NULL,
  `REGION_DESC` varchar(250) DEFAULT NULL,
  `CREATED` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `CREATED_BY` varchar(25) DEFAULT NULL,
  `TYPE` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`R_ID`),
  UNIQUE KEY `REGION_NAME_UNIQUE` (`REGION_NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=294 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `regions`
--

INSERT INTO `regions` (`R_ID`, `REGION_NAME`, `PARENT`, `REGION_DESC`, `CREATED`, `CREATED_BY`, `TYPE`) VALUES (146,'Pakistan','World','Pakistan','2018-01-05 10:36:24','ADMIN','C'),(147,'Balochistan','Pakistan','Balochistan','2018-01-05 10:36:24','ADMIN','P'),(148,'Awaran','Balochistan','Awaran','2018-01-05 10:36:24','ADMIN','D'),(149,'Barkhan','Balochistan','Barkhan','2018-01-05 10:36:25','ADMIN','D'),(150,'Bolan','Balochistan','Bolan','2018-01-05 10:36:25','ADMIN','D'),(151,'Chagai','Balochistan','Chagai','2018-01-05 10:36:25','ADMIN','D'),(152,'Dera Bugti','Balochistan','Dera Bugti','2018-01-05 10:36:25','ADMIN','D'),(153,'Gwadar','Balochistan','Gwadar','2018-01-05 10:36:25','ADMIN','D'),(154,'Jafarabad','Balochistan','Jafarabad','2018-01-05 10:36:25','ADMIN','D'),(155,'Jhal Magsi','Balochistan','Jhal Magsi','2018-01-05 10:36:25','ADMIN','D'),(156,'Kalat','Balochistan','Kalat','2018-01-05 10:36:25','ADMIN','D'),(157,'Kech (Turbat)','Balochistan','Kech (Turbat)','2018-01-05 10:36:25','ADMIN','D'),(158,'Kharan','Balochistan','Kharan','2018-01-05 10:36:25','ADMIN','D'),(159,'Khuzdar','Balochistan','Khuzdar','2018-01-05 10:36:25','ADMIN','D'),(160,'Kohlu','Balochistan','Kohlu','2018-01-05 10:36:25','ADMIN','D'),(161,'Lasbela','Balochistan','Lasbela','2018-01-05 10:36:25','ADMIN','D'),(162,'Loralai','Balochistan','Loralai','2018-01-05 10:36:25','ADMIN','D'),(163,'Mastung','Balochistan','Mastung','2018-01-05 10:36:25','ADMIN','D'),(164,'Musakhel','Balochistan','Musakhel','2018-01-05 10:36:25','ADMIN','D'),(165,'Nasirabad','Balochistan','Nasirabad','2018-01-05 10:36:25','ADMIN','D'),(166,'Nushki','Balochistan','Nushki','2018-01-05 10:36:25','ADMIN','D'),(167,'Panjgur','Balochistan','Panjgur','2018-01-05 10:36:25','ADMIN','D'),(168,'Pishin','Balochistan','Pishin','2018-01-05 10:36:25','ADMIN','D'),(169,'Qila Abdullah','Balochistan','Qila Abdullah','2018-01-05 10:36:25','ADMIN','D'),(170,'Qila Saifullah','Balochistan','Qila Saifullah','2018-01-05 10:36:25','ADMIN','D'),(171,'Quetta','Balochistan','Quetta','2018-01-05 10:36:26','ADMIN','D'),(172,'Sherani','Balochistan','Sherani','2018-01-05 10:36:26','ADMIN','D'),(173,'Sibi','Balochistan','Sibi','2018-01-05 10:36:26','ADMIN','D'),(174,'Washuk','Balochistan','Washuk','2018-01-05 10:36:26','ADMIN','D'),(175,'Zhob','Balochistan','Zhob','2018-01-05 10:36:26','ADMIN','D'),(176,'Ziarat','Balochistan','Ziarat','2018-01-05 10:36:26','ADMIN','D'),(177,'KPK','Pakistan','KPK','2018-01-05 10:36:26','ADMIN','P'),(178,'Abbottabad','KPK','Abbottabad','2018-01-05 10:36:26','ADMIN','D'),(179,'Bannu','KPK','Bannu','2018-01-05 10:36:26','ADMIN','D'),(180,'Batgram','KPK','Batgram','2018-01-05 10:36:26','ADMIN','D'),(181,'Buner (Daggar)','KPK','Buner (Daggar)','2018-01-05 10:36:26','ADMIN','D'),(182,'Charsadda','KPK','Charsadda','2018-01-05 10:36:26','ADMIN','D'),(183,'Chitral','KPK','Chitral','2018-01-05 10:36:26','ADMIN','D'),(184,'D.I. Khan','KPK','D.I. Khan','2018-01-05 10:36:26','ADMIN','D'),(185,'Hangu','KPK','Hangu','2018-01-05 10:36:26','ADMIN','D'),(186,'Haripur','KPK','Haripur','2018-01-05 10:36:26','ADMIN','D'),(187,'Karak','KPK','Karak','2018-01-05 10:36:26','ADMIN','D'),(188,'Kohat','KPK','Kohat','2018-01-05 10:36:26','ADMIN','D'),(189,'Kohistan','KPK','Kohistan','2018-01-05 10:36:26','ADMIN','D'),(190,'Lakki Marwat','KPK','Lakki Marwat','2018-01-05 10:36:26','ADMIN','D'),(191,'Lower Dir','KPK','Lower Dir','2018-01-05 10:36:26','ADMIN','D'),(192,'Malakand','KPK','Malakand','2018-01-05 10:36:26','ADMIN','D'),(193,'Mansehra','KPK','Mansehra','2018-01-05 10:36:27','ADMIN','D'),(194,'Mardan','KPK','Mardan','2018-01-05 10:36:27','ADMIN','D'),(195,'Mingora','KPK','Mingora','2018-01-05 10:36:27','ADMIN','D'),(196,'Nowshera','KPK','Nowshera','2018-01-05 10:36:27','ADMIN','D'),(197,'Peshawar','KPK','Peshawar','2018-01-05 10:36:27','ADMIN','D'),(198,'Shangla','KPK','Shangla','2018-01-05 10:36:27','ADMIN','D'),(199,'Swabi','KPK','Swabi','2018-01-05 10:36:27','ADMIN','D'),(200,'Swat','KPK','Swat','2018-01-05 10:36:27','ADMIN','D'),(201,'Tank','KPK','Tank','2018-01-05 10:36:27','ADMIN','D'),(202,'Upper Dir','KPK','Upper Dir','2018-01-05 10:36:27','ADMIN','D'),(203,'Punjab','Pakistan','Punjab','2018-01-05 10:36:27','ADMIN','P'),(204,'Attock','Punjab','Attock','2018-01-05 10:36:27','ADMIN','D'),(205,'Bahawalpur','Punjab','Bahawalpur','2018-01-05 10:36:27','ADMIN','D'),(206,'Bhakkar','Punjab','Bhakkar','2018-01-05 10:36:27','ADMIN','D'),(207,'Bhawalnagar','Punjab','Bhawalnagar','2018-01-05 10:36:27','ADMIN','D'),(208,'Chakwal','Punjab','Chakwal','2018-01-05 10:36:27','ADMIN','D'),(209,'Chiniot','Punjab','Chiniot','2018-01-05 10:36:27','ADMIN','D'),(210,'D.G. Khan','Punjab','D.G. Khan','2018-01-05 10:36:27','ADMIN','D'),(211,'Faisalabad','Punjab','Faisalabad','2018-01-05 10:36:27','ADMIN','D'),(212,'Gujranwala','Punjab','Gujranwala','2018-01-05 10:36:27','ADMIN','D'),(213,'Gujrat','Punjab','Gujrat','2018-01-05 10:36:27','ADMIN','D'),(214,'Hafizabad','Punjab','Hafizabad','2018-01-05 10:36:27','ADMIN','D'),(215,'Jhang','Punjab','Jhang','2018-01-05 10:36:27','ADMIN','D'),(216,'Jhelum','Punjab','Jhelum','2018-01-05 10:36:27','ADMIN','D'),(217,'Kasur','Punjab','Kasur','2018-01-05 10:36:27','ADMIN','D'),(218,'Khanewal','Punjab','Khanewal','2018-01-05 10:36:28','ADMIN','D'),(219,'Khushab','Punjab','Khushab','2018-01-05 10:36:28','ADMIN','D'),(220,'Lahore','Punjab','Lahore','2018-01-05 10:36:28','ADMIN','D'),(221,'Leyyah','Punjab','Leyyah','2018-01-05 10:36:28','ADMIN','D'),(222,'Lodhran','Punjab','Lodhran','2018-01-05 10:36:28','ADMIN','D'),(223,'Mandi Bahauddin','Punjab','Mandi Bahauddin','2018-01-05 10:36:28','ADMIN','D'),(224,'Mianwali','Punjab','Mianwali','2018-01-05 10:36:28','ADMIN','D'),(225,'Multan','Punjab','Multan','2018-01-05 10:36:28','ADMIN','D'),(226,'Muzaffargarh','Punjab','Muzaffargarh','2018-01-05 10:36:28','ADMIN','D'),(227,'Nankana Sahib','Punjab','Nankana Sahib','2018-01-05 10:36:28','ADMIN','D'),(228,'Narowal','Punjab','Narowal','2018-01-05 10:36:28','ADMIN','D'),(229,'Okara','Punjab','Okara','2018-01-05 10:36:28','ADMIN','D'),(230,'Pakpattan','Punjab','Pakpattan','2018-01-05 10:36:28','ADMIN','D'),(231,'Rahimyar Khan','Punjab','Rahimyar Khan','2018-01-05 10:36:28','ADMIN','D'),(232,'Rajanpur','Punjab','Rajanpur','2018-01-05 10:36:28','ADMIN','D'),(233,'Rawalpindi','Punjab','Rawalpindi','2018-01-05 10:36:28','ADMIN','D'),(234,'Sahiwal','Punjab','Sahiwal','2018-01-05 10:36:28','ADMIN','D'),(235,'Sargodha','Punjab','Sargodha','2018-01-05 10:36:28','ADMIN','D'),(236,'Sheikhupura','Punjab','Sheikhupura','2018-01-05 10:36:28','ADMIN','D'),(237,'Sialkot','Punjab','Sialkot','2018-01-05 10:36:28','ADMIN','D'),(238,'Toba Tek Singh','Punjab','Toba Tek Singh','2018-01-05 10:36:28','ADMIN','D'),(239,'Vihari','Punjab','Vihari','2018-01-05 10:36:28','ADMIN','D'),(240,'Sindh','Pakistan','Sindh','2018-01-05 10:36:29','ADMIN','P'),(241,'Badin','Sindh','Badin','2018-01-05 10:36:29','ADMIN','D'),(242,'Dadu','Sindh','Dadu','2018-01-05 10:36:29','ADMIN','D'),(243,'Ghotki','Sindh','Ghotki','2018-01-05 10:36:29','ADMIN','D'),(244,'Hyderabad','Sindh','Hyderabad','2018-01-05 10:36:29','ADMIN','D'),(245,'Jacobabad','Sindh','Jacobabad','2018-01-05 10:36:29','ADMIN','D'),(246,'Jamshoro','Sindh','Jamshoro','2018-01-05 10:36:29','ADMIN','D'),(247,'Karachi','Sindh','Karachi','2018-01-05 10:36:29','ADMIN','D'),(248,'Kashmore','Sindh','Kashmore','2018-01-05 10:36:29','ADMIN','D'),(249,'Khairpur','Sindh','Khairpur','2018-01-05 10:36:29','ADMIN','D'),(250,'Larkana','Sindh','Larkana','2018-01-05 10:36:29','ADMIN','D'),(251,'Matyari','Sindh','Matyari','2018-01-05 10:36:29','ADMIN','D'),(252,'Mirpur Khas','Sindh','Mirpur Khas','2018-01-05 10:36:29','ADMIN','D'),(253,'Naushahro Feroze','Sindh','Naushahro Feroze','2018-01-05 10:36:29','ADMIN','D'),(254,'Nawabshah','Sindh','Nawabshah','2018-01-05 10:36:29','ADMIN','D'),(255,'Sanghar','Sindh','Sanghar','2018-01-05 10:36:29','ADMIN','D'),(256,'Sehwan Sharif','Sindh','Sehwan Sharif','2018-01-05 10:36:29','ADMIN','D'),(257,'Shehdad Kot','Sindh','Shehdad Kot','2018-01-05 10:36:29','ADMIN','D'),(258,'Shikarpur','Sindh','Shikarpur','2018-01-05 10:36:29','ADMIN','D'),(259,'Sukkur','Sindh','Sukkur','2018-01-05 10:36:29','ADMIN','D'),(260,'Tando Allahyar','Sindh','Tando Allahyar','2018-01-05 10:36:29','ADMIN','D'),(261,'Tando Jam','Sindh','Tando Jam','2018-01-05 10:36:30','ADMIN','D'),(262,'Tando Muhammad Khan','Sindh','Tando Muhammad Khan','2018-01-05 10:36:30','ADMIN','D'),(263,'Tharparkar','Sindh','Tharparkar','2018-01-05 10:36:30','ADMIN','D'),(264,'Thatta','Sindh','Thatta','2018-01-05 10:36:30','ADMIN','D'),(265,'Umer Kot','Sindh','Umer Kot','2018-01-05 10:36:30','ADMIN','D'),(266,'AJK','Pakistan','AJK','2018-01-05 10:36:30','ADMIN','P'),(267,'Bagh','AJK','Bagh','2018-01-05 10:36:30','ADMIN','D'),(268,'Bhimber','AJK','Bhimber','2018-01-05 10:36:30','ADMIN','D'),(269,'Kotli','AJK','Kotli','2018-01-05 10:36:30','ADMIN','D'),(270,'Mirpur','AJK','Mirpur','2018-01-05 10:36:30','ADMIN','D'),(271,'Muzaffarabad','AJK','Muzaffarabad','2018-01-05 10:36:30','ADMIN','D'),(272,'Neelum ','AJK','Neelum ','2018-01-05 10:36:30','ADMIN','D'),(273,'Poonch','AJK','Poonch','2018-01-05 10:36:30','ADMIN','D'),(274,'Sudhnati','AJK','Sudhnati','2018-01-05 10:36:30','ADMIN','D'),(275,'FANA','Pakistan','FANA','2018-01-05 10:36:30','ADMIN','P'),(276,'Astore','FANA','Astore','2018-01-05 10:36:30','ADMIN','D'),(277,'Diamer','FANA','Diamer','2018-01-05 10:36:30','ADMIN','D'),(278,'Ghanche','FANA','Ghanche','2018-01-05 10:36:30','ADMIN','D'),(279,'Ghizer','FANA','Ghizer','2018-01-05 10:36:30','ADMIN','D'),(280,'Gilgit','FANA','Gilgit','2018-01-05 10:36:30','ADMIN','D'),(281,'Skardu','FANA','Skardu','2018-01-05 10:36:30','ADMIN','D'),(282,'FATA','Pakistan','FATA','2018-01-05 10:36:30','ADMIN','P'),(283,'Bajaur Agency','FATA','Bajaur Agency','2018-01-05 10:36:30','ADMIN','D'),(284,'Khyber Agency','FATA','Khyber Agency','2018-01-05 10:36:30','ADMIN','D'),(285,'Kurram Agency','FATA','Kurram Agency','2018-01-05 10:36:30','ADMIN','D'),(286,'Mohmand Agency','FATA','Mohmand Agency','2018-01-05 10:36:31','ADMIN','D'),(287,'North Waziristan Agency','FATA','North Waziristan Agency','2018-01-05 10:36:31','ADMIN','D'),(288,'Orakzai Agency','FATA','Orakzai Agency','2018-01-05 10:36:31','ADMIN','D'),(289,'South Waziristan Agency','FATA','South Waziristan Agency','2018-01-05 10:36:31','ADMIN','D'),(290,'ICT','ICT','ICT','2018-01-05 10:36:31','ADMIN','D'),(291,'GRAND TOTAL','GT','GRAND TOTAL','2018-01-05 10:36:31','ADMIN','GT'),(292,'Shaheed Benazirabad','Sindh','Shaheed Benazirabad','2018-05-08 14:52:12','ADMIN','D'),(293,'Sujawal','Sindh','Sujawal','2018-05-08 14:53:27','ADMIN','D');

--
-- Table structure for table `table1`
--

CREATE TABLE `table1` (
  `p_values` varchar(500) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `table1`
--

INSERT INTO `table1` (`p_values`) VALUES ('Total equity');

--
-- Table structure for table `tally`
--

CREATE TABLE `tally` (
  `n` int(11) NOT NULL,
  PRIMARY KEY (`n`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tally`
--

INSERT INTO `tally` (`n`) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31),(32),(33),(34),(35),(36),(37),(38),(39),(40),(41),(42),(43),(44),(45),(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60),(61),(62),(63),(64),(65),(66),(67),(68),(69),(70),(71),(72),(73),(74),(75),(76),(77),(78),(79),(80),(81),(82),(83),(84),(85),(86),(87),(88),(89),(90),(91),(92),(93),(94),(95),(96),(97),(98),(99),(100);

--
-- Table structure for table `total_temp`
--

CREATE TABLE `total_temp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `indicator_name` varchar(4000) DEFAULT NULL,
  `value` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `total_temp`
--

INSERT INTO `total_temp` (`id`, `indicator_name`, `value`) VALUES (1,'Portfolio at risk (>30)-to-gross loan portfolio',145.21496603011548),(2,'Portfolio at risk(>90)-to-gross loan portfolio',96.84343865724334),(3,'Average gross loan portfolio',413693782),(4,'Write off-to-average gross loan portfolio',6602320.756233744),(5,'Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)',2.1502975258474146);

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
  `u_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `access` varchar(4500) NOT NULL,
  `type` varchar(45) NOT NULL DEFAULT 'normal',
  PRIMARY KEY (`u_id`),
  UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`u_id`, `username`, `password`, `access`, `type`) VALUES (1,'sami','123','[PMR, MicroWatch]','normal'),(2,'humza','123','PMR, MicroWatch, MicroEye, UserManagement','normal'),(3,'admin','123','PMR, MicroWatch, MicroEye, UserManagement','super'),(5,'obaid','123','[MicroWatch]','normal');

--
-- Dumping events for database 'microwb1_db_pmn'
--

--
-- Dumping routines for database 'microwb1_db_pmn'
--
/*!50003 DROP PROCEDURE IF EXISTS `get_categories_from_IID` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `get_categories_from_IID`(IN `p_indicator_name` VARCHAR(150), OUT `pCODE` VARCHAR(4000), OUT `pDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
    NO SQL
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='99';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
select CATEGORY_NAME from categories where cid in (select CATEGORY_ID from indicators where INDICATOR_NAME = p_indicator_name);
  SET pCODE ='00';
  SET pDESC = 'Success';
  SET P_MSG ='Y';
  end ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `get_data_from_iid_cid` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `get_data_from_iid_cid`(IN `p_indicator_name` VARCHAR(150), 
IN `p_category_name` VARCHAR(150), 
IN `pStartYear` INT, 
IN `pEndYear` INT, 
IN porg_type varchar(4000),
IN PORG_ABV VARCHAR(4000),
OUT `pCODE` VARCHAR(4000), 
OUT `pDESC` VARCHAR(4000), 
OUT `P_MSG` VARCHAR(4000))
    NO SQL
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='99';
  SET pDESC = 'Failure';
  SET P_MSG ='N';

IF porg_type IS NULL and PORG_ABV is null
THEN
select 
    o.organization_name,
    pd.year,
    pd.value,
    c.category_name,
    i.indicator_name
from
    categories c,
    indicators i,
    pmr_data pd,
    pmr_organization o
where
    c.cid = pd.cid and i.iid = pd.iid
        and c.category_name = p_category_name
        and i.indicator_name = p_indicator_name
        and o.oid = pd.oid
        and pd.year between pstartyear and pendyear
order by o.organization_name , pd.year asc;


ELSE

if porg_type is not null and PORG_ABV is null
then


select 
    o.organization_name,
    pd.year,
    pd.value,
    c.category_name,
    i.indicator_name
from
    categories c,
    indicators i,
    pmr_data pd,
    pmr_organization o,
	organization_type ot
where
    c.cid = pd.cid and i.iid = pd.iid
and o.otid = ot.otid
        and c.category_name = p_category_name
        and i.indicator_name = p_indicator_name
        and o.oid = pd.oid
        and pd.year between pstartyear and pendyear
		and ot.ORGANIZATION_TYPE in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(porg_type, ',', n.digit+1), ',', -1) val
																				FROM
																				  organization_type
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(porg_type, ',' , '')) <= LENGTH(porg_type)-n.digit)

order by o.organization_name , pd.year asc;
else

select 
    o.organization_name,
    pd.year,
    pd.value,
    c.category_name,
    i.indicator_name
from
    categories c,
    indicators i,
    pmr_data pd,
    pmr_organization o,
	organization_type ot
where
    c.cid = pd.cid and i.iid = pd.iid
and o.otid = ot.otid
        and c.category_name = p_category_name
        and i.indicator_name = p_indicator_name
        and o.oid = pd.oid
        and pd.year between pstartyear and pendyear
		and ot.ORGANIZATION_TYPE in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(porg_type, ',', n.digit+1), ',', -1) val
																				FROM
																				  organization_type
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(porg_type, ',' , '')) <= LENGTH(porg_type)-n.digit)
		AND O.ORG_ABV IN (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(PORG_ABV, ',', n.digit+1), ',', -1) val
																				FROM
																				  pmr_organization
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(PORG_ABV, ',' , '')) <= LENGTH(PORG_ABV)-n.digit)
order by o.organization_name , pd.year asc;
end if;
END IF;
SET pCODE ='00';
  SET pDESC = 'Success';
  SET P_MSG ='Y';
  
end ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `lov_indicator_types` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `lov_indicator_types`(
 IN `P_Indicator_Name` VARCHAR(4000),
 OUT `Pcode` VARCHAR(4000),
 OUT `Pdesc` VARCHAR(4000),
 OUT `P_Msg` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  Set Pdesc = CONCAT(IFNULL(SQLCODE, '') , '|' , Ifnull(Sqlerrm, ''));
  Set Pcode ='99';
  SET P_Msg ='N';
END;
  
  select IT_NAME from microwatch_indicator_types where indicator_ID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = P_Indicator_Name);
  
  
  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `lov_microwatch_indicators` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `lov_microwatch_indicators`(
	OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   SET PDESC  =  CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

 SELECT * FROM microwatch_indicators;


  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `lov_microwatch_indicatorst` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `lov_microwatch_indicatorst`(
	OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   SET PDESC  =  CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

 SELECT * FROM microwatch_indicators;


  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `MW_MFB_LARGEST_GEOGRAPHIC_SPREAD_MICROSAVINGS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `MW_MFB_LARGEST_GEOGRAPHIC_SPREAD_MICROSAVINGS`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';
 

SELECT 
   DISTINCT  
    O.ORGANIZATION_NAME,
    O.ORG_ABV,
   COUNT(*) AS RESULT
FROM
		organization O,
		REGIONS R,
		-- Regional_Indicators RI,
        (SELECT * FROM regional_indicators where FILE_ID in(SELECT distinct FILE_ID FROM  regional_indicators where CID = 2 AND Indicator_Value > 0)) RI,
        FILE_DETAILS F
WHERE
				R.R_ID = RI.REGION_ID
        AND RI.FILE_ID = F.FILE_ID
        AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
         AND RI.Indicator_Value > 0
        AND R.TYPE = 'D'
AND F.YEAR         = PYEAR_FIRST
AND F.QUARTER  = PQUARTER_FIRST
		AND RI.IID IN(13,14) 
        AND O.OTID = 2

GROUP BY  O.ORGANIZATION_NAME , O.ORG_ABV
ORDER BY 3 DESC;
-- LIMIT 5;


 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `MW_MFI_LARGEST_GEOGRAPHIC_SPREAD_MICROCREDIT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `MW_MFI_LARGEST_GEOGRAPHIC_SPREAD_MICROCREDIT`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';
 

SELECT 
   DISTINCT  
    O.ORGANIZATION_NAME,
    O.ORG_ABV,
   COUNT(*) AS RESULT
FROM
		organization O,
		REGIONS R,
		-- Regional_Indicators RI,
        (SELECT * FROM regional_indicators where FILE_ID in(SELECT distinct FILE_ID FROM  regional_indicators where CID = 1 AND Indicator_Value > 0)) RI,
        FILE_DETAILS F
WHERE
				R.R_ID = RI.REGION_ID
        AND RI.FILE_ID = F.FILE_ID
        AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
         AND RI.Indicator_Value > 0
        AND R.TYPE = 'D'
AND F.YEAR         = PYEAR_FIRST
AND F.QUARTER  = PQUARTER_FIRST
		AND RI.IID IN(13,14) 
        AND O.OTID = 1

GROUP BY  O.ORGANIZATION_NAME , O.ORG_ABV
ORDER BY 3 DESC;
-- LIMIT 5;


 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Add_Categorie` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Add_Categorie`(IN `P_CATEGORY_NAME` VARCHAR(4000), IN `P_CATEGORY_DESCRIPTION` VARCHAR(4000), IN `P_Created_By` VARCHAR(4000), OUT `Pcode` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  Set Pdesc = CONCAT(IFNULL(SQLCODE, '') , '|' , Ifnull(Sqlerrm, ''));
  Set Pcode ='99';
  SET P_Msg ='N';
END;
  insert
  into categories
    (
      category_name ,
      category_description,
      created_by
    )
    values
    (
      p_category_name,
      p_category_description,
      p_created_by
    );
  COMMIT;
  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ADD_CURRENCY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ADD_CURRENCY`(IN `P_CURRENCY_NAME` VARCHAR(4000), IN `P_CURRENCY_VALUE` VARCHAR(4000), IN `P_YEAR` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

declare v_count int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  select count(*) into v_count from currency where name = P_CURRENCY_NAME and year = P_YEAR;
  
  if v_count = 0
	then
			
		  INSERT INTO currency
		  (
			NAME,
			VALUE,
			YEAR
		  )
		  VALUES
		  (
			P_CURRENCY_NAME,
			P_CURRENCY_VALUE,
			P_YEAR
		  );
			
		  COMMIT;
		  
		  
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';
	else
    
    SET pCODE ='-1';
		  SET pDESC = 'Already exists';
		  SET p_MSG ='Already exists';
          
	end if;
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Add_INDICATOR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Add_INDICATOR`(IN `P_Indicator_Name` VARCHAR(4000), IN `P_INDICATOR_DESCRIPTION` VARCHAR(4000), IN `P_Created_By` VARCHAR(4000), OUT `Pcode` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  Set Pdesc = CONCAT(IFNULL(SQLCODE, '') , '|' , Ifnull(Sqlerrm, ''));
  Set Pcode ='99';
  SET P_Msg ='N';
END;
  insert
  into indicators
    (
      indicator_name ,
      indicator_description,
      created_by
    )
    values
    (
      p_indicator_name,
      p_indicator_description,
      p_created_by
    );
  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Add_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Add_ORGANIZATION`(
IN `P_ORGANIZATION_NAME` VARCHAR(250), 
IN `P_Organization_Description` VARCHAR(300), 
IN `P_ORG_ABV` VARCHAR(20), 
IN `P_Organization_Country` VARCHAR(150), 
IN `P_ORGANIZATION_type` INT, 
IN `P_Created_By` VARCHAR(40), 
OUT `Pcode` VARCHAR(10), OUT `Pdesc` VARCHAR(500), OUT `P_Msg` VARCHAR(1000))
BEGIN


DECLARE V_COUNT INT;

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;


	  Set Pcode='-1';
	  Set Pdesc='ALREADY EXIST';
	  SET P_Msg='ALREADY EXISTS';


	select exists(select * from organization where organization_name = p_organization_name) into V_COUNT;

IF V_COUNT = 0
	THEN
    

	  insert
	  into organization
		(
		  organization_name ,
		  organization_description,
		  ORG_ABV,
		  organization_country,
		  created_by,
		  otid
		)
		values
		(
		  p_organization_name,
		  p_organization_description,
		  P_ORG_ABV,
		  p_organization_country,
		  p_created_by,
		  p_organization_type
		);
	  Set Pcode='00';
	  Set Pdesc='success';
	  SET P_Msg='Y';
END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Add_PMR_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Add_PMR_ORGANIZATION`(
IN `P_ORGANIZATION_NAME` VARCHAR(250), 
IN `P_Organization_Description` VARCHAR(250), 
IN `P_ORG_ABV` VARCHAR(20), 
IN `P_Organization_Country` VARCHAR(150), 
IN `P_ORGANIZATION_type` INT, 
IN `P_Created_By` VARCHAR(40), 
OUT `Pcode` VARCHAR(10), OUT `Pdesc` VARCHAR(500), OUT `P_Msg` VARCHAR(10))
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  insert
  into pmr_organization
    (
      organization_name ,
      organization_description,
	  ORG_ABV,
      organization_country,
      created_by,
      otid
    )
    values
    (
      p_organization_name,
      p_organization_description,
      P_ORG_ABV,
      p_organization_country,
      p_created_by,
      p_organization_type
    );
  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ADD_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ADD_REGION`(IN `P_REGION_NAME` VARCHAR(4000), IN `P_PROVINCE` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'REGION ALREADY EXISTS';
  SET P_MSG = 'REGION ALREADY EXISTS';
  
  SELECT COUNT(*) INTO V_COUNT FROM REGIONS WHERE REGION_NAME = P_REGION_NAME;
  
  IF V_COUNT = 0
	THEN
		  
		  INSERT INTO REGIONS
		  (
			REGION_NAME,
			PARENT,
			TYPE
		  )
		  VALUES
		  (
			P_REGION_NAME,
			P_PROVINCE,
			'D'
		  );
			
		  COMMIT;
		  
		  
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';
	END IF;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ADD_USER` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ADD_USER`(IN `P_USER_NAME` VARCHAR(100), IN `P_PASSWORD` VARCHAR(100),IN `P_ACCESS` VARCHAR(100), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
		
        
        INSERT INTO USER (USERNAME,PASSWORD,ACCESS) VALUES(P_USER_NAME,P_PASSWORD,P_ACCESS);
        
        
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_CHECK_ORG` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_CHECK_ORG`(
 IN  P_ORG_ABV   VARCHAR(4000),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_COUNT DOUBLE;
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
	SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
        
    SELECT COUNT(*) INTO V_COUNT FROM organization WHERE ORG_ABV = P_ORG_ABV;
	
    IF V_COUNT = 0
    THEN
			SET PCODE = '-1';
			SET PDESC = 'Failure';
			SET PMSG  = 'N';
    END IF;
    
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_COUNTRY_INDICATORS_ADD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_COUNTRY_INDICATORS_ADD`(
IN PCategory VARCHAR(50),
IN PIndicator VARCHAR(50),
IN PIndicatorType VARCHAR(50),
IN PIndicator_Value DOUBLE,
IN PCREATED_BY VARCHAR(25),
IN FILE_ID	INT,
OUT PCODE                 VARCHAR(10),
OUT PDESC                   VARCHAR(500),
OUT PMSG                   VARCHAR(10)
	)
BEGIN

DECLARE V_CI_CID INT;
DECLARE V_CI_IID INT; 
DECLARE V_CI_ITID INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  Set Pdesc =  'Other Vendor related software error.';
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';


	select 	IC_ID INTO  V_CI_CID from MicroWatch_Category WHERE IC_NAME = PCategory;
	select 	MWI_ID INTO V_CI_IID from MicroWatch_indicators WHERE INDICATOR_NAME =PIndicator AND  CATEGORY_ID = V_CI_CID;
	SELECT MWIT_ID INTO V_CI_ITID FROM MicroWatch_Indicator_Types WHERE IT_NAME = PIndicatorType  AND indicator_ID= V_CI_IID ;




	  INSERT INTO Country_Indicators( CI_CID,
																CI_IID,
																CI_ITID,
																Indicator_Value,
																CREATED_BY,
																file_id)
												 VALUE(  V_CI_CID,
																 V_CI_IID,
																 V_CI_ITID,
																 PIndicator_Value,
																PCREATED_BY,
																FILE_ID);
									

  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Data_Transfer` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Data_Transfer`(IN `P_File_ID` int(255),OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
	
  
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET pCODE ='99';
GET DIAGNOSTICS CONDITION 1 pCODE = RETURNED_SQLSTATE, 
  PDESC = MESSAGE_TEXT;
 
  SET P_MSG ='N';
END;
	
    INSERT INTO `microfi3_aksa_1`.`pmr_data` (`OID`,
												`CID`,
												`IID`,
												`VALUE`,
												`CREATED_BY`,
												`YEAR`)
														SELECT  O.OID,
														I.CATEGORY_ID, 
														I.IID, 
														PDD.VALUE, 
														'PUSER_NAME', 
														PFD.YEAR
														   FROM indicators I,
														pmr_dct_data PDD,
														pmr_file_details PFD,
														pmr_organization O
														WHERE I.SUB_CAT_TYPE_ID = PDD.SUB_CAT_TYPE_ID
														  AND PDD.FILE_ID = PFD.FILE_ID
														  AND PFD.ORG_NAME = O.ORG_ABV
														  AND PFD.FILE_ID = P_File_ID; 
                                                          
		  SET Pcode='00';
		  SET Pdesc='success';
		  SET P_Msg='Y';
		
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Data_Upload` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Data_Upload`(IN `P_Catagory` VARCHAR(4000), IN `P_Indicator` VARCHAR(4000), IN `P_Organization` VARCHAR(4000), IN `P_VALUE` VARCHAR(4000), IN `p_year` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
  DECLARE Vcid  DOUBLE;
  DECLARE Viid  DOUBLE;
  DECLARE VVOID DOUBLE;
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET pCODE ='99';
GET DIAGNOSTICS CONDITION 1 pCODE = RETURNED_SQLSTATE, 
  PDESC = MESSAGE_TEXT;
 
  SET P_MSG ='N';
END;
  select cid into vcid from categories where upper(category_name) = upper(p_catagory);
  select iid into viid from indicators where upper(indicator_name) = upper(p_indicator);
  select oid
  into vvoid
  from organization
  where organization_name = p_organization;
  insert
  into pmr_data
    (
      cid,
      iid,
      oid,
      value,
      year
    )
    values
    (
      vcid,
      viid,
      vvoid,
      p_value,
      convert(p_year,signed integer)
    );
  SET Pcode='00';
  SET Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_Data_Upload_DCT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_Data_Upload_DCT`(IN `P_Catagory` VARCHAR(4000), IN `P_Sub_Category` VARCHAR(4000), IN `P_Sub_Category_Type` VARCHAR(4000), IN `P_Organization` VARCHAR(4000), IN `P_VALUE` VARCHAR(4000), IN `p_year` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
	DECLARE c_id	int; -- category id
	DECLARE s_c_id	int; -- sub category id
	DECLARE s_c_t_id int; -- sub category type id 
    DECLARE f_id int; -- file id
    
    DECLARE V_CHECK int;
  
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET pCODE ='99';
GET DIAGNOSTICS CONDITION 1 pCODE = RETURNED_SQLSTATE, 
  PDESC = MESSAGE_TEXT;
 
  SET P_MSG ='N';
END;

	  select file_id into f_id from pmr_file_details where upper(ORG_NAME) = upper(P_Organization) and YEAR = p_year;
	  select cid into c_id from pmr_categories where upper(CATEGORY_NAME) = upper(p_catagory);
	  select cid into s_c_id from pmr_sub_categories_1 where upper(CATEGORY_NAME) = upper(P_Sub_Category) and CATEGORY_ID = c_id;
	  select cid into s_c_t_id from pmr_sub_category_type where upper(CATEGORY_TYPE_NAME) = upper(P_Sub_Category_Type) and SUB_CATEGORY_1_ID = s_c_id;
	  
	  SELECT exists(select * FROM pmr_dct_data WHERE FILE_ID = f_id AND CAT_ID = c_id AND SUB_CAT_ID = s_c_id AND SUB_CAT_TYPE_ID = s_c_t_id) into V_CHECK;
		
        
	  IF V_CHECK = 0
	  THEN
		  
		  insert
		  into pmr_dct_data
			(
			  CAT_ID,
			  SUB_CAT_ID,
			  SUB_CAT_TYPE_ID,
			  value,
			  FILE_ID
			)
			values
			(
			  c_id,
			  s_c_id,
			  s_c_t_id,
			  p_value,
			  f_id
			);
    
		  SET Pcode='00';
		  SET Pdesc='success';
		  SET P_Msg='Y';
	  
	  
	  ELSE
	   SET Pcode =  '01';
	   SET Pdesc  =  'Already exist.';
	   SET P_Msg   =  'Y';
	  
      END IF ;
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_DELETE_CURRENCY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_DELETE_CURRENCY`(IN `P_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM currency where c_id = P_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_DELETE_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_DELETE_REGION`(IN `P_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM REGIONS where R_ID = P_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_del_Categorie` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_del_Categorie`(IN `P_CID` DOUBLE, IN `updated_by` VARCHAR(4000), OUT `Pcode` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_Msg` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update categories
  set ended_by=updated_by ,
    ended     =current_timestamp
  where cid   =p_cid;
  SET pCODE      ='00';
  SET pDESC      = 'Sucess';
  SET p_MSG      ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_del_INDICATOR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_del_INDICATOR`(IN `P_IID` DOUBLE, IN `updated_by` VARCHAR(4000), OUT `Pcode` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update indicators
  set ended_by=updated_by ,
    ended     =current_timestamp
  where iid   =p_iid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_del_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_del_ORGANIZATION`(IN `P_oID` DOUBLE, IN `updated_by` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
declare V_ID int;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  GET DIAGNOSTICS CONDITION 1
		 pDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET pCODE = '99';
   SET p_MSG  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  -- update organization
  -- set ended_by=updated_by ,
  --  ended     =current_timestamp
  -- where oid   =p_oid;
  
    
    delete from country_indicators where file_id in (select file_id from file_details where ORG_NAME = (select org_abv from organization where oid = p_oid));
    delete from regional_indicators where file_id in (select file_id from file_details where ORG_NAME = (select org_abv from organization where oid = p_oid));
    delete from FILE_DETAILS where ORG_NAME = (select org_abv from organization where oid = p_oid);
	delete from organization where oid = p_oid;
  
  SET pCODE      ='00';
  SET pDESC      = 'Sucess';
  SET p_MSG      ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `Prc_del_PMR_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `Prc_del_PMR_ORGANIZATION`(IN `P_oID` DOUBLE, IN `updated_by` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update pmr_organization
  set ended_by=updated_by ,
    ended     =current_timestamp
  where oid   =p_oid;
  SET pCODE      ='00';
  SET pDESC      = 'Sucess';
  SET p_MSG      ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_DEL_USER` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_DEL_USER`(IN `P_ID` INT, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
		
        
       DELETE FROM USER WHERE U_ID = P_ID;
        
        
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_CATEGORIES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_CATEGORIES`(IN `P_CID` DOUBLE, IN `P_CATEGORY_NAME` VARCHAR(4000), IN `P_CATEGORY_DESCRIPTION` VARCHAR(4000), IN `p_updated_by` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update categories
  set category_name     =p_category_name,
    category_description=p_category_description,
    updated_by          =p_updated_by,
    updated             =current_timestamp
  where cid             =p_cid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_CURRENCY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_CURRENCY`(IN `P_CURRENCY_ID` INT ,IN `P_CURRENCY_NAME` VARCHAR(4000), IN `P_CURRENCY_VALUE` VARCHAR(4000), IN `P_YEAR` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  update currency
  set
	NAME     =	P_CURRENCY_NAME,
    VALUE	 = 	P_CURRENCY_VALUE,
    YEAR     =	P_YEAR
  where
	c_id      =	P_CURRENCY_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_INDICATORS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_INDICATORS`(IN `P_IID` DOUBLE, IN `P_INDICATOR_NAME` VARCHAR(4000), IN `P_INDICATOR_DESCRIPTION` VARCHAR(4000), IN `p_updated_by` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update indicators
  set indicator_name     =p_indicator_name,
    indicator_description=p_indicator_description,
    updated_by           =p_updated_by,
    updated              =current_timestamp
  where iid              =p_iid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_ORGANIZATION`(IN `P_OID` DOUBLE, IN `P_ORGANIZATION_NAME` VARCHAR(4000), 
IN `P_ORGANIZATION_DESCRIPTION` VARCHAR(4000), 
IN `P_ORGANIZATION_COUNTRY` VARCHAR(4000), 
IN `P_ORG_ABV` VARCHAR(4000), IN `P_ORGANIZATION_type` VARCHAR(4000), IN `p_updated_by` VARCHAR(4000), 
OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `p_MSG` VARCHAR(4000))
BEGIN
  DECLARE v_otid VARCHAR(250);
 
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
 end;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select otid
  into v_otid
  from organization_type
  where organization_type=p_organization_type;
  update organization
  set organization_name     =p_organization_name,
    organization_description=p_organization_description,
    organization_country    =p_organization_country,
    ORG_ABV     =p_ORG_ABV,
    otid                    =v_otid,
    updated_by              =p_updated_by,
    updated                 =current_timestamp
  where oid                 =p_oid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_PMR_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_PMR_ORGANIZATION`(IN `P_OID` DOUBLE, IN `P_ORGANIZATION_NAME` VARCHAR(4000), 
IN `P_ORGANIZATION_DESCRIPTION` VARCHAR(4000), 
IN `P_ORGANIZATION_COUNTRY` VARCHAR(4000), 
IN `P_ORG_ABV` VARCHAR(4000), IN `P_ORGANIZATION_type` VARCHAR(4000), IN `p_updated_by` VARCHAR(4000), 
OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `p_MSG` VARCHAR(4000))
BEGIN
  DECLARE v_otid VARCHAR(250);
 
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
 end;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select otid
  into v_otid
  from organization_type
  where organization_type=p_organization_type;
  update pmr_organization
  set organization_name     =p_organization_name,
    organization_description=p_organization_description,
    organization_country    =p_organization_country,
    ORG_ABV     =p_ORG_ABV,
    otid                    =v_otid,
    updated_by              =p_updated_by,
    updated                 =current_timestamp
  where oid                 =p_oid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_REGION`(IN `P_REGION_ID` INT ,IN `P_REGION_NAME` VARCHAR(4000), IN `P_REGION_PARENT` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  update REGIONS
  set
	REGION_NAME     =	P_REGION_NAME,
    PARENT			= 	P_REGION_PARENT
  where
	R_ID      =	P_REGION_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_EDIT_USER` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_EDIT_USER`(IN `P_ID` INT, IN `P_USER_NAME` VARCHAR(100), IN `P_PASSWORD` VARCHAR(100), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
		
        
        UPDATE USER SET
			USERNAME = P_USER_NAME,
		    PASSWORD = P_PASSWORD
            
		WHERE U_ID = P_ID;
        
        
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_FILE_DETAILS_ADD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_FILE_DETAILS_ADD`(IN PFILE_NAME   VARCHAR(100),
                                  IN PPATH        VARCHAR(200),
                                  IN PTOTAL_LINES DOUBLE,
                                  IN PSIZE_BYTE   VARCHAR(50),
                                  IN PLOGIN_ID    VARCHAR(20),
								  IN PORG_NAME VARCHAR(150),
								  IN PYEAR VARCHAR(15),
								  IN PQUARTER VARCHAR(15),
                                  OUT PFILE_ID    int ,
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_FILE_ID int;
DECLARE V_COUNT int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  Set Pdesc =  'Other Vendor related software error.';
   SET PMSG  =  'N';
end;

	SELECT COUNT(*) INTO V_COUNT FROM FILE_DETAILS WHERE FILE_NAME = PFILE_NAME AND YEAR = PYEAR AND QUARTER = PQUARTER;

	IF V_COUNT = 0
	THEN

	SELECT (IFNULL(MAX(FILE_ID),0) +1) INTO V_FILE_ID FROM FILE_DETAILS;
    INSERT
          INTO FILE_DETAILS( FILE_ID,
                              FILE_NAME,
                              PATH,
                              TOTAL_LINES,
                              SIZE_BYTE,
                              CREATED_BY,
							  ORG_NAME,
							  YEAR,
							   QUARTER)
                        VALUES( V_FILE_ID,
                                PFILE_NAME,
                                PPATH,
                                PTOTAL_LINES,
                                PSIZE_BYTE,
                                PLOGIN_ID,
								PORG_NAME,
								PYEAR,
								PQUARTER);
                          COMMIT;
	SET PFILE_ID = V_FILE_ID;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';
	
	ELSE
   SET PCODE =  '01';
   SET PDESC  =  'Already exist.';
   SET PMSG   =  'Y';
	
	END IF ;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_FILE_DETAILS_CHECK` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_FILE_DETAILS_CHECK`(
								  IN PYEAR VARCHAR(15),
								  IN PQUARTER VARCHAR(15),
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(1000),
                                  OUT PMSG        VARCHAR(10))
BEGIN

DECLARE V_COUNT int;
DECLARE ERROR_MSG VARCHAR(2000);
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;

	SELECT COUNT(F.FILE_ID)
         INTO V_COUNT 
	   FROM FILE_DETAILS F, organization_type OT, organization O
    WHERE  F.ORG_NAME =O.ORG_ABV
          AND O.OTID = OT.otid
	      AND YEAR = PYEAR 
          AND QUARTER = PQUARTER
          AND OT.ORGANIZATION_TYPE = 'MFI';

	IF V_COUNT = 0
	THEN
		SET ERROR_MSG = 'MFI';
	END IF;

	SELECT COUNT(F.FILE_ID)
         INTO V_COUNT 
	   FROM FILE_DETAILS F, organization_type OT, organization O
    WHERE  F.ORG_NAME =O.ORG_ABV
          AND O.OTID = OT.otid
	      AND YEAR = PYEAR 
          AND QUARTER = PQUARTER
          AND OT.ORGANIZATION_TYPE = 'MFB';

			IF V_COUNT = 0
			THEN
				IF ERROR_MSG IS NOT NULL
				THEN
					SET ERROR_MSG =  CONCAT(ERROR_MSG , ', ' , 'MFB');
				ELSE
					SET ERROR_MSG = 'MFB';
				END IF;
			END IF;

	SELECT COUNT(F.FILE_ID)
         INTO V_COUNT 
	   FROM FILE_DETAILS F, organization_type OT, organization O
    WHERE  F.ORG_NAME =O.ORG_ABV
          AND O.OTID = OT.otid
	      AND YEAR = PYEAR 
          AND QUARTER = PQUARTER
          AND OT.ORGANIZATION_TYPE = 'RSP';

			IF V_COUNT = 0
			THEN
				IF ERROR_MSG IS NOT NULL
				THEN
					SET ERROR_MSG =  CONCAT(ERROR_MSG , ', ' , 'RSP');
				ELSE
					SET ERROR_MSG = 'RSP';
				END IF;
			END IF;

	SELECT COUNT(F.FILE_ID)
         INTO V_COUNT 
	   FROM FILE_DETAILS F, organization_type OT, organization O
    WHERE  F.ORG_NAME =O.ORG_ABV
          AND O.OTID = OT.otid
	      AND YEAR = PYEAR 
          AND QUARTER = PQUARTER
          AND OT.ORGANIZATION_TYPE = 'Other MFPs';

			IF V_COUNT = 0
			THEN
				IF ERROR_MSG IS NOT NULL
				THEN
					SET ERROR_MSG =  CONCAT(ERROR_MSG , ', ' , 'Other MFPs');
				ELSE
					SET ERROR_MSG = 'Other MFPs';
				END IF;
			END IF;

	IF ERROR_MSG IS NULL
	THEN 
		
	   SET PCODE =  '00';
	   SET PDESC  =  'Success';
	   SET PMSG   =  'Y';
	
	ELSE
	   SET PCODE =  '01';
	   SET PDESC  =CONCAT(ERROR_MSG,' orginaization files not avialable.');
	   SET PMSG   =  'N';
	END IF ;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_FILE_DETAILS_DEL` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_FILE_DETAILS_DEL`(
IN	PFILE_NAME  VARCHAR(100),
IN  PDELETE_BY 	VARCHAR(45),
OUT PCODE       VARCHAR(10),
OUT PDESC      	VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE ID INT;
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 
 
 BEGIN
 
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
INSERT INTO `FILE_DETAILS_DEL`
(`FILE_ID`,
`FILE_NAME`,
`PATH`,
`TOTAL_LINES`,
`SIZE_BYTE`,
`CREATED`,
`CREATED_BY`,
`ORG_NAME`,
`YEAR`,
`QUARTER`,
DELETE_BY)

SELECT 
	FILE_ID,
	FILE_NAME,
	PATH,
	TOTAL_LINES,
	SIZE_BYTE,
	CREATED,
	CREATED_BY,
	ORG_NAME,
	YEAR,
	QUARTER,
	PDELETE_BY
FROM 
	FILE_DETAILS
WHERE
	FILE_NAME = PFILE_NAME;

SELECT FILE_ID into ID FROM FILE_DETAILS WHERE FILE_NAME = PFILE_NAME;

DELETE FROM  country_indicators WHERE FILE_ID = ID;
DELETE FROM  regional_indicators WHERE FILE_ID = ID;
DELETE FROM  FILE_DETAILS WHERE FILE_ID = ID;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_FILE_DETAILS_GET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_FILE_DETAILS_GET`(IN PFILE_NAME   VARCHAR(100),
																 						 IN PLOGIN_ID    	VARCHAR(20),
																	 					 IN PORG_NAME	VARCHAR(150),
																		 				 IN PYEAR 				VARCHAR(15),
																			 			 IN PQUARTER 		VARCHAR(15),
																				 		OUT PCODE       	VARCHAR(10),
																					 	OUT PDESC       	VARCHAR(500),
																						OUT PMSG        	VARCHAR(10))
BEGIN

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;

	SELECT FILE_ID,
                    FILE_NAME,
                    PATH,
                    TOTAL_LINES,
                    SIZE_BYTE,
					CREATED,
                    CREATED_BY,
   				    ORG_NAME,
					YEAR,
					QUARTER
		FROM FILE_DETAILS
      WHERE FILE_NAME = COALESCE(PFILE_NAME,FILE_NAME)
             AND ORG_NAME = COALESCE(PORG_NAME,ORG_NAME)
					AND YEAR = COALESCE(PYEAR,YEAR)
					AND QUARTER=COALESCE(PQUARTER,QUARTER) ;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_CATEGORIES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_CATEGORIES`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET p_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select cid,
  category_name,
  category_description,
  created,
  created_by from categories where ended is null;
  SET pCODE                                  ='00';
  SET pDESC                                  = 'Sucess';
  SET p_MSG                                  ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_CAT_INDICATORS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_CAT_INDICATORS`(IN `P_CAT` VARCHAR(4000),OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE c_id int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  select cid into c_id from categories where CATEGORY_NAME = P_CAT;
  
  select *  from indicators where CATEGORY_ID = c_id;
  
  SET pCODE                                  ='00';
  SET pDESC                                  = 'Sucess';
  SET p_MSG                                  ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_COUNTRY`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';

  select distinct organization_country from organization where ended is null;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `prc_get_currency` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `prc_get_currency`(
 OUT `Pcode` VARCHAR(4000),
 OUT `Pdesc` VARCHAR(4000),
 OUT `P_Msg` VARCHAR(4000))
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  select * from currency;
  
  
  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_EXCEL_TOTAL` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_EXCEL_TOTAL`(
 IN     P_CATEGORY_NAME VARCHAR(4000),
 IN     P_ORG_TYPE VARCHAR(4000),
 IN     P_YEAR VARCHAR(4000),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN

DECLARE v_CAT_ID VARCHAR(400);

DECLARE v_IID VARCHAR(400);

DECLARE v_ratio double; 
 
 
DECLARE P_YEAR_PREV INT;
DECLARE V_VAL_6 VARCHAR(4000);
DECLARE V_VAL_5 VARCHAR(4000);
DECLARE V_VAL_4 VARCHAR(4000);
DECLARE V_VAL_3 VARCHAR(4000);
DECLARE V_VAL_2 VARCHAR(4000);
DECLARE V_VAL_1 VARCHAR(4000);
 
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
	
    SET P_YEAR_PREV = P_YEAR - 1;
    
    
    IF upper(P_ORG_TYPE) = upper('total')
    then
		
		 select cid into v_CAT_ID from categories where UPPER(CATEGORY_NAME) = UPPER(P_CATEGORY_NAME);
		 select I.IID, I.indicator_name, 
		   sum(D.value) as Total 
		   from indicators I 
		   Left join pmr_data D 
		   on (I.IID = D.IID 
		   and D.YEAR = P_YEAR
		   and D.IID in (select iid from indicators where AVG_CAL_CHECK = 'false') )
		   where I.CATEGORY_ID = v_CAT_ID
           and I.IID in (select iid from indicators where AVG_CAL_CHECK = 'false') 
           group by I.IID;			

				
		truncate total_temp;
     
     
     
		IF upper(P_CATEGORY_NAME) = upper('financing structure')
		then
			-- FOR EQUITY TO ASSET RATIO
    
				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('total equity');
				
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Equity-to-asset ratio', v_ratio);
				
			-- FOR Commercial liabilities-to-total debt
					
					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('- Commercial debt ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('- Subsidized debt*');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Commercial liabilities-to-total debt', v_ratio);
					
					
			-- FOR Debt-to-equity ratio

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total debt');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total equity ');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Debt-to-equity ratio', v_ratio);


			-- FOR Deposits-to-gross loan portfolio

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total Deposits ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Deposits-to-gross loan portfolio', v_ratio);


			-- FOR Deposits-to-total assets

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total Deposits ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total assets ');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Deposits-to-total assets', v_ratio);



			-- FOR Cost of funds OF FINANCIAL STRUCTURE
						
						SET V_VAL_1 = 0;
						SET V_VAL_2 = 0;
						SET V_VAL_3 = 0;
						
						select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_1 from pmr_data where cid =  5 and iid =  V_IID and YEAR = P_YEAR;

						select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from other sources' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_2 from pmr_data where  cid =  5 and iid =  V_IID and YEAR = P_YEAR;

						select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross financial margin' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_3 from pmr_data where cid =  5 and iid =  V_IID and YEAR = P_YEAR;

						select ( V_VAL_1 /  (V_VAL_2 + V_VAL_3)) * 100 into  v_ratio; 

						insert into total_temp (indicator_name,value) values('Cost of funds', v_ratio);
					



			-- FOR Gross loan portfolio-to-total assets
					
					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total assets');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Gross loan portfolio-to-total assets', v_ratio);
		
        elseif upper(P_CATEGORY_NAME) = upper('outreach')
			then
				-- FOR Proportion of active women borrowers (%)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active women borrowers');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active borrowers');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Proportion of active women borrowers (%)', v_ratio);


				-- FOR Average loan balance per active borrower (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active borrowers');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average loan balance per active borrower (PKR)', v_ratio);


				-- FOR Average loan balance per active borrower/per capita income

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Average loan balance per active borrower (PKR)');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average loan balance per active borrower/per capita income', v_ratio);



				-- FOR Average outstanding loan balance (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of loans outstanding');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average outstanding loan balance (PKR)', v_ratio);


				-- FOR Average outstanding loan balance / per capita income

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Average outstanding loan balance (PKR)');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average outstanding loan balance / per capita income', v_ratio);

				-- FOR Average saving balance per active depositor (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Deposits outstanding');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Depositors');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average saving balance per active depositor (PKR)', v_ratio);




				-- FOR Active deposit account balance (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Deposits outstanding');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of deposit accounts');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_iid and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Active deposit account balance (PKR)', v_ratio);
			
            
		elseif upper(P_CATEGORY_NAME) = upper('financial performance')
            then
            -- FOR Adjusted return-on-assets

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Net Income/(Loss) After Adjustments');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted return-on-assets', v_ratio);

			
		-- FOR Adjusted return-on-equity


				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Net Income/(Loss) After Adjustments');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Average total equity');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted return-on-equity', v_ratio);


		-- FOR Financial expense ratio

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
				select ifnull(sum(D.value),0) into v_val_3 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR_PREV;
				
				select ( V_VAL_1 / ( V_VAL_2 -  V_VAL_3) ) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Financial expense ratio', v_ratio);


		-- FOR Operational self sufficiency (OSS)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: loan loss provision expense');
				
				select sum(D.value) into v_val_3 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: operating expense');
				
				select sum(D.value) into v_val_4 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Other Non operating expense');
				
				select sum(D.value) into v_val_5 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5) ) * 100 into  v_ratio; 
				
				insert into total_temp (indicator_name,value) values('Operational self sufficiency (OSS)', v_ratio);


		-- FOR Financial self sufficiency (FSS)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: loan loss provision expense');
				
				select sum(D.value) into v_val_3 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: operating expense');
				
				select sum(D.value) into v_val_4 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Other Non operating expense');
				
				select sum(D.value) into v_val_5 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total Adjustment Expense');
				
				select sum(D.value) into v_val_6 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5 +  V_VAL_6) ) * 100 into  v_ratio;    
				
				insert into total_temp (indicator_name,value) values('Financial self sufficiency (FSS)', v_ratio);


                
			elseif upper(P_CATEGORY_NAME) = upper('Operating Income')
            then
            
-- calculating TOTALS for Operating Income

		-- FOR Total revenue ratio (total revenue-to-average total assets)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted net operating income / (loss)');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Total revenue ratio (total revenue-to-average total assets)', v_ratio);



		-- FOR Adjusted profit margin (adjusted profit/(loss)-to-total revenue)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted net operating income / (loss)');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted profit margin (adjusted profit/(loss)-to-total revenue)', v_ratio);



		-- FOR Yield on gross portfolio (nominal)


				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Revenue from loan portfolio');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Yield on gross portfolio (nominal)', v_ratio);            
            
            elseif upper(P_CATEGORY_NAME) = upper('Operating expense')
            then
            -- FOR Adjusted total expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted total expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted total expense-to-average total assets', v_ratio);


				-- FOR Adjusted financial expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted financial expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted financial expense-to-average total assets', v_ratio);


				-- FOR Adjusted loan loss provision expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted loan loss provision expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted loan loss provision expense-to-average total assets', v_ratio);


				-- FOR Adjusted operating expense-to-average total assets


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted operating expense-to-average total assets', v_ratio);

				-- FOR Adjusted personnel expense


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted personnel expense', v_ratio);
                        
				-- FOR Adjusted admin expense


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Admin expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID  and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted admin expense', v_ratio);


				-- FOR Adjustment expense-to-average total assets


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjustment expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjustment expense-to-average total assets', v_ratio);


				elseif upper(P_CATEGORY_NAME) = upper('Efficiency')
				then
            
					-- FOR Adjusted operating expense-to-average gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio (PKR 000)');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Adjusted operating expense-to-average gross loan portfolio', v_ratio);


					-- FOR Adjusted personnel expense-to-average gross loan portfolio


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio (PKR 000)');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Adjusted personnel expense-to-average gross loan portfolio', v_ratio);


					-- FOR Average salary/gross domestic product per capita


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Infrastructure')) and UPPER(INDICATOR_NAME) = UPPER('Personnel');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
							
							select sum(D.value) into V_VAL_3 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select (( V_VAL_1 /  V_VAL_2) / ( v_val_3) )* 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Average salary/gross domestic product per capita', v_ratio);


					-- FOR Adjusted cost per borrower (PKR)

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average number of active borrowers');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Adjusted cost per borrower (PKR)', v_ratio);
							
					-- FOR Adjusted cost per loan (PKR)
							
							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average number of active loans');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Adjusted cost per loan (PKR)', v_ratio);

            elseif upper(P_CATEGORY_NAME) = upper('Productivity')
			then
            
            
									-- FOR Borrowers per staff
								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active borrowers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Borrowers per staff', v_ratio);
								

						-- FOR Loans per staff
								
								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active loans');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Loans per staff', v_ratio);
								
						-- FOR Borrowers per loan officer

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active borrowers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Borrowers per loan officer', v_ratio);



						-- FOR Loans per loan officer

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active loans');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Loans per loan officer', v_ratio);



						-- FOR Depositors per staff

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active depositors');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Depositors per staff', v_ratio);


						-- FOR Deposit accounts per staff


								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of deposit accounts');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Deposit accounts per staff', v_ratio);


						-- FOR Personnel allocation ratio

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Personnel allocation ratio', v_ratio);


				elseif upper(P_CATEGORY_NAME) = upper('Risk')
				then
				
									
									-- FOR Portfolio at risk (>30)-to-gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 30 days');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio ');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Portfolio at risk (>30)-to-gross loan portfolio', v_ratio);

					-- FOR Portfolio at risk(>90)-to-gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 90 days');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio ');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Portfolio at risk(>90)-to-gross loan portfolio', v_ratio);


					-- FOR Average gross loan portfolio

						SET V_VAL_1 = 0;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating Income')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolios');
							
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
						
						insert into total_temp (indicator_name,value) values('Average gross loan portfolio', V_VAL_2);




					-- FOR Write off-to-average gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Loan Portfolio written off during year ');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Write off-to-average gross loan portfolio', v_ratio);




					-- FOR Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Loan loss reserve (balance sheet)');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 30 days');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)', v_ratio);
                
            
            
            
		end if;
				
				
				
				
				
				
		 
	else
		 select cid into v_CAT_ID from categories where UPPER(CATEGORY_NAME) = UPPER(P_CATEGORY_NAME);
		 select I.IID, I.indicator_name, 
		   sum(D.value) as Total 
		   from indicators I 
		   Left join pmr_data D 
		   on (I.IID = D.IID 
		   and D.YEAR = P_YEAR
		   and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE))
		   and D.IID in (select iid from indicators where AVG_CAL_CHECK = 'false') )
		   where I.CATEGORY_ID = v_CAT_ID
           and I.IID in (select iid from indicators where AVG_CAL_CHECK = 'false') 
           group by I.IID;
        
        
     truncate total_temp;
     
     
     
		IF upper(P_CATEGORY_NAME) = upper('financing structure')
		then
			-- FOR EQUITY TO ASSET RATIO
    
				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('total equity');
				
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Equity-to-asset ratio', v_ratio);
				
			-- FOR Commercial liabilities-to-total debt
					
					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('- Commercial debt ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('- Subsidized debt*');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Commercial liabilities-to-total debt', v_ratio);
					
					
			-- FOR Debt-to-equity ratio

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total debt');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total equity ');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Debt-to-equity ratio', v_ratio);


			-- FOR Deposits-to-gross loan portfolio

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total Deposits ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Deposits-to-gross loan portfolio', v_ratio);


			-- FOR Deposits-to-total assets

					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total Deposits ');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total assets ');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Deposits-to-total assets', v_ratio);
                    
                    
			-- FOR Cost of funds OF FINANCIAL STRUCTURE
						
						SET V_VAL_1 = 0;
						SET V_VAL_2 = 0;
						SET V_VAL_3 = 0;
						
						select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_1 from pmr_data where cid =  5 and iid =  V_IID and YEAR = P_YEAR and OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE));

						select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from other sources' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_2 from pmr_data where  cid =  5 and iid =  V_IID and YEAR = P_YEAR and OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE));

						select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross financial margin' and CATEGORY_ID =  5;
						select sum(value) into  V_VAL_3 from pmr_data where cid =  5 and iid =  V_IID and YEAR = P_YEAR and OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE));

						select ( V_VAL_1 /  (V_VAL_2 + V_VAL_3)) * 100 into  v_ratio; 

						insert into total_temp (indicator_name,value) values('Cost of funds', v_ratio);


			-- FOR Gross loan portfolio-to-total assets
					
					SET V_VAL_1 = 0;
					SET V_VAL_2 = 1;
					
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
					select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Total assets');
					
					select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
					
					select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
					
					insert into total_temp (indicator_name,value) values('Gross loan portfolio-to-total assets', v_ratio);
		
        elseif upper(P_CATEGORY_NAME) = upper('outreach')
			then
				-- FOR Proportion of active women borrowers (%)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active women borrowers');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active borrowers');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Proportion of active women borrowers (%)', v_ratio);


				-- FOR Average loan balance per active borrower (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Active borrowers');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average loan balance per active borrower (PKR)', v_ratio);


				-- FOR Average loan balance per active borrower/per capita income

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Average loan balance per active borrower (PKR)');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average loan balance per active borrower/per capita income', v_ratio);



				-- FOR Average outstanding loan balance (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of loans outstanding');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average outstanding loan balance (PKR)', v_ratio);


				-- FOR Average outstanding loan balance / per capita income

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Average outstanding loan balance (PKR)');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average outstanding loan balance / per capita income', v_ratio);

				-- FOR Average saving balance per active depositor (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Deposits outstanding');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Depositors');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Average saving balance per active depositor (PKR)', v_ratio);




				-- FOR Active deposit account balance (PKR)

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Deposits outstanding');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_iid from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of deposit accounts');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into v_ratio;
						
						insert into total_temp (indicator_name,value) values('Active deposit account balance (PKR)', v_ratio);
			
            
		elseif upper(P_CATEGORY_NAME) = upper('financial performance')
            then
            -- FOR Adjusted return-on-assets

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Net Income/(Loss) After Adjustments');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted return-on-assets', v_ratio);

			
		-- FOR Adjusted return-on-equity


				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Net Income/(Loss) After Adjustments');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Average total equity');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted return-on-equity', v_ratio);


		-- FOR Financial expense ratio

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financing structure')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio');
				
				select ifnull(sum(D.value),0) into v_val_3 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR_PREV;
				
				select ( V_VAL_1 / ( V_VAL_2 -  V_VAL_3) ) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Financial expense ratio', v_ratio);


		-- FOR Operational self sufficiency (OSS)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: loan loss provision expense');
				
				select sum(D.value) into v_val_3 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: operating expense');
				
				select sum(D.value) into v_val_4 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Other Non operating expense');
				
				select sum(D.value) into v_val_5 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5) ) * 100 into  v_ratio; 
				
				insert into total_temp (indicator_name,value) values('Operational self sufficiency (OSS)', v_ratio);


		-- FOR Financial self sufficiency (FSS)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 0;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less : financial expense ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: loan loss provision expense');
				
				select sum(D.value) into v_val_3 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Less: operating expense');
				
				select sum(D.value) into v_val_4 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Other Non operating expense');
				
				select sum(D.value) into v_val_5 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Total Adjustment Expense');
				
				select sum(D.value) into v_val_6 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5 +  V_VAL_6) ) * 100 into  v_ratio;    
				
				insert into total_temp (indicator_name,value) values('Financial self sufficiency (FSS)', v_ratio);


                
			elseif upper(P_CATEGORY_NAME) = upper('Operating Income')
            then
            
-- calculating TOTALS for Operating Income

		-- FOR Total revenue ratio (total revenue-to-average total assets)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted net operating income / (loss)');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Total revenue ratio (total revenue-to-average total assets)', v_ratio);



		-- FOR Adjusted profit margin (adjusted profit/(loss)-to-total revenue)

				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted net operating income / (loss)');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Total revenue ');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Adjusted profit margin (adjusted profit/(loss)-to-total revenue)', v_ratio);



		-- FOR Yield on gross portfolio (nominal)


				SET V_VAL_1 = 0;
				SET V_VAL_2 = 1;
				
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Revenue from loan portfolio');
			
				select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating income')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio');
				
				select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
				
				select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
				
				insert into total_temp (indicator_name,value) values('Yield on gross portfolio (nominal)', v_ratio);            
            
            elseif upper(P_CATEGORY_NAME) = upper('Operating expense')
            then
            -- FOR Adjusted total expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted total expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted total expense-to-average total assets', v_ratio);


				-- FOR Adjusted financial expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted financial expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted financial expense-to-average total assets', v_ratio);


				-- FOR Adjusted loan loss provision expense-to-average total assets

						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjusted loan loss provision expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted loan loss provision expense-to-average total assets', v_ratio);


				-- FOR Adjusted operating expense-to-average total assets


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted operating expense-to-average total assets', v_ratio);

				-- FOR Adjusted personnel expense


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted personnel expense', v_ratio);
                        
                        
                -- FOR Adjusted admin expense


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('financial performance')) and UPPER(INDICATOR_NAME) = UPPER('Admin expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjusted admin expense', v_ratio);



				-- FOR Adjustment expense-to-average total assets


						SET V_VAL_1 = 0;
						SET V_VAL_2 = 1;
						
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Adjustment expense');
					
						select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating expense')) and UPPER(INDICATOR_NAME) = UPPER('Average total assets');
						
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
						
						insert into total_temp (indicator_name,value) values('Adjustment expense-to-average total assets', v_ratio);


				elseif upper(P_CATEGORY_NAME) = upper('Efficiency')
				then
            
								-- FOR Adjusted operating expense-to-average gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio (PKR 000)');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Adjusted operating expense-to-average gross loan portfolio', v_ratio);


					-- FOR Adjusted personnel expense-to-average gross loan portfolio


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio (PKR 000)');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Adjusted personnel expense-to-average gross loan portfolio', v_ratio);


					-- FOR Average salary/gross domestic product per capita


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Personnel expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Infrastructure')) and UPPER(INDICATOR_NAME) = UPPER('Personnel');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Outreach')) and UPPER(INDICATOR_NAME) = UPPER('Annual per capita income (PKR)*');
							
							select sum(D.value) into V_VAL_3 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select (( V_VAL_1 /  V_VAL_2) / ( v_val_3) )* 100 into  v_ratio;
							
							insert into total_temp (indicator_name,value) values('Average salary/gross domestic product per capita', v_ratio);


					-- FOR Adjusted cost per borrower (PKR)

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average number of active borrowers');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Adjusted cost per borrower (PKR)', v_ratio);
							
					-- FOR Adjusted cost per loan (PKR)
							
							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Operating expense');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Efficiency')) and UPPER(INDICATOR_NAME) = UPPER('Average number of active loans');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Adjusted cost per loan (PKR)', v_ratio);

            elseif upper(P_CATEGORY_NAME) = upper('Productivity')
			then
            
            
									-- FOR Borrowers per staff
								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active borrowers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2) into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Borrowers per staff', v_ratio);
								

						-- FOR Loans per staff
								
								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active loans');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2) into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Loans per staff', v_ratio);
								
						-- FOR Borrowers per loan officer

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active borrowers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2) into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Borrowers per loan officer', v_ratio);



						-- FOR Loans per loan officer

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active loans');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Loans per loan officer', v_ratio);



						-- FOR Depositors per staff

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Number of active depositors');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Depositors per staff', v_ratio);


						-- FOR Deposit accounts per staff


								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Outreach')) and UPPER(INDICATOR_NAME) = UPPER('Number of deposit accounts');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2)  into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Deposit accounts per staff', v_ratio);


						-- FOR Personnel allocation ratio

								SET V_VAL_1 = 0;
								SET V_VAL_2 = 1;
								
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total loan officers');
							
								select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Productivity')) and UPPER(INDICATOR_NAME) = UPPER('Total staff');
								
								select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
								
								select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
								
								insert into total_temp (indicator_name,value) values('Personnel allocation ratio', v_ratio);


				elseif upper(P_CATEGORY_NAME) = upper('Risk')
				then
				
									
									-- FOR Portfolio at risk (>30)-to-gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 30 days');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio ');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Portfolio at risk (>30)-to-gross loan portfolio', v_ratio);

					-- FOR Portfolio at risk(>90)-to-gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 90 days');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Gross loan portfolio ');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Portfolio at risk(>90)-to-gross loan portfolio', v_ratio);


					-- FOR Average gross loan portfolio

						SET V_VAL_1 = 0;
						
						select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Operating Income')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolios');
							
						select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
						
						insert into total_temp (indicator_name,value) values('Average gross loan portfolio', V_VAL_2);




					-- FOR Write off-to-average gross loan portfolio

							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Loan Portfolio written off during year ');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Average gross loan portfolio');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Write off-to-average gross loan portfolio', v_ratio);




					-- FOR Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)


							SET V_VAL_1 = 0;
							SET V_VAL_2 = 1;
							
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Loan loss reserve (balance sheet)');
						
							select sum(D.value) into V_VAL_1 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select iid into v_IID from indicators where CATEGORY_ID = (select cid from categories where upper(CATEGORY_NAME) = UPPER('Risk')) and UPPER(INDICATOR_NAME) = UPPER('Portfolio at risk > 30 days');
							
							select sum(D.value) into V_VAL_2 from pmr_data D where D.IID = v_IID and D.OID in (select oid from pmr_organization where OTID in (select otid from organization_type where ORGANIZATION_TYPE = P_ORG_TYPE)) and D.YEAR = P_YEAR;
							
							select ( V_VAL_1 /  V_VAL_2) * 100 into  v_ratio; 
							
							insert into total_temp (indicator_name,value) values('Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)', v_ratio);
                
            
            
            
		end if;
        
	end if;
       
          select indicator_name, value as Total from total_temp;
        
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_INDICATORS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_INDICATORS`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  select indicator_name  from indicators;
  
  SET pCODE                                  ='00';
  SET pDESC                                  = 'Sucess';
  SET p_MSG                                  ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_LATEST_QUARTER` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_LATEST_QUARTER`(
OUT LATEST_YEAR VARCHAR(10),
OUT LATEST_QUARTER VARCHAR(100),
OUT PCODE       VARCHAR(10),
OUT PDESC      	VARCHAR(500),
OUT PMSG        VARCHAR(10)
)
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 
 BEGIN
 
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;



SELECT max(YEAR) into LATEST_YEAR FROM FILE_DETAILS;
select distinct QUARTER into LATEST_QUARTER from FILE_DETAILS where YEAR = LATEST_YEAR order by QUARTER desc limit 1;

   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_ORGANIZATION`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
END;
  SET pCODE ='00';
  SET PDESC = 'Failure';
  SET P_MSG ='N';
SELECT 
    o.oid,
    o.organization_name,
    o.organization_description,
    o.ORG_ABV,
    ot.organization_type,
    o.created,
    o.created_by,
    o.organization_country
FROM
    organization o,
    organization_type ot
WHERE
    o.otid = ot.otid AND o.ended IS NULL;
  SET pCODE                           ='00';
  SET PDESC                           = 'Sucess';
  SET P_MSG                           ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_ORGANIZATION_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_ORGANIZATION_TYPE`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select otid,
  organization_type from organization_type where ended is null;
  SET pCODE                                                ='00';
  SET pDESC                                                = 'Sucess';
  SET p_MSG                                                ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_ORGS_PMR_FILE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_ORGS_PMR_FILE`(
	IN P_YEAR	VARCHAR(4000),
	OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   SET PDESC  =  CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

	select O.org_abv, OT.organization_type from pmr_organization O, organization_type OT, pmr_file_details F where O.ORG_ABV = F.ORG_NAME and O.OTID = OT.otid and F.YEAR = P_YEAR
    order by OT.organization_type;  

  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_COUNTRY`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';

  select distinct organization_country from pmr_organization where ended is null;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_EXPORT_VALUE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_EXPORT_VALUE`(
	IN P_YEAR	VARCHAR(4000),
    IN P_CAT	VARCHAR(4000),
    IN P_INDI	VARCHAR(4000),
    IN P_ORG	VARCHAR(4000),
    OUT VAL	VARCHAR(4000),
	OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN

declare cat_id varchar(4000);
declare ind_id varchar(4000);
declare org_id varchar(4000);

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

 SELECT oid into org_id from pmr_organization where ORG_ABV = P_ORG;
 SELECT cid into cat_id from categories where CATEGORY_NAME = P_CAT;
 SELECT IID into ind_id from indicators where INDICATOR_NAME = P_INDI and CATEGORY_ID = cat_id;
 
 SELECT VALUE into VAL from pmr_data where OID = org_id and CID = cat_id and IID = ind_id and YEAR = P_YEAR;


  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_FIXED_VALUES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_FIXED_VALUES`(
OUT `pCODE` VARCHAR(4000), 
OUT `PDESC` VARCHAR(4000), 
OUT `P_MSG` VARCHAR(4000)
)
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET pCODE = '99';
   GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
   SET P_MSG  =  'N';
end;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
 SELECT i.INDICATOR_NAME, aa.* FROM pmr_fixed_values aa, indicators i where i.IID = aa.IID;
  
  SET pCODE                                  ='00';
  SET pDESC                                  = 'Sucess';
  SET p_MSG                                  ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_ORGANIZATION`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
END;
  SET pCODE ='00';
  SET PDESC = 'Failure';
  SET P_MSG ='N';
SELECT 
    o.oid,
    o.organization_name,
    o.organization_description,
    o.ORG_ABV,
    ot.organization_type,
    o.created,
    o.created_by,
    o.organization_country
FROM
    pmr_organization o,
    organization_type ot
WHERE
    o.otid = ot.otid AND o.ended IS NULL;
  SET pCODE                           ='00';
  SET PDESC                           = 'Sucess';
  SET P_MSG                           ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_ORGS_FROM_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_ORGS_FROM_TYPE`(
	IN P_TYPE	VARCHAR(4000),
    OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;
	
    
    
    select * from pmr_organization where OTID = (select otid from organization_type where ORGANIZATION_TYPE = P_TYPE);
    
    
  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_REGION`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select distinct organization_region from pmr_organization where ended is null;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_SUB_CATEGORIES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_SUB_CATEGORIES`(
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;

	select * from pmr_sub_categories_1;
    
	SET PCODE =  '00';
	SET PDESC  =  'Success';
	SET PMSG   =  'Y';
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_SUB_CATEGORIES_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_SUB_CATEGORIES_TYPE`(
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;

	select * from pmr_sub_category_type;
    
	SET PCODE =  '00';
	SET PDESC  =  'Success';
	SET PMSG   =  'Y';
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_PMR_SUB_CATEGORY_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_PMR_SUB_CATEGORY_TYPE`(
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;

	select * from pmr_sub_category_type;
    
	SET PCODE =  '00';
	SET PDESC  =  'Success';
	SET PMSG   =  'Y';
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_REGION`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  select distinct organization_region from organization where ended is null;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_GET_USER` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_GET_USER`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
		
        
       SELECT * FROM USER;
        
        
		  SET pCODE ='00';
		  SET pDESC = 'Sucess';
		  SET p_MSG ='Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_LOV_QUARTER_get` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_LOV_QUARTER_get`(OUT PCODE                 VARCHAR(4000),
	OUT PDESC                   VARCHAR(4000),
	OUT PMSG                   VARCHAR(4000)
	)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   SET PDESC  =  CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

 SELECT * FROM LOV_QUARTER;


  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_BRANCH` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_BRANCH`(IN `P_BRANCH_NAME` VARCHAR(4000), IN `P_PROVINCE_NAME` VARCHAR(4000), IN `P_DISTRICT_NAME` VARCHAR(4000), IN `P_TOWN_NAME` VARCHAR(4000), IN `P_LONGITUDE` VARCHAR(4000), IN `P_LATITUDE` VARCHAR(4000), IN `P_FILE_ID` INT, OUT `P_BRANCH_ID` INT, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_TOWN_ID INT;
DECLARE V_PROVINCE_ID INT;
DECLARE V_DISTRICT_ID INT;
DECLARE V_COUNT_PROVINCE INT;
DECLARE V_COUNT_DISTRICT INT;
DECLARE V_COUNT_TOWN INT;
DECLARE V_COUNT_BRANCH INT;
DECLARE V_O_ID INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  SELECT COUNT(*) INTO V_COUNT_PROVINCE FROM MICROEYE_PROVINCE WHERE PROVINCE_NAME = P_PROVINCE_NAME;

  IF V_COUNT_PROVINCE = 1
	THEN
		SELECT P_ID INTO V_PROVINCE_ID FROM MICROEYE_PROVINCE WHERE PROVINCE_NAME = P_PROVINCE_NAME;
        SELECT COUNT(*) INTO V_COUNT_DISTRICT FROM MICROEYE_DISTRICT WHERE DISTRICT_NAME = P_DISTRICT_NAME AND P_ID = V_PROVINCE_ID;
        
        IF V_COUNT_DISTRICT = 1
			THEN
				SELECT D_ID INTO V_DISTRICT_ID FROM MICROEYE_DISTRICT WHERE DISTRICT_NAME = P_DISTRICT_NAME AND P_ID = V_PROVINCE_ID; 
				SELECT COUNT(*) INTO V_COUNT_TOWN FROM MICROEYE_TOWN WHERE TOWN_NAME = P_TOWN_NAME AND D_ID = V_DISTRICT_ID;
				
                IF V_COUNT_TOWN = 1
					THEN
						SELECT T_ID INTO V_TOWN_ID FROM MICROEYE_TOWN WHERE TOWN_NAME = P_TOWN_NAME AND D_ID = V_DISTRICT_ID;
						SELECT COUNT(*) INTO V_COUNT_BRANCH FROM MICROEYE_BRANCHES WHERE BRANCH_NAME = P_BRANCH_NAME AND T_ID = V_TOWN_ID AND F_id= P_FILE_ID;
                         
                        IF V_COUNT_BRANCH = 0
							THEN
								  
                                  
								  INSERT INTO MICROEYE_BRANCHES
								  (
									BRANCH_NAME,
									LATITUDE,
									LONGITUDE,
									T_ID,
                                    F_ID
								  )
								  VALUES
								  (
									P_BRANCH_NAME,
									P_LATITUDE,
									P_LONGITUDE,
									V_TOWN_ID,
                                    P_FILE_ID
								  );
									
								  COMMIT;
                                  
                                  SELECT B_ID INTO P_BRANCH_ID FROM MICROEYE_BRANCHES WHERE BRANCH_NAME = P_BRANCH_NAME AND T_ID = V_TOWN_ID AND F_id= P_FILE_ID;
                                  
                                  SET pCODE ='00';
								  SET pDESC = 'Sucess';
								  SET p_MSG ='Y';
							
                            ELSE
								
								  SELECT B_ID INTO P_BRANCH_ID FROM MICROEYE_BRANCHES WHERE BRANCH_NAME = P_BRANCH_NAME AND T_ID = V_TOWN_ID AND F_id= P_FILE_ID;
                                
								  SET pCODE ='00';
								  SET pDESC = 'Sucess';
								  SET p_MSG ='Y';
						
                        END IF;
					
                    ELSEIF P_TOWN_NAME = '-'
						THEN
							
							INSERT INTO MICROEYE_TOWN
							  (
								TOWN_NAME,
								D_ID
							  )
							  VALUES
							  (
								P_TOWN_NAME,
								V_DISTRICT_ID
							  );
								
							  COMMIT;
							
								SELECT T_ID INTO V_TOWN_ID FROM MICROEYE_TOWN WHERE TOWN_NAME = P_TOWN_NAME AND D_ID = V_DISTRICT_ID;
								
								
									INSERT INTO MICROEYE_BRANCHES
									  (
										BRANCH_NAME,
										LATITUDE,
										LONGITUDE,
										T_ID,
										F_ID
									  )
									  VALUES
									  (
										P_BRANCH_NAME,
										P_LATITUDE,
										P_LONGITUDE,
										V_TOWN_ID,
										P_FILE_ID
									  );
										
									  COMMIT;
									  
									  SELECT B_ID INTO P_BRANCH_ID FROM MICROEYE_BRANCHES WHERE BRANCH_NAME = P_BRANCH_NAME AND T_ID = V_TOWN_ID AND F_id= P_FILE_ID;
									  
									  SET pCODE ='00';
									  SET pDESC = 'Sucess';
									  SET p_MSG ='Y';
							
						ELSE
							SET pCODE ='-1';
							SET pDESC = 'TOWN DOES NOT EXIST';
							SET p_MSG = 'TOWN DOES NOT EXIST';
                        
                    END IF;
                    
		ELSE
						
			SET pCODE ='-1';
			SET pDESC = 'DISTRICT DOES NOT EXIST';
			SET p_MSG = 'DISTRICT DOES NOT EXIST';
				
		END IF;
                
	ELSE
						
		SET pCODE ='-1';
		SET pDESC = 'PROVINCE DOES NOT EXIST';
		SET p_MSG = 'PROVINCE DOES NOT EXIST';
			
    END IF;
	
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_COUNTRY`(IN `P_COUNTRY_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'CAN NOT ADD, ALREADY EXISTS';
  SET P_MSG = 'CAN NOT ADD, ALREADY EXISTS';
  
  
  SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_COUNTRY WHERE COUNTRY_NAME = P_COUNTRY_NAME;
  
  IF V_COUNT = 0
	THEN 
			  INSERT INTO MICROEYE_COUNTRY
			  (
				COUNTYR_NAME
			  )
			  VALUES
			  (
				P_COUNTRY_NAME
			  );
				
			  COMMIT;
			  
			  
			  SET pCODE ='00';
			  SET pDESC = 'Sucess';
			  SET p_MSG ='Y';
	
    END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_DISTRICT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_DISTRICT`(IN `P_DISTRICT_NAME` VARCHAR(4000), IN `P_PROVINCE_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_PROVINCE INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'CAN NOT ADD, ALREADY EXISTS';
  SET P_MSG = 'CAN NOT ADD, ALREADY EXISTS';
  
    
  SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_PROVINCE WHERE PROVINCE_NAME = P_PROVINCE_NAME;
  
  IF V_COUNT = 1
	THEN 
		SELECT P_ID INTO V_PROVINCE FROM MICROEYE_PROVINCE WHERE PROVINCE_NAME = P_PROVINCE_NAME;
        
        SET V_COUNT = 0;
        
        SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_DISTRICT WHERE DISTRICT_NAME = P_DISTRICT_NAME AND P_ID = V_PROVINCE;
			
            
            IF V_COUNT = 0
				THEN
            
						  INSERT INTO MICROEYE_DISTRICT
						  (
							DISTRICT_NAME,
                            P_ID
						  )
						  VALUES
						  (
							P_DISTRICT_NAME,
                            V_PROVINCE
						  );
							
						  COMMIT;
						  
						  
						  SET pCODE ='00';
						  SET pDESC = 'Sucess';
						  SET p_MSG ='Y';
	
			END IF;
            
	END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_INDICATOR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_INDICATOR`(IN `P_INDICATOR_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'CAN NOT ADD, ALREADY EXISTS';
  SET P_MSG = 'CAN NOT ADD, ALREADY EXISTS';
  
  
  SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_INDICATORS WHERE INDICATOR_NAME = P_INDICATOR_NAME;
  
  IF V_COUNT = 0
	THEN 
			  INSERT INTO MICROEYE_INDICATORS
			  (
				INDICATOR_NAME
			  )
			  VALUES
			  (
				P_INDICATOR_NAME
			  );
				
			  COMMIT;
			  
			  
			  SET pCODE ='00';
			  SET pDESC = 'Sucess';
			  SET p_MSG ='Y';
	
    END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_INDICATOR_DATA` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_INDICATOR_DATA`(
 IN `P_FILE_ID` INT,
 IN `P_INDICATOR_NAME` VARCHAR(4000),
 IN `P_BRANCH_ID` INT,
 IN `P_VALUE` VARCHAR(4000),
 OUT `PCODE` VARCHAR(4000),
 OUT `PDESC` VARCHAR(4000),
 OUT `PMSG` VARCHAR(4000))
BEGIN

	declare V_I_ID INT;
    declare V_COUNT INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
  Set PCODE ='99';
  SET PMSG ='N';
END;
  
  
	SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_INDICATORS WHERE INDICATOR_NAME = P_INDICATOR_NAME;
    
    IF V_COUNT = 1
		THEN
			SELECT I_ID INTO V_I_ID FROM MICROEYE_INDICATORS WHERE INDICATOR_NAME = P_INDICATOR_NAME;
            
            INSERT INTO MICROEYE_INDICATOR_DATA(
				B_ID,
                I_ID,
                F_ID,
                VALUE)
                VALUES(
                P_BRANCH_ID,
                V_I_ID,
                P_FILE_ID,
                P_VALUE);
                
                Set PCODE='00';
				Set PDESC='success';
				SET PMSG='Y';
                
		else
				Set PCODE='-1';
				Set PDESC='INDICATOR DOES NOT EXIST';
				SET PMSG='INDICATOR DOES NOT EXIST';
     
    
	END IF;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_ORGANIZATION`(
IN `P_ORGANIZATION_NAME` VARCHAR(250), 
IN `P_ORG_ABV` VARCHAR(20),
OUT `Pcode` VARCHAR(10), OUT `Pdesc` VARCHAR(500), OUT `P_Msg` VARCHAR(500))
BEGIN


DECLARE V_COUNT INT;

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;


	  Set Pcode='-1';
	  Set Pdesc='ALREADY EXIST';
	  SET P_Msg='ALREADY EXISTS';


	select exists(select * from microeye_organization where org_name = p_organization_name) into V_COUNT;

IF V_COUNT = 0
	THEN
    

	  insert
	  into microeye_organization
		(
		  org_name ,
		  ORG_ABV		  
		)
		values
		(
		  p_organization_name,
		  P_ORG_ABV
		);
	  Set Pcode='00';
	  Set Pdesc='success';
	  SET P_Msg='Y';
END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_OTHER_MARKERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_OTHER_MARKERS`(IN `P_ORG_NAME` VARCHAR(4000), IN `P_TYPE` VARCHAR(250), IN `P_LAT` VARCHAR(250),IN `P_LONG` VARCHAR(250), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_COUNTRY INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
	
    
		INSERT INTO MICROEYE_OTHER_MARKERS(ORG_NAME,TYPE,LATITUDE,LONGITUDE) VALUES (P_ORG_NAME,P_TYPE,P_LAT,P_LONG);
    
						  
						  
	  SET pCODE ='00';
	  SET pDESC = 'Sucess';
	  SET p_MSG ='Y';
	
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_PROVINCE`(IN `P_PROVINCE_NAME` VARCHAR(4000), IN `P_COUNTRY_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_COUNTRY INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'CAN NOT ADD, ALREADY EXISTS';
  SET P_MSG = 'CAN NOT ADD, ALREADY EXISTS';
  
    
  SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_COUNTRY WHERE COUNTRY_NAME = P_COUNTRY_NAME;
  
  IF V_COUNT = 1
	THEN 
		SELECT C_ID INTO V_COUNTRY FROM MICROEYE_COUNTRY WHERE COUNTRY_NAME = P_COUNTRY_NAME;
        
        SET V_COUNT = 0;
        
        SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_PROVINCE WHERE PROVINCE_NAME = P_PROVINCE_NAME AND C_ID = V_COUNTRY;
			
            
            IF V_COUNT = 0
				THEN
            
						  INSERT INTO MICROEYE_PROVINCE
						  (
							PROVINCE_NAME,
                            C_ID
						  )
						  VALUES
						  (
							P_PROVINCE_NAME,
                            V_COUNTRY
						  );
							
						  COMMIT;
						  
						  
						  SET pCODE ='00';
						  SET pDESC = 'Sucess';
						  SET p_MSG ='Y';
	
			END IF;
            
	END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_REGION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_REGION`(IN `P_REGION_NAME` VARCHAR(4000), IN `P_PARENT` VARCHAR(4000), IN `P_TYPE` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  INSERT INTO MICROEYE_REGIONS
  (
	REGION_NAME,
    PARENT,
    TYPE
  )
  VALUES
  (
	P_REGION_NAME,
    P_PARENT,
    P_TYPE
  );
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_ADD_TOWN` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_ADD_TOWN`(IN `P_TOWN_NAME` VARCHAR(4000), IN `P_DISTRICT_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_DISTRICT INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'CAN NOT ADD, ALREADY EXISTS';
  SET P_MSG = 'CAN NOT ADD, ALREADY EXISTS';
  
    
  SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_DISTRICT WHERE DISTRICT_NAME = P_DISTRICT_NAME;
  
  IF V_COUNT = 1
	THEN 
		SELECT D_ID INTO V_DISTRICT FROM MICROEYE_DISTRICT WHERE DISTRICT_NAME = P_DISTRICT_NAME;
        
        SET V_COUNT = 0;
        
        SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_TOWN WHERE TOWN_NAME = P_TOWN_NAME AND D_ID = V_DISTRICT;
			
            
            IF V_COUNT = 0
				THEN
            
						  INSERT INTO MICROEYE_TOWN
						  (
							TOWN_NAME,
                            D_ID
						  )
						  VALUES
						  (
							P_TOWN_NAME,
                            V_DISTRICT
						  );
							
						  COMMIT;
						  
						  
						  SET pCODE ='00';
						  SET pDESC = 'Sucess';
						  SET p_MSG ='Y';
	
			END IF;
            
	END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_BRANCH` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_BRANCH`(IN `P_B_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_BRANCHES where B_ID = P_B_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_COUNTRY`(IN `P_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_COUNTRY where C_ID = P_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_DISTRICT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_DISTRICT`(IN `P_D_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_DISTRICT where D_ID = P_D_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_INDICATORS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_INDICATORS`(IN `P_I_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_INDICATORS where I_ID = P_I_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_OTHER_MARKERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_OTHER_MARKERS`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_COUNTRY INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
	
    
    DELETE FROM MICROEYE_OTHER_MARKERS;
						  
	  SET pCODE ='00';
	  SET pDESC = 'Sucess';
	  SET p_MSG ='Y';
	
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_PROVINCE`(IN `P_P_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_PROVINCE where P_ID = P_P_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DELETE_TOWN` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DELETE_TOWN`(IN `P_T_ID` int, OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_TOWN where T_ID = P_T_ID;
	
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_DEL_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_DEL_ORGANIZATION`(IN `P_oID` DOUBLE, OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
  SET PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
  SET P_MSG ='N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  DELETE FROM MICROEYE_ORGANIZATION WHERE O_ID = P_oID;
  
  SET pCODE      ='00';
  SET pDESC      = 'Sucess';
  SET p_MSG      ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_EDIT_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_EDIT_ORGANIZATION`(IN `P_OID` DOUBLE, IN `P_ORGANIZATION_NAME` VARCHAR(4000),
IN `P_ORG_ABV` VARCHAR(4000),
OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `p_MSG` VARCHAR(4000))
BEGIN
  DECLARE v_otid VARCHAR(250);
 
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
 end;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update microeye_organization
  set org_name     =p_organization_name,
    ORG_ABV     =p_ORG_ABV
  where o_id     =p_oid;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_FILE_DETAILS_ADD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_FILE_DETAILS_ADD`(IN PFILE_NAME   VARCHAR(100),
                                  IN PPATH        VARCHAR(200),
                                  IN PTOTAL_LINES DOUBLE,
                                  IN PSIZE_BYTE   VARCHAR(50),
                                  IN PLOGIN_ID    VARCHAR(20),
								  IN PORG_NAME VARCHAR(150),
								  IN PYEAR VARCHAR(15),
                                  OUT PFILE_ID    int ,
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_FILE_ID int;
DECLARE V_COUNT int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
end;

	SELECT COUNT(*) INTO V_COUNT FROM MICROEYE_FILE_DETAILS WHERE FILE_NAME = PFILE_NAME AND YEAR = PYEAR;

	IF V_COUNT = 0
	THEN

	SELECT (IFNULL(MAX(FILE_ID),0) +1) INTO V_FILE_ID FROM MICROEYE_FILE_DETAILS;
    INSERT
          INTO MICROEYE_FILE_DETAILS( FILE_ID,
                              FILE_NAME,
                              PATH,
                              TOTAL_LINES,
                              SIZE_BYTE,
                              CREATED_BY,
							  ORG_NAME,
							  YEAR)
                        VALUES( V_FILE_ID,
                                PFILE_NAME,
                                PPATH,
                                PTOTAL_LINES,
                                PSIZE_BYTE,
                                PLOGIN_ID,
								PORG_NAME,
								PYEAR);
                          COMMIT;
	SET PFILE_ID = V_FILE_ID;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';
	
	ELSE
   SET PCODE =  '01';
   SET PDESC  =  'Already exist.';
   SET PMSG   =  'Y';
	
	END IF ;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_FILE_DETAILS_DEL` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_FILE_DETAILS_DEL`(
IN	PFILE_NAME  VARCHAR(100),
IN  PDELETE_BY 	VARCHAR(45),
OUT PCODE       VARCHAR(10),
OUT PDESC      	VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE ID INT;
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 
 
 BEGIN
 
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
INSERT INTO `MICROEYE_FILE_DETAILS_DEL`
(`FILE_ID`,
`FILE_NAME`,
`PATH`,
`TOTAL_LINES`,
`SIZE_BYTE`,
`CREATED`,
`CREATED_BY`,
`ORG_NAME`,
`YEAR`,
DELETE_BY)

SELECT 
	FILE_ID,
	FILE_NAME,
	PATH,
	TOTAL_LINES,
	SIZE_BYTE,
	CREATED,
	CREATED_BY,
	ORG_NAME,
	YEAR,
	PDELETE_BY
FROM 
	MICROEYE_FILE_DETAILS
WHERE
	FILE_NAME = PFILE_NAME;

SELECT FILE_ID into ID FROM MICROEYE_FILE_DETAILS WHERE FILE_NAME = PFILE_NAME;

	-- DELETE FROM MICROEYE_BRANCHES WHERE F_ID = ID;
    -- DELETE FROM MICROEYE_INDICATOR_DATA WHERE F_ID = ID;
     DELETE FROM MICROEYE_FILE_DETAILS WHERE FILE_ID = ID;



   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_FILE_DETAILS_GET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_FILE_DETAILS_GET`(IN PFILE_NAME   VARCHAR(100),
																 						 IN PLOGIN_ID    	VARCHAR(20),
																	 					 IN PORG_NAME	VARCHAR(150),
																		 				 IN PYEAR 				VARCHAR(15),
																				 		OUT PCODE       	VARCHAR(10),
																					 	OUT PDESC       	VARCHAR(500),
																						OUT PMSG        	VARCHAR(10))
BEGIN

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;

	SELECT FILE_ID,
                    FILE_NAME,
                    PATH,
                    TOTAL_LINES,
                    SIZE_BYTE,
					CREATED,
                    CREATED_BY,
   				    ORG_NAME,
					YEAR
		FROM PRC_ME_FILE_DETAILS
      WHERE FILE_NAME = COALESCE(PFILE_NAME,FILE_NAME)
             AND ORG_NAME = COALESCE(PORG_NAME,ORG_NAME)
					AND YEAR = COALESCE(PYEAR,YEAR) ;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_BRANCHES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_BRANCHES`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  SELECT B.B_ID, B.BRANCH_NAME, T.TOWN_NAME, D.DISTRICT_NAME , P.PROVINCE_NAME, B.LATITUDE, B.LONGITUDE, F.ORG_NAME
  
  FROM 
	microeye_branches B,
    microeye_town T,
    microeye_district D,
    microeye_province P,
    microeye_file_details F

  WHERE


	B.T_ID = T.T_ID AND
	T.D_ID = D.D_ID AND
	D.P_ID = P.P_ID AND
    B.F_ID = F.FILE_ID;
  
	SET pCODE ='00';
	SET pDESC = 'Sucess';
	SET p_MSG ='Y';
  
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_COUNTRY`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  SELECT * FROM MICROEYE_COUNTRY;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_DATA` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_DATA`(
 IN	 PYEAR		 INT,
 OUT PORG_COUNT  INT,
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN

	declare v_year int;

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';
	
    
    IF PYEAR = 0
		THEN
       			select YEAR into v_year from microeye_file_details order by Year desc limit 1;
                
		ELSE
				SET 	v_year = PYEAR;
    
    end if;
    
    select count(ORG_NAME) INTO PORG_COUNT from microeye_file_details where year = (select YEAR from microeye_file_details order by Year desc limit 1);
    
	select B.branch_name, B.latitude, B.longitude, I.indicator_name, ID.value, F.org_name, D.DISTRICT_NAME , P.PROVINCE_NAME, T.TOWN_NAME, F.YEAR
	from 
    microeye_branches B, 
    microeye_indicators I, 
    microeye_indicator_data ID, 
    microeye_file_details F, 
    microeye_town T,
    microeye_district D,
    microeye_province P

	where 
		
		B.F_ID = F.FILE_ID and
        F.YEAR = v_year and
        ID.B_ID = B.B_ID and
        B.T_ID = T.T_ID and
		T.D_ID = D.D_ID and
		D.P_ID = P.P_ID and
        ID.I_ID = I.I_ID
        
        order by F.org_name
        ;


    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
    
    
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_DISTRICT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_DISTRICT`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  select D.D_ID, D.DISTRICT_NAME, P.PROVINCE_NAME from microeye_district D, microeye_province P where D.P_ID = P.P_ID;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_DISTRICT_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_DISTRICT_PROVINCE`(IN `P_PROVINCE_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN



DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  select D.D_ID, D.DISTRICT_NAME, P.PROVINCE_NAME from microeye_district D, microeye_province P where D.P_ID = P.P_ID AND P.PROVINCE_NAME = P_PROVINCE_NAME;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_DISTRICT_TOWN` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_DISTRICT_TOWN`(IN `P_DISTRICT_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN



DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  select T.T_ID, T.TOWN_NAME, D.DISTRICT_NAME from microeye_district D, microeye_town T where T.D_ID = D.D_ID AND D.DISTRICT_NAME = P_DISTRICT_NAME;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_INDICATORS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_INDICATORS`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  SELECT * FROM MICROEYE_INDICATORS;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_ORGANIZATION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_ORGANIZATION`(OUT `pCODE` VARCHAR(4000), OUT `PDESC` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET p_MSG  =  'N';
END;
  SET pCODE ='00';
  SET PDESC = 'Failure';
  SET P_MSG ='N';
SELECT * FROM MICROEYE_ORGANIZATION;
  SET pCODE                           ='00';
  SET PDESC                           = 'Sucess';
  SET P_MSG                           ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_OTHER_MARKERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_OTHER_MARKERS`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN

DECLARE V_COUNT INT;
DECLARE V_COUNTRY INT;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
	
    
    SELECT * FROM MICROEYE_OTHER_MARKERS;
						  
	  SET pCODE ='00';
	  SET pDESC = 'Sucess';
	  SET p_MSG ='Y';
	
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_PROVINCE`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  select P.P_ID, P.PROVINCE_NAME, C.COUNTRY_NAME from MICROEYE_PROVINCE P, MICROEYE_COUNTRY C where P.C_ID = C.C_ID;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_ME_GET_TOWN` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_ME_GET_TOWN`(OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
  select T.T_ID, T.TOWN_NAME, D.DISTRICT_NAME from MICROEYE_DISTRICT D, MICROEYE_TOWN T where T.D_ID = D.D_ID;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_BRANCHES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_BRANCHES`(IN `P_BRANCH_ID` INT ,IN `P_BRANCH_NAME` VARCHAR(4000), IN `P_LATITUDE` VARCHAR(4000), IN `P_LONGITUDE` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update MICROEYE_BRANCHES
  set
	BRANCH_NAME     =	P_BRANCH_NAME,
    LATITUDE		=	P_LATITUDE,
    LONGITUDE		=	P_LONGITUDE
  where
	B_ID      =	P_BRANCH_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_COUNTRY` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_COUNTRY`(IN `P_COUNTRY_ID` INT ,IN `P_COUNTRY_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  
  update MICROEYE_COUNTRY
  set
	COUNTRY_NAME     =	P_COUNTRY_NAME
  where
	C_ID      =	P_COUNTRY_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_DISTRICT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_DISTRICT`(IN `P_DISTRICT_ID` INT ,IN `P_DISTRICT_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update MICROEYE_DISTRICT
  set
	DISTRICT_NAME     =	P_DISTRICT_NAME
  where
	D_ID      =	P_DISTRICT_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_INDICATOR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_INDICATOR`(IN `P_INDICATOR_ID` INT ,IN `P_INDICATOR_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update MICROEYE_INDICATORS
  set
	INDICATOR_NAME     =	P_INDICATOR_NAME
  where
	I_ID      =	P_INDICATOR_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_PROVINCE`(IN `P_PROVINCE_ID` INT ,IN `P_PROVINCE_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update MICROEYE_PROVINCE
  set
	PROVINCE_NAME     =	P_PROVINCE_NAME
  where
	P_ID      =	P_PROVINCE_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROEYE_EDIT_TOWN` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROEYE_EDIT_TOWN`(IN `P_TOWN_ID` INT ,IN `P_TOWN_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN


DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  SET pCODE ='-1';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  
  update MICROEYE_TOWN
  set
	TOWN_NAME     =	P_TOWN_NAME
  where
	T_ID      =	P_TOWN_ID;
  COMMIT;
  
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MICROWATCH_MASTER_SHEET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MICROWATCH_MASTER_SHEET`(
IN     PYEAR VARCHAR(15),
IN     PQUARTER VARCHAR(15),
OUT PCODE       VARCHAR(10),
OUT PDESC       VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
   SET PCODE = '99';
  Set PDESC =  'Other Vendor related software error.';
   SET PMSG  =  'N';
end;

    SET PCODE = '-1';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';

	
    SELECT distinct   F.ORG_NAME, I.INDICATOR_NAME, OT.ORGANIZATION_TYPE, Indicator_Value AS RESULT
	FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I,  MicroWatch_Indicator_Types IT, organization O, organization_type OT
 WHERE CI.FILE_ID   = F.FILE_ID
      AND CI.CI_IID		= I.MWI_ID
      AND CI.CI_ITID     = IT.MWIT_ID
	  AND IT.IT_NAME = 'Total'
      AND F.YEAR         = PYEAR
      AND F.QUARTER = PQUARTER
      and F.org_name = O.org_abv
      and O.otid = OT.otid

     order by F.org_name, I.indicator_name;
    

   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MW_GET_POTENTIAL_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MW_GET_POTENTIAL_PROVINCE`(IN `P_PROVINCE_NAME` VARCHAR(4000), OUT `pCODE` VARCHAR(4000), OUT `Pdesc` VARCHAR(4000), OUT `P_MSG` VARCHAR(4000))
BEGIN



DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET Pcode = '99';
   SET P_Msg  =  'N';
END;
  
  
	select * from potential where REGION_ID = (select R_ID from regions where REGION_NAME = P_PROVINCE_NAME) and year = 2018;
  
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
  
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_MW_GET_REGIONS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_MW_GET_REGIONS`(
	IN PARENT_NAME VARCHAR(4000),
	OUT PCODE	VARCHAR(4000),
	OUT PDESC	VARCHAR(4000),
	OUT PMSG    VARCHAR(4000)
	)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET PCODE = '99';
   SET PDESC  =  CONCAT(IFNULL(SQLCODE, '') , '|' , IFNULL(SQLERRM, ''));
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG  = 'N';

SELECT * FROM regions where PARENT = PARENT_NAME order by REGION_NAME;


  SET PCODE =  '00';
  SET PDESC  =  'Success';
  SET PMSG   =  'Y';



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_CALCULATE_FORMULA_VALUES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_CALCULATE_FORMULA_VALUES`(
 IN     P_ORG VARCHAR(4000),
 IN     P_YEAR INT,
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN

DECLARE P_YEAR_PREV INT;
DECLARE V_VAL_6 VARCHAR(4000);
DECLARE V_VAL_5 VARCHAR(4000);
DECLARE V_VAL_4 VARCHAR(4000);
DECLARE V_VAL_3 VARCHAR(4000);
DECLARE V_VAL_2 VARCHAR(4000);
DECLARE V_VAL_1 VARCHAR(4000);

DECLARE V_INDI_IN VARCHAR(4000);

DECLARE V_OID INT;
DECLARE V_CID INT;
DECLARE V_IID INT;

DECLARE ratio VARCHAR(4000); 


 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
    SET P_YEAR_PREV = P_YEAR - 1;
    
    
select oid into  V_OID from pmr_organization where ORG_ABV = P_ORG;

-- calculating indis for financing structure

		select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('financing structure');
		

	-- FOR 'Equity-to-asset ratio'
		
		
		
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Total equity' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_IID from indicators where INDICATOR_NAME = 'Total assets' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Equity-to-asset ratio' and CATEGORY_ID =  V_CID;

		 insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
		
	-- FOR Commercial liabilities-to-total debt
        
		SET V_VAL_1 = 0;
		SET V_VAL_2 = 1;
		
		select iid into  V_IID from indicators where INDICATOR_NAME = '- Commercial debt ' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_IID from indicators where INDICATOR_NAME = '- Subsidized debt*' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Commercial liabilities-to-total debt' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
       
	-- FOR Debt-to-equity ratio
	
		SET V_VAL_1 = 0;
		SET V_VAL_2 = 1;
	
        select iid into  V_IID from indicators where INDICATOR_NAME = 'Total debt' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_IID from indicators where INDICATOR_NAME = 'Total equity ' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Debt-to-equity ratio' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
      
	-- FOR Deposits-to-gross loan portfolio
		
		SET V_VAL_1 = 0;
		SET V_VAL_2 = 1;
	
        select iid into  V_IID from indicators where INDICATOR_NAME = 'Total Deposits ' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID =  V_CID;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

		select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Deposits-to-gross loan portfolio' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
      
       -- FOR Deposits-to-total assets
	   
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
			
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total Deposits ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total assets ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Deposits-to-total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
      
      -- FOR Gross loan portfolio-to-total assets
	  	  
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Gross loan portfolio-to-total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
            -- calculating indis for outreach
		
		select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('outreach');
        
         -- FOR Proportion of active women borrowers (%)
		 
		 
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Active women borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Active borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Proportion of active women borrowers (%)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
		
         -- FOR Average loan balance per active borrower (PKR)
		 
		 
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		 
		 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Active borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average loan balance per active borrower (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

		 -- FOR Average loan balance per active borrower/per capita income
		 
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average loan balance per active borrower (PKR)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Annual per capita income (PKR)*' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average loan balance per active borrower/per capita income' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

		-- FOR Average outstanding loan balance (PKR)
			
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of loans outstanding' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average outstanding loan balance (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

		-- FOR Average outstanding loan balance / per capita income
		
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average outstanding loan balance (PKR)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Annual per capita income (PKR)*' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average outstanding loan balance / per capita income' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

		-- FOR Average saving balance per active depositor (PKR)
		
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Deposits outstanding' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Depositors' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average saving balance per active depositor (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

		-- FOR Active deposit account balance (PKR)
		
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
			
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Deposits outstanding' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of deposit accounts' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Active deposit account balance (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

      
			-- calculating indis for financial performance
		select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('financial performance');
        
        
	-- FOR Income from loan portfolio
	
	
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select sum(value) into  ratio from pmr_dct_data where SUB_CAT_TYPE_ID in 
				(
				select cid from pmr_sub_category_type where CATEGORY_TYPE_NAME in ('Interest income on loan portfolio','Fee and commission income','Income from penalty fees')
				)

				and FILE_ID = (
						SELECT FILE_ID from pmr_file_details where ORG_NAME = P_ORG and YEAR = P_YEAR
                    )

			;
            
			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Income from loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

	-- FOR Income from other sources
	
	
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select sum(value) into  ratio from pmr_dct_data where SUB_CAT_TYPE_ID in 
				(
				select cid from pmr_sub_category_type where CATEGORY_TYPE_NAME in ('Other financial revenue','Other income from operations', 'Other income')
				)

				and FILE_ID = (
						SELECT FILE_ID from pmr_file_details where ORG_NAME = P_ORG and YEAR = P_YEAR
                    )
			;
            
			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Income from other sources' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

	-- FOR Total revenue
	
	
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from investments' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from other sources' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 +  V_VAL_2 +  V_VAL_3) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Total revenue' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

	-- FOR Less : financial expense
	
	
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select sum(value) into  ratio from pmr_dct_data where SUB_CAT_TYPE_ID in 
				(
				select cid from pmr_sub_category_type where CATEGORY_TYPE_NAME in ('Interest expense on borrowings','Interest expense on deposits', 'Other financial expense')
				)

				and FILE_ID = (
						SELECT FILE_ID from pmr_file_details where ORG_NAME = P_ORG and YEAR = P_YEAR
                    )
			;
            
			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

	-- FOR Gross financial margin
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total revenue' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 -  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Gross financial margin' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	-- FOR Cost of funds OF FINANCIAL STRUCTURE
			
            SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
            SET V_VAL_3 = 0;
            
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID =  5;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Income from other sources' and CATEGORY_ID =  5;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross financial margin' and CATEGORY_ID =  5;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select ( V_VAL_1 /  (V_VAL_2 + V_VAL_3)) * 100 into  ratio; 

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Cost of funds' and CATEGORY_ID =  3;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, 3, V_INDI_IN, ratio,P_YEAR);
			            
            
            

	-- FOR Net financial margin
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross financial margin' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: loan loss provision expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 -  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Net financial margin' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
  
	-- FOR Less: operating expense
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Personnel expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Admin expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
			select ( V_VAL_1 +  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Less: operating expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
  
  -- FOR Net income before tax
  
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
  
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Net financial margin' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Other Non operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
                      
			select ( V_VAL_1 +  V_VAL_2 +  V_VAL_3) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Net income before tax' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
  
	-- FOR Net income/(loss) 
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Net income before tax' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Provision for tax' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
                        
			select ( V_VAL_1 -  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Net income/(loss) ' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
  
	-- FOR Total Adjustment Expense 
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted Financial Expense on Borrowings' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Inflation Adjustment Expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted Loan Loss Provision Expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
			select ( V_VAL_1 +  V_VAL_2 +  V_VAL_3) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Total Adjustment Expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);

	-- FOR Net Income/(Loss) After Adjustments
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Net income/(loss) ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total Adjustment Expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 -  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Net Income/(Loss) After Adjustments' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
  
     -- FOR Average total assets
			
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total assets' and CATEGORY_ID = 3;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total assets' and CATEGORY_ID = 3;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR_PREV;
           
           select ( V_VAL_1 +  V_VAL_2)/2 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
        
     -- FOR Average total equity
	 
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total equity' and CATEGORY_ID = 3;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total equity' and CATEGORY_ID = 3;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR_PREV;
           
           select ( V_VAL_1 +  V_VAL_2)/2 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average total equity' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	-- FOR Adjusted return-on-assets
		
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Net Income/(Loss) After Adjustments' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted return-on-assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted return-on-equity
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Net Income/(Loss) After Adjustments' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total equity' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted return-on-equity' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Financial expense ratio
		
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID = 3;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR;
                       
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID = 3;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  3 and iid =  V_IID and YEAR = P_YEAR_PREV;
           
            
           select ( V_VAL_1 / ( V_VAL_2 -  V_VAL_3) ) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Financial expense ratio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Operational self sufficiency (OSS)
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total revenue' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense ' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
                       
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: loan loss provision expense' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
			
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: operating expense' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_4 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select iid into  V_IID from indicators where INDICATOR_NAME = 'Other Non operating expense' and CATEGORY_ID =   V_CID;
		   select value into  V_VAL_5 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
                       
           select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5) ) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Operational self sufficiency (OSS)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	-- FOR Financial self sufficiency (FSS)
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total revenue' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense ' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
                       
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: loan loss provision expense' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
			
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: operating expense' and CATEGORY_ID =   V_CID;
			select value into  V_VAL_4 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select iid into  V_IID from indicators where INDICATOR_NAME = 'Other Non operating expense' and CATEGORY_ID =   V_CID;
		   select value into  V_VAL_5 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Total Adjustment Expense' and CATEGORY_ID =   V_CID;
		   select value into  V_VAL_6 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
        
           
           select ( V_VAL_1 / ( V_VAL_2 +  V_VAL_3 +  V_VAL_4 +  V_VAL_5 +  V_VAL_6) ) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Financial self sufficiency (FSS)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
            -- calculating indis for Operating Income
	select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('Operating income');
    
	-- FOR Total revenue
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Total revenue' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Total revenue ' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
        
	-- FOR Adjusted net operating income / (loss)
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Net Income/(Loss) After Adjustments' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted net operating income / (loss)' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
    
    -- FOR Average total assets
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
        
	-- FOR Gross loan portfolio (opening balance)
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio' and CATEGORY_ID = 4;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR_PREV;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Gross loan portfolio (opening balance)' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
    
    
	-- FOR Average gross loan portfolio
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio (opening balance)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio (closing balance)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	-- FOR Total revenue ratio (total revenue-to-average total assets)
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted net operating income / (loss)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Total revenue ratio (total revenue-to-average total assets)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted profit margin (adjusted profit/(loss)-to-total revenue)
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted net operating income / (loss)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total revenue ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted profit margin (adjusted profit/(loss)-to-total revenue)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Yield on gross portfolio (nominal)
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Revenue from loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Yield on gross portfolio (nominal)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
            -- calculating indis for Operating expense
	select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('Operating expense');
    
    -- FOR Adjusted financial expense
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Less : financial expense' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted financial expense' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);

	-- FOR Adjusted loan loss provision expense
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Less: loan loss provision expense' and CATEGORY_ID = 5;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted Loan Loss Provision Expense' and CATEGORY_ID = 5;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 +  V_VAL_2) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted loan loss provision expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	 -- FOR Adjustment expense
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Total Adjustment Expense' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjustment expense' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
        
        
	-- FOR Adjusted total expense
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 0;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted financial expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted loan loss provision expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 +  V_VAL_2 +  V_VAL_3) into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted total expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            

	 -- FOR Average total assets
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID = 5;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
        
	
    -- FOR Adjusted total expense-to-average total assets
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted total expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted total expense-to-average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
    
    
    -- FOR Adjusted financial expense-to-average total assets
	
			
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted financial expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted financial expense-to-average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	 -- FOR Adjusted loan loss provision expense-to-average total assets
	 
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjusted loan loss provision expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted loan loss provision expense-to-average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
	-- FOR Adjusted operating expense-to-average total assets
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted operating expense-to-average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted personnel expense
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Personnel expense' and CATEGORY_ID = 5;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted personnel expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted admin expense
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Admin expense' and CATEGORY_ID = 5;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  5 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted admin expense' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	
    -- FOR Adjustment expense-to-average total assets
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Adjustment expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average total assets' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjustment expense-to-average total assets' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
    
   
-- calculating indis for Efficiency
	select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('Efficiency');
    
	-- FOR Average gross loan portfolio (PKR 000)
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolios' and CATEGORY_ID = 6;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  6 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average gross loan portfolio (PKR 000)' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, (V_VAL_1/1000),P_YEAR);
        
        
	-- FOR Average number of active borrowers
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Active borrowers' and CATEGORY_ID = 4;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR;
        
        select iid into  V_IID from indicators where INDICATOR_NAME = 'Active borrowers' and CATEGORY_ID = 4;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR_PREV;
        
         select ( V_VAL_1 +  V_VAL_2) / 2 into  ratio;   

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average number of active borrowers' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
        
        
	-- FOR Average number of active loans
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of loans outstanding' and CATEGORY_ID = 4;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR;
        
        select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of loans outstanding' and CATEGORY_ID = 4;
		select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR_PREV;
        
         select ( V_VAL_1 +  V_VAL_2) / 2 into  ratio;   

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average number of active loans' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
        
                
	 -- FOR Adjusted operating expense-to-average gross loan portfolio
	 
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	 
	 
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolio (PKR 000)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  (V_VAL_2 * 1000)) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted operating expense-to-average gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	
    -- FOR Adjusted personnel expense-to-average gross loan portfolio
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Personnel expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolio (PKR 000)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
           
           select ( V_VAL_1 /  (V_VAL_2 * 1000)) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted personnel expense-to-average gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	
    -- FOR Average salary/gross domestic product per capita
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
			SET V_VAL_3= 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Personnel expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Personnel' and CATEGORY_ID = 2;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  2 and iid =  V_IID and YEAR = P_YEAR;
            
            select iid into  V_IID from indicators where INDICATOR_NAME = 'Annual per capita income (PKR)*' and CATEGORY_ID = 4;
			select value into  V_VAL_3 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR;

				            
           select (( V_VAL_1 /  V_VAL_2) / ( V_VAL_3) )* 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average salary/gross domestic product per capita' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted cost per borrower (PKR)
		
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average number of active borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted cost per borrower (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Adjusted cost per loan (PKR)
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Operating expense' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average number of active loans' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Adjusted cost per loan (PKR)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
            
            
            -- calculating indis for Productivity
	select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('Productivity');
    
    -- FOR Borrowers per staff
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of active borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total staff' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Borrowers per staff' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
        
    -- FOR Loans per staff
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of active loans' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total staff' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Loans per staff' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Borrowers per loan officer
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of active borrowers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total loan officers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Borrowers per loan officer' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Loans per loan officer
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of active loans' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total loan officers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Loans per loan officer' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Depositors per staff
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of active depositors' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total staff' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Depositors per staff' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Deposit accounts per staff
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Number of deposit accounts' and CATEGORY_ID =  4;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  4 and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total staff' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2)  into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Deposit accounts per staff' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Personnel allocation ratio
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total loan officers' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Total staff' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Personnel allocation ratio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
        
-- calculating indis for Risk
	select cid into  V_CID from categories where upper(CATEGORY_NAME) = upper('Risk');
    
    
    -- FOR Portfolio at risk (>30)-to-gross loan portfolio
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Portfolio at risk > 30 days' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Portfolio at risk (>30)-to-gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	-- FOR Portfolio at risk(>90)-to-gross loan portfolio
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Portfolio at risk > 90 days' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Gross loan portfolio ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Portfolio at risk(>90)-to-gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
	
    -- FOR Average gross loan portfolio
		select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolio' and CATEGORY_ID = 6;
		select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  6 and iid =  V_IID and YEAR = P_YEAR;

		select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Average gross loan portfolio' and CATEGORY_ID =  V_CID;

		insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, V_VAL_1,P_YEAR);
    
    
    
    
    -- FOR Write off-to-average gross loan portfolio
	
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
	
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Loan Portfolio written off during year ' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Average gross loan portfolio' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Write off-to-average gross loan portfolio' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
            
    
    -- FOR Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)
		
		
			SET V_VAL_1 = 0;
			SET V_VAL_2 = 1;
		
			select iid into  V_IID from indicators where INDICATOR_NAME = 'Loan loss reserve (balance sheet)' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_1 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;

			select iid into  V_IID from indicators where INDICATOR_NAME = 'Portfolio at risk > 30 days' and CATEGORY_ID =  V_CID;
			select value into  V_VAL_2 from pmr_data where oid =  V_OID and cid =  V_CID and iid =  V_IID and YEAR = P_YEAR;
            				            
           select ( V_VAL_1 /  V_VAL_2) * 100 into  ratio;        

			select iid into  V_INDI_IN from indicators where INDICATOR_NAME = 'Risk coverage ratio (adjusted loan loss reserve-to-portfolio at risk >30days)' and CATEGORY_ID =  V_CID;

			insert into pmr_data (OID,CID,IID,VALUE,YEAR) values ( V_OID, V_CID, V_INDI_IN, ratio,P_YEAR);
	
			
			
        
		


    
    
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_DCT_INSERT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_DCT_INSERT`(
 IN     P_YEAR VARCHAR(4000),
 IN     P_INDICATOR VARCHAR(4000),
 IN     P_CATEGORY VARCHAR(4000),
 IN     P_ORGANIZATION VARCHAR(4000),
 IN     P_VALUE VARCHAR(4000),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN

 DECLARE V_CID INT;
 DECLARE V_IID INT;
 DECLARE V_OID INT;

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
	
    select oid into V_OID from pmr_organization where ORG_ABV = P_ORGANIZATION;

	select cid into V_CID from categories where CATEGORY_NAME = P_CATEGORY;

	select IID into V_IID from indicators where INDICATOR_NAME = P_INDICATOR and CATEGORY_ID = V_CID;

	INSERT INTO pmr_data (OID,CID,IID,VALUE,YEAR) values (V_OID,V_CID,V_IID,P_VALUE,P_YEAR);
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_DEL_FIXED_VALUES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_DEL_FIXED_VALUES`(
IN `P_ID` int,
OUT `Pcode` VARCHAR(4000),
OUT `Pdesc` VARCHAR(4000),
OUT `P_Msg` VARCHAR(4000))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET Pcode = '99';
   GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
   SET P_Msg  =  'N';
END;
  SET Pcode ='00';
  SET Pdesc = 'Failure';
  SET P_Msg ='N';
delete from pmr_fixed_values where ID = P_ID;
  SET Pcode      ='00';
  SET Pdesc      = 'Sucess';
  SET P_Msg      ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_EDIT_FIXED_VALUES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_EDIT_FIXED_VALUES`(
IN `P_ID` int, 
IN `P_VALUE` DOUBLE, 
IN `P_YEAR` DOUBLE, 
OUT `pCODE` VARCHAR(4000), 
OUT `Pdesc` VARCHAR(4000), 
OUT `P_MSG` VARCHAR(4000))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
   SET Pcode = '99';
   GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
   SET P_Msg  =  'N';
END;
  SET pCODE ='00';
  SET pDESC = 'Failure';
  SET P_MSG ='N';
  update pmr_fixed_values
  set value     =P_VALUE,
    year=P_YEAR
  where ID=P_ID;
  COMMIT;
  SET pCODE ='00';
  SET pDESC = 'Sucess';
  SET p_MSG ='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_FILE_DETAILS_ADD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_FILE_DETAILS_ADD`(IN PFILE_NAME   VARCHAR(100),
                                  IN PPATH        VARCHAR(200),
                                  IN PTOTAL_LINES DOUBLE,
                                  IN PSIZE_BYTE   VARCHAR(50),
                                  IN PLOGIN_ID    VARCHAR(20),
								  IN PORG_NAME VARCHAR(150),
								  IN PYEAR VARCHAR(15),
                                  OUT PFILE_ID    int ,
                                  OUT PCODE       VARCHAR(10),
                                  OUT PDESC       VARCHAR(500),
                                  OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_FILE_ID int;
DECLARE V_COUNT int;

DECLARE EXIT HANDLER FOR SQLEXCEPTION 
BEGIN
	GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
 
   SET PMSG  =  'N';
end;

	SELECT COUNT(*) INTO V_COUNT FROM PMR_FILE_DETAILS WHERE FILE_NAME = PFILE_NAME AND YEAR = PYEAR;

	IF V_COUNT = 0
	THEN

	SELECT (IFNULL(MAX(FILE_ID),0) +1) INTO V_FILE_ID FROM PMR_FILE_DETAILS;
    INSERT
          INTO PMR_FILE_DETAILS( FILE_ID,
                              FILE_NAME,
                              PATH,
                              TOTAL_LINES,
                              SIZE_BYTE,
                              CREATED_BY,
							  ORG_NAME,
							  YEAR
							   )
                        VALUES( V_FILE_ID,
                                PFILE_NAME,
                                PPATH,
                                PTOTAL_LINES,
                                PSIZE_BYTE,
                                PLOGIN_ID,
								PORG_NAME,
								PYEAR
                                );
                          COMMIT;
	SET PFILE_ID = V_FILE_ID;


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';
	
	ELSE
   SET PCODE =  '01';
   SET PDESC  =  'Already exist.';
   SET PMSG   =  'Y';
	
	END IF ;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_FILE_DETAILS_DEL` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_FILE_DETAILS_DEL`(
IN	PFILE_NAME  VARCHAR(100),
IN  PDELETE_BY 	VARCHAR(45),
OUT PCODE       VARCHAR(10),
OUT PDESC      	VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE V_FILE_ID INT;
 DECLARE V_O_ID INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 
 
 BEGIN
 
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
INSERT INTO `PMR_FILE_DETAILS_DEL`
(`FILE_ID`,
`FILE_NAME`,
`PATH`,
`TOTAL_LINES`,
`SIZE_BYTE`,
`CREATED`,
`CREATED_BY`,
`ORG_NAME`,
`YEAR`,
DELETE_BY)

SELECT 
	FILE_ID,
	FILE_NAME,
	PATH,
	TOTAL_LINES,
	SIZE_BYTE,
	CREATED,
	CREATED_BY,
	ORG_NAME,
	YEAR,
	PDELETE_BY
FROM 
	PMR_FILE_DETAILS
WHERE
	FILE_NAME = PFILE_NAME;

SELECT F.FILE_ID , O.oid INTO V_FILE_ID,V_O_ID  FROM PMR_FILE_DETAILS F, pmr_organization O WHERE F.FILE_NAME = PFILE_NAME and O.ORG_ABV = F.ORG_NAME;


DELETE FROM  pmr_dct_data WHERE FILE_ID = V_FILE_ID;

DELETE FROM pmr_data where oid = V_O_ID and YEAR = (select YEAR from pmr_file_details where FILE_ID = V_FILE_ID);

DELETE FROM  PMR_FILE_DETAILS WHERE FILE_ID = V_FILE_ID;

   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_INSERT_FIXED_VALUES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_INSERT_FIXED_VALUES`(
 IN     P_YEAR VARCHAR(4000),
 IN     P_INDICATOR_NAME VARCHAR(4000),
 IN     P_VALUE VARCHAR(4000),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN

 DECLARE V_IID INT;
 
 DECLARE V_COUNT INT;
 
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
	
    
    select count(*) into V_COUNT from indicators where INDICATOR_NAME = P_INDICATOR_NAME;
    
    IF V_COUNT > 0
		THEN
    
		select IID into V_IID from indicators where INDICATOR_NAME = P_INDICATOR_NAME;
		
		SELECT COUNT(*) INTO V_COUNT FROM PMR_FIXED_VALUES WHERE YEAR = P_YEAR AND IID = V_IID;
		
		IF V_COUNT = 0
			THEN
			
				INSERT INTO pmr_fixed_values (IID,VALUE,YEAR) values (V_IID,P_VALUE,P_YEAR);
				SET PCODE =  '00';
				SET PDESC  =  'SUCESS';
				SET PMSG   =  'Y';
		else
			
			SET PCODE =  '-1';
			SET PDESC  =  'ALREADY EXISTS';
			SET PMSG   =  'N';
		END IF;
	else
		SET PCODE =  '-1';
			SET PDESC  =  'INDICATOR NOT FOUND';
			SET PMSG   =  'N';
     END IF;
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_PMR_TRANSFER_FIXED` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_PMR_TRANSFER_FIXED`(
 IN     P_YEAR VARCHAR(4000),
 IN     P_ORG VARCHAR(4000),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
	
    INSERT into pmr_data (`OID`,
					`CID`,
					`IID`,
					`VALUE`,
					`YEAR`)

select O.oid, I.CATEGORY_ID, F.IID, F.Value, F.year from indicators I, pmr_fixed_values F, pmr_organization O where F.IID = I.IID and O.ORG_ABV = P_ORG and F.year = P_YEAR;

    
    
    
    
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_POTENTIAL_GET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_POTENTIAL_GET`(    IN PR_ID INT,
																		 				 IN PYEAR 				VARCHAR(15),
																				 		OUT PCODE       	VARCHAR(10),
																					 	OUT PDESC       	VARCHAR(500),
																						OUT PMSG        	VARCHAR(10))
BEGIN

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 PDESC = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;

SELECT
P_ID,
REGION_ID,
POTENTIAL_RATE,
YEAR,
CREATED,
CREATED_BY
FROM POTENTIAL

		where YEAR = PYEAR
AND R_ID = COALESCE(PR_ID,R_ID);


   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_Regional_Indicators_ADD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_Regional_Indicators_ADD`(
IN PCategory VARCHAR(50),
IN PIndicator VARCHAR(50),
IN PIndicator_Value DOUBLE,
IN PCREATED_BY VARCHAR(25),
IN PFILE_ID	INT,
IN PREGION VARCHAR(45),
OUT PCODE                 VARCHAR(10),
OUT PDESC                   VARCHAR(500),
OUT PMSG                   VARCHAR(500)
	)
BEGIN

DECLARE V_CID INT;
DECLARE V_IID INT; 
DECLARE V_R_ID INT;
DECLARE V_CHECK INT;

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
 BEGIN
	 GET DIAGNOSTICS CONDITION 1
		 Pdesc = MESSAGE_TEXT;
       -- PCODE = RETURNED_SQLSTATE, Pdesc = MESSAGE_TEXT;
   SET PCODE = '99';
   SET PMSG  =  'N';
end;

  SET PCODE = '-1';
  SET PDESC = 'Failure';
  SET PMSG   = 'N';


	 select 	IC_ID INTO  V_CID from MicroWatch_Category WHERE IC_NAME = PCategory;
	select 	MWI_ID INTO V_IID from MicroWatch_indicators WHERE INDICATOR_NAME =PIndicator AND  CATEGORY_ID = V_CID;
	-- SELECT MWIT_ID INTO V_CI_ITID FROM MicroWatch_Indicator_Types WHERE IT_NAME = PIndicatorType  AND indicator_ID= V_CI_IID ;
	
    select exists(
    
    SELECT R_ID FROM REGIONS WHERE REGION_NAME = PREGION) INTO V_CHECK;
	
    SET PMSG   = CONCAT('Region does not exist : ',PREGION);

	IF V_CHECK = 1
    THEN
    
		SET PMSG   = 'N';
    
		SELECT R_ID INTO V_R_ID FROM REGIONS WHERE REGION_NAME = PREGION;
        
		SELECT EXISTS(
             SELECT *
             FROM regional_indicators
             WHERE CID =  V_CID AND IID = V_IID and REGION_ID = V_R_ID AND FILE_ID = PFILE_ID ) INTO V_CHECK ;
	
	
			 IF V_CHECK = 0
			THEN

			  INSERT INTO Regional_Indicators( CID,
																		IID,
																		Indicator_Value,
																		CREATED_BY,
																		file_id,
																		REGION_ID)
														 VALUE(  V_CID,
																		 V_IID,
																		 PIndicator_Value,
																		PCREATED_BY,
																		PFILE_ID,
																		V_R_ID);
											

		  SET PCODE =  '00';
		  SET PDESC  =  'Success';
		  SET PMSG   =  'Y';
			ELSE
		  SET PCODE =  '01';
		  SET PDESC  =  'Already exist';
		  SET PMSG   =  'Y';
			END IF;
            
            
	END IF;
            
            
            
            
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_REGION_GET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_REGION_GET`(IN PTYPE VARCHAR(10), 
																				IN   PPARENT VARCHAR(10),  
																				OUT Pcode VARCHAR(10), OUT Pdesc VARCHAR(250), OUT  P_Msg VARCHAR(10))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  Set Pdesc = 'Other Vendor Software Error';
  Set Pcode ='99';
  SET P_Msg ='N';
END;
	IF  PTYPE IS NOT NULL
	THEN
		IF PTYPE = 'P'
		THEN
			    SELECT R_ID,
								REGION_NAME,
								PARENT,
								REGION_DESC,
								CREATED,
								CREATED_BY,
								TYPE
				   FROM  REGIONS
				 WHERE  (TYPE = 'P' OR REGION_NAME = 'ICT');
		ELSEIF PTYPE = 'D'
		THEN 
			    SELECT R_ID,
								REGION_NAME,
								PARENT,
								REGION_DESC,
								CREATED,
								CREATED_BY,
								TYPE
				   FROM  REGIONS
				 WHERE  TYPE = 'D';		
		END IF;
	END IF;

	IF  PPARENT IS NOT NULL
	THEN
				SELECT R_ID,
								REGION_NAME,
								PARENT,
								REGION_DESC,
								CREATED,
								CREATED_BY,
								TYPE
				   FROM REGIONS
				 WHERE  PARENT = PPARENT;
	END IF;

  Set Pcode='00';
  Set Pdesc='success';
  SET P_Msg='Y';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_SEARCH_PMR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_SEARCH_PMR`(IN `P_CAT` VARCHAR(4000), 
																																	  IN `P_INDICATORS` VARCHAR(4000), 
																																	  IN `P_START_YEAR` VARCHAR(4000), 
																																	  IN `P_END_YEAR` VARCHAR(4000), 
																																	  IN P_ORG_TYPE VARCHAR(4000),																																	
																																	  IN P_ORG_NAME VARCHAR(4000),
																																	  IN P_CURRENCY VARCHAR(1000),
																																	  OUT `pCODE` VARCHAR(4000), 
																																	  OUT `PDESC` VARCHAR(4000), 
																																	  OUT `P_MSG` VARCHAR(4000))
BEGIN
  -- DECLARE v_query VARCHAR(2000);
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  SET pCODE ='99';
GET DIAGNOSTICS CONDITION 1 pCODE = RETURNED_SQLSTATE, 
  PDESC = MESSAGE_TEXT;
 
  SET P_MSG ='N';
END;
	
   SET pCODE  ='00';
  SET pDESC  = 'Failure';
  SET P_MSG  ='N';

	IF P_ORG_TYPE IS NULL AND P_ORG_NAME IS NULL
	THEN
  
				select categories.category_name, indicators.indicator_name, pmr_organization.organization_name, pmr_organization.organization_country,
						  case indicators.INDI_CURR_CHECK when 'c' then  pmr_data.value * currency.VALUE  else pmr_data.value end value,
						  pmr_data.created, pmr_data.year, ot.ORGANIZATION_TYPE
				 from categories, indicators, pmr_organization, organization_type ot,	pmr_data , currency
					  where pmr_data.oid             = pmr_organization.oid
						  and pmr_data.cid              = categories.cid
						  and pmr_data.iid               = indicators.iid
						  and currency.year			= pmr_data.year
						  and pmr_organization.OTID  = ot.otid   
						  and UPPER(currency.NAME) = P_CURRENCY
				
				  and pmr_data.year          >= P_START_YEAR
				  and pmr_data.year          <= P_END_YEAR
				  and categories.cid in (select distinct
																				  SUBSTRING_INDEX(SUBSTRING_INDEX(P_CAT, ',', numbers.n), ',', -1) name
																				from
																				  (select @rownum := @rownum + 1 as n
																				from categories
																				cross join (select @rownum := 0) r
																				) numbers 
																				order by
																				   n)
				  and indicators.IID in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_INDICATORS, ',', n.digit+1), ',', -1) val
																				FROM
																				  indicators
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_INDICATORS, ',' , '')) <= LENGTH(P_INDICATORS)-n.digit)	
								order by categories.category_name,
								  indicators.indicator_name,
								  pmr_organization.organization_name;
  
	ELSE
				IF P_ORG_TYPE IS NOT NULL AND P_ORG_NAME IS  NULL
				THEN
				
				
						select categories.category_name,
								  indicators.indicator_name,
								  pmr_organization.organization_name,
								  pmr_organization.organization_country,
								  case indicators.INDI_CURR_CHECK when 'c' then  pmr_data.value * currency.VALUE  else pmr_data.value end value,
								  pmr_data.created,
								  pmr_data.year,
					ot.ORGANIZATION_TYPE
						 from categories,
								  indicators,
								  pmr_organization,
								  organization_type ot,	
								  pmr_data, currency
					  where pmr_data.oid             = pmr_organization.oid
						  and pmr_data.cid              = categories.cid
						  and pmr_data.iid               = indicators.iid
						  and currency.year			= pmr_data.year
						  and pmr_organization.OTID  = ot.otid   
						  and currency.NAME = P_CURRENCY	  
						  and pmr_data.year          >= P_START_YEAR
						  and pmr_data.year          <= P_END_YEAR
						  and categories.cid in (select distinct
																						  SUBSTRING_INDEX(SUBSTRING_INDEX(P_CAT, ',', numbers.n), ',', -1) name
																						from
																						  (select @rownum := @rownum + 1 as n
																						from categories
																						cross join (select @rownum := 0) r
																						) numbers 
																						order by
																						   n)
						  and indicators.IID in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_INDICATORS, ',', n.digit+1), ',', -1) val
																				FROM
																				  indicators
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_INDICATORS, ',' , '')) <= LENGTH(P_INDICATORS)-n.digit)	
						and ot.ORGANIZATION_TYPE in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_ORG_TYPE, ',', n.digit+1), ',', -1) val
																				FROM
																				  organization_type
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_ORG_TYPE, ',' , '')) <= LENGTH(P_ORG_TYPE)-n.digit)
								order by categories.category_name,
								  indicators.indicator_name,
								  pmr_organization.organization_name;



				ELSE
					
						select categories.category_name,
								  indicators.indicator_name,
								  pmr_organization.organization_name,
								  pmr_organization.organization_country,
								  case indicators.INDI_CURR_CHECK when 'c' then  pmr_data.value * currency.VALUE  else pmr_data.value end value,
								  pmr_data.created,
								  pmr_data.year,
					ot.ORGANIZATION_TYPE
						 from categories,
								  indicators,
								  pmr_organization,
								  organization_type ot,	
								  pmr_data, currency
					  where pmr_data.oid             = pmr_organization.oid
						  and pmr_data.cid              = categories.cid
						  and pmr_data.iid               = indicators.iid
						  and currency.year			= pmr_data.year
						  and pmr_organization.OTID  = ot.otid   
						  and currency.NAME = P_CURRENCY	  
						  and pmr_data.year          >= P_START_YEAR
				  and pmr_data.year          <= P_END_YEAR
						  and categories.cid in (select distinct
																						  SUBSTRING_INDEX(SUBSTRING_INDEX(P_CAT, ',', numbers.n), ',', -1) name
																						from
																						  (select @rownum := @rownum + 1 as n
																						from categories
																						cross join (select @rownum := 0) r
																						) numbers 
																						order by
																						   n)
						  and indicators.IID in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_INDICATORS, ',', n.digit+1), ',', -1) val
																				FROM
																				  indicators
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_INDICATORS, ',' , '')) <= LENGTH(P_INDICATORS)-n.digit)	
						and ot.ORGANIZATION_TYPE in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_ORG_TYPE, ',', n.digit+1), ',', -1) val
																				FROM
																				  organization_type
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_ORG_TYPE, ',' , '')) <= LENGTH(P_ORG_TYPE)-n.digit)
						AND pmr_organization.ORG_ABV in (SELECT
																				  DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(P_ORG_NAME, ',', n.digit+1), ',', -1) val
																				FROM
																				  pmr_organization
																				  INNER JOIN
																				  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3  UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6) n
																				  ON LENGTH(REPLACE(P_ORG_NAME, ',' , '')) <= LENGTH(P_ORG_NAME)-n.digit)
								order by categories.category_name,
								  indicators.indicator_name,
								  pmr_organization.organization_name;
						
			

				END IF;
	END IF;
 
 
  SET pCODE  ='00';
  SET pDESC  = 'SUCCESS';
  SET p_MSG  ='Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `PRC_VALIDATE_DATA` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `PRC_VALIDATE_DATA`(
 IN `P_FILE_ID` VARCHAR(4000),
 OUT `PCODE` VARCHAR(4000),
 OUT `PDESC` VARCHAR(4000),
 OUT `PMSG` VARCHAR(4000))
BEGIN

	declare V_VALUE_1 DOUBLE;
    declare V_VALUE_2 DOUBLE;


DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
  Set PDESC = CONCAT(IFNULL(SQLCODE, '') , '|' , Ifnull(Sqlerrm, ''));
  Set PCODE ='99';
  SET PMSG ='N';
END;
  
  SET PCODE = '-1';
  SET PDESC = 'DATA NOT CONSISTENT';
  SET PMSG  = 'DATA NOT CONSISTENT';
  
  
  
	select Indicator_Value INTO V_VALUE_1 from country_indicators where 
				CI_CID = (select ic_id from microwatch_category where IC_NAME = 'Microcredit') and
                CI_IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active Borrowers') and
                CI_ITID = (select MWIT_ID from microwatch_indicator_types where IT_NAME = 'Total' and indicator_id = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active Borrowers')) and
                FILE_ID = P_FILE_ID;
                
                
	select Indicator_Value INTO V_VALUE_2 from regional_indicators where
				CID = (select ic_id from microwatch_category where IC_NAME = 'Microcredit') and
                IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active Borrowers') and
                REGION_ID = (select R_ID from regions where TYPE = 'GT') and
                FILE_ID = P_FILE_ID;
                
    
    IF V_VALUE_1 = V_VALUE_2
		THEN
			-- CHECK NEXT INDICATOR
            
            select Indicator_Value INTO V_VALUE_1 from country_indicators where 
				CI_CID = (select ic_id from microwatch_category where IC_NAME = 'Microcredit') and
                CI_IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Gross Loan Portfolio') and
                CI_ITID = (select MWIT_ID from microwatch_indicator_types where IT_NAME = 'Total' and indicator_id = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Gross Loan Portfolio')) and
                FILE_ID = P_FILE_ID;
                
                
			select Indicator_Value INTO V_VALUE_2 from regional_indicators where
				CID = (select ic_id from microwatch_category where IC_NAME = 'Microcredit') and
                IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Gross Loan Portfolio') and
                REGION_ID = (select R_ID from regions where TYPE = 'GT') and
                FILE_ID = P_FILE_ID;
                
			IF V_VALUE_1 = V_VALUE_2
				THEN
					-- CHECK NEXT INDICATOR
                    select Indicator_Value INTO V_VALUE_1 from country_indicators where 
						CI_CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Savings') and
						CI_IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active savers') and
						CI_ITID = (select MWIT_ID from microwatch_indicator_types where IT_NAME = 'Total' and indicator_id = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active savers')) and
						FILE_ID = P_FILE_ID;
						
						
					select Indicator_Value INTO V_VALUE_2 from regional_indicators where
						CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Savings') and
						IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Active savers') and
						REGION_ID = (select R_ID from regions where TYPE = 'GT') and
						FILE_ID = P_FILE_ID;
						
					IF V_VALUE_1 = V_VALUE_2
						THEN
							-- CHECK NEXT INDICATOR
                            select Indicator_Value INTO V_VALUE_1 from country_indicators where 
								CI_CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Savings') and
								CI_IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Value of Savings') and
								CI_ITID = (select MWIT_ID from microwatch_indicator_types where IT_NAME = 'Total' and indicator_id = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Value of Savings')) and
								FILE_ID = P_FILE_ID;
								
								
							select Indicator_Value INTO V_VALUE_2 from regional_indicators where
								CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Savings') and
								IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Value of Savings') and
								REGION_ID = (select R_ID from regions where TYPE = 'GT') and
								FILE_ID = P_FILE_ID;
								
							IF V_VALUE_1 = V_VALUE_2
								THEN
									-- CHECK NEXT INDICATOR
                                    select Indicator_Value INTO V_VALUE_1 from country_indicators where 
										CI_CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Insurance') and
										CI_IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Policy Holders') and
										CI_ITID = (select MWIT_ID from microwatch_indicator_types where IT_NAME = 'Total' and indicator_id = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Policy Holders')) and
										FILE_ID = P_FILE_ID;
										
										
