Delimiter //
/**
 * Author:  armen
 * Created: 7 juil. 2016
 * @param accountId identifiant du compte à supprimer 
 * @param useTransaction flag indiquant si 'il faut gerer la transaction
 *   - 1 : gerer les transaction
 *   - 0 : ne pas gérer la transaction. Implique que la transaction est gérée en externe 
 * @param tmpTmTableNameOfAccToDel nom de la table temporaire contentant les comptes à supprimer
 *   - n'est pris compte que si useTransaction = 0 (gestion des transaction externe)
 *     et si accountId = 0 (ne correspond à aucun compte car l'auto icrement des comptes
 *     commence à 1)
 * la table temporaire doit avoir une colonne ACCOUNTS_ID_ACCOUNTS
 * la table ne dois pas s'appeler TMP_ACCOUNTS_TO_DELETE
 */
Drop Procedure If Exists BUSINESS_DELETE_ACCOUNTS //
Create Procedure BUSINESS_DELETE_ACCOUNTS(
    In accountId int    
    , In useTransaction TinyInt
    , In tmpTmTableNameOfAccToDel Varchar(50)
)
Begin
    bdelacc_main : Begin
        Declare transactionStarted int default 0;
        
        set @bdelacc_accountId = Coalesce(accountId, 0);
        set @bdelacc_useTransaction = Coalesce(useTransaction, 1);
        set @bdelacc_tmpTmTableNameOfAccToDel = trim(Coalesce(tmpTmTableNameOfAccToDel, ''));

        bdelacc_singleOrMultipleDelete: Begin
            Drop Temporary Table If Exists TMP_ACCOUNTS_TO_DELETE;
            Create Temporary Table TMP_ACCOUNTS_TO_DELETE (
                ID_ACCOUNTS_TO_DEL Int Unsigned Not Null Primary Key
            );



            bdelacc_multipleDelete: Begin
                -- est ce une supression mutliple ou simple ?
                -- on test 
                --  - si la transaction est extene (useTransaction)
                --  - si l'id compte vaut 0 (ne correponspond à aucun comptes 0)
                --  - si tmpTmTableNameOfAccToDel  est fourni

               
                if @bdelacc_accountId <> 0 Or @bdelacc_useTransaction = 1 Or @bdelacc_tmpTmTableNameOfAccToDel = '' Then
                    leave bdelacc_multipleDelete;
                End If;
                bdelacc_multipleDelete_copyaccounts: Begin
                    -- on test la copie des comptes depuis la table temporaire indiquée
                    -- en cas d'erreur on propage l'exeception
                    -- autre solution à affiner on fait une pseudo copie
-- 
--                     Declare Continue Handler for SQLEXCEPTION 
                    Declare Continue Handler For SQLSTATE '42S02'
--                     Declare Exit Handler For Sqlexception 
--                     Declare Exit Handler For SQLSTATE '42S02'
                    Begin
--                         If transactionStarted  <> 0 then
--                             rollback;
--                             set transactionStarted  = 0;
--                         End if;
--                         leave bdelacc_multipleDelete_copyentries;
                        Resignal;
                    End ;
                    Set @bdelacc_copyQuery = Concat(
                                'Insert Into TMP_ACCOUNTS_TO_DELETE (ID_ACCOUNTS_TO_DEL) '
                                , 'Select ACCOUNTS_ID_ACCOUNTS From ' 
                                ,  @bdelacc_tmpTmTableNameOfAccToDel
                            )
                        ;
                    PREPARE bdelacc_copyaccountStmt FROM @bdelacc_copyQuery; 
                    execute bdelacc_copyaccountStmt 
                        ;
                    Deallocate prepare bdelacc_copyaccountStmt; 		

                End ;

            end ;

        End ;


        bdelacc_deletionMain : Begin

            Declare Exit Handler For Sqlexception
            Begin
                If transactionStarted  <> 0 then
                    rollback;
                    set transactionStarted  = 0;
                End if;
                Resignal;
            End;

            Set useTransaction = Coalesce(useTransaction, 1);
            If useTransaction <> 0 Then
                Start Transaction;
                Set transactionStarted  = 1;
            End If;

