project QadFinancials > class BCInvoiceJournalEntry > method ApiStdMaintainTTInitialDefaulting

Description

ApiStdMaintainTTInitialDefaulting: This method is called when creating an invoice through an API right before the processing of the input.
This method can be used to default field values to the incoming dataset prior to the acctual processing.

Standard-defaulting in this method:
Restriction:

• This is only executed in case there is a single CInvoicePosting record of type allocation and a single associated Posting record in the input.

Overview:
• Default Company_ID with the current company you are logged on to
• Default CInvoiceType with INVOICE
• Default CInvoicePostingDate with CInvoiceDate (and vice-versa so one of them is mandatory).
• Default CInvoiceTaxPointDate with CInvoicePostingDate
• Default PostingText, PostingInvoiceReferenceTxt and PostingParentText with CInvoiceReference
• Default Posting.Company_ID with the company of the invoice
• Default PostingDate, PostingYear and PostingPeriod with the corresponding fields of the invoice
• Default PostingValueDate and PostingSystemDate with CInvoicePostingDate.
• Default Invoice-status with the default invoice-status of the supplier
• Default following fields based upon the invoice-status: CInvoiceAllocationStatus, tcReasonAllocationStatus, CInvoiceIsInvoiceApproved, CInvoiceIsLockPayment, CInvoiceIsLogisticMatching and CInvoiceIsInitialStatus
• Default following fields based upon the invoice-type and the associated default profiles of the supplier; ControlGLCode, DivisionCode, CostCentreCode and ProjectCode
• Default PostingVat.PostingVatTaxPointDate with CInvoiceTaxPointDate.
• Default PostingVat.tcDomainCode with the domain of the current company
• Default PostingVat.tcVatInOut with INPUT
• Default PostingVat.PostingVatTaxTransType with 22
• Default PostingVat.PostingVatTransType with PURCHASE
• Default PostingVat.PostingVatBaseDebitTC and CInvoiceVat.CInvoiceVatVatBaseCreditTC with fields CInvoiceVatVatBaseDebitTC and CInvoiceVatVatBaseCreditTC of the PostingVat-record of the invoice that has the same value for tcVatCode
• Default PostingVat.PostingVatTaxDebitTC and CInvoiceVat.PostingVatTaxCreditTC with fields PostingLineDebitTC and PostingLineCreditTC of the parent-record in PostingLine
• Default PostingVatIsReverseCharge , PostingVatIsAbsRet and.PostingVatIsSuspDelwith false.
• Default CInvoiceSiteCode and CInvoiceDayBookSetCode based upon the supplier-defaults
• Default Daybook (on the invoice and on the associated SI-Posting) based upon CInvoiceSiteCode and CInvoiceDayBookSetCode and the daybooks defined in the application
• For the posting-line in the SI-Posting with PostingLineSequence = 1 (the one for the Control-account):
o Default tcGLCode in this posting-line with the Control-account of the supplier
o Default tcDivisionCode, tcCostCentreCode and tcProjectCode with the corresponding values on the invoice
• For the posting-line in the SI-Posting with PostingLineSequence = 2 (the one for the Sirec-account):
o Default tcGLCode in this posting-line with the Sirec
• For the MF-Posting specific, Default a complete MF-Posting when CInvoiceAllocationStatus is set to Allocation or TransientAllocation and and the invoice is not about logistic-matching and there is no CInvoicePosting-record with CInvoicePostingType=ALLOC
• For the MF-Posting specific;
o Take over following values from the SI-Posting: Company_ID, PostingText, PostingInvoiceReferenceTxt, PostingParentText, PostingDate, PostingYear, PostingPeriod, PostingSystemDate and PostingValueDate
o Default the JournalCode based upon CInvoiceAllocationStatus
o Default tcGLCode with the Sirec on the posting-line of the MF-Posting with PostingLineSequence = 1
o Default tcGLCode with the Purchase-account on the posting-line of the MF-Posting with PostingLineSequence = 2
• For the MF-Posting specific, Default following fields from the SI-Posting: Company_ID, PostingInvoiceReferenceTxt, PostingParentText, PostingDate, PostingYear, PostingPeriod, PostingSystemDate and PostingValueDate
• Default PostingLine.tcCurrencyCode with the currency of the invoice
• Default tcDivisionCode, tcCostCentreCode and tcProjectCode in the posting-lines based upon the defaults of the GL-account on the PostingLine
• Default following fields in PostingLine with their normal default-value in case they have the unknown-value: AllocationType, DivisionCode, CostCentreCode and ProjectCode
• Default following fields in Posting with their normal default-value in case they have the unknown-value: PostingVoucher, PostingIsReplacement, PostingIsReversing, PostingIsReversingBySign, PostingIsZeroValueAllowed, PostingIsSkipAutoAssignLC, PostingIsAutoReversal, PostingIsCorrection and PostingAutoReversalType


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceJournalEntry.ApiStdMaintainTTV01


