project QadFinancials > class BCInvoice > method CreateMFPosting

Description

Create MF Posting


Parameters


iiCInvoiceIdinputintegerCreditor invoice ID
iiPeriodYearinputintegerAccounting Year
iiPeriodPeriodinputintegeraccounting period
itPostingDateinputdateposting date
iiCreditorIdinputintegerCreditor ID
icCreditorCodeinputcharacterSupplier Code
icCInvoiceTypeinputcharacterType of the creditor invoice
icDivisionCodeinputcharacterSub-Account Code
icJournalCodeinputcharacterDaybook Code
iiVoucherinputintegerVoucher
idInvoiceAmountTCinputdecimalInvoice Amount in TC
icCurrencyCodeinputcharacterTransactial Currency Code
icMFTextinputcharacterMatching Text
icCostCentreCodeinputcharacter
icProjectCodeinputcharacter
icDescriptioninputcharacterDescription of the creditor invoice
idExchangeRateinputdecimalexchange rate
idExchangeRateScaleinputdecimalExchange Rate Scale
ilIsLogisticMatchinginputlogicalIs Logistic Matching ?
icBusinessRelationCodeinputcharacter
icBusinessRelationICCodeinputcharacter
icCInvoiceRowidinputcharacter
icCInvoiceReferenceinputcharacter
ilGetDefaultSafsinputlogical
tAccountinputtemp-tableTemp table with all account lines of the MF Posting and there value
tApiCInvoiceVatinputtemp-tabletemp table like cinvoice vat
tDefaultSafsCIinputtemp-table
ilHandleCommunicationWithJEinputlogicaldo you want that this method handles the communication with the journal entry component or not ? Pass false, if you have already started the instance of the jouranl entry
ilCInvoiceIsTaxExcludedinputlogical
itCInvoiceDateinputdate
idCCExchangeRateinputdecimal
idCCExchangeRateScaleinputdecimal
biPostingIdinput-outputintegerID of the MF Posting
ocCInvoiceAllocationStatusoutputcharacterAllocation Status of Creditor Invoice
oiBJournalEntryIdoutputintegerInstance of JournalEntry
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ApiStdMaintainTTWithIntPost
method BCInvoice.DefaultValuesGeneralInfo


program code (program3/bcinvoice.p)

assign oiReturnStatus = -98
       viLocalReturn  = 0.