--             Set accountId = Coalesce(accountId, 0);
--             If accountId <> 0 Then
--                 Drop Temporary Table If Exists TMP_ACCOUNTS_TO_DELETE;
--                 Create Temporary Table TMP_ACCOUNTS_TO_DELETE (
--                     ID_ACCOUNTS_TO_DEL Int Unsigned Not Null Primary Key
--                 )
--                 Select ID_ACCOUNTS ID_ACCOUNTS_TO_DEL
--                 From ACCOUNTS
--                 Where 
--                     ID_ACCOUNTS = accountId
--                 ;
--             End If;
            bdelacc_singleDelete_copyaccounts: Begin
                -- en mode multiple @bdelacc_accountId = 0
                -- donc pas de doublons d'entries
                Insert Into TMP_ACCOUNTS_TO_DELETE (ID_ACCOUNTS_TO_DEL)
                Select ID_ACCOUNTS ID_ACCOUNTS_TO_DEL
                From ACCOUNTS 
                Where 
                    ID_ACCOUNTS = @bdelacc_accountId
                ;
            End ;

            -- migration des reclamation
            entriesUpdtReclaim: Begin
                -- cas particulier des réclamations :
                -- écriture appartenant à un compte lié (qui peux avoir été délié entre-temps !!! )
                -- mais la réclamation à  été posé depuis le compte de consolidation
                -- si le compte principal est à supprimer
                -- on doit supprimer les reclaim detail des écritures du compte de conso (perm att = 1)
                -- mais aussi les reclam associé au compte
                -- or dans ce cas la reclaim est supprimée ( car associé au compte de conso)
                -- alors que le reclaim detail ne l'es pas -> violation de contrainte d'intégrité
                -- correction migration des reclaim de compte de conso à supprimer vers les sous-compte des écritures
                Drop Temporary Table If Exists TMP_RECLAIM_MOVE;
                Create Temporary Table TMP_RECLAIM_MOVE (
                            ACCOUNTS_ID_ACCOUNTS Int Unsigned Not Null
                            , RECLAIMS_ID_RECLAIMS Int Unsigned Not Null
                            , TAR_ACCOUNTS_ID_ACCOUNTS Int Unsigned Null
#                             , TAR_ACCOUNTS_ID_ACCOUNTS Int Unsigned Not Null default 0
                            , Index(ACCOUNTS_ID_ACCOUNTS)
                            , Index(RECLAIMS_ID_RECLAIMS)
#                             , Index(TAR_ACCOUNTS_ID_ACCOUNTS)
                        )
                    ;
                Insert Into TMP_RECLAIM_MOVE (
                        ACCOUNTS_ID_ACCOUNTS, RECLAIMS_ID_RECLAIMS, TAR_ACCOUNTS_ID_ACCOUNTS
                    )
                Select
                    tar.ACCOUNTS_ID_ACCOUNTS, tar.ID_RECLAIMS
                    , (
                        Select
                            eha_attach.ACCOUNTS_ID_ACCOUNTS
                        From
                            RECLAIMS_DETAILS rd_attach
                                Inner Join ENTRIES_has_ACCOUNTS eha_attach
                                           On rd_attach.ENTRIES_ID_ENTRIES = eha_attach.ENTRIES_ID_ENTRIES
                                               And eha_attach.ENTRIES_PERMANENT_ATTACHEMENT = 1
                        Where
                                rd_attach.RECLAIMS_ID_RECLAIMS = tar.ID_RECLAIMS
                        Limit 1
                    )
                from
                    TMP_ACCOUNTS_TO_DELETE selacc
                        Inner Join RECLAIMS tar
                                   On selacc.ID_ACCOUNTS_TO_DEL = tar.ACCOUNTS_ID_ACCOUNTS
                -- ci dessous ne pas applique le where pour corriger en même temps des reclamation sur des compte dèliés
