project QadFinancials > class BCInvoiceAPMatching > method ApiEDICInvoiceAPMatchingImport

Description

Process all incomming data from EDI and create Supplier invoice(s) and Receiver matching(s) based on these data.
Processing of the records is done in separate transactions (1 Supplier invoice + 1 Receiver matching one by one). If any supplier invoice or receiver matching cannot be created, return error message points to that incomming EDI record causing this error (tFcMessages.tcFcRowid = tEDICInvoice.tc_Rowid). In case of any error, the supplier invoice and the receiver matching are not created. This means there can be incomming records which are succesfully processed (Supplier invoice and Receiver matching created) and records, which failed (no supplier invoice nor reciver matching are created).
This method expects dataset handle, which is populated with data comming from EDI.

Precondition: Getting handle to dataset where all data from EDI are stored in (BCInvoiceAPMatching.APIEDICInvoiceAPMatchingDSHandle)

Postcondition: If any error is not raised during processing of one record in dCInvoiceAPMatching.tEDICInovice, supplier invoice and corresponding receiver matchings are created.

See method ApiEDICInvoiceAPMatchingImportInitialDefaulting for all further details


Parameters


izCInvoiceAPMatchinginputdataset-handleHandle to dataset where EDI stores all data. These data are processed and supplier invoices and matchings are created.

Structure of dataset
Table: tEDICInvoice - this table contains all details needed for creation of supplier invoices
tcAddressName Not used - for compatibility only
tcAddressStreet1 Not used - for compatibility only
tcAddressStreet2 Not used - for compatibility only
tcAddressStreet3 Not used - for compatibility only
tcAddressCity Not used - for compatibility only
tcAddressStateCode Not used - for compatibility only
tcAddressZip Not used - for compatibility only
tcAddressFormat Not used - for compatibility only
tcAddressCountryCode Not used - for compatibility only
tcAddressCounty Not used - for compatibility only
ttCInvoicePostingDate Posting date, if not supplied value is taken from tcCInvoiceDate (CInvoice.CInvoicePostingdate)
tcCInvoiceCurrencyCode Supplier invoice currency. If not supplied, values is taken from purchase order (CInvoice.Currency_ID)
tcCInvoiceOwnBankNumber Own bank number from which the payment will go out
tcDInvoiceNumber Invoice number (CInvoice.CInvoiceReference)
ttCInvoiceDate Supplier invoice date (CInvoice.CInvoiceDate)
tcCInvoicePaymentCondCode Normal payment condition (CInvoicePaymentCondCode)
tcCInvoiceDiscountDueDate Due date for application of discount (CInvoiceDiscountDueDate)
ttCInvoiceDueDate Payment due date (CInvoiceDueDate)
tcCInvoiceControlGLCode Not used - for compatibility only
tcCInvoiceCostCentreCode Cost centre (CInvoiceCostCentreCode)
tcCInvoiceDiscountGLCode Not used - for compatibility only
tcCInvoiceDiscountCostCentre Not used - for compatibility only
tcCompanyCode Company where supplier invoice is created (CInvoice.Company_ID)
tcCInvoiceRemark Not used - for compatibility only
tcCInvoiceBankNumber Supplier's bank number (CInvoiceBankNumber)
tcCInvoiceCheckForm Not used - for compatibility only
tcCInvoiceIsHoldAmount Does invoice contain any hold amount (CInvoiceIsHoldAmount)
tcCInvoiceHoldAmountTC Supplier invoice hold amount (CInvoiceHoldAmountTC)
tcCInvoiceIsStatusFinal Create supplier invoice in final status (it means including all financial updates)?
tcCInvoiceDivisionCode Division (CInvoiceDivisionCode)
tcCInvoiceDiscountDivision Not used - for compatibility only
tc_Rowid Unique identification of record (used for relationship with tEDIAPMatching)
tiPurchaseOrderNumber Purchase order number (CInvoicePO)
tcReceiverNumber Receiver number
tlIsCreateCInvoice Create supplier invoices?
tcCInvoiceReference Not used - for compatibility only
tcCreditorCode Supplier code (CInvoice.Creditor_ID)
tcRemittanceAddressName Not used - for compatibility only
tcSite If company is not supplied, this field and domain are used to get correct entity, where suppliler invoices should be created.
tcDomainCode Domain code. Complementary field to tcSite


