project QadFinancials > class BDInvoice > method CreateDIPosting

Description

Create the DI Posting


Parameters


iiDInvoiceIdinputintegerDInvoice ID
iiPeriodIdinputintegerPeriod ID
iiPeriodYearinputintegerYear of Accounting Period
iiPeriodPeriodinputintegerPeriod of Accounting Period
itPostingDateinputdateposting date
iiDebtorIdinputintegerDebtor ID
icDebtorCodeinputcharacterCustomer Code
icDInvoiceTypeinputcharacterInvoice Type
iiDivisionIdinputintegerDivision ID
icDivisionCodeinputcharacterSub-Account Code
iiJournalIdinputintegerJournal ID
icJournalCodeinputcharacterDaybook Code
iiVoucherinputintegerVoucher
itTaxPointDateinputdatetax point date
icDescriptioninputcharacterDescription
idInvoiceAmountTCinputdecimalInvoice Amount in TC
idInvoiceAmountLCinputdecimalInvoice Amount in LC
idInvoiceAmountCCinputdecimalInvoice Amount CC
iiCurrencyIdinputintegercurrency id
icCurrencyCodeinputcharacterCurrency Code
idExchangeRateinputdecimalexchange rate
idExchangeRateScaleinputdecimal
icDITextinputcharacterCI Text
itInvoiceDateinputdateInvoice Date
icPostingTypeinputcharacterPosting Type
biPostingIdinput-outputintegerPosting ID
biBJournalEntryIdinput-outputintegerInstance of Journal Entry
tDIAccountinputtemp-tableTemp table with all account lines of the DI Posting and there value (sales account)
tApiDInvoiceVatinput-outputtemp-tabletemp table like dinvoicevat table
icGLCodeinputcharactergl account
icCostCentreCodeinputcharacterCost Center Code
icProjectCodeinputcharacterProject Code
ilOnlyControlAccountinputlogicalOnly posting on the control account
icShipFromCountryCodeinputcharactercountry code of the ship from address
icShipToCountryCodeinputcharacterCountry code of the ship to address
ilShipFromCountryIsEUCountryinputlogicalis the country of the ship from country an EU Country ?
ilShipToCountryIsEUCountryinputlogicalis the country of the ship to address an EU Country ?
icTxclTaxClsinputcharacterTax Class of the Tax tab if filled, otherwise the tax class of the debtor
icTxuTaxUsageinputcharacterTax Usage of the Tax tab, if not filled, then pass the tax usage of the debtor
icShipToTxzTaxZoneinputcharacterTax Zone of the Ship To Address
icTxenvTaxEnvinputcharacterTax environment of the tax tab, if not filled, pass the default value of it
icPaymentConditioninputcharacterPayment Condition
ilIsTaxInCityFromShipToinputlogicaltax in city flag of the ship to address
ilIsTaxableinputlogicalIs taxable flag
icControlCostCentreCodeinputcharacter
icControlProjectCodeinputcharacter
iiShipFromAddressIdinputintegerShip From Address ID : necessary for addtaxposting to fill the postingvat fields federal tax id, state tax id, misc .... at manual journal entry fields can be blank
iiShipToAddressIdinputintegerShip To Address ID : necessary for addtaxposting to fill the postingvat fields federal tax id, state tax id, misc .... at manual journal entry fields can be blank
icSoldToDebtorCodeinputcharacterSold To Debtor Code : only passed when it is the same as the ship to code
ilDInvoiceIsTaxExcludedinputlogicalTax excluded flag of customer invoice
icBusinessRelationCodeinputcharacterBusiness Relation Code
icBusinessRelationICCodeinputcharacterBusiness Relation IC Code
iiInvControlGLProfileIdinputintegerControl GL Profile id for invoice
iiCnControlGLProfileIdinputintegerControl GL Profile ID for credit notes
iiPrepayControlGLProfileIdinputintegerControl GL Profile ID for prepayment
iiSalesAccGLProfileIdinputintegerGL Profile ID for sales account
iiFinChgGLProfileIdinputintegerGL Profile ID for finance charge
icControlDivisionCodeinputcharacter
iiDeductionControlGLProfileIdinputintegerCustomer Control Profile(Deduction)
idExchangeRateCCinputdecimal
idExchangeRateScaleCCinputdecimal
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.ApiStdMaintainTTWithIntPostCreateVat
method BDInvoice.CreateDInvoices
method BDInvoice.CreateFinChgInvoices
method BDInvoice.DefaultValuesGeneralInfo


program code (program3/bdinvoice.p)

if icCostCentreCode = ? then assign icCostCentreCode = "":U.
if icDescription = ? then assign icDescription = "":U.
if icDIText = ? or icDIText = "":U then
do:
    if icDescription <> ? then assign icDIText = icDescription.
    else assign icDIText = "":U.
end.

