project QadFinancials > class BDInvoice > method CreateDInvoices

Description

Create CreateDInvoices :
create dinvoice
create di posting, cinvoicemovement
update balance and status
create DInvoiceposting


Parameters


tDIOpenBalanceinput-outputtemp-tabletemp-table used for the creation of Debtor-Opening-Balances
iiBJournalEntryIdinputintegerInstance of BJournalEntry
iiPostingIdinputintegerPosting ID
ocNewRecordInfooutputcharacternew record information
rowid, dinvoice_id, rowid, dinvoice_id
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BBankEntry.AdditionalUpdatesInvPrePayments
method BDDocument.AdditionalUpdatesPostingPrePayment
method BDInvoice.ApiCreateDebtorInvoicesV01
method BDInvoice.ApiCreateDInvoicesFinCharge
method BDInvoice.ApiStdMaintainTTWithIntPost
method BDInvoice.CreateDInvoicesDeduction
method BDInvoice.StdMaintainTTWithIntPost
method BOpenItemAdjustment.OIAdjCommitSubDInvoice
method BSelfBill.ApiCreateInvPrepayPaymentSub


program code (program3/bdinvoice.p)

/* replace unknown value */
if iiBJournalEntryId = ? then assign iiBJournalEntryId = 0.
if iiPostingId = ? then assign iiPostingId = 0.

/* exception handling */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