program code (program9/bcinvoicejournalentry.p)

    
    /* ============================================================================================================= */
    /* Add an include in here so we can easily trace the methods that do direct db-access (or via dynamic querying)  */
    /* As in this method a lot of different tables are accessed we do not specify the complete list here in the call */
    /* ============================================================================================================= */
    <I-37 {READDIRECTDBACCESS
         &READTABLENAMES = "Plenty of Finacials and Operational tables"}>
         
    /* ============================================================================== */
    /* Notes:                                                                         */
    /*  0. In this method we will ony do the defaulting that applies to both the      */
    /*     Invoice and the Posting, the defaulting that only applies to  the Invoice  */
    /*     should be written in BCInvoice and the defaulting that only applies to the */
    /*     Posting should be written in BPosting. A sample is the JournalCode as that */
    /*     one is on the Invoice and on the Posting                                   */
    /*  1. No records are available at the moment this method is entered              */
    /*  2. The records that need to be extended are the t_sApixxxCIJE-tables          */
    /*  3. Exception handling is done via progress itself (on error undo throw)       */
    /*  4. Database is accessed directly and variables are defined manually to:       */
    /*        A. Come to a proper performance                                         */
    /*        B. Ease downgradability                                                 */
    /*        C. Ease installations on released products: This complete peice of code */
    /*           can be copied into the 'before' hook of the parent-method of this    */
    /*           method by using the customization-technology that we have in the Fin */
    /*  Addition: Note that only Financial-tables can be accessed directly - for      */ 
    /*            Operational tables we will have to use dynamic queries!             */ 
    /* ============================================================================== */
    
    
    /* We are naming these manual defined varaiable with an X to avoid interactions with the normaly defined method-data-items */
    define variable viXCreditorSharedSetID      as  integer no-undo. 
    define variable viXGLSharedSetID            as  integer no-undo. 
    define variable viXDivisionSharedSetID      as  integer no-undo. 
    define variable viXCostCentreSharedSetID    as  integer no-undo. 
    define variable viXProjectSharedSetID       as  integer no-undo. 
    define variable viXJournalSharedSetID       as  integer no-undo.
    define variable viXGLProfileID              as  integer no-undo.
    define variable viXDivisionProfileID        as  integer no-undo.
    define variable viXCostCentreProfileID      as  integer no-undo.
    define variable viXProjectProfileID         as  integer no-undo.
    define variable viXGLIsDivisionAccount      as  logical no-undo.
    define variable viXGLIsCostCentreAccount    as  logical no-undo.
    define variable viXGLIsProjectAccount       as  logical no-undo.    
    define variable vcXDomainCode               as  character no-undo.
    define variable vcXCirecGLCode              as  character no-undo.
    define variable vcXPurchaseGLCode           as  character no-undo.
    define variable vlXCreateMFPostingHere      as  logical no-undo.
    define variable vhXBufferPocCtrl            as  handle no-undo.
    define variable vhXQueryPocCtrl             as  handle no-undo.
    define variable vhXBufferDybsMstr           as  handle no-undo.
    define variable vhXQueryDybsMstr            as  handle no-undo.
    define variable vhXBufferVdMstr             as  handle no-undo.
    define variable vhXQueryVdMstr              as  handle no-undo.
    define buffer   bXtMFCInvoicePosting        for t_sApiCInvoicePostingCIJE.
    define buffer   bXtMFPosting                for t_sApiPostingCIJE.
    define buffer   bXtMFPostingLine            for t_sApiPostingLineCIJE.
    /* Initialise the object-handles for queries and buffers */
    CREATE BUFFER vhXBufferPocCtrl          FOR TABLE "poc_ctrl".
    CREATE QUERY vhXQueryPocCtrl.
    vhXQueryPocCtrl:Set-Buffers(vhXBufferPocCtrl).
    CREATE BUFFER vhXBufferDybsMstr         FOR TABLE "dybs_mstr".
    CREATE QUERY vhXQueryDybsMstr.
    vhXQueryDybsMstr:Set-Buffers(vhXBufferDybsMstr).
    CREATE BUFFER vhXBufferVdMstr           FOR TABLE "vd_mstr".
    CREATE QUERY vhXQueryVdMstr.
    vhXQueryVdMstr:Set-Buffers(vhXBufferVdMstr).
    
    
    /* ====================================================== */
    /* Go through all invoices as only new-mode is supported  */
    /* ====================================================== */
    for each t_sApiCInvoiceCIJE on error undo, throw :
        
        /* ========================================================================================================================== */
        /* Find the associated SI-Posting: Field 'tc_rowid' have to be the same on t_sApiPostingCIJE and on t_sApiCInvoicePostingCIJE */
        /* ========================================================================================================================== */
        find t_sApiCInvoicePostingCIJE where 
             t_sApiCInvoicePostingCIJE.tc_Parentrowid      = t_sApiCInvoiceCIJE.tc_rowid and 
             t_sApiCInvoicePostingCIJE.CInvoicePostingType = {&INVOICEPOSTINGTYPE-INITIAL} /* INIT */
             no-error. /* Do not use a 'first' option as there should only be one */
        if not available t_sApiCInvoicePostingCIJE
        then Return. /* It makes no sense to continue if the user has not provided a SI-Posting */
        find t_sApiPostingCIJE where 
             t_sApiPostingCIJE.tc_rowid = t_sApiCInvoicePostingCIJE.tc_rowid
             no-error. /* Do not use a 'first' option as there should only be one */
        If not available t_sApiPostingCIJE
        then Return. /* It makes no sense to continue if the user has not provided a SI-Posting */
        
        /* ================================================================================================= */
        /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
        /* ================================================================================================= */
        assign t_sApiPostingCIJE.PostingVoucher            = 0     when t_sApiPostingCIJE.PostingVoucher            = ?
               t_sApiPostingCIJE.PostingIsReplacement      = false when t_sApiPostingCIJE.PostingIsReplacement      = ?
               t_sApiPostingCIJE.PostingIsReversing        = false when t_sApiPostingCIJE.PostingIsReversing        = ?
               t_sApiPostingCIJE.PostingIsReversingBySign  = false when t_sApiPostingCIJE.PostingIsReversingBySign  = ?
               t_sApiPostingCIJE.PostingIsZeroValueAllowed = false when t_sApiPostingCIJE.PostingIsZeroValueAllowed = ?
               t_sApiPostingCIJE.PostingIsSkipAutoAssignLC = false when t_sApiPostingCIJE.PostingIsSkipAutoAssignLC = ?
               t_sApiPostingCIJE.PostingIsAutoReversal     = false when t_sApiPostingCIJE.PostingIsAutoReversal     = ?
               t_sApiPostingCIJE.PostingIsCorrection       = false when t_sApiPostingCIJE.PostingIsCorrection       = ?
               t_sApiPostingCIJE.PostingAutoReversalType   = ""    when t_sApiPostingCIJE.PostingAutoReversalType   = ?
               t_sApiPostingCIJE.tlSaveAsTemplate          = false when t_sApiPostingCIJE.tlSaveAsTemplate          = ?.
        
        /* ============================================== */
        /* Fill in default Company_ID when not specified  */
        /* Fill in default InvoiceType when not specified */
        /* Fill in PostingText when not specified         */
        /* ============================================== */
        if t_sApiCInvoiceCIJE.Company_ID = ? or
           t_sApiCInvoiceCIJE.Company_ID = 0
        then assign t_sApiCInvoiceCIJE.Company_ID = viCompanyID.
        assign t_sApiPostingCIJE.Company_ID = t_sApiCInvoiceCIJE.Company_ID.
        if t_sApiCInvoiceCIJE.CInvoiceType = ? or 
           t_sApiCInvoiceCIJE.CInvoiceType = "":U
        then assign t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-INVOICE}. /* INVOICE */ 
        if t_sApiPostingCIJE.PostingText = "":U or 
           t_sApiPostingCIJE.PostingText = ? 
        then assign t_sApiPostingCIJE.PostingText = t_sApiCInvoiceCIJE.CInvoiceReference.
        if t_sApiPostingCIJE.PostingInvoiceReferenceTxt = "":U or 
           t_sApiPostingCIJE.PostingInvoiceReferenceTxt = ?
        then assign t_sApiPostingCIJE.PostingInvoiceReferenceTxt = t_sApiCInvoiceCIJE.CInvoiceReference.
        if t_sApiPostingCIJE.PostingParentText = "":U or 
           t_sApiPostingCIJE.PostingParentText = ?
        then assign t_sApiPostingCIJE.PostingParentText = t_sApiCInvoiceCIJE.CInvoiceReference.
        
        /* =================================== */
        /* Get the DomainCode and SharedSetIDs */
        /* =================================== */
        For each Company where 
                 Company.Company_ID = t_sApiCInvoiceCIJE.Company_ID
                 no-lock ,
            first Domains of Company no-lock,                 
            each CompanySharedSet of Company no-lock
            on error undo, throw :
            Assign vcXDomainCode = Domains.DomainCode.
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-CREDITOR} /* CREDITOR */
                     no-lock 
                     on error undo, throw :
                assign viXCreditorSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-GL} /* GL */
                     no-lock 
                     on error undo, throw :
                assign viXGLSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-DIVISION} /* DIVISION */
                     no-lock 
                     on error undo, throw :
                assign viXDivisionSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-PROJECT} /* PROJECT */
                     no-lock 
                     on error undo, throw :
                assign viXProjectSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-COSTCENTRE} /* COSTCENTRE */
                     no-lock 
                     on error undo, throw :
                assign viXCostCentreSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
            For each SharedSet of CompanySharedSet where 
                     SharedSet.SharedSetTypeCode = {&SHAREDSETTYPE-JOURNAL} /* JOURNAL */
                     no-lock 
                     on error undo, throw :
                assign viXJournalSharedSetID = SharedSet.SharedSet_ID.
            end. /* For each SharedSet of CompanySharedSet where */
        end. /* For each Company where */                 
        if vcXDomainCode           = "":U or 
           vcXDomainCode           = ? or 
           viXCreditorSharedSetID  = 0 or 
           viXCreditorSharedSetID  = ? or 
           viXGLSharedSetID        = 0 or 
           viXGLSharedSetID        = ? or 
           viXDivisionSharedSetID  = 0 or 
           viXDivisionSharedSetID  = ? or 
           viXJournalSharedSetID   = 0 or 
           viXJournalSharedSetID   = ? 
        then Return. /* If this missing then further defaulting makes no sense */

        /* ================================================ */
        /* PostingDate-InvoiceDate + AccountingYearPeriod   */
        /* ================================================ */
        if t_sApiCInvoiceCIJE.CInvoicePostingDate  = ? and 
           t_sApiCInvoiceCIJE.CInvoiceDate        <> ? 
        then assign t_sApiCInvoiceCIJE.CInvoicePostingDate = t_sApiCInvoiceCIJE.CInvoiceDate.
        if t_sApiCInvoiceCIJE.CInvoiceDate         = ? and 
           t_sApiCInvoiceCIJE.CInvoicePostingDate <> ? 
        then assign t_sApiCInvoiceCIJE.CInvoiceDate = t_sApiCInvoiceCIJE.CInvoicePostingDate.
        if t_sApiCInvoiceCIJE.CInvoiceTaxPointDate = ? and 
           t_sApiCInvoiceCIJE.CInvoicePostingDate <> ? 
        then assign t_sApiCInvoiceCIJE.CInvoiceTaxPointDate = t_sApiCInvoiceCIJE.CInvoicePostingDate. 
        if t_sApiCInvoiceCIJE.CInvoicePostingDate <> ? and 
           (t_sApiCInvoiceCIJE.CInvoicePostingYear = 0 or 
            t_sApiCInvoiceCIJE.CInvoicePostingYear = ?) and 
           (t_sApiCInvoiceCIJE.CInvoicePostingPeriod = 0 or 
            t_sApiCInvoiceCIJE.CInvoicePostingPeriod = ?)
        then do :
            Release Period no-error.
            Find Period where 
                 Period.Company_ID                 = t_sApiCInvoiceCIJE.Company_ID          and 
                 Period.PeriodStartDate           <= t_sApiCInvoiceCIJE.CInvoicePostingDate and 
                 Period.PeriodEndDate             >= t_sApiCInvoiceCIJE.CInvoicePostingDate and 
                 Period.PeriodTypeCode             = {&PERIODTYPECODE-NORMAL}         and /* NORM */
                 Period.PeriodIsPostingGLAllowed   = true                             and
                 Period.PeriodIsPostingPurchAllow  = true 
                 no-lock no-error.
            if not available Period 
            then Find Period where 
                      Period.Company_ID                 = t_sApiCInvoiceCIJE.Company_ID          and 
                      Period.PeriodStartDate           <= t_sApiCInvoiceCIJE.CInvoicePostingDate and 
                      Period.PeriodEndDate             >= t_sApiCInvoiceCIJE.CInvoicePostingDate and 
                      Period.PeriodIsPostingGLAllowed   = true                             and
                      Period.PeriodIsPostingPurchAllow  = true 
                      no-lock no-error.
            if available Period
            then assign t_sApiCInvoiceCIJE.CInvoicePostingYear   = Period.PeriodYear
                        t_sApiCInvoiceCIJE.CInvoicePostingPeriod = Period.PeriodPeriod.
        end. /* if t_sApiCInvoiceCIJE.CInvoicePostingDate <> ? and */
        Assign t_sApiPostingCIJE.PostingDate   = t_sApiCInvoiceCIJE.CInvoicePostingDate
               t_sApiPostingCIJE.PostingYear   = t_sApiCInvoiceCIJE.CInvoicePostingYear
               t_sApiPostingCIJE.PostingPeriod = t_sApiCInvoiceCIJE.CInvoicePostingPeriod.
        if t_sApiPostingCIJE.PostingSystemDate = ? 
        then assign t_sApiPostingCIJE.PostingSystemDate = t_sApiCInvoiceCIJE.CInvoicePostingDate.
        if t_sApiPostingCIJE.PostingValueDate = ? 
        then assign t_sApiPostingCIJE.PostingValueDate = t_sApiCInvoiceCIJE.CInvoicePostingDate.               
        
        /* ================================================================ */
        /* Find the Creditor record                                         */
        /* Fill in Creditor_ID as we need that to default the bank-data etc */
        /* ================================================================ */
        Release Creditor no-error.
        if t_sApiCInvoiceCIJE.Creditor_ID = ? or 
           t_sApiCInvoiceCIJE.Creditor_ID = 0 
        then do:
            if t_sApiCInvoiceCIJE.tcCreditorCode <> "":U and 
               t_sApiCInvoiceCIJE.tcCreditorCode <> ?
            then do :
                find Creditor where 
                     Creditor.SharedSet_ID = viXCreditorSharedSetID and 
                     Creditor.CreditorCode = t_sApiCInvoiceCIJE.tcCreditorCode
                     no-lock no-error.
                if available Creditor
                then assign t_sApiCInvoiceCIJE.Creditor_ID = Creditor.Creditor_ID.
            end. /* if t_sApiCInvoiceCIJE.tcCreditorCode <> "":U and  */
        end. /* if (t_sApiCInvoiceCIJE.Creditor_ID = ? or  */
        else find Creditor where 
                  Creditor.Creditor_ID = t_sApiCInvoiceCIJE.Creditor_ID
                  no-lock no-error.
        if not available Creditor
        then Return. /* If this missing then further defaulting makes no sense */
        
        /* ====================================================================================================================================== */
        /* Find the CIREC-GL-account as we need this later on in some cases - raise no error when not found as we don't need this in all cases    */
        /* Find the Purchase-GL-account as we need this later on in some cases - raise no error when not found as we don't need this in all cases */
        /* ====================================================================================================================================== */
        find first GL where 
                   GL.SharedSet_ID     = viXGLSharedSetID       and 
                   GL.GLSystemTypeCode = {&GLSYSTEMTYPE-CIREC}  and  /* CIREC */
                   GL.GlTypeCode       = {&GLTYPECODE-SYST}     and  /* SYSTEM */
                   GL.GLIsActive       = true
                   no-lock no-error.
        if available GL 
        then assign vcXCirecGLCode = GL.GLCode.
        else assign vcXCirecGLCode = "":U.
        assign vcXPurchaseGLCode = "":U.
        for first Profile where 
                  Profile.Profile_ID = Creditor.PurchaseGLProfile_ID
                  no-lock,
            first ProfileLink of Profile where 
                  ProfileLink.SharedSet_ID = viXGLSharedSetID
                  no-lock,
            first GL where 
                  GL.GL_ID = ProfileLink.ProfileLinkObject_ID 
                  no-lock
                  on error undo, throw:
            assign vcXPurchaseGLCode = GL.GLCode.
        end. /* for first Profile where  */
        
        /* =============================================================== */
        /* Fill in the Reason when not speified                            */
        /* Fill all Reason/InvoiceStatus-related fields when not specified */ 
        /* =============================================================== */
        Release Reason no-error.
        if t_sApiCInvoiceCIJE.tcReasonCode = "":U or 
           t_sApiCInvoiceCIJE.tcReasonCode = ?
        then do :
            Find Reason where 
                 Reason.Reason_ID = Creditor.Reason_ID
                 no-lock no-error.
            if available Reason 
            then assign t_sApiCInvoiceCIJE.tcReasonCode = Reason.ReasonCode
                        t_sApiCInvoiceCIJE.Reason_ID    = Reason.Reason_ID.
        end. /* if sApiCInvoiceCIJE.tcReasonCode = "":U or */
        if t_sApiCInvoiceCIJE.tcReasonCode <> "":U and 
           t_sApiCInvoiceCIJE.tcReasonCode <> ?    and 
           (t_sApiCInvoiceCIJE.tcReasonAllocationStatus = "":U or 
            t_sApiCInvoiceCIJE.tcReasonAllocationStatus = ?)
        then do :
            Find Reason where 
                 Reason.ReasonCode = t_sApiCInvoiceCIJE.tcReasonCode
                 no-lock no-error.
            if available Reason 
            then assign t_sApiCInvoiceCIJE.tcReasonAllocationStatus   = Reason.ReasonAllocationStatus
                        t_sApiCInvoiceCIJE.CInvoiceAllocationStatus   = Reason.ReasonAllocationStatus
                        t_sApiCInvoiceCIJE.CInvoiceIsInvoiceApproved  = Reason.ReasonIsInvoiceApproved
                        t_sApiCInvoiceCIJE.CInvoiceIsLockPayment      = Reason.ReasonIsLockPayment
                        t_sApiCInvoiceCIJE.CInvoiceIsLogisticMatching = Reason.ReasonIsLogisticMatching
                        t_sApiCInvoiceCIJE.CInvoiceIsInitialStatus    = Reason.ReasonIsInitialStatus.
        end. /* if t_sApiCInvoiceCIJE.tcReasonCode <> "":U and  */
        
        /* ====================================================================================================================== */
        /* Fill in the Control-GL, Div, CC and Prj-info on the Invoice when not filled                                            */
        /* If the Control-GL is enabled for Div/Prj/CC:                                                                           */
        /*   1st: Take the Div/Prj/CC from the input if it is provided                                                            */
        /*   2nd: If the Div is not yet filled then take the Div from the Creditor-definition (no Creditor-default for Prj/CC)    */
        /*   3rd: If the Div/Prj/CC is not yet filled then take the Div/Prj/CC from the Control-GL-definition                     */
        /* ====================================================================================================================== */
        assign viXProjectProfileID      = 0
               viXCostCentreProfileID   = 0
               viXDivisionProfileID     = 0
               viXGLIsDivisionAccount   = false 
               viXGLIsCostCentreAccount = false
               viXGLIsProjectAccount    = false.
        if t_sApiCInvoiceCIJE.tcControlGLCode = "":U or 
           t_sApiCInvoiceCIJE.tcControlGLCode = ?
        then do :
            assign viXGLProfileID = if t_sApiCInvoiceCIJE.CInvoiceType= {&INVOICETYPE-PREPAYMENT} /* PREPAYMENT */
                                    then Creditor.PrePayControlGLProfile_ID
                                    else if t_sApiCInvoiceCIJE.CInvoiceType= {&INVOICETYPE-INVOICE}           or /* INVOICE */
                                            t_sApiCInvoiceCIJE.CInvoiceType= {&INVOICETYPE-INVOICECORRECTION}    /* INVOICECORRECTION */
                                         then Creditor.InvControlGLProfile_ID
                                         else Creditor.CnControlGLProfile_ID. 
            for first ProfileLink  where 
                      ProfileLink.Profile_ID = viXGLProfileID and 
                      ProfileLink.SharedSet_ID = viXGLSharedSetID
                      no-lock,
                first GL where 
                      GL.GL_ID = ProfileLink.ProfileLinkObject_ID 
                      no-lock
                      on error undo, throw:
                assign t_sApiCInvoiceCIJE.tcControlGLCode = GL.GLCode
                       t_sApiCInvoiceCIJE.ControlGL_ID    = GL.GL_ID                       
                       viXGLIsDivisionAccount             = GL.GLIsDivisionAccount
                       viXGLIsCostCentreAccount           = GL.GLIsCostCentreAccount
                       viXGLIsProjectAccount              = GL.GLIsProjectAccount.
                if GL.GLIsDivisionAccount = true
                then assign viXDivisionProfileID = GL.DivisionProfile_ID.
                if GL.GLIsCostCentreAccount = true
                then assign viXCostCentreProfileID = GL.CostCentreProfile_ID.
                if GL.GLIsProjectAccount = true
                then assign viXProjectProfileID = GL.ProjectProfile_ID.
            end. /* for first ProfileLink  where */
        end. /* if t_sApiCInvoiceCIJE.tcControlGLCode = "":U or */
        else do :
            Find GL where 
                 GL.SharedSet_ID = viXGLSharedSetID and 
                 GL.GLCode       = t_sApiCInvoiceCIJE.tcControlGLCode
                 no-lock no-error.
            if available GL
            then do :     
                assign t_sApiCInvoiceCIJE.ControlGL_ID = GL.GL_ID.
                if GL.GLIsDivisionAccount = true
                then assign viXDivisionProfileID = GL.DivisionProfile_ID.
                if GL.GLIsCostCentreAccount = true
                then assign viXCostCentreProfileID = GL.CostCentreProfile_ID.
                if GL.GLIsProjectAccount = true
                then assign viXProjectProfileID = GL.ProjectProfile_ID.
                assign viXGLIsDivisionAccount       = GL.GLIsDivisionAccount
                       viXGLIsCostCentreAccount     = GL.GLIsCostCentreAccount
                       viXGLIsProjectAccount        = GL.GLIsProjectAccount.                
            end. /* if available GL */
        end. /* Not if t_sApiCInvoiceCIJE.tcControlGLCode = "":U or */
        /* Default the division when the ControlGL supports divisions */                
        if viXGLIsDivisionAccount = true and 
           viXDivisionProfileID <> 0 and 
           viXDivisionProfileID <> ?
        then do :
            if (t_sApiCInvoiceCIJE.tcDivisionCode = "":U or
                t_sApiCInvoiceCIJE.tcDivisionCode = ?) and 
               Creditor.DivisionProfile_ID <> 0 and 
               Creditor.DivisionProfile_ID <> ?
            then for first ProfileLink  where 
                           ProfileLink.Profile_ID   = Creditor.DivisionProfile_ID and 
                           ProfileLink.SharedSet_ID = viXDivisionSharedSetID
                           no-lock,
                     first Division where 
                           Division.Division_ID = ProfileLink.ProfileLinkObject_ID 
                           no-lock
                           on error undo, throw:
                    assign t_sApiCInvoiceCIJE.tcDivisionCode = Division.DivisionCode
                           t_sApiCInvoiceCIJE.Division_ID    = Division.Division_ID.                    
                 end. /* or first Profile where */
            if t_sApiCInvoiceCIJE.tcDivisionCode = "":U or
               t_sApiCInvoiceCIJE.tcDivisionCode = ?
            then for first ProfileLink  where 
                           ProfileLink.Profile_ID   = viXDivisionProfileID and 
                           ProfileLink.SharedSet_ID = viXDivisionSharedSetID
                           no-lock,
                     first Division where 
                           Division.Division_ID = ProfileLink.ProfileLinkObject_ID 
                           no-lock
                           on error undo, throw:
                    assign t_sApiCInvoiceCIJE.tcDivisionCode = Division.DivisionCode
                           t_sApiCInvoiceCIJE.Division_ID    = Division.Division_ID.                    
                 end. /* or first Profile where */
        end. /* if viXDivisionProfileID <> 0 and */
        /* Default the cost-centre when the ControlGL supports cost-centress */
        if viXGLIsCostCentreAccount and 
           viXCostCentreProfileID <> 0 and 
           viXCostCentreProfileID <> ? and 
           (t_sApiCInvoiceCIJE.tcCostCentreCode = "":U or 
            t_sApiCInvoiceCIJE.tcCostCentreCode = ?)
        then do :
            for first ProfileLink  where 
                      ProfileLink.Profile_ID = viXCostCentreProfileID and 
                      ProfileLink.SharedSet_ID = viXCostCentreSharedSetID
                      no-lock,
                first CostCentre where 
                      CostCentre.CostCentre_ID = ProfileLink.ProfileLinkObject_ID 
                      no-lock
                      on error undo, throw:
                assign t_sApiCInvoiceCIJE.tcCostCentreCode = CostCentre.CostCentreCode
                       t_sApiCInvoiceCIJE.CostCentre_ID    = CostCentre.CostCentre_ID.                    
            end. /* or first Profile where */
        end. /* if viXCostCentreProfileID <> 0 and */
        /* Default the project when the ControlGL supports projects */
        if viXGLIsProjectAccount and 
           viXProjectProfileID <> 0 and 
           viXProjectProfileID <> ? and 
           (t_sApiCInvoiceCIJE.tcProjectCode = "":U or 
            t_sApiCInvoiceCIJE.tcProjectCode = ?)
        then do :
            for first ProfileLink  where 
                      ProfileLink.Profile_ID = viXProjectProfileID and 
                      ProfileLink.SharedSet_ID = viXProjectSharedSetID
                      no-lock,
                first qaddb.Project where 
                      qaddb.Project.Project_ID = ProfileLink.ProfileLinkObject_ID 
                      no-lock
                      on error undo, throw:
                assign t_sApiCInvoiceCIJE.tcProjectCode = qaddb.Project.ProjectCode
                       t_sApiCInvoiceCIJE.Project_ID    = qaddb.Project.Project_ID.                    
            end. /* or first Profile where */
        end. /* if viXProjectProfileID <> 0 and */
                
        /* ============================================================================== */
        /* Fill in defaults in t_sApiPostingVatCIJE records that are in the input         */
        /* Only do this in case PostingLine holds only a single PostingVat as with eg     */
        /* InvoicePostAndPrint there can be multiple PostingVats for a single Postingline */
        /* ============================================================================== */
        for each t_sApiPostingLineCIJE where 
                 t_sApiPostingLineCIJE.tc_ParentRowid = t_sApiPostingCIJE.tc_rowid
            on error undo, throw :
            /* Find the PostingVat - do not use a 'first' option as we only want to do this in case there is only one PostingVat for the PostingLine */
            Find t_sApiPostingVatCIJE where 
                 t_sApiPostingVatCIJE.tc_ParentRowid = t_sApiPostingLineCIJE.tc_rowid
                 no-error.
            if not available t_sApiPostingVatCIJE 
            then next.
            /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
            assign t_sApiPostingVatCIJE.PostingVatIsReverseCharge = false when t_sApiPostingVatCIJE.PostingVatIsReverseCharge = ?
                   t_sApiPostingVatCIJE.PostingVatIsAbsRet        = false when t_sApiPostingVatCIJE.PostingVatIsAbsRet        = ?
                   t_sApiPostingVatCIJE.PostingVatIsSuspDel       = false when t_sApiPostingVatCIJE.PostingVatIsSuspDel       = ?.
            /* Specificy some redundant or fixed fields */
            if t_sApiPostingVatCIJE.tcDomainCode = "":U or 
               t_sApiPostingVatCIJE.tcDomainCode = ?
            then assign t_sApiPostingVatCIJE.tcDomainCode = vcXDomainCode.
            if t_sApiPostingVatCIJE.tcVatInOut = "":U or 
               t_sApiPostingVatCIJE.tcVatInOut = ?
            then assign t_sApiPostingVatCIJE.tcVatInOut = {&VATINOUT-INPUT}. /* INPUT */
            if t_sApiPostingVatCIJE.PostingVatInOut = "":U or 
               t_sApiPostingVatCIJE.PostingVatInOut = ?
            then assign t_sApiPostingVatCIJE.PostingVatInOut = t_sApiPostingVatCIJE.tcVatInOut.
            if t_sApiPostingVatCIJE.PostingVatTaxTransType = "":U or 
               t_sApiPostingVatCIJE.PostingVatTaxTransType = ?
            then assign t_sApiPostingVatCIJE.PostingVatTaxTransType = {&VATTAXTRANSACTIONTYPE-APVOUCHER}. /* 22 */
            if t_sApiPostingVatCIJE.PostingVatTransType = "":U or 
               t_sApiPostingVatCIJE.PostingVatTransType = ?
            then assign t_sApiPostingVatCIJE.PostingVatTransType = {&VATTRANSACTIONTYPE-PURCHASE}. /* PURCHASE */
            if t_sApiPostingVatCIJE.PostingVatTaxPointDate = ?
            then assign t_sApiPostingVatCIJE.PostingVatTaxPointDate = t_sApiCInvoiceCIJE.CInvoiceTaxPointDate.
            /* If the tax-amounts on PostingVat are empty then take them from PostingLine */
            if (t_sApiPostingVatCIJE.PostingVatTaxDebitTC = ? or 
                t_sApiPostingVatCIJE.PostingVatTaxDebitTC = 0) and 
               (t_sApiPostingVatCIJE.PostingVatTaxCreditTC = ? or 
                t_sApiPostingVatCIJE.PostingVatTaxCreditTC = 0)
            then assign t_sApiPostingVatCIJE.PostingVatTaxDebitTC  = t_sApiPostingLineCIJE.PostingLineDebitTC
                        t_sApiPostingVatCIJE.PostingVatTaxCreditTC = t_sApiPostingLineCIJE.PostingLineCreditTC.
            /* If the tax-base-amounts on PostingVat are empty then try to take them from t_sApiCInvoiceVatCIJE */
            if (t_sApiPostingVatCIJE.PostingVatBaseDebitTC = ? or 
                t_sApiPostingVatCIJE.PostingVatBaseDebitTC = 0) and 
               (t_sApiPostingVatCIJE.PostingVatBaseCreditTC = ? or 
                t_sApiPostingVatCIJE.PostingVatBaseCreditTC = 0)
            then do :
                Find t_sApiCInvoiceVatCIJE where 
                     t_sApiCInvoiceVatCIJE.tc_ParentRowid = t_sApiCInvoiceCIJE.tc_Rowid and 
                     t_sApiCInvoiceVatCIJE.tcVatCode      = t_sApiPostingVatCIJE.tcVatCode
                     no-lock no-error. /* Do not use the first option - when there are multiple ones then this gets complicated and values cannot be defaulted */
                if not available t_sApiCInvoiceVatCIJE
                then Find t_sApiCInvoiceVatCIJE where 
                          t_sApiCInvoiceVatCIJE.tc_ParentRowid = t_sApiCInvoiceCIJE.tc_Rowid 
                          no-lock no-error. /* Do not use the first option - when there are multiple ones then this gets complicated and values cannot be defaulted */
                if available t_sApiCInvoiceVatCIJE
                then assign t_sApiPostingVatCIJE.PostingVatBaseDebitTC  = t_sApiCInvoiceVatCIJE.CInvoiceVatVatBaseDebitTC                            
                            t_sApiPostingVatCIJE.PostingVatBaseCreditTC = t_sApiCInvoiceVatCIJE.CInvoiceVatVatBaseCreditTC.
            end. /* if (t_sApiPostingVatCIJE.PostingVatBaseDebitTC = ? or  */
        end. /* for each t_sApiPostingLineCIJE where  */
        
        /* ========================================================================================================= */
        /* Fill in the Journal-related fields in the SI-Posting based on the InvoiceType/Creditor when not specified */
        /* ========================================================================================================= */
        /* Normalise input to restict the conditions later on in this program */
        if t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = ? then assign t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = "":U.
        if t_sApiCInvoiceCIJE.CInvoiceSiteCode = ? then assign t_sApiCInvoiceCIJE.CInvoiceSiteCode = "":U.
        if t_sApiCInvoiceCIJE.tcJournalCode = ? then assign t_sApiCInvoiceCIJE.tcJournalCode = "":U.
        if t_sApiCInvoiceCIJE.Journal_ID = ? then assign t_sApiCInvoiceCIJE.Journal_ID = 0.
        /* Get the default DaybookSet and Site of the Creditor */
        if t_sApiCInvoiceCIJE.CInvoiceSiteCode        = "":U or 
           t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode  = "":U
        then do :
            vhXQueryVdMstr:Query-Prepare("for each vd_mstr where vd_mstr.vd_domain = '" + vcDomainCode + "' and " + 
                                          "vd_mstr.vd_addr = '" + Creditor.CreditorCode + "'").
            vhXQueryVdMstr:Query-Open.
            vhXQueryVdMstr:Get-First(no-lock).
            if valid-handle(vhXBufferVdMstr) and 
               vhXBufferVdMstr:available 
            then do :
                if t_sApiCInvoiceCIJE.CInvoiceSiteCode = "":U 
                then assign t_sApiCInvoiceCIJE.CInvoiceSiteCode = vhXBufferVdMstr::vd_site.
                if t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = "":U 
                then assign t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = vhXBufferVdMstr::vd_daybookset.
            end. /* if valid-handle(vhXBufferVdMstr) and  */
        end. /* if t_sApiCInvoiceCIJE.CInvoiceSiteCode = "":U or */
        /* Check if DaybookSetBySite is turned on and optionally fill DaybookSet and Site */
        vhXQueryPocCtrl:Query-Prepare("for each poc_ctrl where poc_ctrl.poc_domain = '" + vcDomainCode + "'").
        vhXQueryPocCtrl:Query-Open.
        vhXQueryPocCtrl:Get-First(no-lock).
        if valid-handle(vhXBufferPocCtrl) and  
           vhXBufferPocCtrl:available     and 
           vhXBufferPocCtrl::poc_dybkset_by_site = true 
        then do :
            if t_sApiCInvoiceCIJE.CInvoiceSiteCode       <> "":U and 
               t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode  = "":U
            then vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                "dybs_mstr.dybs_type = '2' and " + 
                                                "dybs_mstr.dybs_site = '" + t_sApiCInvoiceCIJE.CInvoiceSiteCode + "'").
            else if t_sApiCInvoiceCIJE.CInvoiceSiteCode        = "":U and 
                    t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode <> "":U
                 then vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                     "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                     "dybs_mstr.dybs_type = '2' and " + 
                                                     "dybs_mstr.dybs_code = '" + t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode + "'").
                 else if t_sApiCInvoiceCIJE.CInvoiceSiteCode       <> "":U and 
                         t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode <> "":U
                      then vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                          "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                          "dybs_mstr.dybs_type = '2' and " + 
                                                          "dybs_mstr.dybs_site = '" + t_sApiCInvoiceCIJE.CInvoiceSiteCode + "' and " + 
                                                          "dybs_mstr.dybs_code = '" + t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode + "'").
                      else vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                          "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                          "dybs_mstr.dybs_type = '2' ").
            vhXQueryDybsMstr:Query-Open.
            vhXQueryDybsMstr:Get-First(no-lock).            
        end. /* if valid-handle(vhXBufferPocCtrl) and */
        else do :
            assign t_sApiCInvoiceCIJE.CInvoiceSiteCode = "":U.
            if t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = "":U
            then vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                "dybs_mstr.dybs_type = '2'").
            else vhXQueryDybsMstr:Query-Prepare("for each dybs_mstr where " + 
                                                "dybs_mstr.dybs_domain = '" + vcDomainCode + "' and " + 
                                                "dybs_mstr.dybs_type = '2' and " + 
                                                "dybs_mstr.dybs_code = '" + t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode + "'").
            vhXQueryDybsMstr:Query-Open.
            vhXQueryDybsMstr:Get-First(no-lock).
        end. /* Not if valid-handle(vhXBufferPocCtrl) and */
        if valid-handle(vhXBufferDybsMstr) and 
           vhXBufferDybsMstr:available 
        then do :
            assign t_sApiCInvoiceCIJE.CInvoiceSiteCode       = vhXBufferDybsMstr::dybs_site
                   t_sApiCInvoiceCIJE.CInvoiceDayBookSetCode = vhXBufferDybsMstr::dybs_code.
            if t_sApiCInvoiceCIJE.tcJournalCode = "":U
            then if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-CREDITNOTE} /* CREDITNOTE */
                 then assign t_sApiCInvoiceCIJE.tcJournalCode = vhXBufferDybsMstr::dybs_credit_dybk.
                 else if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}  /* CREDITNOTECORRECTION */
                      then assign t_sApiCInvoiceCIJE.tcJournalCode = vhXBufferDybsMstr::dybs_neg_crcn_dybk.
                      else if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} /* INVOICECORRECTION */
                           then assign t_sApiCInvoiceCIJE.tcJournalCode = vhXBufferDybsMstr::dybs_neg_criv_dybk.
                           else assign t_sApiCInvoiceCIJE.tcJournalCode = vhXBufferDybsMstr::dybs_invoices_dybk.
        end. /* if valid-handle(vhXBufferDybsMstr) and */
        /* If Journal is still empty then use the first Journal with the correct properties */
        if t_sApiCInvoiceCIJE.tcJournalCode = "":U
        then 
            for each  Journal where 
                      Journal.SharedSet_ID    = viXJournalSharedSetID       and 
                      Journal.JournalIsActive = true                        and 
                      Journal.JournalControl  = {&JOURNALCONTROL-FINANCIAL} and  /* FINANCIAL */ 
                      Journal.JournalTypeCode = (if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-CREDITNOTE} /* CREDITNOTE */
                                                 then {&JOURNALTYPE-CREDITORCREDITNOTE}
                                                 else if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} /* INVOICECORRECTION */
                                                      then {&JOURNALTYPE-CREDITORINVOICECORRECT}
                                                      else if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} /* CREDITNOTECORRECTION */
                                                           then {&JOURNALTYPE-CREDITORCREDITNOTECORRECT}
                                                           else {&JOURNALTYPE-CREDITORINVOICE}) /* meaning {&INVOICETYPE-INVOICE} or {&INVOICETYPE-FINANCECHARGE} */
                      no-lock,
                first JournalType of Journal where 
                      JournalType.JournalTypeIsCorrection = (if t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or /* INVOICECORRECTION */
                                                                t_sApiCInvoiceCIJE.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} /* CREDITNOTECORRECTION */
                                                             then true
                                                             else false) 
                      no-lock,
                first Layer of Journal where
                      Layer.LayerTypeCode = {&LAYERTYPECODE-OFFICIAL} /* OFFICIAL */
                      no-lock 
                      on error undo, throw:
                assign t_sApiCInvoiceCIJE.tcJournalCode = Journal.JournalCode. 
                Leave.
            end. /* for first Profile where */
            
        /* ============================================================== */
        /* Copy the Journal and Voucher of the Invoice to the SI-Posting  */
        /* ============================================================== */
        assign t_sApiPostingCIJE.tcJournalCode = t_sApiCInvoiceCIJE.tcJournalCode.
        if t_sApiCInvoiceCIJE.CInvoiceVoucher <> 0 and 
           t_sApiCInvoiceCIJE.CInvoiceVoucher <> ? and 
           (t_sApiPostingCIJE.PostingVoucher = 0 or 
            t_sApiPostingCIJE.PostingVoucher= ?)
        then assign t_sApiPostingCIJE.PostingVoucher = t_sApiCInvoiceCIJE.CInvoiceVoucher.
        
        /* ======================================================================================================= */
        /* Fill the Div, CC and Prj-info on the SI-PostingLine with the Control-Account (take it from the invoice) */
        /* Fill in the GLCode on the first SI-PostingLine with the Control-GL when not filled                      */
        /* Fill in the GLCode on the second SI-PostingLine with the CIREC-GL when not filled                       */
        /* ======================================================================================================= */
        For each t_sApiPostingLineCIJE where 
                 t_sApiPostingLineCIJE.tc_ParentRowid = t_sApiPostingCIJE.tc_rowid 
                 on error undo, throw :
            /* First PostingLine: Fill GL with Control-GL and FDS-data from the invoice */
            if t_sApiPostingLineCIJE.PostingLineSequence = 1
            then if t_sApiPostingLineCIJE.tcGLCode = ? or 
                    t_sApiPostingLineCIJE.tcGLCode = "":U
                 then assign t_sApiPostingLineCIJE.tcGLCode = t_sApiCInvoiceCIJE.tcControlGLCode
                             t_sApiPostingLineCIJE.tcDivisionCode = t_sApiCInvoiceCIJE.tcDivisionCode
                             t_sApiPostingLineCIJE.tcCostCentreCode = t_sApiCInvoiceCIJE.tcCostCentreCode
                             t_sApiPostingLineCIJE.tcProjectCode = t_sApiCInvoiceCIJE.tcProjectCode.                 
            /* For the second PostingLine: fill the GL with the Cirec-Account */
            if t_sApiPostingLineCIJE.PostingLineSequence = 2 and 
               (t_sApiPostingLineCIJE.tcGLCode = ? or 
                t_sApiPostingLineCIJE.tcGLCode = "":U)
            then assign t_sApiPostingLineCIJE.tcGLCode = vcXCirecGLCode.
        End. /* For each t_sApiPostingLineCIJE where */
        
        /* ============================================================================================ */
        /* Compose the MF-posting when there is none and when this is needed based upon the ReasonCode  */
        /* Create the PostingLines in this MF-Postingbased upon the SI-PostingLine on the Cirec-account */
        /* ============================================================================================ */
        if t_sApiCInvoiceCIJE.CInvoiceIsLogisticMatching = false
        then do :
            if t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC} /* TRANSIENTALLOCATION */ or 
               t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-ALLOC} /* ALLOCATION */ 
            then MFPOSTINGBLOCK: do :
                /* Only create a MF-Posting in case the input tables t_sCInvoicePostingCIJE and t_sPostingCIJE does not hold this */
                assign vlXCreateMFPostingHere = true.
                find bXtMFCInvoicePosting where 
                     bXtMFCInvoicePosting.tc_Parentrowid      = t_sApiCInvoiceCIJE.tc_rowid and 
                     bXtMFCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-ALLOCATION} /* ALLOC */
                     no-error. /* Do not use a 'first' option as there should only be one */
                if available bXtMFCInvoicePosting
                then assign vlXCreateMFPostingHere = false.
                else do :
                    create bXtMFCInvoicePosting.
                    assign bXtMFCInvoicePosting.tc_Parentrowid      = t_sApiCInvoiceCIJE.tc_rowid
                           bXtMFCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-ALLOCATION} /* ALLOC */.
                end. /* if available bXtMFCInvoicePosting */
                find bXtMFPosting where 
                     bXtMFPosting.tc_rowid = bXtMFCInvoicePosting.tc_rowid 
                     no-error. /* Do not use a 'first' option as there should only be one */
                if available bXtMFPosting
                then assign vlXCreateMFPostingHere = false.
                else do :
                    create bXtMFPosting.
                    assign bXtMFPosting.tc_rowid = bXtMFCInvoicePosting.tc_rowid.
                end. /* if available bXtMFPosting */
                /* Default in the posting-lines in the MF-Posting */ 
                if vlXCreateMFPostingHere = true
                then do :
                    /* Only continue if we know the CIREC account */
                    if vcXCirecGLCode = "":U or 
                       vcXCirecGLCode = ?
                    then Leave MFPOSTINGBLOCK.
                    /* Find the CIREC posting-line in the SI-Posting */
                    find t_sApiPostingLineCIJE where 
                         t_sApiPostingLineCIJE.tc_ParentRowid = t_sApiPostingCIJE.tc_rowid and 
                         t_sApiPostingLineCIJE.tcGLCode       = vcXCirecGLCode 
                         no-error.
                    if not available t_sApiPostingLineCIJE
                    then Leave MFPOSTINGBLOCK.                    
                    /* Default in the CIREC posting-line in the MF-Posting just by using the CIREC Posting-line of the SI-Posting but with oposite amounts */
                    create bXtMFPostingLine.
                    buffer-copy t_sApiPostingLineCIJE 
                                except t_sApiPostingLineCIJE.tc_rowid
                                to bXtMFPostingLine
                                assign bXtMFPostingLine.tc_parentRowid      = bXtMFPosting.tc_rowid
                                       bXtMFPostingLine.tc_rowid            = "CIREC-" + bXtMFPosting.tc_rowid + t_sApiPostingLineCIJE.tc_Rowid
                                       bXtMFPostingLine.PostingLineSequence = 1
                                       bXtMFPostingLine.PostingLineCreditTC = t_sApiPostingLineCIJE.PostingLineDebitTC
                                       bXtMFPostingLine.PostingLineDebitTC  = t_sApiPostingLineCIJE.PostingLineCreditTC
                                       bXtMFPostingLine.PostingLineCreditLC = 0
                                       bXtMFPostingLine.PostingLineDebitLC  = 0
                                       bXtMFPostingLine.PostingLineCreditCC = 0
                                       bXtMFPostingLine.PostingLineDebitCC  = 0.
                    /* Default in the Purchase-Control posting-line in the MF-Posting just by using the CIREC Posting-line of the SI-Posting */
                    create bXtMFPostingLine.
                    buffer-copy t_sApiPostingLineCIJE 
                                except t_sApiPostingLineCIJE.tc_rowid
                                to bXtMFPostingLine
                                assign bXtMFPostingLine.tc_parentRowid      = bXtMFPosting.tc_rowid
                                       bXtMFPostingLine.tc_rowid            = "PURCTRL-" + bXtMFPosting.tc_rowid + t_sApiPostingLineCIJE.tc_Rowid
                                       bXtMFPostingLine.PostingLineSequence = 2
                                       bXtMFPostingLine.tcGLCode            = vcXPurchaseGLCode.
                end. /* if vlXCreateMFPostingHere = true */
            end. /* MFPOSTINGBLOCK + if t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC} /* TRANSIENTALLOCATION */ or  */
        end. /* if t_sApiCInvoiceCIJE.CInvoiceIsLogisticMatching = false */
        
        /* =============================================================== */
        /* Fill defaults in the MF-Posting - most come from the SI-Posting */
        /* Fill in the GL-codes on the first and second MF-PostingLine     */
        /* =============================================================== */
        if t_sApiCInvoiceCIJE.CInvoiceIsLogisticMatching = false
        then do :
            for first bXtMFCInvoicePosting where 
                      bXtMFCInvoicePosting.tc_Parentrowid      = t_sApiCInvoiceCIJE.tc_rowid and 
                      bXtMFCInvoicePosting.CInvoicePostingType = {&INVOICEPOSTINGTYPE-ALLOCATION} /* ALLOC */,
                first bXtMFPosting where 
                      bXtMFPosting.tc_rowid = bXtMFCInvoicePosting.tc_rowid
                      on error undo, throw : 
                /* === Defaults comming from the SI-Posting */
                if bXtMFPosting.Company_ID = ? or 
                   bXtMFPosting.Company_ID = 0 
                then assign bXtMFPosting.Company_ID = t_sApiPostingCIJE.Company_ID.
                if bXtMFPosting.PostingText = ? or 
                   bXtMFPosting.PostingText = "":U 
                then assign bXtMFPosting.PostingText = t_sApiPostingCIJE.PostingText.
                if bXtMFPosting.PostingInvoiceReferenceTxt = ? or 
                   bXtMFPosting.PostingInvoiceReferenceTxt = "":U 
                then assign bXtMFPosting.PostingInvoiceReferenceTxt = t_sApiPostingCIJE.PostingInvoiceReferenceTxt.
                if bXtMFPosting.PostingParentText = ? or 
                   bXtMFPosting.PostingParentText = "":U 
                then assign bXtMFPosting.PostingParentText = t_sApiPostingCIJE.PostingParentText.
                if bXtMFPosting.PostingDate = ?
                then assign bXtMFPosting.PostingDate = t_sApiPostingCIJE.PostingDate.
                if bXtMFPosting.PostingYear = ? or 
                   bXtMFPosting.PostingYear = 0 
                then assign bXtMFPosting.PostingYear = t_sApiPostingCIJE.PostingYear.
                if bXtMFPosting.PostingPeriod = ? or 
                   bXtMFPosting.PostingPeriod = 0
                then assign bXtMFPosting.PostingPeriod = t_sApiPostingCIJE.PostingPeriod.
                if bXtMFPosting.PostingSystemDate = ? 
                then assign bXtMFPosting.PostingSystemDate = t_sApiPostingCIJE.PostingSystemDate.
                if bXtMFPosting.PostingValueDate = ?
                then assign bXtMFPosting.PostingValueDate = t_sApiPostingCIJE.PostingValueDate.                  
                /* === Get the default MF-journal */
                if bXtMFPosting.tcJournalCode = ? or 
                   bXtMFPosting.tcJournalCode = "":U
                then do :
                    if t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC} /* TRANSIENTALLOCATION */
                    then do :
                        for each  Journal where 
                                  Journal.SharedSet_ID    = viXJournalSharedSetID and 
                                  Journal.JournalTypeCode = {&JOURNALTYPE-CIREC}  and /* MATCHINGENTRYCIREC */
                                  Journal.JournalIsActive = true                  and
                                  Journal.JournalControl  = {&JOURNALCONTROL-FINANCIAL} /* FINANCIAL */
                                  no-lock,
                            first Layer of Journal where
                                  Layer.LayerTypeCode   = {&LAYERTYPECODE-TRANSIENT} /* TRANSIENT */
                                  no-lock
                                  on error undo, throw:
                            assign bXtMFPosting.tcJournalCode = Journal.JournalCode.
                            Leave.
                        end. /* for each Journal where */
                    end. /* if t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC} */
                    else do :
                        for each  Journal where 
                                  Journal.SharedSet_ID    = viXJournalSharedSetID and 
                                  Journal.JournalTypeCode = {&JOURNALTYPE-CIREC}  and /* MATCHINGENTRYCIREC */
                                  Journal.JournalIsActive = true                  and
                                  Journal.JournalControl  = {&JOURNALCONTROL-FINANCIAL} /* FINANCIAL */
                                  no-lock,
                            first Layer of Journal where
                                  Layer.LayerTypeCode   = {&LAYERTYPECODE-OFFICIAL} /* OFFICIAL */
                                  no-lock
                                  on error undo, throw:
                            assign bXtMFPosting.tcJournalCode = Journal.JournalCode.
                            Leave.
                        end. /* for each Journal where */
                    end. /* Not if t_sApiCInvoiceCIJE.CInvoiceAllocationStatus = {&ALLOCSTATUS-TRANSALLOC} */
                end. /* if bXtMFPosting.tcJournalCode = ? or  */
                /* === Fill in the GLCode on the second SI-PostingLine with the Control-GL when not filled */
                /* === Fill in the GLCode on the first SI-PostingLine with the CIREC-GL when not filled    */
                for each bXtMFPostingLine where
                         bXtMFPostingLine.tc_parentRowid = bXtMFPosting.tc_rowid and 
                         (bXtMFPostingLine.tcGLCode = ? or 
                          bXtMFPostingLine.tcGLCode = "":U)
                         on error undo, throw : 
                    if bXtMFPostingLine.PostingLineSequence = 1
                    then assign bXtMFPostingLine.tcGLCode = vcXCirecGLCode.
                    else if bXtMFPostingLine.PostingLineSequence = 2
                         then assign bXtMFPostingLine.tcGLCode = vcXPurchaseGLCode.
                end. /* for each bXtMFPostingLine where */ 
            end. /* for first bXtMFCInvoicePosting where  */
        end. /*  if t_sApiCInvoiceCIJE.CInvoiceIsLogisticMatching = false */
        
        /* ==================================================================================================== */
        /* For all PostingLines of the SI-Posting and of the MF-Posting:                                        */
        /*  - Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
        /*  - Fill the Currency on the CI-PostingLines with the invoice-currency when not filled                */
        /*  - Fill the Div, CC and Prj-info on the CI-PostingLines with the defaults from the accounts          */
        /*     (except the first PostingLine in the SI-Posting that is on the Control-Account)                  */
        /* ==================================================================================================== */
        For each t_sApiCInvoicePostingCIJE where 
                 t_sApiCInvoicePostingCIJE.tc_Parentrowid = t_sApiCInvoiceCIJE.tc_rowid
                 no-lock,
            each t_sApiPostingCIJE where 
                 t_sApiPostingCIJE.tc_rowid = t_sApiCInvoicePostingCIJE.tc_rowid
                 no-lock, 
            each t_sApiPostingLineCIJE where 
                 t_sApiPostingLineCIJE.tc_ParentRowid = t_sApiPostingCIJE.tc_rowid
                 no-lock
                 on error undo, throw :
            /* Avoid unkown-values as this is not allowed although some interfaces (like QXtend) do pass in this */
            assign t_sApiPostingLineCIJE.tcAllocationType = "" when t_sApiPostingLineCIJE.tcAllocationType = ?
                   t_sApiPostingLineCIJE.tcDivisionCode   = "" when t_sApiPostingLineCIJE.tcDivisionCode   = ?
                   t_sApiPostingLineCIJE.tcCostCentreCode = "" when t_sApiPostingLineCIJE.tcCostCentreCode = ?
                   t_sApiPostingLineCIJE.tcProjectCode    = "" when t_sApiPostingLineCIJE.tcProjectCode    = ?.       
            /* Currency */
            if t_sApiPostingLineCIJE.tcCurrencyCode = ? or 
               t_sApiPostingLineCIJE.tcCurrencyCode = "":U
            then assign t_sApiPostingLineCIJE.tcCurrencyCode = t_sApiCInvoiceCIJE.tcCurrencyCode.
            /* Fill the Div, CC and Prj-info (not on the first PostingLine of the SI-Posting) */
            if t_sApiPostingLineCIJE.PostingLineSequence <> 1                                  and 
               t_sApiPostingLineCIJE.tcGLCode            <> t_sApiCInvoiceCIJE.tcControlGLCode and 
               t_sApiPostingLineCIJE.tcGLCode            <> ?                                  and 
               t_sApiPostingLineCIJE.tcGLCode            <> "":U
            then do :
                assign viXProjectProfileID    = 0
                       viXCostCentreProfileID = 0
                       viXDivisionProfileID   = 0.
                find GL where 
                     GL.SharedSet_ID = viXGLSharedSetID and 
                     GL.GLCode       = t_sApiPostingLineCIJE.tcGLCode
                     no-lock no-error.
                if available GL 
                then do :
                    if GL.GLIsDivisionAccount = true
                    then assign viXDivisionProfileID = GL.DivisionProfile_ID.
                    if GL.GLIsCostCentreAccount = true
                    then assign viXCostCentreProfileID = GL.CostCentreProfile_ID.
                    if GL.GLIsProjectAccount = true
                    then assign viXProjectProfileID = GL.ProjectProfile_ID.
                    if (t_sApiPostingLineCIJE.tcDivisionCode = "":U or 
                        t_sApiPostingLineCIJE.tcDivisionCode = ?) and 
                       viXDivisionProfileID <> 0 and 
                       viXDivisionProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXDivisionProfileID and 
                                  ProfileLink.SharedSet_ID = viXDivisionSharedSetID
                                  no-lock,
                            first Division where 
                                  Division.Division_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign t_sApiPostingLineCIJE.tcDivisionCode = Division.DivisionCode
                                   t_sApiPostingLineCIJE.Division_ID    = Division.Division_ID.                    
                        end. /* or first Profile where */
                    end. /* if viXDivisionProfileID <> 0 and */
                    if (t_sApiPostingLineCIJE.tcCostCentreCode = "":U or 
                        t_sApiPostingLineCIJE.tcCostCentreCode = ?) and 
                       viXCostCentreProfileID <> 0 and 
                       viXCostCentreProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXCostCentreProfileID and 
                                  ProfileLink.SharedSet_ID = viXCostCentreSharedSetID
                                  no-lock,
                            first CostCentre where 
                                  CostCentre.CostCentre_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign t_sApiPostingLineCIJE.tcCostCentreCode = CostCentre.CostCentreCode
                                   t_sApiPostingLineCIJE.CostCentre_ID    = CostCentre.CostCentre_ID.                    
                        end. /* or first Profile where */
                    end. /* if viXCostCentreProfileID <> 0 and */
                    if (t_sApiPostingLineCIJE.tcProjectCode = "":U or 
                        t_sApiPostingLineCIJE.tcProjectCode = ?) and 
                       viXProjectProfileID <> 0 and 
                       viXProjectProfileID <> ? 
                    then do :
                        for first ProfileLink  where 
                                  ProfileLink.Profile_ID = viXProjectProfileID and 
                                  ProfileLink.SharedSet_ID = viXProjectSharedSetID
                                  no-lock,
                            first qaddb.Project where 
                                  qaddb.Project.Project_ID = ProfileLink.ProfileLinkObject_ID 
                                  no-lock
                                  on error undo, throw:
                            assign t_sApiPostingLineCIJE.tcProjectCode = qaddb.Project.ProjectCode
                                   t_sApiPostingLineCIJE.Project_ID    = qaddb.Project.Project_ID.                    
                        end. /* for first Profile where */
                    end. /* if viXProjectProfileID <> 0 and */
                end. /* if available GL */
            end. /* if t_sApiPostingLineCIJE.PostingLineSequence <> 1 */ 
        End. /* For each t_sApiCInvoicePostingCIJE where  */

    end. /* for each t_sApiCInvoiceCIJE where */  
    
    
    /* ============================================== */        
    /* close queries and delete buffer handle objects */
    /* ============================================== */         
    FINALLY:
        
        if valid-handle(vhXQueryPocCtrl)
        then do:
            vhXQueryPocCtrl:QUERY-CLOSE().
            delete object vhXQueryPocCtrl.
        end. /* if valid-handle(vhXQueryPocCtrl) */
        if valid-handle(vhXBufferPocCtrl) 
        then delete object vhXBufferPocCtrl.
        
        if valid-handle(vhXQueryDybsMstr)
        then do:
            vhXQueryDybsMstr:QUERY-CLOSE().
            delete object vhXQueryDybsMstr.
        end. /* if valid-handle(vhXQueryDybsMstr) */
        if valid-handle(vhXBufferDybsMstr) 
        then delete object vhXBufferDybsMstr.
        
        if valid-handle(vhXQueryVdMstr)
        then do:
            vhXQueryVdMstr:QUERY-CLOSE().
            delete object vhXQueryVdMstr.
        end. /* if valid-handle(vhXQueryDybsMstr) */
        if valid-handle(vhXBufferVdMstr) 
        then delete object vhXBufferVdMstr.
        
    END FINALLY.