FUNCTION "FC_TSHR_AddAtEnd" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      stInTrolley : "stCarrier";
   END_VAR

   VAR_OUTPUT 
      nOutCounter : DInt;
      xOutResult : Bool;   // 1 = Successfully; 0 = Error
   END_VAR

   VAR_IN_OUT 
      arTSHR : Array[*] of "stCarrier";
   END_VAR

   VAR_TEMP 
      nCounter : DInt;
      xMatch : Bool;
      nArrayMax : DInt;
   END_VAR


BEGIN
	#nArrayMax := UPPER_BOUND(ARR := #arTSHR, DIM := 1);
	IF #arTSHR[#nArrayMax].bStatus.%X0 = TRUE THEN
	    RETURN;
	END_IF;
	
	FOR #nCounter := #nArrayMax - 1 TO 1 BY -1 DO
	    IF #arTSHR[#nCounter].bStatus.%X0 = TRUE THEN
	        #xMatch := 1;
	        EXIT;
	    END_IF;
	END_FOR;
	
	IF #nCounter = 0 THEN
	    #xMatch := TRUE;
	END_IF;
	
	IF #xMatch
	THEN
	    #arTSHR[#nCounter + 1].nDestination := #stInTrolley.nDestination;
	    #arTSHR[#nCounter + 1].nBarcode := #stInTrolley.nBarcode;
	    #arTSHR[#nCounter + 1].bStatus := #stInTrolley.bStatus;
	END_IF;
	
	#xOutResult := #xMatch;
	#nOutCounter := #nCounter;
	
	
END_FUNCTION

