project QadFinancials > class BCInvoice > method ReverseCInvoicePosting

Description

Reverse posting of supplier invoice


Parameters


icOrigCInvoiceRowIdinputcharacter
icNewCInvoiceRowIdinputcharacter
iiReverseInvYearinputinteger
iiReverseInvPeriodinputinteger
icReverseInvJournalinputcharacter
iiReverseInvVoucherinputinteger
itReverseInvPostingDateinputdate
icReverseInvDescriptioninputcharacter
ilReverseBySigninputlogical
icNewControlGLCodeinputcharacter
icDivisionCodeinputcharacter
icCostCentreCodeinputcharacter
icProjectCodeinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ReverseCInvoice


program code (program3/bcinvoice.p)

/* ========================================================================================================== */
/* Reverse posting created for original invoice beeing reversed and attach this new posting to new invoice    */
/* ========================================================================================================== */
/* Parameters:   (I)  OrigCInvoiceRowId     RowId of original invoice which was reversed                      */
/*               (I)  NewCInvoiceRowId      RowId of new invoice which reverse original one                   */
/*               (I)  ReverseInvYear        Reverse invoice year                                              */
/*               (I)  ReverseInvPeriod      Reverse invoice period                                            */
/*               (I)  ReverseInvJorunal     Reverse invoice journal                                           */
/*               (I)  ReverseInvVoucher     Reverse invoice voucher                                           */
/*               (I)  ReverseInvPostingDate Reverse invoice posting date                                      */
/*               (I)  ReverseInvDescription Reverse invoice description                                       */
/* ========================================================================================================== */
         
assign oiReturnStatus = -98.
                            
/* Get new invoice */
find tCInvoice where
     tCInvoice.tc_Rowid = icNewCInvoiceRowId no-error.
if not available tCInvoice
then do:
    assign vcMessage = #T-12'The system cannot find the date for the new (reverse) invoice.':255(69128)T-12#.
           vcContext = "NewCInvoiceRowId=":U + icNewCInvoiceRowId.
    <M-11 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'S':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QadFin-7902':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  vcContext (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    assign oiReturnStatus = -1.
    return.
end.

/* Create list of postings created for original (reversed) invoice */
assign vcPostingIdList = '':U.
for each bOrigCInvoicePosting where
         bOrigCInvoicePosting.tc_ParentRowid = icOrigCInvoiceRowId:
    
   assign vcPostingIdList = if vcPostingIdList = "":U 
                            then string(bOrigCInvoicePosting.Posting_ID)
                            else vcPostingIdList + chr(4) + string(bOrigCInvoicePosting.Posting_ID).
end. /* for each tPendingVoucherUpdate */
    
/* Start comminication with journal entry component */
if viBJournalEntryCIID <> 0 and
   viBJournalEntryCIID <> ?
then do:
    <I-35 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>         
end.
else do:
    <I-25 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION = "true"
         &CLASS              = "BJournalEntry"}>
    assign vlBJEIsStartedFromCI = true.
end.

assign viLocalReturn = 0.

