project QadFinancials > class BDebtorReport > method DebtorReminders

report procedure

Description

This method is used to create external debtor reminders


Parameters


icLanguageCodeinputcharacter
tFilterinputtemp-table
dcrDebtorRemindersoutputdataset
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bdebtorreport.p)

empty temp-table tqDebtorReminders.
empty temp-table tAvailableDebtors.
empty temp-table tDInvoiceIDList.
/* =-=-=-=-=-=-=-=- */
/* Translate Report */
/* =-=-=-=-=-=-=-=- */

<M-1 run GetReportLabels (input  'DebtorReminders':U (icReportName), 
                          input  icLanguageCode (icLanguageCode), 
                          input  tFilter (tFilter), 
                          output tqHeader (tqHeader), 
                          output tqFilter (tqFilter), 
                          output tqText (tqText), 
                          output oiReturnStatus (oiReturnStatus)) in BDebtorReport>
if oiReturnStatus <> 0
then return.

/*assign tFilter*/
<M-2 run SetDataItemsBasedOnFilterTT (output  viFcReturnSuper (oiReturnStatus)) in BDebtorReport>

/*get companyadrees information for the "header entity" filter*/
assign vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}.
<Q-47 run GetCompanyAddress (all) (Read) (NoCache)
   (input 0, (CompanyId)
    input vcAddressTypeCode, (AddressTypeCode)
    input true, (ContactIsPrimary)
    input vcEntityFilter, (CompanyCode)
    output dataset tqGetCompanyAddress) in BDebtorReport >
find first tqGetCompanyAddress where  
           tqGetCompanyAddress.tcAddressTypeCode = vcAddressTypeCode and
           tqGetCompanyAddress.tcCompanyCode     = vcEntityFilter 
           no-lock no-error.
if not available tqGetCompanyAddress or
    (available tqGetCompanyAddress and tqGetCompanyAddress.tcContactName = "":U)
then do:
    assign vcDErrorMessage =#T-49'You have to fill in the primary contact of your own entity before a customer statement of account can be printed':255(71097)T-49#.
end.

