project QadFinancials > class BCompanyProperty > method SwitchPrimaryCompany

Description

Switch the Company flagged as Primary (for a domain) from one to another.


Parameters


iiNewPrimaryCompany_IDinputinteger
iiOldPrimaryCompany_IDinputinteger
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDomain.AdditionalUpdates


program code (program1/bcompanyproperty.p)

assign viLocalReturnStatus = oiReturnStatus
       oiReturnStatus      = -98.

SETPRIMARY: do:
     <M-1 run ClearData
        (output viFcReturnSuper (oiReturnStatus)) in BCompanyProperty>

     /* find and assign the domain code */     
     <Q-33 run CompanyByDomain (all) (Read) (NoCache)
         (input iiNewPrimaryCompany_ID, (CompanyId)
          input ?, (DomainId)
          input ?, (Active)
          output dataset tqCompanyByDomain) in BCompany >
    find tqCompanyByDomain where
         tqCompanyByDomain.tiCompany_ID = iiNewPrimaryCompany_ID 
         no-error.
    if available tqCompanyByDomain
    then assign vcDomainCode = tqCompanyByDomain.tcDomainCode
                viDomainID   = tqCompanyByDomain.tiDomain_ID.
    
    /* Get the new Primary Company... */
    if iiNewPrimaryCompany_ID <> ? 
    then do:
         <Q-9 run CompanyPropertyPrim (all) (Read) (NoCache)
           (input iiNewPrimaryCompany_ID, (CompanyId)
            input ?, (CompanyPropertyId)
            input ?, (CompanyPropertyCode)
            output dataset tqCompanyPropertyPrim) in BCompanyProperty >
    end.
    
    find tqCompanyPropertyPrim where
         tqCompanyPropertyPrim.tiCompany_ID = iiNewPrimaryCompany_ID 
         no-error.
    if available tqCompanyPropertyPrim 
    then do:    
        <M-10 run DataLoad
           (input  '' (icRowids), 
            input  '' (icPkeys), 
            input  tqCompanyPropertyPrim.tiCompanyProperty_ID (icObjectIds), 
            input  '' (icFreeform), 
            input  no (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BCompanyProperty>
    end.
        
    /* Get the previous Primary Company... */
    if iiOldPrimaryCompany_ID <> ? 
    then do:
        <Q-11 run CompanyPropertyPrim (all) (Read) (NoCache)
           (input iiOldPrimaryCompany_ID, (CompanyId)
            input ?, (CompanyPropertyId)
            input ?, (CompanyPropertyCode)
            output dataset tqCompanyPropertyPrim) in BCompanyProperty >
    end.
    
    find tqCompanyPropertyPrim where
         tqCompanyPropertyPrim.tiCompany_ID = iiOldPrimaryCompany_ID 
         no-error.
    if available tqCompanyPropertyPrim 
    then do:    
        <M-12 run DataLoad
           (input  '' (icRowids), 
            input  '' (icPkeys), 
            input  tqCompanyPropertyPrim.tiCompanyProperty_ID (icObjectIds), 
            input  '' (icFreeform), 
            input  yes (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BCompanyProperty>
    end.

    /* Unset the old one... */
    find tCompanyProperty where 
         tCompanyProperty.Company_ID = iiOldPrimaryCompany_ID 
         no-error.        
    if available tCompanyProperty and tCompanyProperty.CompanyPropertyIsPrimary 
    then assign tCompanyProperty.CompanyPropertyIsPrimary = no
                tCompanyProperty.tc_Status                = "C":U
                vcOldPrimaryCompanyCode                   = tCompanyProperty.CompanyPropertyCode.

    /* Set the new one... */
    find tCompanyProperty where 
         tCompanyProperty.Company_ID = iiNewPrimaryCompany_ID 
         no-error.        
    if available tCompanyProperty and not tCompanyProperty.CompanyPropertyIsPrimary 
    then assign tCompanyProperty.CompanyPropertyIsPrimary = yes
                tCompanyProperty.tc_Status                = "C":U
                vcNewPrimaryCompanyCode                   = tCompanyProperty.CompanyPropertyCode.
        
    /* Ensure that a primary company switch is allowed... */
    <I-7 {bFcOpenInstance
            &CLASS           = "Session"}>
                      
    <M-8 run SetLogicalValue (input  'AllowPrimaryCompanyChange':U (icName), 
                          input  true (ilValue), 
                          output viLocalReturnStatus (oiReturnStatus)) in Session>                      
    <I-19 {bFcCloseInstance
            &CLASS           = "Session"}>
    
    if viLocalReturnStatus < 0 
    then leave SETPRIMARY.
    
    /* Check if the domain setup is complete */
    <Q-34 run DomainPropertyByDomain (all) (Read) (NoCache)
        (input viDomainID, (DomainID)
         output dataset tqDomainPropertyByDomain) in BDomainProperty >
 
    find tqDomainPropertyByDomain where
         tqDomainPropertyByDomain.tiDomain_ID                  = viDomainID and
         tqDomainPropertyByDomain.tlDomainPropertyIsSetupCompl = true
         no-error.
    /* Only replicate if the domain setup is complete */     
    if available tqDomainPropertyByDomain 
    then do:    
        if viBMfgEntity6ID = 0 or 
           viBMfgEntity6ID = ?
        then do:
            <I-24 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BMfgEntity"}>
        end. /* if viBMfgEntity6ID = 0 or viBMfgEntity6ID = ? */
        else do:
            <I-25 {bFcOpenInstance
            &CLASS           = "BMfgEntity"}>
        end. /* Not if viBMfgEntity6ID = 0 or viBMfgEntity6ID = ? */
        
        <M-26 run SwitchPrimaryEntity
           (input  vcNewPrimaryCompanyCode (icNewPrimEntity), 
            input  vcOldPrimaryCompanyCode (icOldPrimEntity), 
            input  vcDomainCode (icDomain), 
            output viFcReturnSuper (oiReturnStatus)) in BMfgEntity>             
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and viLocalReturnStatus >= 0)
        then assign viLocalReturnStatus = viFcReturnSuper.

        <M-35 run ValidateBCAndAdditionalUpdates
           (output viFcReturnSuper (oiReturnStatus)) in BMfgEntity>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and viLocalReturnStatus >= 0)
        then assign viLocalReturnStatus = viFcReturnSuper.

        <I-36 {bFcCloseInstance
            &CLASS           = "BMfgEntity"}> 

        if viLocalReturnStatus < 0 
        then do :
            <M-37 run StopExternalInstances
               (output viFcReturnSuper (oiReturnStatus)) in BCompanyProperty>
            leave SETPRIMARY.
        end. /* if viLocalReturnStatus < 0  */
    end.       
end.

/* ==================================== */
/* Set ReturnStatus = localreturnstatus */
/* ==================================== */
assign oiReturnStatus = viLocalReturnStatus.