project QadFinancials > class BDInvoice > method PreSaveValidate

Description

Implement new validations to really ensure, there are really created only those recors which are really needed.


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.PreSave


program code (program5/bdinvoice.p)

/* ====================================================================================== *
 * Method       : PreSaveValidate                                                         *
 * Description  : Implement new validations to ensure, there are really created those     *
 *                records which are expected.                                             *
 * -------------------------------------------------------------------------------------- *
 * Parameters   :                                                                         *
 * ====================================================================================== */
assign oiReturnStatus = -98
       viLocalReturn  = 0.

MAIN_BLOCK:
do on error undo, return:
    
    /* =============================================================================== *
     * Get data from other components                                                  *
     * =============================================================================== */    
    /* Check which data from other components are needed */
    assign vlIsBPostingNeeded = can-find(first tDInvoiceMovement where
                                               tDInvoiceMovement.tc_Status = "N":U).

    /* Get handles to instances other needed components (During PreSave all components *
     * are instantiated)                                                               */
    if vlIsBPostingNeeded
    then do:
        <I-79 {bFcOpenInstance
             &CLASS           = "Transaction"}>
        
        /* Get handles to BJournalEntry component */
        <M-18 run GetInstanceDetails
           (input  'BJournalEntry' (icComponentShortName), 
            input  viCompanyId (iiCompanyIdOfInstance), 
            output viBJournalEntryDIID (oiInstanceId), 
            output vhBJournalEntryDIInst (ohInstanceHandle), 
            output viFcReturnSuper (oiReturnStatus)) in Transaction>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
    
        <I-31 {bFcCloseInstance
             &CLASS           = "Transaction"}>
        if viLocalReturn < 0 then leave MAIN_BLOCK.
    end. /* if vlIsBPostingNeeded */
    
    /* Get Journal Entry details */
    empty temp-table tValPostingRefBDI.
    empty temp-table tValPostingLineRefBDI.
    if valid-handle(vhBJournalEntryDIInst)
    then do:
        <M-3 run GetInstanceData
           (output tValPostingRefBDI (tApiPosting), 
            output tValPostingLineRefBDI (tApiPostingLine), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn < 0 then leave MAIN_BLOCK.
    end.

    /* =============================================================================== *
     * Validate invoices                                                               *
     * =============================================================================== */
    for each tDInvoice where
             tDInvoice.tc_Status <> "D":U:

        /* Invoice have to have at least one movement */
        if not can-find(first tDInvoiceMovement where
                              tDInvoiceMovement.tc_ParentRowid = tDInvoice.tc_Rowid and
                              tDInvoiceMovement.tc_Status     <> "D":U)
        then do:
            assign vcMessage     = #T-74'Customer invoice &1/&2/&3 have to have at least one movement.':255(265154143)T-74#
                   vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                   viLocalReturn = -1.
            <M-85 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  tDInvoice.tc_Rowid (icRowid), 
                input  'QadFin-63476':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        end.

        /* Invoice can have only one movement of type "Initial" */
        if not can-find(tDInvoiceMovement where
                        tDInvoiceMovement.tc_ParentRowid       = tDInvoice.tc_Rowid      and
                        tDInvoiceMovement.DInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} and
                        tDInvoiceMovement.tc_Status           <> "D":U)
        then do:
            assign vcMessage     = #T-44'There is missing Initial movement record for Customer invoice &1/&2/&3.':255(379653460)T-44#
                   vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                   viLocalReturn = -1.
            <M-86 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  tDInvoice.tc_Rowid (icRowid), 
                input  'QadFin-326705':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        end.

        /* ===================================================================================== *
         * DInvoice movement validations                                                         *
         * ===================================================================================== */
        for each tDInvoiceMovement where
                 tDInvoiceMovement.tc_ParentRowid = tDInvoice.tc_Rowid and
                 tDInvoiceMovement.tc_Status     <> "D":U:

            /* Every movement have to have Company to which it belongs */
            if tDInvoiceMovement.Company_ID = 0 or
               tDInvoiceMovement.Company_ID = ?
            then do:
                assign vcMessage     = #T-20'Customer invoice &1/&2/&3 has a movement with missing Entity to which Invoice Movement belongs.':255(621671102)T-20#
                       vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                       viLocalReturn = -1.
                <M-54 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  3 (iiSeverity), 
                    input  tDInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-457721':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                next.
            end.

            /* Every movement have to have posting link to it */
            if tDInvoiceMovement.PostingLine_ID = 0 or
               tDInvoiceMovement.PostingLine_ID = ?
            then do:
                assign vcMessage     = #T-48'Customer invoice &1/&2/&3. There is missing Posting Line linked to Movement.':255(721245871)T-48#
                       vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                       viLocalReturn = -1.
                <M-35 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  3 (iiSeverity), 
                    input  tDInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-910705':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                next.
            end.

            /* For every new DInvoiceMovement posting record have to be created */
            if tDInvoiceMovement.tc_Status = "N":U
            then do:
                find tValPostingLineRefBDI where
                     tValPostingLineRefBDI.PostingLine_ID = tDInvoiceMovement.PostingLine_ID
                     no-error.
                if available tValPostingLineRefBDI
                then find tValPostingRefBDI where
                          tValPostingRefBDI.tc_Rowid = tValPostingLineRefBDI.tc_ParentRowid
                          no-error.

                if not available tValPostingRefBDI or
                   not available tValPostingLineRefBDI
                then do:
                    assign vcMessage     = #T-7'Customer invoice &1/&2/&3. There is missing Posting Line linked to Movement.':255(721245871)T-7#
                           vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                           viLocalReturn = -1.
                   <M-17 run SetMessage
                      (input  vcMessage (icMessage), 
                       input  '':U (icArguments), 
                       input  '':U (icFieldName), 
                       input  '':U (icFieldValue), 
                       input  'S':U (icType), 
                       input  3 (iiSeverity), 
                       input  tDInvoice.tc_Rowid (icRowid), 
                       input  'QadFin-172809':U (icFcMsgNumber), 
                       input  '':U (icFcExplanation), 
                       input  '':U (icFcIdentification), 
                       input  '':U (icFcContext), 
                       output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                    next.
                end.

                /* Posting record have to be created to Customer control account */
                if tValPostingLineRefBDI.tcGLTypeCode <> {&GLTYPECODE-DEBT}
                then do:
                    assign vcMessage     = #T-63'Customer invoice &1/&2/&3 movement have to be posted on Customer Control Account.':255(538407633)T-63#
                           vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-84 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  3 (iiSeverity), 
                        input  tDInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-705532':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                end.

                /* Posting Year/Period have to be the same on Posting and on Movement */
                if tValPostingRefBDI.PostingYearPeriod <> tDInvoiceMovement.DInvoiceMovementYearPeriod
                then do:
                    assign vcMessage     = #T-92'Customer invoice &1/&2/&3 movement have to have the same GL Period as created posting.':255(736108078)T-92#
                           vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-67 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  3 (iiSeverity), 
                        input  tDInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-215936':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                end.

                /* Posting date have to be the same in the Movement and the Posting */
                if tValPostingRefBDI.PostingDate <> tDInvoiceMovement.DInvoiceMovementPostDate
                then do:
                    assign vcMessage     = #T-77'Customer invoice &1/&2/&3 movement have to have the same posting date as created posting.':255(70153334)T-77#
                           vcMessage     = substitute(vcMessage, tDInvoice.DInvoicePostingYear, tDInvoice.tcJournalCode, tDInvoice.DInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-34 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  3 (iiSeverity), 
                        input  tDInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-628867':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                end.
            end. /* if tDInvoiceMovement.tc_Status = "N":U */
        end. /* for each tDInvoiceMovement where */
    end. /* for each tDInvoice: */
end. /* MAIN_BLOCK */

assign oiReturnStatus = viLocalReturn.