if vcDErrorMessage <> "":U
then do:
    <M-48 run SetMessage (input  vcDErrorMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QadFin-7139':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>              
    assign oiReturnStatus = -1.
    return.
end.

/* Validate Filterfields */
assign vcDErrorMessage = "":U.

if viDaysOfDelayFilter < 0 
then assign vcDErrorMessage = vcDErrorMessage + #T-19'Only positive amounts allowed for Days of Delay.':61(1751)T-19# + chr(10).
if viMinimumDelayFilter < 0 
then assign vcDErrorMessage = vcDErrorMessage + #T-20'Only positive amounts allowed for Min Days between Two Reminders.':92(1752)T-20# + chr(10).
if vdDMinimumAmountFilter < 0 
then assign vcDErrorMessage = vcDErrorMessage + #T-21'Only positive amounts allowed for Min Inv Amount.':70(1753)T-21# + chr(10).
if vcEntityFilter = "":U or vcEntityFilter = ?
then assign vcDErrorMessage = vcDErrorMessage + #T-46'You must specify the header entity parameter to run this report':255(65088)T-46#  + chr(10).

/* ===================== */
/* Assign default values */
/* ===================== */

if vtReminderDateFilter = ? 
then assign vtReminderDateFilter = today.
if viDaysOfDelayFilter = ? 
then assign viDaysOfDelayFilter = 0.
if viMinimumDelayFilter = ?
then assign  viMinimumDelayFilter = 0.
if vdDMinimumAmountFilter = ?
then assign vdDMinimumAmountFilter = 0.
if vlAllInvoicesFilter = ?
then assign vlAllInvoicesFilter = no.
if vcReminderLevelFilter = ?
then assign vcReminderLevelFilter = "1,2,3":U.
if vcDRCompanyFilter = ?
then assign vcDRCompanyFilter = "":U.
if vcEntityFilter = ?
then assign vcEntityFilter = vcCompanyCode.
if vlUpdateCountersFilter = ?
then assign vlUpdateCountersFilter = no. /* BTS 4209 */
if vlActiveDebtorsFilter = ?
then assign vlActiveDebtorsFilter = yes.

if vcDRCompanyFilter = ?
then assign vcDRCompanyFilter = "":U.

if vcDErrorMessage <> "":U
then do:
    <M-39 run SetMessage (input  vcDErrorMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QadFin-6018':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>              
    assign oiReturnStatus = -1.
    return.
end.

<Q-53 run GetStateInfoByAddressId  (Start) in BDebtorReport >

/* Cross-company Iteration */
do viDRCompanyIterator = 1 to viDRCompanyEntries:
    <M-33 run CrossCompanyValues
        (input  viDRCompanyIterator (iiEntryNumber), 
         output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
    
   /* dealing with error */
   if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
   then assign oiReturnStatus = viFcReturnSuper.
   if oiReturnStatus < 0 then return.

/* =================================================================== */
/* First Phase                                                         */
/* Choose active debtors, with active reminders which have balance > 0 */
/* =================================================================== */
<Q-4 run DebtorBalance (all) (Read) (NoCache)
   (input viDRCurrentCompany_ID, (CompanyId)
    input ?, (DebtorId)
    input viCurrencyIdFilter, (CurrencyId)
    input vcFromDebtorCodeFilter, (FromDebtor)
    input vcToDebtorCodeFilter, (ToDebtor)
    input vcCandoDebtorCodeFilter, (CanDoDebtor)
    input vlActiveDebtorsFilter, (ActiveDebtors)
    input true, (WithReminders)
    output dataset tqDebtorBalance) in BDebtorReport>

/*Calculates sums of CC,LC and TC
It is obvious that sum of TC doesn't make sens if no currency is specified
that is when vcCurrencyCodeFilter is blank and viCurrencyIdFilter is ? 
but for TC it should return correct sum*/
    assign vdSumDebtorBalance = 0.
    for each tqDebtorBalance break by tqDebtorBalance.tiDebtor_ID:
        /*eliminate ? values*/  
            
        assign vdSumDebtorBalance = vdSumDebtorBalance + if tqDebtorBalance.tdDBalanceLC = ? then 0 else tqDebtorBalance.tdDBalanceLC.
        if last-of(tqDebtorBalance.tiDebtor_ID)
        then do:
            if vdSumDebtorBalance > 0 
            then do:
                create tAvailableDebtors.
                assign tAvailableDebtors.tiDebtor_ID = tqDebtorBalance.tiDebtor_ID.
            end.
            assign vdSumDebtorBalance = 0.               
        end.
    end.

/* ======================================== */
/* Phase two                                */
/* Use open items querry and advance filter */
/* ======================================== */
    assign vcDinvoiceOpInfoType = {&DINVOICEOPINFOTYPE-SHIPPER}.
    /*call the base query*/
    <Q-5 run BaseDebtorOpenItems (all) (Read) (NoCache)
       (input viDRCurrentCompany_ID, (CompanyId)
        input vcFromDebtorCodeFilter, (FromDebtorCode)
        input vcToDebtorCodeFilter, (ToDebtorCode)
        input vcCandoDebtorCodeFilter, (CandoDebtorCode)
        input vcFromBRCodeFilter, (FromBRCode)
        input vcToBRCodeFilter, (ToBRCode)
        input vcCandoBRCodeFilter, (CandoBRCode)
        input ?, (FromJournalCode)
        input ?, (ToJournalCode)
        input ?, (CandoJournalCode)
        input vcCurrencyCodeFilter, (CurrencyCode)
        input vcInvoiceReasonCodeFilter, (ReasonCode)
        input vlActiveDebtorsFilter, (ActiveDebtors)
        input true, (WithReminders)
        output dataset tqBaseDebtorOpenItems) in BDebtorReport>
        
    for each tqBaseDebtorOpenItems 
        break by tqBaseDebtorOpenItems.tiDebtor_ID 
        by tqBaseDebtorOpenItems.tiDInvoiceReminderCount desc 
        by tqBaseDebtorOpenItems.tlReasonIsNoReminderCount:
        /* We want all debtors which have reminder level with max specified in ReminderLevel Filter */
        
        if first-of(tqBaseDebtorOpenItems.tiDebtor_ID)
        then do:
            assign vdSumDebtorBalance = 0.        
            if (can-do(vcReminderLevelFilter,string(tqBaseDebtorOpenItems.tiDInvoiceReminderCount + 1)) and tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = false) or
               (can-do(vcReminderLevelFilter,string(tqBaseDebtorOpenItems.tiDInvoiceReminderCount))     and tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = true)      or
               ((can-do(vcReminderLevelFilter,"3") and tqBaseDebtorOpenItems.tiDInvoiceReminderCount = 3)     and tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = false)
            then assign vlThrowOutDebtor = false.
            else assign vlThrowOutDebtor = true.
        end.

        if vlThrowOutDebtor 
        then next.


        /* If this debtor has balance < 0 we'll skip him */
        /*  Fin-2796 If the customer balance in the entity is negative, do not skip the record */
       /* if not can-find(first tAvailableDebtors where 
                              tAvailableDebtors.tiDebtor_ID = tqBaseDebtorOpenItems.tiDebtor_ID)
        then next. */
        

        /* Last reminder date + Min. Number of Days between 2 reminders <= Reminder Date */
        if tqBaseDebtorOpenItems.ttDinvoiceLastReminderDate + viMinimumDelayFilter > vtReminderDateFilter
        then next.

        /* first get dinvoice deduction status */
        if tqBaseDebtorOpenItems.tcDInvoiceType = {&INVOICETYPE-DEDUCTION}
        then do:
           <M-56 run CalculateDIDeductionStatus
              (input  tqBaseDebtorOpenItems.tiDInvoice_ID (iiDInvoiceId), 
               output tqBaseDebtorOpenItems.tcDeductionStatus (ocDIDeductionStatus), 
               output viFcReturnSuper (oiReturnStatus)) in BDebtorReport>
           if viFcReturnSuper <> 0 
           then assign oiReturnStatus = viFcReturnSuper.
           if viFcReturnSuper < 0 
           then return.
        end.

        /* Due date + Days of Delay <= Reminder Date 
           if we do not want to print all invoices we'll skip this info */
        if tqBaseDebtorOpenItems.tiDInvoiceStage_ID <> 0 and
           tqBaseDebtorOpenItems.ttDInvoiceStageDueDate + viDaysOfDelayFilter <= vtReminderDateFilter and
           tqBaseDebtorOpenItems.tdDInvoiceStageAmountTC <> tqBaseDebtorOpenItems.tdDInvoiceStageAmtAppliedTC
        then do:
            /* Now we summarize Open Balances as in final condition this balance needs to be > 0 or >= minimum invoice amount */
            assign vdSumDebtorBalance = vdSumDebtorBalance + tqBaseDebtorOpenItems.tdDInvoiceStageAmountTC - tqBaseDebtorOpenItems.tdDInvoiceStageAmtAppliedTC.
            if not can-find(first tDInvoiceIDList where tDInvoiceIDList.tiDInvoiceID = tqBaseDebtorOpenItems.tiDInvoice_ID) and tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = false and tqBaseDebtorOpenItems.tiDInvoiceReminderCount <> 3
               and (tqBaseDebtorOpenItems.tdDInvoiceBalanceDebitLC > 0 or tqBaseDebtorOpenItems.tdDInvoiceBalanceCreditLC < 0)
               and (tqBaseDebtorOpenItems.tcDInvoiceType <> {&INVOICETYPE-DEDUCTION} or 
                    (tqBaseDebtorOpenItems.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} and tqBaseDebtorOpenItems.tcDeductionStatus = {&INVOICEDEDUCTSTATUS-REJECTED}) )
            then do:
                create tDInvoiceIDList.
                assign tDInvoiceIDList.tiDInvoiceID = tqBaseDebtorOpenItems.tiDInvoice_ID.
            end.
        end.
        else if tqBaseDebtorOpenItems.tiDInvoiceStage_ID = 0 and 
                tqBaseDebtorOpenItems.ttDInvoiceDueDate + viDaysOfDelayFilter <= vtReminderDateFilter
        then do:
            /* Now we summarize Open Balances as in final condition this balance needs to be > 0 or >= minimum invoice amount */
            assign vdSumDebtorBalance = vdSumDebtorBalance + 
                (tqBaseDebtorOpenItems.tdDInvoiceBalanceDebitTC - tqBaseDebtorOpenItems.tdDInvoiceBalanceCreditTC).
            if not can-find(first tDInvoiceIDList where tDInvoiceIDList.tiDInvoiceID = tqBaseDebtorOpenItems.tiDInvoice_ID) and tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = false and tqBaseDebtorOpenItems.tiDInvoiceReminderCount <> 3
               and (tqBaseDebtorOpenItems.tdDInvoiceBalanceDebitLC > 0 or tqBaseDebtorOpenItems.tdDInvoiceBalanceCreditLC < 0)
               and (tqBaseDebtorOpenItems.tcDInvoiceType <> {&INVOICETYPE-DEDUCTION} or 
                    (tqBaseDebtorOpenItems.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} and tqBaseDebtorOpenItems.tcDeductionStatus = {&INVOICEDEDUCTSTATUS-REJECTED}) )
            then do:
                create tDInvoiceIDList.
                assign tDInvoiceIDList.tiDInvoiceID = tqBaseDebtorOpenItems.tiDInvoice_ID.
            end.
        end.
        else if not vlAllInvoicesFilter 
        then next.
          

        /*CA713061*/
         /* Copy all the data into result set */
         
         find first tqDebtorReminders where tqDebtorReminders.tiDInvoice_ID = tqBaseDebtorOpenItems.tiDInvoice_ID and 
                    if tqBaseDebtorOpenItems.tiDInvoiceStage_ID <> 0 
                    then tqDebtorReminders.tiDInvoiceStage_ID = tqBaseDebtorOpenItems.tiDInvoiceStage_ID
                    else true 
                    no-error.
                    
         if not available tqDebtorReminders
         then do:
             create tqDebtorReminders.
             buffer-copy tqBaseDebtorOpenItems to tqDebtorReminders.
             assign tqDebtorReminders.tlIsOwnAddressLine = false.
                
             if tqBaseDebtorOpenItems.tiDInvoiceStage_ID <> 0 
             then assign tqDebtorReminders.tdDInvoiceBalanceDebitTC  = tqBaseDebtorOpenItems.tdDInvoiceStageAmountTC - tqBaseDebtorOpenItems.tdDInvoiceStageAmtAppliedTC
                         tqDebtorReminders.tdDInvoiceOriginalDebitTC = tqBaseDebtorOpenItems.tdDInvoiceStageAmountTC
                         tqDebtorReminders.tdDInvoiceOriginalDebitLC = tqBaseDebtorOpenItems.tdDInvoiceStageAmountLC
                         tqDebtorReminders.tdDInvoiceOriginalDebitCC = tqBaseDebtorOpenItems.tdDInvoiceStageAmountCC
                         tqDebtorReminders.ttDInvoiceDueDate         = tqBaseDebtorOpenItems.ttDInvoiceStageDueDate
                         tqDebtorReminders.ttDInvoiceDiscountDueDate = tqBaseDebtorOpenItems.ttDInvoiceStageDiscDate
                         tqDebtorReminders.tcDInvoiceDIText          = tqBaseDebtorOpenItems.tcDInvoiceDIText           
                         tqDebtorReminders.tcDInvoiceTSMNumber       = tqBaseDebtorOpenItems.tcDInvoiceTSMNumber        
                         tqDebtorReminders.tcDInvoiceDescription     = tqBaseDebtorOpenItems.tcDInvoiceDescription.
                         tqDebtorReminders.tcDInvoiceDeductionStatus = tqBaseDebtorOpenItems.tcDeductionStatus.
            
                
             if tqDebtorReminders.ttDInvoiceDueDate + viDaysOfDelayFilter <= vtReminderDateFilter
             then assign tqDebtorReminders.tlOverDue = true.
             else assign tqDebtorReminders.tlOverDue = false.

                
             if tqBaseDebtorOpenItems.tlReasonIsNoReminderCount = false and 
                tqBaseDebtorOpenItems.tiDInvoiceReminderCount <> 3      and
                tqDebtorReminders.tlOverDue = true
             then assign tqDebtorReminders.tiDInvoiceReminderCount = tqBaseDebtorOpenItems.tiDInvoiceReminderCount + 1.

        end. /* if not available tqDebtorReminders */
        
            /* If balance < minimum invoice amount we'll delete all (debtor, currency) data from result set */
        define buffer DebtorRemindersBuffer1 for tqDebtorReminders.
        define buffer DebtorRemindersBuffer2 for tqDebtorReminders.
        if last-of (tqBaseDebtorOpenItems.tiDebtor_ID) then 
        do:
            for each DebtorRemindersBuffer1 where DebtorRemindersBuffer1.tiDebtor_ID = tqDebtorReminders.tiDebtor_ID
                                              break by DebtorRemindersBuffer1.tcCurrencyCode:

                vdSumDebtorBalance = vdSumDebtorBalance + 
                                 (DebtorRemindersBuffer1.tdDInvoiceBalanceDebitTC - DebtorRemindersBuffer1.tdDInvoiceBalanceCreditTC).
          
                if last-of(DebtorRemindersBuffer1.tcCurrencyCode) then
                do:
                    if vdSumDebtorBalance > vdDMinimumAmountFilter then
                    vlExistMaxSum = true.
                    vdSumDebtorBalance = 0.
                end.
            end. /* by tcCurrencyCode*/

            if vlExistMaxSum = false then
            do:
                for each DebtorRemindersBuffer2 where DebtorRemindersBuffer2.tiDebtor_ID = tqDebtorReminders.tiDebtor_ID:
                    find first tDInvoiceIDList where tDInvoiceIDList.tiDInvoiceID = DebtorRemindersBuffer2.tiDInvoice_ID no-error.
                    if available tDInvoiceIDList
                    then do:
                        delete tDInvoiceIDList.
                    end.
                    delete DebtorRemindersBuffer2.
                end.
            end.
            vdSumDebtorBalance = 0.
            vlExistMaxSum         = false.
        end.
    end.

/* ============================================================== */
/* Validate the sum of reminder amount exclude the deduction part */
/* if the result is less than 0 then no date will be returned.    */
/* ============================================================== */
assign vdSumDebtorBalance = 0.
for each tqDebtorReminders where tqDebtorReminders.tcDInvoiceType <> {&INVOICETYPE-DEDUCTION} or 
         ( tqDebtorReminders.tcDInvoiceType = {&INVOICETYPE-DEDUCTION} and
           tqDebtorReminders.tcDInvoiceDeductionStatus <> {&INVOICEDEDUCTSTATUS-REJECTED}) :
    assign vdSumDebtorBalance = vdSumDebtorBalance + 
                                tqDebtorReminders.tdDInvoiceBalanceDebitTC - tqDebtorReminders.tdDInvoiceBalanceCreditTC.
   
end.

if vdSumDebtorBalance <= 0
then do:
   empty temp-table tqDebtorReminders. 
   return.
end.
/* ======================= */
/* Phase 3                 */
/* Add Address Information */
/* ======================= */
    for each tqDebtorReminders break by tqDebtorReminders.tcDebtorCode:
        /*Calculates Address information for the BusinessRelation_ID*/ 
        assign vcPhone       = ?
               vcFax         = ?
               vcEmail       = ?
               vhFcComponent = ?
               vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-REMINDER}.
        if first-of (tqDebtorReminders.tcDebtorCode)
        then do:
            <M-6 run GetAddressWithType
               (input  tqDebtorReminders.tiBusinessRelation_ID (iiBusinessRelationId), 
                input  tqDebtorReminders.tcBusinessRelationCode (icBusinessRelationCode), 
                input  ? (iiAddressTypeId), 
                input  vcAddressTypeCode (icAddressTypeCode), 
                output tAddressQueryRef (tAddressQuery), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>    
        end.       
        find first tAddressQueryRef no-error.
        if available tAddressQueryRef
        then buffer-copy tAddressQueryRef to tqDebtorReminders.

        /* start --- defect 10132-3513 zhc */
        <Q-55 run GetStateInfoByAddressId (all) (Read) (NoCache)
           (input tAddressQueryRef.tiAddress_ID, (AddressId)
            output dataset tqGetStateInfoByAddressId) in BDebtorReport >
        find first tqGetStateInfoByAddressId no-error.
        if available tqGetStateInfoByAddressId then
        assign tqDebtorReminders.tcAddressState = tqGetStateInfoByAddressId.tcStateDescription.
        /* end --- defect 10132-3513 zhc */

        
        /* defect 10299-0202 ddl */
        if tqDebtorReminders.tiDInvoiceReminderCount > viMaxReminderLevel
        then assign viMaxReminderLevel = tqDebtorReminders.tiDInvoiceReminderCount.
    end.

    /* Now get own address in case a result set hold some data */
    find first tqDebtorReminders where tqDebtorReminders.tlIsOwnAddressLine = true no-error.
    if not available tqDebtorReminders
    then do:
        if can-find(first tqDebtorReminders) 
        then do:
            /* assign vhFcComponent = ?.
                   vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}. */
            <Q-34 run CompanyPrim (all) (Read) (NoCache)
               (input ?, (LookupCompanyId)
                input vcEntityFilter, (CompanyCode)
                output dataset tqCompanyPrim) in BCompany >
            find first tqCompanyPrim no-error.
            if available tqCompanyPrim
            then assign viEntityFilter = tqCompanyPrim.tiCompany_ID.
            <Q-9 run CompanyPropertyForAllData (all) (Read) (NoCache)
               (input viEntityFilter, (CompanyId)
                output dataset tqCompanyPropertyForAllData) in BCompanyProperty >
    
            find first tqCompanyPropertyForAllData no-error.
            if available tqCompanyPropertyForAllData then do:
                /* if Phone, Fax and Email are all filled in, then get field from INVOICE */
                assign vhFcComponent = ?
                       vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}.
                <M-8 run GetAddressWithType
                   (input  tqCompanyPropertyForAllData.tiBusinessRelation_ID (iiBusinessRelationId), 
                    input  ? (icBusinessRelationCode), 
                    input  ? (iiAddressTypeId), 
                    input  vcAddressTypeCode (icAddressTypeCode), 
                    output tAddressQueryRef (tAddressQuery), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                                 
                /* dealing with error */
                if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus >= 0)
                then assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0 then return.
            
                find first tAddressQueryRef no-error.
                if available tAddressQueryRef
                then do:
                    /* store phone, fax and email info in temp variables */
                    assign vcPhone = tAddressQueryRef.tcAddressTelephone
                           vcFax   = tAddressQueryRef.tcAddressFax
                           vcEmail = tAddressQueryRef.tcAddressEMail.
                    create tqDebtorReminders.
                    buffer-copy tAddressQueryRef to tqDebtorReminders. 
                    assign tqDebtorReminders.tlIsOwnAddressLine      = true.
                end.
                else do:
                    assign viEmpty = 1.
                end.
                if vcPhone = ? or vcFax = ? or vcEmail = ? or viEmpty = 1
                then do:
                    assign vhFcComponent = ?
                           vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}.
                    <M-22 run GetAddressWithType
                       (input  tqCompanyPropertyForAllData.tiBusinessRelation_ID (iiBusinessRelationId), 
                        input  ? (icBusinessRelationCode), 
                        input  ? (iiAddressTypeId), 
                        input  vcAddressTypeCode (icAddressTypeCode), 
                        output tAddressQueryRef (tAddressQuery), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            
                    find first tAddressQueryRef no-error.
                    if  available tAddressQueryRef
                    then do:
                        if viEmpty = 1
                        then do:
                            create tqDebtorReminders.
                            buffer-copy tAddressQueryRef to tqDebtorReminders.
                            assign tqDebtorReminders.tlIsOwnAddressLine   = true.
                        end.
                        else do:
                            find last tqDebtorReminders no-error.
                            if available tqDebtorReminders
                            then do:
                                if vcPhone = ?
                                then assign tqDebtorReminders.tcAddressTelephone = tAddressQueryRef.tcAddressTelephone.
                                if vcFax   = ?
                                then assign tqDebtorReminders.tcAddressFax = tAddressQueryRef.tcAddressFax.
                                if vcEmail = ?
                                then assign tqDebtorReminders.tcAddressEMail = tAddressQueryRef.tcAddressEMail.
                            end.
                        end.
                    end. /* available tAddressQueryRef*/
                end. /* if emtpy phone fax ... */

                /* start --- defect 10132-3513 zhc */
                <Q-52 run GetStateInfoByAddressId (all) (Read) (NoCache)
                   (input tAddressQueryRef.tiAddress_ID, (AddressId)
                    output dataset tqGetStateInfoByAddressId) in BDebtorReport >
                find first tqGetStateInfoByAddressId no-error.
                if available tqGetStateInfoByAddressId then
                assign tqDebtorReminders.tcAddressState = tqGetStateInfoByAddressId.tcStateDescription.
                /* end --- defect 10132-3513 zhc */

            end. /* available tqcompdata */
        
            find first tqGetCompanyAddress no-lock no-error.            
            if available tqGetCompanyAddress
            then do:
                if available tqDebtorReminders
                then assign tqDebtorReminders.tcContactName      = tqGetCompanyAddress.tcContactName
                            tqDebtorReminders.tcContactFunction  = tqGetCompanyAddress.tcContactFunction
                            tqDebtorReminders.tcContactTelephone = tqGetCompanyAddress.tcContactTelephone
                            tqDebtorReminders.tcContactEmail     = tqGetCompanyAddress.tcContactEmail.          
            end.
        end. /* if can-find(first tqDebtorReminders) */
    end. /* if not available tqDebtorReminders */
end.


define buffer bufDebtorReminders for tqDebtorReminders.
define buffer bufDebtorReminders1 for tqDebtorReminders.
define variable balanceByCurrency as decimal init 0.
define variable printDebt as logical init false.
for each tqDebtorReminders break by tqDebtorReminders.tiDebtor_ID:

   if last-of(tqDebtorReminders.tiDebtor_ID) then
   do:
      for each bufDebtorReminders where bufDebtorReminders.tiDebtor_ID = tqDebtorReminders.tiDebtor_ID
          break by bufDebtorReminders.tcCurrencyCode:

          balanceByCurrency = balanceByCurrency + 
                    (bufDebtorReminders.tdDInvoiceBalanceDebitTC - bufDebtorReminders.tdDInvoiceBalanceCreditTC).
          vdSumDebtorBalance = vdSumDebtorBalance + tqDebtorReminders.tdDInvoiceBalanceDebitTC.
          
          if last-of(bufDebtorReminders.tcCurrencyCode) then
          do:
             if balanceByCurrency > vdDMinimumAmountFilter then
             printDebt = true.
             balanceByCurrency = 0.
          end.
      end. /* by tcCurrencyCode*/

      if printDebt = false then
      do:
         for each bufDebtorReminders1 where bufDebtorReminders1.tiDebtor_ID = tqDebtorReminders.tiDebtor_ID
                and tqDebtorReminders.tlIsOwnAddressLine = false:

            find first tDInvoiceIDList where tDInvoiceIDList.tiDInvoiceID = bufDebtorReminders1.tiDInvoice_ID no-error.
            if available tDInvoiceIDList
            then do:
                delete tDInvoiceIDList.
            end.

            delete bufDebtorReminders1.
         end.
      end.
        balanceByCurrency = 0.
        printDebt         = false.
   end. /*last-of Debtor_ID*/

   assign vcDinvoiceOpInfoType = {&DINVOICEOPINFOTYPE-SHIPPER} + "," + {&DINVOICEOPINFOTYPE-GOLDENTAX}
          vcDInvoiceOpInfoCode = "":U.

    /* Add Shipper_ID */
   
   
   <Q-69 run DInvoiceOpInfoForLegalDocGT (all) (Read) (NoCache)
      (input tqDebtorReminders.tiDInvoice_ID, (DInvoiceID)
       input vcDInvoiceOpInfoType, (DInvoiceOpInfoType)
       output dataset tqDInvoiceOpInfoForLegalDocGT) in BDebtorReport>
   for each tqDInvoiceOpInfoForLegalDocGT break by tqDInvoiceOpInfoForLegalDocGT.tiDInvoice_ID:
       if first-of(tqDInvoiceOpInfoForLegalDocGT.tiDInvoice_ID) then
           vcDInvoiceOpInfoCode = tqDInvoiceOpInfoForLegalDocGT.tcDInvoiceOpInfoCode.
       else
           vcDInvoiceOpInfoCode = vcDInvoiceOpInfoCode + "," + tqDInvoiceOpInfoForLegalDocGT.tcDInvoiceOpInfoCode.
   end. 

      
   assign tqDebtorReminders.tcDInvoiceOpInfoCode = vcDInvoiceOpInfoCode.
end.


define variable debtorRecordCount as integer init 0.
for each tqDebtorReminders where tqDebtorReminders.tlIsOwnAddressLine = false:
   debtorRecordCount = debtorRecordCount + 1.
end.

if debtorRecordCount = 0 then
do:
   for each tqDebtorReminders where tqDebtorReminders.tlIsOwnAddressLine = true:
      delete tqDebtorReminders.
   end.
end.


/* pass max ReminderCOunt to UI - ddl */
create tqFilter.
assign 
    tqFilter.ti_Sequence = 0
    tqFilter.tcBusinessFieldLabel = "icActivityCode":U
    tqFilter.tcBusinessFieldName = "ReminderLevel":U
    tqFilter.tcParameterValue = string(viMaxReminderLevel).

<Q-54 run GetStateInfoByAddressId  (Stop) in BDebtorReport >
    
/* ========================== */
/* Phase 4                    */
/* Update reminder count      */
/* ========================== */

if vlUpdateCountersFilter
then do:
    if viBDInvoiceFromBDebtorRepID = 0 or
       viBDInvoiceFromBDebtorRepID = ?     
    then do:
        <I-10 {bFcStartAndOpenInstance
             &ADD-TO-TRANSACTION   = "false"
             &CLASS                = "BDInvoice"}>
    end.
    else do:
        <I-11 {bFcOpenInstance
            &CLASS           = "BDInvoice"}>
    end.

    <M-41 run UpdateDInvoiceReminderCountV01
       (input  vtReminderDateFilter (itReminderDate), 
        input  0 (iiReminderCount), 
        input  yes (ilIncrease), 
        input  tDInvoiceIDList (tDInvoiceListBDInv), 
        output viExternalReturnStatus (oiReturnStatus)) in BDInvoice>            
    <I-18 {bFcCloseAndStopInstance
            &CLASS           = "BDInvoice"}>         
    if viExternalReturnStatus <> 0 
    then assign oiReturnStatus = viExternalReturnStatus.
    if viExternalReturnStatus < 0
    then Return.
end.