project QadFinancials > class BFRWChart > method GetAllChartCOACrossRef

Description

This method will get all COA Cross Ref for chart entity


Parameters


icCompanyIdListinputcharacter
tChartCOACrossRefinput-outputtemp-table
iiTargetGLSharedSetIdinputinteger
iiTargetDivSharedSetIdinputinteger
iiTargetCCSharedSetIdinputinteger
iiTargetPrjSharedSetIdinputinteger
icTargetGLSetinputcharacter
icTargetDivSetinputcharacter
icTargetCCSetinputcharacter
icTargetPrjSetinputcharacter
icFRWChartCodeinputcharacter
iiTargetDomainIdinputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program1/bfrwchart.p)

/**************************************************************************/
/* This method will get all COA Cross Ref for chart entity                */
/**************************************************************************/
MAINBLOCK:
do on error undo, throw:
    if icCompanyIdList = ? or icCompanyIdList = "" 
    then do:
        empty temp-table tChartCOACrossRef.
        leave MAINBLOCK.
    end.

    <M-22 run InitializeFRWXref  (output viFcReturnSuper (oiReturnStatus)) in BFRWChart>
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0) then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave MAINBLOCK.           
    
    for each tChartCOACrossRef:
        assign vlIsValidCompany = no.
        do viCounter = 1 to num-entries(icCompanyIdList):
            if tChartCOACrossRef.tiCompanyId = integer(entry(viCounter, icCompanyIdList))
            then do:
                assign vlIsValidCompany = yes.
                leave.
            end.
        end. /*  do viCounter = 1 */
       if not vlIsValidCompany
       then delete tChartCOACrossRef.
    end. /*  for each tChartCOACrossRef */

    /* Create tChartCOACrossRef for each input company */
    do viCounter = 1 to num-entries(icCompanyIdList):
        assign viCompanyIdTemp = integer(entry(viCounter, icCompanyIdList))
               vcAllSets = "":U.
            <Q-12 run CompanyByDomain (all) (Read) (NoCache)
               (input viCompanyIdTemp, (CompanyId)
                input ?, (DomainId)
                input ?, (Active)
                output dataset tqCompanyByDomain) in BCompany>
            for first tqCompanyByDomain:
                assign viSourceDomainID = tqCompanyByDomain.tiDomain_ID
                       vcSourceDomainCode = tqCompanyByDomain.tcDomainCode.
            end.
            assign viSourceGLSharedSetId = 0
                   viSourceDivSharedSetId = 0
                   viSourceCCSharedSetId = 0
                   viSourcePrjSharedSetId = 0.
            /* Retrieve the shared sets for the target company */                
            <Q-7 run SelectCompanySharedSet (all) (Read) (NoCache)
                   (input viCompanyIdTemp, (CompanyId)
                    output dataset tqSelectCompanySharedSet) in BCompany>
            find first tqSelectCompanySharedSet where tqSelectCompanySharedSet.tcSharedSetTypeCode = {&SHAREDSETTYPE-GL} no-error.
            if available tqSelectCompanySharedSet
            then do:
                find first tChartCOACrossRef where tChartCOACrossRef.tiCompanyId = viCompanyIdTemp and
                                                   tChartCOACrossRef.tcSharedSetType = {&SHAREDSETTYPE-GL} no-error. 
                if not available tChartCOACrossRef   
                then do:  
                    create tChartCOACrossRef.
                    assign tChartCOACrossRef.tiCompanyId = viCompanyIdTemp
                           tChartCOACrossRef.tcCompanyCode = tqSelectCompanySharedSet.tcCompanyCode
                           tChartCOACrossRef.tcCompanyDesc = tqSelectCompanySharedSet.tcCompanyDescription
                           tChartCOACrossRef.tcSharedSetType = tqSelectCompanySharedSet.tcSharedSetTypeCode
                           tChartCOACrossRef.tcSourceSharedSetCode = tqSelectCompanySharedSet.tcSharedSetCode
                           tChartCOACrossRef.tiSourceSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                           tChartCOACrossRef.tiDomainID = viSourceDomainID.
                end.                    
                assign vcAllSets = vcAllSets + string(tqSelectCompanySharedSet.tiSharedSet_ID,"9999999999")
                       viSourceGLSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                       vlIsExistingCOACrossRef = no.
                if tChartCOACrossRef.tcCOACrossRefCode <> ? and tChartCOACrossRef.tcCOACrossRefCode <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
                then do:
                    <Q-30 run COACrossRefPrim (all) (Read) (NoCache)
                        (input ?, (COACrossRefID)
                         input ?, (SourceDomainID)
                         input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                         output dataset tqCOACrossRefPrim) in BCOACrossRef>
                    if can-find (first tqCOACrossRefPrim) then assign vlIsExistingCOACrossRef = yes.
                    if not vlIsExistingCOACrossRef
                    then do:
                        if tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(tChartCOACrossRef.tcTargetSharedSetCode,1,9,"CHARACTER":U)  
                        then assign vlIsExistingCOACrossRef = yes.
                    end.
                end.
                if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef
                then do:
                    if iiTargetGLSharedSetId <> ? and iiTargetGLSharedSetId <> 0
                    then do:   
                        if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetGLSharedSetId 
                        then do:
                            if iiTargetGLSharedSetId = tChartCOACrossRef.tiSourceSharedSetId then assign tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED}.
                            else do:
                                /* we lookup if there is already a tFRWXref (extention of COACrossRef) */
                                find first tFRWXref where tFRWXref.tcFRWCrossRefCode = {&COACROSSREFTYPE-SEPERATE} and
                                                          tFRWXref.tcFRWCrossRefCOAType = {&SHAREDSETTYPE-GL} and
                                                          tFRWXref.tiSourceGLSharedSet_ID = tChartCOACrossRef.tiSourceSharedSetId and
                                                          tFRWXref.tiTargetGLSharedSet_ID = iiTargetGLSharedSetId no-error.
                                if available tFRWXref
                                then do:
                                    /* now there must be a corresponding COACrossRef. It can be deleted, because the standard
                                       COA Cross Ref delete is not checking a link with tFRWXref. In the case the COACrossRef
                                       does not exist anymore, then also the tFRWXref should be deleted.  If the COA Cross Ref
                                       still exists, then the COACrossRefCode can be changed in the standard program,
                                       so we update the FRWGLXRefCode */
                                       <Q-77 run COACrossRefPrim (first) (Read) (NoCache)
                                          (input tFRWXRef.tiCOACrossRef_ID, (COACrossRefID)
                                           input ?, (SourceDomainID)
                                           input ?, (COACrossRefCode)
                                           output dataset tqCOACrossRefPrim) in BCOACrossRef>
                                    find first tqCOACrossRefPrim no-error.
                                    if available tqCOACrossRefPrim then 
                                    assign tChartCOACrossRef.tcCOACrossRefCode = tqCOACrossRefPrim.tcCOACrossRefCode.
                                    else do:
                                        assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetGLSet,1,9,"CHARACTER":U).
                                        delete tFRWXref.
                                    end.
                                end. /* if available tFRWXref */
                                else assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetGLSet,1,9,"CHARACTER":U).
                            end. /* else do */
                        end. /* if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetGLSharedSetId */
                    end. /* if iiTargetGLSharedSetId <> ? and iiTargetGLSharedSetId <> 0 */
                    else assign tChartCOACrossRef.tcCOACrossRefCode = "".
                end. /* if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or not vlIsExistingCOA */
                assign tChartCOACrossRef.tcTargetSharedSetCode = icTargetGLSet
                       tChartCOACrossRef.tiTargetSharedSetId = iiTargetGLSharedSetId.
            end. /* if available tqSelectCompanySharedSet */
            
            find first tqSelectCompanySharedSet where tqSelectCompanySharedSet.tcSharedSetTypeCode = {&SHAREDSETTYPE-DIVISION} no-error.
            if available tqSelectCompanySharedSet
            then do:
                find first tChartCOACrossRef where tChartCOACrossRef.tiCompanyId = viCompanyIdTemp and
                                                   tChartCOACrossRef.tcSharedSetType = {&SHAREDSETTYPE-DIVISION} no-error. 
                if not available tChartCOACrossRef   
                then do:  
                    create tChartCOACrossRef.
                    assign tChartCOACrossRef.tiCompanyId = viCompanyIdTemp
                           tChartCOACrossRef.tcCompanyCode = tqSelectCompanySharedSet.tcCompanyCode
                           tChartCOACrossRef.tcCompanyDesc = tqSelectCompanySharedSet.tcCompanyDescription
                           tChartCOACrossRef.tcSharedSetType = tqSelectCompanySharedSet.tcSharedSetTypeCode
                           tChartCOACrossRef.tcSourceSharedSetCode = tqSelectCompanySharedSet.tcSharedSetCode
                           tChartCOACrossRef.tiSourceSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                           tChartCOACrossRef.tiDomainID = viSourceDomainID.
                end.
                       
                assign vcAllSets = vcAllSets + string(tqSelectCompanySharedSet.tiSharedSet_ID,"9999999999")
                       viSourceDivSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                       vlIsExistingCOACrossRef = no.
                if tChartCOACrossRef.tcCOACrossRefCode <> ? and tChartCOACrossRef.tcCOACrossRefCode <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
                then do:
                    <Q-32 run COACrossRefPrim (all) (Read) (NoCache)
                       (input ?, (COACrossRefID)
                        input ?, (SourceDomainID)
                        input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                    if can-find (first tqCOACrossRefPrim) then assign vlIsExistingCOACrossRef = yes.
                    if not vlIsExistingCOACrossRef
                    then do:
                        if tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(tChartCOACrossRef.tcTargetSharedSetCode,1,9,"CHARACTER":U)  
                        then assign vlIsExistingCOACrossRef = yes.
                    end.
                end.
                if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef
                then do:
                    if iiTargetDivSharedSetId <> ? and iiTargetDivSharedSetId <> 0 
                    then do:
                        if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetDivSharedSetId 
                        then do:
                            if iiTargetDivSharedSetId = tChartCOACrossRef.tiSourceSharedSetId then assign tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED}.
                            else do:
                                /* we lookup if there is already a tFRWXref (extention of COACrossRef) */
                                find first tFRWXref where tFRWXref.tcFRWCrossRefCode = {&COACROSSREFTYPE-SEPERATE} and
                                                          tFRWXref.tcFRWCrossRefCOAType = {&SHAREDSETTYPE-DIVISION} and
                                                          tFRWXref.tiSourceDivSharedSet_ID = tChartCOACrossRef.tiSourceSharedSetId and
                                                          tFRWXref.tiTargetDivSharedSet_ID = iiTargetDivSharedSetId no-error.
                                if available tFRWXref
                                then do:
                                    <Q-16 run COACrossRefPrim (first) (Read) (NoCache)
                                       (input tFRWXRef.tiCOACrossRef_ID, (COACrossRefID)
                                        input ?, (SourceDomainID)
                                        input ?, (COACrossRefCode)
                                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                                    find first tqCOACrossRefPrim no-error.
                                    if available tqCOACrossRefPrim then 
                                    assign tChartCOACrossRef.tcCOACrossRefCode = tqCOACrossRefPrim.tcCOACrossRefCode.
                                    else do:
                                        assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetDivSet,1,9,"CHARACTER":U).
                                        delete tFRWXref.
                                    end.
                                end. /* if available tFRWXref */
                                else assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetDivSet,1,9,"CHARACTER":U).
                            end. /* else do */
                        end. /* if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetDivSharedSetId */
                    end. /* if iiTargetDivSharedSetId <> ? and iiTargetDivSharedSetId <> 0 */
                    else assign tChartCOACrossRef.tcCOACrossRefCode = "".
                end. /* if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef */
                assign tChartCOACrossRef.tcTargetSharedSetCode = icTargetDivSet
                       tChartCOACrossRef.tiTargetSharedSetId = iiTargetDivSharedSetId.
            end.
            find first tqSelectCompanySharedSet where tqSelectCompanySharedSet.tcSharedSetTypeCode = {&SHAREDSETTYPE-COSTCENTRE} no-error.
            if available tqSelectCompanySharedSet
            then do:
                find first tChartCOACrossRef where tChartCOACrossRef.tiCompanyId = viCompanyIdTemp and
                                                   tChartCOACrossRef.tcSharedSetType = {&SHAREDSETTYPE-COSTCENTRE} no-error. 
                if not available tChartCOACrossRef   
                then do:  
                    create tChartCOACrossRef.
                    assign tChartCOACrossRef.tiCompanyId = viCompanyIdTemp
                           tChartCOACrossRef.tcCompanyCode = tqSelectCompanySharedSet.tcCompanyCode
                           tChartCOACrossRef.tcCompanyDesc = tqSelectCompanySharedSet.tcCompanyDescription
                           tChartCOACrossRef.tcSharedSetType = tqSelectCompanySharedSet.tcSharedSetTypeCode
                           tChartCOACrossRef.tcSourceSharedSetCode = tqSelectCompanySharedSet.tcSharedSetCode
                           tChartCOACrossRef.tiSourceSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                           tChartCOACrossRef.tiDomainID = viSourceDomainID.
                end.
                      
                assign vcAllSets = vcAllSets + string(tqSelectCompanySharedSet.tiSharedSet_ID,"9999999999")
                       viSourceCCSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                       vlIsExistingCOACrossRef = no.
                if tChartCOACrossRef.tcCOACrossRefCode <> ? and tChartCOACrossRef.tcCOACrossRefCode <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
                then do:
                    <Q-40 run COACrossRefPrim (all) (Read) (NoCache)
                       (input ?, (COACrossRefID)
                        input ?, (SourceDomainID)
                        input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                    if can-find (first tqCOACrossRefPrim) then assign vlIsExistingCOACrossRef = yes.
                    if not vlIsExistingCOACrossRef
                    then do:
                        if tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(tChartCOACrossRef.tcTargetSharedSetCode,1,9,"CHARACTER":U)  
                        then assign vlIsExistingCOACrossRef = yes.
                    end.                
                end.
                if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef
                then do:                    
                    if iiTargetCCSharedSetId <> ? and iiTargetCCSharedSetId <> 0 
                    then do:
                        if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetCCSharedSetId
                        then do:
                            if iiTargetCCSharedSetId = tChartCOACrossRef.tiSourceSharedSetId then assign tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED}.
                            else do:
                                /* we lookup if there is already a tFRWXref (extention of COACrossRef) */
                                find first tFRWXref where tFRWXref.tcFRWCrossRefCode = {&COACROSSREFTYPE-SEPERATE} and
                                           tFRWXRef.tcFRWCrossRefCOAType = {&SHAREDSETTYPE-COSTCENTRE} and
                                           tFRWXref.tiSourceCCSharedSet_ID = tChartCOACrossRef.tiSourceSharedSetId and
                                           tFRWXref.tiTargetCCSharedSet_ID = iiTargetccSharedSetId no-error.
                                if available tFRWXref
                                then do:
                                       <Q-20 run COACrossRefPrim (first) (Read) (NoCache)
                                          (input tFRWXRef.tiCOACrossRef_ID, (COACrossRefID)
                                           input ?, (SourceDomainID)
                                           input ?, (COACrossRefCode)
                                           output dataset tqCOACrossRefPrim) in BCOACrossRef>
                                    find first tqCOACrossRefPrim no-error.
                                    if available tqCOACrossRefPrim then 
                                    assign tChartCOACrossRef.tcCOACrossRefCode = tqCOACrossRefPrim.tcCOACrossRefCode.
                                    else do:
                                        assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetCCSet,1,9,"CHARACTER":U).
                                        delete tFRWXref.
                                    end.
                                end. /* if available tFRWXref */
                                else assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetCCSet,1,9,"CHARACTER":U).
                            end. /* else do */
                        end. /* if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetCCSharedSetId */
                    end. /* if iiTargetCCSharedSetId <> ? and iiTargetCCSharedSetId <> 0 */
                    else assign tChartCOACrossRef.tcCOACrossRefCode = "".
                end. /* if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef */
                assign tChartCOACrossRef.tcTargetSharedSetCode = icTargetCCSet
                       tChartCOACrossRef.tiTargetSharedSetId = iiTargetCCSharedSetId.
            end.
            
            find first tqSelectCompanySharedSet where tqSelectCompanySharedSet.tcSharedSetTypeCode = {&SHAREDSETTYPE-PROJECT} no-error.
            if available tqSelectCompanySharedSet
            then do:
                find first tChartCOACrossRef where tChartCOACrossRef.tiCompanyId = viCompanyIdTemp and
                                                   tChartCOACrossRef.tcSharedSetType = {&SHAREDSETTYPE-PROJECT} no-error. 
                if not available tChartCOACrossRef   
                then do:  
                    create tChartCOACrossRef.
                    assign tChartCOACrossRef.tiCompanyId = viCompanyIdTemp
                           tChartCOACrossRef.tcCompanyCode = tqSelectCompanySharedSet.tcCompanyCode
                           tChartCOACrossRef.tcCompanyDesc = tqSelectCompanySharedSet.tcCompanyDescription
                           tChartCOACrossRef.tcSharedSetType = tqSelectCompanySharedSet.tcSharedSetTypeCode
                           tChartCOACrossRef.tcSourceSharedSetCode = tqSelectCompanySharedSet.tcSharedSetCode
                           tChartCOACrossRef.tiSourceSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                           tChartCOACrossRef.tiDomainID = viSourceDomainID.
                end.
                       
                assign vcAllSets = vcAllSets + string(tqSelectCompanySharedSet.tiSharedSet_ID,"9999999999")
                       viSourcePrjSharedSetId = tqSelectCompanySharedSet.tiSharedSet_ID
                       vlIsExistingCOACrossRef = no.
                if tChartCOACrossRef.tcCOACrossRefCode <> ? and tChartCOACrossRef.tcCOACrossRefCode <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
                then do:
                    <Q-34 run COACrossRefPrim (all) (Read) (NoCache)
                       (input ?, (COACrossRefID)
                        input ?, (SourceDomainID)
                        input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                    if can-find (first tqCOACrossRefPrim) then assign vlIsExistingCOACrossRef = yes.
                    if not vlIsExistingCOACrossRef
                    then do:
                        if tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(tChartCOACrossRef.tcTargetSharedSetCode,1,9,"CHARACTER":U)  
                        then assign vlIsExistingCOACrossRef = yes.
                    end.
                end.
                if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef
                then do:                    
                    if iiTargetPrjSharedSetId <> ? and iiTargetPrjSharedSetId <> 0
                    then do:
                        if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetPrjSharedSetId
                        then do:
                            if iiTargetPrjSharedSetId = tChartCOACrossRef.tiSourceSharedSetId then assign tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED}.
                            else do:
                                /* we lookup if there is already a tFRWXref (extention of COACrossRef) */
                                find first tFRWXref where tFRWXref.tcFRWCrossRefCode = {&COACROSSREFTYPE-SEPERATE} and
                                                          tFRWXref.tcFRWCrossRefCOAType = {&SHAREDSETTYPE-PROJECT} and
                                                          tFRWXref.tiSourcePrjSharedSet_ID = tChartCOACrossRef.tiSourceSharedSetId and
                                                          tFRWXref.tiTargetPrjSharedSet_ID = iiTargetPrjSharedSetId no-error.
                                if available tFRWXref
                                then do:
                                    <Q-26 run COACrossRefPrim (first) (Read) (NoCache)
                                       (input tFRWXRef.tiCOACrossRef_ID, (COACrossRefID)
                                        input ?, (SourceDomainID)
                                        input ?, (COACrossRefCode)
                                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                                    find first tqCOACrossRefPrim no-error.
                                    if available tqCOACrossRefPrim then 
                                    assign tChartCOACrossRef.tcCOACrossRefCode = tqCOACrossRefPrim.tcCOACrossRefCode.
                                    else do:
                                        assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetPrjSet,1,9,"CHARACTER":U).
                                        delete tFRWXref.
                                    end.
                                end. /* if available tFRWXref */
                                else assign tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(icTargetPrjSet,1,9,"CHARACTER":U).
                            end. /* else do */
                        end. /* if tChartCOACrossRef.tiTargetSharedSetId <> iiTargetPrjSharedSetId */
                    end. /* if iiTargetPrjSharedSetId <> ? and iiTargetPrjSharedSetId <> 0 */
                    else assign tChartCOACrossRef.tcCOACrossRefCode = "".
                end. /* if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef */
                assign tChartCOACrossRef.tcTargetSharedSetCode = icTargetPrjSet
                       tChartCOACrossRef.tiTargetSharedSetId = iiTargetPrjSharedSetId.                
            end.
    
            find first tChartCOACrossRef where tChartCOACrossRef.tiCompanyId = viCompanyIdTemp and
                                               tChartCOACrossRef.tcSharedSetType = {&COACROSSREFTYPE-COMBINED} no-error. 
            if not available tChartCOACrossRef   
            then do:
                /* Create combine */
                create tChartCOACrossRef.
                assign tChartCOACrossRef.tiCompanyId = viCompanyIdTemp
                       tChartCOACrossRef.tcCompanyCode = tqSelectCompanySharedSet.tcCompanyCode
                       tChartCOACrossRef.tcCompanyDesc = tqSelectCompanySharedSet.tcCompanyDescription
                       tChartCOACrossRef.tcSharedSetType = {&COACROSSREFTYPE-COMBINED}
                       tChartCOACrossRef.tcAllSets = vcAllSets
                       tChartCOACrossRef.tiDomainID = viSourceDomainID
                       tChartCOACrossRef.tcSourceSharedSetCode = vcSourceDomainCode.
            end.
            assign tChartCOACrossRef.tcTargetSharedSetCode = if icFRWChartCode = ? then "":U else icFRWChartCode
                   vlIsExistingCOACrossRef = no.
            if tChartCOACrossRef.tcCOACrossRefCode <> ? and tChartCOACrossRef.tcCOACrossRefCode <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
            then do:
                <Q-28 run COACrossRefPrim (all) (Read) (NoCache)
                       (input ?, (COACrossRefID)
                        input ?, (SourceDomainID)
                        input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                if can-find (first tqCOACrossRefPrim) then assign vlIsExistingCOACrossRef = yes.
                if not vlIsExistingCOACrossRef
                then do:
                    if tChartCOACrossRef.tcCOACrossRefCode = substring(tChartCOACrossRef.tcSourceSharedSetCode,1,10,"CHARACTER":U) + "-" + substring(tChartCOACrossRef.tcTargetSharedSetCode,1,9,"CHARACTER":U)  
                    then assign vlIsExistingCOACrossRef = yes.
                end.            
            end.
            if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef
            then do:  
                find first tFRWXRef where tFRWXref.tcFRWCrossRefCode = {&COACROSSREFTYPE-COMBINED} and
                                          tFRWXRef.tcFRWCrossRefCOAType = {&TRANSDIRECTION-ALL} and
                                          tFRWXref.tiSourceGLSharedSet_ID = viSourceGLSharedSetId and
                                          tFRWXref.tiSourceDivSharedSet_ID = viSourceDivSharedSetId and
                                          tFRWXref.tiSourceCCSharedSet_ID = viSourceCCSharedSetId and
                                          tFRWXref.tiSourcePrjSharedSet_ID = viSourcePrjSharedSetId and
                                          tFRWXref.tiTargetGLSharedSet_ID = iiTargetGLSharedSetId and
                                          tFRWXref.tiTargetDivSharedSet_ID = iiTargetDivSharedSetId and
                                          tFRWXref.tiTargetCCSharedSet_ID = iiTargetCCSharedSetId and
                                          tFRWXref.tiTargetPrjSharedSet_ID = iiTargetPrjSharedSetId no-error.
                if available tFRWXref
                then do:
                    <Q-71 run COACrossRefPrim (first) (Read) (NoCache)
                       (input tFRWXRef.tiCOACrossRef_ID, (COACrossRefID)
                        input ?, (SourceDomainID)
                        input ?, (COACrossRefCode)
                        output dataset tqCOACrossRefPrim) in BCOACrossRef>
                    find first tqCOACrossRefPrim no-error.
                    if available tqCOACrossRefPrim then 
                    assign tChartCOACrossRef.tcCOACrossRefCode = tqCOACrossRefPrim.tcCOACrossRefCode.
                    else delete tFRWXref.
                end.
                else assign tChartCOACrossRef.tcCOACrossRefCode = "":U.
                /* here we do not propose a default initial XRef code when there is no existing one, because the combined
                       XRef is optional */
            end. /*  if tChartCOACrossRef.tcCOACrossRefCode = ? or tChartCOACrossRef.tcCOACrossRefCode = "":U or tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED} or vlIsExistingCOACrossRef */
    end. /* do viCounter */

    /* Assign COA Cross Ref calculated code and COA Cross Ref Description */
    for each tChartCOACrossRef:
        if tChartCOACrossRef.tcCOACrossRefCode = {&COACROSSREFCODE-NOTNEEDED}
        then assign tChartCOACrossRef.tcCOACrossRefCodeCalc = {&COACROSSREFCODE-NOTNEEDED-TR}.
        else assign tChartCOACrossRef.tcCOACrossRefCodeCalc = tChartCOACrossRef.tcCOACrossRefCode.

        if tChartCOACrossRef.tiTargetDomainId = ? or tChartCOACrossRef.tiTargetDomainId = 0
        then assign tChartCOACrossRef.tiTargetDomainId = iiTargetDomainId.

        if trim(tChartCOACrossRef.tcCOACrossRefCode) <> "":U and tChartCOACrossRef.tcCOACrossRefCode <> ? 
            and tChartCOACrossRef.tcCOACrossRefCode <> {&COACROSSREFCODE-NOTNEEDED}
        then do:
            <Q-37 run COACrossRefByCodeAndSourceDomain (all) (Read) (NoCache)
               (input tChartCOACrossRef.tcCOACrossRefCode, (COACrossRefCode)
                input ?, (SourceDomain_ID)
                output dataset tqCOACrossRefByCodeSourceDomain) in BCOACrossRef>
            for first tqCOACrossRefByCodeSourceDomain:
                assign tChartCOACrossRef.tcCOACrossRefDesc = tqCOACrossRefByCodeSourceDomain.tcCOACrossRefDescription.
                if tChartCOACrossRef.tiTargetDomainId = ? or tChartCOACrossRef.tiTargetDomainId = 0
                then assign tChartCOACrossRef.tiTargetDomainId = tqCOACrossRefByCodeSourceDomain.tiTargetDomain_ID.
                if tChartCOACrossRef.tiDomainID = ? or tChartCOACrossRef.tiDomainID = 0
                then assign tChartCOACrossRef.tiDomainID = tqCOACrossRefByCodeSourceDomain.tiSourceDomain_ID.
            end.
        end.
        else assign tChartCOACrossRef.tcCOACrossRefDesc = "":U.
    end. /* for each tChartCOACrossRef */
    

end. /* MAINBLOCK */