project QadFinancials > class BDInvoice > method UpdateDeductionDetailPosting

Description

Private method , which is used in UpdateDeductionDetail. it completes the posting generation on the current entity and return the pending across-company postings.


Parameters


tApiUpdateDeductionTmpinputtemp-table
tNewCrossCyPostingDeductionoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.UpdateDeductionDetail


program code (program9/bdinvoice.p)

/* =========================================================== */
/* This method will process new-approved deduction details.
   To generate posting (debit on the deduction expensed account and
   credit on the customer deduction control account.
   It will complete the functions as following steps.
   1.  Look up deduction expensed gl account, expensed daybook and posting year/period.
   2.  Generate posting. Here are the accepted scenarios.
       no-cross company:
       condition: tApiUpdateDeductionTmp.tcEntityCode = vcCompanyCode or '' or ?
       the posting will be like:
          Entity: vcCompanyCode
          DR: deduction expensed gl account
          CR: customer deduction control account
       cross company:
       condition: tApiUpdateDeductionTmp.tcEntityCode <> vcCompanyCode and <> '' and <>?
       the posting will be like:
           Entity: vcCompanyCode
           DR: cross-company account.
           CR: customer deduction control account
    3. In cross company scenario, build the return result "tNewCrossCyPostingDeduction", which
       will be processed in BJournalEntry by the caller 'BDInvoiceMultiCy'.
       the posting will be like:
           Entity: tcEntityCode
           DR: deduction expensed gl account
           CR: cross-company account.       
*/
/* =========================================================== */
assign oiReturnStatus = -98
       vcDInvoiceDeductionIDs = '':U
       vcPostingHeaderRowId = '':U.


empty temp-table tDIMovement.
empty temp-table tDInvoiceStageUpdates.
empty temp-table tNewCrossCyPostingDeduction. 
empty temp-table tDeductionDetailInfo.
empty temp-table tDeductionDetailPostingPeriod.

/* Deduction Invoice not available */ 
if not can-find(first tDInvoice) then
do:
    assign oiReturnStatus = 0.
    return.
end.

if not can-find(first tApiUpdateDeductionTmp where tApiUpdateDeductionTmp.tlNewApprovedWriteOff = yes) then
do:
    assign oiReturnStatus = 0.
    return.
end.

