project QadFinancials > class BFixedAssetDepreciation > method AdditionalUpdatesPosting

Description

Updates Posting


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BFixedAssetDepreciation.AdditionalUpdates


program code (program1/bfixedassetdepreciation.p)

if not can-find(first tFADepreciation where
                      tFADepreciation.tc_Status = 'N':U)
then return.                      

/* start and/or open the BJournalEntry instance    */
if viBJournalEntry9ID = 0 or viBJournalEntry9ID = ?
then do:
    <I-1 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BJournalEntry"}>
    assign vlBJournalEntryStartedFromFADep = true.
end.
else do:
    <I-2 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
end.

/* Set the Correct Data to the Posting Period */
if viPeriodPostingYear = 0 or
   viPeriodPostingYear = ? or
   viPeriodPostingPeriod = 0 or
   viPeriodPostingPeriod = ?
then assign viPeriodPostingYear = ?
            viPeriodPostingPeriod = ?.

/* If the PostingPeriod was passed from UI, get it */
if viPeriodPostingYear <> ? and
   viPeriodPostingPeriod <> ?
then do:
    <Q-13 run PeriodByYearPeriod (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input viPeriodPostingYear, (PeriodYear)
           input viPeriodPostingPeriod, (PeriodPeriod)
           input ?, (PeriodId)
           output dataset tqPeriodByYearPeriod) in BPeriod >
    find first tqPeriodByYearPeriod where
               tqPeriodByYearPeriod.tiPeriodYear   = viPeriodPostingYear and
               tqPeriodByYearPeriod.tiPeriodPeriod = viPeriodPostingPeriod
               no-error.
    if available tqPeriodByYearPeriod
    then assign viPostingYear   = tqPeriodByYearPeriod.tiPeriodYear
                viPostingPeriod = tqPeriodByYearPeriod.tiPeriodPeriod
                vtPostingDate   = tqPeriodByYearPeriod.ttPeriodEndDate.
end.   