#                 Where
#                     tar.ID_RECLAIMS In (
#                         Select rd_conso.RECLAIMS_ID_RECLAIMS
#                         From
#                             RECLAIMS_DETAILS rd_conso
#                             Inner Join ENTRIES_has_ACCOUNTS eha_conso
#                                 On rd_conso.ENTRIES_ID_ENTRIES = eha_conso.ENTRIES_ID_ENTRIES
#                                 And eha_conso.ENTRIES_PERMANENT_ATTACHEMENT = 0
#                         Where
#                             rd_conso.RECLAIMS_ID_RECLAIMS = tar.ID_RECLAIMS
# --                             And eha_conso.ACCOUNTS_ID_ACCOUNTS = tar.ACCOUNTS_ID_ACCOUNTS
#                     )
                ;
                Update RECLAIMS tar
                Inner join TMP_RECLAIM_MOVE src
                    On tar.ID_RECLAIMS = src.RECLAIMS_ID_RECLAIMS
                Set tar.ACCOUNTS_ID_ACCOUNTS = src.TAR_ACCOUNTS_ID_ACCOUNTS
                Where src.TAR_ACCOUNTS_ID_ACCOUNTS Is Not Null
                ;

                Delete tar
                From SUMMARIES tar
                Inner join TMP_RECLAIM_MOVE src
                    On tar.RECLAIMS_ID_RECLAIMS = src.RECLAIMS_ID_RECLAIMS
                Where src.TAR_ACCOUNTS_ID_ACCOUNTS Is Null
                ;

                Delete tar
                From RECLAIMS_DETAILS tar
                Inner join TMP_RECLAIM_MOVE src
                    On tar.RECLAIMS_ID_RECLAIMS = src.RECLAIMS_ID_RECLAIMS
                Where src.TAR_ACCOUNTS_ID_ACCOUNTS Is Null
                ;
                Delete tar
                From RECLAIMS tar
                Inner join TMP_RECLAIM_MOVE src
                    On tar.ID_RECLAIMS = src.RECLAIMS_ID_RECLAIMS
                Where src.TAR_ACCOUNTS_ID_ACCOUNTS Is Null
                ;

            End;
            -- supression des ecritures
            entriesDelMain: Begin 
                Drop Temporary Table If Exists TMP_DEL_ACC_DEL_ENTRIES;
                Create Temporary Table TMP_DEL_ACC_DEL_ENTRIES
                    AS
                Select
                    EHA.ENTRIES_ID_ENTRIES
                From
                    TMP_ACCOUNTS_TO_DELETE TAD
                    Inner Join ENTRIES_has_ACCOUNTS EHA
                        On TAD.ID_ACCOUNTS_TO_DEL = EHA.ACCOUNTS_ID_ACCOUNTS
                Where
                    EHA.ENTRIES_PERMANENT_ATTACHEMENT = 1
                ;
                Call BUSINESS_DELETE_ENTRIES(0, 0, 'TMP_DEL_ACC_DEL_ENTRIES');

        -- 
        -- 		Declare idEntries Int Unsigned;
        -- 		Declare entriesCursor Cursor For
        -- 			Select
        -- 				EHA.ENTRIES_ID_ENTRIES
        -- 			From
        -- 				TMP_ACCOUNTS_TO_DELETE TAD
        -- 				Inner Join ENTRIES_has_ACCOUNTS EHA
        -- 					On TAD.ID_ACCOUNTS_TO_DEL = EHA.ACCOUNTS_ID_ACCOUNTS
        -- 			;
        -- 		Open entriesCursor;
        -- 		entriesDel: Begin
        -- 			Declare Exit Handler for NOT FOUND 
        --                 Begin
        --                     Close entriesCursor;
        --                 End ;
        -- 			entriesDelLoop: Loop
        -- 				fetch entriesCursor Into idEntries;
        -- 				set @idEntries = idEntries;
        -- 				
        -- 				set @query = 'Call BUSINESS_DELETE_ENTRIES(?, 0, null)';
        -- 		        PREPARE entriesDelPSCall FROM @query; 
        -- 				execute entriesDelPSCall 
        -- 						using @idEntries
        -- 					;
        -- 				Deallocate prepare entriesDelPSCall; 			
        -- 			End loop entriesDelLoop;
        -- 		End ;

            End ;

            Delete TAR
            From
                ACCOUNTS_COMPUTED_DATA TAR
                Inner Join TMP_ACCOUNTS_TO_DELETE
                    On ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                ACC_REF_DATAS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;
            Delete TAR
            From
                ACC_REF_DATAS_CUST TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join ACCOUNTS_has_ATTACHED_DOCUMENT TAR
                    On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;


            Delete TAR
            From  ACCOUNTS_has_ACCOUNTS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS_LINK = ID_ACCOUNTS_TO_DEL
            ;
            Delete TAR
            From  ACCOUNTS_has_ACCOUNTS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete tar 
            From ENTRIES_has_ACCOUNTS tar
                Inner Join TMP_ACCOUNTS_TO_DELETE TAD
                    On tar.ACCOUNTS_ID_ACCOUNTS = TAD.ID_ACCOUNTS_TO_DEL
            Where
                tar.ENTRIES_PERMANENT_ATTACHEMENT = 0
            ;

            Delete TAR
            From  FI_ANA TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;
            Delete TAR
            From  FI_ANA_HISTO TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                ACCOUNTS_has_DF_PENALTIES_RULES TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                SUMMARIES TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                ACCOUNTS_has_TASKS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;



            Delete RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join TASKS
                    On EVENTS.ID_EVENTS = TASKS.EVENTS_ID_EVENTS
                Inner Join TASKS_RECIPIENTS
                    On TASKS.ID_TASKS = TASKS_RECIPIENTS.TASKS_ID_TASKS
                Inner Join RECIPIENTS 
                    On TASKS_RECIPIENTS.RECIPIENTS_ID_RECIPIENTS = RECIPIENTS.ID_RECIPIENTS
            ;
            Delete TASKS_RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join TASKS
                    On EVENTS.ID_EVENTS = TASKS.EVENTS_ID_EVENTS
                Inner Join TASKS_RECIPIENTS
                    On TASKS.ID_TASKS = TASKS_RECIPIENTS.TASKS_ID_TASKS
            ;
            Delete TASKS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join TASKS
                    On EVENTS.ID_EVENTS = TASKS.EVENTS_ID_EVENTS
            ;
            Delete TASKS_PLANS_CONT
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join TASKS_PLANS_CONT
                    On EVENTS.ID_EVENTS = TASKS_PLANS_CONT.EVENTS_ID_EVENTS
            ;
            Delete TASKS_PLANS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join TASKS_PLANS
                    On SRC.ID_ACCOUNTS_TO_DEL = TASKS_PLANS.ACCOUNTS_ID_ACCOUNTS
            ;

            Delete RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join CORRESPONDENCES
                    On EVENTS.ID_EVENTS = CORRESPONDENCES.EVENTS_ID_EVENTS
                Inner Join CORRESPONDENCES_RECIPIENTS
                    On CORRESPONDENCES.ID_CORRESPONDENCES = CORRESPONDENCES_RECIPIENTS.CORRESPONDENCES_ID_CORRESPONDENCES
                Inner Join RECIPIENTS 
                    On CORRESPONDENCES_RECIPIENTS.RECIPIENTS_ID_RECIPIENTS = RECIPIENTS.ID_RECIPIENTS
            ;
            Delete CORRESPONDENCES_RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join CORRESPONDENCES
                    On EVENTS.ID_EVENTS = CORRESPONDENCES.EVENTS_ID_EVENTS
                Inner Join CORRESPONDENCES_RECIPIENTS
                    On CORRESPONDENCES.ID_CORRESPONDENCES = CORRESPONDENCES_RECIPIENTS.CORRESPONDENCES_ID_CORRESPONDENCES
            ;
            Delete CORRESPONDENCES
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
                Inner Join CORRESPONDENCES
                    On EVENTS.ID_EVENTS = CORRESPONDENCES.EVENTS_ID_EVENTS
            ;
            Delete EVENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join EVENTS
                    On SRC.ID_ACCOUNTS_TO_DEL = EVENTS.ACCOUNTS_ID_ACCOUNTS
            ;

        bad_messages : Begin
            Delete MESSAGES_STATUS_LOGS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join MESSAGES
                    On SRC.ID_ACCOUNTS_TO_DEL = MESSAGES.ACCOUNTS_ID_ACCOUNTS
                Inner Join MESSAGES_RECIPIENTS
                    On MESSAGES_RECIPIENTS.MESSAGES_ID_MESSAGES = MESSAGES.ID_MESSAGES
                Inner Join MESSAGES_STATUS_LOGS
                    On MESSAGES_STATUS_LOGS.MESSAGES_RECIPIENTS_ID_MESSAGES_RECIPIENTS = MESSAGES_RECIPIENTS.ID_MESSAGES_RECIPIENTS
            ;

            Delete MESSAGES_RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join MESSAGES
                    On SRC.ID_ACCOUNTS_TO_DEL = MESSAGES.ACCOUNTS_ID_ACCOUNTS
                Inner Join MESSAGES_RECIPIENTS
                    On MESSAGES_RECIPIENTS.MESSAGES_ID_MESSAGES = MESSAGES.ID_MESSAGES
            ;

            Delete MESSAGES_has_ATTACHED_DOC
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join MESSAGES
                    On SRC.ID_ACCOUNTS_TO_DEL = MESSAGES.ACCOUNTS_ID_ACCOUNTS
                Inner Join MESSAGES_has_ATTACHED_DOC
                    On MESSAGES_has_ATTACHED_DOC.MESSAGES_ID_MESSAGES = MESSAGES.ID_MESSAGES
            ;

            Delete MESSAGES
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join MESSAGES
                    On SRC.ID_ACCOUNTS_TO_DEL = MESSAGES.ACCOUNTS_ID_ACCOUNTS
            ;
        
        End;

        bad_summaries: Begin
            Delete SUMMARIES_RECIPIENTS
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join  SUMMARIES
                    On SUMMARIES.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
                Inner Join SUMMARIES_RECIPIENTS
                    On SUMMARIES.ID_SUMMARIES = SUMMARIES_RECIPIENTS.SUMMARIES_ID_SUMMARIES
            ;
            Delete SUMMARIES
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join  SUMMARIES
                    On SUMMARIES.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;
        End ;

            Delete TAR
            From
                RECLAIMS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;


            Delete TAR
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join PAYMENT_SCHEDULES TAR
                    On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;
            -- supression des contacts

            -- effacement de la notion de main contacts
            Update 
                ACCOUNTS TAR
                Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            Set 
                TAR.CONTACTS_ID_CONTACTS = null
            ;

            -- table temporaire avec la liste des contact à supprimer
            Drop Temporary Table If Exists TMP_ACCOUNTS_CONTACTS_TO_DEL;
            Create Temporary Table TMP_ACCOUNTS_CONTACTS_TO_DEL (
                CONTACTS_ID_CONTACTS Int Unsigned Not Null
                , Index (CONTACTS_ID_CONTACTS )
            );
            Insert Into TMP_ACCOUNTS_CONTACTS_TO_DEL(
                    CONTACTS_ID_CONTACTS
                )
            Select CONTACTS.ID_CONTACTS
            From TMP_ACCOUNTS_TO_DELETE SRC
            Inner Join CONTACTS 
                On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;
            Call BUSINESS_DELETE_CONTACTS(0, 0, 'TMP_ACCOUNTS_CONTACTS_TO_DEL');
-- 
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_ADDRESSES TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_PHONES TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_EMAILS TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_PHONES TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_RIB_IBAN TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_AVAILABLE TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
-- 
-- 
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_BUSINESS TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS
--                 On CONTACTS.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             Inner Join CONTACTS_INDIVIDUAL TAR
--                 On TAR.CONTACTS_ID_CONTACTS = CONTACTS.ID_CONTACTS
--             ;
--             Delete TAR
--             From TMP_ACCOUNTS_TO_DELETE SRC
--             Inner Join CONTACTS TAR
--                 On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
--             ;


            Delete TAR
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join GUARANTEED TAR
                    On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                TMP_ACCOUNTS_TO_DELETE SRC
                Inner Join CREDIT_LIMIT TAR
                    On TAR.ACCOUNTS_ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            Delete TAR
            From
                ACCOUNTS TAR
            Inner Join TMP_ACCOUNTS_TO_DELETE SRC
                        On TAR.ID_ACCOUNTS = ID_ACCOUNTS_TO_DEL
            ;

            If transactionStarted <> 0 Then
                Commit ;
            End If;
        End ;
    End ;
End //
Delimiter ;