project QadFinancials > class BExpenseNote > method APIExpNoteRegister

Description

Method to register expense notes. Following actions are taken: 1. New CInvoice is created based on the expense note. 2. The invoice is allocated immediately. 3. The invoice may be compensated with linked advances.


Parameters


t_sExpNoteIdsinputtemp-table
iiAccYearinputinteger
iiAccPeriodinputinteger
itRegistrationDateinputdate
icInvoiceJournalCodeinputcharacter
icAdjustmentJournalCodeinputcharacter
icDescriptioninputcharacter
icAllocationJournalCodeinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/bexpensenote.p)

/* Load the selected activity lines */
<M-1 run MyDataLoad (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    return.
end.

if not can-find(first tExpNote where 
         tExpNote.ExpNoteStatusCode   = {&EXPENSENOTESTATUS-CONFIRMED} AND
         tExpNote.tc_Status <> "D":U)
then do:
    assign oiReturnStatus = 0.
    return.
end.
                          
/* Get MF Posting Defaults */
<M-84 run GetCirecCode (output vcCIRECCode (ocCIRECCode), 
                        output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
if vcCIRECCode = "":U
then do:
    <M-59 run SetMessage
       (input  trim(#T-81'Unmatched Invoices account cannot be found.':80(1919)T-81#) (icMessage), 
        input  '':U (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'QADFIN-2951':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    assign oiReturnStatus = min(-1,viFcReturnSuper).
    return.
end.

empty temp-table tMovementEN.
empty temp-table tAdjustments.
empty temp-table tMFPostingENRef.
empty temp-table tCreatedCInvoice.
empty temp-table tAllocCInvoice.
empty temp-table tOpenBalanceEN.

if viBJournalEntry8ID = 0 or
   viBJournalEntry8ID = ?
then do:
    <I-60 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BJournalEntry"}>
    assign vlBJournalEntryIsStartedFromEN = true.
end.
else do:
    <I-61 {bFcOpenInstance
            &CLASS           = "BJournalEntry"}>
end.

assign vlIsBCInvoiceInstanceOpened = false.
assign vlIsBJournalEntryInstanceOpened = true.

/* ---------------------------------------------------------------------- */
/* CALC_BLOCK                                                             */
/* 1) Fill-in following temp-tables:                                      */
/*   tOpenBalanceEN     - New CInvoices that are to be created            */
/*   tAdjustments       - Possible compensations (invoice adjustments)    */
/*   tMFPostingENRef    - References between ExpNote and MF postings      */
/*                      - Necessary for correct linking tAllocCInvoice    */
/*   tAllocCInvoice     - Reference to MF postings (BCInvoice.Allocate..) */
/* 2) Prepare MF Postings                                                 */
/* 3) Prepare Adjustment Postings                                         */
/* ---------------------------------------------------------------------- */
<Q-70 run CreditorByPaysel (Start) in BCreditor >
<Q-86 run CurrencyById  (Start) in BCurrency >

CALC_BLOCK:
for each tExpNote where 
         tExpNote.ExpNoteStatusCode   = {&EXPENSENOTESTATUS-CONFIRMED} AND
         tExpNote.tc_Status <> "D":U:
    
    empty temp-table tPostingLineEN.
    empty temp-table tDefaultSafsEN.

    assign tExpNote.ExpNoteStatusCode       = {&EXPENSENOTESTATUS-REGISTERED}
           tExpNote.ExpNoteRegistrationDate = itRegistrationDate
           tExpNote.RegistrationUsr_ID      = viUsrId   
           tExpNote.tc_Status               = "C":U.
    
    <Q-72 run CreditorByPaysel (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input ?, (Creditor_ID)
           input ?, (CreditorCode)
           input tExpNote.Employee_ID, (Employee_ID)
           output dataset tqCreditorByPaysel) in BCreditor >

    find first tqCreditorByPaysel where
               tqCreditorByPaysel.tiDivisionProfile_ID <> 0 and
               tqCreditorByPaysel.tiDivisionProfile_ID <> ? 
               no-error.
    if not available tqCreditorByPaysel
    then do:
        assign vcMessage = substitute(#T-83'Supplier for employee &1 cannot be found.':100(13574)T-83#, tExpNote.tcEmployeeCode).
        <M-73 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'D':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2962':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
        assign oiReturnStatus = -1.
        leave CALC_BLOCK.
    end.
    
    <Q-89 run GetDivisionFromProfile (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input tqCreditorByPaysel.tiDivisionProfile_ID, (DivisionProfileId)
           output dataset tqDivisionFromProfile) in BProfile >
    find first tqDivisionFromProfile no-error.

    <Q-87 run CurrencyById (all) (Read) (NoCache)
          (input tExpNote.Currency_ID, (CurrencyId)
           output dataset tqCurrencyById) in BCurrency >
    find first tqCurrencyById no-error.

    /* Retrieve Linked Advances - tAdvances */
    <M-8 run RefreshAdvances (input  false (ilMerge), 
                          input  tExpNote.ExpNote_ID (iiExpenseNote_ID), 
                          input  tExpNote.tcEmployeeCode (icEmployeeCode), 
                          output tAdvances (tAdvances), 
                          output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>

    /* OnExpNoteTC will be re-calculated by GetProposedAdvanceLink */
    for each tAdvances:
        assign tAdvances.tdOnExpNoteTC = 0.
    end.

    /* Change status of ExpNoteCInvoice */
    for each tExpNoteCInvoice where
             tExpNoteCInvoice.ExpNote_ID = tExpNote.ExpNote_ID and
             tExpNoteCInvoice.tc_Status <> "D":U:
        assign tExpNoteCInvoice.ExpNoteCInvoiceStatusCode = {&EXPENSENOTESTATUS-REGISTERED}
               tExpNoteCInvoice.tc_Status = (if tExpNoteCInvoice.tc_Status = "":U
                                             then "C":U
                                             else tExpNoteCInvoice.tc_Status).
    end.

    assign vdExpNoteTotalCreditTC = 0
           vdExpNoteTotalCreditLC = 0
           vdExpNoteTotalDebitXC  = 0.

    for each tExpNoteLine where
             tExpNoteLine.ExpNote_ID = tExpNote.ExpNote_ID and
             tExpNoteLine.ExpNoteLineAmountTC <> 0
             by tExpNoteLine.tcCurrencyCode
             by tExpNoteLine.ExpNoteLineDate
             by tExpNoteLine.ExpNoteLine_ID:

        empty temp-table tExpNoteLineRef.
        empty temp-table tProposedAdvanceLink.

        create tExpNoteLineRef.
        buffer-copy tExpNoteLine to tExpNoteLineRef.

        <M-9 run GetProposedAdvanceLink (input  tExpNoteLineRef (t_sExpNoteLineRef), 
                                 input-output tAdvances (t_sAdvances), 
                                 output tProposedAdvanceLink (tProposedAdvanceLink), 
                                 input  false (ilCleanOnExpNoteTC), 
                                 output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
        
        assign vdProposedAdvanceAmountTC = 0
               vdProposedAdvanceAmountLC = 0.

        for each tProposedAdvanceLink:
            assign vdProposedAdvanceAmountTC = vdProposedAdvanceAmountTC + if tProposedAdvanceLink.tdAmountTC <> ? then tProposedAdvanceLink.tdAmountTC else 0
                   vdProposedAdvanceAmountLC = vdProposedAdvanceAmountLC + if tProposedAdvanceLink.tdAmountLC <> ? then tProposedAdvanceLink.tdAmountLC else 0.
            
            /* Adjustment of Advance */
            create tAdjustments.
            assign tAdjustments.tiAdvCInvoice_ID        = tProposedAdvanceLink.tiCInvoice_ID
                   tAdjustments.tiAdjExpNote_ID         = tExpNote.ExpNote_ID
                   tAdjustments.tcPostingText           = string(tProposedAdvanceLink.tiAdvanceYear,"9999":U) + "/":U +
                                                          string(tProposedAdvanceLink.tcAdvanceJournal,"x(4)":U) + "/":U +
                                                          string(tProposedAdvanceLink.tiAdvanceVoucher,"999999":U) 
                   tAdjustments.tcAdjGLCode             = tqCreditorByPaysel.tcInvControlGlCode
                   tAdjustments.tcAdjDivisionCode       = if available tqDivisionFromProfile
                                                          then tqDivisionFromProfile.tcDivisionCode
                                                          else "":U
                   tAdjustments.tcAdvCurrencyCode       = tProposedAdvanceLink.tcAdvanceCurrencyCode
                   tAdjustments.tcInvCurencyCode        = tExpNote.tcCurrencyCode
                   tAdjustments.tcAdjIntercompanyCode   = tqCreditorByPaysel.tcBusinessRelationICCode
                   tAdjustments.tdAdjAmountLC           = (if tProposedAdvanceLink.tdAmountLC <> ? 
                                                           then tProposedAdvanceLink.tdAmountLC 
                                                           else 0)
                   tAdjustments.tdAdvAmountTC           = (if tProposedAdvanceLink.tdAmountTC <> ? 
                                                           then tProposedAdvanceLink.tdAmountTC 
                                                           else 0)
                   tAdjustments.tdAdvExchangeRate       = tExpNoteLine.ExpNoteLineLCRate
                   tAdjustments.tdAdvExchangeRateScale  = tExpNoteLine.ExpNoteLineLCScale.
                               
            case tAdjustments.tcInvCurencyCode:
                when vcCompanyLC
                then assign tAdjustments.tdInvAmountTC              = tAdjustments.tdAdjAmountLC
                            tAdjustments.tdInvExchangeRate          = 1 
                            tAdjustments.tdInvExchangeRateScale     = 1.
                when tAdjustments.tcAdvCurrencyCode
                then assign tAdjustments.tdInvAmountTC              = tAdjustments.tdAdvAmountTC
                            tAdjustments.tdInvExchangeRate          = tAdjustments.tdAdvExchangeRate 
                            tAdjustments.tdInvExchangeRateScale     = tAdjustments.tdAdvExchangeRateScale.                            
                otherwise do:
                    assign tAdjustments.tdInvAmountTC               = tAdjustments.tdAdjAmountLC *
                                                                      tExpNote.ExpNoteRateLCXC *
                                                                      tExpNote.ExpNoteRateScale
                           tAdjustments.tdInvExchangeRate           = tExpNote.ExpNoteRateLCXC 
                           tAdjustments.tdInvExchangeRateScale      = tExpNote.ExpNoteRateScale.
                end.
            end case.

            if available tqCurrencyById
            then assign tAdjustments.tdInvAmountTC = 
                                                                            <M-106 RoundAmount
          (input  tAdjustments.tdInvAmountTC (idUnroundedAmount), 
           input  tqCurrencyById.tiCurrency_ID (iiCurrencyID), 
           input  tqCurrencyById.tcCurrencyCode (icCurrencyCode)) in business>.

            /* calculate total comensation */
            if tProposedAdvanceLink.tcAdvanceCurrencyCode = tExpNote.tcCurrencyCode
            then assign vdExpNoteTotalDebitXC = vdExpNoteTotalDebitXC + if tProposedAdvanceLink.tdAmountTC <> ? then tProposedAdvanceLink.tdAmountTC else 0.
            else do:
                if tExpNote.tcCurrencyCode = vcCompanyLC
                then assign vdExpNoteTotalDebitXC = vdExpNoteTotalDebitXC + if tProposedAdvanceLink.tdAmountLC <> ? then tProposedAdvanceLink.tdAmountLC else 0.
                else assign vdExpNoteTotalDebitXC = vdExpNoteTotalDebitXC + if tProposedAdvanceLink.tdAmountLC <> ? then tProposedAdvanceLink.tdAmountLC * tExpNote.ExpNoteRateLCXC * tExpNote.ExpNoteRateScale else 0.    
            end.            
        end.
        
        /* Prepare Allocation Posting Lines - Expense Accounts */
        create tPostingLineEN.
        assign tPostingLineEN.tiExpNoteLine_ID          = tExpNoteLine.ExpNoteLine_ID
               tPostingLineEN.tcGLCode                  = tExpNoteLine.tcGLCode
               tPostingLineEN.tcProjectCode             = tExpNoteLine.tcProjectCode
               tPostingLineEN.tcCostCentreCode          = tExpNoteLine.tcCostCentreCode
               tPostingLineEN.tcDivisionCode            = tExpNoteLine.tcDivisionCode
               tPostingLineEN.tcPostingLineDescription  = tExpNoteLine.ExpNoteLineDescription
               tPostingLineEN.tcCurrencyCode            = tExpNote.tcCurrencyCode
               tPostingLineEN.tdDebitLC                 = vdProposedAdvanceAmountLC 
                                                + ((tExpNoteLine.ExpNoteLineAmountTC - vdProposedAdvanceAmountTC)
                                                / tExpNoteLine.ExpNoteLineAmountTC * tExpNoteLine.ExpNoteLineAmountLC).
        
        /* TC of posting line is the pay-out currence of the entire exp note */
        case tExpNote.tcCurrencyCode:
            when vcCompanyLC
            then assign tPostingLineEN.tdDebitTC = tPostingLineEN.tdDebitLC
                        tPostingLineEN.tdExchangeRateXCLC       = 1 
                        tPostingLineEN.tdExchangeRateScaleXCLC  = 1.
            when tExpNoteLine.tcCurrencyCode
            then assign tPostingLineEN.tdDebitTC               = tExpNoteLine.ExpNoteLineAmountTC
                        tPostingLineEN.tdExchangeRateXCLC      = tExpNoteLine.ExpNoteLineLCRate
                        tPostingLineEN.tdExchangeRateScaleXCLC = tExpNoteLine.ExpNoteLineLCScale.
            otherwise do:
                assign tPostingLineEN.tdDebitTC                 = tPostingLineEN.tdDebitLC * 
                                                                  tExpNote.ExpNoteRateLCXC * 
                                                                  tExpNote.ExpNoteRateScale
                       tPostingLineEN.tdExchangeRateXCLC        = tExpNote.ExpNoteRateLCXC 
                       tPostingLineEN.tdExchangeRateScaleXCLC   = tExpNote.ExpNoteRateScale.
            end.
        end case.

        assign tPostingLineEN.tdDebitLC = /*round(tPostingLineEN.tdDebitLC,viCompanyLCDec)*/
                                                          <M-107 RoundAmount
          (input  tPostingLineEN.tdDebitLC (idUnroundedAmount), 
           input  viCompanyLCid (iiCurrencyID), 
           input  vcCompanyLC (icCurrencyCode)) in business>
               tPostingLineEN.tdDebitTC = (if available tqCurrencyById
                                           then 
                                                  <M-108 RoundAmount
          (input  tPostingLineEN.tdDebitTC (idUnroundedAmount), 
           input  tqCurrencyById.tiCurrency_ID (iiCurrencyID), 
           input  tqCurrencyById.tcCurrencyCode (icCurrencyCode)) in business>
                                           else tPostingLineEN.tdDebitTC).

        assign vdExpNoteTotalCreditLC = vdExpNoteTotalCreditLC + 
                                        (if tPostingLineEN.tdDebitLC <> ?
                                         then tPostingLineEN.tdDebitLC
                                         else 0)
               vdExpNoteTotalCreditTC = vdExpnoteTotalCreditTC +
                                        (if tPostingLineEN.tdDebitTC <> ?
                                         then tPostingLineEN.tdDebitTC
                                         else 0).
    end. /* for each tExpNoteLine */
    
    create tPostingLineEN.
    assign tPostingLineEN.tcGLCode                  = vcCIRECCode
           tPostingLineEN.tcCurrencyCode            = tExpNote.tcCurrencyCode
           tPostingLineEN.tcPostingLineDescription  = subst("&1/&2":U,
                                                            string(tExpNote.ExpNoteYear,"9999":U),
                                                            string(tExpNote.ExpNoteNumber,"999999":U))
           tPostingLineEN.tdExchangeRateXCLC        = tExpNote.ExpNoteRateLCXC
           tPostingLineEN.tdExchangeRateScaleXCLC   = tExpNote.ExpNoteRateScale
           tPostingLineEN.tdCreditTC                = vdExpNoteTotalCreditTC
           tPostingLineEN.tdCreditLC                = vdExpNoteTotalCreditLC.
           
    /* AddPostingHeader - MF Posting */
    <M-13 run AddPostingHeader
       (input  ? (iiCompanyId), 
        input  iiAccYear (iiPeriodYear), 
        input  iiAccPeriod (iiPeriodPeriod), 
        input  icAllocationJournalCode (icJournalCode), 
        input  '' (icReportingJournalCode), 
        input  0 (iiVoucher), 
        input  itRegistrationDate (itPostingDate), 
        input  ? (itValueDate), 
        input  icDescription (icPostingText), 
        input  ? (icPostingBusinessRelationText), 
        input  ? (icPostingInvoiceReferenceText), 
        input  ? (icPostingParentText), 
        input  ? (iiBPeriodId), 
        input  ? (icPostingOriginAddressCode), 
        input  ? (icPostingOriginDocument), 
        input  ? (icPostingOriginDocumentType), 
        input  '':U (icBatchNumber), 
        input  ? (icBankImpLineRef), 
        output viPosting_ID (oiPostingId), 
        output vcPostingRowid (ocRowid), 
        output viPostingVoucher (oiPostingVoucher), 
        output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>

    if viFcReturnSuper < 0
    then do:
        assign oiReturnStatus = min(-1, viFcReturnSuper).
        leave CALC_BLOCK.
    end.
    
    /* Create MF posting lines */
    <M-105 run ExpNoteRegisterPosting
          (input  icInvoiceJournalCode (icInvoiceJournalCode), 
           input  viPosting_ID (iiPosting_ID), 
           input  itRegistrationDate (itRegistrationDate), 
           input  iiAccYear (iiAccYear), 
           input  tqCreditorByPaysel.tiReason_ID (iiReason_ID), 
           input  vcPostingRowid (icPostingRowid), 
           input  vcCIRECCode (icCIRECCode), 
           output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    if viFcReturnSuper < 0
    then do:
        assign oiReturnStatus = min(-1, viFcReturnSuper).
        leave CALC_BLOCK.
    end.

    /* empty temp-table tOpenBalanceEN. */
    create tOpenBalanceEN.
    assign tOpenBalanceEN.tcAction                      = {&DAEMONACTION-SAVE}
           tOpenBalanceEN.tlOnlyControlAccount          = false
           tOpenBalanceEN.tcInvoiceType                 = {&INVOICETYPE-INVOICE}
           tOpenBalanceEN.tcCreditorCode                = tqCreditorByPaysel.tcCreditorCode
           tOpenBalanceEN.tcBusinessRelationCode        = tqCreditorByPaysel.tcBusinessRelationCode
           tOpenBalanceEN.tiCompanyId                   = viCompanyId
           tOpenBalanceEN.tcTransferAccountGLCode       = vcCIRECCode
           tOpenBalanceEN.tdInvoiceAmountLC             = vdExpNoteTotalCreditLC
           tOpenBalanceEN.tdInvoiceAmountTC             = vdExpNoteTotalCreditTC
           tOpenBalanceEN.tdExchangeRateTCLC            = vdExpNoteTotalCreditLC / vdExpNoteTotalCreditTC
           tOpenBalanceEN.tdExchangeRateScale           = tExpNote.ExpNoteRateScale
           tOpenBalanceEN.tiPeriodYear                  = iiAccYear
           tOpenBalanceEN.tiPeriodPeriod                = iiAccPeriod
           tOpenBalanceEN.tcJournalCode                 = icInvoiceJournalCode
           tOpenBalanceEN.ttInvoiceDate                 = itRegistrationDate
           tOpenBalanceEN.ttPostingDate                 = itRegistrationDate
           tOpenBalanceEN.tcPostingType                 = {&POSTINGTYPE-CREDIT}           
           tOpenBalanceEN.ttInvoiceTaxPointDate         = itRegistrationDate
           tOpenBalanceEN.tcInvoiceReference            = substitute(#T-82'&1/&2':40(1920)t-82#,
                                                                     string(tExpNote.ExpNoteYear,"9999":U),
                                                                     string(tExpNote.ExpNoteNumber,"999999":U))
           tOpenBalanceEN.tcInvoiceDescription          = tExpNote.ExpNoteDescription
           tOpenBalanceEN.tcPostingText                 = tOpenBalanceEN.tcInvoiceReference
           tOpenBalanceEN.tcTCCurrencyCode              = tExpNote.tcCurrencyCode
           tOpenBalanceEN.tcNormalPaymentConditionCode  = tqCreditorByPaysel.tcNormalPaymentConditionCode
           tOpenBalanceEN.tcKey                         = string(tExpNote.ExpNote_ID).

    <M-77 run CalcDueDate (input  tOpenBalanceEN.tcNormalPaymentConditionCode (icPaymentConditionCode), 
                       input  itRegistrationDate (itPostingDate), 
                       output tOpenBalanceEN.ttInvoiceDueDate (otDueDate), 
                       output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = min(-1, viFcReturnSuper).
        leave CALC_BLOCK.
    end.

    assign vdInvoiceAmountTC = tOpenBalanceEN.tdInvoiceAmountTC.
end.    

<Q-88 run CurrencyById  (Stop) in BCurrency >
<Q-71 run CreditorByPaysel (Stop) in BCreditor >

/* Check Error Status */
if oiReturnStatus < 0
then do:
    if vlIsBJournalEntryInstanceOpened
    then do:
       <I-98 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    end.
    <M-109 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    return.
end.

/* Open Instance */
if viBCInvoice5ID = 0 or
   viBCInvoice5ID = ?
then do:
    <I-45 {bFcStartAndOpenInstance
            &ADD-TO-TRANSACTION = "true"
            &CLASS              = "BCInvoice"}>
    assign vlBCInvoiceIsStartedFromExpNote = true.
end.
else do:
    <I-46 {bFcOpenInstance
            &CLASS           = "BCInvoice"}>
end.
assign vlIsBCInvoiceInstanceOpened = true.

/* -------------------------------------------------------------- */
/* Following Actions:                                             */
/* 1) Create new invoices (1 ExpNote = 1 CInvoice)                */
/* 2) Add links in ExpNoteCInvoice                                */
/* 3) Switch the flag CInvoice.CInvoiceIsSelected                 */
/* 4) Process MF posting (BCInvoice.AllocateCInvoices)            */
/* 5) Create invoice adjustments (compensations with advances)    */
/* -------------------------------------------------------------- */

<M-76 run ExpNoteRegister (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if vlIsBJournalEntryInstanceOpened
    then do:
       <I-97 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    end.
    if vlIsBCInvoiceInstanceOpened
    then do:
        <I-104 {bFcCloseInstance
            &CLASS           = "BCInvoice"}>
    end.
    <M-110 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    return.
end.

/* set it in comment, not used anymore
/* Update CInvoicePosting - MF Posting */
<M-66 run AllocateCInvoices
   (input-output tAllocCInvoice (tAllocateCInvoice), 
    input  ? (tApiCInvoiceVat), 
    input  ? (tApiCInvoiceWHT), 
    input  viBJournalEntry8ID (iiBJournalEntryId), 
    output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = min(-1, viFcReturnSuper).
    if vlIsBJournalEntryInstanceOpened
    then do:
       <I-96 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    end.
    if vlIsBCInvoiceInstanceOpened
    then do:
         <I-103 {bFcCloseInstance
            &CLASS           = "BCInvoice"}>
    end.
    <M-111 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    return.
end.
  */
<M-74 run CreateAdjustments (input  iiAccYear (iiAccYear), 
                             input  iiAccPeriod (iiAccPeriod), 
                             input  icAdjustmentJournalCode (icAdjustmentJournalCode), 
                             input  itRegistrationDate (itRegistrationDate), 
                             input  icDescription (icDescription), 
                             output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
if viFcReturnSuper < 0
then do:
    assign oiReturnStatus = min(-1, viFcReturnSuper).
    if vlIsBJournalEntryInstanceOpened
    then do:
       <I-95 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
    end.
    if vlIsBCInvoiceInstanceOpened
    then do:
         <I-102 {bFcCloseInstance
            &CLASS           = "BCInvoice"}>
    end.
    <M-112 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
    return.    
end.

if can-find(first tMovementEN)
then do:
    
    empty temp-table tCDocInvoiceXrefStage.
    <M-40 run CreateCInvoiceMovements
       (input-output tMovementEN (tMovement), 
        input  tCDocInvoiceXrefStage (tCInvoiceStageUpdates), 
        input-output viBJournalEntry8ID (biBJournalEntryId), 
        input  false (ilClearData), 
        input  ? (itPaymentTaxPointDate), 
        input  false (ilIsCallFromBankingEntryOrOIAdj), 
        output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = min(-1, viFcReturnSuper).
        if vlIsBJournalEntryInstanceOpened
        then do:
             <I-100 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
        end.
        if vlIsBCInvoiceInstanceOpened
        then do:
            <I-101 {bFcCloseInstance
            &CLASS           = "BCInvoice"}>
        end.
        <M-113 run StopExternalInstances
           (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
        return.
    end.
end.

/* vlIsCalledFromAPIMethod is there just to tell the validate method not to kill the external instances */
assign vlIsCalledFromAPIMethod = true.
<M-2 run ValidateBCAndAdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
assign vlIsCalledFromAPIMethod = false.

assign oiReturnStatus = if (oiReturnStatus  > 0 and 
                            viFcReturnSuper = 0) or 
                            oiReturnStatus  < 0
                        then oiReturnStatus
                        else viFcReturnSuper.

if vlIsBJournalEntryInstanceOpened
then do:
    if vlBJournalEntryIsStartedFromEN
    then do:
        <M-29 run ValidateBCAndAdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
        if viFcReturnSuper < 0
        then assign oiReturnStatus = min(-1, viFcReturnSuper).
    end.

    <I-50 {bFcCloseInstance
            &CLASS           = "BJournalEntry"}>
end.
if vlIsBCInvoiceInstanceOpened
then do:
    if vlBCInvoiceIsStartedFromExpNote
    then do:
        <M-30 run ValidateBCAndAdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
        if viFcReturnSuper < 0
        then assign oiReturnStatus = min(-1, viFcReturnSuper).
    end.

    <I-28 {bFcCloseInstance
            &CLASS           = "BCInvoice"}>
end.

if oiReturnStatus < 0
then do:
    /* stop all external instances */
    <M-114 run StopExternalInstances
       (output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
end.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BExpenseNote".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "APIExpNoteRegister".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/bexpensenote.apiexpnoteregister.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::iiAccYear = <parameter value>
       vhParameter::iiAccPeriod = <parameter value>
       vhParameter::itRegistrationDate = <parameter value>
       vhParameter::icInvoiceJournalCode = <parameter value>
       vhParameter::icAdjustmentJournalCode = <parameter value>
       vhParameter::icDescription = <parameter value>
       vhParameter::icAllocationJournalCode = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tExpNoteIds").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.