DECLARE @f_menu_id NUMERIC(18,0)
DECLARE @f_menu_seq NUMERIC(18,0)

BEGIN
    BEGIN TRANSACTION

    BEGIN TRY

    ------------------------------------------------------------------------
    -- 1. Work Order - More - WO Misc
    ------------------------------------------------------------------------
    IF NOT EXISTS (
        SELECT 1
        FROM cf_menu
        WHERE object_name = 'o_wo_misc'
          AND program_category = 'MBL'
          AND object_type = 'M'
    )
    BEGIN
        SELECT @f_menu_seq = ISNULL(MAX(cf_menu_seq), 0) + 1
        FROM cf_menu
        WHERE program_category = 'MBL'
          AND object_type = 'M'

        INSERT INTO cf_menu (
            language_cd, object_name, object_descs, object_type, program_category,
            ico_name, remarks, cf_menu_seq, audit_column, custom_flag,
            external_program, access_audit, mobile_object_type,
            audit_user, audit_date, column1, column2, column3, column4, column5
        )
        VALUES (
            'DEFAULT','o_wo_misc','Work Order - More - WO Misc','M','MBL',
            '(Default)',NULL,@f_menu_seq,'0','0',
            '0','0','O',
            'admin',GETDATE(),'Work Order',NULL,NULL,NULL,NULL
        )

        SET @f_menu_id = SCOPE_IDENTITY()

        INSERT INTO cf_user_access (
            site_cd, empl_id, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag
        )
        SELECT site_cd, empl_id, @f_menu_id, 1,1,1,1,1,1
        FROM cf_site_user

        INSERT INTO cf_menu_template_access (
            template_cd, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag, audit_user, audit_date
        )
        SELECT template_cd, @f_menu_id, 1,1,1,1,1,1, 'admin', GETDATE()
        FROM cf_menu_template
    END


    ------------------------------------------------------------------------
    -- 2. Asset - More - Asset PM List
    ------------------------------------------------------------------------
    IF NOT EXISTS (
        SELECT 1
        FROM cf_menu
        WHERE object_name = 'o_asset_pm_list'
          AND program_category = 'MBL'
          AND object_type = 'M'
    )
    BEGIN
        SELECT @f_menu_seq = ISNULL(MAX(cf_menu_seq), 0) + 1
        FROM cf_menu
        WHERE program_category = 'MBL'
          AND object_type = 'M'

        INSERT INTO cf_menu (
            language_cd, object_name, object_descs, object_type, program_category,
            ico_name, remarks, cf_menu_seq, audit_column, custom_flag,
            external_program, access_audit, mobile_object_type,
            audit_user, audit_date, column1, column2, column3, column4, column5
        )
        VALUES (
            'DEFAULT','o_asset_pm_list','Asset - More - Asset PM List','M','MBL',
            '(Default)',NULL,@f_menu_seq,'0','0',
            '0','0','O',
            'admin',GETDATE(),'Asset',NULL,NULL,NULL,NULL
        )

        SET @f_menu_id = SCOPE_IDENTITY()

        INSERT INTO cf_user_access (
            site_cd, empl_id, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag
        )
        SELECT site_cd, empl_id, @f_menu_id, 1,1,1,1,1,1
        FROM cf_site_user

        INSERT INTO cf_menu_template_access (
            template_cd, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag, audit_user, audit_date
        )
        SELECT template_cd, @f_menu_id, 1,1,1,1,1,1, 'admin', GETDATE()
        FROM cf_menu_template
    END

    ------------------------------------------------------------------------
    -- 3. Asset - More - Asset MR History
    ------------------------------------------------------------------------
    IF NOT EXISTS (
        SELECT 1
        FROM cf_menu
        WHERE object_name = 'o_asset_mr_history'
          AND program_category = 'MBL'
          AND object_type = 'M'
    )
    BEGIN
        SELECT @f_menu_seq = ISNULL(MAX(cf_menu_seq), 0) + 1
        FROM cf_menu
        WHERE program_category = 'MBL'
          AND object_type = 'M'

        INSERT INTO cf_menu (
            language_cd, object_name, object_descs, object_type, program_category,
            ico_name, remarks, cf_menu_seq, audit_column, custom_flag,
            external_program, access_audit, mobile_object_type,
            audit_user, audit_date, column1, column2, column3, column4, column5
        )
        VALUES (
            'DEFAULT','o_asset_mr_history','Asset - More - Asset MR History','M','MBL',
            '(Default)',NULL,@f_menu_seq,'0','0',
            '0','0','O',
            'admin',GETDATE(),'Asset',NULL,NULL,NULL,NULL
        )

        SET @f_menu_id = SCOPE_IDENTITY()

        INSERT INTO cf_user_access (
            site_cd, empl_id, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag
        )
        SELECT site_cd, empl_id, @f_menu_id, 1,1,1,1,1,1
        FROM cf_site_user

        INSERT INTO cf_menu_template_access (
            template_cd, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag, audit_user, audit_date
        )
        SELECT template_cd, @f_menu_id, 1,1,1,1,1,1, 'admin', GETDATE()
        FROM cf_menu_template
    END


    ------------------------------------------------------------------------
    -- 4. Asset - More - Asset Relocation History
    ------------------------------------------------------------------------
    IF NOT EXISTS (
        SELECT 1
        FROM cf_menu
        WHERE object_name = 'o_asset_relocation_history'
          AND program_category = 'MBL'
          AND object_type = 'M'
    )
    BEGIN
        SELECT @f_menu_seq = ISNULL(MAX(cf_menu_seq), 0) + 1
        FROM cf_menu
        WHERE program_category = 'MBL'
          AND object_type = 'M'

        INSERT INTO cf_menu (
            language_cd, object_name, object_descs, object_type, program_category,
            ico_name, remarks, cf_menu_seq, audit_column, custom_flag,
            external_program, access_audit, mobile_object_type,
            audit_user, audit_date, column1, column2, column3, column4, column5
        )
        VALUES (
            'DEFAULT','o_asset_relocation_history','Asset - More - Asset Relocation History','M','MBL',
            '(Default)',NULL,@f_menu_seq,'0','0',
            '0','0','O',
            'admin',GETDATE(),'Asset',NULL,NULL,NULL,NULL
        )

        SET @f_menu_id = SCOPE_IDENTITY()

        INSERT INTO cf_user_access (
            site_cd, empl_id, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag
        )
        SELECT site_cd, empl_id, @f_menu_id, 1,1,1,1,1,1
        FROM cf_site_user

        INSERT INTO cf_menu_template_access (
            template_cd, menuid, exe_flag, view_flag, new_flag, edit_flag, del_flag, print_flag, audit_user, audit_date
        )
        SELECT template_cd, @f_menu_id, 1,1,1,1,1,1, 'admin', GETDATE()
        FROM cf_menu_template
    END
    
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION

        SELECT
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_MESSAGE() AS ErrorMessage
    END CATCH
END
