Quintiq file version 2.0
#parent: #root
Method GenerateDemands () id:Method_FormDemandSupply_GenerateDemands
{
  #keys: '[132448.0.712669067]'
  Body:
  [*
    // Generate all demand for visualization                         
    // Clear dataholder
    DataHolderGP.Data( null( shadow[GlobalParameters_MP], owning ) );
    
    // Ownership on GP so it is possible to display the DemandForVisualization
    owner := shadow( MacroPlan.GlobalParameters_MP() );
    
    DataHolderGP.Data( &owner );
    
    traverse( DataHolderDemandPISPIP.Data(), Elements, pispip )
    {
      pispName := pispip.ProductInStockingPoint_MP().Name();
      planningPISPIP := pispip;
      type := DemandForVisualization::GetPlanningDemandString();
      factor := 1.0;
      
      if( pispip.istype( ProductInStockingPointInPeriodNonPlanningBase ) )
      {
        planningPISPIP := pispip.astype( ProductInStockingPointInPeriodNonPlanningBase ).PlanningPISPIP();
        type := DemandForVisualization::GetDisaggregatedDemandString(); 
        factor := pispip.GetPlanningSystemRatio();
      } 
      
      if( not isnull( planningPISPIP )
          and planningPISPIP.istype( ProductInStockingPointInPeriodPlanningLeaf ) )  // If its original demand or disaggregated demand
      {                         
        traverse( planningPISPIP.astype( ProductInStockingPointInPeriodPlanningLeaf ).GetDemand_MPs(), Elements, demand )
        {
          quantity := demand.Quantity() * factor; // If its disaagregated 
                              
          Form.CreateDemandForVisualization( pispName, 
                                             type,
                                             quantity, 
                                             pispip.Start(),
                                             demand );   
        }
      }
      else
      { 
        if( pispip.istype( ProductInStockingPointInPeriodNonPlanningBase )
            and not isnull( pispip.astype( ProductInStockingPointInPeriodNonPlanningBase ).PlanningPISPIP() ) )
        {
          type := DemandForVisualization::GetDisaggregatedDemandString();
        }
        else
        {
          type := DemandForVisualization::GetAggregatedDemandString();
        }
        
        Form.CreateDemandForVisualization( pispName,
                                           type,
                                           pispip.DependentDemandAndSalesDemandQuantity(), // Pispip already have the demand information
                                           pispip.Start(),
                                           null( Demand_MP ) );
      }
    }
  *]
}
