project QadFinancials > class BPosting > method ApiStdMaintainTTInitialDefaulting

Description

ApiStdMaintainTTInitialDefaulting: This method is called when creating a posting through an API right before the processing of the input.
This method can be used to default field values to the incoming dataset prior to the acctual processing.

Standard-defaulting in this method:
• Default Company_ID with the current company you are logged on to
• Default PostingDate with PostingSystemDate (and vice-versa so one of them is mandatory).
• Default CInvoiceTaxPointDate with CInvoicePostingDate
• Default PostingYearPeriod based upon PostingYear and PostingPeriod (and vice-versa)
• Default PostingYearPeriod, PostingYear and PostingPeriod based upon PostingDate
• Default PostingText based upon PostingYear, tcJournalCode, PostingVoucher and the date and time.
• Default PostingLineSequence with a sequential value and ordered by tc_rowid
• Default PostingLineText with PostingText
• Default tcDivisionCode, tcCostCentreCode and tcProjectCode in the posting-lines based upon the defaults of the GL-account on the PostingLine
• Default GLIsAutomaticAccount, GLIsDebitAAccount, GLTypeCode and UnitCode based upon the GL-account on the PostingLine (even in case these fields are already filled by the caller)
• Default PostingVat.PostingVatTaxDebitTC and DInvoiceVat.PostingVatTaxCreditTC with fields PostingLineDebitTC and PostingLineCreditTC of the parent-record in PostingLine
• Default PostingVaT.tcDomainCode based upon the current domain
• Default PostingVatTaxPointDate with PostingDate
• Default PostingVat.PostingVatInOut with tApiPostingVat.tcVatInOut (and vice versa)


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPosting.ApiStdMaintainMultiTT
method BPosting.ApiStdMaintainTT
method BPosting.DataLoadByInput


