project QadFinancials > class TFRWTreeView > method GetFRWTreeLevel2NodeCaculateBalanceAndActivity


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method TFRWTreeView.GetFRWTreeLevel2Node


program code (program1/tfrwtreeview.p)

/* Now we can calculate the balance/activity for the requested period. */
assign viteller = 0.
for each tancode:
	find tnode where tnode.tinodeid = tancode.tileafnodeid no-error.
	if not available tnode then do:
		/*corruption. tnode not available with id #*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(#T-63'Corruption. tnode not available with id # '&1'.':255(81869038)T-63#, string(tancode.tileafnodeid))
           oiReturnStatus = -1.
		return.
	end. /*end if not available tnode then do */

	for each tPerrange:
		 for each tanalelemcubedim where tanalelemcubedim.tccubename =  vccubenames and
						tanalelemcubedim.tcancode = tancode.tcancode use-index analelemcubedimanindex,
			each frwcubedim where frwcubedim.frwcubedim_id =  tanalelemcubedim.ticubedimid no-lock,
			each frwcubemeas where frwcubemeas.frwcubedim_id = frwcubedim.frwcubedim_id and
							  frwcubemeasreportyear = tPerrange.tiyear and
							  frwcubemeasreportperiod >= tPerrange.tifromperiod and
							  frwcubemeasreportperiod <= tPerrange.titoperiod no-lock:

			for each tColPer where tColPer.tiPerSeq = tPerrange.tiPerrangeSeq,
			    each tCols where tCols.tiSeq = tColPer.tiColSeq:
				/* now we are sure about that this FRWCubeMeas needs to be added to some columns */
				assign vdAmountx = 0.
				if tCols.tcMeas = "ACT" then 
					assign vdAmountx = FRWCubeMeasactivityDRPC - FRWCubeMeasactivityCRPC.
				if tCols.tcMeas = "ACTDR" then 
					assign vdAmountx = FRWCubeMeasactivityDRPC.
				if tCols.tcMeas = "ACTCR" then 
					assign vdAmountx = FRWCubeMeasactivityCRPC.
				if tCols.tcMeas = "ACTQTY" then 
					assign vdAmountx = FRWCubeMeasAcitivtyQTY.
				if tCols.tcMeas = "ABC" then 
					assign vdAmountx = FRWCubeMeasactivityDRLC - FRWCubeMeasactivityCRLC.
				if tCols.tcMeas = "ABCDR" then 
					assign vdAmountx = FRWCubeMeasactivityDRLC.
				if tCols.tcMeas = "ABCCR" then 
					assign vdAmountx = FRWCubeMeasactivityCRLC.
				if tCols.tcMeas begins "OP" and
				   FRWCubeMeasReportyear = tCols.tiFromYear and
				   FRWCubeMeasReportPeriod = tCols.tiFromPeriod then do:
					if tCols.tcMeas = "OPEN" then 
						assign vdAmountx = FRWCubeMeasOpenBalPC.
					if tCols.tcMeas = "OPENDR" then 
						assign vdAmountx = FRWCubeMeasOpenBalDRPC.
					if tCols.tcMeas = "OPENCR" then 
						assign vdAmountx = FRWCubeMeasOpenBalCRPC.
					if tCols.tcMeas = "OPENQTY" then 
						assign vdAmountx = FRWCubeMeasOpenBalQTY.
					if tCols.tcMeas = "OPBC" then 
						assign vdAmountx = FRWCubeMeasOpenBalLC.
					if tCols.tcMeas = "OPBCDR" then 
						assign vdAmountx = FRWCubeMeasOpenBalDRLC.
					if tCols.tcMeas = "OPBCCR" then 
						assign vdAmountx = FRWCubeMeasOpenBalCRLC.
				end. /* if tCols.tcMeas begins "OPEN" */
				if tCols.tcMeas begins "CLO" and
				   FRWCubeMeasReportyear = tCols.tiToYear and
				   FRWCubeMeasReportPeriod = tCols.tiToPeriod then do:
					if tCols.tcMeas = "CLOSE" then 
						assign vdAmountx = FRWCubeMeasCloseBalPC.
					if tCols.tcMeas = "CLOSEDR" then 
						assign vdAmountx = FRWCubeMeasCloseBalDRPC.
					if tCols.tcMeas = "CLOSECR" then 
						assign vdAmountx = FRWCubeMeasCloseBalCRPC.
					if tCols.tcMeas = "CLOSEQTY" then 
						assign vdAmountx = FRWCubeMeasClosingBalQTY.
					if tCols.tcMeas = "CLOBC" then 
						assign vdAmountx = FRWCubeMeasCloseBalLC.
					if tCols.tcMeas = "CLOBCDR" then 
						assign vdAmountx = FRWCubeMeasCloseBalDRLC.
					if tCols.tcMeas = "CLOBCCR" then 
						assign vdAmountx = FRWCubeMeasCloseBalCRLC.
				end. /* if tCols.tcMeas begins "CLOSE" */

                if tCols.tiSeq = 1 then assign tNode.tdOpeningBalance = tNode.tdOpeningBalance + vdAmountx.
                if tCols.tiSeq = 2 then assign tNode.tdActivityDR = tNode.tdActivityDR + vdAmountx.
                if tCols.tiSeq = 3 then assign tNode.tdActivityCR = tNode.tdActivityCR + vdAmountx.
                if tCols.tiSeq = 4 then assign tNode.tdClosingBalance = tNode.tdClosingBalance + vdAmountx.   
                if vdAmountx <> 0 then assign tNode.tlHasDetails = yes.             
			end. /* for each tCols */
		 end. /* for each tqAnalElemCubedim, FRWCubeDim, FRWCubeMeas */
	  end. /* for each tPerrange */

	  do with viColNum = 1 to viColDetSeq:
            if tnode.tlisinvertactivitysign = yes then do:
               if vccoltype[viColNum] = "a" then do:
                    if viColNum = 1 then assign tnode.tdOpeningBalance = - tnode.tdOpeningBalance.
                    if viColNum = 2 then assign tnode.tdActivityDR = - tnode.tdActivityDR.
                    if viColNum = 3 then assign tnode.tdActivityCR = - tnode.tdActivityCR.
                    if viColNum = 4 then assign tnode.tdClosingBalance = - tnode.tdClosingBalance.
               end. /* end if vccoltype[viColNum] = "a" */
            end. /* end if tnode.tlisinvertactivitysign = yes then do */
            if tnode.tlisinvertbalancesign = yes then do:
               if vccoltype[viColNum] = "b" then do:
                    if viColNum = 1 then assign tnode.tdOpeningBalance = - tnode.tdOpeningBalance.
                    if viColNum = 2 then assign tnode.tdActivityDR = - tnode.tdActivityDR.
                    if viColNum = 3 then assign tnode.tdActivityCR = - tnode.tdActivityCR.
                    if viColNum = 4 then assign tnode.tdClosingBalance = - tnode.tdClosingBalance.
               end. /* end if vccoltype[viColNum] = "b" then do */
            end. /* end if tnode.tlisinvertbalancesign = yes then do */
	  end. /* end do with vicolnum = 1 to vicoldetseq */


	 /* now the calculated balance should be pushed up in the hierarchy, but stopped when a calculation node is encountered */
	 assign vicurrentparentid = tnode.tiparentnodeid
				    vimaxloop =  tnode.titreelevel - 1
					viteller  = 0
					tnode.tlisaddedtoparent = yes.
	 do while true:
		  assign viteller = viteller + 1.
		  if vicurrentparentid = 0 then leave. /* we reached the top - root node */
		  if viteller > vimaxloop then do:
			/*the tree temp-table is corrupted(2). there were more loops than levels:#. aborting*/
            create terrortable.
            assign tErrorTable.tcErrorMessage = trim(#T-53'The tree temp-table is corrupted(2). there were more loops than levels:# '&1' aborting.':255(668596211)T-53#, string(vimaxloop))
                oiReturnStatus = -1.
			return.
		  end.
		  find bnode where bnode.tinodeid = vicurrentparentid no-error.
		  if not available bnode then do:
			/*the tree temp-table is corrupted(2).parent node with id # was not found*/
            create terrortable.
            assign tErrorTable.tcErrorMessage = trim(#T-3'The tree temp-table is corrupted(2).parent node with id # '&1' was not found.':255(435010320)T-3#, string(vicurrentparentid))
                oiReturnStatus = -1.
			return.
		  end.
		  if  bnode.tcanalysiscodetype = 'c' or
		    bnode.tcanalysiscodetype = 't' then leave.
		  assign vicurrentparentid = bnode.tiparentnodeid
		  bnode.tlisaddedtoparent = yes.
		  do with viColNum = 1 to viColDetSeq:
                if viColNum = 1 then assign bnode.tdOpeningBalance = bnode.tdOpeningBalance + tnode.tdOpeningBalance.
                if viColNum = 2 then assign bnode.tdActivityDR = bnode.tdActivityDR + tnode.tdActivityDR.
                if viColNum = 3 then assign bnode.tdActivityCR = bnode.tdActivityCR + tnode.tdActivityCR.
                if viColNum = 4 then assign bnode.tdClosingBalance = bnode.tdClosingBalance + tnode.tdClosingBalance.
		  end.
	 end. /* do while true */
end. /* for each tancode */

/* now lets try to resolve all calculations */
assign viprevcalctodo = 0.
do while true:
   if viprevcalctodo = vicalctodo and
	  viprevcalctodo <> 0 then do:
		/*the tree seems to have circular calculation references (calculation a needs the result of calculation b and calculation b needs the result of calculation a).resolve this first
		{us/bbi/pxmsg.i &MSGNUM=12012 &ERRORLEVEL=1 &MSGBUFFER=tcErrorMessage}*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(#T-33'The tree seems to have circular calculation references (calculation a needs the result of calculation b and calculation b needs the result of calculation a).Resolve this first':255(126225294)T-33#)
                oiReturnStatus = -1.
	  return.
   end.
   /* run to the tree completely to mark all the nodes that have all children added */
   for each tnode where tnode.tlhasallchildrenadded = no:
	   assign vlalladded = yes.
	   for each bnode where bnode.tiparentnodeid = tnode.tinodeid:
			if bnode.tlisaddedtoparent = no then assign vlalladded = no.
	   end.
	   assign tnode.tlhasallchildrenadded =  vlalladded.
   end.
   assign viprevcalctodo = vicalctodo
		  vicalctodo = 0.
   for each tnode where tnode.tcanalysiscodetype = 'c' and
						 tnode.tlisaddedtoparent = no:
	  assign vicalctodo = vicalctodo + 1.
	  find frwancode where frwancode.frwancode_id = tnode.tiancodeid no-lock no-error.
	  if not available frwancode then do:
	  /*error in tree structure node with id #  has invalid analysis code id # . aborting program.
		{us/bbi/pxmsg.i &MSGNUM=12433 &ERRORLEVEL=1 &MSGARG1=string(tnode.tinodeid) &MSGARG2=string(tnode.qancodeid) &MSGBUFFER=tcErrorMessage}*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(substitute(#T-76'Error in tree structure. Node with ID #'&1' contains an invalid analysis code ID #'&2' .':255(990290776)T-76#, STRING(tnode.tinodeid), STRING(tnode.tiancodeid)))
                oiReturnStatus = -1.
	    return.
	  end.
	  if frwancode.frwancodeanalysetype <> "c" then do:
	   /*error in tree structure node with analysis code # is expected to be calculation, but is no 
		{us/bbi/pxmsg.i &MSGNUM=12435 &ERRORLEVEL=1 &MSGARG1=frwancode.frwancodecode &MSGBUFFER=tcErrorMessage}*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(#T-2'Error in tree structure. Node with analysis code #'&1' is expected for a calculation, but is missing.':255(78092698)T-2#, frwancode.frwancodecode)
                oiReturnStatus = -1.
		return.
	  end.
	  if frwancode.frwancodeanalysetype = "c" and
		 frwancode.frwancodecalcelement  = ""
			then do:
		/*error in analysis code definition analysis code # is expected to have a calculation but that is blank
		{us/bbi/pxmsg.i &MSGNUM=12437 &ERRORLEVEL=1 &MSGARG1=frwancode.frwancodecode &MSGBUFFER=tcErrorMessage}*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(#T-25'Error in analysis code definition. Analysis code # '&1' is expected to have a calculation but that is blank':255(926022673)T-25#, frwancode.frwancodecode)
                oiReturnStatus = -1.
		return.
	  end.
	  assign vcinputcalccode = frwancode.frwancodecalcelement
			 vigeonumberoffset = tnode.tigeonumber.

      /* ================================================================= */
      /* Create tCalcCode table                                            */
      /* ================================================================= */
      for each tcalccode:
          delete tcalccode.
      end.
      assign viteller = 0
           vlalladded = no
           vlcalcnodeready = yes. 
      if index(vcinputcalccode,",") <> 0 then do:
            repeat viteller = 1 to num-entries(vcinputcalccode):
                assign vlconstantvalue = no.
                if substring(entry(viteller,vcinputcalccode),1,1,"CHARACTER":U) = "(" then do:
                    assign vccurrentcalccode = substring(entry(viteller,vcinputcalccode),2,length(entry(viteller,vcinputcalccode),"CHARACTER":U) - 2,"CHARACTER":U).
                    assign vccurrentoperator = "-".
                    find last bnode where bnode.tcancodecode = vccurrentcalccode and
                                          bnode.tigeonumber < vigeonumberoffset use-index ancodegeonumberindex no-error.
                    if not available bnode then do:
                       find first bnode where bnode.tcancodecode = vccurrentcalccode and
                       bnode.tigeonumber > vigeonumberoffset use-index ancodegeonumberindex no-error.
                       if not available bnode then do:
                           /* maybe it is just a constant */
                           assign vdconstant = dec(vccurrentcalccode) no-error.
                           if vdconstant = 0 then do:  /* it is not a valid constant */
                    
                            /*calculation formula # contains a (negative) analysis code #
                              that could not be foundas non-text in the tree
                            {us/bbi/pxmsg.i &MSGNUM=12441 &ERRORLEVEL=1 &MSGARG1=vcinputcalccode &MSGARG2=vccurrentcalccode &MSGBUFFER=tcErrorMessage}*/
                            create terrortable.
                            assign tErrorTable.tcErrorMessage = trim(substitute(#T-77'Calculation formula #'&1' contains a (negative) analysis code #'2'.':255(751853738)T-77#, vcinputcalccode, vccurrentcalccode))
                                    oiReturnStatus = -1.
                            return.
                          end.
                          else do:
                            assign vlconstantvalue = yes.
                          end.
                       end.
                    end.
                end. /* if substring(ENTRY */
                else do:
                    assign vccurrentcalccode = entry(viteller,vcinputcalccode)
                           vccurrentoperator = "+".
                    find last bnode where bnode.tcancodecode = vccurrentcalccode and
                                          bnode.tigeonumber < vigeonumberoffset use-index ancodegeonumberindex no-error.
                    if not available bnode then do:
                       find first bnode where bnode.tcancodecode = vccurrentcalccode and
                                              bnode.tigeonumber > vigeonumberoffset use-index ancodegeonumberindex no-error.
                    
                        if not available bnode then do:
                           /* maybe it is just a constant */
                           assign vdconstant = dec(vccurrentcalccode) no-error.
                           if vdconstant = 0 then do:  /* it was not a valid constant */
                              /*calculation formula # contains an positive analysis code #  that could not be foundas non-text in the tree
                            {us/bbi/pxmsg.i &MSGNUM=12443 &ERRORLEVEL=1 &MSGARG1=vcinputcalccode &MSGARG2=vccurrentcalccode &MSGBUFFER=tcErrorMessage}*/
                            create terrortable.
                            assign tErrorTable.tcErrorMessage = trim(substitute(#T-15'Calculation formula #'&1' contains an positive analysis code #'&2' that could not be foundas non-text in the tree':255(309054271)T-15#,
                                        vcinputcalccode, vccurrentcalccode))
                                    oiReturnStatus = -1.
                            return.
                           end.
                           else do:
                             assign vlconstantvalue = yes.
                           end.
                       end.
                    end.
                end. /*end else do */
                if vlconstantvalue = no then do:
                    assign vicurrentparentid = bnode.tinodeid.
                    /* now check if all the children have been added to this calculation component */
                    assign vlalladded = yes.
                    if bnode.tcanalysiscodetype = 'c' then do:
                        if bnode.tlisaddedtoparent = no then assign vlalladded = no.
                    end.
                    else do:
                        for each bnode where bnode.tiparentnodeid = vicurrentparentid:
                            if bnode.tlisaddedtoparent = no then assign vlalladded = no.
                        end.
                    end.
                    if vlalladded = yes then do:
                        create tcalccode.
                        assign tcalccode.tccalccode = vccurrentcalccode
                        tcalccode.tccalcoperator = vccurrentoperator
                        tcalccode.ticalcnodeid = vicurrentparentid.
                    end.
                    else do:
                        assign vlcalcnodeready = no.
                    end.
                end.
                else do:
                    create tcalccode.
                    assign tcalccode.tccalccode = vccurrentcalccode
                       tcalccode.tccalcoperator = vccurrentoperator
                       tcalccode.ticalcnodeid = 0
                       tcalccode.tdconstant = vdconstant.
                end.
            end. /* REPEAT viteller = 1 TO NUM-ENTRIES(vcinputcalccode)*/
        end. /* if index(vcinputcalccode,",") <> 0 */
        else do:
            if index(vcinputcalccode,"*") <> 0 and
              index(vcinputcalccode,"/") <> 0 then do:
                   if index(vcinputcalccode,"*") <  index(vcinputcalccode,"/") then assign vifirstoperatorpos = index(vcinputcalccode,"*")
                                      visecondoperatorpos = index(vcinputcalccode,"/")
                                      vcfirstoperator = "*"
                                      vcsecondoperator = "/".
                   else assign vifirstoperatorpos = index(vcinputcalccode,"/")
                                      visecondoperatorpos = index(vcinputcalccode,"*")
                                      vcfirstoperator = "/"
                                      vcsecondoperator = "*".
            end.
            else do:
               if index(vcinputcalccode,"*") <> 0  then assign vifirstoperatorpos = index(vcinputcalccode,"*")
                                                                                   visecondoperatorpos = 0
                                                                                   vcfirstoperator = "*"
                                                                                   vcsecondoperator = "".
               if index(vcinputcalccode,"/") <> 0  then assign vifirstoperatorpos = index(vcinputcalccode,"/")
                                                                                   visecondoperatorpos = 0
                                                                                   vcfirstoperator = "/"
                                                                                   vcsecondoperator = "".
               if index(vcinputcalccode,"*") = 0 and
                  index(vcinputcalccode,"/") = 0  then assign vifirstoperatorpos = 0
                                                                                  visecondoperatorpos = 0
                                                                                   vcfirstoperator = ""
                                                                                   vcsecondoperator = "".
            end.
            assign vcsavedinputcalccode = vcinputcalccode.
            assign vcinputcalccode = replace(vcinputcalccode, "*", ",").
            assign vcinputcalccode = replace(vcinputcalccode, "/", ",").
            if num-entries(vcinputcalccode) > 3 then do:
                  /*Calculation with non supported formula found: #
        			{us/bbi/pxmsg.i &MSGNUM=12447 &ERRORLEVEL=1 &MSGARG1=vcsavedinputcalccode &MSGBUFFER=tcErrorMessage}*/
                create terrortable.
                assign tErrorTable.tcErrorMessage = trim(substitute(#T-55'Caculation with non supported formula found: # '&1'.':255(545666745)T-55#, vcsavedinputcalccode))
                        oiReturnStatus = -1.
                return.
            end.
            repeat viteller = 1 to num-entries(vcinputcalccode):
                assign vlconstantvalue = no.
                if substring(entry(viteller,vcinputcalccode),1,1,"CHARACTER":U) = "(" then do:
                    assign vccurrentcalccode = substring(entry(viteller,vcinputcalccode),2,length(entry(viteller,vcinputcalccode),"CHARACTER":U) - 2,"CHARACTER":U).
                    if viteller = 1 then assign vccurrentoperator = "-".
                    if viteller = 2 then assign vccurrentoperator = trim(vcfirstoperator) + "-".
                    if viteller = 3 then assign vccurrentoperator = trim(vcsecondoperator) + "-".
                    find last bnode where bnode.tcancodecode = vccurrentcalccode and
                                      bnode.tigeonumber < vigeonumberoffset use-index ancodegeonumberindex no-error.
                    if not available bnode then do:
                        find first bnode where bnode.tcancodecode = vccurrentcalccode and
                                          bnode.tigeonumber > vigeonumberoffset use-index ancodegeonumberindex no-error.
                        if not available bnode then do:
                            /* maybe it is just a constant */
                            assign vdconstant = dec(vccurrentcalccode) no-error.
                            if vdconstant = 0 then do:  /* it was not a valid constant */
                                /*calculation formula # contains a (negative) analysis code #
                                that could not be found in the tree
                                {us/bbi/pxmsg.i &MSGNUM=12441 &ERRORLEVEL=1 &MSGARG1=vcsavedinputcalccode &MSGARG2=vccurrentcalccode &MSGBUFFER=tcErrorMessage}*/
                                create terrortable.
                                assign tErrorTable.tcErrorMessage = trim(substitute(#T-30'Calculation formula #'&1' contains a (negative) analysis code #'2'.':255(751853738)T-30#, vcinputcalccode, vccurrentcalccode))
                                    oiReturnStatus = -1.
                                return.
                            end.
                            else do:
                                vlconstantvalue = yes.
                            end. /*end else do */
                        end. /*if not available bnode then do:*/
                    end. /* if not available bnode then do:*/
                end. /* if substring */
                else do:
                    assign vccurrentcalccode = entry(viteller,vcinputcalccode).
                    if viteller = 1 then assign vccurrentoperator = "+".
                    if viteller = 2 then assign vccurrentoperator = trim(vcfirstoperator) .
                    if viteller = 3 then assign vccurrentoperator = trim(vcsecondoperator).
                    find last bnode where bnode.tcancodecode = vccurrentcalccode and
                                      bnode.tigeonumber < vigeonumberoffset use-index ancodegeonumberindex no-error.
                    if not available bnode then do:
                        find first bnode where bnode.tcancodecode = vccurrentcalccode and
                                          bnode.tigeonumber > vigeonumberoffset use-index ancodegeonumberindex no-error.
                        if not available bnode then do:
                            /* maybe it is just a constant */
                            assign vdconstant = dec(vccurrentcalccode) no-error.
                            if vdconstant = 0 then do:  /* it was not a valid constant */
                                /*calculation formula # contains an positive analysis code # that could not be found in the tree 
                                {us/bbi/pxmsg.i &MSGNUM=12443 &ERRORLEVEL=1 &MSGARG1=vcsavedinputcalccode &MSGARG2=vccurrentcalccode &MSGBUFFER=tcErrorMessage}*/
                                create terrortable.
                                assign tErrorTable.tcErrorMessage = trim(substitute(#T-46'Calculation formula #'&1' contains an positive analysis code #'&2' that could not be foundas non-text in the tree':255(309054271)T-46#,
                                        vcinputcalccode, vccurrentcalccode))
                                    oiReturnStatus = -1.
                                return.
                            end.
                            else do:
                                vlconstantvalue = yes.
                            end. /* else do */
                        end. /* if not available bnode then do*/
                    end. /* if not available bnode then do: */
                end. /* else do */
                if vlconstantvalue = no then do:
                    assign vicurrentparentid = bnode.tinodeid.
                    /* now check if all the children have been added to this calculation component */
                    assign vlalladded = yes.
                    if bnode.tcanalysiscodetype = 'c' then do:
                        if bnode.tlisaddedtoparent = no then assign vlalladded = no.
                    end.
                    else do:
                        for each bnode where bnode.tiparentnodeid = vicurrentparentid:
                            if bnode.tlisaddedtoparent = no then assign vlalladded = no.
                        end.
                    end.
                    if vlalladded = yes then do:
                        create tcalccode.
                        assign tcalccode.tccalccode = vccurrentcalccode
                           tcalccode.tccalcoperator = vccurrentoperator
                           tcalccode.ticalcnodeid = vicurrentparentid.
                    end.
                    else do:
                        assign vlcalcnodeready = no.
                    end.
                end. /*if vlconstantvalue = no */
                else do:
                    create tcalccode.
                    assign tcalccode.tccalccode = vccurrentcalccode
                       tcalccode.tccalcoperator = vccurrentoperator
                       tcalccode.ticalcnodeid = 0
                       tcalccode.tdconstant = vdconstant.
                end. /* else do */
            end. /* REPEAT viteller = 1 TO NUM-ENTRIES(vcinputcalccode)*/
        end. /* else do */
      /* End create tCalcCode table */
         if vlcalcnodeready = no then next.
	  if not can-find(first tcalccode) then do:
		  /*error in analysis code definition analysis code #  has invalid analysis codes or invalid operators
		{us/bbi/pxmsg.i &MSGNUM=12016 &ERRORLEVEL=1 &MSGARG1=frwancode.frwancodecode &MSGBUFFER=tcErrorMessage}*/
        create terrortable.
        assign tErrorTable.tcErrorMessage = trim(substitute(#T-88'error in analysis code definition analysis code # '&1' is expected to have a calculation but that is blank':255(393136343)T-88#, frwancode.frwancodecode))
                oiReturnStatus = -1.
		return.
	  end.
	  /* perform calculation */
	  do with viColNum = 1 to viColDetSeq:
			assign vdAmount[viColNum] = 0.
	  end.

	  for each tcalccode:
			if tcalccode.ticalcnodeid <> 0 then do:
				   find bnode where bnode.tinodeid = tcalccode.ticalcnodeid no-error.
				   if not available bnode then do:
				   /*program error.  bnode with id # not found
					{us/bbi/pxmsg.i &MSGNUM=12440 &ERRORLEVEL=1 &MSGARG1=string(tcalccode.ticalcnodeid) &MSGBUFFER=tcErrorMessage}*/
                    create terrortable.
                    assign tErrorTable.tcErrorMessage = trim(substitute(#T-8'Program error.  bnode with id #'&1' not found':255(386154302)T-8#, string(tcalccode.ticalcnodeid)))
                        oiReturnStatus = -1.
					return.
				   end.
				   if tcalccode.tccalcoperator = "+" then do:
						do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 then assign vdAmount[viColNum] = vdAmount[viColNum] + bNode.tdOpeningBalance.
                            if viColNum = 2 then assign vdAmount[viColNum] = vdAmount[viColNum] + bNode.tdActivityDR.
                            if viColNum = 3 then assign vdAmount[viColNum] = vdAmount[viColNum] + bNode.tdActivityCR.
                            if viColNum = 4 then assign vdAmount[viColNum] = vdAmount[viColNum] + bNode.tdClosingBalance.
						end.
				   end.
				   if tcalccode.tccalcoperator = "-" then do:
						do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 then assign vdAmount[viColNum] = vdAmount[viColNum] - bNode.tdOpeningBalance.
                            if viColNum = 2 then assign vdAmount[viColNum] = vdAmount[viColNum] - bNode.tdActivityDR.
                            if viColNum = 3 then assign vdAmount[viColNum] = vdAmount[viColNum] - bNode.tdActivityCR.
                            if viColNum = 4 then assign vdAmount[viColNum] = vdAmount[viColNum] - bNode.tdClosingBalance.
						end.
				   end.
				   if tcalccode.tccalcoperator = "*" then do:
						do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 then assign vdAmount[viColNum] = vdAmount[viColNum] * bNode.tdOpeningBalance.
                            if viColNum = 2 then assign vdAmount[viColNum] = vdAmount[viColNum] * bNode.tdActivityDR.
                            if viColNum = 3 then assign vdAmount[viColNum] = vdAmount[viColNum] * bNode.tdActivityCR.
                            if viColNum = 4 then assign vdAmount[viColNum] = vdAmount[viColNum] * bNode.tdClosingBalance.
						end.
				   end.
				   if tcalccode.tccalcoperator = "/" then do:
					    do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 and bNode.tdOpeningBalance <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / bNode.tdOpeningBalance.
                            if viColNum = 2 and bNode.tdActivityDR <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / bNode.tdActivityDR.
                            if viColNum = 3 and bNode.tdActivityCR <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / bNode.tdActivityCR.
                            if viColNum = 4 and bNode.tdClosingBalance <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / bNode.tdClosingBalance.
					    end.
				   end.
				   if tcalccode.tccalcoperator = "*-" then do:
						do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 then assign vdAmount[viColNum] = vdAmount[viColNum] * (- bNode.tdOpeningBalance).
                            if viColNum = 2 then assign vdAmount[viColNum] = vdAmount[viColNum] * (- bNode.tdActivityDR).
                            if viColNum = 3 then assign vdAmount[viColNum] = vdAmount[viColNum] * (- bNode.tdActivityCR).
                            if viColNum = 4 then assign vdAmount[viColNum] = vdAmount[viColNum] * (- bNode.tdClosingBalance).
						end.
				   end.
				   if tcalccode.tccalcoperator = "/-" then do:
					    do with viColNum = 1 to viColDetSeq:
                            if viColNum = 1 and bNode.tdOpeningBalance <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / (- bNode.tdOpeningBalance).
                            if viColNum = 2 and bNode.tdActivityDR <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / (- bNode.tdActivityDR).
                            if viColNum = 3 and bNode.tdActivityCR <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / (- bNode.tdActivityCR).
                            if viColNum = 4 and bNode.tdClosingBalance <> 0 then assign vdAmount[viColNum] = vdAmount[viColNum] / (- bNode.tdClosingBalance).
					    end.
				   end.
		  end. /* tcalccode.ticalcnodeid <> 0 */
		  else do:
               if tcalccode.tdconstant <> 0 then do:
                  if tcalccode.tccalcoperator = "+" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] + tcalccode.tdconstant.
                        end.
                  end.
                  if tcalccode.tccalcoperator = "-" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] - tcalccode.tdconstant.
                        end.
                  end.
                  if tcalccode.tccalcoperator = "*" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] * tcalccode.tdconstant.
                        end.
                  end.
                  if tcalccode.tccalcoperator = "/" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] / tcalccode.tdconstant.
                        end.
                  end.
                  if tcalccode.tccalcoperator = "*-" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] * (- tcalccode.tdconstant).
                        end.
                  end.
                  if tcalccode.tccalcoperator = "/-" then do:
                        do with viColNum = 1 to viColDetSeq:
                            assign vdAmount[viColNum] = vdAmount[viColNum] / (- tcalccode.tdconstant).
                        end.
                  end.
		  end. /* if tcalccode.tdconstant <> 0 */
		  end.
	 end. /* for each tcalccode: */
    do with viColNum = 1 to viColDetSeq:
        if vicolnum = 1 then assign tNode.tdOpeningBalance = round(vdAmount[viColNum],viNumOfDecimals).
        if vicolnum = 2 then assign tNode.tdActivityDR = round(vdAmount[viColNum],viNumOfDecimals).
        if vicolnum = 3 then assign tNode.tdActivityCR = round(vdAmount[viColNum],viNumOfDecimals).
        if vicolnum = 4 then assign tNode.tdClosingBalance = round(vdAmount[viColNum],viNumOfDecimals).
    end.
    /* end calculation */
    /* push the balance up */
	 assign vicurrentparentid = tnode.tiparentnodeid
						  vimaxloop =  tnode.titreelevel - 1
						  viteller = 0
						  tnode.tlisaddedtoparent = yes.
	 do while true:
		  assign viteller = viteller + 1.
		  if vicurrentparentid = 0 then leave. /* we reached the top - root node */
		  if viteller > vimaxloop then do:
		   /* the tree temp-table is corrupted(2). there were more loops than levels:#. aborting
				{us/bbi/pxmsg.i &MSGNUM=12420 &ERRORLEVEL=1 &MSGARG1=string(timaxloop) &MSGBUFFER=tcErrorMessage}*/
                create terrortable.
                assign tErrorTable.tcErrorMessage = trim(substitute(#T-54'The tree temp-table is corrupted(2). there were more loops than levels:# '&1' aborting.':255(668596211)T-54#, string(vimaxloop)))
                        oiReturnStatus = -1.
			    return.
		  end.
		  find bnode where bnode.tinodeid = vicurrentparentid no-error.
		  if not available bnode then do:
			/*the tree temp-table is corrupted(2). parent node with id # was not found.
				{us/bbi/pxmsg.i &MSGNUM=12421 &ERRORLEVEL=1 &MSGARG1=string(vicurrentparentid) &MSGBUFFER=tcErrorMessage}*/
                create terrortable.
                assign tErrorTable.tcErrorMessage = trim(substitute(#T-68'The tree temp-table is corrupted(2).parent node with id # '&1' was not found.':255(435010320)T-68#, string(vicurrentparentid)))
                        oiReturnStatus = -1.
			   return.
		  end.
		  if  bnode.tcanalysiscodetype = 'c' then leave.
		  assign vicurrentparentid = bnode.tiparentnodeid
		   bnode.tlisaddedtoparent = yes.
			do with viColNum = 1 to viColDetSeq:
                if vicolnum = 1 then bNode.tdOpeningBalance = bnode.tdOpeningBalance + tnode.tdOpeningBalance.
                if vicolnum = 2 then bNode.tdActivityDR = bnode.tdActivityDR + tnode.tdActivityDR.
                if vicolnum = 3 then bNode.tdActivityCR = bnode.tdActivityCR + tnode.tdActivityCR.
                if vicolnum = 4 then bNode.tdClosingBalance = bnode.tdClosingBalance + tnode.tdClosingBalance.
			end.
	 end. /* do while true */
	end.  /* for each tnode type calc */ 
	if vicalctodo = 0 then leave.  /* all calculations are done */ 
end. /* do while true for checking all calculation nodes */