						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;
                         
                        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;
                                  
                                  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;
                                
								  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;
									  
									  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 B.BRANCH_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;
										
										
									select Indicator_Value INTO V_VALUE_2 from regional_indicators where
										CID = (select ic_id from microwatch_category where IC_NAME = 'Micro-Insurance') and
										IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Number of Policy Holders') 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 = 'Sum Insured') 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 = 'Sum Insured')) 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-Insurance') and
												IID = (select MWI_ID from microwatch_indicators where INDICATOR_NAME = 'Sum Insured') 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
													  Set PCODE='00';
													  Set PDESC='success';
													  SET PMSG='Y';
											END IF;
									END IF;
							END IF;
					END IF;
			END IF;
            
	END IF;
	
  
  

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_ACTIVE_BORROWERS_BY_SECTOR` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_ACTIVE_BORROWERS_BY_SECTOR`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 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 IFNULL(SUM(Q1.RESULT),0)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	   AND IT.IT_NAME IN  ('Agriculture','Livestock/Poultry','Trade','Services','Manufacturing/Production','Housing','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT ifnull(SUM(Q2.RESULT),0)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	   AND IT.IT_NAME IN  ('Agriculture','Livestock/Poultry','Trade','Services','Manufacturing/Production','Housing','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;


 SELECT  IT.IT_NAME,IFNULL(SUM(CI.Indicator_Value),0)AS RESULT,F.YEAR ,F.QUARTER
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       -- AND F.YEAR         IN ( PYEAR_FIRST,PYEAR_SEC)
		-- AND F.QUARTER IN  (PQUARTER_FIRST,PQUARTER_SEC)
        AND F.FILE_ID IN (select jk.FILE_ID from
    (
	   (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) union
		
		(SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC ) )as jk
    )
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       	   AND IT.IT_NAME IN  ('Agriculture','Livestock/Poultry','Trade','Services','Manufacturing/Production','Housing','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
ORDER BY F.YEAR desc,F.QUARTEr desc;
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_MFP_STAFF` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_MFP_STAFF`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_TOTAL 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 = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT 
    
     IFNULL(SUM(Q1.RESULT ),0) INTO 	Q1_TOTAL
FROM
    (SELECT  SUM(CI.Indicator_Value) as RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE 
    ORDER BY I.INDICATOR_NAME , F.QUARTER) Q1;

SELECT 
    
     IFNULL(SUM(Q2.RESULT ),0) INTO 	Q2_TOTAL
FROM
    (SELECT  SUM(CI.Indicator_Value) AS RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_SEC
            AND F.QUARTER = PQUARTER_SEC
            AND I.INDICATOR_NAME = PINDICATOR_NAME
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE
    ORDER BY I.INDICATOR_NAME , F.QUARTER) Q2;

SELECT   OT.ORGANIZATION_TYPE ,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT, F.YEAR , F.QUARTER
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
        -- AND F.YEAR         IN ( PYEAR_FIRST,PYEAR_SEC)
		-- AND F.QUARTER IN  (PQUARTER_FIRST,PQUARTER_SEC)
        AND F.FILE_ID IN (select jk.FILE_ID from
    (
	   (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) union
		
		(SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC ) )as jk
    )
            AND I.INDICATOR_NAME = PINDICATOR_NAME
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE 
    ORDER BY I.INDICATOR_NAME , F.YEAR desc,F.QUARTEr desc;


 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_MICROENTERPRISE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_MICROENTERPRISE`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
IN      PIC_NAME  VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 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 SUM(CI.Indicator_Value) AS RESULT INTO Q1_TOTAL
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PIC_NAME
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER;

 SELECT SUM(CI.Indicator_Value) AS RESULT
		INTO Q2_TOTAL	
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
           AND CI.CI_IID        = I.MWI_ID
           AND CI.CI_CID   = C.IC_ID
           AND CI.CI_ITID          = IT.MWIT_ID
           AND F.YEAR         = PYEAR_SEC
           AND F.QUARTER = PQUARTER_SEC
           AND I.INDICATOR_NAME = PINDICATOR_NAME
           AND  IT.IT_NAME = PIC_NAME
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER;
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_OF_MFP_STAFF_RATIO` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_OF_MFP_STAFF_RATIO`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 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 Q1.YEAR ,Q1.QUARTER,Q1.ORGANIZATION_TYPE,(Q1.RESULT/(CASE Q2.RESULT  WHEN  0 THEN 1 else Q2.RESULT END)) AS RESULT
FROM 
(
 SELECT  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR         =   PYEAR_FIRST
		AND F.QUARTER =  PQUARTER_FIRST
	   AND I.INDICATOR_NAME =  PINDICATOR_NAME
       AND IT.IT_NAME = 'Total' 
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q1,
 (SELECT  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR         =   PYEAR_FIRST
		AND F.QUARTER =  PQUARTER_FIRST
	   AND I.INDICATOR_NAME =  'Total Microfinance Staff'
       AND IT.IT_NAME = 'Total' 
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q2
WHERE Q1.ORGANIZATION_TYPE= Q2.ORGANIZATION_TYPE;

SELECT Q1.YEAR ,Q1.QUARTER,Q1.ORGANIZATION_TYPE,(Q1.RESULT/(CASE Q2.RESULT  WHEN  0 THEN 1 else Q2.RESULT END)) AS RESULT
FROM 
(
 SELECT  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR         =   PYEAR_SEC
		AND F.QUARTER =  PQUARTER_SEC
	   AND I.INDICATOR_NAME =  PINDICATOR_NAME
       AND IT.IT_NAME = 'Total' 
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q1,
 (SELECT  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR         =   PYEAR_SEC
		AND F.QUARTER =  PQUARTER_SEC
	   AND I.INDICATOR_NAME =  'Total Microfinance Staff'
       AND IT.IT_NAME = 'Total' 
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q2
WHERE Q1.ORGANIZATION_TYPE= Q2.ORGANIZATION_TYPE;


    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_OF_PROVISION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_OF_PROVISION`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
IN PVALUE1 VARCHAR(50),
IN PVALUE2 VARCHAR(50),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
OUT Q1_VALUE1 DOUBLE,
OUT Q2_VALUE1 DOUBLE,
OUT Q1_VALUE2 DOUBLE,
OUT Q2_VALUE2 DOUBLE,
 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 SUM(Q1.RESULT)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME IN( PVALUE1,PVALUE2)
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT SUM(Q2.RESULT)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME IN( PVALUE1,PVALUE2)
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;


  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q1_VALUE1
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE1
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q2_VALUE1
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE1
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q1_VALUE2
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE2
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q2_VALUE2
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE2
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_OF_PROVISION_CHART_22` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_OF_PROVISION_CHART_22`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
OUT Q1_VALUE1 DOUBLE,
OUT Q2_VALUE1 DOUBLE,
OUT Q1_VALUE2 DOUBLE,
OUT Q2_VALUE2 DOUBLE,
 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 SUM(Q1.RESULT)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = 'Value of Savings'
       AND IT.IT_NAME IN( 'Branches4','Mobile Wallets5')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT SUM(Q2.RESULT)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = 'Value of Savings'
       AND IT.IT_NAME IN( 'Branches4','Mobile Wallets5')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;


    SELECT  IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
		INTO Q1_VALUE1
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
        AND OT.otid = 2
		AND F.FILE_ID IN (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) 
 
    and o.ended is null
		AND I.INDICATOR_NAME =  'Value of Savings'
        AND  IT.IT_NAME = 'Total'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR,F.QUARTER;
    
        SELECT  IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
		INTO Q2_VALUE1
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
        AND OT.otid = 2
		AND F.FILE_ID IN (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC) 
 
    and o.ended is null
		AND I.INDICATOR_NAME =  'Value of Savings'
        AND  IT.IT_NAME = 'Total'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR,F.QUARTER;

    SELECT  IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
		INTO Q1_VALUE2
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
        AND OT.otid = 3
		AND F.FILE_ID IN (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) 
 
    and o.ended is null
		AND I.INDICATOR_NAME =  'Value of Savings'
        AND  IT.IT_NAME = 'Total'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR,F.QUARTER;
    
        SELECT  IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
		INTO Q2_VALUE2
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
        AND OT.otid = 3
		AND F.FILE_ID IN (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC) 
 
    and o.ended is null
		AND I.INDICATOR_NAME =  'Value of Savings'
        AND  IT.IT_NAME = 'Total'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR,F.QUARTER;
  
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_OF_PROVISION_LM` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_OF_PROVISION_LM`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
IN PVALUE1 VARCHAR(50),
IN PVALUE2 VARCHAR(50),
IN PVALUE3 VARCHAR(50),

OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,

OUT Q1_VALUE1 DOUBLE,
OUT Q2_VALUE1 DOUBLE,

OUT Q1_VALUE2 DOUBLE,
OUT Q2_VALUE2 DOUBLE,

OUT Q1_VALUE3 DOUBLE,
OUT Q2_VALUE3 DOUBLE,


 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 SUM(Q1.RESULT)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME IN( PVALUE1,PVALUE2,PVALUE3)
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT SUM(Q2.RESULT)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME IN( PVALUE1,PVALUE2,PVALUE3)
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;


  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q1_VALUE1
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE1
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;



  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q1_VALUE3
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE3
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;


  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q2_VALUE3
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE3
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;



  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q2_VALUE1
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE1
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q1_VALUE2
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE2
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;

  SELECT IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
       INTO Q2_VALUE2
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       AND IT.IT_NAME = PVALUE2
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER;



 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_OF_PROVISION_PEERGROUP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_OF_PROVISION_PEERGROUP`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 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 SUM(Q1.RESULT)
INTO Q1_TOTAL
FROM (
	 SELECT  SUM(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR         =   PYEAR_FIRST
		AND F.QUARTER =  PQUARTER_FIRST
		AND I.INDICATOR_NAME =  PINDICATOR_NAME
		AND IT.IT_NAME = 'TOTAL'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q1;

SELECT SUM(Q2.RESULT)
INTO Q2_TOTAL
FROM (	SELECT SUM(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		AND F.YEAR        =PYEAR_SEC
		AND F.QUARTER =PQUARTER_SEC
		AND I.INDICATOR_NAME =  PINDICATOR_NAME
		AND IT.IT_NAME = 'TOTAL'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE) Q2;


	 SELECT  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		-- AND F.YEAR         IN ( PYEAR_FIRST,PYEAR_SEC)
		-- AND F.QUARTER IN  (PQUARTER_FIRST,PQUARTER_SEC)
        AND F.FILE_ID IN (select jk.FILE_ID from
    (
	   (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) union
		
		(SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC ) )as jk
    )
		AND I.INDICATOR_NAME =  PINDICATOR_NAME
		AND IT.IT_NAME = 'TOTAL'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR desc,F.QUARTEr desc;


 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_POLICY_HOLDERS_BY_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_POLICY_HOLDERS_BY_TYPE`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 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 SUM(Q1.RESULT)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	   AND IT.IT_NAME IN  ('Health','Agriculture/Livestock','Life','Property','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT SUM(Q2.RESULT)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	   AND IT.IT_NAME IN  ('Health','Agriculture/Livestock','Life','Property','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;


 SELECT  IT.IT_NAME,SUM(CI.Indicator_Value)AS RESULT,F.YEAR ,F.QUARTER
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       	   AND IT.IT_NAME IN ('Health','Agriculture/Livestock','Life','Property','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER;

 SELECT  IT.IT_NAME,SUM(CI.Indicator_Value)AS RESULT,F.YEAR ,F.QUARTER
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
       	   AND IT.IT_NAME IN  ('Health','Agriculture/Livestock','Life','Property','Other')
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER;

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_CHART_PORTFOLIO_RISK_30_DAYS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_CHART_PORTFOLIO_RISK_30_DAYS`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
OUT Q1_TOTAL DOUBLE,
OUT Q2_TOTAL DOUBLE,
 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 SUM(Q1.RESULT)
     INTO Q1_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q1;

 SELECT SUM(Q2.RESULT)
     INTO Q2_TOTAL
FROM
	(SELECT SUM(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND I.INDICATOR_NAME = PINDICATOR_NAME
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER) Q2;
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_DIST_DISTRIBUTION_MAP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_DIST_DISTRIBUTION_MAP`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PQUARTER_FIRST    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
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  R.REGION_NAME AS Q1_REGION_NAME, SUM(RI.Indicator_Value) AS Q1_Indicator_Value
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID	
      AND R.TYPE = 'D'
      AND F.YEAR         = PYEAR_FIRST
      AND F.QUARTER = PQUARTER_FIRST
      AND I.INDICATOR_NAME =PINDICATOR_NAME
GROUP BY RI.REGION_ID
ORDER BY Q1_Indicator_Value DESC;
	
   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_FILTER_DATA` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_FILTER_DATA`(
IN 	 PINDICATOR VARCHAR(100),
IN	 PINDICATOR_TYPE VARCHAR(100),  
IN 	 PPROVIDER VARCHAR(100),
IN     PYEAR_Q1 VARCHAR(15),
IN     PYEAR_Q2 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 F.YEAR ,F.QUARTER, COALESCE(SUM(Indicator_Value),0)  AS RESULT
	FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Indicator_Types IT, organization O
  WHERE CI.FILE_ID   = F.FILE_ID
      AND CI.CI_IID		= I.MWI_ID
      -- AND CI.CI_CID		= C.IC_ID
      AND CI.CI_ITID     = IT.MWIT_ID
	  AND F.ORG_NAME = O.ORG_ABV
	  AND IT.IT_NAME = PINDICATOR_TYPE
	  AND I.INDICATOR_NAME = PINDICATOR
	  AND O.ORG_ABV = COALESCE(PPROVIDER, O.ORG_ABV )
      AND( F.YEAR         between  PYEAR_Q1
	  AND PYEAR_Q2)
		GROUP BY F.YEAR ,F.QUARTER;


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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_LARGEST_PROVIDERS_OF_MICROCREDIT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_LARGEST_PROVIDERS_OF_MICROCREDIT`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_TOTAL 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 = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT 
        
            SUM(CI.Indicator_Value)
            INTO 
	V_TOTAL
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND OT.ORGANIZATION_TYPE = 'Other MFPs'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME;

 
	SELECT 
        O.ORGANIZATION_NAME,
        CI.Indicator_Value AS RESULT,
        V_TOTAL/CI.Indicator_Value AS MARKET_SHARE
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND OT.ORGANIZATION_TYPE = 'Other MFPs'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME
    ORDER BY RESULT
LIMIT 5;
 

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MC_HIGHESTGROWTH_DIST_BY_PROVINCE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MC_HIGHESTGROWTH_DIST_BY_PROVINCE`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 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 Q1.PARENT, Q1.REGION_NAME,Q1.RESULT AS A,(Q1.RESULT - Q2.RESULT) AS NET,IFNULL((((Q1.RESULT - Q2.RESULT)/Q2.RESULT)*100),0) AS PERCENTAGE ,
P.POTENTIAL_RATE,ifnull((((Q1.RESULT)/POTENTIAL_RATE)*100),0) AS PENETRATION_RATE
FROM 
(
SELECT  R.R_ID,R.PARENT, R.REGION_NAME,F.YEAR ,F.QUARTER, SUM(RI.Indicator_Value) AS RESULT
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
		AND F.YEAR         =   PYEAR_FIRST
		AND F.QUARTER =  PQUARTER_FIRST
	   AND I.INDICATOR_NAME =  PINDICATOR_NAME
GROUP BY R.R_ID,R.PARENT, R.REGION_NAME,F.YEAR ,F.QUARTER
order by  R.PARENT,RESULT desc
)Q1,
(
SELECT  R.R_ID,R.PARENT, R.REGION_NAME,F.YEAR ,F.QUARTER, SUM(RI.Indicator_Value) AS RESULT
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
		AND F.YEAR         =   PYEAR_SEC
		AND F.QUARTER =  PQUARTER_SEC
	   AND I.INDICATOR_NAME =  PINDICATOR_NAME
GROUP BY R.R_ID,R.PARENT, R.REGION_NAME,F.YEAR ,F.QUARTER
order by  R.PARENT,RESULT desc
) Q2,POTENTIAL P
WHERE Q1.REGION_NAME = Q2.REGION_NAME
AND Q1.R_ID = P.REGION_ID
AND Q2.R_ID = P.REGION_ID
order by  Q1.PARENT,Q1.RESULT desc
;

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MFPS_WITH_LARGEST_NET_INCREASE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MFPS_WITH_LARGEST_NET_INCREASE`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_TOTAL 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 = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT    
     SUM(Q1.RESULT) INTO 	V_TOTAL
FROM
    (SELECT  SUM(CI.Indicator_Value)as RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME
    ORDER BY I.INDICATOR_NAME , F.QUARTER) Q1;

SELECT 
    Q1.ORGANIZATION_NAME,
    Q1.ORG_ABV,
    Q1.ORGANIZATION_TYPE,
    Q1.RESULT -coalesce(Q2.RESULT, 0) AS NET_VALUE,
    IFNULL((((Q1.RESULT - coalesce(Q2.RESULT, 0)) / Q2.RESULT) * 100),0) AS PRCENTAGE,
     (Q1.RESULT/V_TOTAL )  * 100 AS MARKET_SHARE
FROM
(
SELECT   OT.ORGANIZATION_TYPE ,O.ORGANIZATION_NAME,O.ORG_ABV,SUM(CI.Indicator_Value) as RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME, O.ORG_ABV
    ORDER BY I.INDICATOR_NAME , F.QUARTER) Q1
LEFT JOIN
(
SELECT   OT.ORGANIZATION_TYPE  ,O.ORGANIZATION_NAME,O.ORG_ABV,SUM(CI.Indicator_Value) as RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_SEC
            AND F.QUARTER = PQUARTER_SEC
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME, O.ORG_ABV
    ORDER BY I.INDICATOR_NAME , F.QUARTER)Q2
ON
    Q1.ORGANIZATION_NAME = Q2.ORGANIZATION_NAME
order by NET_VALUE desc;
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MFPS_WITH_LARGEST_NET_INCREASE_PRECNT` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MFPS_WITH_LARGEST_NET_INCREASE_PRECNT`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_TOTAL 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 = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT  
    SUM(Q1.RESULT ) AS NET_VALUE 
INTO 	V_TOTAL

FROM
    (SELECT 
        O.ORGANIZATION_NAME,
            IFNULL(SUM(CI.Indicator_Value), 0) AS RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
       --    AND OT.ORGANIZATION_TYPE = 'Other MFPs'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME
    ORDER BY I.INDICATOR_NAME , F.QUARTER) Q1;
 

 
SELECT 
    Q1.ORGANIZATION_NAME,
     Q1.ORG_ABV,
    Q1.RESULT - coalesce(Q2.RESULT, 0) AS NET_VALUE,
    IFNULL((((Q1.RESULT - coalesce(Q2.RESULT, 0)) / Q2.RESULT) * 100),0) AS PRCENTAGE,
      (Q1.RESULT/V_TOTAL )  * 100 AS MARKET_SHARE
FROM
    (SELECT 
        O.ORGANIZATION_NAME,O.ORG_ABV,
            IFNULL(SUM(CI.Indicator_Value), 0) AS RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
        --    AND OT.ORGANIZATION_TYPE = 'Other MFPs'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME,O.ORG_ABV) Q1
left join
    (SELECT 
        O.ORGANIZATION_NAME,O.ORG_ABV,
            IFNULL(SUM(CI.Indicator_Value), 0) AS RESULT
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_SEC
            AND F.QUARTER = PQUARTER_SEC
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
           -- AND OT.ORGANIZATION_TYPE = 'Other MFPs'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME,O.ORG_ABV) Q2
on
    Q1.ORGANIZATION_NAME = Q2.ORGANIZATION_NAME
ORDER BY 4 DESC;
-- LIMIT 5;
 

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MFPS_WITH_LARGEST_PROVIDERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MFPS_WITH_LARGEST_PROVIDERS`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 OUT PCODE       VARCHAR(10),
 OUT PDESC       VARCHAR(500),
 OUT PMSG        VARCHAR(10))
BEGIN
DECLARE V_TOTAL 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 = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT SUM(T.Indicator_Value)
INTO V_TOTAL
FROM (
SELECT 
        SUM(CI.Indicator_Value) AS Indicator_Value
    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME) T;

 
SELECT 
O.ORGANIZATION_NAME, O.ORG_ABV,       
SUM(CI.Indicator_Value) AS INDICATOR_VALUE,
((SUM(CI.Indicator_Value))/V_TOTAL) * 100 AS MARKET_SHARE

    FROM
        Country_Indicators CI, FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT, organization_type OT, organization O
    WHERE
        CI.FILE_ID = F.FILE_ID
            AND CI.CI_IID = I.MWI_ID
            AND CI.CI_CID = C.IC_ID
            AND CI.CI_ITID = IT.MWIT_ID
            AND UPPER(F.ORG_NAME) = UPPER(O.ORG_ABV)
            AND O.OTID = OT.otid
            AND F.YEAR = PYEAR_FIRST
            AND F.QUARTER = PQUARTER_FIRST
            AND I.INDICATOR_NAME = PINDICATOR_NAME
            AND IT.IT_NAME = 'Total'
    GROUP BY I.INDICATOR_NAME , F.YEAR , F.QUARTER , OT.ORGANIZATION_TYPE , O.ORGANIZATION_NAME, O.ORG_ABV
    ORDER BY INDICATOR_VALUE  DESC;
-- LIMIT 5;
 

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MFP_LARGEST_GEOGRAPHIC_SPREAD` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MFP_LARGEST_GEOGRAPHIC_SPREAD`(
 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,
        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) 

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 `RPT_MICROWATCH` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MICROWATCH`(
IN     PYEAR VARCHAR(15),
IN     PQUARTER VARCHAR(15),
OUT PNO_OF_DISTRICT_C INT,
OUT P_POTENTIAL_PAKISTAN INT,
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';

SET P_POTENTIAL_PAKISTAN = 0;

SELECT ifnull(POTENTIAL_RATE,0) INTO P_POTENTIAL_PAKISTAN FROM potential where REGION_ID = (select R_ID from regions where REGION_NAME = 'Pakistan') and year = pyear;


SELECT COUNT(Q1.REGION_ID)      INTO PNO_OF_DISTRICT_C
FROM
(
SELECT  DISTINCT REGION_ID

FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
    --   AND RI.CID			= C.IC_ID
	  AND RI.Indicator_Value > 0	
      AND R.TYPE  IN ('D')
     AND F.YEAR         = PYEAR
      AND F.QUARTER = PQUARTER
	--   AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME IN ('Number of offices Fixed', 'Number of offices Mobile','Number of active borrowers','Gross Loan Portfolio','Number of Active Savers','Value of Savings','Number of Policy Holders','Sum Insured') )Q1;

SELECT  C.IC_NAME,  I.INDICATOR_NAME, COALESCE(SUM(Indicator_Value),0)  AS RESULT
	FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
 WHERE CI.FILE_ID   = F.FILE_ID
      AND CI.CI_IID		= I.MWI_ID
      AND CI.CI_CID		= C.IC_ID
      AND CI.CI_ITID     = IT.MWIT_ID
	  AND IT.IT_NAME = 'Total'
      AND F.YEAR         = PYEAR
      AND F.QUARTER = PQUARTER
GROUP BY C.IC_NAME,  I.INDICATOR_NAME;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MIP_AC` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MIP_AC`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';
    truncate table `MI_TEMP`;
insert into `MI_TEMP` (region,ph)  (SELECT    r.REGION_NAME,
     SUM(ri.Indicator_Value) AS indicator_value
FROM
    REGIONS r,
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd
WHERE
   
         r.R_ID = ri.REGION_ID
        AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST
        AND mi.INDICATOR_NAME = 'Number of Active Savers'
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME
ORDER BY indicator_value DESC
LIMIT 5
);

SELECT 
    r.REGION_NAME,
    mt.ph AS Indicator_Value,
    mt.ph - SUM(ri.Indicator_Value)  AS net,
    (( mt.ph - SUM(ri.Indicator_Value)) / mt.ph) * 100 AS per
FROM
    REGIONS r,
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd,
    MI_TEMP mt
WHERE
    r.R_ID = ri.REGION_ID AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER_SEC
        AND fd.YEAR = PYEAR_SEC
        AND mi.INDICATOR_NAME = 'Number of Active Savers'
        AND r.REGION_NAME = mt.region
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME , mt.ph
ORDER BY Indicator_Value DESC
;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MIP_PH` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MIP_PH`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';
    truncate table `MI_TEMP`;
insert into `MI_TEMP` (region,ph)  (SELECT    r.REGION_NAME,
     SUM(ri.Indicator_Value) AS indicator_value
FROM
    REGIONS r,
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd
WHERE
   
         r.R_ID = ri.REGION_ID
        AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME
ORDER BY indicator_value DESC
LIMIT 5
);

SELECT 
    r.REGION_NAME,
    mt.ph AS Indicator_Value,
    mt.ph - SUM(ri.Indicator_Value)  AS net,
    (( mt.ph - SUM(ri.Indicator_Value)) / SUM(ri.Indicator_Value)) * 100 AS per
FROM
    
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd,
	REGIONS r
left join 
    MI_TEMP mt
on  r.REGION_NAME = mt.region
WHERE
    r.R_ID = ri.REGION_ID AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER_SEC
        AND fd.YEAR = PYEAR_SEC
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
        AND r.REGION_NAME = mt.region
        
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME , mt.ph
ORDER BY Indicator_Value DESC
;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MIP_PH_MS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MIP_PH_MS`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
OUT PCODE       VARCHAR(10),
OUT PDESC       VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
DECLARE total 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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';
   
   
SELECT 
    SUM(ct.Indicator_Value)
INTO total FROM
    Country_Indicators ct,
    MicroWatch_indicators mi,
    MicroWatch_Indicator_Types mit,
    FILE_DETAILS fd
WHERE
    ct.CI_IID = mi.MWI_ID
        AND ct.CI_ITID = mit.MWIT_ID
        AND ct.FILE_ID = fd.FILE_ID
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
        AND mit.IT_NAME = 'Total'
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST;
   
SELECT 
    O.ORG_ABV AS ORG_NAME,
    ct.Indicator_Value,
    (ct.Indicator_Value / total) * 100 AS ms
FROM
    Country_Indicators ct,
    MicroWatch_indicators mi,
    MicroWatch_Indicator_Types mit,
    FILE_DETAILS fd,
organization O
	
WHERE
    ct.CI_IID = mi.MWI_ID
        AND ct.CI_ITID = mit.MWIT_ID
        AND ct.FILE_ID = fd.FILE_ID
AND fd.ORG_NAME = O.ORG_ABV
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
        AND mit.IT_NAME = 'Total'
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST
ORDER BY ct.Indicator_Value DESC
LIMIT 5;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MIP_PH_NET` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MIP_PH_NET`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';
    truncate table `MI_TEMP`;
insert into `MI_TEMP` (region,ph)  (SELECT    r.REGION_NAME,
     SUM(ri.Indicator_Value) AS indicator_value
FROM
    REGIONS r,
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd
WHERE
   
         r.R_ID = ri.REGION_ID
        AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
       AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME
ORDER BY indicator_value DESC
);

SELECT 
    r.REGION_NAME,
    mt.ph AS Indicator_Value,
    mt.ph-SUM(ri.Indicator_Value)  AS net,
    ((mt.ph- SUM(ri.Indicator_Value)) / SUM(ri.Indicator_Value)) * 100 AS per
FROM
    REGIONS r,
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd,
    MI_TEMP mt
WHERE
    r.R_ID = ri.REGION_ID AND r.TYPE = 'D'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
         AND fd.QUARTER = PQUARTER_SEC
        AND fd.YEAR = PYEAR_SEC
        AND mi.INDICATOR_NAME = 'Number of Policy Holders'
        AND r.REGION_NAME = mt.region
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME , mt.ph
ORDER BY net DESC
limit 5
;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MIP_PH_SI` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MIP_PH_SI`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
OUT PCODE       VARCHAR(10),
OUT PDESC       VARCHAR(500),
OUT PMSG        VARCHAR(10))
BEGIN
DECLARE total 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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';
   
   
SELECT 
    SUM(ct.Indicator_Value)
INTO total FROM
    Country_Indicators ct,
    MicroWatch_indicators mi,
    MicroWatch_Indicator_Types mit,
    FILE_DETAILS fd
WHERE
    ct.CI_IID = mi.MWI_ID
        AND ct.CI_ITID = mit.MWIT_ID
        AND ct.FILE_ID = fd.FILE_ID
        AND mi.INDICATOR_NAME = 'Sum Insured'
        AND mit.IT_NAME = 'Total'
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST;
   
SELECT 
     O.ORG_ABV AS ORG_NAME ,
    ct.Indicator_Value,
    (ct.Indicator_Value / total) * 100 AS ms
FROM
    Country_Indicators ct,
    MicroWatch_indicators mi,
    MicroWatch_Indicator_Types mit,
    FILE_DETAILS fd,
organization O
WHERE
    ct.CI_IID = mi.MWI_ID
AND fd.ORG_NAME = O.ORG_ABV
        AND ct.CI_ITID = mit.MWIT_ID
        AND ct.FILE_ID = fd.FILE_ID
        AND mi.INDICATOR_NAME = 'Sum Insured'
        AND mit.IT_NAME = 'Total'
        AND fd.QUARTER = PQUARTER_FIRST
        AND fd.YEAR = PYEAR_FIRST
ORDER BY ct.Indicator_Value DESC
LIMIT 5;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MI_PROVISION_BY_MFPS_BY_TYPE` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MI_PROVISION_BY_MFPS_BY_TYPE`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PCAT_NAME VARCHAR(45),
 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;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

	 SELECT  IT.IT_NAME,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
       AND F.YEAR = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND C.IC_NAME = PCAT_NAME
       AND  IT.IT_NAME IN('Health','Life','Other')
	GROUP BY  IT.IT_NAME; 

	 SELECT  IT.IT_NAME,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
       AND F.YEAR = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND C.IC_NAME = PCAT_NAME
       AND  IT.IT_NAME IN('Health','Life','Other')
	GROUP BY  IT.IT_NAME; 



    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MP_NO_OF_BRANCHES_UNITS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MP_NO_OF_BRANCHES_UNITS`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 OUT PQ1_TOTAL DOUBLE,
 OUT PQ2_TOTAL  DOUBLE,
 OUT PQ1_MFB DOUBLE,
 OUT PQ2_MFB DOUBLE,
 OUT PQ1_MFI  DOUBLE,
 OUT PQ2_MFI   DOUBLE,
 OUT PQ1_RSP DOUBLE,
 OUT PQ2_RSP DOUBLE,
 OUT PQ1_OTHER_MFPS DOUBLE,
 OUT PQ2_OTHER_MFPS  DOUBLE,
 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  IFNULL(SUM(CI.Indicator_Value),0)
   INTO   PQ1_TOTAL 
 FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
      AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
    AND C.IC_NAME = 'Operations'
       AND I.INDICATOR_NAME = 'Number of Offices'
       AND IT.IT_NAME = 'Total';
 
 SELECT  IFNULL(SUM(CI.Indicator_Value),0)
   INTO   PQ2_TOTAL 
 FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
      AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
    AND C.IC_NAME = 'Operations'
       AND I.INDICATOR_NAME = 'Number of Offices'
       AND IT.IT_NAME = 'Total';
 
SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ1_MFB			
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'MFB';

SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ2_MFB	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'MFB';
      
      SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ1_MFI	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'MFI';
      
      SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ2_MFI	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'MFI';
      
      SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ1_RSP	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'RSP';

      SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ2_RSP		
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'RSP';
 
SELECT  IFNULL(SUM(CI.Indicator_Value),0)
INTO PQ1_OTHER_MFPS		
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'Other MFPs';

SELECT  IFNULL(SUM(CI.Indicator_Value),0)
	INTO PQ2_OTHER_MFPS
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
      AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
	  AND O.OTID = OT.otid
    AND F.YEAR         = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME IN ('Fixed','Mobile')
      AND UPPER(OT.ORGANIZATION_TYPE) = 'Other MFPs';      

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_MP_NUMBER_OF_BRANCHES_UNITS
` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_MP_NUMBER_OF_BRANCHES_UNITS
`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC    VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    VARCHAR(15),
OUT PQ1_TOTAL DOUBLE,
OUT PQ2_TOTAL  DOUBLE,
OUT PQ1_MFB DOUBLE,
OUT PQ2_MFB DOUBLE,
OUT PQ1_MFI  DOUBLE,
OUT PQ2_MFI   DOUBLE,
OUT PQ1_RSP DOUBLE,
OUT PQ2_RSP DOUBLE,
OUT PQ1_OTHER DOUBLE,
OUT PQ2_OTHER  DOUBLE,
OUT PQ1_MFPS DOUBLE,
OUT PQ2_MFPS DOUBLE, 
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  IFNULL(SUM(CI.Indicator_Value),0)
	 INTO   PQ1_TOTAL	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
     AND F.YEAR         = PYEAR_FIRST
      AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME = 'Total';

SELECT  IFNULL(SUM(CI.Indicator_Value),0)
	 INTO   PQ2_TOTAL	
FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
WHERE CI.FILE_ID        = F.FILE_ID
      AND CI.CI_IID				    = I.MWI_ID
      AND CI.CI_CID			= C.IC_ID
      AND CI.CI_ITID          = IT.MWIT_ID
     AND F.YEAR         = PYEAR_FIRST
      AND F.QUARTER = PQUARTER_FIRST
	  AND C.IC_NAME = 'Operations'
      AND I.INDICATOR_NAME = 'Number of Offices'
      AND IT.IT_NAME = 'Total';



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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_NUMBER_OF_UPLOADED_FILES` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_NUMBER_OF_UPLOADED_FILES`(
 IN     PYEAR VARCHAR(15),
 IN     PQUARTER VARCHAR(15),
 OUT 	PNUMBER_OF_FILES 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 count(*) into PNUMBER_OF_FILES from file_details where YEAR = PYEAR and QUARTER = PQUARTER; -- like concat(PQUARTER,'%');

 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_NUMBER_OF_UPLOADED_FILES_PEER_GROUP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_NUMBER_OF_UPLOADED_FILES_PEER_GROUP`(
 IN     PYEAR VARCHAR(15),
 IN     PQUARTER VARCHAR(15),
 OUT 	PNUMBER_OF_FILES_MFI VARCHAR(15),
 OUT 	PNUMBER_OF_FILES_MFB VARCHAR(15),
 OUT 	PNUMBER_OF_FILES_RSP VARCHAR(15),
 OUT 	PNUMBER_OF_FILES_OTHER 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 count(*) into PNUMBER_OF_FILES_MFB from file_details where ORG_NAME in 
        (select ORG_ABV from organization where otid in (select otid from organization_type where ORGANIZATION_TYPE = 'MFB')) and QUARTER = PQUARTER
		and year  = PYEAR;
        
        select count(*) into PNUMBER_OF_FILES_MFI from file_details where ORG_NAME in 
        (select ORG_ABV from organization where otid in (select otid from organization_type where ORGANIZATION_TYPE = 'MFI')) and QUARTER = PQUARTER
		and year  = PYEAR;
        
        select count(*) into PNUMBER_OF_FILES_RSP from file_details where ORG_NAME in 
        (select ORG_ABV from organization where otid in (select otid from organization_type where ORGANIZATION_TYPE = 'RSP')) and QUARTER = PQUARTER
		and year  = PYEAR;
        
        select count(*) into PNUMBER_OF_FILES_OTHER from file_details where ORG_NAME in 
        (select ORG_ABV from organization where otid in (select otid from organization_type where ORGANIZATION_TYPE = 'Other MFPs')) and QUARTER = PQUARTER
		and year  = PYEAR;
        
 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_OAP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_OAP`(
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;

    SET PCODE = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';

SELECT 
    SUM(ri.Indicator_Value) AS indicator_value,
    r.REGION_NAME,
    mc.IC_NAME,
    mi.INDICATOR_NAME,
	COALESCE(P.POTENTIAL_RATE,0)  AS POTENTIAL_RATE
FROM
	Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd,
	REGIONS r
LEFT JOIN
	potential P
ON (P.REGION_ID = r.R_ID AND P.YEAR = PYEAR)
WHERE
    r.R_ID = ri.REGION_ID AND (r.TYPE = 'P'OR r.REGION_NAME = 'ICT')
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER
        AND fd.YEAR = PYEAR
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME

UNION
SELECT 
    SUM(ri.Indicator_Value) AS indicator_value,
    r.REGION_NAME,
    mc.IC_NAME,
    mi.INDICATOR_NAME,
	COALESCE(P.POTENTIAL_RATE,0)
FROM
	Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd,
	REGIONS r
LEFT JOIN
	potential P
ON (P.REGION_ID = r.R_ID AND P.YEAR = PYEAR)
WHERE
     r.R_ID = ri.REGION_ID AND r.TYPE  = 'GT'
        AND mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER
        AND fd.YEAR = PYEAR
GROUP BY r.REGION_NAME , mc.IC_NAME , mi.INDICATOR_NAME;
   SET PCODE =  '00';
   SET PDESC  =  'Success';
   SET PMSG   =  'Y';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_ODL` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_ODL`(
IN     PYEAR VARCHAR(15),
IN     PQUARTER VARCHAR(15),
IN     PPROVINCE_NAME    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 = '99';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';

    SELECT   
					SUM(T.INDICATOR_VALUE) AS INDICATOR_VALUE,
                    R.REGION_NAME,
					  	
					  T.IC_NAME,
					  T.INDICATOR_NAME,
					  COALESCE(P.POTENTIAL_RATE,0) AS POTENTIAL_RATE
		 FROM  REGIONS R

LEFT JOIN 
						  (
							 SELECT RI.INDICATOR_VALUE AS INDICATOR_VALUE,
									MC.IC_NAME,
									MI.INDICATOR_NAME,
									RI.REGION_ID
							   FROM MICROWATCH_CATEGORY MC,
									MICROWATCH_INDICATORS MI,
									FILE_DETAILS FD,
									REGIONAL_INDICATORS RI
							  WHERE MC.IC_ID = RI.CID
								AND RI.IID = MI.MWI_ID
								AND FD.FILE_ID = RI.FILE_ID
								AND FD.QUARTER =PQUARTER
								AND FD.YEAR =PYEAR
						  )  T
            ON R.R_ID = T.REGION_ID

LEFT JOIN POTENTIAL P
              ON (P.REGION_ID = R.R_ID AND P.YEAR = PYEAR)
     
     WHERE R.PARENT = PPROVINCE_NAME
           AND R.TYPE      = 'D'
 GROUP BY REGION_NAME , IC_NAME , INDICATOR_NAME;

SELECT
    SUM(T.INDICATOR_VALUE) AS INDICATOR_VALUE,
    R.REGION_NAME,
    T.ORG_NAME AS ORG_NAME
FROM REGIONS R
LEFT JOIN
    (
        SELECT
            RI.INDICATOR_VALUE AS INDICATOR_VALUE,
            RI.REGION_ID,
            O.ORG_ABV AS ORG_NAME
        FROM
            REGIONAL_INDICATORS RI,
            MICROWATCH_CATEGORY MC,
            MICROWATCH_INDICATORS MI,
            FILE_DETAILS FD,
            ORGANIZATION O
        WHERE MC.IC_ID = RI.CID
        AND RI.IID = MI.MWI_ID
        AND FD.FILE_ID = RI.FILE_ID
        AND FD.QUARTER = PQUARTER
        AND FD.YEAR = PYEAR
        AND FD.ORG_NAME = O.ORG_ABV
    ) T
ON R.R_ID = T.REGION_ID
WHERE R.PARENT = PPROVINCE_NAME
AND R.TYPE = 'D'
GROUP BY REGION_NAME, ORG_NAME
HAVING SUM(T.INDICATOR_VALUE)>0;


 SELECT    SUM(T.Indicator_Value) as indicator_value,
    r.REGION_NAME,
	T.IC_NAME,
    T.INDICATOR_NAME
FROM REGIONS r

LEFT JOIN 
(
SELECT 
    ri.Indicator_Value as indicator_value,
    RI.REGION_ID,
    mc.IC_NAME,
    mi.INDICATOR_NAME
FROM
   
    Regional_Indicators ri,
    MicroWatch_Category mc,
    MicroWatch_indicators mi,
    FILE_DETAILS fd
WHERE mc.IC_ID = ri.cid
        AND ri.IID = mi.MWI_ID
        AND fd.FILE_ID = ri.FILE_ID
        AND fd.QUARTER = PQUARTER
        AND fd.YEAR = PYEAR

) T
ON  r.R_ID = T.REGION_ID
WHERE
r.REGION_NAME = PPROVINCE_NAME
GROUP BY REGION_NAME , IC_NAME , INDICATOR_NAME ;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_PROVISION_BY_MFPS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_PROVISION_BY_MFPS`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PCAT_NAME VARCHAR(45),
OUT  TOTAL_1 INT,
OUT  TOTAL_2 INT,
 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;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';

SELECT COUNT(*) INTO TOTAL_1 FROM FILE_DETAILS WHERE   YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST;

SELECT COUNT(*) INTO TOTAL_2 FROM FILE_DETAILS WHERE   YEAR = PYEAR_SEC AND QUARTER =PQUARTER_SEC;

SELECT Q1.IT_NAME,COUNT(Q1.RESULT) AS RESULT,Q1.YEAR ,Q1.QUARTER
FROM (
		 SELECT IT.IT_NAME,F.FILE_NAME,F.YEAR ,F.QUARTER,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT 
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND C.IC_NAME = PCAT_NAME
		AND CI.Indicator_Value > 0 
	GROUP BY IT.IT_NAME,F.FILE_NAME,F.YEAR ,F.QUARTER)Q1
GROUP BY Q1.IT_NAME
UNION
SELECT Q1.IT_NAME,COUNT(Q1.RESULT) AS RESULT,Q1.YEAR ,Q1.QUARTER
FROM (
		 SELECT IT.IT_NAME,F.FILE_NAME,F.YEAR ,F.QUARTER,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT 
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND C.IC_NAME = PCAT_NAME
		AND CI.Indicator_Value > 0 
	GROUP BY IT.IT_NAME,F.FILE_NAME,F.YEAR ,F.QUARTER)Q1
GROUP BY Q1.IT_NAME;


/*	
SELECT COUNT(Q1.RESULT)
FROM (
		 SELECT F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORGANIZATION_NAME)
		AND O.OTID = OT.otid
			AND F.YEAR = '2017'       
			AND F.QUARTER = 'Q3 (1JUL-30SEP)'
       AND C.IC_NAME = 'Micro-Savings'
            AND IT.IT_NAME = 'Total'
       AND CI.Indicator_Value > 0
	GROUP BY F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE, O.ORGANIZATION_NAME
	ORDER BY F.QUARTER)Q1





SELECT C.IC_NAME,I.INDICATOR_NAME, IT.IT_NAME,F.YEAR ,F.QUARTER,COUNT(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND C.IC_NAME = PCAT_NAME
            AND IT.IT_NAME <> 'Total'
       AND CI.Indicator_Value > 0
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER; 


	SELECT C.IC_NAME,I.INDICATOR_NAME, IT.IT_NAME,F.YEAR ,F.QUARTER,COUNT(CI.Indicator_Value) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND F.YEAR = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND C.IC_NAME = PCAT_NAME
            AND IT.IT_NAME <> 'Total'
       AND CI.Indicator_Value > 0
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.QUARTER; 
*/
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_PROVISION_BY_MFPSBY_PEER_GROUP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_PROVISION_BY_MFPSBY_PEER_GROUP`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PCAT_NAME VARCHAR(45),
 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;
   SET PCODE = '99';
   SET PMSG  =  'N';
 end;
 
     SET PCODE = '-1';
     SET PDESC = 'Failure';
     SET PMSG  = 'N';


SELECT Q1.ORGANIZATION_TYPE,COUNT(Q1.RESULT) AS RESULT,Q1.YEAR ,Q1.QUARTER
FROM (
		 SELECT F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
       AND F.YEAR = PYEAR_FIRST
       AND F.QUARTER = PQUARTER_FIRST
       AND C.IC_NAME = PCAT_NAME
            AND IT.IT_NAME = 'Total'
       AND CI.Indicator_Value > 0
	GROUP BY F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE, O.ORGANIZATION_NAME)Q1
GROUP BY Q1.ORGANIZATION_TYPE,Q1.YEAR ,Q1.QUARTER
UNION 
SELECT Q2.ORGANIZATION_TYPE,COUNT(Q2.RESULT) AS RESULT,Q2.YEAR ,Q2.QUARTER
FROM (
		 SELECT F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,count(CI.Indicator_Value) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
       AND F.YEAR = PYEAR_SEC
       AND F.QUARTER = PQUARTER_SEC
       AND C.IC_NAME = PCAT_NAME
            AND IT.IT_NAME = 'Total'
       AND CI.Indicator_Value > 0
	GROUP BY F.FILE_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE, O.ORGANIZATION_NAME)Q2
GROUP BY Q2.ORGANIZATION_TYPE,Q2.YEAR ,Q2.QUARTER;


    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_REPORTING_ORGANIZATIONS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_REPORTING_ORGANIZATIONS`(
 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 
O.ORGANIZATION_NAME, OT.ORGANIZATION_TYPE,F.YEAR,F.QUARTER,
CASE WHEN F.FILE_NAME IS NOT NULL THEN '1' ELSE '0' END AS RESULT 
FROM organization_type OT,organization O 
LEFT JOIN
(SELECT
FILE_NAME,
ORG_NAME,
YEAR,
QUARTER
FROM FILE_DETAILS
WHERE YEAR IN (PYEAR_FIRST) AND QUARTER IN  (PQUARTER_FIRST)) F
ON O.ORG_ABV = F.ORG_NAME
where O.ENDED IS NULL
 AND OT.otid = O.OTID
ORDER BY O.ORGANIZATION_NAME;


    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_SUMMARY_OF_MC_PROVISION` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_SUMMARY_OF_MC_PROVISION`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 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 C.IC_NAME,I.INDICATOR_NAME, IT.IT_NAME,F.YEAR ,F.QUARTER,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	  FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT
	 WHERE CI.FILE_ID        = F.FILE_ID
       AND CI.CI_IID        = I.MWI_ID
       AND CI.CI_CID   = C.IC_ID
       AND CI.CI_ITID          = IT.MWIT_ID
       AND I.INDICATOR_NAME = PINDICATOR_NAME
      -- AND IT.IT_NAME IN ('Secured2','Total','Unsecured3','Group','Individual','Micro/Small Enterprise1','Fixed','Mobile')
      AND F.FILE_ID IN (select jk.FILE_ID from
    (
	   (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) union
		
		(SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC ) )as jk
    )
	GROUP BY  IT.IT_NAME,I.INDICATOR_NAME,C.IC_NAME ,F.YEAR ,F.QUARTER
    ORDER BY F.YEAR,F.QUARTEr asc;



 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_SUMMARY_OF_MC_PROVISION_PEERGROUP` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_SUMMARY_OF_MC_PROVISION_PEERGROUP`(
 IN     PYEAR_FIRST VARCHAR(15),
 IN     PYEAR_SEC    VARCHAR(15),
 IN     PQUARTER_FIRST VARCHAR(15),
 IN     PQUARTER_SEC    VARCHAR(15),
 IN     PINDICATOR_NAME VARCHAR(45),
 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  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE,IFNULL(SUM(CI.Indicator_Value),0) AS RESULT
	   FROM Country_Indicators CI,  FILE_DETAILS F, MicroWatch_indicators I, MicroWatch_Category C, MicroWatch_Indicator_Types IT , organization_type OT, organization O
	  WHERE CI.FILE_ID        = F.FILE_ID
		AND CI.CI_IID				    = I.MWI_ID
		AND CI.CI_CID			= C.IC_ID
		AND CI.CI_ITID          = IT.MWIT_ID
		AND UPPER(F.ORG_NAME)	= UPPER(O.ORG_ABV)
		AND O.OTID = OT.otid
		 AND F.FILE_ID IN (select jk.FILE_ID from
    (
	   (SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_FIRST AND QUARTER = PQUARTER_FIRST) union
		
		(SELECT FILE_ID FROM FILE_DETAILS WHERE YEAR = PYEAR_SEC AND QUARTER = PQUARTER_SEC ) )as jk
    )
    and o.ended is null
		AND I.INDICATOR_NAME =  PINDICATOR_NAME
        AND  IT.IT_NAME = 'Total'
	GROUP BY  I.INDICATOR_NAME,F.YEAR ,F.QUARTER,OT.ORGANIZATION_TYPE
	ORDER BY F.YEAR,F.QUARTER;


 
    SET PCODE =  '00';
    SET PDESC  =  'Success';
    SET PMSG   =  'Y';
 
 END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_T5DIST_DISTRIBUTION_ACTIVE_BORROWERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_T5DIST_DISTRIBUTION_ACTIVE_BORROWERS`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC    VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    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 Q1.REGION_NAME,Q1. Indicator_Value AS Q1_Indicator_Value,Q2.Indicator_Value AS Q2_Indicator_Value,(Q1. Indicator_Value -Q2.Indicator_Value) AS NET_VALUE,
((Q1. Indicator_Value -Q2.Indicator_Value)/Q2.Indicator_Value)*100 as PERCENTAGE
FROM
(SELECT  R.REGION_NAME AS REGION_NAME, IFNULL(SUM(RI.Indicator_Value),0) AS Indicator_Value
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
      AND F.YEAR         = PYEAR_FIRST
      AND F.QUARTER = PQUARTER_FIRST
      AND I.INDICATOR_NAME = 'Number of Active Borrowers'
GROUP BY RI.REGION_ID) Q1
LEFT JOIN
(SELECT  R.REGION_NAME AS REGION_NAME, IFNULL(SUM(RI.Indicator_Value),0) AS Indicator_Value
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
      AND F.YEAR         = PYEAR_SEC
      AND F.QUARTER = PQUARTER_SEC
      AND I.INDICATOR_NAME = 'Number of Active Borrowers' 
GROUP BY RI.REGION_ID)Q2
ON Q1.REGION_NAME = Q2.REGION_NAME
ORDER BY NET_VALUE DESC;

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

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RPT_T5DIST_DISTRIBUTION_ACTIVE_SAVERS` */;
DELIMITER ;;
CREATE DEFINER=`microwb1_pmn`@`%` PROCEDURE `RPT_T5DIST_DISTRIBUTION_ACTIVE_SAVERS`(
IN     PYEAR_FIRST VARCHAR(15),
IN     PYEAR_SEC    VARCHAR(15),
IN     PQUARTER_FIRST VARCHAR(15),
IN     PQUARTER_SEC    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 = MESSAGE_TEXT;
   SET PMSG  =  'N';
end;
    SET PCODE = '-1';
    SET PDESC = 'Failure';
    SET PMSG  = 'N';

SELECT Q1.REGION_NAME,Q1. Indicator_Value AS Q1_Indicator_Value,
Q2.Indicator_Value AS Q2_Indicator_Value,(Q1. Indicator_Value -Q2.Indicator_Value) AS NET_VALUE,
((Q1. Indicator_Value -Q2.Indicator_Value)/Q2.Indicator_Value)*100 as PERCENTAGE
FROM
(SELECT  R.REGION_NAME AS REGION_NAME, IFNULL(SUM(RI.Indicator_Value),0) AS Indicator_Value
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
      AND F.YEAR         = PYEAR_FIRST
      AND F.QUARTER = PQUARTER_FIRST
      AND I.INDICATOR_NAME = 'Number of Active Savers'
GROUP BY RI.REGION_ID) Q1
LEFT JOIN
(SELECT  R.REGION_NAME AS REGION_NAME, IFNULL(SUM(RI.Indicator_Value),0) AS Indicator_Value
FROM Regional_Indicators RI, REGIONS R, FILE_DETAILS F, MicroWatch_indicators I
WHERE RI.REGION_ID = R.R_ID
      AND RI.FILE_ID        = F.FILE_ID
      AND RI.IID				= I.MWI_ID
      AND R.TYPE ='D'
      AND F.YEAR         = PYEAR_SEC
      AND F.QUARTER = PQUARTER_SEC
      AND I.INDICATOR_NAME = 'Number of Active Savers'
GROUP BY RI.REGION_ID)Q2
ON Q1.REGION_NAME = Q2.REGION_NAME
ORDER BY NET_VALUE DESC;

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

END ;;
DELIMITER ;


