project QadFinancials > class BCInvoice > method PostSaveLegalDocumentGetLDId

Description

Actions to take after writing current instance to the database, and before final commit of the transaction.
Use the field tc_status to test the status of the updated records:
'' = unchanged
'N' = new
'C' = changed
'D' = deleted


Parameters


iiCInvoiceIdinputinteger
ocLegalDocumentDomainoutputcharacter
ocLegalDocumentNumberoutputcharacter
otLegalDocumentEffectiveDateoutputdate
ocLegalDocumentShipFromoutputcharacter
ocLegalDocumentShipoutputcharacter
oiReturnStatusoutputinteger


Internal usage


QadFinancials
method BCInvoice.PostSaveLegalDocument


program code (program1/bcinvoice.p)

/* ======================================================================================* *
 * Method       : PostSaveLegalDocumentGetLDId                                             *
 * Description  : I19 Requirement for Brazil                                               *
 *                This method retrieves legal document ID for which the invoice is created *
 * --------------------------------------------------------------------------------------- *
 * Parameters   :                                                                          *
 * ======================================================================================= */

define variable doGetLegalDocumentForCInvoice as com.qad.eefin.bmfglegaldocument.GetLegalDocumentForCInvoice no-undo.
define variable doGetLegalDocumentByPendVouch as com.qad.eefin.bmfglegaldocument.GetLegalDocumentByPendVouch no-undo.
 
