project QadFinancials > class BPaymentSelection > method GetInvWithCommonPayAttributes

Description

This method is responsible for grouping CInvoice?s with same PayFormatTypeCode and "Payment Level" Attribute values. This will enable the creation of single CDocument record for CInvoice?s with same PayFormatTypeCode and "Payment Level" Attribute values


Parameters


icPaySelRowIDinputcharacter
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BPaymentSelection.AdditionalUpdatesRegister


program code (program1/bpaymentselection.p)

assign oiReturnStatus = -98.

empty temp-table tMatchingInvoiceGroup.
empty temp-table tMatchingInvoices.
empty temp-table tSelectedCInvoiceFromPay.
empty temp-table tCInvoicePaymentAttributes.

for each tPaySelLine where tPaySelLine.tc_ParentRowid = icPaySelRowID:
    if (tPaySelLine.PaySelLineObjectType <> {&PAYMENTSELECTIONTYPE-DEBTORCREDITNOTE} and
        tPaySelLine.PaySelLineObjectType <> {&PAYMENTSELECTIONTYPE-DEBTORCNCORR}) 
    then do:
        /* Skip PaySelLine which is not belong to current invoice group. For the Batch Solution of Supplier Payment Selection Confirm */
        <M-82 run IsValidPaySelIDInGroup
           (input  tPaySelLine.PaySelLine_ID (iiCurrentPaySelLineID), 
            output vlIsInCurrentInvoiceBatch (olIsValidPaySelLine), 
            input  no (ilChangeStatus), 
            output oiReturnStatus (oiReturnStatus)) in BPaymentSelection>             
       
        if vlIsInCurrentInvoiceBatch = no then
        do:
            next.
        end.

        if tPaySelLine.PaySelLineObjectType <> {&PAYMENTSELECTIONTYPE-PREPAYMENT}
        then do:
            <Q-60 run CInvoiceBankByPayFormatType (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input tPaySelLine.PayFormatTypeCode, (PayFormatTypeCode)
                input tPaySelLine.PaySelLineParentObject_ID, (InvoiceID)
                input tPaySelLine.BankNumber_ID, (BankNumberID)
                output dataset tqCInvoiceBankByPayFormatType) in BCInvoice>
            for each tqCInvoiceBankByPayFormatType no-lock
            where tqCInvoiceBankByPayFormatType.tiCInvoice_ID = tPaySelLine.PaySelLineParentObject_ID
              and tqCInvoiceBankByPayFormatType.tcPayFormatTypeCode = tPaySelLine.PayFormatTypeCode 
              and tqCInvoiceBankByPayFormatType.tiBankNumber_ID = tPaySelLine.BankNumber_ID:
                                                   
                create tCInvoicePaymentAttributes.
                assign tCInvoicePaymentAttributes.tcCInvoiceBankPayCodeValue = tqCInvoiceBankByPayFormatType.tcCInvoiceBankPayCodeValue
                       tCInvoicePaymentAttributes.tcPayFormatTypeCode = tqCInvoiceBankByPayFormatType.tcPayFormatTypeCode
                       tCInvoicePaymentAttributes.tiCInvoiceID = tqCInvoiceBankByPayFormatType.tiCInvoice_ID
                       tCInvoicePaymentAttributes.tiPayFormatCode_ID = tqCInvoiceBankByPayFormatType.tiPayFormatCode_ID
                       tCInvoicePaymentAttributes.tiCInvoiceBankID = tqCInvoiceBankByPayFormatType.tiCInvoiceBank_ID              
                       tCInvoicePaymentAttributes.tiPaymentSelectionLineID = tPaySelLine.PaySelLine_ID
                       tCInvoicePaymentAttributes.tiBankNumberID = tPaySelLine.BankNumber_ID
                       tCInvoicePaymentAttributes.tcPayFormatGroupLevel = tqCInvoiceBankByPayFormatType.tcPayFormatGroupLevel   
                       tCInvoicePaymentAttributes.tcCinvoiceType = tqCInvoiceBankByPayFormatType.tcCInvoiceType
                       tCInvoicePaymentAttributes.tcInvCurrencyCode = tPaySelLine.tcInvoiceCurrencyCode
                       tCInvoicePaymentAttributes.tcInvCreditorCode = tPaySelLine.tcCreditorCode
                       tCInvoicePaymentAttributes.tlInvoiceGrouping = true.                                                                                            

                /* Check to see if this is a grouping attribute. */
                if tqCInvoiceBankByPayFormatType.tcPayFormatGroupCode = 'GROUPING':U
                then do:
                    assign tCInvoicePaymentAttributes.tlAttributeIsGrouping = true.
                
                    if tCInvoicePaymentAttributes.tcCInvoiceBankPayCodeValue = 'False':U 
                    then assign tCInvoicePaymentAttributes.tlInvoiceGrouping = false.
                end.                                                                                       
            end. /* End for each tqCInvoiceBankByPayFormatType. */ 
        end. /* End for if tPaySelLine.PaySelLineObjectType <> {&PAYMENTSELECTIONTYPE-PREPAYMENT} */
        else do:
            <Q-20 run BankNumberPayCodeForCIDef (all) (Read) (NoCache)
               (input tPaySelLine.BankNumber_ID, (BankNumberId)
                output dataset tqBankNumberPayCodeForCIDef) in BBankNumber>
            
            for each tqBankNumberPayCodeForCIDef no-lock
            where tqBankNumberPayCodeForCIDef.tcPayFormatTypeCode = tPaySelLine.PayFormatTypeCode and
                  tqBankNumberPayCodeForCIDef.tiBankNumber_ID     = tPaySelLine.BankNumber_ID:
                create tCInvoicePaymentAttributes.
                assign tCInvoicePaymentAttributes.tcCInvoiceBankPayCodeValue = tqBankNumberPayCodeForCIDef.tcPayFormatCode
                    tCInvoicePaymentAttributes.tcPayFormatTypeCode = tqBankNumberPayCodeForCIDef.tcPayFormatTypeCode
                    tCInvoicePaymentAttributes.tiCInvoiceID = 0
                    tCInvoicePaymentAttributes.tiPayFormatCode_ID = tqBankNumberPayCodeForCIDef.tiPayFormatCode_ID
                    tCInvoicePaymentAttributes.tiCInvoiceBankID = 0
                    tCInvoicePaymentAttributes.tiPaymentSelectionLineID = tPaySelLine.PaySelLine_ID
                    tCInvoicePaymentAttributes.tiBankNumberID = tPaySelLine.BankNumber_ID
                    tCInvoicePaymentAttributes.tcPayFormatGroupLevel = tqBankNumberPayCodeForCIDef.tcPayFormatGroupLevel   
                    tCInvoicePaymentAttributes.tcCinvoiceType = tPaySelLine.PaySelLineObjectType
                    tCInvoicePaymentAttributes.tcInvCurrencyCode = tPaySelLine.tcInvoiceCurrencyCode
                    tCInvoicePaymentAttributes.tcInvCreditorCode = tPaySelLine.tcCreditorCode
                    tCInvoicePaymentAttributes.tlInvoiceGrouping = true.

                if tqBankNumberPayCodeForCIDef.tcPayFormatGroupCode = 'GROUPING':U
                then do:
                    assign tCInvoicePaymentAttributes.tlAttributeIsGrouping = true.
                
                    if tqBankNumberPayCodeForCIDef.tcPayFormatCode = 'False':U 
                    then assign tCInvoicePaymentAttributes.tlInvoiceGrouping = false.
                end.
            end. /* End of for each tqBankNumberPayCodeForCIDef */
        end. /* End of else do: */   
       
    end.
    else do:        
        <Q-5 run DInvoiceBankByPayFormatType (all) (Read) (NoCache)
           (input tPaySelLine.PaySelLineParentObject_ID, (DInvoiceID)
            input tPaySelLine.PayFormatTypeCode, (PayFormatTypeCode)
            input ?, (CompanyId)
            input tPaySelLine.BankNumber_ID, (BankNumberID)
            output dataset tqDInvoiceBankByPayFormatType) in BDInvoice>
       
        for each tqDInvoiceBankByPayFormatType no-lock
           where tqDInvoiceBankByPayFormatType.tiDInvoice_ID = tPaySelLine.PaySelLineParentObject_ID 
             and tqDInvoiceBankByPayFormatType.tcPayFormatTypeCode = tPaySelLine.PayFormatTypeCode
             and tqDInvoiceBankByPayFormatType.tiBankNumber_ID = tPaySelLine.BankNumber_ID :
                            
                create tCInvoicePaymentAttributes.
                assign tCInvoicePaymentAttributes.tcCInvoiceBankPayCodeValue = tqDInvoiceBankByPayFormatType.tcDInvoiceBankPayCodeValue
                       tCInvoicePaymentAttributes.tcPayFormatTypeCode = tqDInvoiceBankByPayFormatType.tcPayFormatTypeCode
                       tCInvoicePaymentAttributes.tiCInvoiceID = tqDInvoiceBankByPayFormatType.tiDInvoice_ID
                       tCInvoicePaymentAttributes.tiPayFormatCode_ID = tqDInvoiceBankByPayFormatType.tiPayFormatCode_ID
                       tCInvoicePaymentAttributes.tiCInvoiceBankID = tqDInvoiceBankByPayFormatType.tiDInvoiceBank_ID                   
                       tCInvoicePaymentAttributes.tiPaymentSelectionLineID = tPaySelLine.PaySelLine_ID
                       tCInvoicePaymentAttributes.tiBankNumberID = tPaySelLine.BankNumber_ID                       
                       tCInvoicePaymentAttributes.tcPayFormatGroupLevel = tqDInvoiceBankByPayFormatType.tcPayFormatGroupLevel
                       tCInvoicePaymentAttributes.tcCInvoiceType = tqDInvoiceBankByPayFormatType.tcDInvoiceType.
                       tCInvoicePaymentAttributes.tcInvCurrencyCode = tPaySelLine.tcInvoiceCurrencyCode.
                       tCInvoicePaymentAttributes.tlInvoiceGrouping = true.         
            
        end.     /* End for each tqDInvoiceBankByPayFormatType. */
    end. /* End else. */