if icGLCode = ? then assign icGLCode = "":U.
if icProjectCode = ? then assign icProjectCode = "":U.
if idExchangeRate = ? then assign idExchangeRate = 0.
if idExchangeRateScale = ? then assign idExchangeRateScale = 1.
if icDivisionCode = ? then assign icDivisionCode = "":U.
/* DO NOT ASSIGN the ? to 0 for CC. later in the code ? should be filled automatically, while 0 should remain 0 ==
if idInvoiceAmountCC = ? then assign idInvoiceAmountCC = 0.
==== */
if idInvoiceAmountLC = ? then assign idInvoiceAmountLC = 0.
if idInvoiceAmountTC = ? then assign idInvoiceAmountTC = 0.
if iiVoucher = ? then assign iiVoucher = 0.
if icShipFromCountryCode = ? then assign icShipFromCountryCode = "":U.
if icShipToCountryCode = ? then assign icShipToCountryCode = "":U.
if icTxclTaxCls = ? then assign icTxclTaxCls = "":U.
if icTxuTaxUsage = ? then assign icTxuTaxUsage = "":U.
if icTxenvTaxEnv = ? then assign icTxenvTaxEnv = "":U.
if icShipToTxzTaxZone = ? then assign icShipToTxzTaxZone = "":U.
if icPaymentCondition = ? then assign icPaymentCondition = "":U.
if icSoldToDebtorCode = ? then assign icSoldToDebtorCode = "":U.
if ilDInvoiceIsTaxExcluded = ? then assign ilDInvoiceIsTaxExcluded = false.
if icBusinessRelationCode = ? then assign icBusinessRelationCode = "":U.
if icBusinessRelationICCode = ? then assign icBusinessRelationICCode = "":U.
if iiInvControlGLProfileId = ? then assign iiInvControlGLProfileId = 0.
if iiCnControlGLProfileId = ? then assign iiCnControlGLProfileId = 0.
if iiPrepayControlGLProfileId = ? then assign iiPrepayControlGLProfileId = 0.
if iiDeductionControlGLProfileId = ? then assign iiDeductionControlGLProfileId = 0.
if iiSalesAccGLProfileId = ? then assign iiSalesAccGLProfileId = 0.
if iiFinChgGLProfileId = ? then assign iiFinChgGLProfileId = 0.

empty temp-table tDefaultSafsDI.
/* If CC Currency is same as Base Currency or tran Currency 
   then Assign idInvoiceAmountLC or idInvoiceAmountTC resp. */
if idInvoiceAmountCC <> 0 and idInvoiceAmountCC <> ? then 
do:
   if viCompanyCCId = viCompanyLCId
   then assign idInvoiceAmountCC = idInvoiceAmountLC.
   else 
   if viCompanyCCId = iiCurrencyId or
      vcCompanyCC   = icCurrencyCode                       
   then assign idInvoiceAmountCC = idInvoiceAmountTC.
end.  


<M-1 run CreateDIPostingValidate
   (input-output iiDInvoiceId (biDInvoiceId), 
    input-output iiPeriodId (biPeriodId), 
    input-output iiPeriodYear (biPeriodYear), 
    input-output iiPeriodPeriod (biPeriodPeriod), 
    input-output iiJournalId (biJournalId), 
    input-output icJournalCode (bcJournalCode), 
    input-output iiDebtorId (biDebtorId), 
    input-output icDebtorCode (bcDebtorCode), 
    input-output iiDivisionId (biDivisionId), 
    input-output icControlDivisionCode (bcDivisionCode), 
    input-output iiCurrencyId (biCurrencyId), 
    input-output icCurrencyCode (bcCurrencyCode), 
    input  itPostingDate (itPostingDate), 
    input-output icDInvoiceType (bcDInvoiceType), 
    input-output icPostingType (bcPostingType), 
    input  idExchangeRateCC (idExchangeRateCC), 
    input  idExchangeRateScaleCC (idExchangeRateScaleCC), 
    input  idInvoiceAmountCC (idInvoiceAmountCC), 
    input  idInvoiceAmountTC (idInvoiceAmountTC), 
    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>

if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.

if oiReturnStatus < 0
then return.

/* Get instance of BJournalEntry                                                                      */
if biBJournalEntryID <> 0 and biBJournalEntryId <> ?
then assign viBJournalEntryDIID = biBJournalEntryID.

if viBJournalEntryDIID = 0 or
   viBJournalEntryDIID = ?
then do:
    <I-37 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "True"
            &CLASS              = "BJournalEntry"}>

    assign vlBJEIsStartedFromDI = true.
end.
else do:
    <I-3 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
end.

/* ================================================================================================== */
/* Get Debtor's info                                                                                  */
/* ================================================================================================== */

<Q-104 run DebtorByDebtor (first) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
    input iiDebtorId, (DebtorId)
    input if iiDebtorId = 0 then icDebtorCode else '':U, (DebtorCode)
    output dataset tqDebtorByDebtor) in BDebtor >    