MAIN_BLOCK:
do on error undo, throw:

    /* ================================================================================================= *
     * Default output values                                                                             *
     * ================================================================================================= */
    assign ocLegalDocumentDomain        = ?
           ocLegalDocumentNumber        = ?
           otLegalDocumentEffectiveDate = ?
           ocLegalDocumentShipFrom      = ?
           ocLegalDocumentShip          = ?.

    /* ================================================================================================= *
     * Preconditions                                                                                     *
     * ================================================================================================= */
    if iiCInvoiceId = 0 or
       iiCInvoiceId = ? 
    then return.


    /* ================================================================================================= *
     * Get the legal document invoice is created for                                                     *
     * ================================================================================================= */
     
    /* ================================================================================================= *
     * First of all try to get legal document based on the instance data of the receiver matching, that  *
     * is created at the same time                                                                       *
     * Legal document is not sotred on the supplier invoice. The one of the way is to get pending        *
     * voucher IDs from the receiver matching. Supplier invoice is always created only for one legal     *
     * document, so we can take pending voucher Id from any of the receiver matching lines               *
     * As the receiver matching is created at the same time as update of this invoice, we have to get    *
     * details from the receiver matching component                                                      *
     * ================================================================================================= */
    AP_MATCHING_INST_BLOCK:
    do on error undo, throw:
        /* Get instance of the Receiver matching component from the Transaction basket */
        <I-96 {bFcOpenInstance
             &CLASS           = "Transaction"}>
        
        /* Get handles to BAPMatching component */
        <M-89 run GetInstanceDetails
           (input  'BAPMatching':U (icComponentShortName), 
            input  viCompanyId (iiCompanyIdOfInstance), 
            output viBAPMatchingCIID (oiInstanceId), 
            output vhBAPMatchingCIInst (ohInstanceHandle), 
            output viFcReturnSuper (oiReturnStatus)) in Transaction>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then return.
        
        /* If there is not instance, stop this try */
        if viBAPMatchingCIID = 0 or viBAPMatchingCIID = ?
        then leave AP_MATCHING_INST_BLOCK.

        if not valid-handle(vhBAPMatchingCIInst)
        then do:
            <I-87 {bFcOpenInstance
                 &CLASS           = "BAPMatching"}>
        end.


        /* Read data from the APMatching component */            
        <M-66 run ReadTable
           (input  't_oAPMatching':U (icTableName), 
            input  'for each t_oAPMatching where t_oAPMatching.CInvoice_ID = ':U + string(iiCInvoiceId) (icQuery), 
            input  'tc_Rowid':U (icFieldNames), 
            input  no (ilConvertToDisplayFormat), 
            output vcAPMatchingRowId (ocFieldValues), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then return.
    
        if vcAPMatchingRowId = "":U or vcAPMatchingRowId = ?
        then leave AP_MATCHING_INST_BLOCK.

        assign vcQuery = "for each t_oAPMatchingLn where t_oAPMatchingLn.tc_ParentRowid = '":U + vcAPMatchingRowId + "'":U.

        <M-84 run ReadTable
           (input  't_oAPMatchingLn':U (icTableName), 
            input  vcQuery (icQuery), 
            input  'PvoDomain,PvoID,PvodLineID':U (icFieldNames), 
            input  no (ilConvertToDisplayFormat), 
            output vcPendingVoucherID (ocFieldValues), 
            output viFcReturnSuper (oiReturnStatus)) in BAPMatching>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0 then return.
                
        if num-entries(vcPendingVoucherId, chr(3)) <  3
        then leave AP_MATCHING_INST_BLOCK.
        
        
        /* Extract pending voucher ID */
        assign vcPvoDomain  = entry(1, vcPendingVoucherId, chr(3))
               viPvoID      = integer(entry(2, vcPendingVoucherId, chr(3)))
               viPvodLineId = integer(entry(3, vcPendingVoucherId, chr(3))).

        if vcPvoDomain  = ? or vcPvoDomain = "":U or
           viPvoID      = ? or viPvoID     = 0    or
           viPvodLineId = ?
        then leave AP_MATCHING_INST_BLOCK.
            
        /* Read legal document data based on the pending voucher from the database */
        publish "Logging.BusinessCode":U ("START-SUB,GetLegalDocumentByPendVouch":U, subst("In: vcPvoDomain=&1 viPvoId=&2 viPvodLine=&3":U, vcPvoDomain, viPvoId, viPvodLineId)).
        doGetLegalDocumentByPendVouch = new com.qad.eefin.bmfglegaldocument.GetLegalDocumentByPendVouch(vcPvoDomain,
                                                                                                        viPvoId,
                                                                                                        viPvodLineId).
        publish "Logging.BusinessCode":U ("END-SUB,GetLegalDocumentByPendVouch":U).
        
                                                                                                        
        if doGetLegalDocumentByPendVouch:available
        then assign ocLegalDocumentDomain        = doGetLegalDocumentByPendVouch:lgd_domain
                    ocLegalDocumentNumber        = doGetLegalDocumentByPendVouch:lgd_nbr
                    otLegalDocumentEffectiveDate = doGetLegalDocumentByPendVouch:lgd_effdate
                    ocLegalDocumentShipFrom      = doGetLegalDocumentByPendVouch:lgd_shipfrom
                    ocLegalDocumentShip          = doGetLegalDocumentByPendVouch:lgd_ship.
            

        finally:
            if valid-object(doGetLegalDocumentByPendVouch) then delete object doGetLegalDocumentByPendVouch no-error.
        end.     
    end. /* AP_MATCHING_INST_BLOCK: */
    
    /* ================================================================================================= *
     * Fall-back mechanism - when the Legal document is not found from the instance of the APMatching    *
     * then try to get legal document from the database starting with the supplier invoice               *
     * ================================================================================================= */
    if ocLegalDocumentDomain = ?
    then do on error undo, throw:

        publish "Logging.BusinessCode":U ("START-SUB,GetLegalDocumentForCInvoice":U, subst("In: iiCInvoiceId=&1":U, iiCInvoiceId)).
        doGetLegalDocumentForCInvoice = new com.qad.eefin.bmfglegaldocument.GetLegalDocumentForCInvoice(?, iiCInvoiceId, ?, ?, ?).
        publish "Logging.BusinessCode":U ("END-SUB,GetLegalDocumentForCInvoice":U).

        if doGetLegalDocumentForCInvoice:available
        then assign ocLegalDocumentDomain        = doGetLegalDocumentForCInvoice:lgd_domain
                    ocLegalDocumentNumber        = doGetLegalDocumentForCInvoice:lgd_nbr
                    otLegalDocumentEffectiveDate = doGetLegalDocumentForCInvoice:lgd_effdate
                    ocLegalDocumentShipFrom      = doGetLegalDocumentForCInvoice:lgd_shipfrom
                    ocLegalDocumentShip          = doGetLegalDocumentForCInvoice:lgd_ship.
         
        finally:
            if valid-object(doGetLegalDocumentForCInvoice) then delete object doGetLegalDocumentForCInvoice no-error.
        end.
        
    end. /* if ocLegalDocumentDomain = ? */
end. /* MAIN_BLOCK */