admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Quintiq file version 2.0
#parent: #root
StaticMethod GetConversionFactor (
  const UnitOfMeasure_MP sourceuom,
  const UnitOfMeasure_MP targetuom,
  const Product_MP product
) const declarative remote as Real
{
  Description: 'Return the conversion factor object from source to target'
  TextBody:
  [*
    //This method returns NAN if conversion factor is not found. 
    //Therefore, it should only be used to check whether the conversion factor exists or not.
    //Should not be used for retrieving the factor value. For that we should always use uom.GetConversionFactor()
    
    factor := log( -1.0 );
    
    if( not isnull( sourceuom ) )
    {
      // If the product has conversion factor, traverse from product
      conversionfactor := constnull( ConversionFactor );
      
      if( not isnull( product )
          and product.HasConversionFactor() )
      {
        conversionfactor := select( product, ConversionFactor, cf,
                                    cf.SourceUnitOfMeasure() = sourceuom,
                                    cf.TargetUnitOfMeasure() = targetuom );
      }
      // Else go for source -> target without product specified
      // Do not use else because some product may have HasConversionFactor = true but the specific conversion factor is not specified.
      // In that case, we will use the conversion factor without product
      
      if( isnull( conversionfactor ) )
      {
        conversionfactor := select( sourceuom, AsSourceUnitOfMeasureWithNullProduct, cf, true, cf.TargetUnitOfMeasure() = targetuom );
      }
      factor := guard( conversionfactor.Factor(), log( -1.0 ) )
      
      // Chain conversion factor because in web, we may only have conversion factors between default uom and each uom
      if( isnull( conversionfactor ) )
      {
        defaultUOM := sourceuom.MacroPlan().DefaultUnitOfMeasure();
        converttodefaultfactor := select( sourceuom, AsSourceUnitOfMeasureWithNullProduct, cf, true, cf.TargetUnitOfMeasure() = defaultUOM );
        defaulttotargetfactor := select( defaultUOM, AsSourceUnitOfMeasureWithNullProduct, cf, true, cf.TargetUnitOfMeasure() = targetuom );
        factor := guard( converttodefaultfactor.Factor() * defaulttotargetfactor.Factor(), log( -1.0 ) );
      }
    }
    
    return factor;
  *]
}