project QadFinancials > class BExpenseNote > method RefreshAdvances


Parameters


ilMergeinputlogicalWe have to consider 2 modes (logical input parameter ?ilMerge Yes/No):
? Retrieve lines from ExpNoteCInvoice only (e. g. the activity View).
? Retreive all open advances + retrieve lines from ExpNoteCInvoice and merge them together (e. g. Modify).
iiExpenseNote_IDinputinteger
icEmployeeCodeinputcharacter
tAdvancesoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BExpenseNote.AdditionalUpdates
method BExpenseNote.APIExpNoteRegister


program code (program3/bexpensenote.p)

empty temp-table tAdvances.

if icEmployeeCode = "":U or
   icEmployeeCode = ?    or
   icEmployeeCode = "?":U
then return.

if ilMerge
then do:
    /* get employee's creditor */
    <Q-1 run EmployeeLinkedToCreditorByUser (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input ?, (Usr_ID)
           input ?, (EmployeeIsExternal)
           input ?, (EmployeeIsActiveExpNote)
           input ?, (EmployeeIsActive)
           input ?, (EmployeeCode)
           output dataset tqEmployeeLinkedToCreditorByUser) in BEmployee >
    find first tqEmployeeLinkedToCreditorByUser where
               tqEmployeeLinkedToCreditorByUser.tcEmployeeCode = icEmployeeCode no-error.
    if not available tqEmployeeLinkedToCreditorByUser
    then do:
        assign vcMessage = subst(#T-15'Employee code '&1' is invalid or no supplier is linked to it.':200(1900)T-15#, icEmployeeCode).
        <M-2 run SetMessage
          (input  vcMessage (icMessage), 
           input  ? (icArguments), 
           input  ? (icFieldName), 
           input  ? (icFieldValue), 
           input  'E':U (icType), 
           input  3 (iiSeverity), 
           input  ? (icRowid), 
           input  'QADFIN-2425':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
        assign oiReturnStatus = -1.
        return.
    end.
    
    /* all prepayment invoices with open balance and type prepayment */
    <Q-3 run CInvoiceByCreditor (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input tqEmployeeLinkedToCreditorByUser.tiCreditor_ID, (Creditor_ID)
           input {&INVOICETYPE-PREPAYMENT}, (CInvoiceType)
           input true, (CInvoiceIsOpen)
           input ?, (CInvoice_ID)
           output dataset tqCInvoiceByCreditor) in BCInvoice >

    for each tqCInvoiceByCreditor:
        if tqCInvoiceByCreditor.tdCInvoiceBalanceDebitTC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditTC <= 0
        then next.
        if tqCInvoiceByCreditor.tlCInvoiceIsSelected
        then do:
            /* skip the invoices that are selected but not used in ExpNotes; 
             * it means that they are used in another module (f.e. PaymentSelection) already 
             */
            <Q-14 assign vlFcQueryRecordsAvailable = ExpNoteCInvoiceByCInvoice (NoCache)
          (input tqCInvoiceByCreditor.tiCInvoice_ID, (CInvoice_ID)
           input {&EXPENSENOTESTATUS-CONFIRMED}, (ExpNoteCInvoiceStatusCode)
           input viCompanyId, (CompanyId)) in BExpenseNote >
            if not vlFcQueryRecordsAvailable
            then next.
        end.
        find first tExpNoteCInvoice where tExpNoteCInvoice.ExpNote_ID = iiExpenseNote_ID and
                                          tExpNoteCInvoice.CInvoice_ID = tqCInvoiceByCreditor.tiCInvoice_ID 
                                          no-error.
        if not available tExpNoteCInvoice
        then do:
            find first tExpNote where tExpNote.ExpNote_ID = iiExpenseNote_ID no-error.
            if not available tExpNote
            then do:
                assign vcMessage = subst(#T-16'An invalid expense note ID (&1) was passed to the Refresh Advances method.':150(1901)T-16#, iiExpenseNote_ID).
                <M-5 run SetMessage
          (input  vcMessage (icMessage), 
           input  ? (icArguments), 
           input  ? (icFieldName), 
           input  ? (icFieldValue), 
           input  'S':U (icType), 
           input  3 (iiSeverity), 
           input  ? (icRowid), 
           input  'QADFIN-2426':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
                assign oiReturnStatus = -1.
                return.
            end.

            <M-4 run AddDetailLine (input  'ExpNoteCInvoice':U (icTable), 
                        input  tExpNote.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
            if viFcReturnSuper < 0
            then do:
                assign oiReturnStatus = viFcReturnSuper.
                return.
            end.
            assign tExpNoteCInvoice.CInvoice_ID = tqCInvoiceByCreditor.tiCInvoice_ID
                   tExpNoteCInvoice.ExpNoteCInvoiceIsReturned = false
                   tExpNoteCInvoice.ExpNoteCInvoiceDeductTC   = 0
                   tExpNoteCInvoice.ExpNoteCInvoiceDeductLC   = 0
                   tExpNoteCInvoice.ExpNoteCInvoiceToReturnTC = 0.
            create tAdvances.
            assign tAdvances.tiCInvoice_ID         = tqCInvoiceByCreditor.tiCInvoice_ID
                   tAdvances.tiExpNote_ID          = iiExpenseNote_ID
                   tAdvances.tcAdvanceCurrencyCode = tqCInvoiceByCreditor.tcCurrencyCode
                   tAdvances.tiCurrency_ID         = tqCInvoiceByCreditor.tiCInvoiceCurrency_ID
                   tAdvances.tcAdvanceDescription  = tqCInvoiceByCreditor.tcCInvoiceDescription
                   tAdvances.tcAdvanceJournal      = tqCInvoiceByCreditor.tcJournalCode
                   tAdvances.tcAdvanceReference    = tqCInvoiceByCreditor.tcCInvoiceReference
                   tAdvances.tdOnExpNoteTC         = 0 /* tExpNoteCInvoice.ExpNoteCInvoiceDeductTC */
                   tAdvances.tdOpenBalanceLC       = tqCInvoiceByCreditor.tdCInvoiceBalanceDebitLC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditLC
                   tAdvances.tdOpenBalanceTC       = tqCInvoiceByCreditor.tdCInvoiceBalanceDebitTC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditTC
                   tAdvances.tdToReturnTC          = tExpNoteCInvoice.ExpNoteCInvoiceToReturnTC
                   tAdvances.tdUnexplainedTC       = 0
                   tAdvances.tiAdvanceVoucher      = tqCInvoiceByCreditor.tiCInvoiceVoucher
                   tAdvances.tiAdvanceYear         = tqCInvoiceByCreditor.tiPeriodYear
                   tAdvances.tlAdvanceReturnRest   = tExpNoteCInvoice.ExpNoteCInvoiceIsReturned
                   tAdvances.ttAdvanceDate         = tqCInvoiceByCreditor.ttCInvoiceDate
                   tAdvances.tdAdvanceExchangeRateTCLC = tqCInvoiceByCreditor.tdCInvoiceExchangeRate
                   tAdvances.tdAdvanceExchangeRateScaleTCLC = tqCInvoiceByCreditor.tdCInvoiceRateScale.
        end. /* if not available tExpNoteCInvoice */
        else do:
            if (tqCInvoiceByCreditor.tdCInvoiceBalanceDebitTC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditTC) <
               (tExpNoteCInvoice.ExpNoteCInvoiceDeductTC + tExpNoteCInvoice.ExpNoteCInvoiceToReturnTC)
            then do:
                assign tExpNoteCInvoice.ExpNoteCInvoiceIsReturned = false
                       tExpNoteCInvoice.ExpNoteCInvoiceDeductTC   = 0
                       tExpNoteCInvoice.ExpNoteCInvoiceDeductLC   = 0
                       tExpNoteCInvoice.ExpNoteCInvoiceToReturnTC = 0
                       tExpNoteCInvoice.tc_Status = if tExpNoteCInvoice.tc_Status = "":U then "C":U else tExpNoteCInvoice.tc_Status.
            end.
        end. /* else - if not available tExpNoteCInvoice  */
    end. /* for each tqCInvoiceByCreditor  */
end. /* if ilMerge */

if iiExpenseNote_ID = ? or
   iiExpenseNote_ID = 0
then return.

assign vlQueryStarted = false.
for each tExpNoteCInvoice where tExpNoteCInvoice.ExpNote_ID = iiExpenseNote_ID:
    if tExpNoteCInvoice.tc_Status = "D":U
    then next.

    if not vlQueryStarted
    then do:
        <Q-9 run CInvoiceByCreditor (Start) in BCInvoice >
        assign vlQueryStarted = true.
    end.
    <Q-6 run CInvoiceByCreditor (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input ?, (Creditor_ID)
           input ?, (CInvoiceType)
           input ?, (CInvoiceIsOpen)
           input tExpNoteCInvoice.CInvoice_ID, (CInvoice_ID)
           output dataset tqCInvoiceByCreditor) in BCInvoice >
    find first tqCInvoiceByCreditor no-error.
    if not available tqCInvoiceByCreditor
    then do:
        assign vcMessage = subst(#T-17'The linked supplier invoice (&1) could not be found.':200(1902)T-17#, string(tExpNoteCInvoice.CInvoice_ID)).
        <M-10 run SetMessage
          (input  vcMessage (icMessage), 
           input  ? (icArguments), 
           input  ? (icFieldName), 
           input  ? (icFieldValue), 
           input  'D':U (icType), 
           input  3 (iiSeverity), 
           input  ? (icRowid), 
           input  'QADFIN-2453':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in BExpenseNote>
        assign oiReturnStatus = -1.
        return.
    end.
    if not tqCInvoiceByCreditor.tlCInvoiceIsOpen
    then do:
        /* delete the CInvoice link if the CInvoice is already closed */
        if tExpNoteCInvoice.tc_Status = "N":U
        then assign tExpNoteCInvoice.tc_Status = "":U.
        else assign tExpNoteCInvoice.tc_Status = "D":U.
    end.
    else do:
        if not can-find(first tAdvances where tAdvances.tiCInvoice_ID = tqCInvoiceByCreditor.tiCInvoice_ID)
        then do:
            create tAdvances.
            assign tAdvances.tiCInvoice_ID         = tqCInvoiceByCreditor.tiCInvoice_ID
                   tAdvances.tiExpNote_ID          = iiExpenseNote_ID   
                   tAdvances.tcAdvanceCurrencyCode = tqCInvoiceByCreditor.tcCurrencyCode
                   tAdvances.tiCurrency_ID         = tqCInvoiceByCreditor.tiCInvoiceCurrency_ID
                   tAdvances.tcAdvanceDescription  = tqCInvoiceByCreditor.tcCInvoiceDescription
                   tAdvances.tcAdvanceJournal      = tqCInvoiceByCreditor.tcJournalCode
                   tAdvances.tcAdvanceReference    = tqCInvoiceByCreditor.tcCInvoiceReference
                   tAdvances.tdOnExpNoteTC         = if ilMerge then 0 else tExpNoteCInvoice.ExpNoteCInvoiceDeductTC
                   tAdvances.tdOpenBalanceLC       = tqCInvoiceByCreditor.tdCInvoiceBalanceDebitLC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditLC
                   tAdvances.tdOpenBalanceTC       = tqCInvoiceByCreditor.tdCInvoiceBalanceDebitTC - tqCInvoiceByCreditor.tdCInvoiceBalanceCreditTC
                   tAdvances.tdToReturnTC          = tExpNoteCInvoice.ExpNoteCInvoiceToReturnTC
                   tAdvances.tdUnexplainedTC       = 0
                   tAdvances.tiAdvanceVoucher      = tqCInvoiceByCreditor.tiCInvoiceVoucher
                   tAdvances.tiAdvanceYear         = tqCInvoiceByCreditor.tiPeriodYear
                   tAdvances.tlAdvanceReturnRest   = tExpNoteCInvoice.ExpNoteCInvoiceIsReturned
                   tAdvances.ttAdvanceDate         = tqCInvoiceByCreditor.ttCInvoiceDate
                   tAdvances.tdAdvanceExchangeRateTCLC = tqCInvoiceByCreditor.tdCInvoiceExchangeRate
                   tAdvances.tdAdvanceExchangeRateScaleTCLC = tqCInvoiceByCreditor.tdCInvoiceRateScale.
        end.
    end.
end. /* for each tExpNoteCInvoice where tExpNoteCInvoice.ExpNote_ID = iiExpenseNote_ID */
if vlQueryStarted
then do:
    <Q-8 run CInvoiceByCreditor (Stop) in BCInvoice >
end.

/* adjust the open balance - subtract the already confirmed expense notes */
if can-find(first tAdvances)
then do:
    <Q-12 run ExpNoteCInvoiceByCInvoice (Start) in BExpenseNote >
    assign vlQueryStarted = true.
end.
else assign vlQueryStarted = false.
for each tAdvances:         
    <Q-11 run ExpNoteCInvoiceByCInvoice (all) (Read) (NoCache)
          (input tAdvances.tiCInvoice_ID, (CInvoice_ID)
           input {&EXPENSENOTESTATUS-CONFIRMED}, (ExpNoteCInvoiceStatusCode)
           input viCompanyId, (CompanyId)
           output dataset tqExpNoteCInvoiceByCInvoice) in BExpenseNote >

    for each tqExpNoteCInvoiceByCInvoice where
             tqExpNoteCInvoiceByCInvoice.tiExpNote_ID <> tAdvances.tiExpNote_ID:
        assign tAdvances.tdOpenBalanceTC = tAdvances.tdOpenBalanceTC - tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductTC - tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceToReturnTC.
        if tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductTC <> 0 and
           tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductTC <> ?
        then assign tAdvances.tdOpenBalanceLC = tAdvances.tdOpenBalanceLC - tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductLC - /*round(tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceToReturnTC * tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductLC / tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductTC, viCompanyLCDec).*/
                                                                                                                                                                                                            <M-18 RoundAmount
          (input  tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceToReturnTC * tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductLC / tqExpNoteCInvoiceByCInvoice.tdExpNoteCInvoiceDeductTC (idUnroundedAmount), 
           input  viCompanyLCid (iiCurrencyID), 
           input  vcCompanyLC (icCurrencyCode)) in business>.
    end.

    if (tAdvances.tdOpenBalanceTC < 0) or
       (tAdvances.tdOpenBalanceTC = 0 and
        ilMerge = true)
    then do:
        find first tExpNoteCInvoice where
                   tExpNoteCInvoice.CInvoice_ID = tAdvances.tiCInvoice_ID
                   no-error.
        if available tExpNoteCInvoice
        then assign tExpNoteCInvoice.tc_Status = (if tExpNoteCInvoice.tc_Status = "N":U or
                                                     tExpNoteCInvoice.tc_Status = "":U
                                                  then "":U
                                                  else "D":U).
        delete tAdvances.
        next.
    end.
end.

if vlQueryStarted
then do:
    <Q-13 run ExpNoteCInvoiceByCInvoice (Stop) in BExpenseNote >
end.