| Quintiq file version 2.0 | 
| #parent: #root | 
| Function CalcShelfLifeSupplyAgeAndQuantityVectorAsBinaryValue | 
| { | 
|   TextBody: | 
|   [* | 
|     // Construct the leading vectors | 
|     quantityvector := RealVector::Construct(); | 
|     agevector := RealVector::Construct(); | 
|     prev_quantityvectorend := RealVector::Construct( ); | 
|     prev_agevectorend := RealVector::Construct(); | 
|     pispippl := this.ProductInStockingPointInPeriodPlanningLeaf(); | 
|     prev_inventorylevelend :=0.0; | 
|              | 
|     // Check if the product has shelf life or maturation | 
|     if( pispippl.ProductInStockingPoint_MP().Product_MP().HasShelfLifeOrMaturation() ) | 
|     { | 
|         prev_pispippl := guard( pispippl.PreviousPlanningPISPIP().astype( ProductInStockingPointInPeriodPlanningLeaf ), null( ProductInStockingPointInPeriodPlanningLeaf ) ); | 
|         prev_pispippl_shelflife := guard( prev_pispippl.ShelfLife(), null( ShelfLife ) ); | 
|          | 
|          | 
|         if ( not isnull( prev_pispippl_shelflife ) ) | 
|         { | 
|          // Get shelf life end vectors from previous planning PISPIP | 
|           prev_quantityvectorend := RealVector::Construct( prev_pispippl_shelflife.ShelfLifeEndQuantityVectorAsBinaryValue() ); | 
|           prev_agevectorend := RealVector::Construct( prev_pispippl_shelflife.ShelfLifeEndAgeVectorAsBinaryValue() ); | 
|                | 
|           // Append the vectors into the leading vector | 
|           quantityvector.Append( prev_quantityvectorend ); | 
|           agevector.Append( prev_agevectorend ); | 
|           prev_inventorylevelend := prev_quantityvectorend.Sum(); | 
|         | 
|         } | 
|      | 
|         // if has total avaialable supply user | 
|         if ( this.ProductInStockingPointInPeriodPlanningLeaf().HasTotalAvailableSupplyUser() ) | 
|         { | 
|          | 
|           agefromcurrentpisp := ShelfLife::GetShelfLifeDaysToAdd( pispippl.Period_MP(), pispippl.Period_MP() ) | 
|           balancewithoutsupply := pispippl.TotalAvailableSupplyUser() - pispippl.InventorySupplyAndNewSupplyQuantity(); | 
|           // totalavailablesupplyuser - inventorysupplyandnewsupplyquantity - previous inventory level end | 
|           balancediffwithprevious := pispippl.TotalAvailableSupplyUser() - pispippl.InventorySupplyAndNewSupplyQuantity() - prev_inventorylevelend; | 
|           if ( balancediffwithprevious > 0 ) | 
|           { | 
|            quantityvector.Append( balancediffwithprevious ); | 
|            agevector.Append( agefromcurrentpisp ); | 
|           }  | 
|           else if ( this.ProductInStockingPointInPeriodPlanningLeaf().TotalAvailableSupplyUser() < prev_inventorylevelend )  | 
|           { | 
|             quantityvector.Remove( 0 );// when less than previous inventory end, removed previous, only append current totalavaialableuser | 
|             agevector.Remove( 0 );// when less than previous, removed previous | 
|             if( balancewithoutsupply > 0 ) | 
|             { | 
|               quantityvector.Append( balancewithoutsupply ); | 
|               agevector.Append( agefromcurrentpisp ); | 
|             }     | 
|           } | 
|         } | 
|         | 
|         // Traverse all new supplies and get their shelf life supply vectors | 
|         traverse( pispippl, NewSupply, newsupply ) | 
|         { | 
|           ns_quantityvector := RealVector::Construct( newsupply.ShelfLifeQuantityVectorAsBinaryValue() ); | 
|           ns_agevector := RealVector::Construct( newsupply.ShelfLifeAgeVectorAsBinaryValue() ); | 
|            | 
|           quantityvector.Append( ns_quantityvector ); | 
|           agevector.Append( ns_agevector ); | 
|         } | 
|          | 
|         // Traverse all inventory supplies and get their quantity and age | 
|         traverse( pispippl, InventorySupply, invsupply, invsupply.Quantity() > 0 ) | 
|         { | 
|           quantityvector.Append( invsupply.Quantity() ); | 
|           agevector.Append( invsupply.Age() ); | 
|         } | 
|        | 
|         // Merge expired (expired in previous period, still need the info of expired stock in current period for constraint and sanity check) | 
|         // shelf life supplies into a single element in the vector to reduce the size of the vector | 
|         ShelfLife::MergeVectorAccordingToAgeAndExpiration( quantityvector, agevector, | 
|                                                            pispippl, | 
|                                                            pispippl.Start() ); | 
|     } | 
|      | 
|     this.ShelfLifeSupplyQuantityVectorAsBinaryValue( quantityvector.AsBinaryValue() ); | 
|     this.ShelfLifeSupplyAgeVectorAsBinaryValue( agevector.AsBinaryValue() ); | 
|   *] | 
| } |