project QadFinancials > class BDebtor > method CalculateBOIforCompanies


Parameters


iiDebtorIdinputinteger
icDebtorCodeinputcharacter
icCurrencyCodeinputcharacter
odBalanceoutputdecimal
odSoBalanceoutputdecimal
odBdBalanceoutputdecimal
icCompanyListIDinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program6/bdebtor.p)

/* Error handling */
assign oiReturnStatus = -98
       viLocalReturn  = 0.


/* Normalize input parameters */
if iiDebtorId     = ? then assign iiDebtorId     = 0.
if icDebtorCode   = ? then assign icDebtorCode   = "":U.
if icCurrencyCode = ? then assign icCurrencyCode = "":U.

if iiDebtorId   = 0 and
   icDebtorCode = "":U
then do:
    assign oiReturnStatus = 0.
    return.
end.

if iiDebtorId     = 0 or
   icCurrencyCode = "":U
then do:
    <Q-12 run DebtorByDebtor (all) (Read) (NoCache)
       (input if iiDebtorId = 0 then viCompanyId else ?, (CompanyId)
        input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
        input if iiDebtorId = 0 then ? else iiDebtorId, (DebtorId)
        input if iiDebtorId = 0 then icDebtorCode else ?, (DebtorCode)
        output dataset tqDebtorByDebtor) in BDebtor >

    find first tqDebtorByDebtor where
               tqDebtorByDebtor.tcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE} and
               tqDebtorByDebtor.tiDebtor_ID       = (if iiDebtorId = 0
                                                     then tqDebtorByDebtor.tiDebtor_ID
                                                     else iiDebtorId)                   and
               tqDebtorByDebtor.tcDebtorCode      = (if iiDebtorId = 0
                                                     then icDebtorCode
                                                     else tqDebtorByDebtor.tcDebtorCode)
               no-error.

    if not available tqDebtorByDebtor
    then do:
        assign oiReturnStatus = 0.
        return.
    end.

    if iiDebtorId = 0
    then assign iiDebtorId = tqDebtorByDebtor.tiDebtor_ID.

    if icCurrencyCode = "":U
    then assign icCurrencyCode = tqDebtorByDebtor.tcCurrencyCode.
end.
    
<I-24 {bFcStartAndOpenInstance
     &ADD-TO-TRANSACTION   = "false"
     &CLASS                = "PMfgProSOApi"}>

