-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--   CORPORATE->TBL_CORPORATE_CUSTOM_DATA_CONF TBL_CORPORATE_CUSTOM_DATA_CONF TBL_CORPORATE_CUSTOM_DATA_CONF TBL_CORPORATE_CUSTOM_DATA_CONF TBL_CORPORATE_CUSTOM_DATA_CONF  
-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
------------------------------------------------------------------------------------------
            CREATE OR REPLACE FUNCTION fn_get_all_corporate_custom_data_conf() RETURNS SETOF corporate.tbl_corporate_custom_data_conf AS $$
                DECLARE

                BEGIN
                    RETURN QUERY SELECT * FROM corporate.tbl_corporate_custom_data_conf WHERE int_sys_action_id != -1;  
                END;
            $$ LANGUAGE plpgsql;
        

------------------------------------------------------------------------------------------
            CREATE OR REPLACE FUNCTION fn_get_all_corporate_custom_data_conf_with_action_user() 
                            RETURNS TABLE (pk_corporate_custom_data_conf_id BIGINT,
								fk_customer_id BIGINT,
								fk_corporate_custom_data_id BIGINT,

								sin_mandatory SMALLINT,
								sin_optional SMALLINT,
								vhr_default VARCHAR,

								sin_order SMALLINT,


								int_sys_action_id INT,
								fk_created_user_id BIGINT,
								vhr_created_user VARCHAR,
								dtm_created TIMESTAMP WITH TIME ZONE,
								fk_last_modified_user_id BIGINT,
								vhr_last_modified_user VARCHAR,
								dtm_last_modified TIMESTAMP WITH TIME ZONE,
								fk_deleted_user_id BIGINT,
								vhr_deleted_user VARCHAR,
								dtm_deleted TIMESTAMP WITH TIME ZONE) AS $$
                DECLARE

                BEGIN
                    RETURN QUERY SELECT mt.pk_corporate_custom_data_conf_id,
					mt.fk_customer_id,
					mt.fk_corporate_custom_data_id,

					mt.sin_mandatory,
					mt.sin_optional,
					mt.vhr_default,

					mt.sin_order,


					mt.int_sys_action_id,
					mt.fk_created_user_id,
					u1.vhr_login_name AS vhr_created_user,
					mt.dtm_created,
					mt.fk_last_modified_user_id,
					u2.vhr_login_name AS vhr_last_modified_user,
					mt.dtm_last_modified,
					mt.fk_deleted_user_id,
					u3.vhr_login_name AS vhr_deleted_user,
					mt.dtm_deleted
                        FROM corporate.tbl_corporate_custom_data_conf mt 
                            LEFT JOIN authentication.tbl_user u1
                                ON mt.fk_created_user_id =  u1.pk_user_id
                            LEFT JOIN authentication.tbl_user u2
                                ON mt.fk_last_modified_user_id =  u2.pk_user_id
                            LEFT JOIN authentication.tbl_user u3
                                ON mt.fk_deleted_user_id =  u3.pk_user_id
                        WHERE mt.int_sys_action_id != -1  
                        ORDER BY sin_order;
                END;
            $$ LANGUAGE plpgsql;
        

