project QadFinancials > class BCInvoice > method AllocateCInvoices

Description

match / allocate creditor invoices based on temp table
* give the ci a new reason code (that allows allocation)
* alloc status is now allocated
* create cinvoiceposting record for it


Parameters


tAllocateCInvoiceinput-outputtemp-tableMatch CInvoices
tApiCInvoiceVatinputtemp-tabletemp table like cinvoicevat table : with the tax records of the supplier invoice
tApiCInvoiceWHTinputtemp-tabletemp table like cinvoiceWHT table : with the WTH records of the supplier invoice
iiBJournalEntryIdinputintegerInstance of BJournalEntry
ilBAPMatchingInvoiceinputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BAPMatching.AdditionalUpdatesAllExtCInvoice
method BExpenseNote.APIExpNoteRegister


program code (program7/bcinvoice.p)

assign vlBAPMatchingInvoice = ilBAPMatchingInvoice.

/* Set default return status */
/* give journalentry id otherwise the CIposting will be deleted in additionalupdatescheckallocstatus */
assign oiReturnStatus      = -98
       viBJournalEntryCIID = iiBJournalEntryId
       viLocalReturnStatus = 0.

<Q-41 run CInvoiceByCyIDYearJrnlVchr  (Start) in BCInvoice >

/* Go through all input records */
for each tAllocateCInvoice:
    
    /* ====================== */
    /* replace unknown values */
    /* ====================== */
    if tAllocateCInvoice.tiCInvoiceId = ? then assign tAllocateCInvoice.tiCInvoiceId = 0.
    if tAllocateCInvoice.tcJournalCode = ? then assign tAllocateCInvoice.tcJournalCode = "":U.
    if tAllocateCInvoice.tcReasonCode = ? then assign tAllocateCInvoice.tcReasonCode = "":U.
    if tAllocateCInvoice.tiCICompanyId = ? then assign tAllocateCInvoice.tiCICompanyId = 0.
    if tAllocateCInvoice.tiCInvoiceVoucher = ? then assign tAllocateCInvoice.tiCInvoiceVoucher = 0.
    if tAllocateCInvoice.tiPeriodYear = ? then assign tAllocateCInvoice.tiPeriodYear = 0.
    if tAllocateCInvoice.tiReasonId = ? then assign tAllocateCInvoice.tiReasonId = 0.
    if tAllocateCInvoice.tdCInvoiceHoldAmountTC = ? then assign tAllocateCInvoice.tdCInvoiceHoldAmountTC = 0.
    if tAllocateCInvoice.tlCInvoiceIsLogisticMatching = ? then assign tAllocateCInvoice.tlCInvoiceIsLogisticMatching = false.
    
    /* ========================================================================================================= */
    /* Get the AccountsPayableContril-file flag that indicates if the whole balance should be put on hold or not */
    /* Get this flag only once and only when there is a hold-amount                                              */
    /* ========================================================================================================= */
    if vlPutWholeBalanceOnHold                   = ? and
       tAllocateCInvoice.tdCInvoiceHoldAmountTC <> 0
    then do :
        <Q-47 run AccountsPayableCtrlByDomain (all) (Read) (NoCache)
           (input vcDomainCode, (DomainCode)
            output dataset tqAccountsPayableCtrlByDomain) in BMfgAccountPayableCtrl >
        Find first tqAccountsPayableCtrlByDomain where 
                   tqAccountsPayableCtrlByDomain.tlapc_holdinvoiceamt = false no-lock no-error.
        assign vlPutWholeBalanceOnHold = (available tqAccountsPayableCtrlByDomain).
    end. /* if vlPutWholeBalanceOnHold                   = ? and */
    
    /* ============================================================================ */
    /* Find the CInvoice in the instance or in the db based on the input parameters */
    /* ============================================================================ */
    assign vlFcQueryRecordsAvailable = false.
    if tAllocateCInvoice.tiCInvoiceId <> 0 and 
       can-find (first tCInvoice where 
                       tCInvoice.CInvoice_ID = tAllocateCInvoice.tiCInvoiceId)
    then next.

    if tAllocateCInvoice.tiCICompanyId        <> 0    and
       tAllocateCInvoice.tiPeriodYear         <> 0    and
       tAllocateCInvoice.tcJournalCode        <> "":U and
       tAllocateCInvoice.tiCInvoiceVoucher    <> 0  and
       tAllocateCInvoice.tiCInvoiceId         = 0
    then do :
        find first tCinvoice where
                   tCInvoice.Company_ID          = tAllocateCInvoice.tiCICompanyId and
                   tCInvoice.CInvoicePostingYear = tAllocateCInvoice.tiPeriodYear  and
                   tCInvoice.tcJournalCode       = tAllocateCInvoice.tcJournalCode and
                   tCInvoice.CInvoiceVoucher     = tAllocateCInvoice.tiCInvoiceVoucher
                   no-error.
        if available tCinvoice
        then do :
            assign tAllocateCInvoice.tiCInvoiceId = tCInvoice.CInvoice_ID.
            next.
        end. /* if available tCinvoice */
    end. /* if tAllocateCInvoice.tiCICompanyId     <> 0    and */

    if vlFcQueryRecordsAvailable      <> true and 
       tAllocateCInvoice.tiCInvoiceId <> 0
    then do:
        <Q-38 assign vlFcQueryRecordsAvailable = CInvoiceByCyIDYearJrnlVchr (NoCache)
           (input 0, (CompanyId)
            input tAllocateCInvoice.tiCInvoiceId, (CInvoiceId)
            input 0, (PeriodYear)
            input 0, (JournalId)
            input '':U, (JournalCode)
            input 0, (Voucher)) in BCInvoice >
    end. /* if vlFcQueryRecordsAvailable      <> true and */
    if vlFcQueryRecordsAvailable <> true
    then do:
        <Q-39 run CInvoiceByCyIDYearJrnlVchr (all) (Read) (NoCache)
           (input tAllocateCInvoice.tiCICompanyId, (CompanyId)
            input 0, (CInvoiceId)
            input tAllocateCInvoice.tiPeriodYear, (PeriodYear)
            input 0, (JournalId)
            input tAllocateCInvoice.tcJournalCode, (JournalCode)
            input tAllocateCInvoice.tiCInvoiceVoucher, (Voucher)
            output dataset tqCInvoiceByCyIDYearJrnlVchr) in BCInvoice >
        find tqCInvoiceByCyIDYearJrnlVchr where
             tqCInvoiceByCyIDYearJrnlVchr.tiCompany_ID      = tAllocateCInvoice.tiCICompanyId   and
             tqCInvoiceByCyIDYearJrnlVchr.tiPeriodYear      = tAllocateCInvoice.tiPeriodYear   and
             tqCInvoiceByCyIDYearJrnlVchr.tcJournalCode     = tAllocateCInvoice.tcJournalCode and
             tqCInvoiceByCyIDYearJrnlVchr.tiCInvoiceVoucher = tAllocateCInvoice.tiCInvoiceVoucher
             no-lock no-error.
        if not available tqCInvoiceByCyIDYearJrnlVchr
        then do:
            assign vcMessage      = trim(#T-16'Supplier invoice not found.':200(1105)T-16#) + chr(10) + 
                                    trim(substitute(#T-17'Entity: &1 / Supplier Invoice: &2 / GL Calendar Year: &3 / Daybook: &4 / Voucher: &5':255(999890717)T-17#,
                                                    trim(string(tAllocateCInvoice.tiCICompanyId)), trim(string(tAllocateCInvoice.tiCInvoiceId)), trim(string(tAllocateCInvoice.tiPeriodYear)), trim(tAllocateCInvoice.tcJournalCode), trim(string(tAllocateCInvoice.tiCInvoiceVoucher)) ))
                   oiReturnStatus = -1.
            <M-4 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-2110':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
             next.
        end.
        assign tAllocateCInvoice.tiCInvoiceId = tqCInvoiceByCyIDYearJrnlVchr.tiCInvoice_ID.
    end. /* if vlFcQueryRecordsAvailable <> true */
    
    /* ======================================================= */
    /* Compose a list with the incoives that need to be loaded */
    /* ======================================================= */
    if not can-find (first tCInvoice where
                           tCInvoice.CInvoice_ID = tAllocateCInvoice.tiCInvoiceId)
    then assign vcListCInvoiceIds =  if vcListCInvoiceIds = "":U
                                     then string(tAllocateCInvoice.tiCInvoiceId)
                                     else
                                     if lookup(string(tAllocateCInvoice.tiCInvoiceId), vcListCInvoiceIds, chr(4)) = 0
                                     then vcListCInvoiceIds + chr(4) + string (tAllocateCInvoice.tiCInvoiceId)
                                     else vcListCInvoiceIds.

end. /* for each tAllocateCInvoice: */

<Q-42 run CInvoiceByCyIDYearJrnlVchr  (Stop) in BCInvoice >

/* ================================== */
/* if a cinvoice is not found, return */
/* Otherwise: load the required ones  */
/* ================================== */
if oiReturnStatus < 0 and oiReturnStatus <> -98 then return.

if vcListCInvoiceIds <> "":U
then do:
    /* ======== */
    /* Dataload */
    /* ======== */
    <M-5 run DataLoad
          (input  '':U (icRowids), 
           input  vcListCInvoiceIds (icPkeys), 
           input  '':U (icObjectIds), 
           input  '' (icFreeform), 
           input  false (ilKeepPrevious), 
           output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do :
            assign vcMessage      = trim(substitute(#T-18'Data could not be loaded: unable to load the list of supplier invoices (&1).':100(1107)T-18#, replace(vcListCInvoiceIds, chr(4), ",":U))).
            <M-6 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2111':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>    
            return.
        end. /* if oiReturnStatus < 0 */
    end. /* if viFcReturnSuper <> 0 */
end. /* if vcListCInvoiceIds <> "":U */


/* ============================ */
/* Go through all input records */
/* ============================ */
for each tAllocateCInvoice :
    
    /* ================== */
    /* search the invoice */
    /* ================== */
    find first tCInvoice where
               tCInvoice.CInvoice_ID = tAllocateCInvoice.tiCInvoiceId
               no-error.
    if not available tCInvoice
    then do:
        assign vcMessage           = trim(substitute(#T-19'Invalid supplier invoice: &1.':255(1108)T-19#,string(tAllocateCInvoice.tiCInvoiceId)))
               viLocalReturnStatus = -1.
        <M-7 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2112':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>    
        next.
    end.

    <M-40 run AllocateCInvoicesValidate  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0
    then next.   

    /* get a number and create a SI Posting for initial status invoices */
    if tCInvoice.CInvoiceIsInitialStatus 
    then do:
         /* if the tax is defined with tax by line false, then don't do anything */
        <Q-48 assign vlFcQueryRecordsAvailable = CInvoiceVatForTaxByLine (NoCache)
           (input tAllocateCInvoice.tiCInvoiceId, (CInvoiceId)
            input tCInvoice.Company_ID, (CompanyId)
            input vcDomainCode, (DomainCode)) in BCInvoice>
        if vlFcQueryRecordsAvailable = false
        then do:
            /* copy the taxes from the matching, so first delete old records, because matching is definite */
            /* if there are no taxes supplied from APMatching, then keep those already on the invoice */
            if can-find(first tApiCInvoiceVat where 
                              tApiCInvoiceVat.tc_Status   <> "D":U and 
                              tApiCInvoiceVat.CInvoice_ID  = tAllocateCInvoice.tiCInvoiceId)
            then do:
                for each tCInvoiceVat where
                         tCInvoiceVat.tc_ParentRowid = tCInvoice.tc_Rowid:
                    if tCInvoiceVat.tc_Status = "N":U
                    then delete tCInvoiceVat.
                    else assign tCInvoiceVat.tc_Status = "D":U.
                end. /* for each tCInvoiceVat where */
                for each tApiCInvoiceVat where 
                         tApiCInvoiceVat.tc_Status   <> "D":U and 
                         tApiCInvoiceVat.CInvoice_ID  = tAllocateCInvoice.tiCInvoiceId :
                    <M-37 run AddDetailLine
                       (input  'CInvoiceVat':U (icTable), 
                        input  tCInvoice.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                    if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
                    then assign viLocalReturnStatus = viFcReturnSuper.
                    if viLocalReturnStatus < 0
                    then leave.   
                    buffer-copy tApiCInvoiceVat 
                            except CInvoiceVat_ID CInvoice_ID tc_rowid tc_ParentRowid tc_status 
                            to tCInvoiceVat.
                    /* Make sure the sign of the NonRecTaxAmtTC is correct regardless the value the caller passes in */
                    if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
                       tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE}
                    then assign tCInvoiceVat.CInvoiceVatNonRecTaxAmtTC = absolute(tCInvoiceVat.CInvoiceVatNonRecTaxAmtTC).
                    else assign tCInvoiceVat.CInvoiceVatNonRecTaxAmtTC = absolute(tCInvoiceVat.CInvoiceVatNonRecTaxAmtTC) * -1.
                end. /* for each tApiCInvoiceVat */
            end. /* if can-find(first tApiCInvoiceVat where */

            /* copy the WHT from the matching, so first delete old records, because matching is definite */
            for each tCInvoiceWHT where
                     tCInvoiceWHT.tc_ParentRowid = tCInvoice.tc_Rowid:
                if tCInvoiceWHT.tc_Status = "N":U
                then delete tCInvoiceWHT.
                else assign tCInvoiceWHT.tc_Status = "D":U.
            end. /* for each tCInvoiceWHT where */
            for each tApiCInvoiceWHT where 
                     tApiCInvoiceWHT.tc_Status   <> "D":U and 
                     tApiCInvoiceWHT.CInvoice_ID  = tAllocateCInvoice.tiCInvoiceId :
                <M-86 run AddDetailLine
                   (input  'CInvoiceWHT':U (icTable), 
                    input  tCInvoice.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0
                then leave.   
                buffer-copy tApiCInvoiceWHT 
                        except CInvoiceWHT_ID CInvoice_ID tc_rowid tc_ParentRowid tc_status 
                        to tCInvoiceWHT.
            end. /* for each tApiCInvoiceWHT */
        end. /* if vlFcQueryRecordsAvailable = false */

        if tCInvoice.CInvoiceVoucher = 0
        then do:
            /* if not yet a voucher is filled, get it based on the journal */
            /* Reserve a number for this sheet.                          */
            /* This number will be released again by housekeeping if the */
            /* transaction was not committed by then.                    */
            /* ============================================== */
            /* Check if consecutive numbering is enabled.     */
            /* If so, use a temporary number.                 */
            /* Otherwise use standard numbering functionality */
            /* ============================================== */
            if vlCIDomainIsConsecutNr = true                        and
               (tCInvoice.CInvoiceType <> {&INVOICETYPE-PREPAYMENT} and
 	         tCInvoice.CInvoiceType <> {&INVOICETYPE-ADJUSTMENT})
            then assign viTempCInvoiceVoucher     = viTempCInvoiceVoucher + 1
                        tCInvoice.CInvoiceVoucher = viTempCInvoiceVoucher.
            else do:
                assign vhFcComponent = ?.
                <M-33 run GetNumber
                   (input  tAllocateCInvoice.tiCICompanyId (iiCompanyId), 
                    input  tAllocateCInvoice.tiPeriodYear (iiNumbrYear), 
                    input  tAllocateCInvoice.tcJournalCode (icNumbrType), 
                    output tCInvoice.CInvoiceVoucher (oiNumber), 
                    input  viFcCurrentInstanceId (iiInstanceId), 
                    input  vcFcComponentName (icClassName), 
                    output viFcReturnSuper (oiReturnStatus)) in BNumber>
                if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0
                then next.  
            end. /* else do: */
        end.
        
        assign tAllocateCInvoice.tiCInvoiceVoucher = tCInvoice.CInvoiceVoucher.    
        
        <Q-35 run CreditorForCInvoiceDefault (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input tCInvoice.tcCreditorCode, (CreditorCode)
            input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
            output dataset tqCreditorForCInvoiceDefault) in BCreditor >
        find first tqCreditorForCInvoiceDefault where
                   tqCreditorForCInvoiceDefault.tcCreditorCode     = tCInvoice.tcCreditorCode and
                   tqCreditorForCInvoiceDefault.tlCreditorIsActive = true
                   no-error.
        if not available tqCreditorForCInvoiceDefault
        then next.
        
        /* ci posting */
        assign viRepeatingPostingId = 0. /* to make sure it stays 0 after the first run */
        empty temp-table tDefaultSafsCI.
        <M-34 run CreateCIPosting
           (input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
            input  tCInvoice.CInvoicePostingYear (iiPeriodYear), 
            input  tCInvoice.CInvoicePostingPeriod (iiPeriodPeriod), 
            input  tCInvoice.CInvoicePostingDate (itPostingDate), 
            input  tCInvoice.Creditor_ID (iiCreditorId), 
            input  tCInvoice.tcCreditorCode (icCreditorCode), 
            input  tCInvoice.CInvoiceType (icCInvoiceType), 
            input  tCInvoice.tcDivisionCode (icDivisionCode), 
            input  tCInvoice.tcJournalCode (icJournalCode), 
            input  tCInvoice.CInvoiceVoucher (iiVoucher), 
            input  tCInvoice.CInvoiceTaxPointDate (itTaxPointDate), 
            input  tCInvoice.CInvoiceReference (icReference), 
            input  tCInvoice.CInvoiceDescription (icDescription), 
            input  tCInvoice.tdCInvoiceOriginalTC (idInvoiceAmountTC), 
            input  tCInvoice.tdCInvoiceOriginalLC (idInvoiceAmountLC), 
            input  ? (idInvoiceAmountCC), 
            input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
            input  tCInvoice.CInvoiceExchangeRate (idExchangeRate), 
            input  tCInvoice.CInvoiceRateScale (idExchangeRateScale), 
            input  tCInvoice.CInvoiceCIText (icCIText), 
            input  '':U (icPostingType), 
            input  '':U (icGlCode), 
            input  tCInvoice.tcCostCentreCode (icCostCentreCode), 
            input  tCInvoice.tcProjectCode (icProjectCode), 
            input  false (ilOnlyControlAccount), 
            input  tCInvoice.tcShipToCountryCode (icShipToCountryCode), 
            input  tqCreditorForCInvoiceDefault.tcTxclTaxCls (icTxclTaxCls), 
            input  tqCreditorForCInvoiceDefault.tcTxuTaxUsage (icTxuTaxUsage), 
            input  ? (icTxenvTaxEnv), 
            input  tCInvoice.tcNormalPaymentConditionCode (icPaymentCondition), 
            input  tCInvoice.tlShipToAddressIsTaxInCity (ilIsTaxInCityFromShipTo), 
            input  '':U (icPoNbr), 
            input  tCInvoice.CInvoiceIsTaxable (ilIsTaxable), 
            input  tCInvoice.tcCostCentreCode (icControlCostCentreCode), 
            input  tCInvoice.tcProjectCode (icControlProjectCode), 
            input   tqCreditorForCInvoiceDefault.tiAddress_ID (iiShipFromAddressId), 
            input  tCInvoice.tiShipToAddressId (iiShipToAddressId), 
            input  tCInvoice.CInvoiceIsLogisticMatching (ilIsLogisticMatching), 
            input  true (ilGetDefaultSafs), 
            input  tCInvoice.tcBusinessRelationCode (icBusinessRelationCode), 
            input  tqCreditorForCInvoiceDefault.tiPrePayControlGLProfile_ID (iiPrePayControlGLProfileId), 
            input  tqCreditorForCInvoiceDefault.tiInvControlGLProfile_ID (iiInvControlGLProfileId), 
            input  tqCreditorForCInvoiceDefault.tiCnControlGLProfile_ID (iiCnControlGLProfileId), 
            input  tCInvoice.tc_Rowid (icCInvoiceRowid), 
            input  if tqCreditorForCInvoiceDefault.tlBusinessRelationIsInterco then tqCreditorForCInvoiceDefault.tcBusinessRelationICCode else '':U (icBusinessRelationInterCoCode), 
            input  tCInvoice.tcTransactionStartCountryCode (icShipFromCountryCode), 
            input  tCInvoice.tlShipFromCountryIsEUCountry (ilShipFromCountryIsEUCountry), 
            input  tqCreditorForCInvoiceDefault.tcTxzTaxZone (icCreditorTaxZone), 
            input  tCInvoice.CInvoiceIsTaxExcluded (ilCInvoiceIsTaxExcluded), 
            input  tCInvoice.CInvoiceIsInitialStatus (ilIsInitialStatus), 
            input-output tCInvoiceVat (tApiCInvoiceVat), 
            input  tDefaultSafsCI (tDefaultSafsCI), 
            input  yes (ilHandleCommunicationWithJE), 
            input-output iiBJournalEntryId (biBJournalEntryId), 
            input-output viRepeatingPostingID (biPostingId), 
            input  tCInvoice.CInvoiceCCRate (idExchangeRateCC), 
            input  tCInvoice.CInvoiceCCScale (idExchangeRateScaleCC), 
            input  tCInvoice.CInvoiceDate (itInvoiceDate), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viLocalReturnStatus < 0
        then next.  
    end. 

    <Q-9 run ReasonByIdCode (all) (Read) (NoCache)
          (input tAllocateCInvoice.tiReasonId, (ReasonId)
           input if tAllocateCInvoice.tiReasonId = 0 then tAllocateCInvoice.tcReasonCode else '':U, (ReasonCode)
           output dataset tqReasonByIdCode) in BReason >
    find first tqReasonByIdCode where
        (if tAllocateCInvoice.tiReasonId <> 0
         then tqReasonByIdCode.tiReason_ID  = tAllocateCInvoice.tiReasonId
         else tqReasonByIdCode.tcReasonCode = tAllocateCInvoice.tcReasonCode)
         no-lock no-error.
    if not available tqReasonByIdCode
    then do:
        assign vcMessage           = trim(substitute(#T-28'Invoice status code (&1) is not defined in the system.':200(1110)T-28#, if tAllocateCInvoice.tcReasonCode <> "":U then trim(tAllocateCInvoice.tcReasonCode) else string(tAllocateCInvoice.tiReasonId) )) + chr(10) + 
                                     trim(substitute(#T-29'Entity: &1 / Supplier Invoice: &2 / GL Calendar Year: &3 / Daybook: &4 / Pending Invoice: &5':255(1106)T-29#,
                                                     trim(string(tAllocateCInvoice.tiCICompanyId)), trim(string(tAllocateCInvoice.tiCInvoiceId)), trim(string(tAllocateCInvoice.tiPeriodYear)), trim(tAllocateCInvoice.tcJournalCode), trim(string(tAllocateCInvoice.tiCInvoiceVoucher)) ))
               viLocalReturnStatus = -1.
        <M-10 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-2105':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        next.
    end.
    if tqReasonByIdCode.tcReasonAllocationStatus <> {&ALLOCSTATUS-ALLOC}
    then do:
        assign vcMessage           = trim(substitute(#T-30'Invoice status code (&1) is not Allocated.':200(1183)T-30#, trim(tqReasonByIdCode.tcReasonCode) )) + chr(10) + 
                                     trim(substitute(#T-31'Entity: &1 / Supplier Invoice: &2 / GL Calendar Year: &3 / Daybook: &4 / Pending Invoice: &5':255(1106)T-31#,
                                                     trim(string(tAllocateCInvoice.tiCICompanyId)), trim(string(tAllocateCInvoice.tiCInvoiceId)), trim(string(tAllocateCInvoice.tiPeriodYear)), trim(tAllocateCInvoice.tcJournalCode), trim(string(tAllocateCInvoice.tiCInvoiceVoucher)) ))
               viLocalReturnStatus = -1.
        <M-11 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-2106':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        next.
    end.
    
    /* Update the invoice */
    assign tCInvoice.tc_Status                  = (if tCInvoice.tc_Status = "":U  then "C":U  else tCInvoice.tc_Status)
           tCInvoice.CInvoiceIsLogisticMatching = tqReasonByIdCode.tlReasonIsLogisticMatching
           tCInvoice.CInvoiceCirecClosingDate   = tAllocateCInvoice.ttPostingDate
           tCInvoice.CInvoiceAllocationStatus   = {&ALLOCSTATUS-ALLOC}
           tCInvoice.CInvoiceIsInvoiceApproved  = tqReasonByIdCode.tlReasonIsInvoiceApproved
           tCInvoice.CInvoiceIsLockPayment      = tqReasonByIdCode.tlReasonIsLockPayment
           tCInvoice.CInvoiceIsInitialStatus    = tqReasonByIdCode.tlReasonIsInitialStatus
           tCInvoice.tcReasonCode               = tqReasonByIdCode.tcReasonCode
           tCInvoice.tcReasonAllocationStatus   = tqReasonByIdCode.tcReasonAllocationStatus.
    
    /* In case the caller of this method provides a value for the HoldAmountTC we will use this to update the field in CI */
    /* Either the balance, either the input value is used to update the hold-amoutn: this depends upon vlPutWholeBalanceOnHold: flag set in the beginning of this method */
    /* The hold-amount should be postive for Invoices and creditnotes and negative for the other ones */
    if tAllocateCInvoice.tdCInvoiceHoldAmountTC <> 0 
    then assign tCInvoice.CInvoiceHoldAmountTC = 
            if tCInvoice.CInvoiceBalanceTC = 0
            then 0
            else if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
                    tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE}
                 then if vlPutWholeBalanceOnHold = true
                      then absolute(tCInvoice.CInvoiceBalanceTC)
                      else absolute(tAllocateCInvoice.tdCInvoiceHoldAmountTC)
                 else if vlPutWholeBalanceOnHold = true
                      then absolute(tCInvoice.CInvoiceBalanceTC) * -1
                      else absolute(tAllocateCInvoice.tdCInvoiceHoldAmountTC) * -1.
    
    /* Make cinvoiceposting */
    <M-13 run UpdateCInvoicePosting (input  tCInvoice.tc_Rowid (icCInvoiceRowId), 
                                 input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
                                 input  tAllocateCInvoice.tiPostingId (iiPostingId), 
                                 input  tAllocateCInvoice.tiCirecPostingLineId (iiCIRECPostingLineId), 
                                 input  {&INVOICEPOSTINGTYPE-ALLOCATION} (icCInvoicePostingType), 
                                 output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0 and viLocalReturnStatus >= 0
    then assign viLocalReturnStatus = viFcReturnSuper.
    if viLocalReturnStatus < 0
    then next.             
end.


assign oiReturnStatus = viLocalReturnStatus.