project QadFinancials > class BCInvoice > method ApiStdMaintainTTWithIntPost

Description

This method is a submethod of ApiStdMaintainTT.
This methods creates a ci posting and mf posting for the cinvoice records


Parameters


icActioninputcharacterThis parameter defines what should happen with the input data : -SAVE: Save the data if there were no errors, -SAVESTORE : Save the data if there were no errors. If there were errors, save as draft. -STORE : Save the data as draft -VALIDATE : Validate the input data, but do no save the data.
bcLstReturninput-outputcharacterChar 4 separated list with the return status of the newly created main-table records. Zero means not errors occurred.
bcLstPrimKeyinput-outputcharacterCharacter 4 separated list with the primary keys of the newly created main-table records. The fields of the primary key are separated with Character 2.
bcLstRowIdinput-outputcharacterChar 4 separated list with the temporarely rowids (negative for new ones) of the newly created main-table records.
olSaveAsDraftoutputlogicalSave As Draft
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoice.ApiStdMaintainTTV01


program code (program9/bcinvoice.p)

assign oiReturnStatus = -98.
empty temp-table tOpenBalanceCI.
empty temp-table tApiStdCInvoiceVat.

for each tApiCInvoiceVat no-lock:
    create tApiStdCInvoiceVat.
    buffer-copy tApiCInvoiceVat to tApiStdCInvoiceVat.
end. /* for each tApiCInvoiceVat no-lock: */

/* Initialise                         */
/* Validate the value for the Company */
/* Replace unknown values             */
assign vlWarningsFound = false.
if tApiCInvoice.Company_ID <> 0 and
   tApiCInvoice.Company_ID <> ? and 
   tApiCInvoice.Company_ID <> viCompanyId
