project QadFinancials > class BCInvoice > method CheckDuplicateCInvoiceReference

Description

Non-api and remoting-enabled method is segment 3 as this segments holds the existing defaulting-methods that are called during UI-entry.
This method will check for multiple invoices having the same reference and will be called from the UI (when leaving the main tabpage) and from the valdiations).


Parameters


iiCInvoiceIDinputintegerID of the CInvoice
iiCompanyIDinputintegerCompany of the CInvoice
iiCreditorIDinputintegerID of the creditor - optional as not required when icCreditorCode is filled
icCreditorCodeinputcharacterCode of the creditor - optional as not required when icCreditorCode is filled
icCInvoiceTypeinputcharacterType of the current invoice
icCInvoiceReferenceinputcharacterReference of the current invoice
ilCInvoiceIsInitialStatusinputlogicalTrue in case the invoice is marked as an initial one
iiLinkedPeriodYearinputintegerWhen the current CInvoice is linked to another CInvoice: year of that other CInvoice
icLinkedJournalCodeinputcharacterWhen the current CInvoice is linked to another CInvoice: JournalCode of that other CInvoice
iiLinkedCInvoiceVoucherinputintegerWhen the current CInvoice is linked to another CInvoice: Voucher of that other CInvoice
ilForCheckUIinputlogicalTo Check if call is Remoting.
ocCInvoiceReferenceDuplicateoutputcharacterCInvoiceReferenceDuplicate: This paramater will hold the combination of the conditions that were used to perform this check.
When this method is called and it did not raised an error than the corresponding field in tCInvoice should be filled with this values.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ValidateComponentPost