end. /* End for each tPaySelLine. */
for each tCInvoicePaymentAttributes where tCInvoicePaymentAttributes.tlInvoiceGrouping = false: 
   for each bCInvoicePaymentAttributes3 where bCInvoicePaymentAttributes3.tiPaymentSelectionLineID = tCInvoicePaymentAttributes.tiPaymentSelectionLineID:
        assign bCInvoicePaymentAttributes3.tlInvoiceGrouping = false.
   end.
end.

assign viGroupCounter = 0.

INVOICE:
for each tCInvoicePaymentAttributes where 
   (tCInvoicePaymentAttributes.tcPayFormatGroupLevel = {&PAYFORMATGROUPLEVEL-PAYMENT} or
    tCInvoicePaymentAttributes.tcPayFormatGroupLevel = ? or
    tCInvoicePaymentAttributes.tcPayFormatGroupLevel = "":U or
    tCInvoicePaymentAttributes.tcPayFormatGroupLevel = "?":U)
break by tCInvoicePaymentAttributes.tiPaymentSelectionLineID on error undo, throw:
   
    /* Check to see if we have already processed this invoice. */
    if can-find (first tMatchingInvoices where tMatchingInvoices.tiPaySelLineID = tCInvoicePaymentAttributes.tiPaymentSelectionLineID)
    then next INVOICE.
    
    if first-of(tCInvoicePaymentAttributes.tiPaymentSelectionLineID) 
    then do:
        if tCInvoicePaymentAttributes.tlInvoiceGrouping or 
           (not (tCInvoicePaymentAttributes.tlInvoiceGrouping) and 
           tCInvoicePaymentAttributes.tcCInvoiceType <> 'CREDITNOTE':U and
           tCInvoicePaymentAttributes.tcCInvoiceType <> 'INVOICECORRECTION':U)
        then do:
            create tMatchingInvoiceGroup.
            assign tMatchingInvoiceGroup.tiGroupSeq = viGroupCounter        
                   tMatchingInvoiceGroup.tcCDocRowID = ""
                   tMatchingInvoiceGroup.tlCDocCreated = no
                   viGroupCounter = viGroupCounter + 1.
    
            create tMatchingInvoices.
            assign 
               tMatchingInvoices.tiPaySelLineID = tCInvoicePaymentAttributes.tiPaymentSelectionLineID
               tMatchingInvoices.tiGroupSeq = tMatchingInvoiceGroup.tiGroupSeq.
        end.

        /* Ignore the credit notes and InvoiceCorrection if we are not grouping. */
        if not (tCInvoicePaymentAttributes.tlInvoiceGrouping) and 
          (tCInvoicePaymentAttributes.tcCInvoiceType = 'CREDITNOTE':U or
           tCInvoicePaymentAttributes.tcCInvoiceType = 'INVOICECORRECTION':U)
        then do:
        
           find first tPaySelLine 
                where tPaySelLine.PaySelLine_ID = tCInvoicePaymentAttributes.tiPaymentSelectionLineID no-error.
           if available tPaySelLine
           then 
              assign tPaySelLine.tc_Status = "D".
           find first tSelectedCInvoiceFromPay    
                where tSelectedCInvoiceFromPay.tiCInvoiceId = tPaySelLine.PaySelLineParentObject_ID no-error.
           if not available tSelectedCInvoiceFromPay
           then do:
               create tSelectedCInvoiceFromPay.  
               assign tSelectedCInvoiceFromPay.tiCInvoiceId = tPaySelLine.PaySelLineParentObject_ID.                     
           end.    
           assign tSelectedCInvoiceFromPay.tlCInvoiceIsSelected = no  
                                                      vlWarning = true.
        end. /* Ignore credit notes. */
           
        if not(tCInvoicePaymentAttributes.tlInvoiceGrouping)
        then next INVOICE.
  
        MATCHINGINVOICE:
        for each bCInvoicePaymentAttributes where
            bCInvoicePaymentAttributes.tcPayFormatTypeCode = tCInvoicePaymentAttributes.tcPayFormatTypeCode and     
            bCInvoicePaymentAttributes.tcPayFormatGroupLevel = tCInvoicePaymentAttributes.tcPayFormatGroupLevel and
            bCInvoicePaymentAttributes.tiBankNumberID = tCInvoicePaymentAttributes.tiBankNumberID and
           (if bCInvoicePaymentAttributes.tcCinvoiceType <> {&PAYMENTSELECTIONTYPE-PREPAYMENT} then bCInvoicePaymentAttributes.tiCInvoiceBankID <> tCInvoicePaymentAttributes.tiCInvoiceBankID else true) and
            bCInvoicePaymentAttributes.tcInvCurrencyCode = tCInvoicePaymentAttributes.tcInvCurrencyCode and
            bCInvoicePaymentAttributes.tcInvCreditorCode = tCInvoicePaymentAttributes.tcInvCreditorCode and
            bCInvoicePaymentAttributes.tlInvoiceGrouping <> false             
        break by bCInvoicePaymentAttributes.tiPaymentSelectionLineID on error undo, throw:
                  
            /* Check to see we haven't processed this invoice already. */
            if can-find (first tMatchingInvoices where tMatchingInvoices.tiPaySelLineID = bCInvoicePaymentAttributes.tiPaymentSelectionLineID)
            then next MATCHINGINVOICE.

            if first-of(bCInvoicePaymentAttributes.tiPaymentSelectionLineID) then do:            
                assign viNumberOfAttributes = 0
                       viNumberOfMatchingAttributes = 0.
                        
                for each bCInvoicePaymentAttributes1 where
                    bCInvoicePaymentAttributes1.tcPayFormatTypeCode = tCInvoicePaymentAttributes.tcPayFormatTypeCode and
                   (if bCInvoicePaymentAttributes1.tcCinvoiceType <> {&PAYMENTSELECTIONTYPE-PREPAYMENT} then bCInvoicePaymentAttributes1.tiCInvoiceID = tCInvoicePaymentAttributes.tiCInvoiceID else true) and 
                    bCInvoicePaymentAttributes1.tiPaymentSelectionLineID = tCInvoicePaymentAttributes.tiPaymentSelectionLineID and 
                    bCInvoicePaymentAttributes1.tcPayFormatGroupLevel = tCInvoicePaymentAttributes.tcPayFormatGroupLevel and
                    bCInvoicePaymentAttributes1.tlAttributeIsGrouping <> true:
                    /* If the atrtribute's PayFormatGroupCode is 'Grouping' then ignore it. */
                    assign viNumberOfAttributes = viNumberOfAttributes + 1.
                    find first bCInvoicePaymentAttributes2 where
                        bCInvoicePaymentAttributes2.tcPayFormatTypeCode = bCInvoicePaymentAttributes.tcPayFormatTypeCode and
                        bCInvoicePaymentAttributes2.tiCInvoiceID = bCInvoicePaymentAttributes.tiCInvoiceID and
                        bCInvoicePaymentAttributes2.tiPaymentSelectionLineID = bCInvoicePaymentAttributes.tiPaymentSelectionLineID and
                        bCInvoicePaymentAttributes2.tcCInvoiceBankPayCodeValue = bCInvoicePaymentAttributes1.tcCInvoiceBankPayCodeValue and
                        bCInvoicePaymentAttributes2.tiPayFormatCode_ID = bCInvoicePaymentAttributes1.tiPayFormatCode_ID and
                        bCInvoicePaymentAttributes2.tcPayFormatGroupLevel = bCInvoicePaymentAttributes1.tcPayFormatGroupLevel no-error.
                
                    if available bCInvoicePaymentAttributes2 then do:
                        assign viNumberOfMatchingAttributes = viNumberOfMatchingAttributes + 1.
                    end.
                end.
                
                if viNumberOfAttributes = viNumberOfMatchingAttributes then
                do:   
                    create tMatchingInvoices.
                    assign 
                       tMatchingInvoices.tiPaySelLineID = bCInvoicePaymentAttributes.tiPaymentSelectionLineID
                       tMatchingInvoices.tiGroupSeq = tMatchingInvoiceGroup.tiGroupSeq.

                end. /* Attributes match */
            end.  /* End of first matching paysel line */                                          
        end.  /* End of for each bCInvoicePaymentAttributes */    
    end. /* First of paysel line */
