Delimiter //
/**
 * Author:  armen
 * Created: 20 juil. 2020
 */
Drop Procedure if exists LOADER_CREDITPROVIDERS_COPY//
Create Procedure LOADER_CREDITPROVIDERS_COPY(
    In batchnum int
)
Begin
    lcpc_main: Begin
        Call LOADER_CREDITPROVIDERS_CREDITSPROVIDERS_UPDATE(batchnum);

        Call CASH_AUTOINCREMENT_VALUE('CREDITS_PROVIDERS', @lastCREDITSPROVIDERSId);
        Call LOADER_CREDITPROVIDERS_CREDITSPROVIDERS_INSERT(batchnum, @lastCREDITSPROVIDERSId);

        if batchnum = 0 Then
            leave lcpc_main;
        End if; 

        Set @lcpc_max_batch = GET_LOADER_INFO('MAX_BATCH', 1);
        if batchnum = @lcpc_max_batch Then
            Set @lcpc_entity = GET_LOADER_INFO('ID_ENTITIES', 1);
            call BUSINESS_CREDITSPROVIDERS_KEEP_1_UNEXPIRED_BY_REF(null, @lcpc_entity, null, 0);
        End if;
    End;

End //
Delimiter ;