program code (program3/bcinvoice.p)

    
    /* ============================================================================================= */
    /* Return without any error in case icCInvoiceReference is empty or ilCInvoiceIsInitialStatus is */
    /* true or the current-activity is ‘Supplier Invoice Reverse’ or  ‘Supplier Invoice Replace' or  */
    /* in case icCInvoiceType is not Invoice, InvoiceCorrection, CreditNote or CreditNoteCorrection  */
    /* ============================================================================================= */
    if icCInvoiceReference       = "":U        or 
       icCInvoiceReference       = ?           or 
       ilCInvoiceIsInitialStatus = true        or
       vcActivityCode            = "Reverse":U or 
       (icCInvoiceType <> {&INVOICETYPE-INVOICE}           and 
        icCInvoiceType <> {&INVOICETYPE-INVOICECORRECTION} and 
        icCInvoiceType <> {&INVOICETYPE-CREDITNOTE}        and 
        icCInvoiceType <> {&INVOICETYPE-CREDITNOTECORRECTION})
    then Return.

    /* ============================================================================================= */        
    /* Validate that at least one of these fields is filled: iiCreditorID and icCreditorCode         */
    /* Default icCreditorCode based upon iiCreditorID in case icCreditorCode is not filled           */
    /* Default iiCreditorID based upon icCreditorCode in case iiCreditorID is not filled             */
    /* Validate that iiCInvoiceID, iiCompanyID, iiCreditorID, icCreditorCode, icCInvoiceType and     */
    /* icCInvoiceReference are filled                                                                */
    /* Find the tCInvoice record based upon iiCInvoiceID                                             */
    /* ============================================================================================= */
    if iiCompanyID = 0 or
       iiCompanyID = ?
    then do :
        assign vcMessage      = trim(substitute(#T-23'Internal error: unable to check for a duplicate invoice-reference as the entity of the invoice has not been specified.':255(177251380)T-23#))
               oiReturnStatus = -3.
        <M-63 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-278237':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /*if iiCompanyID = 0 */
    if (iiCreditorID = 0 or
        iiCreditorID = ?) and 
       (icCreditorCode = "":U or 
        icCreditorCode = ?)
    then do :
        assign vcMessage      = trim(substitute(#T-97'Internal error: unable to check for a duplicate invoice-reference as the supplier has not been specified.':255(567355223)T-97#))
               oiReturnStatus = -3.
        <M-81 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-521278':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /* if (iiCreditorID = 0 or */
    <Q-39 run CreditorPrim (all) (Read) (NoCache)
       (input iiCompanyID, (CompanyId)
        input iiCreditorID, (CreditorId)
        input icCreditorCode, (CreditorCode)
        output dataset tqCreditorPrim) in BCreditor >
    find tqCreditorPrim no-lock no-error. /* do not use the FIRST option as we really need to find a single creditor */
    if not available tqCreditorPrim
    then do :
        assign vcMessage      = trim(substitute(#T-3'Unable to check for a duplicate invoice-reference as the supplier could not be found based on its Code (&1), its ID (&2) and the Entity (&3).':255(682994251)T-3#,icCreditorCode,iiCreditorID,iiCompanyID))
               oiReturnStatus = -3.
        <M-83 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-436213':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /* if not available tqCreditorPrim */
    assign iiCreditorID   = tqCreditorPrim.tiCreditor_ID
           icCreditorCode = tqCreditorPrim.tcCreditorCode.
    Find tCInvoice where 
         tCInvoice.CInvoice_ID = iiCInvoiceID
         no-lock no-error.
    if not available tCInvoice
    then do :
        assign vcMessage      = trim(substitute(#T-74'Internal error: Unable to check for a duplicate invoice-reference as the supplier-invoice could not be found based on its ID (&1).':255(599550721)T-74#,iiCInvoiceID))
               oiReturnStatus = -3.
        <M-44 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-962451':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /* if not available tCInvoice */
    
    /* ========================================================================================================= */
    /* Compose a local data-item by concatenating following fields pipe-delimited:                               */
    /*  iiCompanyID, icCreditorCode, icCInvoiceType, icCInvoiceReference, ilCInvoiceIsInitialStatus              */
    /* Return without any error in case this local data-item matches tCInvoice.tcCInvoiceReferenceDuplicate      */
    /* as in that case none of the fields that impact this check are changed                                     */
    /* Set tCInvoice.tcCInvoiceReferenceDuplicate and olCInvoiceReferenceDuplicate = to this composed string     */
    /* ========================================================================================================= */
    assign vcCInvoiceReferenceDuplicate = string(iiCompanyID)  + "|":U + 
                                          icCreditorCode       + "|":U + 
                                          icCInvoiceType       + "|":U + 
                                          icCInvoiceReference  + "|":U +
                                          string(ilCInvoiceIsInitialStatus).
    if tCInvoice.tcCInvoiceReferenceDuplicate = vcCInvoiceReferenceDuplicate and
       ilForCheckUI = false
    then Return.
    assign tCInvoice.tcCInvoiceReferenceDuplicate = vcCInvoiceReferenceDuplicate /* no need to set the tc_status as this just is about a calculated field */
           ocCInvoiceReferenceDuplicate           = vcCInvoiceReferenceDuplicate.
    
    /* ========================================================================================= */
    /* Raise the warning in case there is already (in the instance or in the db) an invoice with */
    /* this reference in the same company, for the same creditor and for the same invoice-type   */
    /* ========================================================================================= */
    Find first btCInvoice where 
               btCInvoice.Company_ID         = iiCompanyID          and 
               btCInvoice.tcCreditorCode     = icCreditorCode       and 
               btCInvoice.CInvoiceType       = icCInvoiceType       and
               btCInvoice.CInvoiceReference  = icCInvoiceReference  and 
               btCInvoice.tc_Rowid          <> tCInvoice.tc_Rowid
               no-lock no-error.
    if available btCInvoice
    then do : 
        assign vcMessage = trim(substitute(#T-51'The reference (&1) already exists on another invoice (&2/&3/&4)  for this supplier (&5).':255(732658488)T-51#,icCInvoiceReference, btCInvoice.CInvoicePostingYear, btCInvoice.tcJournalCode, btCInvoice.CInvoiceVoucher, icCreditorCode)).
        if oiReturnStatus >= 0 
        then assign oiReturnStatus = +1.
        <M-89 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-827895':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /* if available btCInvoice */
    <Q-70 run CInvoiceForDuplicateReference (first) (Read) (NoCache)
       (input iiCompanyID, (CompanyId)
        input iiCreditorID, (CreditorID)
        input icCInvoiceType, (CInvoiceType)
        input icCInvoiceReference, (CInvoiceReference)
        input iiCInvoiceID, (CInvoiceIDNotEqual)
        output dataset tqCInvoiceForDuplicateReference) in BCInvoice >
    Find first tqCInvoiceForDuplicateReference /* No where clause needed as we already verified that the passed parameters were filled */
         no-lock no-error.
    if available tqCInvoiceForDuplicateReference
    then do : 
        assign vcMessage = trim(substitute(#T-84'The reference (&1) already exists on another invoice (&2/&3/&4)  for this supplier (&5).':255(732658488)T-84#,icCInvoiceReference, tqCInvoiceForDuplicateReference.tiCInvoicePostingYear, tqCInvoiceForDuplicateReference.tcJournalCode, tqCInvoiceForDuplicateReference.tiCInvoiceVoucher, icCreditorCode)).
        if oiReturnStatus >= 0 
        then assign oiReturnStatus = +1.
        <M-10 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-500498':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        Return.
    end. /* if available tqCInvoiceForDuplicateReference */