end. /* Each tCInvoicePaymentAttributes */

if vlWarning then do:   /* Give warning for the credit notes that's ignored. */
   <M-41 run SetMessage
   (input  #T-29'This selection contains credit notes, these cannot be processed with Single Payment Per Invoice and will be removed from the selection':255(439223779)t-29# (icMessage), 
    input  '':U (icArguments), 
    input  '':U (icFieldName), 
    input  '':U (icFieldValue), 
    input  'W':U (icType), 
    input  3 (iiSeverity), 
    input  '':U (icRowid), 
    input  'qadfin-824290':U (icFcMsgNumber), 
    input  '':U (icFcExplanation), 
    input  '':U (icFcIdentification), 
    input  '':U (icFcContext), 
    output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
    assign oiReturnStatus = 1.
end.

if (vlWarning = true)
then do:
    if viBCInvoicePaySelID = 0 or viBCInvoicePaySelID = ?
     then do:
         <I-7 {bFcStartAndOpenInstance
              &ADD-TO-TRANSACTION   = "true"
              &CLASS                = "BCInvoice"}>
          assign vlBCInvIsStartedFromPaySel = true.
     end.
     else do:
         <I-62 {bFcOpenInstance
              &CLASS           = "BCInvoice"}>
     end.
     <M-23 run UpdateCInvoiceIsSelected
        (input  tSelectedCInvoiceFromPay (tSelectedCInvoice), 
         input  false (ilClearData), 
         output viFcReturnSuper (oiReturnStatus)) in BCInvoice>
     <I-56 {bFcCloseInstance
          &CLASS           = "BCInvoice"}>
     if viFcReturnSuper <> 0   
     then assign oiReturnStatus = viFcReturnSuper.        
     if viFcReturnSuper < 0
     then do:
         <M-9 run StopExternalInstances  (output viFcReturnSuper (oiReturnStatus)) in BPaymentSelection>
         return. 
     end.  
end.                           
              
if oiReturnStatus = -98 then do:
    assign oiReturnStatus = 0.
end.