find first tqDebtorByDebtor no-error.
if not available tqDebtorByDebtor
then do:
                   
    <M-106 run CreateDIPostingSetMessage
       (input  #T-105'Invalid customer code':255(733714905)T-105# (icMessage), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    assign oiReturnStatus = -1.
         
    <I-107 {bFcCloseInstance
         &CLASS           = "BJournalEntry"}>     
    return.
end.

assign vcDBusinessRelationInterCoCode = if tqDebtorByDebtor.tlBusinessRelationIsInterco
                                        then tqDebtorByDebtor.tcBusinessRelationICCode
                                        else "":U.

/* Give all default saf concept values from Debtor */

  <Q-98 run DebtorSafDefaultByDebtor (all) (Read) (NoCache)
     (input tDInvoice.Company_ID, (CompanyId)
      input icDebtorCode, (DebtorCode)
      output dataset tqDebtorSafDefaultByDebtor) in BDebtor >

for each tqDebtorSafDefaultByDebtor where 
         tqDebtorSafDefaultByDebtor.tcDebtorCode = icDebtorCode:
    create tDefaultSafsDI.
    assign tDefaultSafsDI.tcSafCode        = tqDebtorSafDefaultByDebtor.tcSafCode
           tDefaultSafsDI.tcSafConceptCode = tqDebtorSafDefaultByDebtor.tcSafConceptCode.
end.

/* Give all default saf concept values from Business relation but don't override default values from Debtor */

<Q-99 run BusRelationSafDefaultByBusRel (all) (Read) (NoCache)
   (input icBusinessRelationCode, (BusinessRelationCode)
    output dataset tqBusRelationSafDefaultByBusRel) in BBusinessRelation >
  
for each tqBusRelationSafDefaultByBusRel where 
         tqBusRelationSafDefaultByBusRel.tcBusinessRelationCode =  icBusinessRelationCode:
    
    if can-find(tDefaultSafsDI where 
                tDefaultSafsDI.tcSafConceptCode = tqBusRelationSafDefaultByBusRel.tcSafConceptCode)
    then next.
    
    create tDefaultSafsDI.
    assign tDefaultSafsDI.tcSafCode        = tqBusRelationSafDefaultByBusRel.tcSafCode
           tDefaultSafsDI.tcSafConceptCode = tqBusRelationSafDefaultByBusRel.tcSafConceptCode.
end. /* FOR EACH tqBusRelationSafDefaultByBusRel */

/* ============================================== */
/* Get the proper ControlGL, If:                  */
/* - the ControlGL on the invoice is still empty  */
/* - the Debtor has changed                       */ 
/* - the InvoiceType has chnaged                  */
/* ============================================== */
if tDInvoice.tcControlGLCode = "":U       or 
   tDInvoice.tcControlGLCode = ?          or 
   tDInvoice.tcDebtorCode <> icDebtorCode or 
   tDInvoice.DInvoiceType <> icDInvoiceType
then do:
    assign viControlGLProfileId = if icDInvoiceType = {&INVOICETYPE-PREPAYMENT}
                                  then iiPrepayControlGLProfileId
                                  else if icDInvoiceType = {&INVOICETYPE-DEDUCTION}
                                      then iiDeductionControlGLProfileId
                                      else if icDInvoiceType = {&INVOICETYPE-INVOICE}       or 
                                              icDInvoiceType = {&INVOICETYPE-FINANCECHARGE}    or 
                                              icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                           then iiInvControlGLProfileId
                                           else iiCnControlGLProfileId.                                                                        
    if viControlGLProfileId = ? or 
       viControlGLProfileId = 0
    then assign vlError = true.
    else do:
        <Q-40 run ProfileLinkByGL (all) (Read) (NoCache)
           (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 vlError = true.
    end.
    if vlError
    then do:
        <M-48 run CreateDIPostingSetMessage
           (input  trim(#T-70'The specified customer control account is not defined in the system.':150(49402)T-70#) (icMessage), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        assign oiReturnStatus = -1.
        <I-94 {bFcCloseInstance
                &CLASS           = "BJournalEntry"}>
        return.
    end. /* if vlError */
    /* Store all values so the ControlGL can be used further on the program-code and the Debtor and InvoiceType can be used for comparison when this method is called again for the same invoice */
    assign tDInvoice.tcControlGLCode = tqProfileLinkByGL.tcGLCode
           tDInvoice.tcDebtorCode    = icDebtorCode 
           tDInvoice.DInvoiceType    = icDInvoiceType.    
end. /* if tDInvoice.tcControlGLCode = ? or  */

/* Posting header and reference to posting in BJournalEntry */
/* for deduction type, we need to create new posting header in customer payment.*/
if biPostingId <> 0 or
   (ilOnlyControlAccount and icDInvoiceType <> {&INVOICETYPE-DEDUCTION})
then do:
    assign viPostingId = biPostingId.

    /* Get Row Id of Posting Header in BJournalPosting */
    <M-74 run PassRowIdBasedOnPostingIdInInstance
       (input  viPostingId (iiPostingId), 
        output vcPostingHeaderRowId (ocPostingRowId), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.

    if oiReturnStatus < 0
    then do:
        <I-75 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
        return.
    end.
end.
else do:
    /* Create posting header */
    if iiVoucher = 0 then 
    do: 
        assign vcPostingInvoiceReferenceText = "CREATEDBYPOSTING":U.
    end.
    else 
    do:
        assign vcPostingInvoiceReferenceText =
               string(iiPeriodYear  , "9999":U) + "/":U +
               string(iiPeriodPeriod,"99":U)    + " ":U +
               icJournalCode                    +
               string(iiVoucher, "999999999":U).
    end.
    <M-76 run AddPostingHeader
       (input  ? (iiCompanyId), 
        input  iiPeriodYear (iiPeriodYear), 
        input  iiPeriodPeriod (iiPeriodPeriod), 
        input  icJournalCode (icJournalCode), 
        input  icJournalCode (icReportingJournalCode), 
        input  iiVoucher (iiVoucher), 
        input  itPostingDate (itPostingDate), 
        input  ? (itValueDate), 
        input  icDIText (icPostingText), 
        input  icBusinessRelationCode (icPostingBusinessRelationText), 
        input  vcPostingInvoiceReferenceText (icPostingInvoiceReferenceText), 
        input  icDescription (icPostingParentText), 
        input  ? (iiBPeriodId), 
        input  icDebtorCode (icPostingOriginAddressCode), 
        input  string(iiPeriodYear, '9999':U) + '/':U + trim(icJournalCode) + string(iiVoucher, '999999999':U) (icPostingOriginDocument), 
        input  ? (icPostingOriginDocumentType), 
        input  '':U (icBatchNumber), 
        input  ? (icBankImpLineRef), 
        output viPostingId (oiPostingId), 
        output vcPostingHeaderRowId (ocRowid), 
        output viDummy (oiPostingVoucher), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.

    if oiReturnStatus < 0
    then do:
        <I-77 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
        return.
    end.

    assign tDInvoice.DInvoiceVoucher = viDummy.
end.

/* Create refrence record between DInvoice and Posting */

if not can-find(first tDInvoicePosting where
                      tDInvoicePosting.tc_ParentRowid = tDInvoice.tc_Rowid and
                      tDInvoicePosting.Posting_ID     = viPostingId)
then do:
    <M-84 run AddDetailLine
       (input  'DInvoicePosting':U (icTable), 
        input  tDInvoice.tc_Rowid (icParentRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>

    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.

    assign tDInvoicePosting.DInvoice_ID = iiDInvoiceId
           tDInvoicePosting.Posting_ID  = viPostingId.
end.


/* First delete all already generated postings except control posting, if posting was already generated */

if biPostingId <> 0 and
   not ilOnlyControlAccount
then do:
    /* Delete initial DInvoice movement to control acccount posting */
    for each tDInvoiceMovement where
        tDInvoiceMovement.tc_ParentRowid = tDInvoice.tc_Rowid:
        delete tDInvoiceMovement.
    end.
    
    assign tDInvoice.DInvoiceBalanceCreditTC = 0
           tDInvoice.DInvoiceBalanceCreditLC = 0
           tDInvoice.DInvoiceBalanceCreditCC = 0
           tDInvoice.DInvoiceBalanceDebitTC  = 0
           tDInvoice.DInvoiceBalanceDebitLC  = 0
           tDInvoice.DInvoiceBalanceDebitCC  = 0.

    /* Delete created posting */    
    find first tDInvoicePosting where
               tDInvoicePosting.tc_ParentRowid = tDInvoice.tc_Rowid and
               tDInvoicePosting.Posting_ID     = viPostingId
               no-error.

    if available tDInvoicePosting
    then do:
        <M-87 run DeleteInstanceLinesExceptGL
           (input  tDInvoicePosting.Posting_ID (iiPostingId), 
            input  ? (icGLCode), 
            output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.

        if viFcReturnSuper < 0
        then do:
            <I-88 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
            return.
        end.
    end.
end.

assign biBJournalEntryID = viBJournalEntryDIID
       biPostingId       = viPostingId.

/* Create control posting */

<M-80 run CreateDIPostingCalculateAmounts
   (input  yes (ilIsDebtorControlAccount), 
    input  icDInvoiceType (icDInvoiceType), 
    input  idInvoiceAmountTC (idInvoiceAmountTC), 
    input  idInvoiceAmountLC (idInvoiceAmountLC), 
    input  idInvoiceAmountCC (idInvoiceAmountCC), 
    input  icPostingType (icPostingType), 
    output vdDebitTC (odDebitTC), 
    output vdDebitLC (odDebitLC), 
    output vdDebitCC (odDebitCC), 
    output vdCreditTC (odCreditTC), 
    output vdCreditLC (odCreditLC), 
    output vdCreditCC (odCreditCC), 
    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>

if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.

if oiReturnStatus < 0
then do:
    <I-81 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    return.
end.

/* tax excluded : */
if ilDInvoiceIsTaxExcluded
then do:
    /* cumulate values posting on VAT */
    for each tApiDInvoiceVat:
        accumulate tApiDInvoiceVat.DInvoiceVatVatDebitTC(TOTAL).
        accumulate tApiDInvoiceVat.DInvoiceVatVatCreditTC(TOTAL).
    end.

    /* calculate remaining value to post */
    assign vdDebitTC =
               if icDInvoiceType = {&INVOICETYPE-INVOICE} or icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
               then vdDebitTC - (accum total tApiDInvoiceVat.DInvoiceVatVatDebitTC) + (accum total tApiDInvoiceVat.DInvoiceVatVatCreditTC)
               else 0
           vdCreditTC =
               if icDInvoiceType = {&INVOICETYPE-INVOICE} or icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
               then 0
               else vdCreditTC - (accum total tApiDInvoiceVat.DInvoiceVatVatCreditTC) + (accum total tApiDInvoiceVat.DInvoiceVatVatDebitTC).

    /* recalculate value to post to local currency */
    if iiCurrencyId   = viCompanyLCId or
       icCurrencyCode = vcCompanyLC
    then assign vdDebitLC  = vdDebitTC
                vdCreditLC = vdCreditTC.
    else assign vdDebitLC  = <M-91 RoundAmount
          (input  vdDebitTC * idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
           input  viCompanyLCId (iiCurrencyID), 
           input  vcCompanyLC (icCurrencyCode)) in business>
                vdCreditLC = <M-92 RoundAmount
          (input  vdCreditTC * idExchangeRate * idExchangeRateScale (idUnroundedAmount), 
           input  viCompanyLCId (iiCurrencyID), 
           input  vcCompanyLC (icCurrencyCode)) in business>.
    if idInvoiceAmountCC <> 0 and idInvoiceAmountCC <> ? then 
    do:
        if viCompanyCCId = viCompanyLCId or
           vcCompanyCC   = vcCompanyLC
        then assign vdDebitCC  = vdDebitLC
                    vdCreditCC = vdCreditLC.
        else 
        if viCompanyCCId = iiCurrencyId or
           vcCompanyCC   = icCurrencyCode                       
        then assign vdDebitCC  = vdDebitTC
                    vdCreditCC = vdCreditTC.
        else
        do:
            assign vdExRateCC      = idInvoiceAmountLC / idInvoiceAmountCC
                   vdExRateScaleCC = 1
                   vdDebitCC       = <M-101 RoundAmount
                                        (input  vdDebitLC / (vdExRateCC * vdExRateScaleCC) (idUnroundedAmount), 
                                         input  viCompanyCCId (iiCurrencyID), 
                                         input  vcCompanyCC (icCurrencyCode)) in BDInvoice>
                   vdCreditCC      = <M-102 RoundAmount
                                        (input  vdCreditLC / (vdExRateCC * vdExRateScaleCC) (idUnroundedAmount), 
                                         input  viCompanyCCId (iiCurrencyID), 
                                         input  vcCompanyCC (icCurrencyCode)) in BDInvoice>.                                 
        end.                
    end.    /*if idInvoiceAmountCC <> 0 and idInvoiceAmountCC <> ? then */
end.



if vlCompanyPropertyIsARUseTaxDate = true
then
    assign vtExchangeRateDate = itTaxPointDate.
else
    assign vtExchangeRateDate = ?.

<M-82 run AddControlPosting
   (input  vcPostingHeaderRowId (icPostingtcRowid), 
    input  tDInvoice.tcControlGLCode (icGLCode), 
    input  icControlDivisionCode (icDivisionCode), 
    input  vcDBusinessRelationInterCoCode (icIntercoBusinessRelationCode), 
    input  icCurrencyCode (icCurrencyCode), 
    input  vdDebitTC (idDebitTC), 
    input  vdDebitLC (idDebitLC), 
    input  vdDebitCC (idDebitCC), 
    input  vdCreditTC (idCreditTC), 
    input  vdCreditLC (idCreditLC), 
    input  vdCreditCC (idCreditCC), 
    input  icDIText (icText), 
    input  iiDebtorId (iiMasterId), 
    input  iiDInvoiceId (iiTransactionId), 
    input  idExchangeRate (idExchangeRate), 
    input  idExchangeRateScale (idExchangeRateScale), 
    input  idExchangeRateCC (idPostingLineCCRate), 
    input  idExchangeRateScaleCC (idPostingLineCCScale), 
    input  '':U (icAllocationKey), 
    input  icControlCostCentreCode (icCostCentreCode), 
    input  icControlProjectCode (icProjectCode), 
    input  tDefaultSafsDI (tDefaultSafs), 
    input  vtExchangeRateDate (itExchangeRateDate), 
    output viControlPostingLineId (oiPostingLineId), 
    input  false (ilLinkedCrCyDaemonReqExists), 
    output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

if viFcReturnSuper < 0 or oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.

if oiReturnStatus < 0
then do:
    <I-83 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    return.
end.

/* create posting on system suspend account if the DInvoice is deduction */

if icDInvoiceType = {&INVOICETYPE-DEDUCTION} and ilOnlyControlAccount
then do:
     <Q-373 run GLByGLSystemType (all) (Read) (NoCache)
        (input viCompanyID, (CompanyId)
         input {&GLSYSTEMTYPE-DEDUCTION}, (GLSystemTypeCode)
         input {&GLTYPECODE-SYST}, (GlTypeCode)
         input ?, (GlIsDivisionAccount)
         output dataset tqGLByGLSystemType) in BGL>

     find first tqGLByGLSystemType where
               tqGLByGLSystemType.tcGLSystemTypeCode = {&GLSYSTEMTYPE-DEDUCTION} and
               tqGLByGLSystemType.tcGLTypeCode       = {&GLTYPECODE-SYST}
               no-error.
   
     if not available tqGLByGLSystemType
     then do:  
         assign vcMessage = trim(#T-68'The system account for Deduction Suspends has not been defined':255(352726945)T-68#). 
          
         <M-44 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-922995':U (icFcMsgNumber), 
             input  '':U (icFcExplanation), 
             input  '':U (icFcIdentification), 
             input  '':U (icFcContext), 
             output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
     
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
    
        if oiReturnStatus < 0
        then do:
            <I-34 {bFcCloseInstance
                 &CLASS           = "BJournalEntry"}>
            return.
        end.
       
    end.   
   
  
    <M-434 run AddStandardPosting
       (input  vcPostingHeaderRowId (icPostingtcRowid), 
        input  tqGLByGLSystemType.tcGLCode (icGLCode), 
        input  ? (icDivisionCode), 
        input  ? (icCostCentreCode), 
        input  ? (icCostCentreText), 
        input  ? (icProjectCode), 
        input  ? (icProjectText), 
        input  ? (icIntercoBusinessRelationCode), 
        input  tDInvoice.tcCurrencyCode (icCurrencyCode), 
        input  0 (idDebitTC), 
        input  0 (idDebitLC), 
        input  0 (idDebitCC), 
        input  0 (idDebitPC), 
        input  tDInvoice.DInvoiceOriginalDebitTC (idCreditTC), 
        input  tDInvoice.DInvoiceOriginalDebitLC (idCreditLC), 
        input  tDInvoice.DInvoiceOriginalDebitCC (idCreditCC), 
        input  0 (idCreditPC), 
        input  0 (idQty), 
        input  icDIText (icLineText), 
        input  ? (icSafText), 
        input  tDefaultSafsDI (tDefaultSafs), 
        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateType), 
        input  tDInvoice.DInvoiceExchangeRate (idExchangeRate), 
        input  tDInvoice.DInvoiceRateScale (idExchangeRateScale), 
        input  tDInvoice.DInvoiceCCRate (idPostingLineCCRate), 
        input  tDInvoice.DInvoiceCCScale (idPostingLineCCScale), 
        output viSystemPostingLineID (oiPostingLineId), 
        input  ? (iiSafStructureId), 
        input  ? (icSafStructureCode), 
        input  ? (icAllocationKey), 
        input  false (ilLinkedCrCyDaemonReqExists), 
        input  vtExchangeRateDate (itExchangeRateDate), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
        
        if oiReturnStatus < 0
        then do:
            <I-29 {bFcCloseInstance
                 &CLASS           = "BJournalEntry"}>
            return.
        end.
end.

/* Create initial DInvoice movement to cotnrol posting */

<M-85 run CreateDIMovementInitial
   (input  iiDInvoiceId (iiDInvoiceId), 
    input  icDInvoiceType (icDInvoiceType), 
    input  icPostingType (icPostingType), 
    input  viControlPostingLineId (iiControlPostingLineId), 
    input  idInvoiceAmountTC (idInvoiceAmountTC), 
    input  vdDInvoiceAmountCC (idInvoiceAmountCC), 
    input  idInvoiceAmountLC (idInvoiceAmountLC), 
    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
if viFcReturnSuper < 0 or
   oiReturnStatus  = 0 and viFcReturnSuper > 0
then assign oiReturnStatus = viFcReturnSuper.

assign biBJournalEntryID = viBJournalEntryDIID
       biPostingId       = viPostingId.

/* read debtor when taxable or when sales account gl profile id is zero and working with drafts */
if (((icDInvoiceType = {&INVOICETYPE-INVOICE}           or 
      icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or 
      icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
      icDInvoiceType = {&INVOICETYPE-CREDITNOTE}        or 
      icDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}) and 
      ilIsTaxable)                                           OR
     (not ilOnlyControlAccount and
      iiSalesAccGLProfileId = 0 and
      viCurrentDraftInstanceId <> 0))                        AND
   iiDebtorId <> 0
then do:
    <Q-100 run DebtorByIDAllInfo (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input iiDebtorId, (DebtorID)
        output dataset tqDebtorByIDAllInfo) in BDebtor >
    find first tqDebtorByIDAllInfo where
               tqDebtorByIDAllInfo.tiDebtor_ID = iiDebtorId
               no-error.
    if available tqDebtorByIDAllInfo
    then assign vcShipToTaxIDFederal      = tqDebtorByIDAllInfo.tcDebtorTaxIDFederal
                vcShipToTaxIDState        = tqDebtorByIDAllInfo.tcDebtorTaxIDState
                vcShipToTaxIDMisc1        = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc1
                vcShipToTaxIDMisc2        = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc2
                vcShipToTaxIDMisc3        = tqDebtorByIDAllInfo.tcDebtorTaxIDMisc3
                viShipToTaxDeclaration    = tqDebtorByIDAllInfo.tiDebtorTaxDeclaration
                vcDebtorTxzTaxZone        = tqDebtorByIDAllInfo.tcTxzTaxZone
                viSalesAccountGLProfileId = tqDebtorByIDAllInfo.tiSalesAccountGLProfile_ID
                viFinChgGLProfileId       = tqDebtorByIDAllInfo.tiFinChgGLProfile_ID.
end.
else assign vcShipToTaxIDFederal      = "":U
            vcShipToTaxIDState        = "":U
            vcShipToTaxIDMisc1        = "":U
            vcShipToTaxIDMisc2        = "":U
            vcShipToTaxIDMisc3        = "":U
            viShipToTaxDeclaration    = 0
            vcDebtorTxzTaxZone        = "":U
            viSalesAccountGLProfileId = 0
            viFinChgGLProfileId       = 0.

/* Create VAT posting */

if (icDInvoiceType = {&INVOICETYPE-INVOICE}           or 
   icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or 
   icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} or
   icDInvoiceType = {&INVOICETYPE-CREDITNOTE}        or 
   icDInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}) and 
   ilIsTaxable
then do:
    <M-96 run CreateDIPostingGetShipFrom
       (input  iiShipFromAddressId (iiShipFromAddressId), 
        output vcShipFromTaxIDFederal (ocShipFromTaxIDFeder), 
        output vcShipFromTaxIDState (ocShipFromTaxIDState), 
        output vcShipFromTaxIDMisc1 (ocShipFromTaxIdMisc1), 
        output vcShipFromTaxIDMisc2 (ocShipFromTaxIdMisc2), 
        output vcShipFromTaxIDMisc3 (ocShipFromTaxIdMisc3), 
        output viShipFromTaxDeclaration (oiShipFromTaxDeclaration), 
        output vcShipFromTxzTaxZone (ocShipFromTaxZone), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then return.

    assign vdDebitAmountTC  = if biPostingId = 0 
                              then if icDInvoiceType = {&INVOICETYPE-INVOICE} or icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                   then 0 
                                   else idInvoiceAmountTC 
                              else 0
           vdCreditAmountTC = if biPostingId = 0 
                              then if icDInvoiceType = {&INVOICETYPE-INVOICE} or icDInvoiceType = {&INVOICETYPE-FINANCECHARGE} or icDInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                   then idInvoiceAmountTC 
                                   else 0 
                              else 0.
    <M-89 run CreateDIPostingTax
       (input  vcPostingHeaderRowId (icRowId), 
        input  biPostingId (iiPostingId), 
        input  itTaxPointDate (itTaxPointDate), 
        input  icDIText (icDIText), 
        input  icCurrencyCode (icCurrencyCode), 
        input  iiCurrencyId (iiCurrencyId), 
        input  {&EXCHANGERATETYPE-ACCOUNTING-TR} (icExchangeRateType), 
        input  idExchangeRate (idExchangeRate), 
        input  idExchangeRateScale (idExchangeRateScale), 
        input  vdDebitAmountTC (idVatFullDebitTC), 
        input  vdCreditAmountTC (idVatFullCreditTC), 
        input  icDInvoiceType (icDInvoiceType), 
        input  icControlDivisionCode (icDivisionCode), 
        input  '':U (icVatCurrencyCode), 
        input  0 (iiVatCurrencyId), 
        input  0 (idVatExchangeRate), 
        input  0 (idVatExchangeRateScale), 
        input  icShipFromCountryCode (icShipFromCountryCode), 
        input  icShipToCountryCode (icShipToCountryCode), 
        input  ilShipFromCountryIsEUCountry (ilShipFromCountryIsEUCountry), 
        input  ilShipToCountryIsEUCountry (ilShipToCountryIsEUCountry), 
        input  icTxclTaxCls (icTxclTaxCls), 
        input  icTxuTaxUsage (icTxuTaxUsage), 
        input  vcShipFromTxzTaxZone (icShipFromTaxZone), 
        input  icShipToTxzTaxZone (icShipToTxzTaxZone), 
        input  icTxenvTaxEnv (icTxenvTaxEnv), 
        input  iiVoucher (iiVoucher), 
        input  icPaymentCondition (icPaymentCondition), 
        input  ilIsTaxInCityFromShipTo (ilIsTaxInCityFromShipTo), 
        input  tDInvoice.tcControlGLCode (icControlGLCode), 
        input  iiDebtorId (iiMasterId), 
        input  iiDInvoiceId (iiTransactionId), 
        input  ilIsTaxable (ilIsTaxable), 
        input  icControlCostCentreCode (icCostCentreCode), 
        input  icControlProjectCode (icProjectCode), 
        input  ? (iiShipFromAddressId), 
        input  ? (iiShipToAddressId), 
        input  icSoldToDebtorCode (icSoldToDebtorCode), 
        input  idInvoiceAmountTC (idInvoiceAmountTC), 
        input  vcShipFromTaxIDFederal (icShipFromTaxIDFeder), 
        input  vcShipFromTaxIDState (icShipFromTaxIDState), 
        input  vcShipFromTaxIDMisc1 (icShipFromTaxIDMisc1), 
        input  vcShipFromTaxIDMisc2 (icShipFromTaxIDMisc2), 
        input  vcShipFromTaxIDMisc3 (icShipFromTaxIDMisc3), 
        input  viShipFromTaxDeclaration (iiShipFromTaxDeclaration), 
        input  vcShipToTaxIDFederal (icShipToTaxIDFeder), 
        input  vcShipToTaxIDState (icShipToTaxIDState), 
        input  vcShipToTaxIDMisc1 (icShipToTaxIDMisc1), 
        input  vcShipToTaxIDMisc2 (icShipToTaxIDMisc2), 
        input  vcShipToTaxIDMisc3 (icShipToTaxIDMisc3), 
        input  viShipToTaxDeclaration (iiShipToTaxDeclaration), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper < 0 or oiReturnStatus  = 0
    then assign oiReturnStatus = viFcReturnSuper.

    if oiReturnStatus < 0
    then do:
        <I-61 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
        return.
    end.
end.

/* Create posting on Sales Account */
/* for deduction invoice , we do not need to create sales part.*/

if not ilOnlyControlAccount /* and icDInvoiceType <> {&INVOICETYPE-DEDUCTION} */
then do:
    /* to make sure you can complete drafts, fill the input parameters with fields of customer */
    if iiSalesAccGLProfileId = 0 and viCurrentDraftInstanceId <> 0
    then assign iiSalesAccGLProfileId = viSalesAccountGLProfileId
                iiFinChgGLProfileId   = viFinChgGLProfileId.

    if iiSalesAccGLProfileId <> 0
    then do:
        assign viSalesProfileID = if icDInvoiceType EQ {&INVOICETYPE-FINANCECHARGE}
                                  then iiFinChgGLProfileId
                                  else iiSalesAccGLProfileId.
            
        <M-86 run CreateDIPostingSales
           (input  icGLCode (icGLCode), 
            input  icDInvoiceType (icDInvoiceType), 
            input  idInvoiceAmountTC (idInvoiceAmountTC), 
            input  idInvoiceAmountCC (idInvoiceAmountCC), 
            input  idInvoiceAmountLC (idInvoiceAmountLC), 
            input  iiCurrencyId (iiCurrencyId), 
            input  icCurrencyCode (icCurrencyCode), 
            input  idExchangeRate (idExchangeRate), 
            input  idExchangeRateScale (idExchangeRateScale), 
            input  viSalesProfileID (iiDebtorSalesAccountGLProfileId), 
            input  iiDivisionId (iiDivisionId), 
            input  icDivisionCode (icDivisionCode), 
            input  icCostCentreCode (icCostCentreCode), 
            input  icProjectCode (icProjectCode), 
            input  icDIText (icDIText), 
            input  icPostingType (icPostingType), 
            input  ilDInvoiceIsTaxExcluded (ilDInvoiceIsTaxExcluded), 
            input  idExchangeRateCC (idExchangeRateCC), 
            input  idExchangeRateScaleCC (idExchangeRateScaleCC), 
            input  itTaxPointDate (itTaxPointDate), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
    end.
end.

/* ============================================================================== */
/* For Opening Balances we do not close the BJE as this is not needed             */
/* ============================================================================== */
assign vlCloseBJE       = yes
       viProgramNameCnt = 2.
do while program-name (viProgramNameCnt) <> ? :

    if program-name (viProgramNameCnt) matches '*OpenBalance*'
    then do:
         assign vlCloseBJE = no.
         leave.
    end.    
    assign viProgramNameCnt = viProgramNameCnt + 1.
    
end. /* do while program-name (viProgramNameCnt) <> ? */

if vlCloseBJE = yes
then do :
    <I-23 {bFcCloseInstance
         &CLASS           = "BJournalEntry"}>
end.

if oiReturnStatus < 0
then do:
    <M-95 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    return.
end.