then do :
    assign vcMessage      = trim(substitute(#T-3'The entity (&1) of the supplier invoice must match the current entity (&2).':255(15920)T-3#, string(tApiCInvoice.Company_ID),string(viCompanyId)))
           oiReturnStatus = -1
           bcLstReturn    = bcLstReturn  + chr(4) + string(-1)
           bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
           bcLstRowid     = bcLstRowid   + chr(4) + "*":U
           vlSaveAsDraft  = false.    
    <M-4 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QADFIN-3835':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    Return.
end.
assign tApiCInvoice.Company_ID = viCompanyId.    
if tApiCInvoice.Creditor_ID = ?    then assign tApiCInvoice.Creditor_ID = 0.
if tApiCInvoice.tcCreditorCode = ? then assign tApiCInvoice.tcCreditorCode = "":U.
if tApiCInvoice.CInvoiceType = ?   then assign tApiCInvoice.CInvoiceType = "":U.

/* Create tOpenBalanceCI record that will be used for a call to a submethod */
create tOpenBalanceCI.
assign tOpenBalanceCI.tiCompanyId                  = tApiCInvoice.Company_ID
       tOpenBalanceCI.tcAction                     = icAction 
       tOpenBalanceCI.tcCreditorCode               = tApiCInvoice.tcCreditorCode
       tOpenBalanceCI.tcInvoiceType                = tApiCInvoice.CInvoiceType
       tOpenBalanceCI.tiPeriodYear                 = tApiCInvoice.CInvoicePostingYear
       tOpenBalanceCI.tiPeriodPeriod               = tApiCInvoice.CInvoicePostingPeriod
       tOpenBalanceCI.tcJournalCode                = tApiCInvoice.tcJournalCode
       tOpenBalanceCI.tiInvoiceVoucher             = tApiCInvoice.CInvoiceVoucher
       tOpenBalanceCI.ttInvoiceDate                = tApiCInvoice.CInvoiceDate
       tOpenBalanceCI.ttPostingDate                = tApiCInvoice.CInvoicePostingDate
       tOpenBalanceCI.tcPostingType                = if tApiCInvoice.CInvoiceOriginalDebitTC <> 0 and tApiCInvoice.CInvoiceOriginalDebitTC <> ? then {&POSTINGTYPE-DEBIT} else {&POSTINGTYPE-CREDIT}
       tOpenBalanceCI.tcPostingText                = if tApiCInvoice.CInvoiceDescription = "":U or tApiCInvoice.CInvoiceDescription = ?
                                                     then trim(#T-7'Supplier Invoice Import':40(999890044)T-7#)
                                                     else substring(tApiCInvoice.CInvoiceDescription,1,40,"Character":U)
       tOpenBalanceCI.tdInvoiceAmountTC            = if tApiCInvoice.CInvoiceOriginalDebitTC <> 0 and tApiCInvoice.CInvoiceOriginalDebitTC <> ? then tApiCInvoice.CInvoiceOriginalDebitTC else tApiCInvoice.CInvoiceOriginalCreditTC
       tOpenBalanceCI.tdInvoiceAmountLC            = if tApiCInvoice.CInvoiceOriginalDebitLC <> 0 and tApiCInvoice.CInvoiceOriginalDebitLC <> ? then tApiCInvoice.CInvoiceOriginalDebitLC else tApiCInvoice.CInvoiceOriginalCreditLC
       tOpenBalanceCI.tdInvoiceAmountCC            = if tApiCInvoice.CInvoiceOriginalDebitCC <> 0 and tApiCInvoice.CInvoiceOriginalDebitCC <> ? then tApiCInvoice.CInvoiceOriginalDebitCC else tApiCInvoice.CInvoiceOriginalCreditCC      
       tOpenBalanceCI.tdExchangeRateTCLC           = tApiCInvoice.CInvoiceExchangeRate
       tOpenBalanceCI.tdExchangeRateScale          = tApiCInvoice.CInvoiceRateScale
       tOpenBalanceCI.tdExchangeRateTCCC           = tApiCInvoice.CInvoiceCCRate
       tOpenBalanceCI.tdCCExchangeRateScale        = tApiCInvoice.CInvoiceCCScale
       tOpenBalanceCI.tcTCCurrencyCode             = tApiCInvoice.tcCurrencyCode
       tOpenBalanceCI.ttInvoiceTaxPointDate        = tApiCInvoice.CInvoiceDate
       tOpenBalanceCI.tcProjectCode                = tApiCInvoice.tcProjectCode
       tOpenBalanceCI.tcReasonCode                 = tApiCInvoice.tcReasonCode
       tOpenBalanceCI.tcCostCentreCode             = tApiCInvoice.tcCostCentreCode
       tOpenBalanceCI.tcInvoiceReference           = if tApiCInvoice.CInvoiceReference = "":U or tApiCInvoice.CInvoiceReference = ?
                                                     then substring(trim(string(today)) + " - ":U + trim(string(tOpenBalanceCI.tdInvoiceAmountTC)) + " - ":U + trim(tOpenBalanceCI.tcPostingText),1,22,"Character":U)
                                                     else substring(tApiCInvoice.CInvoiceReference,1,22,"Character":U)
       tOpenBalanceCI.tcInvoiceDescription         = if tApiCInvoice.CInvoiceDescription = "":U or tApiCInvoice.CInvoiceDescription = ?
                                                     then tOpenBalanceCI.tcPostingText
                                                     else substring(tApiCInvoice.CInvoiceDescription,1,40,"Character":U)
       tOpenBalanceCI.tcNormalPaymentConditionCode = tApiCInvoice.tcNormalPaymentConditionCode
       tOpenBalanceCI.tcKey                        = "KEY":U
       tOpenBalanceCI.tcDivisionCode               = tApiCInvoice.tcDivisionCode
       tOpenBalanceCI.tcRoleName                   = tApiCInvoice.tcRoleName
       tOpenBalanceCI.tcInvoiceCommentNote         = tApiCInvoice.CInvoiceCommentNote
       tOpenBalanceCI.tcBLWIGroupCode              = tApiCInvoice.tcBLWIGroupCode.
       
<M-39 run SetRowidConvert
   (input  tApiCInvoice.tc_Rowid (icOldRowid), 
    input  string(rowid(topenbalanceCI)) (icNewRowid), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper > 0 then assign vlWarningsFound = true.
else if viFcReturnSuper < 0 
     then do:
        assign oiReturnStatus = viFcReturnSuper
               olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(oiReturnStatus)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
     end. /* if viFcReturnSuper < 0  */
  
/* Get the AVRCode of the current company (used te retrieve the VAT-defaults */
<Q-5 run CompanyPropertyByBusinessRel (first) (Read) (NoCache)
   (input tOpenBalanceCI.tiCompanyId, (CompanyId)
    input ?, (AddressType)
    output dataset tqCompanyPropertyByBusinessRel) in BCompanyProperty >
find first tqCompanyPropertyByBusinessRel no-error.
if available tqCompanyPropertyByBusinessRel
then do:
    <Q-6 run VatNumberByIdBusRelCntryVat (first) (Read) (NoCache)
       (input ?, (BusinessRelationId)
        input ?, (IdentityCountryId)
        input ?, (IdentityCountryCode)
        input tqCompanyPropertyByBusinessRel.tcVatNumberIdentity, (VatNumberIdentity)
        input tqCompanyPropertyByBusinessRel.tiVatNumber_ID, (VatNumberId)
        input ?, (VatNumberIsActive)
        output dataset tqVatNumberByIdBusRelCntryVat) in BBusinessRelation>
    find first tqVatNumberByIdBusRelCntryVat no-error.
    assign vcShipToTaxZone     = tqCompanyPropertyByBusinessRel.tcTxzTaxZone
           vcShipToCountryCode = tqCompanyPropertyByBusinessRel.tcAddressCountryCode
           vlShipToIsEUCountry = tqCompanyPropertyByBusinessRel.tlIdentityIsEUCountry
           viShipToAddressID     = tqCompanyPropertyByBusinessRel.tiAddress_ID.
end. /* if available tqCompanyPropertyByBusinessRel */

/* Get some values from the creditor: DeliveryType, PercentageLevel, */ 
/* BusRel, AVRCode, Division, PayConditions and BankNumber-info      */
if tApiCInvoice.Company_ID   <> 0      and tApiCInvoice.Company_ID   <> ? and
   tApiCInvoice.tcCreditorCode <> "":U and tApiCInvoice.tcCreditorCode <> ?
then do:
    <Q-8 run CreditorByCreditor (first) (Read) (NoCache)
       (input ?, (CreditorId)
        input tApiCInvoice.Company_ID, (CompanyId)
        input tApiCInvoice.tcCreditorCode, (CreditorCode)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        output dataset tqCreditorByCreditor) in BCreditor >
    find first tqCreditorByCreditor no-error.
    if available tqCreditorByCreditor
    then do:
        assign tOpenBalanceCI.tcBusinessRelationCode = tqCreditorByCreditor.tcBusinessRelationCode
               vcCreditorCode                        = tqCreditorByCreditor.tcCreditorCode
               vlAddressIsTaxInCity                  = tqCreditorByCreditor.tlAddressIsTaxInCity
               vcTxclTaxClass                        = tqCreditorByCreditor.tcTxclTaxCls
               vcTxuTaxUsage                         = tqCreditorByCreditor.tcTxuTaxUsage
               vcShipFromTaxZone                     = (if tqCreditorByCreditor.tcTxzTaxZone <> "":U and 
                                                           tqCreditorByCreditor.tcTxzTaxZone <> ? 
                                                        then tqCreditorByCreditor.tcTxzTaxZone 
                                                        else tqCreditorByCreditor.tcAddressTxzTaxZone)
               vcShipFromCountryCode                 = tqCreditorByCreditor.tcCountryCode
               vlShipFromIsEUCountry                 = tqCreditorByCreditor.tlCountryIsEUCountry
               vcCreditorTaxZone                     = tqCreditorByCreditor.tcTxzTaxZone
               viShipFromAddressID                   = tqCreditorByCreditor.tiAddress_ID.
        /* Get division */
        if (tOpenBalanceCI.tcDivisionCode = "":U or tOpenBalanceCI.tcDivisionCode = ?) and 
           tqCreditorByCreditor.tiDivisionProfile_ID <> ? and tqCreditorByCreditor.tiDivisionProfile_ID <> 0
        then do:
            <Q-10 run GetDivisionFromProfile (all) (Read) (NoCache)
               (input tApiCInvoice.Company_ID, (CompanyId)
                input tqCreditorByCreditor.tiDivisionProfile_ID, (DivisionProfileId)
                output dataset tqDivisionFromProfile) in BProfile >
            find first tqDivisionFromProfile no-error.
            if available tqDivisionFromProfile
            then assign tOpenBalanceCI.tcDivisionCode = tqDivisionFromProfile.tcDivisionCode.
        end.
        /* Get the Payment-conditions from the Creditor */
        if tApiCInvoice.tcNormalPaymentConditionCode   = "":U or tApiCInvoice.tcNormalPaymentConditionCode   = ? 
        then assign tOpenBalanceCI.tcNormalPaymentConditionCode   = (if tOpenBalanceCI.tcNormalPaymentConditionCode   = "":U or tOpenBalanceCI.tcNormalPaymentConditionCode   = ? then tqCreditorByCreditor.tcNormalPaymentConditionCode else tOpenBalanceCI.tcNormalPaymentConditionCode).
    end.
end.

/* Calculate due dates */
<M-12 run ApiStdMaintainTTWithIntPostDates (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>                             
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid    = bcLstRowid   + chr(4) + "*":U.
        return.
    end.
    else assign vlWarningsFound = true.
end.

/* Call a submethod that will create the creditor invoices                */
/* Result: CInvoice, CInvoicePosting and CInvoiceMovement records created */
<M-13 run CreateCInvoices
   (input-output tOpenBalanceCI (tOpenBalanceCI), 
    input  0 (iiBJournalEntryId), 
    input  0 (iiPostingId), 
    input  ? (icPONbrInv), 
    output vcListNewInvoices (ocNewRecordInfo), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
    end.
    else assign vlWarningsFound = true.
end.

/* Search the creditor invoice that you have just created */
assign viListNewInvoices = integer(entry(2,vcListNewInvoices)) no-error.
if error-status:error = false
then find first tCInvoice where
                tCInvoice.CInvoice_ID = viListNewInvoices
                no-error.
if error-status:error = true or 
   not available tCInvoice
then do :
    assign vcMessage      = trim(substitute(#T-14'There was an internal error on the supplier invoice integration. The call to method '&1' (method that initiates the invoice) returned unexpected information.':255(15926)T-14#, "CreateCInvoices":U)) + chr(10) + 
                            trim(substitute(#T-15'Data returned: &1.':255(14497)T-15#,viListNewInvoices)) 
           oiReturnStatus = -1
           bcLstReturn    = bcLstReturn  + chr(4) + string(-1)
           bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
           bcLstRowid     = bcLstRowid   + chr(4) + "*":U
           vlSaveAsDraft  = false.
    if error-status:error         = true and 
       error-status:num-messages >= 1
    then assign vcMessage = vcMessage + chr(10) + ERROR-STATUS:GET-MESSAGE(1).
    <M-16 run SetMessage
       (input  vcMessage (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QADFIN-3836':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    Return.
end. /* if error-status:error = true or  */

/* By calling CreateCInvoices, the ShipTo-fields on tCInvoice have been set to their default values.     */
/* But if tApiCInvoice holds filled ShipTo-fields and they differ from the ones on tCInvoice then we     */
/* have to make sure that all ShipTo-fields on tCInvoice are overwritten with the ones from tApiCInvoice */
/* and that the same is done for the local-data-items about the ShipTo                                   */
if tApiCInvoice.tcShipToBusinessRelationCode <> "":U and 
   tApiCInvoice.tcShipToBusinessRelationCode <> ?    and 
   tApiCInvoice.tcShipToAddressStreet1       <> "":U and 
   tApiCInvoice.tcShipToAddressStreet1       <> ?    and 
   (tApiCInvoice.tcShipToBusinessRelationCode <> tCInvoice.tcShipToBusinessRelationCode or
    tApiCInvoice.tcShipToAddressStreet1       <> tCInvoice.tcShipToAddressStreet1)
then assign tCInvoice.tcShipToBusinessRelationCode = tApiCInvoice.tcShipToBusinessRelationCode 
            tCInvoice.tcShipToCountryCode          = tApiCInvoice.tcShipToCountryCode          
            tCInvoice.tcOwnVatNumberCountryCode    = tApiCInvoice.tcOwnVatNumberCountryCode    
            tCInvoice.tlShipToCountryIsEUCountry   = tApiCInvoice.tlShipToCountryIsEUCountry   
            tCInvoice.tcShipToAddressTypeCode      = tApiCInvoice.tcShipToAddressTypeCode      
            tCInvoice.CInvoiceOwnVatNumber         = tApiCInvoice.CInvoiceOwnVatNumber         
            tCInvoice.tcShipToAddressStreet1       = tApiCInvoice.tcShipToAddressStreet1
            tCInvoice.tcShipToAddressStreet2       = tApiCInvoice.tcShipToAddressStreet2
            tCInvoice.tcShipToAddressStreet3       = tApiCInvoice.tcShipToAddressStreet3
            tCInvoice.tcShipToAddressZip           = tApiCInvoice.tcShipToAddressZip
            tCInvoice.tcShipToAddressCity          = tApiCInvoice.tcShipToAddressCity
            tCInvoice.tlShipToAddressIsTaxInCity   = tApiCInvoice.tlShipToAddressIsTaxInCity
            tCInvoice.tcShipToTaxZone              = tApiCInvoice.tcShipToTaxZone              
            tCInvoice.tiShipToAddressID            = tApiCInvoice.tiShipToAddressID
            tCInvoice.ShipToAddress_ID             = tApiCInvoice.ShipToAddress_ID     
            tCInvoice.tcShipToAddressState         = tApiCInvoice.tcShipToAddressState 
            tCInvoice.tcVatCurrencyCode            = tApiCInvoice.tcVatCurrencyCode
            tCInvoice.VatCurrency_ID               = tApiCInvoice.VatCurrency_ID
            vcShipToTaxZone                        = tApiCInvoice.tcShipToTaxZone
            vcShipToCountryCode                    = tApiCInvoice.tcShipToCountryCode
            vlShipToIsEUCountry                    = tApiCInvoice.tlShipToCountryIsEUCountry
            viShipToAddressID                      = tApiCInvoice.ShipToAddress_ID.

/* By calling CreateCInvoices, a single tCInvoiceBank-record has been created                                      */
/* But if there are tApiCInvoiceBank-records for the tApiCInvoice then we should use the data on                   */
/* the tApiCInvoiceBank-records and not the default ones that were created in CreateCInvoices                      */
/* Note: this is not just about the CInvoiceBank-records but also about the underlying CInvoiceBankPayCode-records */
if can-find (first tApiCInvoiceBank where 
                   tApiCInvoiceBank.tc_ParentRowid  = tApiCInvoice.tc_Rowid and 
                   tApiCInvoiceBank.tc_status      <> "D":U)
then do :
    /* Remove the default tCInvoiceBank-records that were created by CreateCInvoices */
    for each tCInvoiceBank where 
             tCInvoiceBank.tc_ParentRowid  = tCInvoice.tc_Rowid and 
             tCInvoiceBank.tc_Status       = "N":U : 
        for each tCInvoiceBankPayCode where 
                 tCInvoiceBankPayCode.tc_ParentRowid = tCInvoiceBank.tc_Rowid and 
                 tCInvoiceBankPayCode.tc_Status      = "N":U : 
            delete tCInvoiceBankPayCode .
        end. /* for each tCInvoiceBankPayCode where */
        delete tCInvoiceBank.
    end. /* for each tCInvoiceBank where */
    /* Create new tCInvoiceBank-records based upon the tApiCInvoiceBank-records */
    for each tApiCInvoiceBank where
             tApiCInvoiceBank.tc_ParentRowid = tApiCInvoice.tc_Rowid
             by tApiCInvoiceBank.tc_Rowid :
        <M-113 run AddDetailLine
           (input  'CInvoiceBank':U (icTable), 
            input  tCInvoice.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do: 
                assign olSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid    = bcLstRowid   + chr(4) + "*":U.
                return.
            end. /* if oiReturnStatus < 0 */
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */
        buffer-copy tApiCInvoiceBank
             except tApiCInvoiceBank.CInvoiceBank_ID
                    tApiCInvoiceBank.CInvoice_ID
                    tApiCInvoiceBank.tc_ParentRowid
                    tApiCInvoiceBank.tc_Rowid
                    tApiCInvoiceBank.tc_Status
                 to tCInvoiceBank.
        for each tApiCInvoiceBankPayCode where
                 tApiCInvoiceBankPayCode.tc_ParentRowid = tApiCInvoiceBank.tc_Rowid
                 by tApiCInvoiceBankPayCode.tc_Rowid :
            <M-64 run AddDetailLine
               (input  'CInvoiceBankPayCode':U (icTable), 
                input  tCInvoiceBank.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0
            then do:
                assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0
                then do: 
                    assign olSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                           bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                           bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
                           bcLstRowid    = bcLstRowid   + chr(4) + "*":U.
                    return.
                end. /* if oiReturnStatus < 0 */
                else assign vlWarningsFound = true.
            end. /* if viFcReturnSuper <> 0 */
            buffer-copy tApiCInvoiceBankPayCode
                 except tApiCInvoiceBankPayCode.CInvoiceBankPayCode_ID
                        tApiCInvoiceBankPayCode.CInvoiceBank_ID
                        tApiCInvoiceBankPayCode.PayFormatCode_ID
                        tApiCInvoiceBankPayCode.PayFormatGroup_ID                    
                        tApiCInvoiceBankPayCode.tc_ParentRowid
                        tApiCInvoiceBankPayCode.tc_Rowid
                        tApiCInvoiceBankPayCode.tc_Status
                     to tCInvoiceBankPayCode.
        end. /* for each tApiCInvoiceBankPayCode where */
    end. /* for each tApiCInvoiceBank where */
end. /* if can-find (first tApiCInvoiceBank where  */

/* Before continuing, we copy the primary-key-fields of the main table to the input parameters */
/* This is just to hold it there so it can be assigned to ocPrimKey if the save succeeds. This */
/* is done because tCInvoice is no longer available after the save. We use ocRowid in stead    */
assign vcRowId                  = tCInvoice.tc_Rowid
       tCInvoice.tcReasonCode   = tApiCInvoice.tcReasonCode
       tApiCInvoice.CInvoice_ID = tCInvoice.CInvoice_ID.
assign tCInvoice.CInvoiceIsTaxable = tApiCInvoice.CInvoiceIsTaxable.

/* Handling the taxes */
if /* The taxes where passed as input */ 
   can-find(first tApiStdCInvoiceVat where
                  tApiStdCInvoiceVat.tc_ParentRowid = tApiCInvoice.tc_Rowid)     OR
   /* The caller marked the invoice as taxable but did not pass any taxes so the default ones should be used */ 
   (tCInvoice.CInvoiceIsTaxable = true and 
    not can-find (first tApiStdCInvoiceVat) )
then do:      
    /* Remove previously created default records */
    for each tCInvoiceVat where
             tCInvoiceVat.tc_ParentRowid = tCInvoice.tc_Rowid:
        if tCInvoiceVat.tc_Status = "N"
        then delete tCInvoiceVat.
        else assign tCInvoiceVat.tc_Status = "D".     
    end. /* for each tCInvoiceVat where */  
    for each tCInvoiceWHT where
             tCInvoiceWHT.tc_ParentRowid = tCInvoice.tc_Rowid:
        if tCInvoiceWHT.tc_Status = "N"
        then delete tCInvoiceWHT.
        else assign tCInvoiceWHT.tc_Status = "D".     
    end. /* for each tCInvoiceWHT where */  
    /* If the caller marked the invoice as taxable but did not pass any taxes meaning the default taxes should be used */ 
    /* Then we will first get the default taxes by calling DefaultValuesTax() that stores these taxes in tCInvoiceVat and then copy tCInvoiceVat into tApiStdCInvoiceVat */
    if tCInvoice.CInvoiceIsTaxable = true and 
        not can-find (first tApiStdCInvoiceVat) 
    then do :
        assign vdInvoiceAmountTC = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or 
                                      tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                   then tCInvoice.CInvoiceOriginalCreditTC 
                                   else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or 
                                           tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} 
                                        then tCInvoice.CInvoiceOriginalDebitTC 
                                        else 0.
        <M-40 run DefaultValuesTax
           (input  tCInvoice.tc_Rowid (icCInvoiceRowid), 
            input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
            input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
            input  tCInvoice.CInvoiceReference (icDocumentReference), 
            input  tCInvoice.CInvoiceTaxPointDate (itTaxPointDate), 
            input  vcShipFromTaxZone (icShipFromTaxZone), 
            input  vcShipToTaxZone (icShipToTaxZone), 
            input  vcTxclTaxClass (icTxclTaxClass), 
            input  vcTxuTaxUsage (icTxuTaxUsage), 
            input  vdInvoiceAmountTC (idInvoiceAmountTC), 
            input  tCInvoice.CInvoiceExchangeRate (idExchangeRate), 
            input  tCInvoice.CInvoiceExchangeRate (idExchangeRateScale), 
            input  tCInvoice.CInvoiceRateScale (icPaymentConditionCode), 
            input  tCInvoice.CInvoicePostingDate (itPostingDate), 
            input  tCInvoice.CInvoiceIsTaxable (ilTaxable), 
            input  tqCreditorByCreditor.tlCreditorIsWHT (ilCreditorIsWht), 
            input  tCInvoice.CInvoiceType (icInvoiceType), 
            input  vlAddressIsTaxInCity (ilShipToAddressIsTaxInCity), 
            input  tCInvoice.CInvoiceVatExchangeRate (idVatExchangeRate), 
            input  tCInvoice.CInvoiceVatRateScale (idVatRateScale), 
            input  tCInvoice.CInvoiceIsLogisticMatching (ilIsLogisticMatching), 
            output vcDummy (ocTxenvTaxEnv), 
            input  tCInvoice.CInvoiceIsTaxExcluded (ilCInvoiceIsTaxExcluded), 
            input  tCInvoice.CInvoiceDate (itCInvoiceDate), 
            input  tCInvoice.CInvoiceCCRate (idCCExchangeRate), 
            input  tCInvoice.CInvoiceCCScale (idCCExchangeRateScale), 
            input  viShipToAddressID (iiShipToAddressID), 
            input  viShipFromAddressID (iiShipFromAddressID), 
            input  tCInvoice.tcCreditorCode (icCreditorCode), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>   
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do: 
                assign vcMessage     = trim(substitute(#T-61'An error (&1) occurred when saving the tax details of the new supplier invoice. See other messages for further details.':255(425047688)T-61#,string(viFcReturnSuper)))
                       olSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid    = bcLstRowid   + chr(4) + "*":U.
                <M-70 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-720846':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                return.
            end. /*  if oiReturnStatus < 0 */
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */
        /* Copy tCInvoiceVat into tApiStdCInvoiceVat as that will be used by the next call to ApiStdMaintainTTCreateVat */
        for each tCInvoiceVat :
            create tApiStdCInvoiceVat.
            buffer-copy tCInvoiceVat to tApiStdCInvoiceVat.
            /* Remove previously created records */
            if tCInvoiceVat.tc_Status = "N"
            then delete tCInvoiceVat.
            else assign tCInvoiceVat.tc_Status = "D":U.
        end. /* for each tCInvoiceVat  */
    end. /*  if tCInvoice.CInvoiceIsTaxable = true and  */
    /* Call the method that will Loop through the tApiStdCInvoiceVat records and create tCInvoiceVat for it and that will call CreateCIPosting so the posting gets corrected based upon the taxes */
    <M-65 run ApiStdMaintainTTCreateVatWHT
       (input  tCInvoice.tc_Rowid (icInvoiceRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.
end. /* if /* The taxes where passed as input */ */
else do :   
    /* If invoice is non-taxable and the caller indicated to skip the call to GTM (by assigning tApiCInvoice.tcBLWIGroupCode) and there is already a */
    /* tCInvoiceVat representing a non-taxable VAT then we do not need to take further actions here as all has already been done in CreateCInvoices  */
    if tCInvoice.CInvoiceIsTaxable  <> false                          or 
       tApiCInvoice.tcBLWIGroupCode <> {&SKIPGTMCALLFORNONTAXOPENBAL} or 
       not can-find (first tCInvoiceVat where
                           tCInvoiceVat.tc_ParentRowid = tCInvoice.tc_Rowid and 
                           tCInvoiceVat.tcVatCode      = "00000000":U       and 
                           tCInvoiceVat.tcVatInOut     = {&VATINOUT-INPUT}  and 
                           tCInvoiceVat.tc_Status     <> "D":U)
    then do :
        <M-30 run DefaultValuesTax
           (input  tCInvoice.tc_Rowid (icCInvoiceRowid), 
            input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
            input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
            input  tCInvoice.CInvoiceReference (icDocumentReference), 
            input  tCInvoice.CInvoiceTaxPointDate (itTaxPointDate), 
            input  vcShipFromTaxZone (icShipFromTaxZone), 
            input  vcShipToTaxZone (icShipToTaxZone), 
            input  vcTxclTaxClass (icTxclTaxClass), 
            input  vcTxuTaxUsage (icTxuTaxUsage), 
            input  if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} then tCInvoice.CInvoiceOriginalCreditTC else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} then tCInvoice.CInvoiceOriginalDebitTC else 0 (idInvoiceAmountTC), 
            input  tCInvoice.CInvoiceExchangeRate (idExchangeRate), 
            input  tCInvoice.CInvoiceExchangeRate (idExchangeRateScale), 
            input  tCInvoice.CInvoiceRateScale (icPaymentConditionCode), 
            input  tCInvoice.CInvoicePostingDate (itPostingDate), 
            input  tCInvoice.CInvoiceIsTaxable (ilTaxable), 
            input  tqCreditorByCreditor.tlCreditorIsWHT (ilCreditorIsWht), 
            input  tCInvoice.CInvoiceType (icInvoiceType), 
            input  vlAddressIsTaxInCity (ilShipToAddressIsTaxInCity), 
            input  tCInvoice.CInvoiceVatExchangeRate (idVatExchangeRate), 
            input  tCInvoice.CInvoiceVatRateScale (idVatRateScale), 
            input  tCInvoice.CInvoiceIsLogisticMatching (ilIsLogisticMatching), 
            output vcDummy (ocTxenvTaxEnv), 
            input  tCInvoice.CInvoiceIsTaxExcluded (ilCInvoiceIsTaxExcluded), 
            input  tCInvoice.CInvoiceDate (itCInvoiceDate), 
            input  tCInvoice.CInvoiceCCRate (idCCExchangeRate), 
            input  tCInvoice.CInvoiceCCScale (idCCExchangeRateScale), 
            input  viShipToAddressID (iiShipToAddressID), 
            input  viShipFromAddressID (iiShipFromAddressID), 
            input  tCInvoice.tcCreditorCode (icCreditorCode), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do: 
                assign vcMessage     = trim(substitute(#T-31'An error (&1) occurred when saving the tax of the new supplier invoices. See other messages for further details.':255(15927)T-31#,string(viFcReturnSuper)))
                       olSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid    = bcLstRowid   + chr(4) + "*":U.
                <M-32 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-6639':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                return.
            end. /* if oiReturnStatus < 0 */ 
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */ 
        
        /* Call a submethod that will create the PostingVat records */
        find first tCInvoicePosting where
                   tCInvoicePosting.CInvoice_ID = tCInvoice.CInvoice_ID
                   no-error.
        if available tCinvoiceposting and 
           (tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE})
        then do:  
             assign vcPostingTcRowid = "":U
                    vdAmountTC       = if tCInvoice.CInvoicetype = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then tCInvoice.CInvoiceOriginalDebitTC else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tCInvoice.CInvoiceOriginalCreditTC else 0
                    vdAmountLC       = if tCInvoice.CInvoicetype = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then tCInvoice.CInvoiceOriginalDebitLC else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tCInvoice.CInvoiceOriginalCreditLC else 0
                    vdAmountCC       = if tCInvoice.CInvoicetype = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then tCInvoice.CInvoiceOriginalDebitCC else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} or tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION} then tCInvoice.CInvoiceOriginalCreditCC else 0
                    vdDebitAmountTC  = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then 0 else vdAmountTC 
                    vdCreditAmountTC = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then vdAmountTC else 0.
            if viBJournalEntryCIID <> 0 
            then do :
                if valid-handle(vhBJournalEntryCIInst) = false
                then do :
                    <I-20 {bFcOpenInstance
                         &CLASS           = "BJournalEntry"}>
                end.
                <M-21 run PassRowIdBasedOnPostingIdInInstance
                   (input  tCInvoicePosting.Posting_ID (iiPostingId), 
                    output vcPostingTcRowid (ocPostingRowId), 
                    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
            end. /* if viBJournalEntryCIID <> 0 */
            assign viControlGLProfileId = if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} or tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICECORRECTION} then tqCreditorByCreditor.tiInvControlGLProfile_ID else tqCreditorByCreditor.tiCnControlGLProfile_ID.
            <Q-33 run ProfileLinkByGL (all) (Read) (NoCache)
               (input viControlGLProfileId, (GlProfileId)
                input viCompanyId, (CompanyId)
                output dataset tqProfileLinkByGL) in BProfile >
            find first tqProfileLinkByGL no-error.
            <M-34 run CreateCIPostingTax
               (input  vcPostingTcRowid (icRowId), 
                input  tCInvoicePosting.Posting_ID (iiPostingId), 
                input  tCInvoice.CInvoiceTaxPointDate (itTaxPointDate), 
                input  tCInvoice.tc_Rowid (icCInvoiceRowId), 
                input  tCInvoice.CInvoiceCIText (icCIText), 
                input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
                input  tCInvoice.CInvoiceCurrency_ID (iiCurrencyId), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
                input  tCInvoice.CInvoiceExchangeRate (idExchangeRate), 
                input  tCInvoice.CInvoiceRateScale (idExchangeRateScale), 
                input  vdDebitAmountTC (idVatFullDebitTC), 
                input  vdCreditAmountTC (idVatFullCreditTC), 
                input  (if tCInvoice.CInvoiceType = {&INVOICETYPE-INVOICE} then {&POSTINGTYPE-DEBIT} else if tCInvoice.CInvoiceType = {&INVOICETYPE-CREDITNOTE} then {&POSTINGTYPE-CREDIT} else '':U) (icCInvoiceType), 
                input  tCInvoice.tcDivisionCode (icDivisionCode), 
                input  tCInvoice.tcVatCurrencyCode (icVatCurrencyCode), 
                input  tCInvoice.VatCurrency_ID (iiVatCurrencyId), 
                input  tCInvoice.CInvoiceVatExchangeRate (idVatExchangeRate), 
                input  tCInvoice.CInvoiceVatRateScale (idVatExchangeRateScale), 
                input  vcShipFromCountryCode (icShipFromCountryCode), 
                input  vcShipToCountryCode (icShipToCountryCode), 
                input  vlShipFromIsEUCountry (ilShipFromCountryIsEUCountry), 
                input  vlShipToIsEUCountry (ilShipToCountryIsEUCountry), 
                input  vcTxclTaxClass (icTxclTaxCls), 
                input  vcTxuTaxUsage (icTxuTaxUsage), 
                input  vcCreditorTaxZone (icCreditorTxzTaxZone), 
                input  vcShipToTaxZone (icShipToTxzTaxZone), 
                input  ? (icTxenvTaxEnv), 
                input  tCInvoice.CInvoiceVoucher (iiVoucher), 
                input  tCInvoice.tcNormalPaymentConditionCode (icPaymentCondition), 
                input  vlIsCompanyTaxInCityCI (ilIsTaxInCityFromShipTo), 
                input  if available tqProfileLinkByGL then tqprofileLinkByGL.tcGLCode else ? (icControlGLCode), 
                input  tCInvoice.Creditor_ID (iiMasterId), 
                input  tCInvoice.CInvoice_ID (iiTransactionId), 
                input  ? (icPoNbr), 
                input  tCInvoice.CInvoiceIsTaxable (ilIsTaxable), 
                input  tCInvoice.tcCostCentreCode (icCostCentreCode), 
                input  tCInvoice.tcProjectCode (icProjectCode), 
                input  viShipFromAddressID (iiShipFromAddressId), 
                input  viShipToAddressID (iiShipToAddressId), 
                input  tCInvoice.CInvoiceIsLogisticMatching (ilIsLogisticMatching), 
                input  0 (idInvoiceAmountTC), 
                input  ? (icShipFromTaxIDFeder), 
                input  ? (icShipFromTaxIDState), 
                input  ? (icShipFromTaxIDMisc1), 
                input  ? (icShipFromTaxIDMisc2), 
                input  ? (icShipFromTaxIDMisc3), 
                input  ? (icShipToTaxIDFeder), 
                input  ? (icShipToTaxIDState), 
                input  ? (icShipToTaxIDMisc1), 
                input  ? (icShipToTaxIDMisc2), 
                input  ? (icShipToTaxIDMisc3), 
                input  ? (iiShipToTaxDeclaration), 
                input  ? (itInvoiceDate), 
                input  ? (idCCExchangeRate), 
                input  ? (idCCExchangeRateScale), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
            if viFcReturnSuper <> 0
            then do:
                assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0
                then do: 
                    assign vcMessage     = trim(substitute(#T-35'An error (&1) occurred when saving the tax of the new supplier invoices. See other messages for further details.':255(15927)T-35#,string(viFcReturnSuper)))
                           bcLstReturn   = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                           bcLstPrimKey  = bcLstPrimKey + chr(4) + "*":U
                           bcLstRowid    = bcLstRowid   + chr(4) + "*":U 
                           vlSaveAsDraft = icAction <> {&DAEMONACTION-SAVE}.
                    <M-36 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  '':U (icFieldName), 
                        input  '':U (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  '':U (icRowid), 
                        input  'QadFin-6640':U (icFcMsgNumber), 
                        input  '' (icFcExplanation), 
                        input  '' (icFcIdentification), 
                        input  '' (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
                    return.
                end.
                else assign vlWarningsFound = true.
            end. /* if viFcReturnSuper <> 0 */
        end. /* if available tCinvoiceposting and */
    end. /* if tCInvoice.CInvoiceIsTaxable  <> false                          or  */
end. /* Not if /* The taxes where passed as input */ */

/* Add the CI-PO details */
for each tApiCInvoicePO where
         tApiCInvoicePO.tc_ParentRowid = tApiCInvoice.tc_Rowid
         by tApiCInvoicePO.tc_Rowid:
    <M-71 run AddDetailLine
       (input  'CInvoicePO':U (icTable), 
        input  tCInvoice.tc_Rowid (icParentRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper >= 0 
    then do:
        <M-98 run SetRowidConvert
           (input  tApiCInvoicePO.tc_Rowid (icOldRowid), 
            input  tCInvoicePO.tc_Rowid (icNewRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    end.
    if viFcReturnSuper > 0 
    then assign vlWarningsFound = true.
    if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 
    then do :
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        Return.
    end.
    buffer-copy tApiCInvoicePO
         except tApiCInvoicePO.CInvoicePO_ID  tApiCInvoicePO.CInvoice_ID
                tApiCInvoicePO.tc_ParentRowid tApiCInvoicePO.tc_Rowid
                tApiCInvoicePO.tc_Status
             to tCInvoicePO.
end. /* for each tApiCInvoicePO where */

/* When the ReasonCode implicates a Financial-Matching (not APMatching!) then we create here the MF-posting  */
if tCInvoice.CInvoiceIsLogisticMatching  = false and 
   tCInvoice.CInvoiceIsInitialStatus     = false and 
   tCInvoice.CInvoiceAllocationStatus   <> "":U  and 
   tCInvoice.CInvoiceAllocationStatus   <> ?     and 
   tCInvoice.CInvoiceAllocationStatus   <> {&ALLOCSTATUS-NOALLOC}   
then do :
    /* Get the correct JournalCode to use for the MF-posting */
    <M-317 run GetMFJournal
       (input  tCInvoice.Company_ID (iiCompanyId), 
        input  tCInvoice.CInvoiceAllocationStatus (icReasonAllocationStatus), 
        output vcMFJournalCode (ocMFJournalCode), 
        output vcDummy (ocLayerTypeCode), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
    /* Get the proper SafDefaults to be used on the MF-Posting */   
    if not can-find (first tDefaultSafsCI) and 
       tCInvoice.Creditor_ID <> 0 and 
       tCInvoice.Creditor_ID <> ?
    then do :
        <Q-86 run CreditorSafDefaultForCIDef (all) (Read) (Cache)
           (input tCInvoice.Creditor_ID, (CreditorId)
            output dataset tqCreditorSafDefaultForCIDef) in BCreditor>
        for each tqCreditorSafDefaultForCIDef where
                 tqCreditorSafDefaultForCIDef.tiCreditor_ID = tCInvoice.Creditor_ID :
            create tDefaultSafsCI.
            assign tDefaultSafsCI.tcSafCode        = tqCreditorSafDefaultForCIDef.tcSafCode
                   tDefaultSafsCI.tcSafConceptCode = tqCreditorSafDefaultForCIDef.tcSafConceptCode.
        end. /* for each tqCreditorSafDefaultForCIDef where .. */
    end. /* if not can-find (first tDefaultSafsCI) and  */
    empty temp-table tAccount.
    /* Call method that will create the actual posting and associated actions */
    <M-96 run CreateMFPosting
       (input  tCInvoice.CInvoice_ID (iiCInvoiceId), 
        input  tCInvoice.CInvoicePostingYear (iiPeriodYear), 
        input  tCInvoice.CInvoicePostingPeriod (iiPeriodPeriod), 
        input  tCInvoice.CInvoicePostingDate (itPostingDate), 
        input  tCInvoice.Creditor_ID (iiCreditorId), 
        input  tCInvoice.tcCreditorCode (icCreditorCode), 
        input  tCInvoice.CInvoiceType (icCInvoiceType), 
        input  tCInvoice.tcDivisionCode (icDivisionCode), 
        input  vcMFJournalCode (icJournalCode), 
        input  0 (iiVoucher), 
        input  (if tCInvoice.CInvoiceOriginalDebitTC = 0 or tCInvoice.CInvoiceOriginalDebitTC = ? then tCInvoice.CInvoiceOriginalCreditTC else tCInvoice.CInvoiceOriginalDebitTC) (idInvoiceAmountTC), 
        input  tCInvoice.tcCurrencyCode (icCurrencyCode), 
        input  tCInvoice.CInvoiceDescription (icMFText), 
        input  tCInvoice.tcCostCentreCode (icCostCentreCode), 
        input  tCInvoice.tcProjectCode (icProjectCode), 
        input  tCInvoice.CInvoiceDescription (icDescription), 
        input  tCInvoice.CInvoiceExchangeRate (idExchangeRate), 
        input  tCInvoice.CInvoiceRateScale (idExchangeRateScale), 
        input  false (ilIsLogisticMatching), 
        input  tCInvoice.tcBusinessRelationCode (icBusinessRelationCode), 
        input  '':U (icBusinessRelationICCode), 
        input  tCInvoice.tc_Rowid (icCInvoiceRowid), 
        input  tCInvoice.CInvoiceReference (icCInvoiceReference), 
        input  false (ilGetDefaultSafs), 
        input  tAccount (tAccount), 
        input  tCInvoiceVat (tApiCInvoiceVat), 
        input  tDefaultSafsCI (tDefaultSafsCI), 
        input  false (ilHandleCommunicationWithJE), 
        input  tCInvoice.CInvoiceIsTaxExcluded (ilCInvoiceIsTaxExcluded), 
        input  tCInvoice.CInvoiceDate (itCInvoiceDate), 
        input  tCInvoice.CInvoiceCCRate (idCCExchangeRate), 
        input  tCInvoice.CInvoiceCCScale (idCCExchangeRateScale), 
        input-output viMFPostingID (biPostingId), 
        output vcDummy (ocCInvoiceAllocationStatus), 
        output viDummy (oiBJournalEntryId), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
end. /* if tCInvoice.CInvoiceIsLogisticMatching  = false and */

/* This is where it ends if only SaveAsDraft is needed */
if vlSaveAsDraft
then do:
    assign oiReturnStatus = -1
           bcLstReturn    = bcLstReturn  + chr(4) + "-1":U
           bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
           bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
    Return.
end. /* if vlSaveAsDraft */

/* Add method which can be implemented to set some defaults */
<M-79 run ApiStdMaintainTTDefaulting  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
    end.
    else assign vlWarningsFound = true.
end. /* if viFcReturnSuper <> 0 */

/* Validate the data and performa AddUpd */
<M-27 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
    end.
    else assign vlWarningsFound = true.
end. /* if viFcReturnSuper <> 0 */

<M-26 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
    end.
    else assign vlWarningsFound = true.
end. /* if viFcReturnSuper <> 0 */


/* Save the data */
if icAction <> {&DAEMONACTION-VALIDATE}
then do:
    <M-11 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
    /* Storing the instance-data in the output of ApiMaintainByDataSetWithOutput - this has to be done after the data is saved as in PreSave the final voucher of the invoice is assigned */
    if vlReturnDatasetCI
    then do:
        <M-37 run GetXmlRepresentationDataset
           (input  '' (icObjectRowId), 
            input  false (ilHeaderOnly), 
            input  0 (iiPriority), 
            output vhTempDS by-reference (ozXMLDataset), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do:
                assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
                return.
            end. /* if oiReturnStatus < 0 */
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */       
        /* Make a deep-copy of the dataset to prevent any further updates in the class tables from being reflected into the published dataset of ApiMaintainByDataSetWithOutput. This construction is needed as when we would use "vhDatasetToReturnCI by-reference" as output of GetXmlRepresentationDataset then the output-dataset of ApiMaintainByDataSetWithOutput is empty */ 
        vhDatasetToReturnCI:copy-dataset (vhTempDS, true, false, false, "", false, "").
        delete object vhTempDS no-error.
        /* Get the key fields */
        assign vcTableName = "".
        <M-38 run GetKeyFields
           (input-output vcTableName (bcTableName), 
            output vcPrimKeyCI (ocPrimaryKey), 
            output vcDummy (ocAlternateKey), 
            output vcDummy (ocObjectID), 
            output vcDummy (ocObjectStatus), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do:
                assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
                return.
            end. /* if oiReturnStatus < 0 */
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */
    end. /* if vlReturnDatasetCI */
end. /* if icAction <> {&DAEMONACTION-VALIDATE} */

/* Indicate that everything was saved correctly */
/* Set return status = OK                       */
assign bcLstReturn    = bcLstReturn  + chr(4) + (if vlWarningsFound = true then "1":U else "0":U)
       bcLstPrimKey   = bcLstPrimKey + chr(4) + string(tApiCInvoice.CInvoice_ID)
       bcLstRowid     = bcLstRowid   + chr(4) + vcRowID
       oiReturnStatus = (if vlWarningsFound = true then 1 else 0).