lazhen
2025-01-09 8afe90b633046db39042aada36b88193062f8cff
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
53
54
55
56
57
Quintiq file version 2.0
#parent: #root
Method Execute_DataMigration_NonFinancialKPI (
  String oldname,
  String newname
)
{
  Description: 'Generic data migration method for NonFinancialKPI'
  TextBody:
  [*
    oldkpi := KPI::FindKPITypeIndex( oldname );
    newkpi := KPI::FindKPITypeIndex( newname );
    newkpitable := NonFinancialKPITable::Table();
    newkpitablerow := select( newkpitable, Rows, row, row.Name() = newname );
    needsnewkpi := isnull( newkpi ) and not isnull( newkpitablerow );
    hasdatamigration := needsnewkpi and not isnull( oldkpi );
    
    // can't use ChangeKeyTypeIndex as KPI has subclasses, so create new, delete old
    if( needsnewkpi )
    {
      newkpi := NonFinancialKPI::Create( this,
                                         newname,
                                         newkpitablerow.IsShown(),
                                         newkpitablerow.IsPercentage(),
                                         hasdatamigration /* if migration, prevents creation of kpiweight,etc as we will re-assign from old kpi to this new KPI as owner */
                                       );
    
    
      if( hasdatamigration )
      { 
        oldkpi.IsMigratingData( true ); // Prevents cleanup as we want to re-assign kpiweight,etc to new KPI (owner)
        
        traverse( oldkpi, KPIValue, kpivalue )
        {
          newkpi.KPIValue( relinsert, kpivalue ); 
        }
        
        traverse( oldkpi, KPIWeight, kpiweight )
        {
          KPIWeight::ChangeKeyKPIWeightTypeIndex( kpiweight, newname, kpiweight.StrategyName() );
        }
        
        kpisetting := oldkpi.KPISetting();
        if( not( isnull( kpisetting ) ) )
        {
          KPISetting::ChangeKeyKPISettingTypeIndex( kpisetting, newname ); 
          newkpi.KPISetting( relmove, kpisetting );
        }  
        
        newkpi.IsMigratingData( false ); // Reset migration status once we done owning those objects from old KPI
        
        oldkpi.Delete();
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}