project QadFinancials > class BCInvoiceAPMatching > method CreateCInvoiceAPMatchingOneInOneTran

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 OneInOneTran                                                 */
/* 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.

/* =================================================================================================== */
/* Go through all inovices to be created                                                               */
/* =================================================================================================== */
MAIN_BLOCK:

for each tCInvoiceRef:
    
    assign viOneCInvoiceReturn = 0.

    CINVOICE_BLOCK:
    do on error undo, leave:    
        /* =================================================================================================== */
        /* Only do the INVOICE BLOCK when the invoices were not passed.                                        */
        /* =================================================================================================== */    
        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 leave CINVOICE_BLOCK.
           
        /* =================================================================================================== */
        /* Create supplier invoice (In separate transaction)                                                   */
        /* =================================================================================================== */
        <I-2 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION = "false"
             &CLASS              = "Transaction"}>    
             
        <M-3 run StartTransaction  (output viExternalReturnStatus (oiReturnStatus)) in Transaction>
        if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
       
        
        /* =================================================================================================== */
        /* Create Supplier Invoice incomming data                                                              */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 0
        then do:
            <M-9 run CreateCInvoice  (output viExternalReturnStatus (oiReturnStatus)) in BCInvoiceAPMatching>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
        end.
        
        /* =================================================================================================== */
        /* Create Supplier Invoice - execute the real method creation                                          */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 0
        then do:
            <M-28 run CreateCInvoiceRun  (output viExternalReturnStatus (oiReturnStatus)) in BCInvoiceAPMatching>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
        end.
        
        if viOneCInvoiceReturn >= 0
        then do:
            <M-55 run ValidateBCAndAdditionalUpdates  (output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
        end.
    
        
        /* =================================================================================================== */
        /* Stop transaction for this supplier invoice (Data have to be saved in database for RM)               */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 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 viOneCInvoiceReturn = 0)
            then assign viOneCInvoiceReturn  = viFcReturnSuper.
        end. /* if viOneCInvoiceReturn >= 0 */
        
        if viOneCInvoiceReturn < 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 viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = 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.
        end.
        
        finally:
            if not((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 do:        
                <I-8 {bFcCloseAndStopInstance
                     &CLASS           = "Transaction"}>
                assign viTransactionID = 0.
            end.
        end finally.
    end. /* CINVOICE_BLOCK */

    /* if creation of supplier invoice failed, there is not reason to continue */
    if viOneCInvoiceReturn < 0 
    then do:
        assign tCInvoiceRef.tiCInvoice_ID = ?.
        next MAIN_BLOCK.
    end.

    /* =================================================================================================== */
    /* Create receiver matching (in separate transaction)                                                  */
    /* =================================================================================================== */
    APMATCHING_BLOCK:
    do on error undo, leave:

        <I-10 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION = "false"
             &CLASS              = "Transaction"}>    
             
        <M-11 run StartTransaction  (output viExternalReturnStatus (oiReturnStatus)) in Transaction>
        if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
             
    
        /* =================================================================================================== */
        /* Create Receiver matching - data for real APMatching creation                                        */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 0
        then do:
            <M-13 run CreateAPMatching  (output viExternalReturnStatus (oiReturnStatus)) in BCInvoiceAPMatching>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
        end.
        
        /* =================================================================================================== */
        /* Create Receiver matching - run real data creation                                                   */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 0
        then do:
            <M-1 run CreateAPMatchingRun  (output viExternalReturnStatus (oiReturnStatus)) in BCInvoiceAPMatching>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.
        end.
        
        if viOneCInvoiceReturn >= 0
        then do:           
            <M-81 run ValidateBCAndAdditionalUpdates  (output viExternalReturnStatus (oiReturnStatus)) in BAPMatching>
            if viExternalReturnStatus < 0 or viExternalReturnStatus > 0 and viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn = viExternalReturnStatus.            
        end.
        
        /* =================================================================================================== */
        /* Finish transaction for this receiver matching                                                       */
        /* =================================================================================================== */
        if viOneCInvoiceReturn >= 0
        then do:
            /* Call a submethod that fills the class-level data-item vhBCInvoiceInstanceData that points to the newly created invoice */
            <M-40 run CreateCInvoiceAPMatchingGetCInvoiceData  (output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            if viFcReturnSuper < 0 or 
               (viFcReturnSuper > 0 and viOneCInvoiceReturn = 0) 
            then assign viOneCInvoiceReturn = viFcReturnSuper.
            if viOneCInvoiceReturn >= 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 viOneCInvoiceReturn = 0)
                then assign viOneCInvoiceReturn  = viFcReturnSuper.
            end. /* if viOneCInvoiceReturn >= 0 */
        end. /* if viOneCInvoiceReturn >= 0 */

        if viOneCInvoiceReturn < 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 viOneCInvoiceReturn = 0 then assign viOneCInvoiceReturn  = 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.
            
            /* Remove already created instance of the invoice in the initial status    */   
            /* Not needed when the invoice was existed before running this integration */   
            if not ((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 do:                                          
                <I-72 {bFcStartAndOpenInstance
                     &ADD-TO-TRANSACTION   = "false"
                     &CLASS                = "BCInvoice"}>    

                <M-56 run apiDeleteInstance
                   (input  'Primary=':U + string(tcInvoiceRef.tiCInvoice_ID) (icKey), 
                    output viExternalReturnStatus (oiReturnStatus)) in BCInvoice>

                <I-92 {bFcCloseAndStopInstance
                     &CLASS           = "BCInvoice"}>
                assign tCInvoiceRef.tiCInvoice_ID = ?.
            end. /* if not ((tCInvoiceRef.tiCInvoiceRegistrationNr <> ? and tCInvoiceRef.tiCInvoiceRegistrationNr <> 0 ) or */
        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                         */
    /* =================================================================================================== */
    if tCInvoiceRef.tiCInvoice_ID <> ? and
       tCInvoiceRef.tiCInvoice_ID <> 0
    then do:
         <Q-21 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-22'Missing record':255(69247)T-22#
                    vcContext                  = "CInvoiceId=":U + string(tCInvoiceRef.tiCInvoice_ID)
                    tCInvoiceRef.tiCInvoice_ID = ?
                    viLocalReturn              = -1.
            <M-23 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-8545':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
         end. /* not if available tqCInvoiceByID */
    end. /* if tCInvoiceRef.tiCInvoice_ID <> ? and */
    
end. /* for each tCInvoiceRef: and end of MAIN_BLOCK */

/* =================================================================================================== */
/* Return                                                                                              */
/* =================================================================================================== */
assign oiReturnStatus = viLocalReturn.