MAIN_BLOCK:
do on error undo, leave MAIN_BLOCK:

    if vlCompanyPropertyIsAPUseInvDate = true
    then assign vtExchangeRateDate = itCInvoiceDate.
    else assign vtExchangeRateDate = ?. 

    /* Only default MF if CI Posting is on CIREC account */
    if not vlCIREC
    then do:
        assign ocCInvoiceAllocationStatus = {&ALLOCSTATUS-NOALLOC}.
        leave MAIN_BLOCK.
    end. /* end not vlCIREC */
    
    /* Get CInvoice record */
    find tCInvoice where
         tCInvoice.tc_Rowid = icCInvoiceRowid
         no-error.
    if not available tCInvoice
    then do:
        assign vcMessage = #T-91'Cannot found a Supplier invoice data.':255(368790169)T-91#
               vcContext = "icCInvoiceRowid=&1"
               vcContext = substitute(vcContext, icCInvoiceRowid).
        <M-56 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  'tCInvoice.tc_rowid':U (icFieldName), 
            input  icCInvoiceRowid (icFieldValue), 
            input  'S':U (icType), 
            input  3 (iiSeverity), 
            input  icCInvoiceRowid (icRowid), 
            input  'qadfin-293833':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        assign viLocalReturn = -1.
        leave MAIN_BLOCK. 
    end.

    /* Actions on the Statutory Currency Rate fields */ 
    if idCCExchangeRate = 0 
    then assign idCCExchangeRate = ?.
    if idCCExchangeRateScale = 0
    then assign idCCExchangeRateScale = ?.
    if viCompanyCCId <> 0 and 
       viCompanyCCId <> ? and 
       (idCCExchangeRate      = ? or 
        idCCExchangeRateScale = ?)
    then do:
        if icCurrencyCode = vcCompanyCC
        then assign idCCExchangeRate      = 1
                    idCCExchangeRateScale = 1. 
        if vlDomainIsStatutory = false
        then assign idCCExchangeRate      = idExchangeRate 
                    idCCExchangeRateScale = idExchangeRateScale.
        else do :
            <M-9 run GetExRateByEntityInvoiceFlag
               (input  itCInvoiceDate (itCInvoiceDate), 
                input  itPostingDate (itCInvoicePostingDate), 
                input  ? (iiFromCurrencyId), 
                input  icCurrencyCode (icFromCurrencyCode), 
                input  viCompanyCCId (iiToCurrencyId), 
                input  vcCompanyCC (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeId), 
                input  {&EXCHANGERATETYPE-STATUTORY} (icExchangeRateTypeCode), 
                output idCCExchangeRate (odCInvoiceExchangeRate), 
                output idCCExchangeRateScale (odCInvoiceRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper < 0 or 
               (viFcReturnSuper > 0 and 
                oiReturnStatus  = 0) 
            then assign oiReturnStatus = viFcReturnSuper.
    		if viFcReturnSuper < 0 
            then leave MAIN_BLOCK.
       end. /* Not if vlDomainIsStatutory = false */
    end. /* if viCompanyCCId <> 0 and  */
         
    /* Start and/or Open BJournalEntry if necessary */
    if ilHandleCommunicationWithJE = true
    then do:
        if viBJournalEntryCIId = 0 or
           viBJournalEntryCIId = ?
        then do:
            <I-4 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION = "true"
                 &CLASS              = "BJournalEntry"}>
        
            assign vlBJEIsStartedFromCI = true.
        end. /* end viBJournalEntryCIID */
        else do:
            <I-5 {bFcOpenInstance
                 &CLASS           = "BJournalEntry"}>
        end. /* end else viBJournalEntryCIID */
    end. /* end ilHandleCommunicationWithJE */
    
	/* if the tax excluded is checked on, then we have to calculated total invoice *
	 * amount. This total invoice amount is used by CreateCIPosting + all          *
	 * sub-methods                                                                 */
	if ilCInvoiceIsTaxExcluded = true
	then do:
		assign vdDummy = ?.
		<M-91 run CreatePostingCalculateTotInvAmount
		   (input  iiCInvoiceId (iiCInvoiceId), 
			input  icCurrencyCode (icCurrencyCode), 
			input  ilCInvoiceIsTaxExcluded (ilCInvoiceIsTaxExcluded), 
			input  tApiCInvoiceVat (tApiCInvoiceVat), 
			input  icCInvoiceRowid (icCInvoiceRowid), 
			input  icCInvoiceType (icCInvoiceType), 
			input  idExchangeRate (idExchangeRate), 
			input  idExchangeRateScale (idExchangeRateScale), 
			input-output idInvoiceAmountTC (bdInvoiceAmountTC), 
			input-output vdDummy (bdInvoiceAmountLC), 
			input-output vdDummy (bdInvoiceAmountCC), 
			output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
		if viFcReturnSuper < 0 or viFcReturnSuper > 0 and oiReturnStatus = 0 then assign oiReturnStatus = viFcReturnSuper.
		if viFcReturnSuper < 0 then return.
	end.

    /* Calculate CIREC amounts */
    for each tApiCInvoiceVat:
        if not ilIsLogisticMatching or
           not tApiCInvoiceVat.CInvoiceVatIsAccrRcpUs
        then do:
            accumulate tApiCInvoiceVat.CInvoiceVatVatDebitTC (total).
            accumulate tApiCInvoiceVat.CInvoiceVatVatCreditTC (total).
            accumulate tApiCInvoiceVat.CInvoiceVatNonRecTaxAmtTC (total).
        end. /* end not ilIsLogisticMatching or ... */
    end. /* end for each tApiCInvoiceVat */
    
    assign vlTax           = (not ilIsLogisticMatching or
                             can-find(first tApiCInvoiceVat where
                                            tApiCInvoiceVat.CInvoiceVatIsAccrRcpUs = false)) and
                             can-find(first tApiCInvoiceVat where
                                            tApiCInvoiceVat.CInvoiceVatIsAccrRcpUs = false)
           vdCIRECDebitTC  = if icCInvoiceType = {&INVOICETYPE-INVOICE} or
                                icCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                             then if vlTax
                                  then idInvoiceAmountTC - ((accum total tApiCInvoiceVat.CInvoiceVatVatDebitTC) - (accum total tApiCInvoiceVat.CInvoiceVatNonRecTaxAmtTC)) + (accum total tApiCInvoiceVat.CInvoiceVatVatCreditTC)
                                  else idInvoiceAmountTC
                             else 0
           vdCIRECCreditTC = if icCInvoiceType = {&INVOICETYPE-INVOICE} or
                                icCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                             then 0
                             else if vlTax
                                  then idInvoiceAmountTC - ((accum total tApiCInvoiceVat.CInvoiceVatVatCreditTC) - (accum total tApiCInvoiceVat.CInvoiceVatNonRecTaxAmtTC)) + (accum total tApiCInvoiceVat.CInvoiceVatVatDebitTC)
                                  else idInvoiceAmountTC.
    
    if icCurrencyCode = vcCompanyLC
    then assign vdCIRECDebitLC  = vdCIRECDebitTC
                vdCIRECCreditLC = vdCIRECCreditTC.
    else assign vdCIRECDebitLC  = <M-35 RoundAmount
                                     (input  vdCIRECDebitTC  * idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                      input  viCompanyLCId (iiCurrencyID), 
                                      input  vcCompanyLC (icCurrencyCode)) in business>
                vdCIRECCreditLC = <M-51 RoundAmount
                                     (input  vdCIRECCreditTC * idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
                                      input  viCompanyLCId (iiCurrencyID), 
                                      input  vcCompanyLC (icCurrencyCode)) in business>.
                                      
    if icCurrencyCode = vcCompanyCC
    then assign vdCIRECDebitCC  = vdCIRECDebitTC
                vdCIRECCreditCC = vdCIRECCreditTC.
    else if idCCExchangeRate      = ? or 
            idCCExchangeRate      = 0 or 
            idCCExchangeRateScale = ? or 
            idCCExchangeRateScale = 0
         then assign vdCIRECDebitCC  = ?
                     vdCIRECCreditCC = ?.
         else assign vdCIRECDebitCC  = <M-24 RoundAmount
                                          (input  vdCIRECDebitTC  * idCCExchangeRate * idCCExchangeRateScale (idUnroundedAmount), 
                                           input  viCompanyCCId (iiCurrencyID), 
                                           input  vcCompanyCC (icCurrencyCode)) in BCInvoice>
                     vdCIRECCreditCC = <M-58 RoundAmount
                                          (input  vdCIRECCreditTC * idCCExchangeRate * idCCExchangeRateScale (idUnroundedAmount), 
                                           input  viCompanyCCId (iiCurrencyID), 
                                           input  vcCompanyCC (icCurrencyCode)) in BCInvoice>.
    
    /* Start the posting */
    if biPostingId = 0
    then do: 
        /* ==================================================================================================== */
        /* Creation of the new posting                                                                          */
        /* ==================================================================================================== */

        /* Get Posting year and period */
        if iiPeriodYear = ? and iiPeriodPeriod = ?
        then do:
            /* get the default period and year based on today */
            assign vhFcComponent = ?.
            <M-1 run GetDefaultPeriodDate
               (input  ? (iiYearIn), 
                input  ? (iiPeriodIn), 
                input  itPostingDate (itDateIn), 
                input  true (ilOpenForPurchase), 
                input  ? (ilOpenForSales), 
                input  ? (ilOpenForInventory), 
                output iiPeriodYear (oiYear), 
                output iiPeriodPeriod (oiPeriod), 
                output vtDummy (otPostingDate), 
                output viFcReturnSuper (oiReturnStatus)) in BPeriod>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave MAIN_BLOCK.
        end. /* end iiPeriodYear = ? ... */
    
        if iiPeriodYear = 0 or iiPeriodPeriod = 0
        then do:
             
             <Q-33 run PeriodByStartEndDate (all) (Read) (NoCache)
                (input tCInvoice.Company_ID, (CompanyId)
                 input itPostingDate, (Date)
                 output dataset tqPeriodByStartEndDate) in BPeriod>
                 
             find first tqPeriodByStartEndDate
             where (tcPeriodStatus                = {&PERIODSTATUS-OPEN} and
                   (tlPeriodIsPostingGLAllowed    = no                    or
                    tlPeriodIsPostingPurchAllow   = no ))                 or
                    tcPeriodStatus                = {&PERIODSTATUS-LOCKED} no-error.
             if available tqPeriodByStartEndDate 
             then do:
                 if tlPeriodIsPostingGLAllowed  = yes and
                    tlPeriodIsPostingPurchAllow =  no
                 then do:
                     assign iiPeriodYear   = year(itPostingDate)
                            iiPeriodPeriod = month(itPostingDate).
                 end.
                 else do:
                     assign iiPeriodYear   = year(today)
                            iiPeriodPeriod = month(today)
                            itPostingDate  = today.
                 end. /* else */
        
             end. /*  if available tqPeriodByStartEndDate */ 
             else do:
             
                 empty temp-table tqPeriodByStartEndDate.
             
                 <Q-49 run PeriodByStartEndDate (all) (Read) (NoCache)
                    (input tCInvoice.Company_ID, (CompanyId)
                     input today, (Date)
                     output dataset tqPeriodByStartEndDate) in BPeriod>
                     
                 find first tqPeriodByStartEndDate where 
                            tqPeriodByStartEndDate.tiCompany_ID        = tCInvoice.Company_ID and
                            tqPeriodByStartEndDate.ttPeriodStartDate  <= today                and
                            tqPeriodByStartEndDate.ttPeriodEndDate    >= today  no-error.
                 if available tqPeriodByStartEndDate
                 then do:
                     assign iiPeriodYear   = tqPeriodByStartEndDate.tiPeriodYear
                            iiPeriodPeriod = tqPeriodByStartEndDate.tiPeriodPeriod
                            itPostingDate  = today.
                 end. /*if available tqPeriodByStartEndDate*/    
             end. /*else do:*/
        end. /* if iiPeriodYear = 0 or iiPeriodPeriod = 0 */    
    
        assign vcPostingInvoiceReferenceText = string(iiPeriodYear, "9999":U)                   + "/":U +
                                               string(iiPeriodPeriod, "99":U)                   + " ":U +
                                               tCInvoice.tcJournalCode                          + " ":U +
                                               string(tCInvoice.CInvoiceVoucher, "999999999":U) + " ":U +
                                               trim(icCInvoiceReference).
    
        /* Create posting header */
        <M-73 run AddPostingHeader
           (input  tCInvoice.Company_ID (iiCompanyId), 
            input  iiPeriodYear (iiPeriodYear), 
            input  iiPeriodPeriod (iiPeriodPeriod), 
            input  icJournalCode (icJournalCode), 
            input  '' (icReportingJournalCode), 
            input  iiVoucher (iiVoucher), 
            input  itPostingDate (itPostingDate), 
            input  ? (itValueDate), 
            input  icMFText (icPostingText), 
            input  icBusinessRelationCode (icPostingBusinessRelationText), 
            input  vcPostingInvoiceReferenceText (icPostingInvoiceReferenceText), 
            input  icDescription (icPostingParentText), 
            input  ? (iiBPeriodId), 
            input  icCreditorCode (icPostingOriginAddressCode), 
            input  string(tCInvoice.CInvoicePostingYear, '9999':U) + '/':U + trim(tCInvoice.tcJournalCode) + string(tCInvoice.CInvoiceVoucher,'999999999':U) (icPostingOriginDocument), 
            input  ? (icPostingOriginDocumentType), 
            input  '':U (icBatchNumber), 
            input  ? (icBankImpLineRef), 
            output biPostingId (oiPostingId), 
            output vcRowId (ocRowid), 
            output viVoucher (oiPostingVoucher), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave MAIN_BLOCK.
        
        /* Get SAF Defaults for the Creditor */
        if ilGetDefaultSafs
        then do:
            empty temp-table tDefaultSafsCI.
        
            <Q-95 run CreditorSafDefaultForCIDef (all) (Read) (NoCache)
               (input iiCreditorId, (CreditorId)
                output dataset tqCreditorSafDefaultForCIDef) in BCreditor >
        
            for each tqCreditorSafDefaultForCIDef where
                     tqCreditorSafDefaultForCIDef.tiCreditor_ID = iiCreditorId:

                create tDefaultSafsCI.
                assign tDefaultSafsCI.tcSafCode        = tqCreditorSafDefaultForCIDef.tcSafCode
                       tDefaultSafsCI.tcSafConceptCode = tqCreditorSafDefaultForCIDef.tcSafConceptCode.
            end. /* end for each tqCreditorSafDefaultForCIDef */
        end. /* end ilGetDefaultSafs */    
        
        /* Get SIREC account */
        <Q-72 run GLByGLSystemType (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input {&GLSYSTEMTYPE-CIREC}, (GLSystemTypeCode)
            input {&GLTYPECODE-SYST}, (GlTypeCode)
            input ?, (GlIsDivisionAccount)
            output dataset tqGLByGLSystemType) in BGL >
        
        find first tqGLByGLSystemType no-error.
        if not available tqGLByGLSystemType
        then do:
            assign vcMessage = #T-62'The system could not find an Unmatched Invoices account record.':255(632180569)T-62#.
            <M-98 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-682001':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end. /* end not available tqGLByGLSystemType */ 

        /* Create posting on SIREC account                                            */
        /* Credit get debit amounts because it must be the opposite of the ci posting */
        <M-48 run AddStandardPosting
           (input  vcRowId (icPostingtcRowid), 
            input  tqGLByGLSystemType.tcGLCode (icGLCode), 
            input  icDivisionCode (icDivisionCode), 
            input  icCostCentreCode (icCostCentreCode), 
            input  icMFText (icCostCentreText), 
            input  icProjectCode (icProjectCode), 
            input  icMFText (icProjectText), 
            input  icBusinessRelationICCode (icIntercoBusinessRelationCode), 
            input  icCurrencyCode (icCurrencyCode), 
            input  vdCIRECCreditTC (idDebitTC), 
            input  vdCIRECCreditLC (idDebitLC), 
            input  vdCIRECCreditCC (idDebitCC), 
            input  0 (idDebitPC), 
            input  vdCIRECDebitTC (idCreditTC), 
            input  vdCIRECDebitLC (idCreditLC), 
            input  vdCIRECDebitCC (idCreditCC), 
            input  0 (idCreditPC), 
            input  0 (idQty), 
            input  icMFText (icLineText), 
            input  icMFText (icSafText), 
            input  tDefaultSafsCI (tDefaultSafs), 
            input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
            input  idExchangeRate (idExchangeRate), 
            input  idExchangeRateScale (idExchangeRateScale), 
            input  idCCExchangeRate (idPostingLineCCRate), 
            input  idCCExchangeRateScale (idPostingLineCCScale), 
            output viCIRECPostingLineId (oiPostingLineId), 
            input  ? (iiSafStructureId), 
            input  ? (icSafStructureCode), 
            input  ? (icAllocationKey), 
            input  false (ilLinkedCrCyDaemonReqExists), 
            input  vtExchangeRateDate (itExchangeRateDate), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave MAIN_BLOCK.
        
        /* ================================================= */
        /* Create posting on the Supplier's purchase account */
        /* ================================================= */
        PURCH_ACC_BLOCK:
        do on error undo, leave MAIN_BLOCK:
            <Q-7 run SupplierForSIMFPosting (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input iiCreditorId, (CreditorId)
                output dataset tqSupplierForSIMFPosting) in BCreditor >
        
            find first tqSupplierForSIMFPosting where
                       tqSupplierForSIMFPosting.tiCreditor_ID = iiCreditorId
                       no-error.
            if not available tqSupplierForSIMFPosting              or
               tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID = ? or
               tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID = 0
            then do:
                assign vcMessage = #T-80'Cannot find Supplier definition or there is missing definition of the Suppliers Purchase Account Profile (Supplier = &1).':255(589323332)T-80#
                       vcMessage = substitute(vcMessage, icCreditorCode)
                       vcContext = "available tqSupplierForSIMFPosting=&1|tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID=&2":U
                       vcContext = substitute(vcContext, available tqSupplierForSIMFPosting, if available tqSupplierForSIMFPosting then string(tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID) else "N/A")
                       vcContext = replace(vcContext, "|", chr(2)).
                <M-54 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tCInvoice.tcCreditorCode':U (icFieldName), 
                    input  tCInvoice.tcCreditorCode (icFieldValue), 
                    input  'W':U (icType), 
                    input  3 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'qadfin-537750':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  vcContext (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                assign viLocalReturn = if viLocalReturn = 0 then 1 else viLocalReturn.
                leave PURCH_ACC_BLOCK.
            end. 
        
            <Q-60 run ProfileLinkByGL (all) (Read) (NoCache)
               (input tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID, (GlProfileId)
                input viCompanyId, (CompanyId)
                output dataset tqProfileLinkByGL) in BProfile >
            find first tqProfileLinkByGL no-error.
            if not available tqProfileLinkByGL
            then do:
                assign vcMessage = #T-329'The system cannot find a supplier purchase account record.':255(65558)T-329#
                       vcContext = "icCreditorCode=&1|tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID=&2"
                       vcContext = substitute(vcContext, icCreditorCode, tqSupplierForSIMFPosting.tiPurchaseGLProfile_ID)
                       vcContext = replace(vcContext, "|", chr(2)).
                <M-83 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tCInvoice.tcCreditorCode':U (icFieldName), 
                    input  tCInvoice.tcCreditorCode (icFieldValue), 
                    input  'W':U (icType), 
                    input  3 (iiSeverity), 
                    input  tCInvoice.tc_Rowid (icRowid), 
                    input  'qadfin-335984':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  vcContext (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                assign viLocalReturn = if viLocalReturn = 0 then 1 else viLocalReturn.
                leave PURCH_ACC_BLOCK. 
            end. /* if not available tqProfileLinkByGL */
            else do:
                 /*Gets cost center of Purchase account*/
                <Q-96 run GetCostCentreFromProfile (all) (Read) (NoCache)
                   (input viCompanyId, (CompanyId)
                    input tqProfileLinkByGL.tiCostCentreProfile_ID, (CostCentreProfileId)
                    output dataset tqCostCentreFromProfile) in BProfile>
        
                find first tqCostCentreFromProfile where 
                    tqCostCentreFromProfile.tiProfile_ID = tqProfileLinkByGL.tiCostCentreProfile_ID
                    no-error.
                if available tqCostCentreFromProfile
                then assign vcCostCenterCodeMF = tqCostCentreFromProfile.tcCostCentreCode.
                else assign vcCostCenterCodeMF = "":U.

                /*Gets Project code of Purchase account */
                <Q-94 run GetProjectFromProfile (all) (Read) (NoCache)
                   (input viCompanyId, (CompanyId)
                    input tqProfileLinkByGL.tiProjectProfile_ID, (ProjectProfileID)
                    output dataset tqProjectFromProfile) in BProfile>

                find first tqProjectFromProfile where 
                    tqProjectFromProfile.tiProfile_ID = tqProfileLinkByGL.tiProjectProfile_ID
                    no-error.
                if available tqProjectFromProfile
                then assign vcProjectCodeMF = tqProjectFromProfile.tcProjectCode.
                else assign vcProjectCodeMF = "":U.
                
            end. /* else available tqProfileLinkByGL */
            <M-86 run AddStandardPosting
               (input  vcRowId (icPostingtcRowid), 
                input  tqProfileLinkByGL.tcGLCode (icGLCode), 
                input  icDivisionCode (icDivisionCode), 
                input  vcCostCenterCodeMF (icCostCentreCode), 
                input  icMFText (icCostCentreText), 
                input  vcProjectCodeMF (icProjectCode), 
                input  icMFText (icProjectText), 
                input  icBusinessRelationICCode (icIntercoBusinessRelationCode), 
                input  icCurrencyCode (icCurrencyCode), 
                input  vdCIRECDebitTC (idDebitTC), 
                input  vdCIRECDebitLC (idDebitLC), 
                input  vdCIRECDebitCC (idDebitCC), 
                input  0 (idDebitPC), 
                input  vdCIRECCreditTC (idCreditTC), 
                input  vdCIRECCreditLC (idCreditLC), 
                input  vdCIRECCreditCC (idCreditCC), 
                input  0 (idCreditPC), 
                input  0 (idQty), 
                input  icMFText (icLineText), 
                input  icMFText (icSafText), 
                input  tDefaultSafsCI (tDefaultSafs), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                input  idExchangeRate (idExchangeRate), 
                input  idExchangeRateScale (idExchangeRateScale), 
                input  idCCExchangeRate (idPostingLineCCRate), 
                input  idCCExchangeRateScale (idPostingLineCCScale), 
                output viDummy (oiPostingLineId), 
                input  ? (iiSafStructureId), 
                input  ? (icSafStructureCode), 
                input  ? (icAllocationKey), 
                input  false (ilLinkedCrCyDaemonReqExists), 
                input  vtExchangeRateDate (itExchangeRateDate), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave MAIN_BLOCK.
        end. /* PURCH_ACC_BLOCK: */
    
        /* ================================================ */
        /* Update CInvoicePosting record                    */
        /* ================================================ */
        <M-730 run UpdateCInvoicePosting
           (input  tCInvoice.tc_Rowid (icCInvoiceRowId), 
            input  iiCInvoiceId (iiCInvoiceId), 
            input  biPostingId (iiPostingId), 
            input  viCIRECPostingLineId (iiCIRECPostingLineId), 
            input  {&INVOICEPOSTINGTYPE-ALLOCATION} (icCInvoicePostingType), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave MAIN_BLOCK.
            
        /* ================================================ */
        /* Update Supplier invoice                          */
        /* ================================================ */
        assign tCInvoice.CInvoiceCirecClosingDate = itPostingDate.
    end. /* end biPostingId = 0 */
    else do:
        /* ==================================================================================================== */
        /* Update of already created MF posting                                                                 */
        /* ==================================================================================================== */
        find first tCInvoicePosting where
                   tCInvoicePosting.tc_ParentRowid      = tCInvoice.tc_Rowid and
                   tCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-ALLOCATION}
                   no-error.
        if not available tCInvoicePosting
        then do:
             assign vcMessage = #T-39'The system cannot find a CInvoicePosting record.':255(928371405)T-39#
                   vcContext = "tCInvoice.tc_Rowid=&1"
                   vcContext = substitute(vcContext, tCInvoice.tc_Rowid).
            <M-19 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'tCInvoice.tc_rowid':U (icFieldName), 
                input  tCInvoice.tc_Rowid (icFieldValue), 
                input  'S':U (icType), 
                input  3 (iiSeverity), 
                input  tCInvoice.tc_Rowid (icRowid), 
                input  'qadfin-714476':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            assign viLocalReturn = -1.
            leave MAIN_BLOCK.
        end. 
				
        /* Update amounts of the CIREC posting line */        
        <M-93 run UpdatePostingLine
           (input  ? (icPostingLineRowId), 
            input  tCInvoicePosting.CIRECPostingLine_ID (iiPostingLineId), 
            input  ? (icNewGLCode), 
            input  ? (iiNewGLId), 
            input  false (ilIsUpdateGLAccount), 
            input  ? (icNewDivisionCode), 
            input  false (ilIsUpdateDivision), 
            input  ? (icNewCostCentreCode), 
            input  false (ilIsUpdateCostCentre), 
            input  ? (icNewProjectCode), 
            input  false (ilIsUpdateProject), 
            input  vdCIRECCreditTC (idDebitTC), 
            input  vdCIRECCreditCC (idDebitCC), 
            input  vdCIRECCreditLC (idDebitLC), 
            input  vdCIRECDebitTC (idCreditTC), 
            input  vdCIRECDebitCC (idCreditCC), 
            input  vdCIRECDebitLC (idCreditLC), 
            input  true (ilIsUpdateAmount), 
            input  ? (icBusinessRelationIntercoCode), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave MAIN_BLOCK.
        
        /* If there are just two posting lines, change amount also for the second posting line so the posting is balanced */
        <M-23 run GetPostingLineDataForPosting
           (input  ? (icPostingTcRowid), 
            input  tCInvoicePosting.Posting_ID (iiPostingID), 
            output tMFPostingLines (tPostingLineMinimalData), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
        if viLocalReturn   < 0 then leave MAIN_BLOCK.
        
        assign vcPurchaseGLPostingLineRowId = ?
               viCount                      = 0.
        for each tMFPostingLines:
            assign viCount = viCount + 1.
            if tMFPostingLines.tiPostingLineID <> tCInvoicePosting.CIRECPostingLine_ID
            then assign vcPurchaseGLPostingLineRowId = tMFPostingLines.tcPostingLineTcRowid.
        end.
        
        if viCount = 2
        then do:
            /* Update amounts of the Purchase posting line */
            <M-18 run UpdatePostingLine
               (input  vcPurchaseGLPostingLineRowId (icPostingLineRowId), 
                input  ? (iiPostingLineId), 
                input  ? (icNewGLCode), 
                input  ? (iiNewGLId), 
                input  false (ilIsUpdateGLAccount), 
                input  ? (icNewDivisionCode), 
                input  false (ilIsUpdateDivision), 
                input  ? (icNewCostCentreCode), 
                input  false (ilIsUpdateCostCentre), 
                input  ? (icNewProjectCode), 
                input  false (ilIsUpdateProject), 
                input  vdCIRECDebitTC (idDebitTC), 
                input  vdCIRECDebitCC (idDebitCC), 
                input  vdCIRECDebitLC (idDebitLC), 
                input  vdCIRECCreditTC (idCreditTC), 
                input  vdCIRECCreditCC (idCreditCC), 
                input  vdCIRECCreditLC (idCreditLC), 
                input  true (ilIsUpdateAmount), 
                input  ? (icBusinessRelationIntercoCode), 
                output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn   < 0 then leave MAIN_BLOCK.
        end. /* if viCount = 2 */
    end. /* end else biPostingId = 0 */
    
    /* ================================================ */
    /* Update allocation status                         */
    /* ================================================ */
    <Q-84 run JournalForJEDefault (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input icJournalCode, (JournalCode)
        input ?, (JournalControl)
        input ?, (JournalTypeCode)
        output dataset tqJournalForJEDefault) in BJournal >
    
    find first tqJournalForJEDefault where
               tqJournalForJEDefault.tcJournalCode = icJournalCode
               no-error.
    
    assign ocCInvoiceAllocationStatus = if not available tqJournalForJEDefault
                                        then {&ALLOCSTATUS-NOALLOC}
                                        else if tqJournalForJEDefault.tcLayerTypeCode = {&LAYERTYPECODE-TRANSIENT}
                                             then {&ALLOCSTATUS-TRANSALLOC}
                                             else if tqJournalForJEDefault.tcLayerTypeCode = {&LAYERTYPECODE-OFFICIAL}
                                                  then {&ALLOCSTATUS-ALLOC}
                                                  else {&ALLOCSTATUS-NOALLOC}.
end. /* MAIN_BLOCK */    

/* ================================================================================ */
/* Stop communication with the opened components                                    */
/* ================================================================================ */

if ilHandleCommunicationWithJE = true and
   valid-handle(vhBJournalEntryCIInst)
then do:
    <I-32 {bFcCloseInstance
         &CLASS           = "BJournalEntry"}>
end. /* end ilHandleCommunicationWithJE */

/* ================================================================================ */
/* Assign remaining output parameters                                               */
/* ================================================================================ */
assign oiBJournalEntryId = viBJournalEntryCIID
       oiReturnStatus    = viLocalReturn.