| Quintiq file version 2.0 | 
| #parent: #root | 
| StaticMethod CreateOrUpdateForStockingPointInPeriod ( | 
|   StockingPointInPeriod stockingpointinperiod, | 
|   Real maxcapacity, | 
|   Boolean isthisperiodonwards | 
| ) | 
| { | 
|   Description: "Create or update the stocking point capacity when we want only the particular stocking point in period user's value ( get from stocking point capacity ) change." | 
|   TextBody: | 
|   [* | 
|     // soh yee Feb-20-2013 (modified) | 
|     // This method will be used to change the max capacity based on product capacity for stocking point in period too. | 
|      | 
|     stockingpointcapacity := stockingpointinperiod.StockingPointCapacity(); | 
|     stockingpoint := stockingpointinperiod.StockingPoint_MP(); | 
|      | 
|     /* | 
|     Case1: all stocking point in period are referring to Jan, max capacity 100. | 
|     stocking point in period     sp capacity | 
|       Jan                        Jan, 100 | 
|       Feb | 
|       Mar | 
|       Apr | 
|      | 
|     Case2: when user edit max capacity of the stocking point in period via context menu in spip list on Mar (max capacity = 200), (a) and (b) are created. | 
|     stocking point in period     sp capacity | 
|       Jan                        Jan, 100 | 
|       Feb                        Mar, 200 (a) | 
|       Mar                        Apr, 100 (b) | 
|       Apr | 
|      | 
|      | 
|     Case3: when user edit max capacity for Mar via context menu in spip list again (now set max capacity = 300), (a) is updated. | 
|     stocking point in period     sp capacity | 
|       Jan                        Jan, 100 | 
|       Feb                        Mar, 300 (a) | 
|       Mar                        Apr, 100 (b) | 
|       Apr | 
|      | 
|      | 
|     Special case: When the scenario have parallel periods, for example Weeks and Days. | 
|     stocking point in period     sp capacity | 
|     Week 1 (isbase)              Week 1 | 
|     Week 2 (isbase)              Week 2 | 
|     Week 3 (not base)            Day 1, Day 2, Day 3, Day 5, Day 6, Day 7 | 
|     Week 4 (not base)            Day 1, Day 2, Day 3, Day 5, Day 6, Day 7 | 
|     */ | 
|      | 
|     nextspip := stockingpointinperiod.Next(); | 
|     nextstockingpointcapacity := guard( nextspip.StockingPointCapacity(), null( StockingPointCapacity ) ); | 
|      | 
|     // Get all period dimension children's stocking point capacity, for handling special case and when isthisperiodonwads is true | 
|     spcapacitys := selectset( stockingpoint, StockingPointCapacity, sc, | 
|                               sc.Start() >= stockingpointinperiod.Start().Date() | 
|                               and ( sc.Start() < stockingpointinperiod.Period_MP().EndDate() // Ignore the end if isthisperiodonwards | 
|                                     or isthisperiodonwards ) ); | 
|     // To handle special case | 
|     if( spcapacitys.Size() > 0 | 
|         and not isnull( nextspip ) ) | 
|     { | 
|       lastchildsc := null( StockingPointCapacity ); | 
|        | 
|       // If have next sc, we want to shift the last sc from the childsc as next period sc | 
|       if( not isthisperiodonwards ) | 
|       { | 
|         // Shift the last sc of the child period to next period | 
|         lastchildsc := maxselect( stockingpoint, StockingPointCapacity, sc, stockingpointinperiod.End().Date() >= sc.Start(), sc.Start() ); | 
|        | 
|         if( StockingPointCapacity::IsPrimaryKeysUnique( stockingpoint, nextspip.Start().Date(), null( StockingPointCapacity ) ) ) | 
|         { | 
|           // Keep all value except the start, to handle period > day where it have uc start in the middle of the period | 
|           // We will shift the last sc to next period | 
|           lastchildsc.Update( lastchildsc.StockingPoint_MP(), | 
|                               nextspip.Start().Date(), | 
|                               lastchildsc.MaxCapacity(), | 
|                               false ); // it is an operation from UI | 
|         } | 
|       } | 
|      | 
|       // Delete all sc except the shifted one (also for handle isthisperiodonwads for week 3, 4 Day 1~7 for the given example) | 
|       traverse( spcapacitys, Elements, e, | 
|                 e <> stockingpointcapacity | 
|                 and e <> lastchildsc ) | 
|       { | 
|         e.Delete(); | 
|       } | 
|      | 
|       // Propogate the relation to unit period quantity. | 
|       // This is needed for the following checking. | 
|       Transaction::Transaction().Propagate(); | 
|     } | 
|      | 
|     if( not isthisperiodonwards | 
|         and not isnull( nextspip ) | 
|         and nextstockingpointcapacity = stockingpointcapacity | 
|         and StockingPointCapacity::IsPrimaryKeysUnique( stockingpoint, nextspip.Start().Date(), null( StockingPointCapacity ) ) ) | 
|     { | 
|       // Case2: creating (b) | 
|       StockingPointCapacity::Create( stockingpoint, | 
|                                      nextspip.Start().Date(), | 
|                                      guard( stockingpointcapacity.MaxCapacity(), nextspip.MaxCapacity() ), | 
|                                      false ); // it is an manual operation from UI | 
|      | 
|      | 
|       // Propogate the relation to stocking point in periods. | 
|       // This is needed for the following checking. | 
|       Transaction::Transaction().Propagate(); | 
|     } | 
|      | 
|     // Create a new stocking point capacity for that particular stocking point in period if it does not bind to any stocking point capacity, or if it already has a stocking point capacity, | 
|     // the related stocking point capacity doesn't have the same start as the stocking point in period. | 
|     if( not stockingpointinperiod.HasStockingPointCapacity() | 
|         or StockingPointCapacity::IsPrimaryKeysUnique( stockingpoint, stockingpointinperiod.Start().Date(), null( StockingPointCapacity ) ) ) | 
|     { | 
|       // Case2: creating (a) | 
|       StockingPointCapacity::Create( stockingpoint, | 
|                                      stockingpointinperiod.Start().Date(), | 
|                                      maxcapacity, | 
|                                      false ); // operation from UI is Manual | 
|     } | 
|      | 
|     else | 
|     { | 
|       // Case3, (a) is updated. | 
|       // The to-be created stocking point capacity has the same primary keys with existing data. Therefore, we update it instead of creating a new data to avoid duplication. | 
|       stockingpointcapacity.Update( stockingpoint, | 
|                                     stockingpointcapacity.Start(), | 
|                                     maxcapacity, | 
|                                     false ); // operation from UI is Manual  | 
|     } | 
|   *] | 
| } |