------------------------------------------------------------------------------------------

            CREATE OR REPLACE PROCEDURE sp_insert_or_update_corporate_custom_data_conf(jsnCorporateCustomDataConf JSONB, jsnSqlRes OUT JSON) AS $$
                DECLARE
                    objSpOutRes TYP_SP_OUT_RES;
                    objFnCheckRes TYP_FN_CHECK_RES;
                    dtmCurrent TIMESTAMP WITH TIME ZONE;
                BEGIN
                    SELECT NOW() INTO dtmCurrent;

                    -- // Duplication Checking in DB FUCTION(Insert and Update)
                    SELECT * FROM  fn_check_data_uniqueness('corporate.tbl_corporate_custom_data_conf'::VARCHAR, ''::VARCHAR, ::VARCHAR, 'pk_corporate_custom_data_conf_id'::VARCHAR, CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataConfId' AS BIGINT)::BIGINT) INTO objFnCheckRes;
                    IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN
                        objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                        objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                        objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message;

                        objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR';

                        SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                        RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                        RETURN;
                    END IF;

                    IF CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataConfId' AS BIGINT)::BIGINT = 0::BIGINT THEN
                        -- // Count Exced or other checking License related in DB FUCTION(Insert)
                        

                        --jsnCorporateCustomDataConf.intSysActionId = 0;
                        --jsnCorporateCustomDataConf.binCreatedUserId = jsnCorporateCustomDataConf.linLoginUserId;
                        --jsnCorporateCustomDataConf.dtmCreated = jsnCorporateCustomDataConf.dtmCurrentDateTime; --SELECT NOW();

                        INSERT INTO corporate.tbl_corporate_custom_data_conf (fk_customer_id,
						fk_corporate_custom_data_id,

						sin_mandatory,
						sin_optional,
						vhr_default,

						sin_order,


						int_sys_action_id,
						fk_created_user_id,
						dtm_created)
                            VALUES(CAST(jsnCorporateCustomDataConf->>'binCustomerId' AS BIGINT),
						CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataId' AS BIGINT),

						CAST(jsnCorporateCustomDataConf->>'sinMandatory' AS SMALLINT),
						CAST(jsnCorporateCustomDataConf->>'sinOptional' AS SMALLINT),
						jsnCorporateCustomDataConf->>'strDefault',

						CAST(jsnCorporateCustomDataConf->>'sinOrder' AS SMALLINT),


						0,
						CAST(jsnCorporateCustomDataConf->>'binCreatedUserId' AS BIGINT),
						dtmCurrent) RETURNING pk_corporate_custom_data_conf_id INTO objSpOutRes.binSqlPk;
                    ELSE
                        -- // UPDATE Check already update another usersin DB FUCTION(Update and Delete)
                        SELECT * FROM  fn_check_already_update_or_delete_another_user('corporate.tbl_corporate_custom_data_conf'::VARCHAR, 'pk_corporate_custom_data_conf_id'::VARCHAR, CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataConfId' AS BIGINT)::BIGINT, CAST(jsnCorporateCustomDataConf->>'intSysActionId' AS INT)::INT) INTO objFnCheckRes;
                        IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN
                            objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                            objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                            objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message;

                            objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR';

                            SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                            RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                            RETURN;
                        END IF;

                        --jsnCorporateCustomDataConf.intSysActionId = jsnCorporateCustomDataConf.intSysActionId + 1;
                        --jsnCorporateCustomDataConf.binLastModifiedUserId = jsnCorporateCustomDataConf.linLoginUserId;
                        --jsnCorporateCustomDataConf.dtmLastModified = jsnCorporateCustomDataConf.dtmCurrentDateTime; --SELECT NOW();

                        UPDATE corporate.tbl_corporate_custom_data_conf SET fk_customer_id = CAST(jsnCorporateCustomDataConf->>'binCustomerId' AS BIGINT),
						fk_corporate_custom_data_id = CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataId' AS BIGINT),

						sin_mandatory = CAST(jsnCorporateCustomDataConf->>'sinMandatory' AS SMALLINT),
						sin_optional = CAST(jsnCorporateCustomDataConf->>'sinOptional' AS SMALLINT),
						vhr_default = jsnCorporateCustomDataConf->>'strDefault',

						sin_order = CAST(jsnCorporateCustomDataConf->>'sinOrder' AS SMALLINT),


						int_sys_action_id = int_sys_action_id + 1,
						fk_last_modified_user_id = CAST(jsnCorporateCustomDataConf->>'binLastModifiedUserId' AS BIGINT),
						dtm_last_modified = dtmCurrent 
                        WHERE pk_corporate_custom_data_conf_id = CAST(jsnCorporateCustomDataConf->>'binCorporateCustomDataConfId' AS BIGINT);
                    END IF;

                    --COMMIT;
                    objSpOutRes.sinSqlCustomStatus := 1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'SUCCESS';
                    objSpOutRes.strSqlCustomMessage := 'CorporateCustomDataConf Saved';
                    objSpOutRes.dtmCurrent = dtmCurrent;
                    
                    SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                    RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                    RETURN;
                EXCEPTION WHEN OTHERS THEN
                    ROLLBACK;
                    objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                    objSpOutRes.strSqlCustomMessage := 'CorporateCustomDataConf Save Failed';

                    objSpOutRes.strSqlCustomErrorType := 'DATABASE_SYSTEM_ERROR';

                    GET STACKED DIAGNOSTICS 
				        objSpOutRes.strSqlSysMessage := MESSAGE_TEXT,
                    	objSpOutRes.strSqlSysExceptionDetail := PG_EXCEPTION_DETAIL,
                    	objSpOutRes.strSqlSysExceptionHint := PG_EXCEPTION_CONTEXT;
                
                    SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                    RAISE EXCEPTION 'jsnSqlRes: %', jsnSqlRes;
                    RETURN;
                END;
            $$ LANGUAGE plpgsql;
        

------------------------------------------------------------------------------------------

            CREATE OR REPLACE PROCEDURE sp_delete_corporate_custom_data_conf(binCorporateCustomDataConfId BIGINT, intSysActionId INT, binDeletedUserId BIGINT, jsnSqlRes OUT JSON) AS $$
                DECLARE
                    objSpOutRes TYP_SP_OUT_RES;
                    objFnCheckRes TYP_FN_CHECK_RES;
                    dtmCurrent TIMESTAMP WITH TIME ZONE;
                BEGIN
                    SELECT NOW() INTO dtmCurrent;

                    -- // FOREIGN KEY REFERENCES Checking in DB FUCTION(Delete)
                    SELECT * FROM  fn_check_foreignkey_referance('tbl_corporate_custom_data_conf'::VARCHAR, 'pk_corporate_custom_data_conf_id'::VARCHAR, binCorporateCustomDataConfId::BIGINT, '{}'::VARCHAR ARRAY) INTO objFnCheckRes;
                    IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN
                        objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                        objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                        objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message;

                        objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR';

                        SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                        RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                        RETURN;
                    END IF;

                    -- // UPDATE Check already update another users in DB FUCTION(Update and Delete)
                    SELECT * FROM  fn_check_already_update_or_delete_another_user('corporate.tbl_corporate_custom_data_conf'::VARCHAR, 'pk_corporate_custom_data_conf_id'::VARCHAR, CAST(binCorporateCustomDataConfId AS BIGINT)::BIGINT, intSysActionId::INT) INTO objFnCheckRes;
                    IF objFnCheckRes.sin_sql_check_status = -1::SMALLINT THEN
                        objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                        objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                        objSpOutRes.strSqlCustomMessage := objFnCheckRes.vhr_sql_check_message;

                        objSpOutRes.strSqlCustomErrorType := 'DATABASE_VALIDATION_ERROR';

                        SELECT * FROM  fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                        RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                        RETURN;
                    END IF;

                    --dtmDeleted = SELECT NOW();
                    UPDATE corporate.tbl_corporate_custom_data_conf SET int_sys_action_id = -1::INT,
                                fk_deleted_user_id = binDeletedUserId,
                                dtm_deleted = dtmCurrent
                        WHERE pk_corporate_custom_data_conf_id = binCorporateCustomDataConfId;

                    --COMMIT;
                    objSpOutRes.sinSqlCustomStatus := 1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'SUCCESS';
                    objSpOutRes.strSqlCustomMessage := 'CorporateCustomDataConf Deleted';
                    objSpOutRes.dtmCurrent = dtmCurrent;

                    SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                    RAISE NOTICE 'jsnSqlRes: %', jsnSqlRes;
                    RETURN;
                EXCEPTION WHEN OTHERS THEN
                    ROLLBACK;
                    objSpOutRes.sinSqlCustomStatus := -1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'ERROR';
                    objSpOutRes.strSqlCustomMessage := 'CorporateCustomDataConf Delete Failed';

                    objSpOutRes.strSqlCustomErrorType := 'DATABASE_SYSTEM_ERROR';

                    GET STACKED DIAGNOSTICS 
				        objSpOutRes.strSqlSysMessage := MESSAGE_TEXT,
                    	objSpOutRes.strSqlSysExceptionDetail := PG_EXCEPTION_DETAIL,
                    	objSpOutRes.strSqlSysExceptionHint := PG_EXCEPTION_CONTEXT;

                    SELECT * FROM fn_convert_type_out_object_to_out_json(objSpOutRes) INTO jsnSqlRes;
                    RAISE EXCEPTION 'jsnSqlRes: %', jsnSqlRes;
                    RETURN;
                END;
            $$ LANGUAGE plpgsql;
        