POSTING_BLOCK:
do:
    /* Load created posting for original invoice to instance of BJournal component */    
    if vcPostingIdList <> '':U
    then do:
        /* keep previous can be set on false because we only work with one invoice */
        <M-6 run DataLoad
           (input  ? (icRowids), 
            input  vcPostingIdList (icPkeys), 
            input  ? (icObjectIds), 
            input  ? (icFreeform), 
            input  false (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper <  0
        then do:
            assign vcMessage = #T-7'The system cannot reverse the original posting created for the reversed supplier invoice.':255(69126)T-7#
                   vcContext = "vcPostingIdList=":U + vcPostingIdList.
            <M-8 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'QadFin-7900':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            leave POSTING_BLOCK.
        end.
    end. /* if vcPostingIdList <> '':U */
    
    /* Performe reverse of original posting */
    for each bOrigCInvoicePosting where
             bOrigCInvoicePosting.tc_ParentRowID = icOrigCInvoiceRowId:
    
        <M-4 run GetPostingRowidBasedOnPostingID
           (input  bOrigCInvoicePosting.Posting_ID (iiPostingID), 
            output vcPostingRowId (ocPostingRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper <  0 then leave POSTING_BLOCK.

        if bOrigCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL}
        then do:
            <M-27 run CreateReverseEntry
               (input  vcPostingRowId (icPostingTcRowid), 
                input  iiReverseInvYear (iiNewPeriodYear), 
                input  iiReverseInvPeriod (iiNewPeriodPeriod), 
                input  icReverseInvJournal (icNewJournalCode), 
                input  iiReverseInvVoucher (iiNewVoucher), 
                input  itReverseInvPostingDate (itNewPostingDate), 
                input  icReverseInvDescription (icNewPostingText), 
                output viNewPostingId (oiNewPostingID), 
                input  ilReverseBySign (ilReverseBySign), 
                input  ? (iiOriginVoucher), 
                input  false (ilIsValidateJournalType), 
                input  icReverseInvJournal (icReportingJournalCode), 
                input  viBQCrossCyPostingID (iiBQCrossCyPostingID), 
                input  false (ilCallFromDaemon), 
                output viNewPostingLineID (oiNewPostingLineID), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.    
    
            /* Create CInvoiceMovement for new (reverse) invoice */
            find bOrigCInvoiceMovement where
                 bOrigCInvoiceMovement.tc_ParentRowid = icOrigCInvoiceRowId and
                 bOrigCInvoiceMovement.CInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} no-error.
            if not available bOrigCInvoiceMovement
            then do:
                assign vcMessage = #T-10'The system cannot reverse the original posting created for the reversed supplier invoice. The system cannot find original INIT movement.':255(69127)T-10#.
                       vcContext = "CInvoice_ID=":U + string(bOrigCInvoicePosting.CInvoice_ID).
                <M-9 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-7901':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  vcContext (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                assign viLocalReturn = -1.
                leave POSTING_BLOCK.
            end.
    
            <M-13 run AddDetailLine
               (input  'CInvoiceMovement':U (icTable), 
                input  tCInvoice.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.
    
            buffer-copy bOrigCInvoiceMovement
                except CInvoice_ID
                       CInvoiceMovement_ID
                       tc_ParentRowid
                       tc_Rowid
                       tc_Status
                to tCInvoiceMovement.
    
            <M-28 run GetPostingLineByGL
               (input  ? (icPostingRowId), 
                input  viNewPostingId (iiPostingID), 
                input  tCInvoice.tcControlGLCode (icGLCode), 
                output vcDummy (ocPostingLineRowId), 
                output tCInvoiceMovement.PostingLine_ID (oiPostingLineID), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.

            <M-16 run GetPostingInfoByPostingLineID
               (input  tCInvoiceMovement.PostingLine_ID (iiPostingLineID), 
                output tCInvoiceMovement.CInvoiceMovementPostDate (otPostingDate), 
                output tCInvoiceMovement.Period_ID (oiPeriodID), 
                output tCInvoiceMovement.CInvoiceMovementYearPeriod (oiPostingYearPeriod), 
                output vcDummy (ocJournalCode), 
                output vcDummy (ocJournalTypeCode), 
                output vcDummy (ocLayerTypeCode), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.

            /* Change control G/L account */
            if icNewControlGLCode <> ? and
               icNewControlGLCode <> "":U
            then do:
                <Q-42 run BusinessRelationByIDCodeIC (all) (Read) (NoCache)
                   (input ?, (BusinessRelationID)
                    input tCInvoice.tcBusinessRelationCode, (BusinessRelationCode)
                    input ?, (BusinessRelationIntercoCode)
                    input true, (BusinessRelationIsActive)
                    output dataset tqBusinessRelationByIDCodeIC) in BBusinessRelation>
    
                find first tqBusinessRelationByIDCodeIC where 
                           tqBusinessRelationByIDCodeIC.tcBusinessRelationCode = tCInvoice.tcBusinessRelationCode
                no-lock no-error.
                if available tqBusinessRelationByIDCodeIC then
                assign vcBusinessRelationICCode = tqBusinessRelationByIDCodeIC.tcBusinessRelationICCode.

                <M-63 run UpdatePostingLine
                   (input  ? (icPostingLineRowId), 
                    input  tCInvoiceMovement.PostingLine_ID (iiPostingLineId), 
                    input  icNewControlGLCode (icNewGLCode), 
                    input  ? (iiNewGLId), 
                    input  true (ilIsUpdateGLAccount), 
                    input  icDivisionCode (icNewDivisionCode), 
                    input  true (ilIsUpdateDivision), 
                    input  icCostCentreCode (icNewCostCentreCode), 
                    input  true (ilIsUpdateCostCentre), 
                    input  icProjectCode (icNewProjectCode), 
                    input  true (ilIsUpdateProject), 
                    input  ? (idDebitTC), 
                    input  ? (idDebitCC), 
                    input  ? (idDebitLC), 
                    input  ? (idCreditTC), 
                    input  ? (idCreditCC), 
                    input  ? (idCreditLC), 
                    input  false (ilIsUpdateAmount), 
                    input  vcBusinessRelationICCode (icBusinessRelationIntercoCode), 
                    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
                if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
                if viFcReturnSuper <  0 then leave POSTING_BLOCK.

                assign tCInvoice.tcControlGLCode = icNewControlGLCode
                       tCInvoice.ControlGL_ID    = ?.
            end.
        end. /* if bOrigCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL} */
        else do:
            <M-17 run GetPostingHeaderByPostingId
               (input  bOrigCInvoicePosting.Posting_ID (iiPostingId), 
                input  ? (icPostingRowID), 
                output viDummy (oiPostingYear), 
                output viDummy (oiPostingPeriod), 
                output vtDummy (otPostingDate), 
                output vcOrigJournalCode (ocPostingJournalCode), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.
    
            <M-18 run CreateReverseEntry
               (input  vcPostingRowId (icPostingTcRowid), 
                input  iiReverseInvYear (iiNewPeriodYear), 
                input  iiReverseInvPeriod (iiNewPeriodPeriod), 
                input  vcOrigJournalCode (icNewJournalCode), 
                input  0 (iiNewVoucher), 
                input  itReverseInvPostingDate (itNewPostingDate), 
                input  icReverseInvDescription (icNewPostingText), 
                output viNewPostingId (oiNewPostingID), 
                input  ilReverseBySign (ilReverseBySign), 
                input  ? (iiOriginVoucher), 
                input  false (ilIsValidateJournalType), 
                input  vcOrigJournalCode (icReportingJournalCode), 
                input  viBQCrossCyPostingID (iiBQCrossCyPostingID), 
                input  false (ilCallFromDaemon), 
                output viNewPostingLineID (oiNewPostingLineID), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave POSTING_BLOCK.
        end. /* else bOrigCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL} */
    
        /* Create CInvoicePosting record for new (reversed) posting */
        <M-19 run AddDetailLine
           (input  'CInvoicePosting':U (icTable), 
            input  tCInvoice.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper <  0 then leave POSTING_BLOCK.
    
        buffer-copy bOrigCInvoicePosting
            except CInvoice_ID
                   CInvoicePosting_ID
                   tc_ParentRowid
                   tc_Rowid
                   tc_Status
            to tCInvoicePosting.
    
        assign tCInvoicePosting.Posting_ID = viNewPostingId.

        <M-33 run GetCIRECPostingLineID
           (input  tCInvoicePosting.Posting_ID (iiPostingId), 
            output tCInvoicePosting.CIRECPostingLine_ID (oiCIRECPostingLineId), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper <  0 then leave POSTING_BLOCK.
        
        for each tcinvoicevat where tCInvoiceVat.CInvoice_ID = tCInvoicePosting.CInvoice_ID and
                                    tCInvoiceVat.tc_Status   <> "D":U :
            assign  vipostingvatid = 0.
            
            <M-67 run UpdateCinvoiceVatPostingVatID
               (input  tCInvoicePosting.Posting_ID (iiReversePostingid), 
                input  tCInvoiceVat.TxclTaxCls (icTaxClass), 
                input  tCInvoiceVat.TxtyTaxType (icTaxType), 
                input  tCInvoiceVat.TxuTaxUsage (ictaxUsage), 
                input  tCInvoiceVat.TxenvTaxEnv (icTaxEnv), 
                output vipostingvatid (oiPostingVatid), 
                input  tCInvoiceVat.Vat_ID (iiVatid), 
                input  tCInvoiceVat.tcVatCode (icvatcode), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>                            
            
            if viFcReturnSuper <  0 then viLocalReturn = viFcReturnSuper.
            if vipostingvatid <> 0 and vipostingvatid <> ?
            then
                tCInvoiceVat.PostingVat_ID = vipostingvatid.
                
        end.                            
    end. /* for each bOrigCInvoicePosting where */
end. /* POSTING_BLOCK */

/* Close communication with journal entry component */
<I-23 {bFcCloseInstance
     &CLASS           = "BJournalEntry"}>
     
/* Return */
if viLocalReturn <> 0 then assign oiReturnStatus = viLocalReturn.
if oiReturnStatus = -98 then assign oiReturnStatus = 0.