project QadFinancials > class BCInvoice > method PostSaveLegalDocument

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


oiReturnStatusoutputinteger


Internal usage


QadFinancials
method BCInvoice.PostSave


program code (program1/bcinvoice.p)

/* ====================================================================================== *
 * Method       : PostSaveLegalDocument                                                   *
 * Description  : I19 Requirtement for Brazil                                             *
 *                Update Legal document posting flag                                      *
 * -------------------------------------------------------------------------------------- *
 * Parameters   :                                                                         *
 * ====================================================================================== */

MAIN_BLOCK:
do on error undo, throw:
    /* I19 requirement for Brazil =============================================================================================== *
     * When the invoice is created for the Legal document, and this invoice is beeing or Finished or Reversed, then we have to    *
     * update also Legal document to indicate, it is or posted or un-posted                                                       *
     * ========================================================================================================================== */
    CINVOICE_BLOCK:
    for each tCInvoice where
             tCInvoice.tlCInvoiceIsForLegalDoc = true and
             (tCInvoice.tc_Status = "N":U or 
              tCInvoice.tc_Status = "C":U):
              
        /* If this is reversal of original Supplier invoice for the Legal document, do nothing */
        if tCInvoice.LinkedCInvoice_ID <> 0 and
           tCInvoice.LinkedCInvoice_ID <> ?
        then next.
             
        if tCInvoice.tc_Status = "C":U
        then do:
            find t_iCInvoice where
                 t_iCInvoice.tc_Rowid = tCInvoice.tc_Rowid
                 no-error.
        end. /* if tCInvoice.tc_Status = "C:U */
         
        /* Get legal document details */
        assign vcLegalDocumentDomain   = ?
               vcLegalDocumentNumber   = ?
               vtLegalDocumentEffDate  = ?
               vcLegalDocumentShipFrom = ?
               vcLegalDocumentShipTo   = ?
               vlLegalDocumentPosted   = ?.
         
        /* If the invoice is Finished and was not before, change the Legal document to be posted */
        if (tCInvoice.tc_Status = "N":U and tCInvoice.CInvoiceIsInitialStatus <> true ) or
           (tCInvoice.tc_Status = "C":U and tCInvoice.CInvoiceIsInitialStatus <> true  and t_iCInvoice.CInvoiceIsInitialStatus = true)
        then assign vlLegalDocumentPosted = true.
         
        /* If the invoice is beeing reversed, then change status of Legal document to be unposted */
        find first tLegalDocumentUpdateFromCInv where
                   tLegalDocumentUpdateFromCInv.tiCInvoiceID = tCInvoice.CInvoice_ID
                   no-error.
                    
        if available tLegalDocumentUpdateFromCInv 
        then assign vcLegalDocumentDomain   = tLegalDocumentUpdateFromCInv.tcDomainCode
                    vcLegalDocumentNumber   = tLegalDocumentUpdateFromCInv.tcLegalDocumentNumber
                    vtLegalDocumentEffDate  = tLegalDocumentUpdateFromCInv.ttLegalDocumentEffectiveDate
                    vcLegalDocumentShipfrom = tLegalDocumentUpdateFromCInv.tcLegalDocumentShipFrom
                    vcLegalDocumentShipTo   = tLegalDocumentUpdateFromCInv.tcLegalDocumentShip
                    vlLegalDocumentPosted   = tLegalDocumentUpdateFromCInv.tlLegalDocumentPostStatus.
                     
        /* If there is not status updated needed, continue with next invoice */
        if vlLegalDocumentPosted = ?
        then next CINVOICE_BLOCK.
         
         
        /* If the update should be done, but we don't know legal document yet, get it from the supplier invoice */
        if vcLegalDocumentDomain = ?
        then do:
            <M-14 run PostSaveLegalDocumentGetLDId
               (input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
                output vcLegalDocumentDomain (ocLegalDocumentDomain), 
                output vcLegalDocumentNumber (ocLegalDocumentNumber), 
                output vtLegalDocumentEffDate (otLegalDocumentEffectiveDate), 
                output vcLegalDocumentShipFrom (ocLegalDocumentShipFrom), 
                output vcLegalDocumentShipTo (ocLegalDocumentShip), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus  < 0 then return.
        end. /* if vcLegalDocumentDomain = ? */


        /* If the invoice has different supplier then legal document supplier, don't do update also *
         * as it will be updated by the invoice with the same supplier                              */
        if vlLegalDocumentPosted    = true                     and
           vcLegalDocumentShipFrom <> tCInvoice.tcCreditorCode and
           vcLegalDocumentShipTo   <> tCInvoice.tcCreditorCode
        then next.

        
        /* Update other invoice details stored on the supplier invoice */
        assign vcLegalDocumentInvNbr = substitute("&1/&2/&3":U, 
                                                  tCInvoice.CInvoicePostingYear,
                                                  tCInvoice.tcJournalCode,
                                                  string(tCInvoice.CInvoiceVoucher,"999999999"))
               vtLegalDocumentInvDate = tCInvoice.CInvoiceDate.
         

        /* Update the flag in the database */
        <M-1 run StartPersistence
           (output vhFcPL (ohPersistence), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus  < 0 then return.

            
        assign vcWhere       = "For each lgd_mstr where lgd_mstr.lgd_domain = '&1' and lgd_mstr.lgd_nbr = '&2' and lgd_mstr.lgd_effdate = date(&3,&4,&5) ":U
                             + " and lgd_mstr.lgd_shipfrom = '&6' and lgd_mstr.lgd_ship = '&7'":U
               vcWhere       = substitute(vcWhere, vcLegalDocumentDomain, 
                                                   vcLegalDocumentNumber, 
                                                   month(vtLegalDocumentEffDate),
                                                   day(vtLegalDocumentEffDate), 
                                                   year(vtLegalDocumentEffDate), 
                                                   vcLegalDocumentShipFrom, 
                                                   vcLegalDocumentShipTo)
               vhFcComponent = vhFcPL.
                
        /* If this is posting of confirmed legal document, update posted flag, invoice number */
        if vlLegalDocumentPosted
        then do:
            <M-20 run WriteDirect
               (input  'lgd_mstr':U (icTableName), 
                input  vcWhere (icPrepare), 
                input  'lgd_posted,lgd_inv_nbr,lgd_inv_date':U (icFieldList), 
                input  'l,c,t':U (icFieldListDataTypes), 
                input  'true':U + chr(2) + vcLegalDocumentInvNbr + chr(2) + string(vtLegalDocumentInvDate) (icAbsolute), 
                input  '':U (icIncremental), 
                input  {&TARGETPROCEDURE} (ihClass), 
                input  vcUserLogin (icUserLogin), 
                output viFcReturnSuper (oiReturnStatus)) in Progress>
            /* If the legal document was not find, then this LD was created for the Logistic charge of kombined LD */
            if viFcReturnSuper <>  -4 and
               (viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus  < 0 then return.
        end. /* if vlLegalDocumentPosted */
        else do: /* else if vlLegalDocumentPosted */
            /* If this is reversal of supplier invoice, only change Posted flag */
            <M-52 run WriteDirect
               (input  'lgd_mstr':U (icTableName), 
                input  vcWhere (icPrepare), 
                input  'lgd_posted':U (icFieldList), 
                input  'l':U (icFieldListDataTypes), 
                input  'false':U (icAbsolute), 
                input  '':U (icIncremental), 
                input  {&TARGETPROCEDURE} (ihClass), 
                input  vcUserLogin (icUserLogin), 
                output viFcReturnSuper (oiReturnStatus)) in Progress>
            /* If the legal document was not find, then this LD was created for the Logistic charge of kombined LD */
            if viFcReturnSuper <>  -4 and
               (viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus  < 0 then return.
        end. /* else if vlLegalDocumentPosted */
    end. /* for each tCInvoice where */
    
end. /* MAIN_BLOCK */