project QadFinancials > class BCInvoiceAPMatching > method CreateCInvoiceAPMatchingAllInOneTran

Description

Create Supplier Invoice and APMatchng based on incomming data stored in imput temporary tables.


Parameters


tCInvoiceRefinput-outputtemp-table
tCInvoicePORefinputtemp-table
tAPMatchingLnRefinput-outputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceAPMatching.CreateCInvoiceAPMatching


program code (program6/bcinvoiceapmatching.p)

/* =================================================================================================== */
/* Method      : CreateCInvoiceAPMatching                                                              */
/* Desc        : This method create Supplier Invoice and Receiver Matching (optional) based on data    */
/*               stored in incomming termporary table                                                  */
/* --------------------------------------------------------------------------------------------------- */
/* Params:  (I)  tCInvoiceRef         Supplier Invoice definition                                      */
/*          (I)  tCInvoicePORef       List of purchase orders linked to supplier invoice               */
/*          (I)  tAPMatchingLnRef     Matching line details                                            */
/* =================================================================================================== */

assign oiReturnStatus = -98
       viLocalReturn  = 0.

MAIN_BLOCK:
do on error undo, return:

    /* =================================================================================================== */
    /* Go through all inovices to be created                                                               */
    /* =================================================================================================== */
    CINVOICE_BLOCK:
    do on error undo, leave:
        /* =================================================================================================== */
        /* Only do the INVOICE BLOCK when the invoices were not passed.                                        */
        /* =================================================================================================== */
        if not can-find(first tCInvoiceRef where
           (tCInvoiceRef.tiCInvoiceRegistrationNr = ? or tCInvoiceRef.tiCInvoiceRegistrationNr = 0 ) and
           (tCInvoiceRef.tcJournalCode            = ? or tCInvoiceRef.tcJournalCode            = '') and
           (tCInvoiceRef.tiCInvoicePostingYear    = ? or tCInvoiceRef.tiCInvoicePostingYear    = 0 ) and
           (tCInvoiceRef.tiCInvoiceVoucher        = ? or tCInvoiceRef.tiCInvoiceVoucher        = 0 )) 
        then leave.        
        
        /* =================================================================================================== */
        /* Create supplier invoice (In separate transaction)                                                   */
        /* =================================================================================================== */
        <I-74 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "false"
             &CLASS                = "Transaction"}>    
             
        <M-96 run StartTransaction  (output viFcReturnSuper (oiReturnStatus)) in Transaction>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave CINVOICE_BLOCK.
        
        /* =================================================================================================== */
        /* Create supplier invoices                                                                            */
        /* =================================================================================================== */
        for each tCInvoiceRef where
           (tCInvoiceRef.tiCInvoiceRegistrationNr = ? or tCInvoiceRef.tiCInvoiceRegistrationNr = 0 ) and
           (tCInvoiceRef.tcJournalCode            = ? or tCInvoiceRef.tcJournalCode            = '') and
           (tCInvoiceRef.tiCInvoicePostingYear    = ? or tCInvoiceRef.tiCInvoicePostingYear    = 0 ) and
           (tCInvoiceRef.tiCInvoiceVoucher        = ? or tCInvoiceRef.tiCInvoiceVoucher        = 0 ):
            
            /* =============================================================================================== */
            /* Create Supplier Invoice Data                                                                    */
            /* =============================================================================================== */
            <M-9 run CreateCInvoice  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave.
            
            /* =============================================================================================== */
            /* Create Supplier Invoice                                                                         */
            /* =============================================================================================== */
            <M-49 run CreateCInvoiceRun  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave.
            
        end. /* for each tCInvoiceRef: */    
        
        /* =================================================================================================== */
        /* Validate created data                                                                               */
        /* =================================================================================================== */
        if viLocalReturn >= 0
        then do:
            <M-78 run ValidateBCAndAdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        end.
        
        /* =================================================================================================== */
        /* Commit or releas all data                                                                           */
        /* =================================================================================================== */
        if viLocalReturn >= 0
        then do:
            <M-6 run CommitTransaction
               (input  true (ilStop), 
                input  ? (ihParentInstance), 
                output vcDummy (ocInstances), 
                output viFcReturnSuper (oiReturnStatus)) in Transaction>
            /* Clear the IDs to the other classes in case they were commited successfully and thus the classes are stopped as otherwise the instance-IDs are tried to be re-used in the next flow of the repeating loop */
            if viFcReturnSuper >= 0
            then assign viBCInvoiceFromBCIAPMID   = 0
                        viBAPMatchingFromBCIAPMID = 0.               
            if viFcReturnSuper <  0 or 
               (viFcReturnSuper >  0 and viLocalReturn = 0)
            then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   <  0 then leave CINVOICE_BLOCK.
        end. /* if viLocalReturn >= 0 */
        
        if viLocalReturn < 0
        then do:
            /* Remember all error messages becauue they are lost during abort transaction */
            empty temp-table tCopyFCMessages.
            temp-table tCopyFCMessages:copy-temp-table(temp-table tFcMessages:handle).
        
            <M-7 run AbortTransaction
               (input  ? (ihParentInstance), 
                output vcDummy (ocInstances), 
                output viFcReturnSuper (oiReturnStatus)) in Transaction>
            if viFcReturnSuper <  0 or viFcReturnSuper >  0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            
            /* If the invoices are not created, clear the INVOICE_ID */
            for each tCInvoiceRef:
                assign tCInvoiceRef.tiCInvoice_ID = ?.
            end.
            
            /* Restore all error messages because they are lost during abort transaction */
            temp-table tFCMessages:copy-temp-table(temp-table tCopyFCMessages:handle).
            empty temp-table tCopyFCMessages.
        end.
        
        finally:
            <I-57 {bFcCloseAndStopInstance
                 &CLASS           = "Transaction"}>
            assign viTransactionID = 0.
        end finally.
    end. /* CINVOICE_BLOCK */
    
    
    /* =================================================================================================== */
    /* If there were errors during processing of Supplier invoice, no reason to continue                   */
    /* =================================================================================================== */
    if viLocalReturn < 0 then leave MAIN_BLOCK.
    
    
    /* =================================================================================================== */
    /* Create Receiver Matching                                                                            */
    /* =================================================================================================== */
    APMATCHING_BLOCK:
    do on error undo, leave:
    
        /* =================================================================================================== */
        /* Create receiver matching (in separate transaction)                                                  */
        /* =================================================================================================== */
        <I-10 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION = "false"
             &CLASS              = "Transaction"}>    
             
        <M-11 run StartTransaction  (output viFcReturnSuper (oiReturnStatus)) in Transaction>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave APMATCHING_BLOCK.
         
        for each tCInvoiceRef:
            /* =================================================================================================== */
            /* Create Receiver matching data                                                                       */
            /* =================================================================================================== */
            <M-13 run CreateAPMatching  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave.
        
            /* =================================================================================================== */
            /* Create Receiver matching                                                                            */
            /* =================================================================================================== */
            <M-28 run CreateAPMatchingRun  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave.                                              
        end.
        
        /* =================================================================================================== */
        /* Validate created data                                                                               */
        /* =================================================================================================== */
        if viLocalReturn >= 0
        then do:
            <M-62 run ValidateBCAndAdditionalUpdates  (output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        end.
             
        if viLocalReturn >= 0
        then do:
            /* Call a submethod that fills the class-level data-item vhBCInvoiceInstanceData that points to the newly created invoice */
            <M-31 run CreateCInvoiceAPMatchingGetCInvoiceData  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or 
               (viFcReturnSuper > 0 and viLocalReturn = 0) 
            then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn >= 0
            then do:
                <M-15 run CommitTransaction
                   (input  true (ilStop), 
                    input  ? (ihParentInstance), 
                    output vcDummy (ocInstances), 
                    output viFcReturnSuper (oiReturnStatus)) in Transaction>
                /* Clear the IDs to the other classes in case they were commited successfully and thus the classes are stopped as otherwise the instance-IDs are tried to be re-used in the next flow of the repeating loop */
                if viFcReturnSuper >= 0
                then assign viBCInvoiceFromBCIAPMID   = 0
                            viBAPMatchingFromBCIAPMID = 0.               
                if viFcReturnSuper <  0 or 
                   (viFcReturnSuper >  0 and viLocalReturn = 0 )
                then assign viLocalReturn = viFcReturnSuper.
            end. /* if viLocalReturn >= 0 */
        end. /* if viLocalReturn >= 0 */
        
        if viLocalReturn < 0
        then do:
            /* Remember all error messages becauue they are lost during abort transaction */
            empty temp-table tCopyFCMessages.
            temp-table tCopyFCMessages:copy-temp-table(temp-table tFcMessages:handle).
            
            <M-16 run AbortTransaction
                (input  ? (ihParentInstance), 
                 output vcDummy (ocInstances), 
                 output viFcReturnSuper (oiReturnStatus)) in Transaction>
            if viFcReturnSuper <  0 or viFcReturnSuper >  0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            
            /* Restore all error messages because they are lost during abort transaction */
            temp-table tFCMessages:copy-temp-table(temp-table tCopyFCMessages:handle).
            empty temp-table tCopyFCMessages.
            
            /* Delete already created initial invoices */
            /* Not needed when the invoice was existed before running this integration */   
            for each tCInvoiceRef where
                     tCInvoiceRef.tiCInvoice_ID <> ? and
                     tCInvoiceRef.tiCInvoice_ID <> 0:
                if (tCInvoiceRef.tiCInvoiceRegistrationNr <> ? and tCInvoiceRef.tiCInvoiceRegistrationNr <> 0 ) or
                   (tCInvoiceRef.tcJournalCode            <> ? and tCInvoiceRef.tcJournalCode            <> '') or
                   (tCInvoiceRef.tiCInvoicePostingYear    <> ? and tCInvoiceRef.tiCInvoicePostingYear    <> 0 ) or
                   (tCInvoiceRef.tiCInvoiceVoucher        <> ? and tCInvoiceRef.tiCInvoiceVoucher        <> 0 )
                then next.
                
                <I-45 {bFcStartAndOpenInstance
                     &ADD-TO-TRANSACTION   = "false"
                     &CLASS                = "BCInvoice"}>    
            
                <M-14 run apiDeleteInstance
                   (input  'Primary=':U + string(tcInvoiceRef.tiCInvoice_ID) (icKey), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            
                <I-66 {bFcCloseAndStopInstance
                     &CLASS           = "BCInvoice"}>
                     
                assign tCInvoiceRef.tiCInvoice_ID = ?.
            end. /* for each tCInvoiceRef where */     
        end.
        
        finally:
            <I-17 {bFcCloseAndStopInstance
                 &CLASS           = "Transaction"}>
            assign viTransactionID = 0.
        end finally.
    end. /* APMATCHING_BLOCK: */
    
    /* =================================================================================================== */
    /* Get external reference to supplier inovice. It has to be done after creation of receiver matching,  */
    /* because from creation of receiver matching supplier invoice can be switched from initial status     */
    /* to final status and at that moment supplier invoice voucher number assigned                         */
    /* =================================================================================================== */
    for each tCInvoiceRef where
             tCInvoiceRef.tiCInvoice_ID <> ? and
             tCInvoiceRef.tiCInvoice_ID <> 0:
        <Q-3 run CInvoiceByID (all) (Read) (NoCache)
            (input ?, (CompanyId)
             input tCInvoiceRef.tiCInvoice_ID, (CInvoiceID)
            output dataset tqCInvoiceByID) in BCInvoice >
        find first tqCInvoiceByID where
                   tqCInvoiceByID.tiCInvoice_ID = tCInvoiceRef.tiCInvoice_ID
                   no-lock no-error.
        if available tqCInvoiceByID
        then do :
            assign tCInvoiceRef.tcCinvoiceReferenceInt = substitute("&1/&2/&3":U,
                                                                    tqCInvoiceByID.tiPeriodYear,
                                                                    tqCInvoiceByID.tcJournalCode,
                                                                    string(tqCInvoiceByID.tiCInvoiceVoucher,"999999999")).
            /* If this method (its callers) is called from MaintainByDataSetWithOutput then it can be that vhBCInvoiceInstanceData points to the  */
            /* invoice-instance-dataset and when the 'ChronologicalInvoiceNumber' is turned on on the domain then the final InvoiceVoucher is     */
            /* only known after the APMatching is saved. As such, we will update the InvoiceVoucher in vhBCInvoiceInstanceData with the final one */
            if vlReturnCInvoiceInstanceData and 
               valid-handle(vhBCInvoiceInstanceData) and
               vhBCInvoiceInstanceData:num-buffers > 0 
            then UPDATEINVOICEVOUCHERBLOCK: DO :
                assign vhTempTableBuffer = vhBCInvoiceInstanceData:get-buffer-handle("tCInvoice":U) no-error.
                if error-status:error or 
                   valid-handle(vhTempTableBuffer) = false 
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                Create query vhQuery in widget-pool "non-persistent":U no-error.
                if error-status:error or 
                   valid-handle(vhQuery) = false 
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                vhQuery:add-buffer(vhTempTableBuffer) no-error.
                if error-status:error or 
                   valid-handle(vhQuery) = false 
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                vhQuery:query-prepare("for each " +  vhTempTableBuffer:name + " where " +  vhTempTableBuffer:name + ".CInvoice_ID = " + string(tCInvoiceRef.tiCInvoice_ID)) no-error.
                if error-status:error or 
                   valid-handle(vhQuery) = false 
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                vhQuery:query-open() no-error.
                if error-status:error or 
                   valid-handle(vhQuery) = false 
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                vhQuery:get-first() no-error.
                if error-status:error or 
                   valid-handle(vhQuery) = false or 
                   vhTempTableBuffer:available = false or 
                   vhTempTableBuffer::CInvoiceVoucher = tqCInvoiceByID.tiCInvoiceVoucher
                then Leave UPDATEINVOICEVOUCHERBLOCK.
                assign vhTempTableBuffer::CInvoiceVoucher = tqCInvoiceByID.tiCInvoiceVoucher no-error. 
            END. /* UPDATEINVOICEVOUCHERBLOCK */
            if valid-handle(vhQuery) 
            then do :
                vhQuery:query-close() no-error.
                delete object vhQuery.
            end. /* if valid-handle(vhQuery) */
         end. /* if available tqCInvoiceByID */
         else do:
             assign vcMessage                  = #T-52'Missing record':255(69247)T-52#
                    vcContext                  = "CInvoiceId=":U + string(tCInvoiceRef.tiCInvoice_ID)
                    tCInvoiceRef.tiCInvoice_ID = ?
                    viLocalReturn              = -1.
            <M-34 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'tCInvoiceRef.tiCInvoice_ID':U (icFieldName), 
                input  tCInvoiceRef.tiCInvoice_ID (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-720170':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
         end. /* not if available tqCInvoiceByID */
    end. /* for each tCInvoiceRef where */
    
end. /* MAIN_BLOCK: */
    
/* =================================================================================================== */
/* Return                                                                                              */
/* =================================================================================================== */
assign oiReturnStatus = viLocalReturn.