project QadFinancials > class BCInvoice > 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 BCInvoice.PreSave


program code (program5/bcinvoice.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 tCInvoiceMovement where
                                               tCInvoiceMovement.tc_Status = "N":U) or
                                can-find(first tCInvoicePosting where
                                               tCInvoicePosting.tc_Status = "N":U).

    /* Get handles to instances other needed components (During PreSave all components *
     * are instantiated)                                                               */
    if vlIsBPostingNeeded
    then do:
        <I-7 {bFcOpenInstance
             &CLASS           = "Transaction"}>    
        
        /* Get handles to BJournalEntry component */
        <M-52 run GetInstanceDetails
           (input  'BJournalEntry':U (icComponentShortName), 
            input  viCompanyId (iiCompanyIdOfInstance), 
            output viBJournalEntryCIID (oiInstanceId), 
            output vhBJournalEntryCIInst (ohInstanceHandle), 
            output viFcReturnSuper (oiReturnStatus)) in Transaction>
        if viFcReturnSuper <> 0 then assign viLocalReturn = viFcReturnSuper.
    
        <I-20 {bFcCloseInstance
             &CLASS           = "Transaction"}>
        if viLocalReturn < 0 then leave MAIN_BLOCK.
    end. /* if vlIsBPostingNeeded */

    /* Get Journal Entry details */
    empty temp-table tValPostingRefBCI.
    empty temp-table tValPostingLineRefBCI.

    if valid-handle(vhBJournalEntryCIInst)
    then do:
        <M-40 run GetInstanceData
           (output tValPostingRefBCI (tApiPosting), 
            output tValPostingLineRefBCI (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 tCInvoice where
             tCInvoice.tc_Status <> "D":U:

        /* Find original record */
        if tCInvoice.tc_Status <> "N":U
        then do:
            find t_iCInvoice where
                 t_iCInvoice.tc_Rowid = tCInvoice.tc_Rowid
                 no-error.
            if not available t_iCInvoice
            then do:
                assign vcMessage     = #T-76'Could not find initial values for supplier invoice &1/&2/&3.':255(156783171)T-76#
                       vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                       viLocalReturn = -1.
                <M-16 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  2 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-286440':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                next.
            end.
        end. /* Find original record */

        /* Invoice created in Initial Status can have no posting created */
        if tCInvoice.CInvoiceIsInitialStatus and
           can-find(first tCInvoiceMovement where
                          tCInvoiceMovement.tc_ParentRowid = tCInvoice.tc_Rowid)
        then do:
             assign vcMessage     = #T-38'Supplier invoice &1/&2/&3 in the Initial status cannot have any movement.':255(32407798)T-38#
                    vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                    viLocalReturn = -1.
            <M-24 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  2 (iiSeverity), 
                input  tCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-553604':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        end.

        /* Invoice in non initial status have to have at least one movement */
        if not tCInvoice.CInvoiceIsInitialStatus                                 and
           not can-find(first tCInvoiceMovement where
                              tCInvoiceMovement.tc_ParentRowid = tCInvoice.tc_Rowid and
                              tCInvoiceMovement.tc_Status     <> "D":U)
        then do:
            assign vcMessage     = #T-57'Supplier invoice &1/&2/&3 in non Initial status have to have at least one movement':255(106184519)T-57#
                   vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                   viLocalReturn = -1.
            <M-71 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  2 (iiSeverity), 
                input  tCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-816962':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        end.

        /* Invoice created in non initial status can have only one movement of type "Initial" */
        if not tCInvoice.CInvoiceIsInitialStatus                                 and
           not can-find(tCInvoiceMovement where
                        tCInvoiceMovement.tc_ParentRowid       = tCInvoice.tc_Rowid      and
                        tCInvoiceMovement.CInvoiceMovementType = {&MOVEMENTTYPE-INITIAL} and
                        tCInvoiceMovement.tc_Status           <> "D":U)
        then do:
            assign vcMessage     = #T-53'There is missing Initial movement record for Supplier invoice &1/&2/&3.':255(285260843)T-53#
                   vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                   viLocalReturn = -1.
            <M-86 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  2 (iiSeverity), 
                input  tCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-265977':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        end.

        /* MIZ Temporary commented until BA decission received 
        if (tCInvoice.CInvoiceAllocationStatus = {&ALLOCSTATUS-ALLOC} or
            tCInvoice.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC}) and
           not can-find(first tCInvoicePosting where
                              tCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-ALLOCATION})
        then do:
            assign vcMessage     = #T-54'Supplier invoice &1\&2\&3 with allocation status Allocate or Transient allocation have to have Allocation posting created.':255(152587551)T-54#
                   vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                   viLocalReturn = -1.
            <M-45 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'S':U (icType), 
                input  2 (iiSeverity), 
                input  tCInvoice.tc_Rowid (icRowid), 
                input  'QadFin-539009':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        end.
        */

        /* ===================================================================================== *
         * CInvoice movement validations                                                         *
         * ===================================================================================== */
        for each tCInvoiceMovement where
                 tCInvoiceMovement.tc_ParentRowid = tCInvoice.tc_Rowid and
                 tCInvoiceMovement.tc_Status     <> "D":U:

            /* Every movement have to have Company to which it belongs */
            if tCInvoiceMovement.Company_ID = 0 or
               tCInvoiceMovement.Company_ID = ?
            then do:
                assign vcMessage     = #T-21'Supplier invoice &1/&2/&3 has a movement with missing Entity to which Invoice Movement belongs.':255(985951305)T-21#
                       vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                       viLocalReturn = -1.
                <M-89 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  2 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-718600':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                next.
            end.

            /* Every movement have to have posting link to it */
            if tCInvoiceMovement.PostingLine_ID = 0 or
               tCInvoiceMovement.PostingLine_ID = ?
            then do:
                assign vcMessage     = #T-17'There is missing Posting Line linked to Movement.':255(230703880)T-17#
                       vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                       viLocalReturn = -1.
                <M-87 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  2 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-885892':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                next.
            end.

            /* For every new CInvoiceMovement posting record have to be created */
            if tCInvoiceMovement.tc_Status = "N":U
            then do:
                find tValPostingLineRefBCI where
                     tValPostingLineRefBCI.PostingLine_ID = tCInvoiceMovement.PostingLine_ID
                     no-error.
                if available tValPostingLineRefBCI
                then find tValPostingRefBCI where
                          tValPostingRefBCI.tc_Rowid = tValPostingLineRefBCI.tc_ParentRowid
                          no-error.

                if not available tValPostingLineRefBCI or
                   not available tValPostingRefBCI
                then do:
                    assign vcMessage     = #T-95'There is missing Posting Line linked to Movement.':255(230703880)T-95#
                           vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-63 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  2 (iiSeverity), 
                        input  tCInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-398511':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                    next.
                end.

                /* Posting record have to be created to Creditor control account */
                if tValPostingLineRefBCI.tcGLTypeCode <> {&GLTYPECODE-CRED}
                then do:
                    assign vcMessage     = #T-25'Supplier invoice &1/&2/&3 movement have to be posted on Customer Control Account.':255(964668977)T-25#
                           vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-49 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  2 (iiSeverity), 
                        input  tCInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-251323':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                end.

                /* Posting Year/Period have to be the same on Posting and on Movement */
                if tValPostingRefBCI.PostingYearPeriod <> tCInvoiceMovement.CInvoiceMovementYearPeriod
                then do:
                    assign vcMessage     = #T-34'Supplier invoice &1/&2/&3 movement have to have the same GL Period as created posting.':255(673197927)T-34#
                           vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-67 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  2 (iiSeverity), 
                        input  tCInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-801908':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                end.

                /* Posting date have to be the same in the Movement and the Posting */
                if tValPostingRefBCI.PostingDate <> tCInvoiceMovement.CInvoiceMovementPostDate
                then do:
                    assign vcMessage     = #T-22'Supplier invoice &1/&2/&3 movement have to have the same posting date as created posting.':255(216658966)T-22#
                           vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                           viLocalReturn = -1.
                    <M-74 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'S':U (icType), 
                        input  2 (iiSeverity), 
                        input  tCInvoice.tc_Rowid (icRowid), 
                        input  'QadFin-109292':U (icFcMsgNumber), 
                        input  '':U (icFcExplanation), 
                        input  '':U (icFcIdentification), 
                        input  '':U (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                end.
            end. /* if tCInvoiceMovement.tc_Status = "N":U */
        end. /* for each tCInvoiceMovement where */
        
        /* ===================================================================================== *
         * CInvoice posting validations                                                          *
         * ===================================================================================== */
        for each tCInvoicePosting where
                 tCInvoicePosting.tc_Status = "N":U:

            if not can-find(tValPostingRefBCI where
                            tValPostingRefBCI.Posting_ID = tCInvoicePosting.Posting_ID)
            then do:
                assign vcMessage     = #T-94'Supplier invoice &1/&2/&3 has missing created Posting.':255(952430963)T-94#
                       vcMessage     = substitute(vcMessage, tCInvoice.CInvoicePostingYear, tCInvoice.tcJournalCode, tCInvoice.CInvoiceVoucher)
                       viLocalReturn = -1.
                <M-73 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  2 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'QadFin-158575':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            end.
        end. /* for each tCInvoicePosting where */
    end. /* for each tCInvoice: */
end. /* MAIN_BLOCK */

assign oiReturnStatus = viLocalReturn.