-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- @@  AIRTICKET->TBL_AIR_FARE_TYPE TBL_AIR_FARE_TYPE TBL_AIR_FARE_TYPE TBL_AIR_FARE_TYPE  @@
-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
------------------------------------------------------------------------------------------
            CREATE OR REPLACE FUNCTION fn_get_all_air_fare_type() RETURNS SETOF airticket.tbl_air_fare_type AS $$
                DECLARE

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

------------------------------------------------------------------------------------------
            CREATE OR REPLACE FUNCTION fn_get_all_air_fare_type_with_action_user() 
                            RETURNS TABLE (pk_air_fare_type_id BIGINT,
								vhr_air_fare_type_code VARCHAR,
								vhr_air_fare_type VARCHAR,
								txt_remarks TEXT,
								arj_gds_fare_code JSON[],


								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_air_fare_type_id,
					mt.vhr_air_fare_type_code,
					mt.vhr_air_fare_type,
					mt.txt_remarks,
					mt.arj_gds_fare_code,


					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 airticket.tbl_air_fare_type 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 ;
                END;
            $$ LANGUAGE plpgsql;
        

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

            CREATE OR REPLACE PROCEDURE sp_insert_or_update_air_fare_type(jsnAirFareType 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('airticket.tbl_air_fare_type'::VARCHAR, 'vhr_air_fare_type_code'::VARCHAR, jsnAirFareType->>'strAirFareTypeCode'::VARCHAR, 'pk_air_fare_type_id'::VARCHAR, CAST(jsnAirFareType->>'binAirFareTypeId' 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(jsnAirFareType->>'binAirFareTypeId' AS BIGINT)::BIGINT = 0::BIGINT THEN
                        -- // Count Exced or other checking License related in DB FUCTION(Insert)
                        

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

                        INSERT INTO airticket.tbl_air_fare_type (vhr_air_fare_type_code,
						vhr_air_fare_type,
						txt_remarks,
						arj_gds_fare_code,


						int_sys_action_id,
						fk_created_user_id,
						dtm_created)
                            VALUES(jsnAirFareType->>'strAirFareTypeCode',
						jsnAirFareType->>'strAirFareType',
						CAST(jsnAirFareType->>'strRemarks' AS TEXT),
						STRING_TO_ARRAY(jsnAirFareType->>'strDbAdsFareCode', ',')::JSON[],


						0,
						CAST(jsnAirFareType->>'binCreatedUserId' AS BIGINT),
						dtmCurrent) RETURNING pk_air_fare_type_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('airticket.tbl_air_fare_type'::VARCHAR, 'pk_air_fare_type_id'::VARCHAR, CAST(jsnAirFareType->>'binAirFareTypeId' AS BIGINT)::BIGINT, CAST(jsnAirFareType->>'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;

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

                        UPDATE airticket.tbl_air_fare_type SET vhr_air_fare_type_code = jsnAirFareType->>'strAirFareTypeCode',
						vhr_air_fare_type = jsnAirFareType->>'strAirFareType',
						txt_remarks = jsnAirFareType->>'strRemarks',
						arj_gds_fare_code = STRING_TO_ARRAY(jsnAirFareType->>'strDbAdsFareCode', ',')::JSON[],


						int_sys_action_id = int_sys_action_id + 1,
						fk_last_modified_user_id = CAST(jsnAirFareType->>'binLastModifiedUserId' AS BIGINT),
						dtm_last_modified = dtmCurrent 
                        WHERE pk_air_fare_type_id = CAST(jsnAirFareType->>'binAirFareTypeId' AS BIGINT);
                    END IF;

                    --COMMIT;
                    objSpOutRes.sinSqlCustomStatus := 1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'SUCCESS';
                    objSpOutRes.strSqlCustomMessage := 'AirFareType 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 := 'AirFareType 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_air_fare_type(binAirFareTypeId 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_air_fare_type'::VARCHAR, 'pk_air_fare_type_id'::VARCHAR, binAirFareTypeId::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('airticket.tbl_air_fare_type'::VARCHAR, 'pk_air_fare_type_id'::VARCHAR, CAST(binAirFareTypeId 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 airticket.tbl_air_fare_type SET int_sys_action_id = -1::INT,
                                fk_deleted_user_id = binDeletedUserId,
                                dtm_deleted = dtmCurrent
                        WHERE pk_air_fare_type_id = binAirFareTypeId;

                    --COMMIT;
                    objSpOutRes.sinSqlCustomStatus := 1::SMALLINT;
                    objSpOutRes.strSqlCustomStatusCode := 'SUCCESS';
                    objSpOutRes.strSqlCustomMessage := 'AirFareType 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 := 'AirFareType 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;
        