/* Create JournalEntry for each tFADepreciation record.*/
for each tFADepreciation where
         tFADepreciation.tc_Status = 'N':U and
         can-find(first tFADepreciationDet where
                        tFADepreciationDet.tc_ParentRowid = tFADepreciation.tc_Rowid and
                        tFADepreciationDet.tc_Status      = 'N':U)
    /*grouped by Asset code so we can easily create additional postings for an official book*/
    group by tFADepreciation.tiFAAsset_ID:
    /* Add Posting Header */
    assign vcPostingText = if vcText <> "":U and
                              vcText <> ?
                           then vcText
                           else ((if tFADepreciation.tlFABookTypeIsOfficial 
                                  then trim(#T-6'Off Deprec':20(14557)T-6#)
                                  else trim(#T-7'Unoff Deprec':20(14558)T-7#)) + " ":U +
                                       trim(tFADepreciation.tcFAAssetCode) + " ":U +
                                       trim(tFADepreciation.tcFAAssetBookCode) + " ":U +
                                       string(tFADepreciation.FADepreciationYear,"9999":U) + "/":U +
                                       string(tFADepreciation.FADepreciationMonth,"99":U)).
    
    /* Set the Posting Period to the Depreciation Period if it was not passed from UI */
    if tFADepreciation.FAAssetBook_ID <> 0 and
       tFADepreciation.FAAssetBook_ID <> ?
    then do:
        <Q-19 run FAAssetBookByID (all) (Read) (Cache)
          (input tFADepreciation.FAAssetBook_ID, (AssetBookID)
           input viCompanyId, (CompanyId)
           output dataset tqFAAssetBookByID) in BFixedAssetAsset >
    end.                               
    
    find first tqFAAssetBookByID where
               tqFAAssetBookByID.tiFAAssetBook_ID = tFADepreciation.FAAssetBook_ID
               no-error.
    assign vlCZTax = available tqFAAssetBookByID and
                     can-do({&DEPRECIATIONTYPE-CZTAXSTRAIGHT}   + ",":U +
                            {&DEPRECIATIONTYPE-CZTAXACCELERATED}, tqFAAssetBookByID.tcFAAssetBookDeprType).

    if (viPeriodPostingYear = ? and
        viPeriodPostingPeriod = ?) or
       vlCZTax
    then do:
        /* For tax depreciations posting date = depreciation date */
        assign vtDeprecPostingDate = if vlCZTax then tFADepreciation.FADepreciationDate
                                                else date(if tFADepreciation.FADepreciationMonth = 12 then 1 else tFADepreciation.FADepreciationMonth + 1, 1, if tFADepreciation.FADepreciationMonth = 12 then tFADepreciation.FADepreciationYear + 1 else tFADepreciation.FADepreciationYear) - 1.
        
        <Q-14 run PeriodByStartEndDate (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input vtDeprecPostingDate, (Date)
           output dataset tqPeriodByStartEndDate) in BPeriod >                            
        find first tqPeriodByStartEndDate where
                   tqPeriodByStartEndDate.ttPeriodStartDate <= vtDeprecPostingDate and
                   tqPeriodByStartEndDate.ttPeriodEndDate   >= vtDeprecPostingDate
                   no-error.
        if available tqPeriodByStartEndDate
        then assign viPostingYear   = tqPeriodByStartEndDate.tiPeriodYear
                    viPostingPeriod = tqPeriodByStartEndDate.tiPeriodPeriod
                    vtPostingDate   = tqPeriodByStartEndDate.ttPeriodEndDate.
        else assign viPostingYear   = tFADepreciation.FADepreciationYear
                    viPostingPeriod = tFADepreciation.FADepreciationMonth
                    vtPostingDate   = vtDeprecPostingDate.
    end.
    
    <M-3 run AddPostingHeader
       (input  ? (iiCompanyId), 
        input  viPostingYear (iiPeriodYear), 
        input  viPostingPeriod (iiPeriodPeriod), 
        input  tFADepreciation.tcJournalCode (icJournalCode), 
        input  '' (icReportingJournalCode), 
        input  0 (iiVoucher), 
        input  vtPostingDate (itPostingDate), 
        input  ? (itValueDate), 
        input  vcPostingText (icPostingText), 
        input  '':U (icPostingBusinessRelationText), 
        input  '':U (icPostingInvoiceReferenceText), 
        input  '':U (icPostingParentText), 
        input  0 (iiBPeriodId), 
        input  ? (icPostingOriginAddressCode), 
        input  ? (icPostingOriginDocument), 
        input  ? (icPostingOriginDocumentType), 
        input  '':U (icBatchNumber), 
        input  ? (icBankImpLineRef), 
        output viPostingID (oiPostingId), 
        output vcRowid (ocRowid), 
        output viPostingVoucher (oiPostingVoucher), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
    then assign oiReturnStatus = viFcReturnSuper.
    
    /*do not continue with this posting if there were errors*/
    if viFcReturnSuper < 0
    then next.
    
    /*create tFADepreciationPosting*/
    <M-15 run AddDetailLine (input  'FADepreciationPosting':U (icTable), 
                         input  tFADepreciation.tc_Rowid (icParentRowid), 
                         output viFcReturnSuper (oiReturnStatus)) in BFixedAssetDepreciation>
    
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
    then assign oiReturnStatus = viFcReturnSuper.
    
    if available tFADepreciationPosting
    then assign tFADepreciationPosting.FADepreciationPostingType = if tFADepreciation.tlFABookTypeIsOfficial 
                                                                   then {&FAPOSTINGTYPE-OFFICIAL}
                                                                   else {&FAPOSTINGTYPE-UNOFFICIAL}
                tFADepreciationPosting.Posting_ID      = viPostingId
                tFADepreciationPosting.tiPeriodYear    = viPostingYear
                tFADepreciationPosting.tiPeriodPeriod  = viPostingPeriod
                tFADepreciationPosting.tcJournalCode   = tFADepreciation.tcJournalCode
                tFADepreciationPosting.tiPostingVoucher= viPostingVoucher.
                  
    /* Add PostingLines for each tFADepreciationDet */    
    for each tFADepreciationDet where
             tFADepreciationDet.tc_ParentRowid = tFADepreciation.tc_Rowid and
             tFADepreciationDet.tc_Status      = 'N':U:
        
        assign vdDebitCC  = ?
               vdCreditCC = ?.
               
        /* Cumulative/Cost GL */
        if tFADepreciationDet.FADepreciationDetAmountLC > 0 
        then assign vdDebitLC  = tFADepreciationDet.FADepreciationDetAmountLC
                    vdCreditLC = 0.
        else assign vdDebitLC  = 0
                    vdCreditLC = tFADepreciationDet.FADepreciationDetAmountLC * -1.
        
        if vlCZTax
        then do:
            if tFADepreciationDet.FADepreciationDetAmountCC > 0 
            then assign vdDebitCC  = tFADepreciationDet.FADepreciationDetAmountCC
                        vdCreditCC = 0.
            else assign vdDebitCC  = 0
                        vdCreditCC = tFADepreciationDet.FADepreciationDetAmountCC * -1.        
        end.
        
        empty temp-table tDefaultSafValuesFADeprec.
        
        assign vcSafStructureCode = '':U.
        for each tFADepreciationDetSaf where
                 tFADepreciationDetSaf.tc_ParentRowid = tFADepreciationDet.tc_Rowid and
                 tFADepreciationDetSaf.tc_Status      = 'N':U:
            
            if vcSafStructureCode = '':U
            then assign vcSafStructureCode = tFADepreciationDetSaf.tcSafStructureCode.

            create tDefaultSafValuesFADeprec.
            assign tDefaultSafValuesFADeprec.tcSafConceptCode = tFADepreciationDetSaf.tcSafConceptCode
                   tDefaultSafValuesFADeprec.tcSafCode        = tFADepreciationDetSaf.tcSafCode.
        end.

        <M-5 run AddStandardPosting (input  vcRowID (icPostingtcRowid), 
                             input  if not tFADepreciationDet.tcCumulativeDepreciationGLCode eq '':U and not tFADepreciationDet.tcCumulativeDepreciationGLCode eq ? then tFADepreciationDet.tcCumulativeDepreciationGLCode else tFADepreciationDet.tcCostDepreciationGLCode (icGLCode), 
                             input  tFADepreciationDet.tcDivisionCode (icDivisionCode), 
                             input  tFADepreciationDet.tcCostCentreCode (icCostCentreCode), 
                             input  '':U (icCostCentreText), 
                             input  tFADepreciationDet.tcProjectCode (icProjectCode), 
                             input  '':U (icProjectText), 
                             input  '':U (icIntercoBusinessRelationCode), 
                             input  vcCompanyLC (icCurrencyCode), 
                             input  vdDebitLC (idDebitTC), 
                             input  vdDebitLC (idDebitLC), 
                             input  vdDebitCC (idDebitCC), 
                             input  ? (idDebitPC), 
                             input  vdCreditLC (idCreditTC), 
                             input  vdCreditLC (idCreditLC), 
                             input  vdCreditCC (idCreditCC), 
                             input  ? (idCreditPC), 
                             input  0 (idQty), 
                             input  tFADepreciationDet.FADepreciationPostingDescr (icLineText), 
                             input  '':U (icSafText), 
                             input  tDefaultSafValuesFADeprec (tDefaultSafs), 
                             input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                             input  1 (idExchangeRate), 
                             input  1 (idExchangeRateScale), 
                             output viPostingLineID (oiPostingLineId), 
                             input  ? (iiSafStructureId), 
                             input  vcSafStructureCode (icSafStructureCode), 
                             input  ? (icAllocationKey), 
                             output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
        then assign oiReturnStatus = viFcReturnSuper.
    end.
    /*ONLY FOR OFFICIAL BOOKS*/
    if not tFADepreciation.tlFABookTypeIsOfficial 
    then next.
    /*get all non-official books of the asset*/
    if first-of (tFADepreciation.tiFAAsset_ID)
    then do:
        empty temp-table tqFAAssetBookByFADeprecAddPost.
        <Q-18 run FAAssetBookByFADeprecAddPost (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input tFADepreciation.tiFAAsset_ID, (FAAssetId)
           input false, (FABookTypeIsOfficial)
           output dataset tqFAAssetBookByFADeprecAddPost) in BFixedAssetAsset >
    end.
    /*additional postings into those non-official books retrieved above*/
    for each tqFAAssetBookByFADeprecAddPost: 
        /* Add Posting Header */
        assign vcPostingText = if vcText <> "":U and
                                  vcText <> ?
                               then vcText
                               else (trim(#T-9'Rev Deprec':20(14580)T-9#) + " ":U +
                                     trim(tFADepreciation.tcFAAssetCode) + " ":U +
                                     trim(tFADepreciation.tcFAAssetBookCode) + " ":U +
                                     string(tFADepreciation.FADepreciationYear,"9999":U) + "/":U +
                                     string(tFADepreciation.FADepreciationMonth,"99":U)).
        
        <M-8 run AddPostingHeader
           (input  ? (iiCompanyId), 
            input  viPostingYear (iiPeriodYear), 
            input  viPostingPeriod (iiPeriodPeriod), 
            input  tqFAAssetBookByFADeprecAddPost.tcJournalCode (icJournalCode), 
            input  '' (icReportingJournalCode), 
            input  0 (iiVoucher), 
            input  vtPostingDate (itPostingDate), 
            input  ? (itValueDate), 
            input  vcPostingText (icPostingText), 
            input  '':U (icPostingBusinessRelationText), 
            input  '':U (icPostingInvoiceReferenceText), 
            input  '':U (icPostingParentText), 
            input  0 (iiBPeriodId), 
            input  ? (icPostingOriginAddressCode), 
            input  ? (icPostingOriginDocument), 
            input  ? (icPostingOriginDocumentType), 
            input  '':U (icBatchNumber), 
            input  ? (icBankImpLineRef), 
            output viPostingID (oiPostingId), 
            output vcRowid (ocRowid), 
            output viPostingVoucher (oiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
        then assign oiReturnStatus = viFcReturnSuper.
        
        /*do not continue with this posting if there were errors*/
        if viFcReturnSuper < 0
        then next.
        
        /*create tFADepreciationPosting*/        
        <M-17 run AddDetailLine (input  'FADepreciationPosting':U (icTable), 
                             input  tFADepreciation.tc_Rowid (icParentRowid), 
                             output viFcReturnSuper (oiReturnStatus)) in BFixedAssetDepreciation>
        
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
        then assign oiReturnStatus = viFcReturnSuper.
        
        if available tFADepreciationPosting
        then assign tFADepreciationPosting.FADepreciationPostingType = {&FAPOSTINGTYPE-REVERSE}
                    tFADepreciationPosting.Posting_ID      = viPostingId
                    tFADepreciationPosting.tiPeriodYear    = viPostingYear
                    tFADepreciationPosting.tiPeriodPeriod  = viPostingPeriod
                    tFADepreciationPosting.tcJournalCode   = tqFAAssetBookByFADeprecAddPost.tcJournalCode
                    tFADepreciationPosting.tiPostingVoucher= viPostingVoucher.
                      
        /* Add PostingLines for each tFADepreciationDet */    
        for each tFADepreciationDet where
                 tFADepreciationDet.tc_ParentRowid = tFADepreciation.tc_Rowid and
                 tFADepreciationDet.tc_Status      = 'N':U:
            /*REVERSE POSTING, so amount > 0 is posted on Debit*/             
            /* Cumulative/Cost GL */
            assign vdDebitCC  = ?
                   vdCreditCC = ?.
                   
            if tFADepreciationDet.FADepreciationDetAmountLC > 0 
            then assign vdCreditLC = tFADepreciationDet.FADepreciationDetAmountLC
                        vdDebitLC  = 0.
            else assign vdCreditLC = 0
                        vdDebitLC  = tFADepreciationDet.FADepreciationDetAmountLC * -1.
            
            if vlCZTax
            then do:
                if tFADepreciationDet.FADepreciationDetAmountCC > 0 
                then assign vdDebitCC  = tFADepreciationDet.FADepreciationDetAmountCC
                            vdCreditCC = 0.
                else assign vdDebitCC  = 0
                            vdCreditCC = tFADepreciationDet.FADepreciationDetAmountCC * -1.        
            end.    
            
            empty temp-table tDefaultSafValuesFADeprec.
            
            assign vcSafStructureCode = '':U.
            for each tFADepreciationDetSaf where
                     tFADepreciationDetSaf.tc_ParentRowid = tFADepreciationDet.tc_Rowid and
                     tFADepreciationDetSaf.tc_Status      = 'N':U:
                
                if vcSafStructureCode = '':U
                then assign vcSafStructureCode = tFADepreciationDetSaf.tcSafStructureCode.
    
                create tDefaultSafValuesFADeprec.
                assign tDefaultSafValuesFADeprec.tcSafConceptCode = tFADepreciationDetSaf.tcSafConceptCode
                       tDefaultSafValuesFADeprec.tcSafCode        = tFADepreciationDetSaf.tcSafCode.
            end.
    
            <M-10 run AddStandardPosting (input  vcRowID (icPostingtcRowid), 
                              input  if not tFADepreciationDet.tcCumulativeDepreciationGLCode eq '':U and not tFADepreciationDet.tcCumulativeDepreciationGLCode eq ? then tFADepreciationDet.tcCumulativeDepreciationGLCode else tFADepreciationDet.tcCostDepreciationGLCode (icGLCode), 
                              input  tFADepreciationDet.tcDivisionCode (icDivisionCode), 
                              input  tFADepreciationDet.tcCostCentreCode (icCostCentreCode), 
                              input  '':U (icCostCentreText), 
                              input  tFADepreciationDet.tcProjectCode (icProjectCode), 
                              input  '':U (icProjectText), 
                              input  '':U (icIntercoBusinessRelationCode), 
                              input  vcCompanyLC (icCurrencyCode), 
                              input  vdDebitLC (idDebitTC), 
                              input  vdDebitLC (idDebitLC), 
                              input  vdDebitCC (idDebitCC), 
                              input  ? (idDebitPC), 
                              input  vdCreditLC (idCreditTC), 
                              input  vdCreditLC (idCreditLC), 
                              input  vdCreditCC (idCreditCC), 
                              input  ? (idCreditPC), 
                              input  0 (idQty), 
                              input  tFADepreciationDet.FADepreciationPostingDescr (icLineText), 
                              input  '':U (icSafText), 
                              input  tDefaultSafValuesFADeprec (tDefaultSafs), 
                              input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                              input  1 (idExchangeRate), 
                              input  1 (idExchangeRateScale), 
                              output viPostingLineID (oiPostingLineId), 
                              input  ? (iiSafStructureId), 
                              input  vcSafStructureCode (icSafStructureCode), 
                              input  ? (icAllocationKey), 
                              output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
            then assign oiReturnStatus = viFcReturnSuper.
        end.
    end.
end.

/* Validate Component and Additional updates */
if vlBJournalEntryStartedFromFADep = true
then do:
    <M-11 run ValidateBCAndAdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
    then assign oiReturnStatus = viFcReturnSuper.
end.
/* close the BJournalEntry instance    */
<I-12 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>