/* validate input parameters */
<M-1 run CreateDInvoicesValidate
   (input-output tDIOpenBalance (tDIOpenBalance), 
    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    
if viFcReturnSuper <> 0 
then assign viLocalReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0 
then do:
    assign oiReturnStatus = viLocalReturnStatus.
    return.
end.

CREATEBLOCK :
for each tDIOpenBalance where 
         tDIOpenBalance.tcDebtorCode <> "":U:
         
    /* create the invoice */
    <M-2 run AddDetailLine (input  'DInvoice':U (icTable), 
                            input  '':U (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                            
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.

    assign tDInvoice.Company_ID                     = tDIOpenBalance.tiCompanyId
           tDInvoice.DInvoicePostingPeriod          = tDIOpenBalance.tiPeriodPeriod
           tDInvoice.DInvoicePostingYear            = tDIOpenBalance.tiPeriodYear
           tDInvoice.tcDebtorCode                   = tDIOpenBalance.tcDebtorCode
           tDInvoice.tcSoldToDebtorCode             = tDIOpenBalance.tcDebtorCode
           tDInvoice.tcDivisionCode                 = tDIOpenBalance.tcDivisionCode
           tDInvoice.tcJournalCode                  = tDIOpenBalance.tcJournalCode
           tDInvoice.tcReasonCode                   = tDIOpenBalance.tcReasonCode
           tDInvoice.DInvoicePostingDate            = tDIOpenBalance.ttPostingDate
           tDInvoice.DInvoiceType                   = tDIOpenBalance.tcInvoiceType
           tDInvoice.DInvoiceDate                   = tDIOpenBalance.ttInvoiceDate
           tDInvoice.DInvoiceTaxPointDate           = tDIOpenBalance.ttInvoiceTaxPointDate
           tDInvoice.DInvoiceDescription            = tDIOpenBalance.tcInvoiceDescription
           tDInvoice.tcCurrencyCode                 = tDIOpenBalance.tcTCCurrencyCode
           tDInvoice.tcNormalPaymentConditionCode   = tDIOpenBalance.tcNormalPaymentConditionCode
           tDInvoice.DInvoiceExchangeRate           = tDIOpenBalance.tdExchangeRateTCLC
           tDInvoice.DInvoiceRateScale              = if tDIOpenBalance.tdExchangeRateScale = 0 
                                                      then 1 
                                                      else tDIOpenBalance.tdExchangeRateScale 
           tDInvoice.DInvoiceCCRate                 = tDIOpenBalance.tdExchangeRateTCCC
           tDInvoice.DInvoiceCCScale                = if tDIOpenBalance.tdCCExchangeRateScale = 0 
                                                      then 1 
                                                      else tDIOpenBalance.tdCCExchangeRateScale 
           tDInvoice.DInvoiceTSMNumber              = tDIOpenBalance.tcInvoiceTSMNumber
           tDInvoice.DInvoiceDueDate                = tDIOpenBalance.ttInvoiceDueDate
           tDInvoice.DInvoiceDiscountDueDate        = tDIOpenBalance.ttInvoiceDiscountDueDate
           tDInvoice.DInvoiceDIText                 = tDIOpenBalance.tcPostingText
           tDInvoice.tcProjectCode                  = tDIOpenBalance.tcProjectCode
           tDInvoice.tcCostCentreCode               = tDIOpenBalance.tcCostCentreCode
           tDInvoice.DInvoiceIsOpen                 = true
           tDInvoice.DInvoiceIsSelected             = false
           tDInvoice.DInvoiceIsPaymentAllowed       = false
           tDInvoice.DInvoiceIsTaxable              = false
           tDInvoice.DInvoiceIsTaxExcluded          = false
           tDInvoice.DInvoiceIsWithDeduction        = if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} then true else false
           tDInvoice.tcVatCurrencyCode              = tDInvoice.tcCurrencyCode
           ocNewRecordInfo                          = if tDIOpenBalance.tcKey <> "":U
                                                      then tDIOpenBalance.tcKey + ",":U + string(tDInvoice.DInvoice_ID)
                                                      else "":U
           tDIOpenBalance.tiInvoiceId               = tDInvoice.DInvoice_ID
           tDInvoice.DInvoiceCommentNote            = tDIOpenBalance.tcInvoiceCommentNote
           tDInvoice.DInvoiceSlsPsn1                = tDIOpenBalance.tcSalesPerson1
           tDInvoice.DInvoiceSlsPsn2                = tDIOpenBalance.tcSalesPerson2
           tDInvoice.DInvoiceSlsPsn3                = tDIOpenBalance.tcSalesPerson3
           tDInvoice.DInvoiceSlsPsn4                = tDIOpenBalance.tcSalesPerson4
           tDInvoice.DInvoiceComm1                  = tDIOpenBalance.tdDInvoiceCom1
           tDInvoice.DInvoiceComm2                  = tDIOpenBalance.tdDInvoiceCom2
           tDInvoice.DInvoiceComm3                  = tDIOpenBalance.tdDInvoiceCom3
           tDInvoice.DInvoiceComm4                  = tDIOpenBalance.tdDInvoiceCom4.
           
    if tDIOpenBalance.tcPostingType = {&POSTINGTYPE-DEBIT}
    then assign tDInvoice.DInvoiceOriginalDebitTC = tDIOpenBalance.tdInvoiceAmountTC
                tDInvoice.DInvoiceOriginalDebitLC = tDIOpenBalance.tdInvoiceAmountLC
                tDInvoice.DInvoiceOriginalDebitCC = tDIOpenBalance.tdInvoiceAmountCC
                tDInvoice.tdDInvoiceOriginalTC    = tDIOpenBalance.tdInvoiceAmountTC
                tDInvoice.tdDInvoiceOriginalLC    = tDIOpenBalance.tdInvoiceAmountLC.
    else assign tDInvoice.DInvoiceOriginalCreditTC = tDIOpenBalance.tdInvoiceAmountTC
                tDInvoice.DInvoiceOriginalCreditLC = tDIOpenBalance.tdInvoiceAmountLC
                tDInvoice.DInvoiceOriginalCreditCC = tDIOpenBalance.tdInvoiceAmountCC
                tDInvoice.tdDInvoiceOriginalTC     = tDIOpenBalance.tdInvoiceAmountTC
                tDInvoice.tdDInvoiceOriginalLC     = tDIOpenBalance.tdInvoiceAmountLC.
                
    if tDIOpenBalance.tiInvoiceVoucher = 0
    then do:
    
        <Q-74 run JournalBeginsForJEDefault (all) (Read) (Cache)
           (input viCompanyId, (CompanyId)
            input tDIOpenBalance.tcJournalCode, (JournalCode)
            input {&JOURNALCONTROL-LOGISTIC}, (JournalControl)
            input '':U, (JournalTypeCode)
            output dataset tqJournalBeginsForJEDefault) in BJournal>
               
        find first tqJournalBeginsForJEDefault where
                   tqJournalBeginsForJEDefault.tcJournalCode    = tDIOpenBalance.tcJournalCode and
                   tqJournalBeginsForJEDefault.tcJournalControl = {&JOURNALCONTROL-LOGISTIC} 
                   no-lock no-error.
        
        if available tqJournalBeginsForJEDefault
        then assign vlJournalIsOperational = TRUE.
        else assign vlJournalIsOperational = FALSE.
        
        /* Voucher number generation */ 
        /* skip the consective numbering check for Deduction and prepayment. */    
        if vlCCNIsConsecutNr      = TRUE  and 
           vlJournalIsOperational = FALSE and 
           tDInvoice.DInvoiceType <> {&INVOICETYPE-DEDUCTION} and
           tDInvoice.DInvoiceType <> {&INVOICETYPE-PREPAYMENT}
        then do:
            assign tDIOpenBalance.tiInvoiceVoucher = viCCNTempDInvNbr
                   viCCNTempDInvNbr                = viCCNTempDInvNbr + 1.
        end. /* if vlCCNIsConsecutNr = TRUE */
        else do:
        
            /* Reserve a number for this sheet.                          */
            /* This number will be released again by housekeeping if the */
            /* transaction was not committed by then.                    */
            assign vhFcComponent = ?.
            <M-3 run GetNumber
               (input  tDIOpenBalance.tiCompanyId (iiCompanyId), 
                input  tDIOpenBalance.tiPeriodYear (iiNumbrYear), 
                input  tDIOpenBalance.tcJournalCode (icNumbrType), 
                output tDIOpenBalance.tiInvoiceVoucher (oiNumber), 
                input  viFcCurrentInstanceId (iiInstanceId), 
                input  vcFcComponentName (icClassName), 
                output viFcReturnSuper (oiReturnStatus)) in BNumber>
                
            if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave CREATEBLOCK.
            
        end. /* NOT if vlCCNIsConsecutNr = TRUE */
    end. /* end tDIOpenBalance.tiInvoiceVoucher = 0 */

    assign tDInvoice.DInvoiceVoucher = tDIOpenBalance.tiInvoiceVoucher.
    
    if not can-do(vcCommitNumber,tDInvoice.tc_Rowid)
    then assign vcCommitNumber = vcCommitNumber + ",":U + tDInvoice.tc_Rowid.

    <Q-4 run DebtorByDebtor (first) (Read) (NoCache)
       (input tDIOpenBalance.tiCompanyId, (CompanyId)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        input ?, (DebtorId)
        input tDIOpenBalance.tcDebtorCode, (DebtorCode)
        output dataset tqDebtorByDebtor) in BDebtor>
        
    find first tqDebtorByDebtor no-error.
    if not available tqDebtorByDebtor
    then do:
        assign vcMessage            = (#T-10'The specified customer is not defined in the system.':150(327)T-10#)
               viLocalReturnStatus  = -1.
        <M-5 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-696':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        leave CREATEBLOCK.
    end. /* end not available tqDebtorByDebtor */

    /* default the payment condition of the debtor if not yet filled */
    if tDInvoice.tcNormalPaymentConditionCode = "":U or
       tDInvoice.tcNormalPaymentConditionCode = ?
    then do:
        /* get credit terms for deduction invoice if the payment type is stage */
        if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} or tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT} 
        then do:
            <Q-87 run DomainCreditTermByDomain (all) (Read) (Cache)
               (input viDomainID, (Domain_ID)
                output dataset tqDomainCreditTermByDomain) in BDomainProperty>
               
            find first tqDomainCreditTermByDomain no-error.
           
            if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} 
            then do:
               if not available tqDomainCreditTermByDomain or 
                  tqDomainCreditTermByDomain.tcDeductPaymentConditionCode = '':U or
                  tqDomainCreditTermByDomain.tcDeductPaymentConditionCode = ?
               then do:
                    <M-96 run SetMessage
                       (input  #T-97'Cannot find credit term deduction in the current domain.':255(134957455)T-97# (icMessage), 
                        input  ? (icArguments), 
                        input  ? (icFieldName), 
                        input  ? (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  ? (icRowid), 
                        input  'qadfin-326284':U (icFcMsgNumber), 
                        input  ? (icFcExplanation), 
                        input  ? (icFcIdentification), 
                        input  ? (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                    Leave CREATEBLOCK.
                 end.
                 
                 if tqDomainCreditTermByDomain.tlDeductPaymentConditionIsActive = false 
                 then do:
                     <M-65 run SetMessage
                        (input  substitute(#T-65'the Credit Terms Deduction &1 is not active':255(495685807)T-65#,  tqDomainCreditTermByDomain.tcDeductPaymentConditionCode) (icMessage), 
                         input  ? (icArguments), 
                         input  ? (icFieldName), 
                         input  ? (icFieldValue), 
                         input  'E' (icType), 
                         input  3 (iiSeverity), 
                         input  ? (icRowid), 
                         input  'qadfin-364755':U (icFcMsgNumber), 
                         input  ? (icFcExplanation), 
                         input  ? (icFcIdentification), 
                         input  ? (icFcContext), 
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                     Leave CREATEBLOCK.
                 end.
                 assign tDInvoice.tcNormalPaymentConditionCode = tqDomainCreditTermByDomain.tcDeductPaymentConditionCode.
                 
            end. /* if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} */
                          
            if tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT} 
            then do:
           
                if available tqDomainCreditTermByDomain and tqDomainCreditTermByDomain.tlPrePaymentConditionIsActive = true 
                then assign tDInvoice.tcNormalPaymentConditionCode = tqDomainCreditTermByDomain.tcPrePaymentConditionCode.
                
                if tDInvoice.tcNormalPaymentConditionCode = '':U or tDInvoice.tcNormalPaymentConditionCode = ? 
                then do:
                    assign tDInvoice.tcNormalPaymentConditionCode = tqDebtorByDebtor.tcNormalPaymentConditionCode.
                
                    /* prepayment should use no-stage no-discount payment condition code. */
                    if tqDebtorByDebtor.tcNormalPaymentType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} 
                    then do:
                        <Q-62 run PaymentConditionByProperties (all) (Read) (Cache)
                           (input ?, (PaymentCondition_ID)
                            input ?, (PaymentConditionCode)
                            input TRUE, (PaymentConditionIsActive)
                            input {&PAYMENTCONDITIONPAYMENTTYPE-NORMAL}, (PaymentConditionPaymentType)
                            input ?, (PaymentConditionPdDiscType)
                            input 0, (PaymentConditionPercentage)
                            output dataset tqPaymentConditionByProperties) in BPaymentCondition>   
                                                 
                        find first tqPaymentConditionByProperties no-error.
                        if available tqPaymentConditionByProperties 
                        then assign tDInvoice.tcNormalPaymentConditionCode = tqPaymentConditionByProperties.tcPaymentConditionCode. 
                    end.
                end.
                
            end. /* if tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT} */
        end. /* if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} or tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT} */
        
        else assign tDInvoice.tcNormalPaymentConditionCode = tqDebtorByDebtor.tcNormalPaymentConditionCode.
        
    end. /* if tDInvoice.tcNormalPaymentConditionCode = "":U */
    
    /* default reason of debtor if not yet filled */
    if tDInvoice.tcReasonCode = "":U
    then assign tDInvoice.Reason_ID    = if tqDebtorByDebtor.tiReason_ID = ? then 0 else tqDebtorByDebtor.tiReason_ID
                tDInvoice.tcReasonCode = if tqDebtorByDebtor.tcReasonCode = ? then "":U else tqDebtorByDebtor.tcReasonCode.

    /* default own vat number, transaction end country */
    <Q-6 run CompanyPropertyByBusinessRel (first) (Read) (Cache)
       (input tDIOpenBalance.tiCompanyId, (CompanyId)
        input ?, (AddressType)
        output dataset tqCompanyPropertyByBusinessRel) in BCompanyProperty>
    find first tqCompanyPropertyByBusinessRel no-error.
    if available tqCompanyPropertyByBusinessRel
    then assign tDInvoice.DInvoiceOwnVatNumber           = tqCompanyPropertyByBusinessRel.tcAddressTaxIDState
                tDInvoice.tcOwnVatNumberCountryCode      = tqCompanyPropertyByBusinessRel.tcAddressCountryCode.
    
    /* fill ship to address */
    <M-17 run DefaultValuesShipFromShipTo
       (input  ? (iiDebtorId), 
        input  tDInvoice.tcDebtorCode (icDebtorCode), 
        output tDInvoice.tcShipToBusinessRelationCode (ocShipToBusinessRelationCode), 
        output tDInvoice.tcShipToAddressTypeCode (ocShipToAddressTypeCode), 
        output tDInvoice.tcShipToAddressStreet1 (ocShipToAddressStreet1), 
        output tDInvoice.tcShipToAddressStreet2 (ocShipToAddressStreet2), 
        output tDInvoice.tcShipToAddressStreet3 (ocShipToAddressStreet3), 
        output tDInvoice.tcShipToAddressZip (ocShipToAddressZip), 
        output tDInvoice.tcShipToAddressCity (ocShipToAddressCity), 
        output tDInvoice.tcShipToAddressState (ocShipToAddressState), 
        output tDInvoice.tcShipToCountryCode (ocShipToCountryCode), 
        output vlDummy (olShipToAddressIsTaxInCity), 
        output vlDummy (olShipToCountryIsEUCountry), 
        output vcDummy (ocShipToTxzTaxZone), 
        output tDInvoice.ShipToAddress_ID (oiShipToAddressId), 
        output vcDummy (ocDebtorShipToCode), 
        output vcShipFromBusinessRelationCode (ocShipFromBusinessRelationCode), 
        output vcShipFromAddressTypeCode (ocShipFromAddressTypeCode), 
        output vcShipFromAddressStreet1 (ocShipFromAddressStreet1), 
        output vcShipFromAddressStreet2 (ocShipFromAddressStreet2), 
        output vcShipFromAddressStreet3 (ocShipFromAddressStreet3), 
        output vcShipFromAddressZip (ocShipFromAddressZip), 
        output vcShipFromAddressCity (ocShipFromAddressCity), 
        output vcShipFromAddressState (ocShipFromAddressState), 
        output vcShipFromCountryCode (ocShipFromCountryCode), 
        output vcShipFromTaxIdState (ocShipFromTaxIdState), 
        output vlShipFromCountryIsEUCountry (olShipFromCountryIsEUCountry), 
        output vcShipFromTaxZone (ocShipFromTaxZone), 
        output viShipFromAddressId (oiShipFromAddressId), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.

    if viShipFromAddressId <> 0
    then assign tDInvoice.ShipFromAddress_ID                = viShipFromAddressId
                tDInvoice.tcShipFromBusinessRelationCode    = vcShipFromBusinessRelationCode
                tDInvoice.tcShipFromAddressTypeCode         = vcShipFromAddressTypeCode
                tDInvoice.tcShipFromAddressStreet1          = vcShipFromAddressStreet1
                tDInvoice.tcShipFromAddressStreet2          = vcShipFromAddressStreet2
                tDInvoice.tcShipFromAddressStreet3          = vcShipFromAddressStreet3
                tDInvoice.tcShipFromAddressZip              = vcShipFromAddressZip
                tDInvoice.tcShipFromAddressCity             = vcShipFromAddressCity
                tDInvoice.tcShipFromAddressState            = vcShipFromAddressState
                tDInvoice.tcShipFromCountryCode             = vcShipFromCountryCode
                tDInvoice.tcShipFromTaxIdState              = vcShipFromTaxIdState
                tDInvoice.tlShipFromCountryIsEUCountry      = vlShipFromCountryIsEUCountry
                tDInvoice.tcShipFromTaxZone                 = vcShipFromTaxZone.

    /* PTW */
    <M-13 run GetDebtorVatNumber
       (input  tqDebtorByDebtor.tcDebtorCode (icDebtorCode), 
        input  tqDebtorByDebtor.tcBillToDebtorCode (icBillToDebtorCode), 
        input  tqDebtorByDebtor.tcDebtorTaxIDState (icDebtorTaxIDState), 
        input  tqDebtorByDebtor.tcCountryCode (icDebtorCountryCode), 
        input  tqDebtorByDebtor.tlCountryIsEUCountry (ilDebtorCountryIsEUCountry), 
        output tDInvoice.DInvoiceDebtorVatNumber (ocDebtorVatNumber), 
        output tDInvoice.tcDebVatNumberCountryCode (ocDebtorVatCountryCode), 
        output vlTransactionEndIsEUCountry (olDebtorVatCountryIsEUCountry), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.
    
    /* VAT currency rate - VAT currency itself is defaulted in IntialData */
    <M-12 run GetVatExchangeRate (input  tDInvoice.tcVatCurrencyCode (icOwnVatCurrencyCode), 
                              input  tDInvoice.VatCurrency_ID (iiOwnVatCurrencyId), 
                              input  tDInvoice.tcCurrencyCode (icDInvoiceCurrencyCode), 
                              input  tDInvoice.DInvoiceCurrency_ID (iiDInvoiceCurrencyId), 
                              input  tDInvoice.DInvoiceExchangeRate (idDInvoiceExchangeRate), 
                              input  tDInvoice.DInvoiceRateScale (idDInvoiceRateScale), 
                              input  tDInvoice.DInvoiceTaxPointDate (itTaxPointDate), 
                              output tDInvoice.DInvoiceVatExchangeRate (odVatExchangeRate), 
                              output tDInvoice.DInvoiceVatRateScale (odVatRateScale), 
                              output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.

    /* create bank record */
    if (tDIOpenBalance.tiBankNumber_ID <> ? and tDIOpenBalance.tiBankNumber_ID <> 0) or
       (tDIOpenBalance.tcBankNumber <> ? and tDIOpenBalance.tcBankNumber <> "":U)
    then do:
        <M-11 run AddDetailLine (input  'DInvoiceBank':U (icTable), 
                         input  tDInvoice.tc_Rowid (icParentRowid), 
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>        
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave CREATEBLOCK.

        assign tDInvoiceBank.DInvoice_ID           = tDInvoice.DInvoice_ID
               tDInvoiceBank.BankNumber_ID         = tDIOpenBalance.tiBankNumber_ID
               tDInvoiceBank.tcBankNumber          = tDIOpenBalance.tcBankNumber
               tDInvoiceBank.DInvoiceBankToPayTC   = tDIOpenBalance.tdInvoiceAmountTC
               tDInvoiceBank.tcBankNumberExtension = tDIOpenBalance.tcBankNumberExtension
               tDInvoiceBank.tiParentObject_ID     = tDInvoice.Debtor_ID
               tDInvoiceBank.tcOwnBankNumber       = if tDIOpenBalance.tcOwnBankNumber <> ? and tDIOpenBalance.tcOwnBankNumber <> "" then tDIOpenBalance.tcOwnBankNumber else ?
               tDInvoiceBank.tcPayFormatTypeCode   = if tDIOpenBalance.tcPaymentFormat <> ? and tDIOpenBalance.tcPaymentFormat <> "" then tDIOpenBalance.tcPaymentFormat else ?.
               
        <Q-20 run BankNumberForParentObject (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input tDInvoice.Debtor_ID, (ParentObjectID)
            input tDIOpenBalance.tcBankNumber, (BankNumber)
            input tDIOpenBalance.tcOwnBankNumber, (OwnBankNumber)
            input tDIOpenBalance.tcPaymentFormat, (PayFormatTypeCode)
            output dataset tqBankNumberForParentObject) in BBankNumber>
        for first tqBankNumberForParentObject no-lock:
            tDInvoiceBank.BankNumber_ID = tqBankNumberForParentObject.tiBankNumber_ID.
            <Q-58 run BankNumberForPayFormatBank (all) (Read) (NoCache)
               (input viCompanyId, (CompanyId)
                input tDInvoice.Debtor_ID, (ParentObjectID)
                input tqBankNumberForParentObject.tiBankNumber_ID, (BankNumberID)
                output dataset tqBankNumberForPayFormatBank) in BBankNumber>
            for first tqBankNumberForPayFormatBank no-lock:
                assign tDInvoiceBank.tiBankPayFormat_ID = tqBankNumberForPayFormatBank.tiBankPayFormat_ID.
            end.
        end.
        if not available tqBankNumberForParentObject 
        then do:
            assign vcMessage = trim(substitute(#T-49'Bank number (&1) not found with Own Bank Number (&2) for Customer (&3) with Payment Format (&4).':255(602301135)T-49#,tDIOpenBalance.tcBankNumber,tDIOpenBalance.tcOwnBankNumber,tDIOpenBalance.tcDebtorCode,tDIOpenBalance.tcPaymentFormat))
                              oiReturnStatus = -1.
            <M-68 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-877379':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            leave CREATEBLOCK.
        end. /* end not available tqDebtorByDebtor */           
                       
    end. /* end tDIOpenBalance.tiBankNumber_ID <> ? and .. */
    else if tDIOpenBalance.tcDebtorCode <> '' and tDIOpenBalance.tcDebtorCode <> ?
         then do :
            /* Get the Default BankNumber(s) of the Customer */
            <Q-51 run BankNumberByDebtor (all) (Read) (NoCache)
                (input viCompanyId, (CompanyId)
                 input tDIOpenBalance.tcDebtorCode, (DebtorCode)
                 input ?, (Debtor_ID)
                 input TRUE, (BankNumberIsDefault)
                 output dataset tqBankNumberByDebtor) in BBankNumberQuery>
            for first tqBankNumberByDebtor where 
                      tqBankNumberByDebtor.tcDebtorCode = tDIOpenBalance.tcDebtorCode and
                      tqBankNumberByDebtor.tlBankNumberIsDefault :
                <M-15 run AddDetailLine
                   (input  'DInvoiceBank':U (icTable), 
                    input  tDInvoice.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave CREATEBLOCK.
                assign tDInvoiceBank.DInvoice_ID           = tDInvoice.DInvoice_ID
                       tDInvoiceBank.BankNumber_ID         = tqBankNumberByDebtor.tiBankNumber_ID
                       tDInvoiceBank.tcBankNumber          = tqBankNumberByDebtor.tcBankNumber
                       tDInvoiceBank.DInvoiceBankToPayTC   = tDIOpenBalance.tdInvoiceAmountTC
                       tDInvoiceBank.tcBankNumberExtension = tqBankNumberByDebtor.tcBankNumberExtension
                       tDInvoiceBank.tiBankPayFormat_ID    = tqBankNumberByDebtor.tiBankPayFormat_ID
                       tDInvoiceBank.tiParentObject_ID     = tqBankNumberByDebtor.tiParentObject_ID.
            end. /*  for first tqBankNumberByDebtor */
         end. /* tDIOpenBalance.tcDebtorCode <> '' */

    assign viDummy = int(entry(lookup(tDInvoice.tcVatCurrencyCode, vcCurrencyDecimalsList) + 1, vcCurrencyDecimalsList)) no-error.
    assign vdInvoiceAmountTC = if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} 
                               then tDInvoice.DInvoiceOriginalDebitTC 
                               else 
                               if tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTE} 
                               then tDInvoice.DInvoiceOriginalCreditTC 
                               else 0.
    
    /* If field tDIOpenBalance.tcBLWIGroupCode is set to SKIPGTMCALLFORNONTAXOPENBAL and the invoice is non-taxable */
    /* then we just look for the non-taxable VAT of the domain and create DInvoiceVat ourself                       */
    /* else we call DefaultValuesTax that will create all required data after calling GTM                           */
    /* This was implemented to increase the performance for opening-balances (as they are always non-taxable)       */
    if tDIOpenBalance.tcBLWIGroupCode  = {&SKIPGTMCALLFORNONTAXOPENBAL} and 
       tDInvoice.DInvoiceIsTaxable     = false                          and 
       tDInvoice.Company_ID           <> 0                              and
       tDInvoice.Company_ID           <> ?
    then do :
        /* Find the domain of the invoice */
        if tDInvoice.Company_ID = viCompanyID 
        then assign vcDomainCodeOfDInvoice = vcDomainCode.
        else do : 
            <Q-59 run CompanyByCompanyIdCode (all) (Read) (Cache)
               (input tDInvoice.Company_ID, (CompanyId)
                input ?, (CompanyCode)
                output dataset tqCompanyByCompanyIdCode) in BCompany>
            Find tqCompanyByCompanyIdCode where 
                 tqCompanyByCompanyIdCode.tiCompany_ID = tDInvoice.Company_ID
                 no-lock no-error.
            if not available tqCompanyByCompanyIdCode
            then do :
                assign vcMessage            = trim(substitute(#T-38'Unable to find the Domain based upon the ID of the Company (&1) of the Invoice':250(211246318)T-38#,tDInvoice.Company_ID))
                       viLocalReturnStatus  = -1.
                <M-29 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-416271':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
                leave CREATEBLOCK.                
            end. /* if not available tqCompanyByCompanyIdCode */
            assign vcDomainCodeOfDInvoice = tqCompanyByCompanyIdCode.tcDomainCode. 
        end. /* Not if tDInvoice.Company_ID = viCompanyID */
        /* Remove any taxes that were already on the invoice */
        for each tDInvoiceVat where
                 tDInvoiceVat.tc_ParentRowid = tDInvoice.tc_Rowid :
            if tDInvoiceVat.tc_Status = "N":U
            then delete tDInvoiceVat.
            else assign tDInvoiceVat.tc_Status = "D":U.
        end. /* for each tDInvoiceVat where */
        /* Query the non-taxable VAT of the domain used for output */
        <Q-76 run VATPrim (all) (Read) (Cache)
           (input ?, (VatId)
            input '00000000':U, (VatCode)
            input ?, (DomainId)
            input vcDomainCode, (DomainCode)
            input {&VATINOUT-OUTPUT}, (VatInOut)
            output dataset tqVATPrim) in BVAT>
        for first tqVATPrim where
                  tqVATPrim.tcVatCode    = "00000000":U       and 
                  tqVATPrim.tcVatInOut   = {&VATINOUT-OUTPUT} and 
                  tqVATPrim.tcDomainCode = vcDomainCode
                  no-lock :
            /* Create a tDInvoiceVat with all default values */
            <M-873 run AddDetailLine
               (input  'DInvoiceVat':U (icTable), 
                input  tDInvoice.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
            if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave CREATEBLOCK.
            assign tDInvoiceVat.tcVatCode                  = tqVATPrim.tcVatCode
                   tDInvoiceVat.tcDomainCode               = tqVATPrim.tcDomainCode
                   tDInvoiceVat.tcVatInOut                 = tqVATPrim.tcVatInOut
                   tDInvoiceVat.tcNormalTaxGLCode          = "":U
                   tDInvoiceVat.tcNormalTaxDivisionCode    = "":U
                   tDInvoiceVat.tcAbsRetTaxGLCode          = "":U
                   tDInvoiceVat.tcAbsRetTaxDivisionCode    = "":U
                   tDInvoiceVat.DInvoiceVatIsTaxable       = tDInvoice.DInvoiceIsTaxable
                   tDInvoiceVat.DInvoiceVatSequence        = 1
                   tDInvoiceVat.DInvoiceVatUILinkedRowID   = "0":U                   
                   tDInvoiceVat.DInvoiceVatVatBaseDebitLC  = 0
                   tDInvoiceVat.DInvoiceVatVatBaseDebitTC  = 0
                   tDInvoiceVat.DInvoiceVatVatBaseCreditLC = 0
                   tDInvoiceVat.DInvoiceVatVatBaseCreditTC = 0
                   tDInvoiceVat.DInvoiceVatVatBaseDebitCC  = 0
                   tDInvoiceVat.DInvoiceVatVatBaseCreditCC = 0
                   tDInvoiceVat.DInvoiceVatNTBaseDebitTC   = 0
                   tDInvoiceVat.DInvoiceVatNTBaseCreditTC  = 0 
                   tDInvoiceVat.DInvoiceVatVatDebitTC      = 0
                   tDInvoiceVat.DInvoiceVatVatDebitLC      = 0        
                   tDInvoiceVat.DInvoiceVatVatCreditTC     = 0
                   tDInvoiceVat.DInvoiceVatVatCreditLC     = 0
                   tDInvoiceVat.DInvoiceVatIsUpdAllow      = false
                   tDInvoiceVat.DInvoiceVatIsAbsRet        = false
                   tDInvoiceVat.DInvoiceVatIsAccrRcpUs     = false
                   tDInvoiceVat.DInvoiceVatIsRevCharge     = false
                   tDInvoiceVat.DInvoiceVatTaxTrType       = {&VATTAXTRANSACTIONTYPE-ARDTCRMEMO}
                   tDInvoiceVat.TxtyTaxType                = {&VATTAXTYPE-NONTAX}
                   tDInvoiceVat.TxenvTaxEnv                = "":U
                   tDInvoiceVat.TxclTaxCls                 = "":U
                   tDInvoiceVat.TxuTaxUsage                = "":U.           
        end. /* for first tqVATPrim where */
    end. /* if tDIOpenBalance.tcBLWIGroupCode = {&SKIPGTMCALLFORNONTAXOPENBAL} and */
    else do :
        <M-16 run DefaultValuesTax
           (input  tDInvoice.DInvoice_ID (iiDinvoiceId), 
            input  tDInvoice.tcCurrencyCode (icCurrencyCode), 
            input  tDInvoice.DInvoiceVoucher (icDocumentReference), 
            input  tDInvoice.DInvoiceTaxPointDate (itTaxPointDate), 
            input  tDInvoice.tcShipFromTaxZone (icShipFromTaxZone), 
            input  tqDebtorByDebtor.tcAddressTxzTaxZone (icShipToTaxZone), 
            input  tDInvoice.tcDebtorCode (icDebtorCode), 
            input  tqDebtorByDebtor.tcTxclTaxCls (icTxclTaxClass), 
            input  tqDebtorByDebtor.tcTxuTaxUsage (icTxuTaxUsage), 
            input  vdInvoiceAmountTC (idInvoiceAmountTC), 
            input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
            input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
            input  tDInvoice.tcNormalPaymentConditionCode (icPaymentConditionCode), 
            input  tDInvoice.DInvoicePostingDate (itPostingDate), 
            input  false (ilTaxable), 
            input  tDInvoice.DInvoiceIsTaxExcluded (ilDInvoiceIsTaxExcluded), 
            input  tDInvoice.DInvoiceType (icInvoiceType), 
            input  ? (ilShipToAddressIsTaxInCity), 
            input  tDInvoice.DInvoiceVatExchangeRate (idVatExchangeRate), 
            input  tDInvoice.DInvoiceVatRateScale (idVatRateScale), 
            input  tDInvoice.DInvoiceCCRate (idCCExchangeRate), 
            input  tDInvoice.DInvoiceCCScale (idCCExchangeRateScale), 
            input  tDInvoice.ShipToAddress_ID (iiShipToAddressID), 
            input  viShipFromAddressId (iiShipFromAddressID), 
            output vcDummy (ocTxenvTaxEnv), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>        
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave CREATEBLOCK.
    end. /* Not if tDIOpenBalance.tcBLWIGroupCode = {&SKIPGTMCALLFORNONTAXOPENBAL} and */

    /* get default subaccount and cost centre if control gl code is defined with it and values are not passed */
    assign viControlGLProfileId = if tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT}
                                  then tqDebtorByDebtor.tiPrePayControlGLProfile_ID
                                  else if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION}
                                  then tqDebtorByDebtor.tiDeductionCtrlGLProfile_ID
                                  else if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE}       or 
                                          tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE}    or 
                                          tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                       then tqDebtorByDebtor.tiInvControlGLProfile_ID
                                       else tqDebtorByDebtor.tiCnControlGLProfile_ID
           vlError              = false
           vcControlProfileType = if tDInvoice.DInvoiceType = {&INVOICETYPE-DEDUCTION} 
                                  then #T-75'(Deduction)':20(689711431)T-75#
                                  else '':U.
  
    if viControlGLProfileId = ? or viControlGLProfileId = 0
    then assign vcMessage = trim(#T-177'The Control GL Profile $1 is not specified in the customer definition':255(450896101)T-177#)
                viLocalReturnStatus = -1
                vlError = true.
    else do:
        <Q-56 run ProfileById (all) (Read) (Cache)
           (input viControlGLProfileId, (Profile_ID)
            output dataset tqProfileById) in BProfile>
        find first tqProfileById no-error.
     
        if not available tqProfileById 
        then do:
           assign vcMessage = trim(#T-71'The specified customer Control GL Profile $1 is not defined in the system.':255(477916609)T-71#)
                viLocalReturnStatus = -1
                vlError = true.
        end.
        else do:
           if tqProfileById.tlProfileIsActive <> true 
           then do:
               assign vcMessage = trim(#T-57'the Control GL Profile $1 of the customer is not active':255(960422682)T-57#)
                      viLocalReturnStatus = -1
                      vlError = true.
           end. 
           else do:
                <Q-18 run ProfileLinkByGL (all) (Read) (Cache)
                   (input viControlGLProfileId, (GlProfileId)
                    input tDInvoice.Company_ID, (CompanyId)
                    output dataset tqProfileLinkByGL) in BProfile>
                find first tqProfileLinkByGl where
                           tqProfileLinkByGL.tiProfile_ID = viControlGLProfileId
                           no-error.
                if not available tqProfileLinkByGl
                then assign vcMessage = trim(#T-239'The specified customer Control GL Profile $1 has no linked GL account':255(488726368)T-239#)
                            viLocalReturnStatus = -1
                            vlError = true.
                else 
                do:
                   if tqProfileLinkByGL.tlGLIsActive <> true 
                   then assign vcMessage = trim(#T-19'the linked GL account of Control GL Profile $1 of the cutomer is not active':255(120191634)T-19#)
                               viLocalReturnStatus = -1
                               vlError = true.
                end.
           end. /*tqProfileById.tlProfileIsActive <> true*/
         end. /*end else not available tqProfileById*/
         
    end. /* end else viControlGLProfileId = ? .. */
    
    if vlError
    then do:         
        <M-23 run SetMessage
           (input  vcMessage (icMessage), 
            input  vcControlProfileType (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'QadFin-9707':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        leave CREATEBLOCK.
    end. /* end vlError */

    assign tDInvoice.tcControlGLCode = tqProfileLinkByGL.tcGLCode
           tDInvoice.ControlGL_ID = tqProfileLinkByGL.tiGL_ID.
           
     <M-22 run CreateDIPostingGLAccount
        (input  tDInvoice.tcControlGLCode (icGLCode), 
         input  ? (iiDivisionId), 
         input-output tDInvoice.tcDivisionCode (bcDivisionCode), 
         output vldummy (olGLIsDivisionAcount), 
         input-output tDInvoice.tcCostCentreCode (bcCostCentreCode), 
         input-output tDInvoice.tcProjectCode (bcProjectCode), 
         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
         
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.

    for each tDInvoiceBank where 
             tDInvoiceBank.tc_ParentRowid =  tDInvoice.tc_Rowid:
             
        <M-36 run CreateDIBankPayCode
           (input  tDInvoiceBank.DInvoiceBank_ID (iiDInvoiceBankId), 
            input  tDInvoiceBank.tc_Rowid (icDInvoiceBankRowid), 
            input  tDInvoiceBank.BankNumber_ID (iiBankNumberId), 
            output tApiDInvoiceBankPayCode (tApiDInvoiceBankPayCode), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>   
             
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave CREATEBLOCK.
        
    end. /*FOR EACH tDInvoiceBank */

    /* di posting */
    /* i don't use it but i have to give it as input parameter so i make it empty first */
    empty temp-table tDIAccount.
    assign viPostingId          = 0
           viRepeatingPostingID = iiPostingId. /* to make sure that in case iiPostingId = 0, it stays 0 after the first run */
    /* you don't work with taxes here, so you can left those parameters on unknown value */
    
    <M-9 run CreateDIPosting
       (input  tDInvoice.DInvoice_ID (iiDInvoiceId), 
        input  ? (iiPeriodId), 
        input  tDIOpenBalance.tiPeriodYear (iiPeriodYear), 
        input  tDIOpenBalance.tiPeriodPeriod (iiPeriodPeriod), 
        input  tDIOpenBalance.ttPostingDate (itPostingDate), 
        input  ? (iiDebtorId), 
        input  tDIOpenBalance.tcDebtorCode (icDebtorCode), 
        input  tDIOpenBalance.tcInvoiceType (icDInvoiceType), 
        input  ? (iiDivisionId), 
        input  if tDIOpenBalance.tcTransferAccountDivisionCode = '':U then tDInvoice.tcDivisionCode else tDIOpenBalance.tcTransferAccountDivisionCode (icDivisionCode), 
        input  ? (iiJournalId), 
        input  tDIOpenBalance.tcJournalCode (icJournalCode), 
        input  tDIOpenBalance.tiInvoiceVoucher (iiVoucher), 
        input  tDIOpenBalance.ttInvoiceTaxPointDate (itTaxPointDate), 
        input  tDIOpenBalance.tcInvoiceDescription (icDescription), 
        input  tDIOpenBalance.tdInvoiceAmountTC (idInvoiceAmountTC), 
        input  tDIOpenBalance.tdInvoiceAmountLC (idInvoiceAmountLC), 
        input  tDIOpenBalance.tdInvoiceAmountCC (idInvoiceAmountCC), 
        input  ? (iiCurrencyId), 
        input  tDIOpenBalance.tcTCCurrencyCode (icCurrencyCode), 
        input  tDIOpenBalance.tdExchangeRateTCLC (idExchangeRate), 
        input  if tDIOpenBalance.tdExchangeRateScale = 0 then 1 else tDIOpenBalance.tdExchangeRateScale (idExchangeRateScale), 
        input  tDIOpenBalance.tcPostingText (icDIText), 
        input  tDIOpenBalance.ttInvoiceDate (itInvoiceDate), 
        input  tDIOpenBalance.tcPostingType (icPostingType), 
        input-output viRepeatingPostingID (biPostingId), 
        input-output iiBJournalEntryId (biBJournalEntryId), 
        input  tDIAccount (tDIAccount), 
        input-output tDInvoiceVat (tApiDInvoiceVat), 
        input  tDIOpenBalance.tcTransferAccountGLCode (icGLCode), 
        input  if tDIOpenBalance.tcTransferAccountCostCentreCode = '':U then tDInvoice.tcCostCentreCode else tDIOpenBalance.tcTransferAccountCostCentreCode (icCostCentreCode), 
        input  if tDIOpenBalance.tcTransferAccountProjectCode = '':U then tDInvoice.tcProjectCode else tDIOpenBalance.tcTransferAccountProjectCode (icProjectCode), 
        input  tDIOpenBalance.tlOnlyControlAccount (ilOnlyControlAccount), 
        input  ? (icShipFromCountryCode), 
        input  ? (icShipToCountryCode), 
        input  ? (ilShipFromCountryIsEUCountry), 
        input  ? (ilShipToCountryIsEUCountry), 
        input  ? (icTxclTaxCls), 
        input  ? (icTxuTaxUsage), 
        input  ? (icShipToTxzTaxZone), 
        input  ? (icTxenvTaxEnv), 
        input  ? (icPaymentCondition), 
        input  ? (ilIsTaxInCityFromShipTo), 
        input  ? (ilIsTaxable), 
        input  tDInvoice.tcCostCentreCode (icControlCostCentreCode), 
        input  tDInvoice.tcProjectCode (icControlProjectCode), 
        input  0 (iiShipFromAddressId), 
        input  0 (iiShipToAddressId), 
        input  '':U (icSoldToDebtorCode), 
        input  ? (ilDInvoiceIsTaxExcluded), 
        input  tqDebtorByDebtor.tcBusinessRelationCode (icBusinessRelationCode), 
        input  tqDebtorByDebtor.tcBusinessRelationICCode (icBusinessRelationICCode), 
        input  tqDebtorByDebtor.tiInvControlGLProfile_ID (iiInvControlGLProfileId), 
        input  tqDebtorByDebtor.tiCnControlGLProfile_ID (iiCnControlGLProfileId), 
        input  tqDebtorByDebtor.tiPrePayControlGLProfile_ID (iiPrepayControlGLProfileId), 
        input  tqDebtorByDebtor.tiSalesAccountGLProfile_ID (iiSalesAccGLProfileId), 
        input  tqDebtorByDebtor.tiFinChgGLProfile_ID (iiFinChgGLProfileId), 
        input  tDInvoice.tcDivisionCode (icControlDivisionCode), 
        input  tqDebtorByDebtor.tiDeductionCtrlGLProfile_ID (iiDeductionControlGLProfileId), 
        input  if tDIOpenBalance.tcCCCurrencyCode = tDIOpenBalance.tcTCCurrencyCode then ? else tDIOpenBalance.tdExchangeRateTCCC (idExchangeRateCC), 
        input  if tDIOpenBalance.tcCCCurrencyCode = tDIOpenBalance.tcTCCurrencyCode then ? else tDIOpenBalance.tdccExchangeRateScale (idExchangeRateScaleCC), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        
    if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0 then leave CREATEBLOCK.
    
end. /* end for each tDIOpenBalance */

/* exception handling */
assign oiReturnStatus = viLocalReturnStatus.