Quintiq file version 2.0
#parent: #root
Method DrawNode (
  internal[DrawNodeInfo] drawinfo,
  UnitPeriod element
) id:Method_FormCapacityPlanning_DrawNode
{
  #keys: '[145730.1.1156292293]'
  Body:
  [*
    // Determine colors for gantt chart nodes
    nr := MacroPlan.GlobalParameters_MP().NumberOfDecimals();
    below := element.BottleneckThreshold();
    above := element.OverloadThreshold();
    
    changeoverratio := Duration::Zero();
    if( element.istype( UnitPeriodTimeBase ) )
    {
      changeoverratio := element.astype( UnitPeriodTimeBase ).ChangeoverDurationRatio();
    }
    if( element.Duration() > Duration::Zero() )
    {
      // Quantity aggregated unit without child, show as irrelevant
      if( element.Unit().CapacityType() = GlobalParameters_MP::GetCapacityTypeQuantityAggregation()
          and element.ChildOfUnitDimension( relsize ) = 0 )
      {
        color := ifexpr( element.HasConstraintViolations(), ColorScheme.GanttChart_Error(),ColorScheme.GanttChart_Irrelevant() );
        drawinfo.AddCompartment( color, element.FreeCapacityRatio() );
      }
      //infinte and no child, show purple
      else if ( not element.Unit().IsPotentialBottleneck() )
      {
        color := ifexpr( element.HasConstraintViolations(), ColorScheme.GanttChart_Error(), ColorScheme.GanttChart_Neutral1() );
        drawinfo.AddCompartment( color, element.Duration() );
      }
      //infinite but with child, show constraint violation if its children is overloaded
      else if ( element.Unit().HasCapacityTypeInfinite() and element.Unit().IsPotentialBottleneck() )
      {
        color := ifexpr( element.HasConstraintViolations(),  ColorScheme.GanttChart_Error(), ColorScheme.GanttChart_Neutral1() );
        drawinfo.AddCompartment( color, element.UsedCapacityRatio() );
        drawinfo.AddCompartment( ColorScheme.GanttChart_Free(), element.FreeCapacityRatio() );
      }
      //units which are part of an aggregation
      else if ( element.Unit().IsForCapacityAggregation() )
      {
        color := ifexpr( element.HasConstraintViolations(), ColorScheme.GanttChart_Error(), ColorScheme.GanttChart_Custom2() ); 
        drawinfo.AddCompartment( color, element.UsedCapacityRatio() );
        drawinfo.AddCompartment( ColorScheme.GanttChart_Changeover(), changeoverratio );
        drawinfo.AddCompartment( ColorScheme.GanttChart_Free(), element.FreeCapacityRatio() );
        //drawinfo.AddCompartment( ColorScheme.GanttChart_Irrelevant(), element.TotalUnavailableCapacityRatio() );
      }
      // IsPlannedInfinite, show purple
      else if ( element.IsPlannedInfinite() )
      {
        color := ifexpr( element.HasConstraintViolations(), ColorScheme.GanttChart_Error(), ColorScheme.GanttChart_Neutral1() ); 
        drawinfo.AddCompartment( color, element.Duration() );
      }
      else
      {
      
        displayutilizationpercentage := element.DisplayUtilizationPercentageWithMaxLoadRatio();
        loadpercentage := displayutilizationpercentage.Round( nr );
    
        color :=  ColorScheme.GanttChart_Good();
        if ( element.HasConstraintViolations() )
        {
          color := ColorScheme.GanttChart_Error();
        }
        else if ( below > 0 and above > 0 )
        {
          if ( loadpercentage > below and loadpercentage <= above and element.Period_MP().IsWithinBottleneckWindow() )
          {
            color := ColorScheme.GanttChart_Warning2();
          }
          else if ( loadpercentage > above )
          {
            color := ColorScheme.GanttChart_Warning1();
          }
        }
        
        usedCapacityRatio := ifexpr( element.Unit().CapacityType() = GlobalParameters_MP::GetCapacityTypeTransportQuantity()
                                    ,element.astype( UnitPeriodTransportQuantity).DisplayUsedCapacityRatio()
                                    ,element.UsedCapacityRatio() );
           
        freeCapacityRatio := ifexpr( element.Unit().CapacityType() = GlobalParameters_MP::GetCapacityTypeTransportQuantity()
                                    ,element.astype( UnitPeriodTransportQuantity).DisplayFreeCapacityRatio()
                                    ,element.FreeCapacityRatio() );
    
        drawinfo.AddCompartment( color, usedCapacityRatio );
        drawinfo.AddCompartment( ColorScheme.GanttChart_Changeover(), changeoverratio );
        drawinfo.AddCompartment( ColorScheme.GanttChart_Free(), freeCapacityRatio );
      }
    
      color := ifexpr( not element.HasOpenUnit(), ColorScheme.GanttChart_Error(), ColorScheme.GanttChart_Irrelevant() );
      drawinfo.AddCompartment( color, element.TotalUnavailableCapacityRatio() );
    }
    
    if ( drawinfo.BackgroundColor().IsDark() )
    {
      drawinfo.TextColor( Color::White() );
    }
  *]
  Declarative: true
}