program code (program9/bposting.p)

    /* ============================================================================================================= */
    /* Add an include in here so we can easily trace the methods that do direct db-access (or via dynamic querying)  */
    /* As in this method a lot of different tables are accessed we do not specify the complete list here in the call */
    /* ============================================================================================================= */
    <I-63 {READDIRECTDBACCESS
         &READTABLENAMES = "Plenty of Finacials tables"}>
    
    /* ============================================================================== */
    /* Notes:                                                                         */
    /*  0. In this method we will ony do the defaulting that applies to               */
    /*  1. No records are available at the moment this method is entered              */
    /*  2. The records that need to be extended are the tApixxx-tables                */
    /*  3. Exception handling is done via progress itself (on error undo throw)       */
    /*  4. Database is accessed directly and variables are defined manually to:       */
    /*        A. Come to a proper performance                                         */
    /*        B. Ease downgradability                                                 */
    /*        C. Ease installations on released products: This complete peice of code */
    /*           can be copied into the 'before' hook of the parent-method of this    */
    /*           method by using the customization-technology that we have in the Fin */
    /*  Addition: Note that only Financial-tables can be accessed directly - for      */ 
    /*            Operational tables we will have to use dynamic queries!             */ 
    /* ============================================================================== */
    
    
    /* We are naming these manual defined varaiable with an X to avoid interactions with the normaly defined method-data-items */
    define variable viXGLSharedSetID                as  integer no-undo. 
    define variable viXDivisionSharedSetID          as  integer no-undo. 
    define variable viXCostCentreSharedSetID        as  integer no-undo. 
    define variable viXProjectSharedSetID           as  integer no-undo. 
    define variable viXJournalSharedSetID           as  integer no-undo.
    define variable viXGLProfileID                  as  integer no-undo.
    define variable viXDivisionProfileID            as  integer no-undo.
    define variable viXCostCentreProfileID          as  integer no-undo.
    define variable viXProjectProfileID             as  integer no-undo.
    define variable vcXDomainCode                   as  character no-undo.
    define variable viHighestPostingLineSequence    as  integer no-undo initial 0.
    
    
    /* ========================= */
    /* Go through all postingss  */
    /* ========================= */
    For each tApiPosting on error undo, throw :
        
        /* ================================================================================================= */
        /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
        /* ================================================================================================= */
        assign tApiPosting.PostingIsReplacement      = false when tApiPosting.PostingIsReplacement      = ?
               tApiPosting.PostingIsReversing        = false when tApiPosting.PostingIsReversing        = ?
               tApiPosting.PostingIsReversingBySign  = false when tApiPosting.PostingIsReversingBySign  = ?
               tApiPosting.PostingIsZeroValueAllowed = false when tApiPosting.PostingIsZeroValueAllowed = ?
               tApiPosting.PostingIsSkipAutoAssignLC = false when tApiPosting.PostingIsSkipAutoAssignLC = ?
               tApiPosting.PostingIsAutoReversal     = false when tApiPosting.PostingIsAutoReversal     = ?
               tApiPosting.PostingIsCorrection       = false when tApiPosting.PostingIsCorrection       = ?
               tApiPosting.tlSaveAsTemplate          = false when tApiPosting.tlSaveAsTemplate          = ?
               tApiPosting.PostingVoucher            = 0     when tApiPosting.PostingVoucher            = ?
               tApiPosting.PostingAutoReversalType   = "":U  when tApiPosting.PostingAutoReversalType   = ?.
        
        /* ============================================== */
        /* Fill in default Company_ID when not specified  */
        /* ============================================== */
        if tApiPosting.Company_ID = ? or
           tApiPosting.Company_ID = 0
        then assign tApiPosting.Company_ID = viCompanyID.
        
        /* =================================== */
        /* Get the DomainCode and SharedSetIDs */
        /* =================================== */
        For each Company where 
                 Company.Company_ID = tApiPosting.Company_ID
                 no-lock,
            first Domains of Company no-lock,                 
            each CompanySharedSet of Company no-lock
            on error undo, throw :
            Assign vcXDomainCode = Domains.DomainCode.
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-GL} /* GL */
                     no-lock 
                     on error undo, throw :
                assign viXGLSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-DIVISION} /* DIVISION */
                     no-lock 
                     on error undo, throw :
                assign viXDivisionSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-PROJECT} /* PROJECT */
                     no-lock 
                     on error undo, throw :
                assign viXProjectSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-COSTCENTRE} /* COSTCENTRE */
                     no-lock 
                     on error undo, throw :
                assign viXCostCentreSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-JOURNAL} /* JOURNAL */
                     no-lock 
                     on error undo, throw :
                assign viXJournalSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
        end. /* For each Company where */                 
        if vcXDomainCode            = "":U or 
           vcXDomainCode            = ? or 
           viXGLSharedSetID         = 0 or 
           viXGLSharedSetID         = ? or 
           viXDivisionSharedSetID   = 0 or 
           viXDivisionSharedSetID   = ? or
           viXProjectSharedSetID    = 0 or 
           viXProjectSharedSetID    = ? or
           viXCostCentreSharedSetID = 0 or 
           viXCostCentreSharedSetID = ? or
           viXJournalSharedSetID    = 0 or 
           viXJournalSharedSetID    = ? 
        then Return. /* If this missing then further defaulting makes no sense */

        /* =================================================== */
        /* PostingDate-PostingSystemDate + PostingYearPeriod   */
        /* =================================================== */
        if tApiPosting.PostingDate         = ? and 
           tApiPosting.PostingSystemDate  <> ? 
        then assign tApiPosting.PostingDate = tApiPosting.PostingSystemDate.
        if tApiPosting.PostingSystemDate   = ? and 
           tApiPosting.PostingDate        <> ? 
        then assign tApiPosting.PostingSystemDate = tApiPosting.PostingDate.
        if tApiPosting.PostingYearPeriod <> 0 and 
           tApiPosting.PostingYearPeriod <> ? and 
           (tApiPosting.PostingYear = 0 or 
            tApiPosting.PostingYear = ?)      and 
           (tApiPosting.PostingPeriod = 0 or 
            tApiPosting.PostingPeriod = ?)
        then assign tApiPosting.PostingYear   = truncate(tApiPosting.PostingYearPeriod / 100, 0)
                    tApiPosting.PostingPeriod = tApiPosting.PostingYearPeriod - (tApiPosting.PostingYear * 100) no-error.
        if tApiPosting.PostingYear   <> 0 and 
           tApiPosting.PostingYear   <> ? and 
           tApiPosting.PostingPeriod <> 0 and 
           tApiPosting.PostingPeriod <> ? and 
           (tApiPosting.PostingYearPeriod  = 0 or
            tApiPosting.PostingYearPeriod = ?)
        then assign tApiPosting.PostingYearPeriod = (tApiPosting.PostingYear * 100) + tApiPosting.PostingPeriod no-error.
        if tApiPosting.PostingDate <> ? and 
           (tApiPosting.PostingYear = 0 or 
            tApiPosting.PostingYear = ?) and 
           (tApiPosting.PostingPeriod = 0 or 
            tApiPosting.PostingPeriod = ?)
        then do :
            Release Period no-error.
            Find Period where 
                 Period.Company_ID                 = tApiPosting.Company_ID   and 
                 Period.PeriodStartDate           <= tApiPosting.PostingDate  and 
                 Period.PeriodEndDate             >= tApiPosting.PostingDate  and 
                 Period.PeriodTypeCode             = {&PERIODTYPECODE-NORMAL} and /* NORM */
                 Period.PeriodIsPostingGLAllowed   = true     
                 no-lock no-error.
            if not available Period 
            then Find Period where 
                      Period.Company_ID                 = tApiPosting.Company_ID  and 
                      Period.PeriodStartDate           <= tApiPosting.PostingDate and 
                      Period.PeriodEndDate             >= tApiPosting.PostingDate and 
                      Period.PeriodIsPostingGLAllowed   = true                           
                      no-lock no-error.
            if available Period
            then assign tApiPosting.PostingYear       = Period.PeriodYear
                        tApiPosting.PostingPeriod     = Period.PeriodPeriod
                        tApiPosting.PostingYearPeriod = (tApiPosting.PostingYear * 100) + tApiPosting.PostingPeriod.
        end. /* if tApiPosting.PostingDate <> ? and */               
        
        /* ============================================== */
        /* Fill in PostingText when not specified         */
        /* ============================================== */
        if tApiPosting.PostingText = "":U or 
           tApiPosting.PostingText = ? 
        then do :
            assign tApiPosting.PostingText = "":U.
            if tApiPosting.PostingYear <> 0 and 
               tApiPosting.PostingYear <> ?
            then assign tApiPosting.PostingText = string(tApiPosting.PostingYear,"9999":U). 
            if tApiPosting.tcJournalCode <> "":U and 
               tApiPosting.tcJournalCode <> ?
            then assign tApiPosting.PostingText = (if tApiPosting.PostingText = "":U then "":U else trim(tApiPosting.PostingText) + "/":U) + trim(tApiPosting.tcJournalCode).
            if tApiPosting.PostingVoucher <> 0 and 
               tApiPosting.PostingVoucher <> ?
            then assign tApiPosting.PostingText = (if tApiPosting.PostingText = "":U then "":U else trim(tApiPosting.PostingText) + "/":U) + string(tApiPosting.PostingVoucher,"999999999":U).
            else assign tApiPosting.PostingText = trim(substring(trim(tApiPosting.PostingText) + " - ":U + substitute(#T-91'Created &1 &2':255(932597315)T-91#, string(today), string(time,"HH:MM:SS":U)), 1, 40,"character":U)).
        end. /* if tApiPosting.PostingText = "":U or  */ 
        
        /* ============================================ */
        /* Go through all PostingLines of the Posting   */
        /* ============================================ */
        if vcActivityCode = "ExternalCreate":U 
        then do :
            for each tApiPostingLine  use-index i_parent :
                assign tApiPostingLine.tc_Rowid =  string(int(substring(tApiPostingLine.tc_Rowid,2,length(tApiPostingLine.tc_Rowid,"CHARACTER":U),"CHARACTER":U)),'9999').
            end.
        end.        
        For each tApiPostingLine where 
                 tApiPostingLine.tc_ParentRowid = tApiPosting.tc_rowid
                 by tApiPostingLine.tc_rowid
                 on error undo, throw :
            
            /* ====================================================================================================== */
            /* Fill PostingLineSequence when this is not specified and keep track of the highest value for this field */
            /* ====================================================================================================== */
            if tApiPostingLine.PostingLineSequence = 0 or 
               tApiPostingLine.PostingLineSequence = ? or 
               tApiPostingLine.PostingLineSequence < viHighestPostingLineSequence
            then assign viHighestPostingLineSequence        = viHighestPostingLineSequence + 1
                        tApiPostingLine.PostingLineSequence = viHighestPostingLineSequence.
            else assign viHighestPostingLineSequence = tApiPostingLine.PostingLineSequence.
            
            /* ==================================================================================================== */
            /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this    */
            /* ==================================================================================================== */
            assign tApiPostingLine.tcAllocationType = "" when tApiPostingLine.tcAllocationType = ?
                   tApiPostingLine.tcDivisionCode   = "" when tApiPostingLine.tcDivisionCode   = ?
                   tApiPostingLine.tcCostCentreCode = "" when tApiPostingLine.tcCostCentreCode = ?
                   tApiPostingLine.tcProjectCode    = "" when tApiPostingLine.tcProjectCode    = ?.  
            
            /* ==================================================================================================== */
            /* If the Text on the PostignLine is not filled then use the one from the Posting                       */
            /* ==================================================================================================== */
            if tApiPostingLine.PostingLineText = "":U or 
               tApiPostingLine.PostingLineText = ?
            then assign tApiPostingLine.PostingLineText = tApiPosting.PostingText.
            
            /* ==================================================================================================== */
            /* Fill the Div, CC and Prj-info on the CI-PostingLines with the defaults from the accounts             */
            /* Default GLIsAutomaticAccount, GLIsDebitAAccount, GLTypeCode and UnitCode based upon the GL-account   */
            /* ==================================================================================================== */
            if tApiPostingLine.tcGLCode <> ? and 
               tApiPostingLine.tcGLCode <> "":U
            then do :
                assign viXProjectProfileID    = 0
                       viXCostCentreProfileID = 0
                       viXDivisionProfileID   = 0.
                find GL where 
                     GL.SharedSet_ID = viXGLSharedSetID and 
                     GL.GLCode       = tApiPostingLine.tcGLCode
                     no-lock no-error.
                if available GL 
                then do :
                    assign tApiPostingLine.tlGLIsAutomaticAccount = GL.GLIsAutomaticAccount
                           tApiPostingLine.tlGLIsDebitAccount     = GL.GLIsDebitAccount
                           tApiPostingLine.tcGLTypeCode           = GL.GLTypeCode.
                    if GL.Unit_ID <> 0 and
                       GL.Unit_ID <> ? 
                    then do :
                        Find Unit where 
                             Unit.Unit_ID = GL.Unit_ID
                             no-lock no-error.
                        if available Unit 
                        then assign tApiPostingLine.tcUnitCode = Unit.UnitCode.
                    end. /* if GL.Unit_ID <> 0 and */
                    else assign tApiPostingLine.tcUnitCode = "":U.
                    if GL.GLIsDivisionAccount = true
                    then assign viXDivisionProfileID = GL.DivisionProfile_ID.
                    if GL.GLIsCostCentreAccount = true
                    then assign viXCostCentreProfileID = GL.CostCentreProfile_ID.
                    if GL.GLIsProjectAccount = true
                    then assign viXProjectProfileID = GL.ProjectProfile_ID.
                    if (tApiPostingLine.tcDivisionCode = "":U or 
                        tApiPostingLine.tcDivisionCode = ?) and 
                       viXDivisionProfileID <> 0 and 
                       viXDivisionProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXDivisionProfileID and 
                                  ProfileLink.SharedSet_ID = viXDivisionSharedSetID
                                  no-lock,
                            first Division where 
                                  Division.Division_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign tApiPostingLine.tcDivisionCode = Division.DivisionCode
                                   tApiPostingLine.Division_ID    = Division.Division_ID.                    
                        end. /* or first Profile where */
                    end. /* if viXDivisionProfileID <> 0 and */
                    if (tApiPostingLine.tcCostCentreCode = "":U or 
                        tApiPostingLine.tcCostCentreCode = ?) and 
                       viXCostCentreProfileID <> 0 and 
                       viXCostCentreProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXCostCentreProfileID and 
                                  ProfileLink.SharedSet_ID = viXCostCentreSharedSetID
                                  no-lock,
                            first CostCentre where 
                                  CostCentre.CostCentre_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign tApiPostingLine.tcCostCentreCode = CostCentre.CostCentreCode
                                   tApiPostingLine.CostCentre_ID    = CostCentre.CostCentre_ID.                    
                        end. /* or first Profile where */
                    end. /* if viXCostCentreProfileID <> 0 and */
                    if (tApiPostingLine.tcProjectCode = "":U or 
                        tApiPostingLine.tcProjectCode = ?) and 
                       viXProjectProfileID <> 0 and 
                       viXProjectProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXProjectProfileID and 
                                  ProfileLink.SharedSet_ID = viXProjectSharedSetID
                                  no-lock,
                            first qaddb.Project where 
                                  qaddb.Project.Project_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign tApiPostingLine.tcProjectCode = qaddb.Project.ProjectCode
                                   tApiPostingLine.Project_ID    = qaddb.Project.Project_ID.                    
                        end. /* for first Profile where */
                    end. /* if viXProjectProfileID <> 0 and */
                end. /* if available GL */
            end. /* if tApiPostingLine.tcGLCode <> ? and */ 
            
            /* ==================================================== */ 
            /* Go through the PostingVat records of the PostingLine */                           
            /* ==================================================== */ 
            for each tApiPostingVat where 
                     tApiPostingVat.tc_ParentRowid = tApiPostingLine.tc_rowid
                     break by tApiPostingVat.tc_ParentRowid
                     on error undo, throw  :
                
                /* =================================================================== */
                /* If there is only a single PostingVat for this PostingLine and the   */
                /* tax-amounts on PostingVat are empty then take them from PostingLine */
                /* =================================================================== */
                if first-of (tApiPostingVat.tc_ParentRowid)     and 
                   last-of (tApiPostingVat.tc_ParentRowid)      and 
                   (tApiPostingVat.PostingVatTaxDebitTC = ? or 
                    tApiPostingVat.PostingVatTaxDebitTC = 0)    and 
                   (tApiPostingVat.PostingVatTaxCreditTC = ? or 
                    tApiPostingVat.PostingVatTaxCreditTC = 0)
                then assign tApiPostingVat.PostingVatTaxDebitTC  = tApiPostingLine.PostingLineDebitTC
                            tApiPostingVat.PostingVatTaxCreditTC = tApiPostingLine.PostingLineCreditTC.
                
                /* ================================================================================================= */
                /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
                /* ================================================================================================= */
                assign tApiPostingVat.PostingVatIsReverseCharge = false when tApiPostingVat.PostingVatIsReverseCharge = ?
                       tApiPostingVat.PostingVatIsAbsRet        = false when tApiPostingVat.PostingVatIsAbsRet        = ?
                       tApiPostingVat.PostingVatIsSuspDel       = false when tApiPostingVat.PostingVatIsSuspDel       = ?.
                
                /* ===================================== */
                /* Fill in defaults in tApiPostingVat    */
                /* ===================================== */
                if tApiPostingVat.tcDomainCode = "":U or 
                   tApiPostingVat.tcDomainCode = ?
                then assign tApiPostingVat.tcDomainCode = vcXDomainCode.
                if tApiPostingVat.PostingVatTaxPointDate = ?
                then assign tApiPostingVat.PostingVatTaxPointDate = tApiPosting.PostingDate.
                if tApiPostingVat.tcVatInOut <> "":U and 
                   tApiPostingVat.tcVatInOut <> ?    and 
                   (tApiPostingVat.PostingVatInOut = "":U or 
                    tApiPostingVat.PostingVatInOut = ?)
                then assign tApiPostingVat.PostingVatInOut = tApiPostingVat.tcVatInOut.
                if tApiPostingVat.PostingVatInOut <> "":U and 
                   tApiPostingVat.PostingVatInOut <> ?    and 
                   (tApiPostingVat.tcVatInOut = "":U or 
                    tApiPostingVat.tcVatInOut = ?)
                then assign tApiPostingVat.tcVatInOut = tApiPostingVat.PostingVatInOut.
                
            end. /* for each tApiPostingVat where  */
            
            /* ================================================================================== */
            /* Go through the PostingVatDelay records of the PostingLine and avoid unknown values  */                           
            /* =================================================================================== */
            for each tApiPostingVatDelay where 
                     tApiPostingVatDelay.tc_ParentRowid = tApiPostingLine.tc_rowid 
                     on error undo, throw : 
                assign tApiPostingVatDelay.PostingVatDelayIsOpen = ? when tApiPostingVatDelay.PostingVatDelayIsOpen = ?.
            end. /* for each tApiPostingVatDelay where */
            
        End. /* For each tApiPostingLine where  */

    End. /* for each tApiPosting where */  
    
    
    /* ============================================== */        
    /* close queries and delete buffer handle objects */
    /* ============================================== */         
    FINALLY:
        
        
    END FINALLY.