| Quintiq file version 2.0 | 
| #parent: #root | 
| Method GetServiceLevel () declarative remote as ServiceLevelBase | 
| { | 
|   Description: 'Selects and returns  the ServiceLevel object' | 
|   TextBody: | 
|   [* | 
|     // edz1 Jul-11-2016 (created) | 
|      | 
|     servicelevel := null( ServiceLevelBase ); | 
|      | 
|     setservicelevels := selectset( this,  | 
|                                     ProductInStockingPoint_MP.ServiceLevels,  | 
|                                     sl,  | 
|                                     sl.IsUsedForSafetyStockCalculation()  | 
|                                     and sl.IsEnabled() | 
|                                     and Period_MP::IsInPeriod( this.Start(), this.End(), sl.Start().DateTime(), sl.End().DateTime() ) ); | 
|      | 
|     //When no service level is defined on PISP with the matching date, look for service level where the prod / sp is specified. | 
|     if( setservicelevels.Size() = 0 )                                                                                     | 
|     { | 
|       setservicelevels := selectset( this,  | 
|                                      ProductInStockingPoint_MP.Product_MP.MacroPlan.SafetyStockCalculationServiceLevel,  | 
|                                      sl,  | 
|                                      sl.IsUsedForSafetyStockCalculation()  | 
|                                      and sl.IsEnabled(),  | 
|                                      ( isnull( sl.Product_MP() ) or sl.Product_MP() = this.ProductInStockingPoint_MP().Product_MP() ) | 
|                                      and ( isnull( sl.StockingPoint_MP() ) or sl.StockingPoint_MP() = this.StockingPointInPeriod().StockingPoint_MP() )  | 
|                                      and Period_MP::IsInPeriod( this.Start(), this.End(), sl.Start().DateTime(), sl.End().DateTime() ) ); | 
|     } | 
|     if( setservicelevels.Size() > 0 ) | 
|     { | 
|       servicelevel := maxselect ( setservicelevels, Elements, sl, true,  sl.TargetPercentage() ) | 
|     } | 
|     // If no service levels have been found, then check whether a service level has been defined for a parent of this product | 
|     // Martijn: I cannot make it recursive because it is declarative | 
|     // So I have to repeat the method but then while checking if the product of the service level is a parent of this product | 
|     else | 
|     { | 
|       // This will only select the service levels if it is on the direct parent (not a parent of the parent) | 
|       // The next if loop will select the service levels that are on higher level parents | 
|       setservicelevels := selectset( this,  | 
|                                      ParentOfProductDimension.ProductInStockingPoint_MP.ServiceLevels,  | 
|                                      sl,  | 
|                                      sl.IsUsedForSafetyStockCalculation()  | 
|                                      and sl.IsEnabled() | 
|                                      and Period_MP::IsInPeriod( this.Start(), this.End(), sl.Start().DateTime(), sl.End().DateTime() ) ); | 
|      | 
|       //When no service level is defined on PISP with the matching date, look for service level where the prod / sp is specified. | 
|       if( setservicelevels.Size() = 0 )                                                                                     | 
|       { | 
|         setservicelevels := selectset( this,  | 
|                                        ProductInStockingPoint_MP.Product_MP.MacroPlan.SafetyStockCalculationServiceLevel,  | 
|                                        sl,  | 
|                                        sl.IsUsedForSafetyStockCalculation()  | 
|                                        and sl.IsEnabled(),  | 
|                                        ( isnull( sl.Product_MP() ) or sl.Product_MP().IsParentOf( this.ProductInStockingPoint_MP().Product_MP() ) ) | 
|                                        and ( isnull( sl.StockingPoint_MP() ) or sl.StockingPoint_MP() = this.StockingPointInPeriod().StockingPoint_MP() )  | 
|                                        and Period_MP::IsInPeriod( this.Start(), this.End(), sl.Start().DateTime(), sl.End().DateTime() ) ); | 
|       } | 
|       if( setservicelevels.Size() > 0 ) | 
|       { | 
|         servicelevel := maxselect ( setservicelevels, Elements, sl, true,  sl.TargetPercentage() ) | 
|       } | 
|     } | 
|      | 
|     return servicelevel; | 
|   *] | 
| } |