Table: tEDIAPMatching - this table contains details for creation of matching linked to supplier invoice
tiPurchaseOrderLineNumber Purchase order line number (APMatchingLn.APMatchingLnPvodOrderLnNbr)
tdMatchUnitPrice Unit price of purchase order line (APMatchingLn.APMatchingLnMatchPrice)
tdMatchQty Matched quantity (APMatchingLn.APMatchingLnMatchQty)
tc_Rowid Unique record identification
tc_ParentRowid Relation filed for linking with parent record tEDICInvoices
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceAPMatching.DataLoadByInput


program code (program3/bcinvoiceapmatching.p)

/* =========================================================================================================== */
/* Method      : ApiEDICInvoiceAPMatchingImport                                                                */
/* Desc        : This method is primarly designed to create  supplier invoices and receiver matchings from EDI */
/* ----------------------------------------------------------------------------------------------------------- */
/* Params:  (I)  izCInvoiceAPMatching Handle to dataset containing data comming from EDI related to creation   */
/*                                    of new Supplier Invoices and Receiver matchings in financial application */
/* =========================================================================================================== */

assign oiReturnStatus = -98
       viLocalReturn  = 0
       viRowid        = -1.

/* ============ */
/* Validations  */
/* ============ */
if izCInvoiceAPMatching = ? or
   not valid-handle(izCInvoiceAPMatching)
