project QadFinancials > class BPosting > method PreSave

Description

Actions to take before writing current instance to the database (inside the db transaction!).
Use the field tc_status to test the status of the updated records:
'' = unchanged
'N' = new
'C' = changed
'D' = deleted


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program/bposting.p)

<ANCESTOR-CODE>

/* ====================================================================================== */
/* Execute some extravalidations to ensure sub-ledger and general ledger are in line      */
/* ====================================================================================== */
<M-22 run PreSaveValidate  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper <  0 then return.
        
/* ====================================================================================== */
/* If the system created auto-balancing posting, send notification mail about             */
/* ====================================================================================== */
if can-find(first tAutoBalanceMailNotif)
then do:
    <M-92 run PreSaveAutoBalNotifMailSend  (output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper <  0 then return.
end.


/* ====================================================================================== *
 * Do the update of the Cross company postings, which are already in the database, but    *
 * which still does not have relation with the couterpart of the cross company posting    *
 * ====================================================================================== */
for each tPosting where 
         tPosting.tc_status <> "D":U,
    each tPostingLine where 
         tPostingLine.tc_ParentRowid              = tPosting.tc_rowid   and
         tPostingLine.tc_status                  <> "D":U               and
         tPostingLine.tcGLTypeCode                = {&GLTYPECODE-CROSS} and
         tPostingLine.CrossCompanyPostingLine_ID <> 0                   and
         tPostingLine.CrossCompanyPostingLine_ID <> ?                   and
         tPostingLine.PostingLineCrossExternRef  <> "":U                and 
         tPostingLine.PostingLineCrossExternRef  <> ?:
                 
    /* Check, if the linked CrossCompanyPosting has already cross company details */
    <Q-23 assign vlFcQueryRecordsAvailable = PostingLineForCrossCyLink (NoCache)
       (input ?, (CompanyId)
        input tPostingLine.CrossCompanyPostingLine_ID, (PostingLineId)) in BPosting >
    if vlFcQueryRecordsAvailable = false
    then next.

    /* Get business relation for cross company posting */
    <M-9 run CalcCrossCompanyBusinessRelation
       (input  tPostingLine.Company_ID (iiCrossCompanyId), 
        input  ? (icCrossCompanyCode), 
        output vcDummy (ocBusinessRelationICCode), 
        output viBusinessRelationId (oiBusinessRelationId), 
        output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper <  0 then return.

    /* else: make sure that single record is available in tqPostingLineByPostingLineCrossExternRef */
    <M-4 run StartPersistence (output vhFcComponent (ohPersistence), 
                       output viFcReturnSuper (oiReturnStatus)) in BPosting>
    if viFcReturnSuper <> 0
    then do :
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then return.
    end. /* if viFcReturnSuper <> 0 */

    assign vcWhereClause = "for each PostingLine where PostingLine.PostingLine_ID = ":U + string (tPostingLine.CrossCompanyPostingLine_ID).
    <M-5 run WriteDirect
       (input  'PostingLine':U (icTableName), 
        input  vcWhereClause (icPrepare), 
        input  'CrossCompany_ID,CrossCompanyPosting_ID,CrossCompanyPostingLine_ID,IntercoBusinessRelation_ID':U (icFieldList), 
        input  'i,i,i':U (icFieldListDataTypes), 
        input  string(tPostingLine.Company_ID) + chr(2) + string(tPostingLine.Posting_ID) + chr(2) + string(tPostingLine.PostingLine_ID) + chr(2) + string(viBusinessRelationId) (icAbsolute), 
        input  '':U (icIncremental), 
        input  {&TARGETPROCEDURE} (ihClass), 
        input  vcUserLogin (icUserLogin), 
        output viFcReturnSuper (oiReturnStatus)) in Progress>        
                                                 
    if viFcReturnSuper <> 0
    then do :
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then return.
    end. /* if viFcReturnSuper <> 0 */
end. /* for each tPosting, each tPostingLine */

/* ================================================================================================ */
/* Before the real db-commit is done we check that the posting is balanced. When all components     */
/* that use BPosting and BJournalEntry are correctly programmed (set vlPostingMustBeBalnced to      */
/* true before the transaction gets committed) then this check is redundant. We add it here anyway  */
/* because we cannot allow postings to be unbalance due to a programming-error is another component */
/* ================================================================================================ */
for each tPosting where tposting.tc_Status <> "D":U :
    assign vdBalanceLC = 0
           vdBalanceCC = 0.
    for each tPostingLine where
             tPostingLine.tc_ParentRowid = tPosting.tc_Rowid and
             tPostingLine.tc_Status <> "D":U :
        assign vdBalanceLC = vdBalanceLC + tPostingLine.PostingLineDebitLC - tPostingLine.PostingLineCreditLC
               vdBalanceCC = vdBalanceCC + tPostingLine.PostingLineDebitCC - tPostingLine.PostingLineCreditCC.
    end. /* for each tPostingLine where */
    if vdBalanceLC <> 0 or 
       vdBalanceCC <> 0
    then do :
        assign vcMsgExplanation = <M-11 GetErrorExplanation
                                     (input  tPosting.tc_Rowid (icPostingRowId), 
                                      input  ? (icPostingLineRowId), 
                                      output viFcReturnSuper (oiReturnStatus)) in BPosting>.
                               
        assign vcMessage = trim(substitute(#T-2'The posting &1 is not balanced.':255(681307785)T-2#,trim(string(tPosting.PostingYear)) + "/":U + trim(tPosting.tcJournalCode) + "/":U + trim(string(tPosting.PostingVoucher)),string(vdBalanceLC)))
               oiReturnStatus = -1.
        if vdBalanceLC <> 0
        then assign vcMessage = vcMessage + " ":U + trim(substitute(#T-66'The difference &1 in base currency was detected.':255(249351708)T-66#,string(vdBalanceLC))).
        if vdBalanceCC <> 0
        then assign vcMessage = vcMessage + (if vdBalanceLC <> 0 then chr(10) else " ":U ) + trim(substitute(#T-88'The difference &1 in statutory currency was detected.':255(480873448)T-88#,string(vdBalanceCC))).                   
        <M-1 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  'tPosting.PostingOriginReference':U (icFieldName), 
            input  tPosting.PostingOriginReference (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tPosting.tc_Rowid (icRowid), 
            input  'QADFIN-1323':U (icFcMsgNumber), 
            input  vcMsgExplanation (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BPosting>
    end. /* if  */
end. /* for each tPosting */    

/* ================================================================================================ */
/* Before save, we need to check if the postingtax type is WHT. If result is yes, so move the Vat   */
/* data into WHT table.                                                                             */
/* ================================================================================================ */
for each tPosting where tposting.tc_Status <> "D":U :
    for each tPostingLine where
        tPostingLine.tc_ParentRowid = tPosting.tc_Rowid and
        tPostingLine.tc_Status <> "D":U :
        
        for each tPostingVat where 
            tPostingVat.tc_ParentRowid = tPostingLine.tc_Rowid and
            tPostingVat.TxtyTaxType <> '':U and 
            tPostingVat.TxtyTaxType <> ? and
            tPostingVat.tc_Status = "N":U:
            
            <Q-84 run MfgTaxTypeByTaxTypeV01 (all) (Read) (NoCache)
               (input tPostingVat.TxtyTaxType, (TaxTypeCode)
                input yes, (TaxTypeIsWHT)
                output dataset tqMfgTaxTypeByTaxTypeV01) in BMfgTaxType>
            find first tqMfgTaxTypeByTaxTypeV01 where
                tqMfgTaxTypeByTaxTypeV01.tltxty_wt_type = yes no-error.
            if available tqMfgTaxTypeByTaxTypeV01 then
            do:
                <M-55 run AddDetailLine
                   (input  'PostingWHT':U (icTable), 
                    input  tPostingLine.tc_Rowid (icParentRowid), 
                    output  viFcReturnSuper (oiReturnStatus)) in BPosting>

                assign tPostingWHT.Currency_ID                = tPostingVat.Currency_ID
                       tPostingWHT.PeriodMark_ID              = tPostingVat.PeriodMark_ID
                       tPostingWHT.Posting_ID                 = tPostingVat.Posting_ID
                       tPostingWHT.PostingLine_ID             = tPostingVat.PostingLine_ID
                       tPostingWHT.Vat_ID                     = tPostingVat.Vat_ID
                       tPostingWHT.PostingWHTBaseCreditCC     = tPostingVat.PostingVatBaseCreditCC
                       tPostingWHT.PostingWHTBaseCreditLC     = tPostingVat.PostingVatBaseCreditLC
                       tPostingWHT.PostingWHTBaseCreditTC     = tPostingVat.PostingVatBaseCreditTC
                       tPostingWHT.PostingWHTBaseDebitCC      = tPostingVat.PostingVatBaseDebitCC
                       tPostingWHT.PostingWHTBaseDebitLC      = tPostingVat.PostingVatBaseDebitLC
                       tPostingWHT.PostingWHTBaseDebitTC      = tPostingVat.PostingVatBaseDebitTC
                       tPostingWHT.PostingWHTExchangeRate     = tPostingVat.PostingVatExchangeRate
                       tPostingWHT.PostingWHTInOut            = tPostingVat.PostingVatInOut
                       tPostingWHT.PostingWHTOwnTaxDeclarat   = tPostingVat.PostingVatOwnTaxDeclarat
                       tPostingWHT.PostingWHTOwnTaxIDFeder    = tPostingVat.PostingVatOwnTaxIDFeder
                       tPostingWHT.PostingWHTOwnTaxIDMisc1    = tPostingVat.PostingVatOwnTaxIDMisc1
                       tPostingWHT.PostingWHTOwnTaxIDMisc2    = tPostingVat.PostingVatOwnTaxIDMisc2
                       tPostingWHT.PostingWHTOwnTaxIDMisc3    = tPostingVat.PostingVatOwnTaxIDMisc3
                       tPostingWHT.PostingWHTOwnTaxIDState    = tPostingVat.PostingVatOwnTaxIDState
                       tPostingWHT.PostingWHTRateScale        = tPostingVat.PostingVatRateScale
                       tPostingWHT.PostingWHTTaxCreditCC      = tPostingVat.PostingVatTaxCreditCC
                       tPostingWHT.PostingWHTTaxCreditLC      = tPostingVat.PostingVatTaxCreditLC
                       tPostingWHT.PostingWHTTaxCreditTC      = tPostingVat.PostingVatTaxCreditTC
                       tPostingWHT.PostingWHTTaxDebitCC       = tPostingVat.PostingVatTaxDebitCC
                       tPostingWHT.PostingWHTTaxDebitLC       = tPostingVat.PostingVatTaxDebitLC
                       tPostingWHT.PostingWHTTaxDebitTC       = tPostingVat.PostingVatTaxDebitTC
                       tPostingWHT.PostingWHTTaxIDFeder       = tPostingVat.PostingVatTaxIDFeder
                       tPostingWHT.PostingWHTTaxIDMisc1       = tPostingVat.PostingVatTaxIDMisc1
                       tPostingWHT.PostingWHTTaxIDMisc2       = tPostingVat.PostingVatTaxIDMisc2
                       tPostingWHT.PostingWHTTaxIDMisc3       = tPostingVat.PostingVatTaxIDMisc3
                       tPostingWHT.PostingWHTTaxIDState       = tPostingVat.PostingVatTaxIDState
                       tPostingWHT.PostingWHTTaxPointDate     = tPostingVat.PostingVatTaxPointDate
                       tPostingWHT.PostingWHTTaxTransType     = tPostingVat.PostingVatTaxTransType
                       tPostingWHT.PostingWHTTransType        = tPostingVat.PostingVatTransType
                       tPostingWHT.PostingWHTFromTxzTaxZone   = tPostingVat.FromTxzTaxZone
                       tPostingWHT.PostingWHTToTxzTaxZone     = tPostingVat.ToTxzTaxZone
                       tPostingWHT.tcCurrencyCode             = tPostingVat.tcCurrencyCode
                       tPostingWHT.tcDomainCode               = tPostingVat.tcDomainCode
                       tPostingWHT.tcVatCode                  = tPostingVat.tcVatCode
                       tPostingWHT.tcVatInOut                 = tPostingVat.tcVatInOut
                       tPostingWHT.tdFullCreditTC             = tPostingVat.tdFullCreditTC
                       tPostingWHT.tdFullDebitTC              = tPostingVat.tdFullDebitTC
                       tPostingWHT.PostingWHTTxclTaxCls       = tPostingVat.TxclTaxCls
                       tPostingWHT.PostingWHTTxenvTaxEnv      = tPostingVat.TxenvTaxEnv
                       tPostingWHT.PostingWHTTxtyTaxType      = tPostingVat.TxtyTaxType
                       tPostingWHT.PostingWHTTxutaxusage      = tPostingVat.Txutaxusage.
                /* Update Vat Status */
                /*assign tPostingVat.tc_Status                  = '':U.*/
                delete tPostingVat.
            end.
        end. /* for each tPostingVat */
    end. /* for each tPostingLine */
end. /* for each tPosting */