for each tDInvoice no-lock:
    for each tApiUpdateDeductionTmp where tApiUpdateDeductionTmp.tiDInvoiceID = tDInvoice.DInvoice_ID 
                                         and tApiUpdateDeductionTmp.tlNewApprovedWriteOff = yes
                                     no-lock:
        /*Look up the expense daybook and gl */
        find first tDeductionDetailInfo where tDeductionDetailInfo.tcDeductionCatCode
                                        = tApiUpdateDeductionTmp.tcDeductionCatCode
                                        and tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiDomainIdTmp  no-lock no-error.
        
        if not available tDeductionDetailInfo then
        do:  
            /* get expense account info. entity is invoice level */
            if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? then
            do:                   
                <Q-32 run DeductionCatLineExpenseAccount (all) (Read) (NoCache)
                   (input tDInvoice.Company_ID, (CompanyId)
                    input tApiUpdateDeductionTmp.tiDeductionCatLineId, (DeductionCatLine_ID)
                    output dataset tqDeductionCatLineExpenseAccount) in BDeductionCat>
                if can-find(first tqDeductionCatLineExpenseAccount) then
                do:
                    create tDeductionDetailInfo.
                    assign tDeductionDetailInfo.tcDeductionCatCode = tApiUpdateDeductionTmp.tcDeductionCatCode
                           tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiDomainIdTmp
                           viCounter = 0.
                           
                    for each tqDeductionCatLineExpenseAccount:
                        if viCounter = 0 then
                        do:
                            assign tDeductionDetailInfo.tcGLCode = tqDeductionCatLineExpenseAccount.tcGLCode
                                   tDeductionDetailInfo.tcCostCenter = tqDeductionCatLineExpenseAccount.tcCostCentreCode
                                   tDeductionDetailInfo.tcSubAccount = tqDeductionCatLineExpenseAccount.tcDivisionCode
                                   tDeductionDetailInfo.tcSafCode1 = tqDeductionCatLineExpenseAccount.tcSafCode
                                   tDeductionDetailInfo.tcExpenseDaybookCode = tqDeductionCatLineExpenseAccount.tcJournalCode
                                   tDeductionDetailInfo.tcSafConceptCode1 = tqDeductionCatLineExpenseAccount.tcSafConceptCode.
                        end.
                        if viCounter = 1 then
                        do:
                            assign tDeductionDetailInfo.tcSafCode2 = tqDeductionCatLineExpenseAccount.tcSafCode
                                   tDeductionDetailInfo.tcSafConceptCode2 = tqDeductionCatLineExpenseAccount.tcSafConceptCode.
                        end.
                        if viCounter = 2 then
                        do:
                            assign tDeductionDetailInfo.tcSafCode3 = tqDeductionCatLineExpenseAccount.tcSafCode
                                   tDeductionDetailInfo.tcSafConceptCode3 = tqDeductionCatLineExpenseAccount.tcSafConceptCode.
                        end.
                        if viCounter = 3 then
                        do:
                            assign tDeductionDetailInfo.tcSafCode4 = tqDeductionCatLineExpenseAccount.tcSafCode
                                   tDeductionDetailInfo.tcSafConceptCode4 = tqDeductionCatLineExpenseAccount.tcSafConceptCode.
                        end.
                        if viCounter = 4 then
                        do:
                            assign tDeductionDetailInfo.tcSafCode5 = tqDeductionCatLineExpenseAccount.tcSafCode
                                   tDeductionDetailInfo.tcSafConceptCode5 = tqDeductionCatLineExpenseAccount.tcSafConceptCode.
                        end.
                        if viCounter > 4 then
                        do:
                            leave.
                        end.
                        assign viCounter = viCounter + 1.
                    end.
                end. /* end if can-find(first tqDeductionCatLineExpenseAccount) */
                else
                do:
                    assign vcMessage = trim(substitute(#T-35'The expense daybook or expense account is not specified in the Deduction Category Code &1':255(21506847)T-35#,tApiUpdateDeductionTmp.tcDeductionCatCode ))
                           oiReturnStatus = -1.
                    <M-71 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'qadfin-464644':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  ? (icFcIdentification), 
                        input  ? (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                    return.
                end. 
             end. /* end of if tApiUpdateDeductionTmp.tcGLCode = '' or ? */
             else
             do:  
                 /* Only get the expense daybook info */                          
                 <Q-66 run DeductionCatExpenseInfoByCode (all) (Read) (NoCache)
                    (input ?, (CompanyId)
                     input tApiUpdateDeductionTmp.tcDeductionCatCode, (DeductionCatCode)
                     input viDomainID, (Domain_ID)
                     output dataset tqDeductionCatExpenseInfoByCode) in BDeductionCat>
                 find first tqDeductionCatExpenseInfoByCode no-lock no-error.
                 if not available tqDeductionCatExpenseInfoByCode then 
                 do:
                     assign vcMessage = trim(substitute(#T-10'The expense daybook is not specified in the Deduction Category Code &1':255(137814909)T-10#,tApiUpdateDeductionTmp.tcDeductionCatCode ))
                            oiReturnStatus = -1.
                     <M-5 run SetMessage
                        (input  vcMessage (icMessage), 
                         input  '':U (icArguments), 
                         input  '':U (icFieldName), 
                         input  '':U (icFieldValue), 
                         input  'E':U (icType), 
                         input  3 (iiSeverity), 
                         input  '':U (icRowid), 
                         input  'qadfin-119666':U (icFcMsgNumber), 
                         input  '':U (icFcExplanation), 
                         input  ? (icFcIdentification), 
                         input  ? (icFcContext), 
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                     return.
                 end.
                 create tDeductionDetailInfo.
                 assign tDeductionDetailInfo.tcDeductionCatCode = tApiUpdateDeductionTmp.tcDeductionCatCode
                        tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiDomainIdTmp
                        tDeductionDetailInfo.tcExpenseDaybookCode = tqDeductionCatExpenseInfoByCode.tcExpenseJournalCode
                        tDeductionDetailInfo.tiExpenseJournal_ID = tqDeductionCatExpenseInfoByCode.tiExpenseJournal_ID.

             end. /* end elseif tApiUpdateDeductionTmp.tcGLCode = '' or ? */
        
        end. /* end not available tDeductionDetailInfo */
        
        /*Look up the expense daybook and gl on the expense entity */
       
        if tApiUpdateDeductionTmp.tiExpensedCompanyID <> ? and tApiUpdateDeductionTmp.tiExpensedCompanyID <> 0 then
        do: 
            find first tDeductionDetailInfo where tDeductionDetailInfo.tcDeductionCatCode
                                            = tApiUpdateDeductionTmp.tcDeductionCatCode
                                            and tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiExpensedDomainID no-lock no-error.
            if not available tDeductionDetailInfo then
            do:
                if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? then
                do:
                    <Q-61 run DedCatLineExpenseAccByCode (all) (Read) (NoCache)
                       (input tApiUpdateDeductionTmp.tcDeductionCatCode, (DeductionCatCode)
                        input tApiUpdateDeductionTmp.tiExpensedDomainID, (Domain_ID)
                        output dataset tqDedCatLineExpenseAccByCode) in BDeductionCat>
                    if can-find(first tqDedCatLineExpenseAccByCode) then
                    do:
                        create tDeductionDetailInfo.
                        assign tDeductionDetailInfo.tcDeductionCatCode = tApiUpdateDeductionTmp.tcDeductionCatCode
                               tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiExpensedDomainID
                               viCounter = 0.
                               
                        for each tqDedCatLineExpenseAccByCode:
                            if viCounter = 0 then
                            do:
                                assign tDeductionDetailInfo.tcGLCode = tqDedCatLineExpenseAccByCode.tcGLCode
                                       tDeductionDetailInfo.tcCostCenter = tqDedCatLineExpenseAccByCode.tcCostCentreCode
                                       tDeductionDetailInfo.tcSubAccount = tqDedCatLineExpenseAccByCode.tcDivisionCode
                                       tDeductionDetailInfo.tcExpenseDaybookCode = tqDedCatLineExpenseAccByCode.tcJournalCode
                                       tDeductionDetailInfo.tcSafCode1 = tqDedCatLineExpenseAccByCode.tcSafCode
                                       tDeductionDetailInfo.tcSafConceptCode1 = tqDedCatLineExpenseAccByCode.tcSafConceptCode.
                            end.
                            if viCounter = 1 then
                            do:
                               assign tDeductionDetailInfo.tcSafCode2 = tqDedCatLineExpenseAccByCode.tcSafCode
                                      tDeductionDetailInfo.tcSafConceptCode2 = tqDedCatLineExpenseAccByCode.tcSafConceptCode.
                            end.
                            if viCounter = 2 then
                            do:
                               assign tDeductionDetailInfo.tcSafCode3 = tqDedCatLineExpenseAccByCode.tcSafCode
                                      tDeductionDetailInfo.tcSafConceptCode3 = tqDedCatLineExpenseAccByCode.tcSafConceptCode.
                            end.
                            if viCounter = 3 then
                            do:
                               assign tDeductionDetailInfo.tcSafCode4 = tqDedCatLineExpenseAccByCode.tcSafCode
                                      tDeductionDetailInfo.tcSafConceptCode4 = tqDedCatLineExpenseAccByCode.tcSafConceptCode.
                            end.
                            if viCounter = 4 then
                            do:
                               assign tDeductionDetailInfo.tcSafCode5 = tqDedCatLineExpenseAccByCode.tcSafCode
                                      tDeductionDetailInfo.tcSafConceptCode5 = tqDedCatLineExpenseAccByCode.tcSafConceptCode.
                            end.
                            if viCounter > 4 then
                            do:
                               leave.
                            end.
                            assign viCounter = viCounter + 1.
                        end. 
                   end. /* end if can-find(first tqDedCatLineExpenseAccByCode) */
                   else
                   do:
                       assign vcMessage = trim(substitute(#T-41'The expense daybook or expense account is not specified in the Deduction Category Code &1':255(21506847)T-41#,tApiUpdateDeductionTmp.tcDeductionCatCode ))
                              oiReturnStatus = -1.
                       <M-2 run SetMessage
                          (input  vcMessage (icMessage), 
                           input  '':U (icArguments), 
                           input  '':U (icFieldName), 
                           input  '':U (icFieldValue), 
                           input  'E':U (icType), 
                           input  3 (iiSeverity), 
                           input  '':U (icRowid), 
                           input  'qadfin-270933':U (icFcMsgNumber), 
                           input  '':U (icFcExplanation), 
                           input  ? (icFcIdentification), 
                           input  ? (icFcContext), 
                           output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                       return.
                   end. 
                end. /* end if tApiUpdateDeductionTmp.tcGLCode = '':U or ? . */
                else do:
                    <Q-70 run DeductionCatExpenseInfoByCode (all) (Read) (NoCache)
                       (input ?, (CompanyId)
                        input tApiUpdateDeductionTmp.tcDeductionCatCode, (DeductionCatCode)
                        input tApiUpdateDeductionTmp.tiExpensedDomainID, (Domain_ID)
                        output dataset tqDeductionCatExpenseInfoByCode) in BDeductionCat>
                       
                    find first tqDeductionCatExpenseInfoByCode no-lock no-error.
                    if not available tqDeductionCatExpenseInfoByCode then 
                    do:
                        assign vcMessage = trim(substitute(#T-14'The expense daybook is not specified in the Deduction Category Code &1':255(137814909)T-14#,tApiUpdateDeductionTmp.tcDeductionCatCode ))
                               oiReturnStatus = -1.
                        <M-75 run SetMessage
                           (input  vcMessage (icMessage), 
                            input  '':U (icArguments), 
                            input  '':U (icFieldName), 
                            input  '':U (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  '':U (icRowid), 
                            input  'qadfin-428161':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  ? (icFcIdentification), 
                            input  ? (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                        return.
                    end.
                    create tDeductionDetailInfo.
                    assign tDeductionDetailInfo.tcDeductionCatCode = tApiUpdateDeductionTmp.tcDeductionCatCode
                           tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiExpensedDomainID
                           tDeductionDetailInfo.tcExpenseDaybookCode = tqDeductionCatExpenseInfoByCode.tcExpenseJournalCode
                           tDeductionDetailInfo.tiExpenseJournal_ID = tqDeductionCatExpenseInfoByCode.tiExpenseJournal_ID. 
                end. /* end else if tApiUpdateDeductionTmp.tcGLCode = '':U or ?. */
            end. /* end not available tDeductionDetailInfo */
        end. /* tApiUpdateDeductionTmp.tiExpensedCompanyID <> ? and tApiUpdateDeductionTmp.tiExpensedCompanyID <> 0 */
        
        /*Look up the posting period*/
        find first tDeductionDetailPostingPeriod where tDeductionDetailPostingPeriod.ttDeductionPostingDate = tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate no-lock no-error.
        if not available tDeductionDetailPostingPeriod then 
        do:
            <M-9 run GetPeriodDateForDeduction
               (input  tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate (itPostingDate), 
                output viPostingYear (oiPostingYear), 
                output viPostingPeriod (oiPostingPeriod), 
                output vtPostingDate (otPostingDateOut), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
             if viFcReturnSuper < 0 then
             do:
                 assign vcMessage = trim(substitute(#T-54'The posting year and period is not defined for the date $1.':255(817044894)T-54#,string(tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate)))
                        oiReturnStatus = -1.
                 <M-39 run SetMessage
                    (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'E':U (icType), 
                     input  3 (iiSeverity), 
                     input  ? (icRowid), 
                     input  'qadfin-195296':U (icFcMsgNumber), 
                     input  ? (icFcExplanation), 
                     input  ? (icFcIdentification), 
                     input  ? (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                 return.
             end. 
             create tDeductionDetailPostingPeriod.
             assign tDeductionDetailPostingPeriod.ttDeductionPostingDate = tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate
                       tDeductionDetailPostingPeriod.tiDeductionPostingYear = viPostingYear
                       tDeductionDetailPostingPeriod.tiDeductionPostingPeriod = viPostingPeriod.
        end. /* end if not available tDeductionDetailPostingPeriod */
    end. /* end each tApiUpdateDeductionTmp */
end. /* end each tDInvoice */

/* =========================== */
/* starting the posting part   */
/* =========================== */
if can-find(first tDeductionDetailInfo) then
do:
    /* start journal entry */
    if viBJournalEntryDIID = 0 or viBJournalEntryDIID = ? then
    do:
          <I-57 {bFcStartAndOpenInstance
               &ADD-TO-TRANSACTION   = "true"
               &CLASS                = "BJournalEntry"}>
          assign vlBJEIsStartedFromDI = true.
          
    end.
    else do:
          <I-65 {bFcOpenInstance
               &CLASS           = "BJournalEntry"}>
    end.
    /*
    if viBNumber9ID = 0 or viBNumber9ID = ? then
    do:
        <I-76 {bFcStartAndOpenInstance
             &CLASS                = "BNumber"}>
    end.
    else do:
        <I-56 {bFcOpenInstance
             &CLASS           = "BNumber"}>
    end. */

    for each tDInvoice no-lock:
        for each tApiUpdateDeductionTmp where tApiUpdateDeductionTmp.tiDInvoiceID = tDInvoice.DInvoice_ID
                                              and tApiUpdateDeductionTmp.tlNewApprovedWriteOff = yes
                                              no-lock:
            
            /***************************************/
            /*create posting header                */
            /*                                     */
            /***************************************/
            /*create posting header */
            /*look up expense*/
            find first tDeductionDetailInfo where tDeductionDetailInfo.tcDeductionCatCode
                                             = tApiUpdateDeductionTmp.tcDeductionCatCode
                                             and tDeductionDetailInfo.tiDomain_ID = viDomainID no-lock no-error.

            if not available tDeductionDetailInfo then 
            do:
               <I-19 {bFcCloseAndStopInstance
                    &CLASS           = "BJournalEntry"}>
                    /*
               <I-47 {bFcCloseAndStopInstance
                    &CLASS           = "BNumber"}> */
               return.
            end.
               
            find first tDeductionDetailPostingPeriod where tDeductionDetailPostingPeriod.ttDeductionPostingDate = tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate no-lock no-error.
            if not available tDeductionDetailPostingPeriod then 
            do:
                <I-52 {bFcCloseAndStopInstance
                     &CLASS           = "BJournalEntry"}>
                /*
                <I-12 {bFcCloseAndStopInstance
                    &CLASS           = "BNumber"}> */
                return.
            end.
            assign vhFcComponent = ?.
            /*get voucher number */                  
            <M-67 run GetNumber
               (input  tDInvoice.Company_ID (iiCompanyId), 
                input  tDeductionDetailPostingPeriod.tiDeductionPostingYear (iiNumbrYear), 
                input  tDeductionDetailInfo.tcExpenseDaybookCode (icNumbrType), 
                output viVoucher (oiNumber), 
                input  viFcCurrentInstanceId (iiInstanceId), 
                input  vcFcComponentName (icClassName), 
                output viFcReturnSuper (oiReturnStatus)) in BNumber>
            if viFcReturnSuper < 0 then 
            do:
                assign oiReturnStatus = viFcReturnSuper.
                <I-73 {bFcCloseAndStopInstance
                     &CLASS           = "BJournalEntry"}>
                 /*
                <I-23 {bFcCloseAndStopInstance
                     &CLASS           = "BNumber"}> */
                     
                return.
            end.
               
            <M-36 run AddPostingHeader
               (input  tDInvoice.Company_ID (iiCompanyId), 
                input  tDeductionDetailPostingPeriod.tiDeductionPostingYear (iiPeriodYear), 
                input  tDeductionDetailPostingPeriod.tiDeductionPostingPeriod (iiPeriodPeriod), 
                input  tDeductionDetailInfo.tcExpenseDaybookCode (icJournalCode), 
                input  '':U (icReportingJournalCode), 
                input  viVoucher (iiVoucher), 
                input  tApiUpdateDeductionTmp.ttDInvoiceDeductionPostingDate (itPostingDate), 
                input  ? (itValueDate), 
                input  tDInvoice.DInvoiceDescription (icPostingText), 
                input  '':U (icPostingBusinessRelationText), 
                input  '':U (icPostingInvoiceReferenceText), 
                input  '':U (icPostingParentText), 
                input  ? (iiBPeriodId), 
                input  '':U (icPostingOriginAddressCode), 
                input  '':U (icPostingOriginDocument), 
                input  ? (icPostingOriginDocumentType), 
                input  '':U (icBatchNumber), 
                input  ? (icBankImpLineRef), 
                output viPostingId (oiPostingId), 
                output vcPostingHeaderRowId (ocRowid), 
                output viDummy (oiPostingVoucher), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
             if viFcReturnSuper < 0 then 
             do:
                 assign oiReturnStatus = viFcReturnSuper.
                 <I-91 {bFcCloseAndStopInstance
                      &CLASS           = "BJournalEntry"}>
                      /*
                 <I-93 {bFcCloseAndStopInstance
                      &CLASS           = "BNumber"}> */
                 return.
             end.
             /* set approve posting id. */
             find tDInvoiceDeduction where tDInvoiceDeduction.DInvoice_ID = tApiUpdateDeductionTmp.tiDInvoiceID
                                           and tDInvoiceDeduction.DeductionCatLine_ID = tApiUpdateDeductionTmp.tiDeductionCatLineId 
                                           and tDInvoiceDeduction.PaidDInvoice_ID = tApiUpdateDeductionTmp.tiPaidInvoiceID 
                                           and tDInvoiceDeduction.DInvoiceDeductionSequence = tApiUpdateDeductionTmp.tiDInvoiceDeductionSequence
                                            no-error.
             if available tDInvoiceDeduction then 
                 assign tDInvoiceDeduction.RejectPosting_ID = viPostingId.
             /*create DIMovement. */
             create tDIMovement.
             assign tDIMovement.tiDInvoiceId            = tApiUpdateDeductionTmp.tiDInvoiceID
                                tDIMovement.tcGLAccountDivisionCode = ""
                                tDIMovement.tcPostingRowId          = vcPostingHeaderRowId
                                tDIMovement.tlMovementIsForDraft    = false
                                tDIMovement.tlIsUndoPayment         = false
                                tDIMovement.tdAmountCreditTC        = tApiUpdateDeductionTmp.tdDInvoiceDeductionTC
                                tDIMovement.tdAmountDebitTC         = 0.
             if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? then
             do:
                 assign vcExpensedGLCode = tDeductionDetailInfo.tcGLCode
                        vcExpensedCostCentreCode = tDeductionDetailInfo.tcCostCenter
                        vcExpensedSubAccountCode = tDeductionDetailInfo.tcSubAccount. 
                 /***************************************/
                 /* Build Saf Code                      */
                 /***************************************/   
                 empty temp-table tDefaultSystemSafDI.
                 if tDeductionDetailInfo.tcSafCode1 <> '':U and tDeductionDetailInfo.tcSafCode1 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tDeductionDetailInfo.tcSafCode1
                            tDefaultSystemSafDI.tcSafConceptCode = tDeductionDetailInfo.tcSafConceptCode1.
                 end.  
                 if tDeductionDetailInfo.tcSafCode2 <> '':U and tDeductionDetailInfo.tcSafCode2 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tDeductionDetailInfo.tcSafCode2
                            tDefaultSystemSafDI.tcSafConceptCode = tDeductionDetailInfo.tcSafConceptCode2.
                 end.   
                 if tDeductionDetailInfo.tcSafCode3 <> '':U and tDeductionDetailInfo.tcSafCode3 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tDeductionDetailInfo.tcSafCode3
                            tDefaultSystemSafDI.tcSafConceptCode = tDeductionDetailInfo.tcSafConceptCode3.
                 end.   
                 if tDeductionDetailInfo.tcSafCode4 <> '':U and tDeductionDetailInfo.tcSafCode4 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tDeductionDetailInfo.tcSafCode4
                            tDefaultSystemSafDI.tcSafConceptCode = tDeductionDetailInfo.tcSafConceptCode4.
                 end.   
                 if tDeductionDetailInfo.tcSafCode5 <> '':U and tDeductionDetailInfo.tcSafCode5 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tDeductionDetailInfo.tcSafCode5
                            tDefaultSystemSafDI.tcSafConceptCode = tDeductionDetailInfo.tcSafConceptCode5.
                 end.                                                                                                      
            end.   /* end if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? */               
            else
            do:
                assign vcExpensedGLCode = tApiUpdateDeductionTmp.tcGLCode
                       vcExpensedCostCentreCode = tApiUpdateDeductionTmp.tcCostCenter
                       vcExpensedSubAccountCode = tApiUpdateDeductionTmp.tcSubAccount.
                 /***************************************/
                 /* Build Saf Code                      */
                 /***************************************/                                  
                 empty temp-table tDefaultSystemSafDI.
                 if tApiUpdateDeductionTmp.tcSafCode1 <> '':U and tApiUpdateDeductionTmp.tcSafCode1 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tApiUpdateDeductionTmp.tcSafCode1
                            tDefaultSystemSafDI.tcSafConceptCode = tApiUpdateDeductionTmp.tcSafConceptCode1.
                 end.
                 if tApiUpdateDeductionTmp.tcSafCode2 <> '':U and tApiUpdateDeductionTmp.tcSafCode2 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tApiUpdateDeductionTmp.tcSafCode2
                            tDefaultSystemSafDI.tcSafConceptCode = tApiUpdateDeductionTmp.tcSafConceptCode2.
                 end.
                 if tApiUpdateDeductionTmp.tcSafCode3 <> '':U and tApiUpdateDeductionTmp.tcSafCode3 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tApiUpdateDeductionTmp.tcSafCode3
                            tDefaultSystemSafDI.tcSafConceptCode = tApiUpdateDeductionTmp.tcSafConceptCode3.
                 end.   
                 if tApiUpdateDeductionTmp.tcSafCode4 <> '':U and tApiUpdateDeductionTmp.tcSafCode4 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tApiUpdateDeductionTmp.tcSafCode4
                            tDefaultSystemSafDI.tcSafConceptCode = tApiUpdateDeductionTmp.tcSafConceptCode4.
                 end.
                 if tApiUpdateDeductionTmp.tcSafCode5 <> '':U and tApiUpdateDeductionTmp.tcSafCode5 <> ? then
                 do:
                     create tDefaultSystemSafDI.
                     assign tDefaultSystemSafDI.tcSafCode = tApiUpdateDeductionTmp.tcSafCode5
                            tDefaultSystemSafDI.tcSafConceptCode = tApiUpdateDeductionTmp.tcSafConceptCode5.
                 end.

            end. /* end else if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? */               
            
            
            if tApiUpdateDeductionTmp.tcEntityCode = vcCompanyCode or tApiUpdateDeductionTmp.tcEntityCode = ? 
               or tApiUpdateDeductionTmp.tcEntityCode = '':U then
            do:
              <M-90 run AddStandardPosting
                 (input  vcPostingHeaderRowId (icPostingtcRowid), 
                  input  vcExpensedGLCode (icGLCode), 
                  input  vcExpensedSubAccountCode (icDivisionCode), 
                  input  vcExpensedCostCentreCode (icCostCentreCode), 
                  input  '':U (icCostCentreText), 
                  input  tApiUpdateDeductionTmp.tcProject (icProjectCode), 
                  input  '':U (icProjectText), 
                  input  '':U (icIntercoBusinessRelationCode), 
                  input  tDInvoice.tcCurrencyCode (icCurrencyCode), 
                  input  tApiUpdateDeductionTmp.tdDInvoiceDeductionTC (idDebitTC), 
                  input  tApiUpdateDeductionTmp.tdDInvoiceDeductionLC (idDebitLC), 
                  input  tApiUpdateDeductionTmp.tdDInvoiceDeductionCC (idDebitCC), 
                  input  0 (idDebitPC), 
                  input  0 (idCreditTC), 
                  input  0 (idCreditLC), 
                  input  0 (idCreditCC), 
                  input  0 (idCreditPC), 
                  input  0 (idQty), 
                  input  tDInvoice.DInvoiceDescription (icLineText), 
                  input  '':U (icSafText), 
                  input  tDefaultSystemSafDI (tDefaultSafs), 
                  input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                  input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
                  input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
                  input  tDInvoice.DInvoiceCCRate (idPostingLineCCRate), 
                  input  tDInvoice.DInvoiceCCScale (idPostingLineCCScale), 
                  output viPostingLineId (oiPostingLineId), 
                  input  0 (iiSafStructureId), 
                  input  '':U (icSafStructureCode), 
                  input  ? (icAllocationKey), 
                  input  false (ilLinkedCrCyDaemonReqExists), 
                  output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                if viFcReturnSuper < 0 then
                do:
                    assign oiReturnStatus = viFcReturnSuper.
                    <I-51 {bFcCloseAndStopInstance
                         &CLASS           = "BJournalEntry"}>
                    /*
                    <I-85 {bFcCloseAndStopInstance
                         &CLASS           = "BNumber"}> */
                    return.
                end.
            end. /* if tApiUpdateDeductionTmp.tcEntityCode = vcCompanyCode or tApiUpdateDeductionTmp.tcEntityCode = ? 
               or tApiUpdateDeductionTmp.tcEntityCode = '':U */
            else do: /*CrossCompany Processing*/
                /*cross company posting */
                /* ============================================================================================ */
                /* Get a GL that will be used for the posting in the current company with following properties; */
                /* Get the A/R G/L account that is specified in the domain of the current company               */
                /* - GL.GLType should be CrossCompany                                                           */
                /* - GL.GLSystemType should be empty                                                            */
                /* - GL.GLIsIntercompany should be true                                                         */
                /* ============================================================================================ */               
               <Q-37 run DomainForInterCompany (all) (Read) (Cache)
                  (input viDomainID, (DomainID)
                   input ?, (DomainCode)
                   input ?, (CompanyID)
                   output dataset tqDomainForInterCompany) in BDomain>
               find first tqDomainForInterCompany no-lock no-error.
               
               if not available tqDomainForInterCompany
               then do :
                   assign vcMessage = trim(substitute(#T-83'A cross-company error occurred. No cross-company account information was defined for the current domain (&1 / &2')':255(59432)T-83#,vcDomainCode,viDomainID)).
                   <M-50 run SetMessage
                      (input  vcMessage (icMessage), 
                       input  '':U (icArguments), 
                       input  '':U (icFieldName), 
                       input  '':U (icFieldValue), 
                       input  'E':U (icType), 
                       input  3 (iiSeverity), 
                       input  '':U (icRowid), 
                       input  'qadfin-276595':U (icFcMsgNumber), 
                       input  '' (icFcExplanation), 
                       input  '' (icFcIdentification), 
                       input  '' (icFcContext), 
                       output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                   <I-683 {bFcCloseAndStopInstance
                        &CLASS           = "BJournalEntry"}>
                        /*
                   <I-34 {bFcCloseAndStopInstance
                        &CLASS           = "BNumber"}> */
                   return.
               end.  /* end if not available tqDomainForInterCompany. */
               assign vcMessage = "":U.
               if tqDomainForInterCompany.tcIntercoARGLCode = "":U and 
                  tqDomainForInterCompany.tcIntercoARGLCode = ?
               then assign vcMessage = vcMessage + chr(10) + trim(substitute(#T-13'AR cross-company accounts are not defined for the domain (&1).':255(59436)T-13#,vcDomainCode)).
               if tqDomainForInterCompany.tlIntercoARGLIsIntercoAccount <> true
               then assign vcMessage = vcMessage + chr(10) + trim(#T-79'The GL account must be an intercompany account.':255(997)T-79#).
               if tqDomainForInterCompany.tlIntercoARGLIsCostCentreAccount and 
                  (tqDomainForInterCompany.tiIntercoARCostCentreProfile_ID = 0 or
                   tqDomainForInterCompany.tiIntercoARCostCentreProfile_ID = ?)
               then assign vcMessage = vcMessage + chr(10) + trim(#T-25'No cost center was specified, and the GL account is defined with cost center analysis. A default cost center is mandatory for automatic postings.':255(998)T-25#).
               if tqDomainForInterCompany.tlIntercoARGLIsProjectAccount and 
                  (tqDomainForInterCompany.tiIntercoARProjectProfile_ID = 0 or
                   tqDomainForInterCompany.tiIntercoARProjectProfile_ID = ?)
               then assign vcMessage = vcMessage + chr(10) + trim(#T-74'No project was specified, and the GL account is defined with project analysis. A default project is mandatory for automatic postings.':255(999)T-74#).
               if tqDomainForInterCompany.tlIntercoARGLIsDivisionAccount and 
                  (tqDomainForInterCompany.tiIntercoARDivisionProfile_ID  = 0 or 
                   tqDomainForInterCompany.tiIntercoARDivisionProfile_ID = ?)
               then assign vcMessage = vcMessage + chr(10) + trim(#T-240'No sub-account was specified, and the GL account is defined with sub-account analysis. A default sub-account is mandatory for automatic postings.':255(1000)T-240#).
               
               if vcMessage <> "":U and length(vcMessage,"CHARACTER":U) > 1
               then do : 
                   assign vcMessage = trim(substitute(#T-8'A cross-company error occurred. The specified GL account '&1' of domain '&2' is not defined in the system or is invalid.':255(59440)T-8#,tqDomainForInterCompany.tcIntercoARGLCode,vcDomainCode)).
                   <M-811 run SetMessage
                      (input  vcMessage (icMessage), 
                       input  '':U (icArguments), 
                       input  '':U (icFieldName), 
                       input  '':U (icFieldValue), 
                       input  'E':U (icType), 
                       input  3 (iiSeverity), 
                       input  '':U (icRowid), 
                       input  'qadfin-412037':U (icFcMsgNumber), 
                       input  '' (icFcExplanation), 
                       input  '' (icFcIdentification), 
                       input  '' (icFcContext), 
                       output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                   <I-770 {bFcCloseAndStopInstance
                        &CLASS           = "BJournalEntry"}>
                        /*
                   <I-89 {bFcCloseAndStopInstance
                        &CLASS           = "BNumber"}> */
                   return.      
               end. /* if vcMessage <> "":U and LENGTH(vcMessage,"CHARACTER":U) > 1 */
               /* =========================== */
               /* Get the division of the G/L */
               /* =========================== */
               if tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> 0 and 
                  tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> ?
               then do :
                   <Q-815 run GetDivisionFromProfile (all) (Read) (NoCache)
                      (input viCompanyId, (CompanyId)
                       input tqDomainForInterCompany.tiIntercoARDivisionProfile_ID, (DivisionProfileId)
                       output dataset tqDivisionFromProfile) in BProfile>
                   
                   find first tqDivisionFromProfile where
                       tqDivisionFromProfile.tiProfile_ID =  tqDomainForInterCompany.tiIntercoARDivisionProfile_ID no-error.  
                       
                   if not available tqDivisionFromProfile
                   then do:
                       assign vcMessage = trim(substitute(#T-82'A cross-company error occurred. The system is unable to find the sub-account of the GL account (&1) based on its profile.':255(1101)T-82#,tqDomainForInterCompany.tcIntercoARGLCode)).
                                          
                       <M-97 run SetMessage
                          (input  vcMessage (icMessage), 
                           input  '':U (icArguments), 
                           input  '':U (icFieldName), 
                           input  '':U (icFieldValue), 
                           input  'E':U (icType), 
                           input  3 (iiSeverity), 
                           input  '':U (icRowid), 
                           input  'qadfin-660508':U (icFcMsgNumber), 
                           input  '' (icFcExplanation), 
                           input  '' (icFcIdentification), 
                           input  '' (icFcContext), 
                           output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                       <I-96 {bFcCloseAndStopInstance
                             &CLASS           = "BJournalEntry"}>
                             /*
                       <I-15 {bFcCloseAndStopInstance
                             &CLASS           = "BNumber"}> */
                       return.
                   end. /* if not available tqDivisionFromProfile */
               end. /* if tqDomainForInterCompany.tiIntercoARDivisionProfile_ID <> 0 */
               /* tApiUpdateDeduction.tcEntityCode get company id .*/
               <Q-59 run CompanyPrim (all) (Read) (NoCache)
                  (input ?, (LookupCompanyId)
                   input tApiUpdateDeductionTmp.tcEntityCode, (CompanyCode)
                   output dataset tqCompanyPrim) in BCompany>
                find first tqCompanyPrim no-error.
                if not available tqCompanyPrim then 
                do:
                    assign oiReturnStatus = -1
                           vcMessage = trim(substitute(#T-1'Incorrect company code &1.':255(652374720)T-1#,tApiUpdateDeductionTmp.tcEntityCode)).
                    <M-456 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  ? (icRowid), 
                        input  'qadfin-911298':U (icFcMsgNumber), 
                        input  ? (icFcExplanation), 
                        input  ? (icFcIdentification), 
                        input  ? (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                     <I-28 {bFcCloseAndStopInstance
                          &CLASS           = "BJournalEntry"}>
                          /*
                     <I-95 {bFcCloseAndStopInstance
                          &CLASS           = "BNumber"}> */
                    return.
                 end. /* end if not available tqCompanyPrim */
               <Q-4 run CompanyPropertyByBusRelSimple (all) (Read) (Cache)
                  (input tqCompanyPrim.tiCompany_ID, (CompanyId)
                   input ?, (CompanyCode)
                   input ?, (BusinessRelationCode)
                   output dataset tqCompanyPropertyByBusRelSimple) in BCompanyProperty>
               find first tqCompanyPropertyByBusRelSimple where 
                      tqCompanyPropertyByBusRelSimple.tiCompany_ID = tqCompanyPrim.tiCompany_ID
                      no-lock no-error.
               if not available tqCompanyPropertyByBusRelSimple
               then do :
                   assign oiReturnStatus = -1
                          vcMessage = trim(substitute(#T-6'Intercompany Business Relation Code missing for the company &1.':100(495217481)T-6#,tApiUpdateDeductionTmp.tcEntityCode)).
                   <M-46 run SetMessage
                      (input  vcMessage (icMessage), 
                       input  '':U (icArguments), 
                       input  '':U (icFieldName), 
                       input  '':U (icFieldValue), 
                       input  'E':U (icType), 
                       input  3 (iiSeverity), 
                       input  ? (icRowid), 
                       input  'qadfin-810456':U (icFcMsgNumber), 
                       input  ? (icFcExplanation), 
                       input  ? (icFcIdentification), 
                       input  ? (icFcContext), 
                       output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                   <I-55 {bFcCloseAndStopInstance
                        &CLASS           = "BJournalEntry"}>
                        /*
                   <I-38 {bFcCloseAndStopInstance
                        &CLASS           = "BNumber"}>    */
                   return.
               end. /* end if not available tqCompanyPropertyByBusRelSimple. */
               
               empty temp-table tDefaultSystemSafDI.
               <M-58 run AddStandardPosting
                  (input  vcPostingHeaderRowId (icPostingtcRowid), 
                   input  tqDomainForInterCompany.tcIntercoARGLCode (icGLCode), 
                   input  (if available tqDivisionFromProfile then tqDivisionFromProfile.tcDivisionCode else '':U) (icDivisionCode), 
                   input  '':U (icCostCentreCode), 
                   input  '':U (icCostCentreText), 
                   input  '':U (icProjectCode), 
                   input  '':U (icProjectText), 
                   input  tqCompanyPropertyByBusRelSimple.tcBusinessRelationICCode (icIntercoBusinessRelationCode), 
                   input  tDInvoice.tcCurrencyCode (icCurrencyCode), 
                   input  tApiUpdateDeductionTmp.tdDInvoiceDeductionTC (idDebitTC), 
                   input  tApiUpdateDeductionTmp.tdDInvoiceDeductionLC (idDebitLC), 
                   input  tApiUpdateDeductionTmp.tdDInvoiceDeductionCC (idDebitCC), 
                   input  ? (idDebitPC), 
                   input  0 (idCreditTC), 
                   input  0 (idCreditLC), 
                   input  0 (idCreditCC), 
                   input  ? (idCreditPC), 
                   input  ? (idQty), 
                   input  #T-20'Cross-Company Posting':30(57722)T-20# (icLineText), 
                   input  '':U (icSafText), 
                   input  tDefaultSystemSafDI (tDefaultSafs), 
                   input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                   input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
                   input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
                   input  tDInvoice.DInvoiceCCRate (idPostingLineCCRate), 
                   input  tDInvoice.DInvoiceCCScale (idPostingLineCCScale), 
                   output viPostingLineId (oiPostingLineId), 
                   input  0 (iiSafStructureId), 
                   input  '':U (icSafStructureCode), 
                   input  '':U (icAllocationKey), 
                   input  true (ilLinkedCrCyDaemonReqExists), 
                   output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
               if viFcReturnSuper < 0 then
               do:
                   assign oiReturnStatus = viFcReturnSuper.
                   <I-81 {bFcCloseAndStopInstance
                        &CLASS           = "BJournalEntry"}>
                        /*
                   <I-68 {bFcCloseAndStopInstance
                       &CLASS           = "BNumber"}> */
                   return.
               end.
       
               /* create cross companry record which will be processed by the BDInvoiceMultiCy */
               find first tDeductionDetailInfo where tDeductionDetailInfo.tcDeductionCatCode
                                               = tApiUpdateDeductionTmp.tcDeductionCatCode
                                               and tDeductionDetailInfo.tiDomain_ID = tApiUpdateDeductionTmp.tiExpensedDomainID no-lock no-error.
               if not available tDeductionDetailInfo then 
               do:
                   <I-961 {bFcCloseAndStopInstance
                        &CLASS           = "BJournalEntry"}>
                        /*
                   <I-17 {bFcCloseAndStopInstance
                        &CLASS           = "BNumber"}>   */                       
                   return.
               end.
               if tApiUpdateDeductionTmp.tcGLCode = '':U or tApiUpdateDeductionTmp.tcGLCode = ? then
               do:
                   assign vcExpensedGLCode = tDeductionDetailInfo.tcGLCode
                          vcExpensedCostCentreCode = tDeductionDetailInfo.tcCostCenter
                          vcExpensedSubAccountCode = tDeductionDetailInfo.tcSubAccount
                          vcSafCode1 = tDeductionDetailInfo.tcSafCode1
                          vcSafCode2 = tDeductionDetailInfo.tcSafCode2
                          vcSafCode3 = tDeductionDetailInfo.tcSafCode3
                          vcSafCode4 = tDeductionDetailInfo.tcSafCode4
                          vcSafCode5 = tDeductionDetailInfo.tcSafCode5
                          vcSafConcept1 =  tDeductionDetailInfo.tcSafConceptCode1
                          vcSafConcept2 =  tDeductionDetailInfo.tcSafConceptCode2
                          vcSafConcept3 =  tDeductionDetailInfo.tcSafConceptCode3
                          vcSafConcept4 =  tDeductionDetailInfo.tcSafConceptCode4
                          vcSafConcept5 =  tDeductionDetailInfo.tcSafConceptCode5.  
               end.                  
               else
               do:
                   assign vcExpensedGLCode = tApiUpdateDeductionTmp.tcGLCode
                          vcExpensedCostCentreCode = tApiUpdateDeductionTmp.tcCostCenter
                          vcExpensedSubAccountCode = tApiUpdateDeductionTmp.tcSubAccount
                          vcSafCode1 = tApiUpdateDeductionTmp.tcSafCode1
                          vcSafCode2 = tApiUpdateDeductionTmp.tcSafCode2
                          vcSafCode3 = tApiUpdateDeductionTmp.tcSafCode3
                          vcSafCode4 = tApiUpdateDeductionTmp.tcSafCode4
                          vcSafCode5 = tApiUpdateDeductionTmp.tcSafCode5
                          vcSafConcept1 =  tApiUpdateDeductionTmp.tcSafConceptCode1
                          vcSafConcept2 =  tApiUpdateDeductionTmp.tcSafConceptCode2
                          vcSafConcept3 =  tApiUpdateDeductionTmp.tcSafConceptCode3
                          vcSafConcept4 =  tApiUpdateDeductionTmp.tcSafConceptCode4
                          vcSafConcept5 =  tApiUpdateDeductionTmp.tcSafConceptCode5.
               end.
               create tNewCrossCyPostingDeduction.
               assign tNewCrossCyPostingDeduction.tiCompanyID             = tqCompanyPrim.tiCompany_ID
                      tNewCrossCyPostingDeduction.tiCurrencyID            = tDInvoice.DInvoiceCurrency_ID
                      tNewCrossCyPostingDeduction.tiBJournalEntrySourceID  = viBJournalEntryDIID
                      
                      tNewCrossCyPostingDeduction.ttDInvoicePostingDate    = tDInvoice.DInvoicePostingDate
                      tNewCrossCyPostingDeduction.tcAPMatchingCostCenterCode = vcExpensedCostCentreCode 
                      tNewCrossCyPostingDeduction.tcAPMatchingDivisionCode = vcExpensedSubAccountCode
                      tNewCrossCyPostingDeduction.tcAPMatchingProjectCode  = tApiUpdateDeductionTmp.tcProject
                      tNewCrossCyPostingDeduction.tiCrossCompanyGLID        = tqDomainForInterCompany.tiIntercoARGL_ID
                      tNewCrossCyPostingDeduction.tiOriginatorCompanyID     = viCompanyId
                      tNewCrossCyPostingDeduction.tcOriginatorCompnayCode  = vcCompanyCode /* to-do: change this name to originalCompanycode*/
                      tNewCrossCyPostingDeduction.tcIntercoBusinessRelationCode = '':U /* tqCompanyPropertyByBusRelSimple.tcBusinessRelationICCode */
                      tNewCrossCyPostingDeduction.tiOriginatorPostingID     = viPostingId
                      tNewCrossCyPostingDeduction.tiOriginatorPostingLineID = viPostingLineId
                      tNewCrossCyPostingDeduction.tdCrossCyPostingAmountTC   = tApiUpdateDeductionTmp.tdDInvoiceDeductionTC
                      tNewCrossCyPostingDeduction.tcRowId                  = string(tNewCrossCyPostingDeduction.tiOriginatorPostingLineID) + "-":U + string(tNewCrossCyPostingDeduction.tdCrossCyPostingAmountTC) + "-":U + STRING(TIME,"HH:MM:SS":U)
                      
                      tNewCrossCyPostingDeduction.tcDInvoiceDescription    = tDInvoice.DInvoiceDescription
                      tNewCrossCyPostingDeduction.tiDeductionPostingPeriod = 0
                      tNewCrossCyPostingDeduction.tiDeductionPostingYear   = 0
                      tNewCrossCyPostingDeduction.tcJournalCode            = tDeductionDetailInfo.tcExpenseDaybookCode
                      tNewCrossCyPostingDeduction.tdExchangeRate           = 0
                      tNewCrossCyPostingDeduction.tdExchangeRateCC         = 0
                      tNewCrossCyPostingDeduction.tdExchangeRateScale      = 0
                      tNewCrossCyPostingDeduction.tdExchangeRateCCScale    = 0
                      tNewCrossCyPostingDeduction.tcCurrencyTC             = tDInvoice.tcCurrencyCode
                      tNewCrossCyPostingDeduction.tcGLCode                 = vcExpensedGLCode
                      tNewCrossCyPostingDeduction.tcCrossCompanyGLCode     = tqDomainForInterCompany.tcIntercoARGLCode
                      tNewCrossCyPostingDeduction.tcSafCode1               = vcSafCode1
                      tNewCrossCyPostingDeduction.tcSafCode2               = vcSafCode2
                      tNewCrossCyPostingDeduction.tcSafCode3               = vcSafCode3
                      tNewCrossCyPostingDeduction.tcSafCode4               = vcSafCode4
                      tNewCrossCyPostingDeduction.tcSafCode5               = vcSafCode5
                      tNewCrossCyPostingDeduction.tcSafConceptCode1        = vcSafConcept1
                      tNewCrossCyPostingDeduction.tcSafConceptCode2        = vcSafConcept2
                      tNewCrossCyPostingDeduction.tcSafConceptCode3        = vcSafConcept3
                      tNewCrossCyPostingDeduction.tcSafConceptCode4        = vcSafConcept4
                      tNewCrossCyPostingDeduction.tcSafConceptCode5        = vcSafConcept5
                      tNewCrossCyPostingDeduction.tcCrossCompanyDivisionCode = (if available tqDivisionFromProfile then tqDivisionFromProfile.tcDivisionCode else '':U).
                      
            end. /* end else if tApiUpdateDeductionTmp.tcEntityCode = vcCompanyCode or tApiUpdateDeductionTmp.tcEntityCode = ? 
               or tApiUpdateDeductionTmp.tcEntityCode = '':U */
            
        end. /* end of tApiUpdateDeductionTmp */
    end. /* end of tDInvoice */
    /* can not stop journal entry instance */
    <I-30 {bFcCloseInstance
         &CLASS           = "BJournalEntry"}>
         /*
    <I-22 {bFcCloseAndStopInstance
         &CLASS           = "BNumber"}> 
         */

end. 


/* Create DInvoice Movements and Posting */
if can-find(first tDIMovement) then 
do:
    <M-87 run CreateDInvoiceMovements
       (input-output tDIMovement (tDIMovement), 
        input  tDInvoiceStageUpdates (tDInvoiceStageUpdates), 
        input-output viBJournalEntryDIID (biBJournalEntryId), 
        input  false (ilClearData), 
        input  ? (itPaymentTaxPointDate), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
 
     if viFcReturnSuper < 0 
     then do:
         assign oiReturnStatus = viFcReturnSuper.
         /*close instance */
         <I-40 {bFcCloseInstance
              &CLASS           = "BJournalEntry"}>
         return.
     end.              
end. /* end if can-find(first tDIMovement) */

  
/* ========================================================================== */
/* Create Q-CrossCompany postings that will be picked up later-on by a daemon */
/* ========================================================================== */
/*
if can-find (first tNewCrossCyPostingDeduction)
then do :
    if viBQCrossCyPostingDIID = 0 or viBQCrossCyPostingDIID = ?
    then do:
        <I-31 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "true"
             &CLASS                = "BQCrossCyPosting"}>
    end. /* if viBQCrossCyPostingID = 0 */
    else do:
        <I-88 {bFcOpenInstance
             &CLASS           = "BQCrossCyPosting"}>
    end. /* if viBQCrossCyPostingID <> 0 */
    <M-16 run ApiCreateQCrossCyPosting
       (input  tNewQCrossCyPostingDeduction (tNewQCrossCyPosting), 
        output viExternalReturn (oiReturnStatus)) in BQCrossCyPosting>   
    <I-20 {bFcCloseInstance
         &CLASS           = "BQCrossCyPosting"}>  
    if viExternalReturn < 0 
    then do:
        assign oiReturnStatus = viExternalReturn.
        return.  
    end.
    
end. /* if can-find (first tNewQCrossCyPostingDI) */
*/
assign oiReturnStatus = 0.