then do:
    assign vcMessage = #T-26'Integration dataset comming as input parameter is not correctly initiated.':255(71106)T-26#
           vcContext = 'izCInvoiceAPMatching=&1':U
           vcContext = substitute(vcContext, izCInvoiceAPMatching).
    <M-22 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'S':U (icType), 
        input  '':U (iiSeverity), 
        input  '':U (icRowid), 
        input  'QadFin-8614':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  vcContext (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
    assign oiReturnStatus = -1.
    return.
end. /* if izCInvoiceAPMatching = ? or */

/* ================================================================================================== */
/* Call method that can extend the tEDI-tables with some defaults                                     */
/* Note the dataset-handle is an input-output parameter so the submethod can chnage the content of it */
/* ================================================================================================== */
<M-36 run ApiEDICInvoiceAPMatchingImportInitialDefaulting
   (input-output izCInvoiceAPMatching (bzCInvoiceAPMatching), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
if viFcReturnSuper <> 0 
then assign viLocalReturn = viFcReturnSuper.
if viFcReturnSuper < 0
then do : 
    assign oiReturnStatus = viLocalReturn.
    Return.
end. /* if viFcReturnSuper < 0 */

/* =================================================================================================== */
/* Copy data to static temporary tables                                                                */
/* =================================================================================================== */
if dataset dCInvoiceAPMatching:handle <> izCInvoiceAPMatching
then dataset dCInvoiceAPMatching:handle:copy-dataset(izCInvoiceAPMatching).

/* =============== */
/* Initial actions */
/* =============== */
empty temp-table tCInvoiceRef.
empty temp-table tCInvoicePORef.
empty temp-table tAPMatchingLnRef.
empty temp-table tAPMatchingCostRef.
empty temp-table tAPMatchingCostSafRef.

/* ====================================== */
/* Is there set-up for invoice correction */
/* ====================================== */
assign vlGLCorrAP = false.
<Q-85 run GLCorrectionControlByDomain (all) (Read) (NoCache)
   (input vcDomainCode, (DomainCode)
    output dataset tqGLCorrectionControlByDomain) in BMFGGLCorrectionControl >
find first tqGLCorrectionControlByDomain where
           tqGLCorrectionControlByDomain.tcglcor_domain = vcDomainCode 
           no-error.
if available tqGLCorrectionControlByDomain
then assign vlGLCorrAP = tqGLCorrectionControlByDomain.tlglcor_ap.

/* ============================================================== */
/* Go though incomming dataset and convert to internal structure  */
/* ============================================================== */
for each tEDICInvoice:
    
    /* Skip those invoices which should not be created */
    if tEDICInvoice.tlIsCreateCInvoice <> true
    then next.
    
    /* Normalize input parameters */
    if tEDICInvoice.tcDomainCode = '':U then assign tEDICInvoice.tcDomainCode = ?.
    if tEDICInvoice.tcPurchaseOrderNumber = '':U then assign tEDICInvoice.tcPurchaseOrderNumber = ?.
    if tEDICInvoice.tcReceiverNumber      = '':U then assign tEDICInvoice.tcReceiverNumber      = ?.

    /* Initialise total per invoice */
    assign vdTotalAmountInvoice = 0.

    /* Calculate entity where the invoice should be created in */
    if tEDICInvoice.tcCompanyCode = ? or
       tEDICInvoice.tcCompanyCode = '':U
    then do:
        <Q-27 run SiteForCompany (all) (Read) (NoCache)
           (input tEDICInvoice.tcSite, (Site)
            input tEDICInvoice.tcDomainCode, (Domain)
            input ?, (CompanyId)
            output dataset tqSiteForCompany) in BMfgSite >

        find first tqSiteForCompany where
                   tqSiteForCompany.tcsi_domain = tEDICInvoice.tcDomainCode and
                   tqSiteForCompany.tcsi_site   = tEDICInvoice.tcSite
            no-error.
        if available tqSiteForCompany
        then assign tEDICInvoice.tcCompanyCode = tqSiteForCompany.tcCompanyCode
                    viResolvedCompanyId        = tqSiteForCompany.tiCompany_ID.
        else do:
            assign vcMessage = #T-33'System cannot determine Entity based on supplied domain and site (Domain = &1, Site = &2).':255(71130)T-33#
                   vcMessage = substitute(vcMessage, tEDICInvoice.tcDomainCode, tEDICInvoice.tcSite)
                   vcContext = "Domain=&1|Site=&2":U
                   vcContext = substitute(vcContext, tEDICInvoice.tcDomainCode, tEDICInvoice.tcSite)
                   vcContext = replace(vcContext, "|":U, chr(2)).
            <M-32 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tEDICInvoice.tc_Rowid (icRowid), 
                input  'QadFin-8635':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            assign viLocalReturn = -1.
            next.
        end.
    end.
    else do:
        <Q-95 run CompanyPrim (all) (Read) (NoCache)
           (input ?, (LookupCompanyId)
            input tEDICInvoice.tcCompanyCode, (CompanyCode)
            output dataset tqCompanyPrim) in BCompany>
        find tqCompanyPrim where
             tqCompanyPrim.tcCompanyCode = tEDICInvoice.tcCompanyCode
             no-error.
        if not available tqCompanyPrim
        then do:
            assign vcMessage = #T-13'System cannot find the entity based on the enity code that was passed (&1).':255(362572341)T-13#
                   vcMessage = substitute(vcMessage, tEDICInvoice.tcCompanyCode).
            <M-63 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  'tEDICInvoice.tcCompanyCode':U (icFieldName), 
                input  tEDICInvoice.tcCompanyCode (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tEDICInvoice.tc_Rowid (icRowid), 
                input  'qadfin-616627':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            assign viLocalReturn = -1.
            next.
        end. /* if not available tqCompanyPrim */
        
        assign viResolvedCompanyId = tqCompanyPrim.tiCompany_ID.
    end. /* else do: */
    
    /* Get Bank number */
    assign viBankNumberID = ?.
    if tEDICInvoice.tcCInvoiceOwnBankNumber <> ?    and
       tEDICInvoice.tcCInvoiceOwnBankNumber <> "":U and
       tEDICInvoice.tcCInvoiceBankNumber    <> ?    and
       tEDICInvoice.tcCInvoiceBankNumber    <> "":U
    then do:
        <Q-6 run BankNumberByPayFormat (all) (Read) (NoCache)
           (input tEDICInvoice.tcCInvoiceBankNumber, (BankNumber)
            input tEDICInvoice.tcCreditorCode, (CreditorCode)
            input tEDICInvoice.tcCInvoiceOwnBankNumber, (OwnBankNumber)
            input ?, (PaymentFormat)
            input viCompanyId, (CompanyId)
            output dataset tqBankNumberByPayFormat) in BBankNumber >
        find first tqBankNumberByPayFormat where
                   tqBankNumberByPayFormat.tcBankNumber          = tEDICInvoice.tcCInvoiceBankNumber    and
                   tqBankNumberByPayFormat.tcOwnBankNumber       = tEDICInvoice.tcCInvoiceOwnBankNumber and
                   tqBankNumberByPayFormat.tcCreditorCode        = tEDICInvoice.tcCreditorCode          and
                   tqBankNumberByPayFormat.tlBankNumberIsDefault = true
                   no-error.
        if not available tqBankNumberByPayFormat
        then find  tqBankNumberByPayFormat where
                   tqBankNumberByPayFormat.tcBankNumber          = tEDICInvoice.tcCInvoiceBankNumber    and
                   tqBankNumberByPayFormat.tcOwnBankNumber       = tEDICInvoice.tcCInvoiceOwnBankNumber and
                   tqBankNumberByPayFormat.tcCreditorCode        = tEDICInvoice.tcCreditorCode
                   no-error.

        if available tqBankNumberByPayFormat
        then assign viBankNumberID = tqBankNumberByPayFormat.tiBankNumber_ID.
        else do:
            assign vcMessage = #T-25'System cannot determine bank number and payment format for supplier invoice creation.':255(71103)T-25#
                   vcContext = "BankNumber=&1|OwnBankNumber=&2|CreditorCode=&3":U
                   vcContext = substitute(vcContext, tEDICInvoice.tcCInvoiceBankNumber, tEDICInvoice.tcCInvoiceOwnBankNumber, tEDICInvoice.tcCreditorCode)
                   vcMessage = substitute("&1 (&2)":U, vcMessage, vcContext)
                   vcContext = replace(vcContext, "|":U, chr(2)).
            <M-24 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tEDICInvoice.tc_Rowid (icRowid), 
                input  'QadFin-8621':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            assign viLocalReturn = -1.
            next.
        end.
    end.
    
    /* Get default AP Matching daybook */
    <Q-87 run JournalByJournalLayer (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        input ?, (JournalCode)
        input {&JOURNALTYPE-CIREC}, (CandoJournalTypeCode)
        input ?, (LayerCode)
        input {&LAYERTYPECODE-OFFICIAL}, (CandoLayerTypeCode)
        input ?, (JournalID)
        input true, (JournalIsActive)
        output dataset tqJournalByJournalAndLayer) in BJournal>

    for first tqJournalByJournalAndLayer where
              tqJournalByJournalAndLayer.tcJournalTypeCode = {&JOURNALTYPE-CIREC}      and
              tqJournalByJournalAndLayer.tcLayerTypeCode   = {&LAYERTYPECODE-OFFICIAL} and
              tqJournalByJournalAndLayer.tlJournalIsActive = true:
        assign vcAPMatchingJournal = tqJournalByJournalAndLayer.tcJournalCode.
    end.

    /* Validate mandatory fields */
    if tEDICInvoice.tcCompanyCode    = ? or tEDICInvoice.tcCompanyCode       = "":U or
       tEDICInvoice.tcCreditorCode   = ? or tEDICInvoice.tcCreditorCode      = "":U or
       tEDICInvoice.ttCInvoiceDate   = ? or
       tEDICInvoice.tcDInvoiceNumber = ? or tEDICInvoice.tcDInvoiceNumber = "":U
    then do:
        assign vcMessage = #T-35'Not all manadatory values were provided by EDI in order to create supplier invoice.':255(71135)T-35#
               vcContext = "tEDICInvoice.tcCompanyCode=&1|tEDICInvoice.tcCreditorCode=&2|tEDICInvoice.ttCInvoiceDate=&3|tEDICInvoice.tcDInvoiceNumber=&4":U
               vcContext = substitute(vcContext, tEDICInvoice.tcCompanyCode, tEDICInvoice.tcCreditorCode, tEDICInvoice.ttCInvoiceDate, tEDICInvoice.tcDInvoiceNumber)
               vcMessage = substitute("&1 (&2)":U, vcMessage, vcContext)
               vcContext = replace(vcContext, "|":U, chr(2)).
        <M-34 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tEDICInvoice.tc_Rowid (icRowid), 
            input  'QadFin-8638':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
        assign viLocalReturn = -1.
        next.
    end.

    /* Create record for creation of supplier invoice */
    create tCInvoiceRef.
    assign tCInvoiceRef.tc_Rowid                      = string(viRowid)
           viRowid                                    = viRowid - 1
           tCInvoiceRef.tcCInvoiceCostCentreCode      = tEDICInvoice.tcCInvoiceCostCentreCode
           tCInvoiceRef.tcCInvoiceDescription         = tEDICInvoice.tcDInvoiceNumber
           tCInvoiceRef.tcCInvoiceDivisionCode        = tEDICInvoice.tcCInvoiceDivisionCode
           tCInvoiceRef.tcCInvoiceJournalCode         = vcJournal
           tCInvoiceRef.tcAPMatchingJournalCode       = vcAPMatchingJournal
           tCInvoiceRef.tcCInvoiceReference           = tEDICInvoice.tcDInvoiceNumber
           tCInvoiceRef.tcCInvoiceType                = {&INVOICETYPE-INVOICE}
           tCInvoiceRef.tcCompanyCode                 = tEDICInvoice.tcCompanyCode
           tCInvoiceRef.tcCreditorCode                = tEDICInvoice.tcCreditorCode
           tCInvoiceRef.tcCurrencyCode                = tEDICInvoice.tcCInvoiceCurrencyCode
           tCInvoiceRef.tcNormalPaymentConditionCode  = tEDICInvoice.tcCInvoicePaymentCondCode
           tCInvoiceRef.tcShipToBusinessRelationCode  = ?
           tCInvoiceRef.tdCInvoiceAmountTC            = 0
           tCInvoiceRef.tdExchangeRate                = ?
           tCInvoiceRef.tdRateScale                   = ?
           tCInvoiceRef.tlCInvoiceIsTaxable           = true
           tCInvoiceRef.tlIsInitialStatus             = (tEDICInvoice.tlCInvoiceIsStatusFinal <> true)
           tCInvoiceRef.ttCInvoiceDate                = tEDICInvoice.ttCInvoiceDate
           tCInvoiceRef.ttCInvoiceDiscountDueDate     = tEDICInvoice.ttCInvoiceDiscountDueDate
           tCInvoiceRef.ttCInvoiceDueDate             = tEDICInvoice.ttCInvoiceDueDate
           tCInvoiceRef.ttPostingDate                 = (if tEDICInvoice.ttCInvoicePostingDate = ?
                                                         then tEDICInvoice.ttCInvoiceDate
                                                         else tEDICInvoice.ttCInvoicePostingDate)
           tCInvoiceRef.ttTaxPointDate                = tCInvoiceRef.ttPostingDate 
           tCInvoiceRef.tiBankNumberId                = viBankNumberId
           tCInvoiceRef.tdCInvoiceHoldAmount          = (if tEDICInvoice.tlCInvoiceIsHoldAmount = true and
                                                            tEDICInvoice.tdCInvoiceHoldAmountTC <> ?   and
                                                            tEDICInvoice.tdCInvoiceHoldAmountTC <> 0
                                                         then tEDICInvoice.tdCInvoiceHoldAmountTC
                                                         else 0)
           tCInvoiceRef.tlIsInitialStatus             = (tEDICInvoice.tlCInvoiceIsStatusFinal <> true)           
           tCInvoiceRef.tiCInvoiceRegistrationNr      = tEDICInvoice.tiCInvoiceRegistrationNr 
           tCInvoiceRef.tiCInvoicePostingYear         = tEDICInvoice.tiCInvoicePostingYear
           tCInvoiceRef.tcJournalCode                 = tEDICInvoice.tcJournalCode
           tCInvoiceRef.tiCInvoiceVoucher             = tEDICInvoice.tiCInvoiceVoucher
           tCInvoiceRef.tcCInvoiceTSMNumber           = tEDICInvoice.tcCInvoiceTSMNumber
           tCInvoiceRef.tlAPMatchingIsStatusFinal     = (if (tEDICInvoice.tiCInvoiceRegistrationNr <> ?  and
                                                             tEDICInvoice.tiCInvoiceRegistrationNr <> 0) or
                                                            (tEDICInvoice.tcJournalCode            <> ? and
                                                             tEDICInvoice.tcJournalCode            <> '')
                                                         then (if tEDICInvoice.tlAPMatchingIsStatusFinal = true
                                                               then true
                                                               else false)
                                                         else (if tEDICInvoice.tlCInvoiceIsStatusFinal = true
                                                               then true
                                                               else false)).                                                                       
    
    /* In case it concerns an existing invoice (so either the registration number , either the yr+voucher+jrnl is passed) then we */
    /* assign here tCInvoiceRef.tiCInvoice_ID and tCInvoiceRef.tlIsInitialStatus based upon the CInvoice-record in the            */
    if (tEDICInvoice.tiCInvoiceRegistrationNr <> ? and tEDICInvoice.tiCInvoiceRegistrationNr <> 0 ) or
       (tEDICInvoice.tcJournalCode            <> ? and tEDICInvoice.tcJournalCode            <> '') or
       (tEDICInvoice.tiCInvoicePostingYear    <> ? and tEDICInvoice.tiCInvoicePostingYear    <> 0 ) or
       (tEDICInvoice.tiCInvoiceVoucher        <> ? and tEDICInvoice.tiCInvoiceVoucher        <> 0 )     
    then do:
        <Q-65 run CInvoiceForAPMValidation (all) (Read) (NoCache)
           (input viResolvedCompanyId, (CompanyId)
            input tEDICInvoice.tiCInvoicePostingYear, (Year)
            input tEDICInvoice.tcJournalCode, (JournalCode)
            input tEDICInvoice.tiCInvoiceVoucher, (Voucher)
            input tEDICInvoice.tiCInvoiceRegistrationNr, (CInvoiceRegistrationNr)
            output dataset tqCInvoiceForAPMValidation) in BCInvoice>
        find tqCInvoiceForAPMValidation where
             tqCInvoiceForAPMValidation.tiCompany_ID             = viResolvedCompanyId                and
             tqCInvoiceForAPMValidation.tiCInvoicePostingYear    = tEDICInvoice.tiCInvoicePostingYear and
             tqCInvoiceForAPMValidation.tcJournalCode            = tEDICInvoice.tcJournalCode         and
             tqCInvoiceForAPMValidation.tiCInvoiceVoucher        = tEDICInvoice.tiCInvoiceVoucher
             no-error.
        if not available tqCInvoiceForAPMValidation
        then do:
            find tqCInvoiceForAPMValidation where
                 tqCInvoiceForAPMValidation.tiCompany_ID             = viResolvedCompanyId                and
                 tqCInvoiceForAPMValidation.tiCInvoiceRegistrationNr = tEDICInvoice.tiCInvoiceRegistrationNr
                 no-error.
            if not available tqCInvoiceForAPMValidation            
            then do:
                assign vcMessage = #T-59'Could not find the invoice in the system.':255(276829532)T-59# + chr(10) +
                                   substitute(#T-2'Entity Code: &1':255(495923248)T-2#, tEDICInvoice.tcCompanyCode) + chr(10) +
                                   substitute(#T-45'Year/Daybook/Voucher: &1/&2/&3':255(494810828)T-45#, string(tEDICInvoice.tiCInvoicePostingYear), tEDICInvoice.tcJournalCode, string(tEDICInvoice.tiCInvoiceVoucher)) + chr(10) +
                                   substitute(#T-90'Supplier Invoice Registration Number: &1':255(614099364)T-90#, string(tEDICInvoice.tiCInvoiceRegistrationNr)).
                <M-83 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  'tEDICInvoice.tiCInvoiceRegistrationNr':U (icFieldName), 
                    input  string(tEDICInvoice.tiCInvoiceRegistrationNr) (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  tEDICInvoice.tc_Rowid (icRowid), 
                    input  'qadfin-922244':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
                assign viLocalReturn = -1.
                delete tCInvoiceRef.
                next.        
            end. /* if not available tqCInvoiceForAPMValidation */
        end. /* if not available tqCInvoiceForAPMValidation */
        assign tCInvoiceRef.tiCInvoice_ID     = tqCInvoiceForAPMValidation.tiCInvoice_ID
               tCInvoiceRef.tlIsInitialStatus = tqCInvoiceForAPMValidation.tlCInvoiceIsInitialStatus.
    end. /* if (tEDICInvoice.tiCInvoiceRegistrationNr <> ? and tEDICInvoice.tiCInvoiceRegistrationNr <> '') or */

    if tEDICInvoice.tcPurchaseOrderNumber <>  ? and
       tEDICInvoice.tcPurchaseOrderNumber <> "":U
    then do:
        create tCInvoicePORef.
        assign tCInvoicePORef.CInvoicePOPoDomain = tEDICInvoice.tcDomainCode
               tCInvoicePORef.CInvoicePOPoNbr    = tEDICInvoice.tcPurchaseOrderNumber
               tCInvoicePORef.tc_ParentRowid     = tCInvoiceRef.tc_Rowid
               tCInvoicePORef.tc_Rowid           = string(viRowid)
               viRowid                           = viRowid - 1.
        
        /* if currency of the supplier invoices was not defined, take it from PO */
        if tCInvoiceRef.tcCurrencyCode = "":U or
           tCInvoiceRef.tcCurrencyCode = ?
        then do:
            <Q-40 run PurchaseOrderByDomainNbrVend (all) (Read) (NoCache)
               (input tEDICInvoice.tcDomainCode, (DomainCode)
                input tEDICInvoice.tcPurchaseOrderNumber, (PONbr)
                input ?, (POVendor)
                input ?, (POCurrency)
                input ?, (POType)
                output dataset tqPurchaseOrderByDomainNbrVend) in BMfgPO >
            find tqPurchaseOrderByDomainNbrVend where
                 tqPurchaseOrderByDomainNbrVend.tcpo_domain = tEDICInvoice.tcDomainCode and
                 tqPurchaseOrderByDomainNbrVend.tcpo_nbr    = tEDICInvoice.tcPurchaseOrderNumber
                 no-error.
            if available tqPurchaseOrderByDomainNbrVend
            then assign tCInvoiceRef.tcCurrencyCode = tqPurchaseOrderByDomainNbrVend.tcpo_curr.
        end.
    end.
    
    /* ============================================================================================= */
    /* Additional Posting Lines                                                                      */
    /* ============================================================================================= */
    for each tEDIAPMatchingCost where
             tEDIAPMatchingCost.tc_ParentRowid = tEDICInvoice.tc_Rowid:
        create tAPMatchingCostRef.
        buffer-copy tEDIAPMatchingCost to tAPMatchingCostRef
        assign tAPMatchingCostRef.tc_ParentRowid = tCInvoiceRef.tc_Rowid
               tAPMatchingCostRef.tc_Rowid       = string(viRowid)
               viRowid                           = viRowid - 1.
        for each tEDIAPMatchingCostSaf where
                 tEDIAPMatchingCostSaf.tc_ParentRowid = tEDIAPMatchingCost.tc_Rowid:
            create tAPMatchingCostSafRef.
            buffer-copy tEDIAPMatchingCostSaf to tAPMatchingCostSafRef
            assign tAPMatchingCostSafRef.tc_ParentRowid = tAPMatchingCostRef.tc_Rowid
                   tAPMatchingCostSafRef.tc_Rowid       = string(viRowid)
                   viRowid                              = viRowid - 1.
        end. /* for each tEDIAPMatchingCostSaf where */
    end. /* for each tEDIAPMatchingCost where */
        
    /* ============================================================================================= */
    /* Matching of non-logistic charges                                                              */
    /* ============================================================================================= */
    <M-79 run ApiEDICInvoiceAPMatchingImportNoLC
       (input-output vdTotalAmountInvoice (bdTotalAmountInvoice), 
        input-output viRowid (biRowId), 
		output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viLocalReturn < 0 then next.
	
    /* ============================================================================================= */
    /* Matching of logistic charges                                                                  */
    /* ============================================================================================= */
    <M-96 run ApiEDICInvoiceAPMatchingImportLC
       (input-output vdTotalAmountInvoice (bdTotalAmountInvoice), 
        input-output viRowid (biRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
    if viLocalReturn < 0 then next.

    if available tCInvoiceRef
    then do:
        /* ================================================ */
        /* Assign the correct invoice type for the invoice: */
        /* Positive: Invoice                                */
        /* Negative & AP flag: Invoice Correction           */
        /* Negative & No AP flag: Credit Note               */
        /* ================================================ */
        if vdTotalAmountInvoice >= 0
        then assign tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICE}
                    vcCInvoiceType              = {&INVOICETYPE-INVOICE-TR}.
        else do:
            if vlGLCorrAP = true
            then assign tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                        vcCInvoiceType              = {&INVOICETYPE-INVOICECORRECTION-TR}.
            else assign tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}
                        vcCInvoiceType              = {&INVOICETYPE-CREDITNOTE-TR}.
        end. /* else do: */
    
        /* ==================================================================== */
        /* Determine daybook to be used for creation of supplier inovices       */
        /* ==================================================================== */
        assign vcJournal = ?.

        /* Get Daybook set and Site for Supplier */
        <Q-67 run PurchaseOrderByDomainNbrVend (all) (Read) (NoCache)
           (input tEDICInvoice.tcDomainCode, (DomainCode)
            input tEDICInvoice.tcPurchaseOrderNumber, (PONbr)
            input ?, (POVendor)
            input ?, (POCurrency)
            input ?, (POType)
            output dataset tqPurchaseOrderByDomainNbrVend) in BMfgPO>
        find tqPurchaseOrderByDomainNbrVend where
             tqPurchaseOrderByDomainNbrVend.tcpo_domain = tEDICInvoice.tcDomainCode and
             tqPurchaseOrderByDomainNbrVend.tcpo_nbr    = tEDICInvoice.tcPurchaseOrderNumber
             no-error.
        if available tqPurchaseOrderByDomainNbrVend
        then do: 
            assign vcSupplierSite = tqPurchaseOrderByDomainNbrVend.tcpo_site
                   vcDaybookSet   = tqPurchaseOrderByDomainNbrVend.tcpo_daybookset.
        end.
               
        if vcDaybookSet   = "":U or
           vcDaybookSet   = ?
        then do:
            <Q-81 run SupplierForDaybookSetSite (all) (Read) (NoCache)
               (input vcDomainCode, (DomainCode)
                input tCInvoiceRef.tcCreditorCode, (SupplierCode)
                output dataset tqSupplierForDaybookSetSite) in BMfgSupplier>    
        
            find first tqSupplierForDaybookSetSite where 
                       tqSupplierForDaybookSetSite.tcvd_addr = tCInvoiceRef.tcCreditorCode no-error.
            if available tqSupplierForDaybookSetSite 
            then do:
                assign vcSupplierSite = tqSupplierForDaybookSetSite.tcvd_site
                       vcDaybookSet   = tqSupplierForDaybookSetSite.tcvd_daybookset.
                
                if vcDaybookSet = "":U or
                   vcDaybookSet = ? 
                then do:
                    /* Get default daybook from Domain control */
                    <Q-47 run POAccountingControlByDomain (all) (Read) (NoCache)
                       (input vcDomainCode, (Domain)
                        output dataset tqPOAccountingControlByDomain) in BMfgPOAccountingControl>
                    for first tqPOAccountingControlByDomain where 
                              tqPOAccountingControlByDomain.tcpoc_domain = vcDomainCode:
                        assign vcDaybookSet = tqPOAccountingControlByDomain.tcpoc_dflt_dybkset.
                    end.
                end. /* if tqSupplierForDaybookSetSite.tcvd_daybookset = "":U or */

            end. /* if available tqSupplierForDaybookSetSite */
        end. /* if vcDaybookSet = "":U or*/       
        
        /* Get the daybook for the supplier invoice */
        if vcDaybookSet <> "":U and
           vcDaybookSet <> ?
        then do:
            assign vhFcComponent = ?.
            <M-80 run ApiGetDaybookCodeByType
               (input  vcDaybookSet (icDaybookSetCode), 
                input  vcSupplierSite (icSite), 
                input  '2':U (icDaybookSetType), 
                input  tCInvoiceRef.tcCInvoiceType (icInvoiceType), 
                output vcJournal (ocDaybookCode), 
                output viFcReturnSuper (oiReturnStatus)) in BMfgDaybookSet>
            if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
            if viLocalReturn < 0 then next.
        end. /* if vcDaybookSet <> "":U and */

        /* Fall-back mechanism - as it was implemented originally */
        /* If there is daybook defined with EDI keyword in code, this daybook is used */
        if vcJournal = ?
        then do:       
            <Q-11 run JournalBeginsForJEDefault (all) (Read) (NoCache)
               (input viCompanyId, (CompanyId)
                input ?, (JournalCode)
                input ?, (JournalControl)
                input ?, (JournalTypeCode)
                output dataset tqJournalBeginsForJEDefault) in BJournal >
            
            for each  tqJournalBeginsForJEDefault where
                      tqJournalBeginsForJEDefault.tlJournalIsActive = true                           and
                      tqJournalBeginsForJEDefault.tcJournalControl  = {&JOURNALCONTROL-FINANCIAL}    and
                      ((tqJournalBeginsForJEDefault.tcJournalTypeCode = {&JOURNALTYPE-CREDITORINVOICE}        and tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICE})    or
                       (tqJournalBeginsForJEDefault.tcJournalTypeCode = {&JOURNALTYPE-CREDITORCREDITNOTE}     and tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE}) or
                       (tqJournalBeginsForJEDefault.tcJournalTypeCode = {&JOURNALTYPE-CREDITORINVOICECORRECT} and tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION})):
                if vcJournal = ?
                then assign vcJournal = tqJournalBeginsForJEDefault.tcJournalCode.
        
                if index ( tqJournalBeginsForJEDefault.tcJournalCode, "EDI":U) > 0
                then do:
                    assign vcJournal =  tqJournalBeginsForJEDefault.tcJournalCode.
                    leave.
                end.
            end.
        end. /* if vcJournal = ? */
        
        if vcJournal = ? or
           vcJournal = '':U
        then do:
            assign vcMessage = substitute(#T-74'System does not contain definition of any Daybook of type &1 which can be used for creation of Supplier invoice. If you create Daybook with code containing EDI world, this is pickuped before other daybooks.':255(148377800)T-74#, vcCInvoiceType).
            <M-82 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  tEDICInvoice.tc_Rowid (icRowid), 
                input  'qadfin-267193':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  vcContext (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
            assign viLocalReturn = -1.
            next.
        end.
        else assign tCInvoiceRef.tcCInvoiceJournalCode = vcJournal.   
    end. /* if available tCInvoiceRef */
end. /* for each tEDICInvoice: */

/* ================= */
/* Do real execution */
/* ================= */
<M-18 run CreateCInvoiceAPMatching
   (input-output tCInvoiceRef (tCInvoiceRef), 
    input  tCInvoicePORef (tCInvoicePORef), 
    input-output tAPMatchingLnRef (tAPMatchingLnRef), 
    input-output tAPMatchingCostRef (tAPMatchingCostRef), 
    input-output tAPMatchingCostSafRef (tAPMatchingCostSafRef), 
    input  false (ilIsAllInvoicesInOneTrans), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
if viFcReturnSuper < 0 or
   viFcReturnSuper > 0 and viLocalReturn = 0  then assign viLocalReturn = viFcReturnSuper.

/* ====== */
/* Return */
/* ====== */
assign oiReturnStatus = viLocalReturn.