project QadFinancials > class BDPaymentSelection > method StoreSelectedObjects

Description

Stores selected objects (invoices/prepayments) for this selection


Parameters


iiDPaySel_IDinputintegerPaySel ID
tDIncSelLineinputtemp-table
tDIncSelLineStageinputtemp-table
icGLCodeinputcharacter
icDPaySelStatusinputcharacterStatus of the payment selection
icTargetPaySelCodeinputcharacterIf there are records cancelled from this payment selection they will be added to the target Payments Selection (or new created)
iiTargetPaySel_IDinputintegerIf there are records cancelled from this payment selection they will be added to the target Payments Selection (or new created)
tDPaySelRefIDoutputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program4/bdpaymentselection.p)

find first tDPaySel where
           tDPaySel.DPaySel_ID = iiDPaySel_ID
           no-error.
assign viCollRowid = 0.
empty temp-table tRemovedDIncSelLine.
empty temp-table tRemovedDIncSelLineStg.
if not available tDPaySel
then do:    
    <M-11 run SetMessage
       (input  #T-7'The specified payment selection ID ($1) is not defined in the system or is invalid. No record was found with this ID.':100(2996)T-7# (icMessage), 
        input  string(iiDPaySel_ID) (icArguments), 
        input  '':U (icFieldName), 
        input  '':U (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-655283':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
        assign oiReturnStatus = -1.
    return.
end.

assign 
    vcCurrentDPaySelRowID = tDPaySel.tc_Rowid
    vcCurrentDPaySelCode  = tDPaySel.DPaySelCode
    vlIsCurrentPaySelExecuted = tDPaySel.tlPaySelIsExecuted.
    
/* Get header bank number */
if icGlCode <> "":U and
   icGlCode <> ?
then do:
    <Q-77 run GLPrim (all) (Read) (NoCache)
       (input tDPaySel.Company_ID, (CompanyId)
        input icGlCode, (GLCode)
        input ?, (GLId)
        output dataset tqGLPrim) in BGL>

    find first tqGLPrim
               no-error.
end.

if icGlCode = "":U or
   icGlCode = ?    or
   not available tqGLPrim
then do:
    <M-53 run SetMessage
       (input  trim(#T-85'The specified bank account code $1 is invalid.':80(2997)T-85#) (icMessage), 
        input  icGlCode (icArguments), 
        input  'tDPaySel.tcGLCode':U (icFieldName), 
        input  tDPaySel.tcGLCode (icFieldValue), 
        input  'E':U (icType), 
        input  3 (iiSeverity), 
        input  '':U (icRowid), 
        input  'qadfin-699217':U (icFcMsgNumber), 
        input  '':U (icFcExplanation), 
        input  '':U (icFcIdentification), 
        input  '':U (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
        assign oiReturnStatus = -1.
    return.
end.
<M-54 run CalculateDPaySelLine
    (input  yes (ilPrepaymentOnly), 
     output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>        

empty temp-table tDInvoiceToUpdate.
empty temp-table tDInvoiceStageToUpdate.
empty temp-table tDPaySelRefID.
/* new records */
for each tDIncSelLine where
    tDIncSelLine.tc_ParentRowid = tDPaySel.tc_Rowid and             
    ( tDIncSelLine.tc_Status = "N":U or ( tDIncSelLine.tc_Status = "C":U and vlIsCurrentPaySelExecuted = false ) ) 
    and tDIncSelLine.tlIsSelected on error undo, throw:
    
    /* for every selected incsel line create PaySelLine in inital mode */
    if icDPaySelStatus = {&DOCUMENTSTATUS-INIT}
    then do:
    
        if tDIncSelLine.tc_status = "N":U or tDIncSelLine.tc_status = "C":U
        then do:
            
            /* Check if the paysel line already exist in the instance */
            for first tDPaySelLine 
                where tDPaySelLine.tc_parentrowid = tDPaySel.tc_Rowid
                  and tDPaySelLine.DPaySelLineParentObject_ID = tDIncSelLine.tiDInvoiceId:
            end.
            if tDIncSelLine.tc_status = "C":U and not available tDPaySelLine
            then do:
                assign vcMessage = substitute(#T-68'The payment line for invoice &1 does not exist in the system.':255(139775052)T-68#, tDIncSelLine.tcInvoiceNumber).
                <M-45 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-817124':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            
                assign oiReturnStatus = -1.
            return.        
        end. /* if tDIncSelLine.tc_status = "N":U or tDIncSelLine.tc_status = "C":U */
        
        if tDIncSelLine.tc_status = "N":U
        then do:
            if not available tDPaySelLine
            then do:
                <M-58 run AddDetailLine
                   (input  'DPaySelLine':U (icTable), 
                    input  tDPaySel.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>                               
                if viFcReturnSuper < 0 or
                   oiReturnStatus  = 0
                then assign oiReturnStatus = viFcReturnSuper.                
                if oiReturnStatus < 0
                then return.                     
            end. /* end create payselline */
            
            assign 
               tDPaySelLine.tc_Status = "N"
               tDPaySelLine.DPaySelLineParentObject_ID  = tDIncSelLine.tiDInvoiceId
               tDPaySelLine.DPaySelLineObjectType       = tDIncSelLine.tcInvoiceType
               tDPaySelLine.tlIsSelected                = tDIncSelLine.tlIsSelected
               tDPaySelLine.BankNumber_ID               = tDIncSelLine.tiBankNumberId
               tDPaySelLine.tcDInvoiceNumber            = tDIncSelLine.tcInvoiceNumber           
               tDPaySelLine.tcDebtorCode                = tDIncSelLine.tcDebtorCode
               tDPaySelLine.tcBusinessRelationCode      = tDIncSelLine.tcBusinessRelationCode
               tDPaySelLine.tcCurrencyCode              = tDIncSelLine.tcInvoiceCurrencyCode              
               tDPaySelLine.tcInvoiceCrDt               = tDIncSelLine.tcInvoiceAmountCrDt
               tDPaySelLine.tcPaymentCrDt               = tDIncSelLine.tcIncSelLineAmountCrDt
               tDPaySelLine.DPaySelLineStatus           = tDIncSelLine.tcIncSelLineStatus.            
                      
                     
            /* create the tDPaySelPayRef Record which will store bankpayment details for this staged
               record - only create if not staged payment */
            if tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-NORMAL}               
            then do:  
            
                for first tDPaySelPayRef 
                    where tDPaySelPayRef.tc_ParentRowid = tDPaySelLine.tc_Rowid
                      and tDPaySelPayRef.DPaySelLine_ID = tDPaySelLine.DPaySelLine_ID
                      and tDPaySelPayRef.DPaySelLineStg_id = 0:
                end.      
                
                if not available tDPaySelPayRef
                then do:
                    <M-57 run AddDetailLine
                       (input  'DPaySelPayRef':U (icTable), 
                        input  tDPaySelLine.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>                                                        
                    if viFcReturnSuper < 0 or
                       oiReturnStatus  = 0
                    then assign oiReturnStatus = viFcReturnSuper.    
                    if oiReturnStatus < 0
                    then return.                     
                end.
                
                assign
                    tDPaySelPayRef.DPaySelLine_ID = tDPaySelLine.DPaySelLine_ID
                    tDPaySelPayRef.DPaySelLineStg_id = 0
                    /*  Pad the Number with leading zeros */
                    tDPaySelPayRef.DPaySelPayRefCode = string(tDPaySelPayRef.DPaySelPayRef_ID,"9999999999").                    
            end. /* if tDIncSelLine.tcPaymentConditionPayType = {PAYMENTCONDITIONPAYMENTTYPE-NORMAL} */
            
            /* Get the diskette format in order to split the collections */
            if tDPaySelLine.BankNumber_ID <> ? and
               tDPaySelLine.BankNumber_ID <> 0 and
               tDPaySelLine.BankNumber_ID <> -999999
            then do:           
                
                <Q-69 run BankNumberPrim (all) (Read) (NoCache)
                   (input tDPaySelLine.BankNumber_ID, (BankNumberId)
                    input ?, (ParentObjectId)
                    input '':U, (BankNumber)
                    input '':U, (BankNumberExtension)
                    input ?, (BankPayFormatID)
                    output dataset tqBankNumberPrim) in BBankNumber>
                 
                find first tqBankNumberPrim where tqBankNumberPrim.tiBankNumber_ID = tDPaySelLine.BankNumber_ID no-error.
        
                if available tqBankNumberPrim and
                   tqBankNumberPrim.tiBankPayFormat_ID <> ? and
                   tqBankNumberPrim.tiBankPayFormat_ID <> 0
                then do:
                    
                    <Q-23 run BankPayFormatPrim (all) (Read) (NoCache)
                       (input ?, (BankNumberId)
                        input tqBankNumberPrim.tiBankPayFormat_ID, (BankPayFormatId)
                        input ?, (PayFormatTypeCode)
                        output dataset tqBankPayFormatPrim) in BBankPayFormat>
        
                    for first tqBankPayFormatPrim:
                        assign tDPaySelLine.DPaySelPayFormatTypeCode = tqBankPayFormatPrim.tcPayFormatTypeCode.
                    end. /* for first tqBankPayFormatPrim */
                end. /* if available tqBankNumberPrim */
            end. /* if tDPaySelLine.BankNumber_ID <> ? */
       end. /* if tDIncSelLine.tc_status = "N":U */
       
                                                
       /* process the stage records - only want to include those lines which are being paid */
        for each tDIncSelLineStage where
            tDIncSelLineStage.tc_Status = 'N':U and
            tDIncSelLineStage.tc_ParentRowid = tDIncSelLine.tc_Rowid and 
            tDIncSelLineStage.tdStagePaymentAmountTC > 0 on error undo, throw:
            
             /* Check to make sure the paysel line doesn't already exist in the instance */
            for first tDPaySelLineStg
                where tDPaySelLineStg.tc_parentrowid = tDPaySelLine.tc_Rowid
                  and tDPaySelLineStg.DInvoiceStage_ID = tDIncSelLineStage.tiDInvoiceStageId:
            end.
        
            if not available tDPaySelLineStg
            then do:
             
                <M-50 run AddDetailLine
                   (input  'DPaySelLineStg':U (icTable), 
                    input  tDPaySelLine.tc_rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
                 
                if viFcReturnSuper < 0 or oiReturnStatus  = 0
                then assign oiReturnStatus = viFcReturnSuper.
    
                if oiReturnStatus < 0
                then return.                 
            end.
            
            assign 
                tDPaySelLineStg.tc_Status                  = 'N'
                tDPaySelLineStg.DPaySelLine_ID             = tDPaySelLine.DPaySelLine_ID
                tDPaySelLineStg.DInvoiceStage_ID           = tDIncSelLineStage.tiDInvoiceStageId
                tDPaySelLineStg.DPaySelLineStgOrigAmtTC   = tDIncSelLineStage.tdOriginalStageAmountTC
                tDPaySelLineStg.DPaySelLineStgOpenAmtTC   = tDIncSelLineStage.tdOpenStageAmountTC
                tDPaySelLineStg.DPaySelLineStgStatus      = tDIncSelLineStage.tcIncSelLineStageStatus.
                 
              /* create the tDPaySelPayRef Record which will store bankpayment details for this staged
               record - only create if not staged payment */
         
                for first tDPaySelPayRef 
                    where tDPaySelPayRef.tc_ParentRowid = tDPaySelLine.tc_Rowid
                      and tDPaySelPayRef.DPaySelLine_ID = tDPaySelLine.DPaySelLine_ID
                      and tDPaySelPayRef.DPaySelLineStg_id = tDPaySelLineStg.DPaySelLineStg_id:
                end.      
            
                if not available tDPaySelPayRef
                then do:

                    <M-79 run AddDetailLine
                       (input  'DPaySelPayRef':U (icTable), 
                        input  tDPaySelLine.tc_rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
                    if viFcReturnSuper < 0 or
                       oiReturnStatus  = 0
                    then assign oiReturnStatus = viFcReturnSuper.
            
                    if oiReturnStatus < 0
                    then return.        
                end.             
                                              
                assign                    
                    tDPaySelPayRef.DPaySelLine_ID = tDPaySelLine.DPaySelLine_ID
                    tDPaySelPayRef.DPaySelLineStg_id = tDPaySelLineStg.DPaySelLineStg_id   
                    /*  Pad the Number with leading zeros */
                    tDPaySelPayRef.DPaySelPayRefCode = string(tDPaySelPayRef.DPaySelPayRef_ID,"9999999999").  
            end. /* for each tDIncSelLineStage */                                                                 
                     
       end. /* if tDIncSelLine.tc_status = "N":U */                           
    end. /* if status = initial */
end. /* for each tDIncSelLine */

for each tDIncSelLine where 
        tDIncSelLine.tc_ParentRowid  = vcCurrentDPaySelRowID and 
        tDIncSelLine.tc_Status = "C":U or tDIncSelLine.tc_Status = "D":U or 
        tDIncSelLine.tc_Status = "N":U:
    
    for first tDPaySelLine 
        where tDPaySelLine.tc_ParentRowid  = vcCurrentDPaySelRowID and                                                                     
	       tDPaySelLine.DPaySelLineParentObject_ID = tDIncSelLine.tiDInvoiceId and
	       tDPaySelLine.DPaySelLineObjectType = tDIncSelLine.tcInvoiceType: 
				   
        if vlIsCurrentPaySelExecuted = false
        then do:
            assign 
                tDPaySelLine.DPaySelLineIntRate        = tDIncSelLine.tdIncSelLineIntRate
                tDPaySelLine.DPaySelLineAmountTC       = tDIncSelLine.tdIncSelLineAmountTC
                tDPaySelLine.DPaySelLineDiscountTC     = tDIncSelLine.tdIncSelLineDiscountTC
                tDPaySelLine.tc_status                 = tDIncSelLine.tc_Status.
        end.	           
        else do:
            assign 
                tDPaySelLine.DPaySelLineIntRate        = tDIncSelLine.tdIncSelLineIntRate        	      
                tDPaySelLine.tc_status                 = "C":U.                        
        end.				                       							  				            
  
        /* check to see if the due date has changed */                       
        if tDIncSelLine.ttDefaultDueDate <> tDIncSelLine.ttDueDate
        then do:
	     find first tDInvoiceToUpdate 
		    where tDInvoiceToUpdate.tiDInvoiceId = tDPaySelLine.DPaySelLineParentObject_ID no-error.
		    
		    if not available tDInvoiceToUpdate then
			   create tDInvoiceToUpdate.
 
                  assign 
	    	        tDInvoiceToUpdate.tiDInvoiceId = tDPaySelLine.DPaySelLineParentObject_ID
			 tDInvoiceToUpdate.ttDueDate = tDIncSelLine.ttDueDate
			 tDInvoiceToUpdate.tlIsStageInvoice = if tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} then true else false
			 tDInvoiceToUpdate.tlIsSelected = if (vlIsCurrentPaySelExecuted and tDIncSelLine.tcIncSelLineStatus = {&PAYSEL-LINE-STATUS-MODIFIED})
			                                     then ?   
			                                  else if (tDIncSelLine.tc_Status = "N":U and tDIncSelLine.tcIncSelLineStatus = {&PAYSEL-LINE-STATUS-NEW})
                                                           then true 
                                                       else if (tDIncSelLine.tc_Status = "D":U and tDIncSelLine.tcIncSelLineStatus <> {&PAYSEL-LINE-STATUS-MODIFIED})                                                       
                                                           then false
                                                       else 
                                                           ?.                
        end.  /* end of due date has changed */
        
        /* find if any of the due dates have been modified, if so then we need to update the duedate and is selected
           in one transaction so add all records to the StageToUpdate table 
        */
        find first tDIncSelLineStage where tDIncSelLineStage.ttDefaultIncSelLineStageDueDat <> tDIncSelLineStage.ttStageDueDate no-error.
        assign vlStageDueDateChanged = if available tDIncSelLineStage 
                                        then true else false.	 
	      	      	      													  
        /* Each Stage in Temp table */          
        /* if the stage is modified, the status will be D on executed as it will be deleted
            from the current paysel and moved to the new paysel */
        for each tDIncSelLineStage 
	     where tDIncSelLineStage.tc_ParentRowid = tDIncSelLine.tc_Rowid 
	     and tDIncSelLineStage.tc_Status = "D":U 
	     or tDIncSelLineStage.tc_Status = "C":U 
	     or tDIncSelLineStage.tc_Status = "N":U on error undo, throw:
		    																																			  
                /* For first stage Payment  */                    
            for first tDPaySelLineStg 
                where tDPaySelLineStg.DInvoiceStage_ID = tDIncSelLineStage.tiDInvoiceStageId and
                tDPaySelLineStg.tc_ParentRowid = tDPaySelLine.tc_Rowid:
						
                /* Non Executed */
                if vlIsCurrentPaySelExecuted = false 
                then do:                    
                    assign 
                        tDPaySelLineStg.DPaySelLineStgIntRate      = tDIncSelLineStage.tdIncSelLineStageIntRate
                        tDPaySelLineStg.DPaySelLineStgPaymAmtTC    = tDIncSelLineStage.tdStagePaymentAmountTC
                        tDPaySelLineStg.DPaySelLineStgDiscAmtTC    = tDIncSelLineStage.tdStageDiscountAmountTC
                        tDPaySelLineStg.tc_status                  = tDIncSelLineStage.tc_status.
                end.
                else do: /* Executed */
                    assign 
                        tDPaySelLineStg.DPaySelLineStgIntRate      = tDIncSelLineStage.tdIncSelLineStageIntRate
                        tDPaySelLineStg.tc_status                  = "C":U.                                                
                end.
                                    																			  				
                /* check to see if the due date has changed - if it's changed for any stage, create DInvoiceStateToUpdate record */      
                if tDIncSelLineStage.ttDefaultIncSelLineStageDueDat <> tDIncSelLineStage.ttStageDueDate or vlStageDueDateChanged
                then do:
                    find first tDInvoiceStageToUpdate 
                        where tDInvoiceStageToUpdate.tiDInvoiceStageId = tDPaySelLineStg.DInvoiceStage_ID no-error.
                    if not available tDInvoiceStageToUpdate then
                        create tDInvoiceStageToUpdate.
	   
                    assign 
                        tDInvoiceStageToUpdate.tiDInvoiceId      = tDPaySelLine.DPaySelLineParentObject_ID
                        tDInvoiceStageToUpdate.tiDInvoiceStageId = tDPaySelLineStg.DInvoiceStage_ID
                        tDInvoiceStageToUpdate.ttDueDate         = tDIncSelLineStage.ttStageDueDate
                        tDInvoiceStageToUpdate.tlIsSelected      = if vlIsCurrentPaySelExecuted and tDIncSelLineStage.tcIncSelLineStageStatus = {&PAYSEL-LINE-STATUS-MODIFIED}
                    			                                  then ?
                                                                   else if (tDIncSelLineStage.tc_Status = "N" and tDIncSelLineStage.tcIncSelLineStageStatus = {&PAYSEL-LINE-STATUS-NEW})
                                                                      then true 
                                                                   else if (tDIncSelLineStage.tc_Status = "D" and tDIncSelLineStage.tcIncSelLineStageStatus <> {&PAYSEL-LINE-STATUS-MODIFIED})
                                                                      then false
                                                                   else ?.
                                                                                                                                                           
                    /* if we have changed an invoice stage then also record the invoice itself.  We will need
                       this to load the invoice into the instance later */     
                    find first tDInvoiceToUpdate 
                        where tDInvoiceToUpdate.tiDInvoiceId = tDPaySelLine.DPaySelLineParentObject_ID no-error.
                    
                    if not available tDInvoiceToUpdate
                    then do:
                        create tDInvoiceToUpdate.                                  
                        assign 
                            tDInvoiceToUpdate.tiDInvoiceId = tDPaySelLine.DPaySelLineParentObject_ID
                            tDInvoiceToUpdate.tlIsStageInvoice = if tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED} 
                                                                    then true else false
            	              tDInvoiceToUpdate.tlIsSelected      = if tDIncSelLine.tc_Status = "N" 
                                                                      then true 
                                                                  else if tDIncSelLine.tc_Status = "D"
                                                                      then false
                                                                  else ?. /* ? = no change */                                                      
                    end. /* no tDInvoiceToUpdate available */
                end.  /* due date on stage has changed */                        
            end. /* for first tDPaySelLineStg */                                     
        end. /* for each tDIncSelLineStage */
        
       /* if it's a staged payment, then if all the stages are deleted, set the line to D also */    
       if tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}               
       then do:                                                                           
            for each tDIncSelLineStage where tDIncSelLineStage.tc_ParentRowid = tDIncSelLine.tc_Rowid:
                  if tDIncSelLineStage.tc_Status <> "D" then do:                          
                         assign vlallStagesDeleted = FALSE.
                         LEAVE.
                  end.          
            end.
                     
            /*Are all stages flagged as delete, then set the payment line to delete also */
            if (vlallStagesDeleted) then do:                                                                                                           
               assign tDPaySelLine.tc_status = "D".                              
            end. /*if (allStagesDeleted) then do:*/                       
        end. /*tDIncSelLine.tcPaymentConditionPayType= */                                                                                                                                                                                               
    end.  /* for first tDPaySelLine */            
end. /* for each tIncSelLine */

/* For each payment line than is non-executed and marked as deleted */
for each tDIncSelLine where ((tDIncSelLine.tlIsCancelled = false) and
  ((tDIncSelLine.tc_Status = "D":U and vlIsCurrentPaySelExecuted  = false) OR (tDIncSelLine.tlIsSelected = false))):   
    for first tDPaySelLine 
        where tDPaySelLine.tc_ParentRowid  = vcCurrentDPaySelRowID and                                                                     
              tDPaySelLine.DPaySelLineParentObject_ID = tDIncSelLine.tiDInvoiceId 
              and  tDPaySelLine.DPaySelLineObjectType = tDIncSelLine.tcInvoiceType:         
        /* Delete the payment line */                
        assign tDPaySelLine.tc_status = "D":U.
                       
    end.       
end.


/* Ensure that a new payment selection has been specified */    
/* if there is a cancellation in any line then create the new/modified 
DPaySel instance */

if ((can-find
        (first tDIncSelLine where
                      tDIncSelLine.tc_ParentRowid            = tDPaySel.tc_Rowid and
                      tDIncSelLine.tc_Status                 = "D":U or 
                      tDIncSelLine.tc_Status                 = "C":U)) and
    vlIsCurrentPaySelExecuted )
then do:
    assign 
        tDPaySel.tc_Status              = "C"
        vdCurrentPaySelTotalAmountBC    = tDPaySel.tdDPaySelTotalAmountBC
        viCurrentGLID                   = tDPaySel.GL_ID
        viCurrentBankPayFormatID        = tDPaySel.BankPayFormat_ID.
    /* has a new paysel code been entered ? */    
    if (icTargetPaySelCode = "":U or icTargetPaySelCode = ?)
    then do:
        assign vcMessage =  #T-86'You attempted to cancel/modify an invoice from this payment selection. You must specify a target payment selection.':255(417273455)T-86#.
        <M-44 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-333645':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
        
            assign oiReturnStatus = -1.
        return.
    end. /* if (icTargetPaySelCode) */
    
    /* is this a new or existing DPaySel */
    <Q-73 run DPaySelPrim (all) (Read) (NoCache)
       (input ?, (CompanyId)
        input ?, (DPaySelId)
        input icTargetPaySelCode, (DPaySelCode)
        output dataset tqDPaySelPrim) in BDPaymentSelection>
    
    find first tqDPaySelPrim where tqDPaySelPrim.tcDPaySelCode = icTargetPaySelCode no-error.   
    if available tqDPaySelPrim 
    then do:        
        <M-92 run DataLoad
           (input  '':U (icRowids), 
            input  tqDPaySelPrim.tiDPaySel_ID (icPkeys), 
            input  '':U (icObjectIds), 
            input  '':U (icFreeform), 
            input  true (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then do:        
                assign vcMessage      = trim(substitute(#T-60'&1 not available.':200(406)T-60#, "icPaySelIDs":U)) + chr(10) + 
                                          program-name(1).                   
                <M-81 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':u (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-168545':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            end. /* if viFcReturnSuper < 0 */
            return.                       
        end. /* if viFcReturnSuper <> 0 */                       
        find first tDPaySel where tDPaySel.DPaySel_ID = tqDPaySelPrim.tiDPaySel_ID.
        if not available tDPaySel 
        then do:
                assign 
                    oiReturnStatus = -1                                          
                    vcMessage      = trim(substitute(#T-70'Error occurred when load Customer Payment Selection &1':150(731089745)T-70#, 
                                            icTargetPaySelCode)).
                <M-98 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'E':u (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'qadfin-168222':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>            
            return.                       
        end. /* if not available tDPaySel */ 
        
        <M-65 run CheckTargetPaymentSelection
           (input  viCurrentGLID (iiCurrentGLID), 
            input  viCurrentBankPayFormatID (iiCurrentBankPayFormatID), 
            input  tDPaySel.DPaySel_ID (iiTargetPaySelID), 
            input  vcCurrentDPaySelCode (icCurrentDPaySelCode), 
            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
    
        if oiReturnStatus < 0
        then return.
        assign 
            vcTargetDPaySelRowID = tDPaySel.tc_Rowid
            tDPaySel.tc_Status   = "C".
    end. /* if available tqDPaySelPrim */
    else do:                    
        <M-90 run AddDetailLine
            (input  'DPaySel':U (icTable), 
            input  '':U (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>    
               
        if viFcReturnSuper < 0 or oiReturnStatus  = 0
        then assign oiReturnStatus = viFcReturnSuper.
    
        if oiReturnStatus < 0
        then return.
        find first bDPaySel where bDPaySel.DPaySel_ID = iiDPaySel_ID no-error.
        if available bDPaySel
        then do:
            buffer-copy bDPaySel  
                except 
                    DPaySel_ID                     
                    tc_Rowid                     
                    tc_status
                    DPaySelCode
                    DPaySelDate 
                    to tDPaySel.
            assign                                  
                tDPaySel.DPaySelCode = icTargetPaySelCode
                tDPaySel.DPaySelDate = today.   
        /* copy the header fields to the new paysel*/
            for each bDPaySelPayCode where bDPaySelPayCode.DPaySel_ID = iiDPaySel_ID
                on error undo, throw:               
             
               <M-96 run AddDetailLine
                  (input  'DPaySelPayCode':U (icTable), 
                   input  tDPaySel.tc_Rowid (icParentRowid), 
                   output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection> 
                    
               if viFcReturnSuper < 0 or oiReturnStatus  = 0
                    then assign oiReturnStatus = viFcReturnSuper.
        
                if oiReturnStatus < 0
                then return.
                buffer-copy bDPaySelPayCode 
                    except 
                        DPaySelPayCode_ID 
                        DPaySel_ID                    
                        tc_Rowid
                        tc_ParentRowid                     
                        tc_status                                        
                        to tDPaySelPayCode.
                assign                                  
                    tDPaySelPayCode.DPaySel_ID = tDPaySel.DPaySel_ID.
            end.   /* for each */         
        end. /* if available bDPaySel */                                  
    end. /* else do */                  
    /* find lines to move to new selection */

    assign
        vcTargetDPaySelRowID = tDPaySel.tc_Rowid
        viTargetDPaySelID    = tDPaySel.DPaySel_ID
        vdtotalSumCR = 0
        vdtotalSumDR = 0.

    /* Do the moving for deleted and modified lines and stages */        
    for each tDIncSelLine where
        tDIncSelLine.tc_ParentRowid = vcCurrentDPaySelRowID and  
        tDIncSelLine.tc_Status = "D":U on error undo, throw:               
        assign viSourceDPaySelLineID = 0.
        /* get the tDPaySelLine Record */
        find first tDPaySelLine where
            tDPaySelLine.tc_ParentRowid             = vcCurrentDPaySelRowID      and            
            tDPaySelLine.DPaySelLineParentObject_ID = tDIncSelLine.tiDInvoiceId and
            tDPaySelLine.DPaySelLineObjectType      = tDIncSelLine.tcInvoiceType no-error.
       
        if not available tDPaySelLine          
        then do:
            assign vcMessage =  #T-31'The specified payment selection line is not defined in the system or is invalid.':140(9847407)T-31#.
            <M-75 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-533122':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
        
                assign oiReturnStatus = -1.
            return.
        end. /* if available tDPaySelLine */
        
        assign viSourceDPaySelLineID = tDPaySelLine.DPaySelLine_ID.
        
        /* if the line had a status of cancelled, the user cannot change it */
        if (tDPaySelLine.DPaySelLineStatus = {&PAYSEL-LINE-STATUS-CANCEL} and
            tDIncSelLine.tcIncSelLineStatus <> {&PAYSEL-LINE-STATUS-CANCEL})
        then do:
             assign vcMessage =  trim(substitute(#T-520'You cannot change the status of a cancelled payment - invoice reference &1.':100(382984695)T-520#, tDPaySelLine.tcDInvoiceNumber)).
            <M-9 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  tDIncSelLine.tcIncSelLineStatusTR (icFieldValue), 
                input  'E':U (icType), 
                input  3 (iiSeverity), 
                input  '':U (icRowid), 
                input  'qadfin-130156':U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
        
                assign oiReturnStatus = -1.
            return.
        end.
                
        if vlIsCurrentPaySelExecuted AND tDIncSelLine.tc_Status = "D":U 
           AND tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-NORMAL}
        then do:                                       
            assign 
               tDPaySelLine.tcCurrencyCode      = tDIncSelLine.tcInvoiceCurrencyCode
               tDPaySelLine.tcInvoiceCrDt       = tDIncSelLine.tcInvoiceAmountCrDt
               tDPaySelLine.tcPaymentCrDt       = tDIncSelLine.tcIncSelLineAmountCrDt
               tDPaySelLine.DPaySelLineStatus   = tDIncSelLine.tcIncSelLineStatus
               tDPaySelLine.tc_ParentRowid      = tDPaySel.tc_Rowid
               tDPaySelLine.DPaySel_ID          = tDPaySel.DPaySel_ID
               tDPaySelLine.tc_Status           = "C":U
               tDPaySelLine.DPaySelLineStatus   = tDIncSelLine.tcIncSelLineStatus.
            /* create a copy */
            
            
            create tRemovedDIncSelLine.
            buffer-copy tDIncSelLine to tRemovedDIncSelLine.

            if tDIncSelLine.tcIncSelLineAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
            then do:
                assign vdtotalSumDR = vdtotalSumDR + tDIncSelLine.tdIncSelLineAmountBC.
            end.
            else do:
                assign vdtotalSumCR = vdtotalSumCR + tDIncSelLine.tdIncSelLineAmountBC.
            end.
        end. /* if tDIncSelLine.tc_Status = "D":U */                            
        else if vlIsCurrentPaySelExecuted and tDIncSelLine.tcPaymentConditionPayType = {&PAYMENTCONDITIONPAYMENTTYPE-STAGED}
        then do:          
            
            /* have any staged lines been cancelled - if so then update the amounts on the line */     
            if can-find(first tDIncSelLineStage where tDIncSelLineStage.tc_parentrowid = tDIncSelLine.tc_rowid and      
                tDIncSelLineStage .tc_Status = "D":U)   
            then do:            
            
                /* update the source line amounts as they have changed because of a staged payment being removed */
                assign
                    tDPaySelLine.DPaySelLineAmountTC    = tDIncSelLine.tdIncSelLineAmountTC 
                    tDPaySelLine.DPaySelLineDiscountTC  = tDIncSelLine.tdIncSelLineDiscountTC
                    tDPaySelLine.tc_Status              = "C":U.
                create tRemovedDIncSelLine.
                buffer-copy tDIncSelLine to tRemovedDIncSelLine.
            end. /* if can-find */      
                      
            for each tDIncSelLineStage where tDIncSelLineStage.tc_ParentRowid = tDIncSelLine.tc_Rowid and             
                (tDIncSelLineStage.tc_Status = "D":U or tDIncSelLineStage.tc_Status = "C":U) on error undo, throw:                                                                                       
                /* find the DPaySelLineStg record */                                   
                find first tDPaySelLineStg where
                    tDPaySelLineStg.tc_ParentRowid         = tDIncSelLine.tc_Rowid   and
                    tDPaySelLineStg.tc_Status              <> "D":U                  and
                    tDPaySelLineStg.DInvoiceStage_ID       = tDIncSelLineStage.tiDInvoiceStageId no-error.

                /* Find the stage in question */                   
                if available tDPaySelLineStg          
                then do:                                                                   
                    /* if the line stage had a status of cancelled, the user cannot change it */
                    if (tDPaySelLineStg.DPaySelLineStgStatus = {&PAYSEL-LINE-STATUS-CANCEL} and
                        tDIncSelLineStage.tcIncSelLineStageStatus <> {&PAYSEL-LINE-STATUS-CANCEL})
                    then do:
                         assign vcMessage =  trim(substitute(#T-40'You cannot change the status of a cancelled payment - invoice reference &1.':100(382984695)T-40#, tDPaySelLine.tcDInvoiceNumber)).
                        <M-37 run SetMessage
                           (input  vcMessage (icMessage), 
                            input  '':U (icArguments), 
                            input  '':U (icFieldName), 
                            input  tDIncSelLineStage.tcIncSelLineStageStatusTR (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  '':U (icRowid), 
                            input  'qadfin-262336':U (icFcMsgNumber), 
                            input  '':U (icFcExplanation), 
                            input  '':U (icFcIdentification), 
                            input  '':U (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
                    
                            assign oiReturnStatus = -1.
                        return.
                    end.  
                    
                    create tRemovedDIncSelLineStg.                    
                    buffer-copy tDIncSelLineStage to tRemovedDIncSelLineStg.
                    
                    
                    empty  temp-table tDPaySelPayRefCopy.
                    
                    /* take a copy of the tDPaySelPayRef record - before I create new one */
                    find first tDPaySelPayRef where 
                        tDPaySelPayRef.DPaySelLineStg_ID = tDPaySelLineStg.DPaySelLineStg_ID   and
                        tDPaySelPayRef.DPaySelLine_ID    = tDPaySelLineStg.DPaySelLine_ID no-error.
                        
                    create tDPaySelPayRefCopy.
                        
                    if available tDPaySelPayRef
                    then do:
                        buffer-copy tDPaySelPayRef to tDPaySelPayRefCopy.                            
                        /* kyn - make sure the stage ref line is also deleted*/
                        tDPaySelPayRef.tc_Status = 'D':U.
                        create tDPaySelRefID.
                        assign tDPaySelRefID.tiObjectId = tDPaySelPayRef.DPaySelPayRef_ID.                                             
                    end. /* if available tDPaySelPayRef */   
                         
                    /* does this line exist on the target DPaySel? */
                    find first bDPaySelLine where                    
                        bDPaySelLine.tc_ParentRowid             = vcTargetDPaySelRowID and
                        bDPaySelLine.DPaySelLineParentObject_ID = tDPaySelLine.DPaySelLineParentObject_ID no-error.
                    
                    /* Payment Line already exists on target - just add the stage line*/
                    if available bDPaySelLine
                    then do:                                                                               
                        /* only assign to C if it's not been created here */                                                 
                        if bDPaySelLine.tc_Status <> "N":U
                            then assign bDPaySelLine.tc_Status = "C":U.
                           
                        /* Flag the orginial stage as deleted */ 
                        /* Need to flag the old stage on the source as deleted */
                        assign 
                            tDPaySelLineStg.tc_Status = 'D':U
                            tDPaySelLineStg.DPaySelLineStgStatus = tDIncSelLineStage.tcIncSelLineStageStatus.
                      
                                                                                                                                                                                                                                                                                                                                         
                        /* Create new stage line (tc_status = ā€˜Nā€™) on target pay sel line  */                                                        
                        <M-34 run AddDetailLine
                           (input  'DPaySelLineStg':U (icTable), 
                            input  bDPaySelLine.tc_Rowid (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>                                

                        if viFcReturnSuper < 0 or oiReturnStatus  = 0
                            then assign oiReturnStatus = viFcReturnSuper.    
                        if oiReturnStatus < 0
                            then return.   
                                                                                                        
                         assign 
                            tDPaySelLineStg.DPaySelLine_ID            = bDPaySelLine.DPaySelLine_ID
                            tDPaySelLineStg.DInvoiceStage_ID          = tDIncSelLineStage.tiDInvoiceStageId
                            tDPaySelLineStg.DPaySelLineStgPaymAmtTC   = tDIncSelLineStage.tdStagePaymentAmountTC
                            tDPaySelLineStg.DPaySelLineStgDiscAmtTC   = tDIncSelLineStage.tdStageDiscountAmountTC
                            tDPaySelLineStg.DPaySelLineStgOrigAmtTC   = tDIncSelLineStage.tdOriginalStageAmountTC
                            tDPaySelLineStg.DPaySelLineStgOpenAmtTC   = tDIncSelLineStage.tdOpenStageAmountTC
                            tDPaySelLineStg.DPaySelLineStgStatus      = tDIncSelLineStage.tcIncSelLineStageStatus
                            tDPaySelLineStg.DPaySelLineStgIntRate     = tDIncSelLineStage.tdIncSelLineStageIntRate.                           
                                                                                                                                                               
                    end. /* if available tDPaySelLine */                    
                                        
                    else do:                            
                        /* need to copy the line */
                        <M-76 run AddDetailLine
                           (input  'DPaySelLine':U (icTable), 
                            input  vcTargetDPaySelRowID (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>                                                                                    
                                        
                        if viFcReturnSuper < 0 or oiReturnStatus  = 0
                            then assign oiReturnStatus = viFcReturnSuper.
                                
                        if oiReturnStatus < 0
                            then return.                                                                            
                                                
                        /* Find the current payment line */    
                        find first bDPaySelLine where  
                            bDPaySelLine.tc_ParentRowid             = vcCurrentDPaySelRowID      and                
                            bDPaySelLine.DPaySelLineParentObject_ID = tDIncSelLine.tiDInvoiceId and
                            bDPaySelLine.DPaySelLineObjectType      = tDIncSelLine.tcInvoiceType no-error.                          
                        
                        if available bDPaySelLine
                        then do:
                            
                            /* Copy the existing payment line */                                                                                          
                            buffer-copy bDPaySelLine  
                                except 
                                DPaySel_ID 
                                DPaySelLine_ID                    
                                tc_Rowid
                                tc_ParentRowid                    
                                tc_status                                           
                            to tDPaySelLine.
                                                                       
                            assign               
                                tDPaySelLine.DPaySel_ID         = viTargetDPaySelID                                
                                tDPaySelLine.tdInvoiceRate      = tDIncSelLine.tdInvoiceRate
                                tDPaySelLine.tdInvoiceRateScale = tDIncSelLine.tdInvoiceRateScale.
                                                                
                        end. /* if available bDPaySelLine */      
                                                
                        /* Need to flag the old stage on the source as deleted */
                        assign 
                            tDPaySelLineStg.tc_Status = 'D':U
                            tDPaySelLineStg.DPaySelLineStgStatus = tDIncSelLineStage.tcIncSelLineStageStatus.
                        
                                                        
                        /* Create new stage line (tc_status = ā€˜Nā€™) on target pay sel line */                                                           
                        <M-91 run AddDetailLine
                           (input  'DPaySelLineStg':U (icTable), 
                            input  tDPaySelLine.tc_Rowid (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>                                                        

                        if viFcReturnSuper < 0 or oiReturnStatus  = 0
                            then assign oiReturnStatus = viFcReturnSuper.    
                        if oiReturnStatus < 0
                            then return.  
                                                                                                        
                        assign 
                            tDPaySelLineStg.DPaySelLine_ID            = tDPaySelLine.DPaySelLine_ID
                            tDPaySelLineStg.DInvoiceStage_ID          = tDIncSelLineStage.tiDInvoiceStageId
                            tDPaySelLineStg.DPaySelLineStgPaymAmtTC   = tDIncSelLineStage.tdStagePaymentAmountTC
                            tDPaySelLineStg.DPaySelLineStgDiscAmtTC   = tDIncSelLineStage.tdStageDiscountAmountTC
                            tDPaySelLineStg.DPaySelLineStgOrigAmtTC   = tDIncSelLineStage.tdOriginalStageAmountTC
                            tDPaySelLineStg.DPaySelLineStgOpenAmtTC   = tDIncSelLineStage.tdOpenStageAmountTC
                            tDPaySelLineStg.DPaySelLineStgStatus      = tDIncSelLineStage.tcIncSelLineStageStatus
                            tDPaySelLineStg.DPaySelLineStgIntRate     = tDIncSelLineStage.tdIncSelLineStageIntRate.                           
                    end. /* else do */                                         
 
 
                    <M-41 run AddDetailLine
                       (input  'DPaySelPayRef':U (icTable), 
                        input  tDPaySelLineStg.tc_ParentRowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
                        
                    if viFcReturnSuper < 0 or oiReturnStatus  = 0
                        then assign oiReturnStatus = viFcReturnSuper.    
                    if oiReturnStatus < 0
                        then return. 
                        
                        
                    assign
                        tDPaySelPayRef.DPaySelLine_ID       = tDPaySelLineStg.DPaySelLine_ID
                        tDPaySelPayRef.DPaySelLineStg_ID    = tDPaySelLineStg.DPaySelLineStg_ID
                        tDPaySelPayRef.DPaySelPayRefBankRef = tDPaySelPayRefCopy.DPaySelPayRefBankRef
                        tDPaySelPayRef.DPaySelPayRefCode    = tDPaySelPayRefCopy.DPaySelPayRefCode.                      
                                                                                                                                
                assign viTargetDPaySelLineID =  tDPaySelLineStg.DPaySelLine_ID.
            end. /* for each tDIncSelLineStage */      
            
            /* check to see if all the stages have been moved from a staged line */
            find first tDPaySelLine where tDPaySelLine.DPaySel_ID = iiDPaySel_ID and tDPaySelLine.DPaySelLine_ID = viSourceDPaySelLineID.
            if available tDPaySelLine
            then do:
                find first bDPaySelLineStg where
                        bDPaySelLineStg.tc_Status           <> "D":U                  and
                        bDPaySelLineStg.DPaySelLine_ID      =  viSourceDPaySelLineID and
                        bDPaySelLineStg.DPaySelLineStg_ID   <>  tDPaySelLineStg.DPaySelLineStg_ID 
                        no-error.
                                                                        
                if available bDPaySelLineStg then
                    assign tDPaySelLine.tc_Status = "C":U.
                else                     
                    assign tDPaySelLine.tc_Status = "D":U.                                        
                end. /* if available bDPaySelLine */                 
            end. /* if available */
                    
            /* calculate the line totals */    
            <M-2 run CalculateStagedLineAmounts
               (input  viTargetDPaySelLineID (iiDPaySelLineID), 
                input  tDPaySel.tcBankCurrencyCode (icBankCurrencyCode), 
                input  tDIncSelLine.tdBankRate (idBankExchangeRate), 
                input  tDIncSelLine.tdBankRateScale (idBankExchangeRateScale), 
                output vdTotalNewLineBC (odTotalLineAmountBC), 
                output viFcReturnSuper (oiReturnStatus)) in BDPaymentSelection>
            if tDIncSelLine.tcIncSelLineAmountCrDt = {&CREDITDEBITABBREVIATION-DEBIT}
            then do:
                assign vdtotalSumDR = vdtotalSumDR + vdTotalNewLineBC.
            end.
            else do:
                assign vdtotalSumCR = vdtotalSumCR + vdTotalNewLineBC.
            end.                
        end. /* else do */        
    end. /* for each tDIncSelLine */
    assign tDPaySel.tdDPaySelTotalAmountBC = vdtotalSumCR - vdtotalSumDR.
end. /* if (can-find */