/* For each on company list to get company ID */
MAIN_BLOCK:
do viCompanyCnt = num-entries(icCompanyListID, ',':U) to 1 by -1:

    empty temp-table tDebtorSalesOrderBalance.
    assign viTempCompanyID = integer(entry(viCompanyCnt, icCompanyListID)) no-error.
    

    /* Balance Open Item */
    <Q-13 run DInvoiceByDebtorIsOpen (all) (Read) (NoCache)
       (input viTempCompanyID, (CompanyId)
        input iiDebtorId, (DebtorId)
        input true, (DInvoiceIsOpen)
        input ?, (DInvoiceType)
        output dataset tqDInvoiceByDebtorIsOpen) in BDInvoice >
    
    for each tqDInvoiceByDebtorIsOpen where 
             tqDInvoiceByDebtorIsOpen.tiCompany_ID     = viTempCompanyID and
             tqDInvoiceByDebtorIsOpen.tiDebtor_ID      = iiDebtorId and
             tqDInvoiceByDebtorIsOpen.tlDInvoiceIsOpen = true
        break by tqDInvoiceByDebtorIsOpen.tiCompany_ID
              by tqDInvoiceByDebtorIsOpen.tcCurrencyCode:

        /* Calculate invoice balance */
        assign vdBalanceTC = tqDInvoiceByDebtorIsOpen.tdDInvoiceBalanceDebitTC - tqDInvoiceByDebtorIsOpen.tdDInvoiceBalanceCreditTC
               vdBalanceLC = tqDInvoiceByDebtorIsOpen.tdDInvoiceBalanceDebitLC - tqDInvoiceByDebtorIsOpen.tdDInvoiceBalanceCreditLC.

        /* Determine which amount is used eventually calculate exchange rate */
        if first-of(tqDInvoiceByDebtorIsOpen.tcCurrencyCode)
        then do:
            assign vlUseTCAmount = false
                   vlUseLCAmount = false
                   vdRate        = 1
                   vdRateScale   = 1.

            /* Required currency is the same as invoice currency */ 
            if tqDInvoiceByDebtorIsOpen.tcCurrencyCode = icCurrencyCode
            then assign vlUseTCAmount = true.
            else do:
                 <Q-38 assign vlFcQueryRecordsAvailable = CompanyPropertyByCompany (NoCache)
                    (input tqDInvoiceByDebtorIsOpen.tiCompany_ID, (CompanyId)
                     input ?, (CompanyCode)
                     input icCurrencyCode, (CurrencyCode)) in BCompanyProperty >
                /* Required currency is the same as local currency of the invoice */
                if vlFcQueryRecordsAvailable = true
                then assign vlUseLCAmount = true.
                else do:
                     /* We have to calculate exchange rate between required currency and invoice currency */
                    <M-39 run GetExchangeRate
                       (input  viCompanyID (iiCompanyID), 
                        input  ? (iiFromCurrencyID), 
                        input  tqDInvoiceByDebtorIsOpen.tcCurrencyCode (icFromCurrencyCode), 
                        input  ? (iiToCurrencyID), 
                        input  icCurrencyCode (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        input  today (itValidityDate), 
                        output vdRate (odExchangeRate), 
                        output vdRateScale (odExchangeScaleFactor), 
                        output viFcReturnSuper (oiReturnStatus)) in BDebtor>
                    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
                    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
                end. /* else if vlFcQueryRecordsAvailable = true */
            end. /* else if tqDInvoiceByDebtorIsOpen.tcCurrencyCode = icCurrencyCode */
        end. /* if first-of(tqDInvoiceByDebtorIsOpen.tcCurrencyCode) */

        if vlUseTCAmount
        then assign odBalance = odBalance + vdBalanceTC.
        else if vlUseLCAmount
        then assign odBalance = odBalance + vdBalanceLC.
        else assign odBalance = odBalance
                              + <M-40 RoundAmount
                                   (input  vdBalanceTC * vdRate * vdRateScale (idUnroundedAmount), 
                                    input  0 (iiCurrencyID), 
                                    input  icCurrencyCode (icCurrencyCode)) in business>.
    end. /* for each tqDInvoiceByDebtorIsOpen where */
    
    /* Bank Draft */
    <Q-21 run DDocumentByDebtorTypeOpen (all) (Read) (NoCache)
       (input viTempCompanyID, (CompanyId)
        input iiDebtorId, (DebtorId)
        input ?, (DebtorCode)
        input {&DOCUMENTTYPE-DRAFT}, (DocumentType)
        input true, (IsOpen)
        output dataset tqDDocumentByDebtorTypeOpen) in BDDocument >
    
    for each tqDDocumentByDebtorTypeOpen where
             tqDDocumentByDebtorTypeOpen.tiDebtor_ID        = iiDebtorId and
             tqDDocumentByDebtorTypeOpen.tcDDocumentType    = {&DOCUMENTTYPE-DRAFT} and
             tqDDocumentByDebtorTypeOpen.tlDDocumentIsOpen  = true                  and
              tqDDocumentByDebtorTypeOpen.tcDDocumentStatus <> {&DOCUMENTSTATUS-INIT}
        break by tqDDocumentByDebtorTypeOpen.tiCompany_ID
              by tqDDocumentByDebtorTypeOpen.tcCurrencyCode:

        assign vdBalanceTC = tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalDebitTC - tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalCreditTC
               vdBalanceLC = tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalDebitLC - tqDDocumentByDebtorTypeOpen.tdDDocumentOriginalCreditLC.
    
        /* Determine which amount is used eventually calculate exchange rate */
        if first-of(tqDDocumentByDebtorTypeOpen.tcCurrencyCode)
        then do:
            assign vlUseTCAmount = false
                   vlUseLCAmount = false
                   vdRate        = 1
                   vdRateScale   = 1.

            /* Required currency is the same as invoice currency */ 
            if tqDDocumentByDebtorTypeOpen.tcCurrencyCode = icCurrencyCode
            then assign vlUseTCAmount = true.
            else do:
                 <Q-41 assign vlFcQueryRecordsAvailable = CompanyPropertyByCompany (NoCache)
                    (input tqDDocumentByDebtorTypeOpen.tiCompany_ID, (CompanyId)
                     input ?, (CompanyCode)
                     input tqDDocumentByDebtorTypeOpen.tcCurrencyCode, (CurrencyCode)) in BCompanyProperty >
                /* Required currency is the same as local currency of the invoice */
                if vlFcQueryRecordsAvailable = true
                then assign vlUseLCAmount = true.
                else do:
                     /* We have to calculate exchange rate between required currency and invoice currency */
                    <M-42 run GetExchangeRate
                       (input  viCompanyId (iiCompanyID), 
                        input  ? (iiFromCurrencyID), 
                        input  tqDDocumentByDebtorTypeOpen.tcCurrencyCode (icFromCurrencyCode), 
                        input  ? (iiToCurrencyID), 
                        input  icCurrencyCode (icToCurrencyCode), 
                        input  ? (iiExchangeRateTypeID), 
                        input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                        input  today (itValidityDate), 
                        output vdRate (odExchangeRate), 
                        output vdRateScale (odExchangeScaleFactor), 
                        output viFcReturnSuper (oiReturnStatus)) in BDebtor>
                    if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0 then assign viLocalReturn = viFcReturnSuper.
                    if viFcReturnSuper < 0 then leave MAIN_BLOCK.
                end. /* if vlFcQueryRecordsAvailable = true */
            end. /* else if tqDDocumentByDebtorTypeOpen.tcCurrencyCode = icCurrencyCode */
        end. /* if first-of(tqDDocumentByDebtorTypeOpen.tcCurrencyCode) */
        
        if vlUseTCAmount
        then assign odBdBalance = odBdBalance + vdBalanceTC.
        else if vlUseLCAmount
        then assign odBdBalance = odBdBalance + vdBalanceLC.
        else assign odBdBalance = odBdBalance
                                + <M-43 RoundAmount
                                     (input  vdBalanceTC * vdRate * vdRateScale (idUnroundedAmount), 
                                      input  0 (iiCurrencyID), 
                                      input  icCurrencyCode (icCurrencyCode)) in business>.
    end. /* for each tqDDocumentByDebtorTypeOpen where */
    

    <Q-27 run CompanyPrim (all) (Read) (NoCache)
       (input viTempCompanyID, (LookupCompanyId)
        input '':U, (CompanyCode)
        output dataset tqCompanyPrim) in BCompany>

    find first tqCompanyPrim where tqCompanyPrim.tiCompany_ID = viTempCompanyID no-error. 
    /*Populate temp-table for Calculating Sales Order Total for Selected Companies*/    
    <Q-94 run OpenSalesOrderByCompany (all) (Read) (NoCache)
       (input tqCompanyPrim.tcCompanyCode, (CompanyCodeList)
        input icDebtorCode, (DebtorCode)
        output dataset tqOpenSalesOrderByCompany) in BMfgSalesOrderQuery>
    
    for each tqOpenSalesOrderByCompany: 
    
        create tDebtorSalesOrderBalance.
        assign tDebtorSalesOrderBalance.tcSalesOrderDomain =  tqOpenSalesOrderByCompany.tcso_domain
               tDebtorSalesOrderBalance.tcSalesOrderNumber =  tqOpenSalesOrderByCompany.tcso_nbr.
        
    end. /*for each tqSalesOrderByDomainCompany*/         
        
    /*Calculate Sales Order Total for Selected Companies. Sales orders are populated into temp-table*/   
    if can-find (first tDebtorSalesOrderBalance)
    then do :    
         assign vhInOut = dataset tDebtorSalesOrderBalance:handle.
     
         <M-81 run GetSalesOrderBalance
           (input-output vhInOut (bzSalesOrderBalance), 
            output viFcReturnSuper (oiReturnStatus)) in PMfgProSOApi>
     
         for each tDebtorSalesOrderBalance:  
            find first tqOpenSalesOrderByCompany where tqOpenSalesOrderByCompany.tcso_nbr = tDebtorSalesOrderBalance.tcSalesOrderNumber no-error.
            
            if available tqOpenSalesOrderByCompany and
               tqOpenSalesOrderByCompany.tcso_curr <> icCurrencyCode
            then do:
               <M-49 run GetExchangeRate
                  (input  viCompanyId (iiCompanyID), 
                   input  ? (iiFromCurrencyID), 
                   input  tqOpenSalesOrderByCompany.tcso_curr (icFromCurrencyCode), 
                   input  ? (iiToCurrencyID), 
                   input  icCurrencyCode (icToCurrencyCode), 
                   input  ? (iiExchangeRateTypeID), 
                   input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                   input  today (itValidityDate), 
                   output vdRate (odExchangeRate), 
                   output vdRateScale (odExchangeScaleFactor), 
                   output viFcReturnSuper (oiReturnStatus)) in BDebtor>
               if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturn = 0
               then assign viLocalReturn = viFcReturnSuper.
               if viFcReturnSuper < 0 
               then leave MAIN_BLOCK.
               
               assign odSoBalance = odSoBalance + <M-10 RoundAmount
                                                     (input  tDebtorSalesOrderBalance.tdSalesOrderOpenAmount * vdRate * vdRateScale (idUnroundedAmount), 
                                                      input  0 (iiCurrencyID), 
                                                      input  icCurrencyCode (icCurrencyCode)) in BDebtor>.
            end.
            else do:
               assign odSoBalance = odSoBalance + tDebtorSalesOrderBalance.tdSalesOrderOpenAmount.
            end.            
         end. /* for each tDebtorSalesOrderBalance */  
    end. /* can-find (first tDebtorSalesOrderBalance) */
end. /* end of MAIN_BLOCK */

<I-9 {bFcCloseAndStopInstance
     &CLASS           = "PMfgProSOApi"}> 

/* Error handling */
assign oiReturnStatus = viLocalReturn.

if oiReturnStatus = -98
then  assign oiReturnStatus = 0.