FUNCTION "FC_Jam" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_OUTPUT 
      stOutCarrierStatsStandard : "CarrierStatisticsStandard";
      stOutCarrierStatsAdv : "CarrierStatisticsStorage";
   END_VAR

   VAR_IN_OUT 
      arInOutJamCarrier : Array[*] of "stCarrier";
      stInOutData : "stJamData";
   END_VAR

   VAR_TEMP 
      nIdx : Int;
      nArrayMax : Int;
   END_VAR

   VAR CONSTANT 
      cForecast : USInt := 110;
      cRetrieveSeqSeat : USInt := 1;
      cRetrieveSeqRest : USInt := 2;
      cRetrieveExpress : USInt := 3;
      cDummy : USInt := 1;
   END_VAR


BEGIN
	//Prüft Einstellwert
	IF (#stInOutData.Config.nCapacitySet > #stInOutData.Config.nCapacityMax) OR (#stInOutData.Config.nCapacitySet <= 0) THEN
	    #stInOutData.Config.nCapacitySet := #stInOutData.Config.nCapacityMax;
	END_IF;
	
	//Init
	#stOutCarrierStatsStandard.nEmptyCarrier := 0;
	#stOutCarrierStatsStandard.nLoadedCarrier := 0;
	#stOutCarrierStatsStandard.nDummyCarrier := 0;
	
	#stOutCarrierStatsAdv.nDest111 := 0;
	#stOutCarrierStatsAdv.nDest112 := 0;
	#stOutCarrierStatsAdv.nDest113 := 0;
	#stOutCarrierStatsAdv.nDest114 := 0;
	#stOutCarrierStatsAdv.nDest121 := 0;
	#stOutCarrierStatsAdv.nDest122 := 0;
	#stOutCarrierStatsAdv.nDest123 := 0;
	#stOutCarrierStatsAdv.nDest124 := 0;
	#stOutCarrierStatsAdv.nDest131 := 0;
	#stOutCarrierStatsAdv.nDest132 := 0;
	#stOutCarrierStatsAdv.nDest133 := 0;
	#stOutCarrierStatsAdv.nDest134 := 0;
	
	//Ermittelt den Status/Füllstand des Staubereichs
	#nArrayMax := DINT_TO_INT(UPPER_BOUND(ARR := #arInOutJamCarrier, DIM := 1));
	
	IF NOT #arInOutJamCarrier[1].bStatus.%X0 THEN //Wenn 1. Teil leer, dann muss Schleife nicht ausgeführt werden
	    #stInOutData.Status.nCount := 0;
	    #stInOutData.Status.bByte.%X0 := TRUE;
	    
	    //Jam ist leer
	    #stOutCarrierStatsStandard.nEmptyCarrier := 0;
	    #stOutCarrierStatsStandard.nLoadedCarrier := 0;
	    #stOutCarrierStatsStandard.nDummyCarrier := 0;
	    
	    #stOutCarrierStatsAdv.nDest111 := 0;
	    #stOutCarrierStatsAdv.nDest112 := 0;
	    #stOutCarrierStatsAdv.nDest113 := 0;
	    #stOutCarrierStatsAdv.nDest114 := 0;
	    #stOutCarrierStatsAdv.nDest121 := 0;
	    #stOutCarrierStatsAdv.nDest122 := 0;
	    #stOutCarrierStatsAdv.nDest123 := 0;
	    #stOutCarrierStatsAdv.nDest124 := 0;
	    #stOutCarrierStatsAdv.nDest131 := 0;
	    #stOutCarrierStatsAdv.nDest132 := 0;
	    #stOutCarrierStatsAdv.nDest133 := 0;
	    #stOutCarrierStatsAdv.nDest134 := 0;
	    #stOutCarrierStatsAdv.nDestAMRLoaded := 0;
	    #stOutCarrierStatsAdv.nDestF2HLoaded := 0;
	    #stOutCarrierStatsAdv.nDestF2HEmpty := 0;
	    #stOutCarrierStatsAdv.nDestInboundEmpty := 0;
	    #stOutCarrierStatsAdv.nDest11x := 0;
	    #stOutCarrierStatsAdv.nDest12x := 0;
	    #stOutCarrierStatsAdv.nDest13x := 0;
	    
	    
	    //****Für Statistics muss dies auskommentiert werden
	    // ELSIF #arInOutJamCarrier[#nArrayMax].xOnline THEN //Wenn letztes Teil belegt, dann muss Schleife nicht ausgeführt werden
	    //     #stInOutData.Status.nCount := #nArrayMax;
	    //     #stInOutData.Status.xEmpty := FALSE;
	    //*****
	    
	ELSE
	    FOR #nIdx := 1 TO #nArrayMax DO
	        //Statistics - nur wenn etwas vorhanden, ansonsten nicht machen
	        IF #arInOutJamCarrier[#nIdx].nBarcode <> 0 THEN
	            //Statistics Standard
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND NOT #arInOutJamCarrier[#nIdx].bStatus.%X1 AND #arInOutJamCarrier[#nIdx].nBarcode <> #cDummy THEN
	                #stOutCarrierStatsStandard.nEmptyCarrier += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].bStatus.%X1 AND #arInOutJamCarrier[#nIdx].nBarcode <> #cDummy THEN
	                #stOutCarrierStatsStandard.nLoadedCarrier += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nBarcode = #cDummy THEN
	                #stOutCarrierStatsStandard.nDummyCarrier += 1;
	            END_IF;
	            
	            //  Statistics Advanced (Bereichsbezogen)  
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest11100" THEN
	                #stOutCarrierStatsAdv.nDest111 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest11200" THEN
	                #stOutCarrierStatsAdv.nDest112 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest11300" THEN
	                #stOutCarrierStatsAdv.nDest113 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest11400" THEN
	                #stOutCarrierStatsAdv.nDest114 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest12100" THEN
	                #stOutCarrierStatsAdv.nDest121 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest12200" THEN
	                #stOutCarrierStatsAdv.nDest122 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest12300" THEN
	                #stOutCarrierStatsAdv.nDest123 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest12400" THEN
	                #stOutCarrierStatsAdv.nDest124 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest13100" THEN
	                #stOutCarrierStatsAdv.nDest131 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest13200" THEN
	                #stOutCarrierStatsAdv.nDest132 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest13300" THEN
	                #stOutCarrierStatsAdv.nDest133 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDest13400" THEN
	                #stOutCarrierStatsAdv.nDest134 += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestAMRLoaded" THEN
	                #stOutCarrierStatsAdv.nDestAMRLoaded += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestF2HLoaded" THEN
	                #stOutCarrierStatsAdv.nDestF2HLoaded += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestF2HEmpty" THEN
	                #stOutCarrierStatsAdv.nDestF2HEmpty += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestInboundEmpty" THEN
	                #stOutCarrierStatsAdv.nDestInboundEmpty += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B1" THEN
	                #stOutCarrierStatsAdv.nDest11x += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B2" THEN
	                #stOutCarrierStatsAdv.nDest12x += 1;
	            END_IF;
	            
	            IF #arInOutJamCarrier[#nIdx].bStatus.%X0 AND #arInOutJamCarrier[#nIdx].nDestination = "cDestLvl1B3" THEN
	                #stOutCarrierStatsAdv.nDest13x += 1;
	            END_IF;
	            
	        END_IF;
	        
	        
	        IF #arInOutJamCarrier[#nIdx].nBarcode = 0 THEN //Zählt nicht die Teile sondern merkt sich die Positionen wo ein Teil vorhanden ist (minus 1)
	            #stInOutData.Status.nCount := INT_TO_UINT(#nIdx - 1);
	            #stInOutData.Status.bByte.%X0 := FALSE;
	            EXIT;
	            //****Für Statistics würde dies benötigt werden
	        ELSIF #nIdx = #nArrayMax THEN
	            #stInOutData.Status.nCount := INT_TO_UINT(#nArrayMax);
	            #stInOutData.Status.bByte.%X0 := FALSE;
	            EXIT;
	            //******
	        END_IF;
	    END_FOR;
	END_IF;
	
END_FUNCTION

