陈清红
2025-04-14 880f3c0257eeb8c37761d484258fdd102a369a19
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
Quintiq file version 2.0
#parent: #root
StaticMethod ComparedNew (
  RecycleBin recycleBin,
  const NewOfflinePlanTable baseOfflinePlan,
  const NewOfflinePlanTable compareOfflinePlan
) as LocalTable
{
  TextBody:
  [*
    // rislai Jun-20-2024 (created)
    table := recycleBin.LocalTable( relnew,Name := LocalCell_OfflinePlanComparison::GetTableName());
    
    localColumns := construct( LocalColumns );
    localColumnIndexTree := NamedValueTree::Create();
    traverse( baseOfflinePlan,NewOfflinePlanColumn,column,column.StartDate() >=  baseOfflinePlan.MacroPlan().StartOfPlanning().Date()){
      localColumnHandle := localColumnIndexTree.GetHandle( column.StartDate().AsQUILL() );
      localColumnIndex := guard( localColumnIndexTree.Root().Child( localColumnHandle ),null( NamedValue ));
      if( isnull( localColumnIndex )){
        localColumn := table.LocalColumn( relnew,CustomDate := column.StartDate(),TimeUnit := "Day");
        localColumns.Add( localColumn );
        localColumnIndexTree.Root().AddChild( localColumnHandle,localColumns.Size() - 1 );
      }
    }
    traverse( compareOfflinePlan,NewOfflinePlanColumn,column,column.StartDate() >=  compareOfflinePlan.MacroPlan().StartOfPlanning().Date()){
      localColumnHandle := localColumnIndexTree.GetHandle( column.StartDate().AsQUILL() );
      localColumnIndex := guard( localColumnIndexTree.Root().Child( localColumnHandle ),null( NamedValue ));
      if( isnull( localColumnIndex )){
        localColumn := table.LocalColumn( relnew,CustomDate := column.StartDate(),TimeUnit := "Day");
        localColumns.Add( localColumn );
        localColumnIndexTree.Root().AddChild( localColumnHandle,localColumns.Size() - 1 );
      }
    }
    
    baseOfflinePlanCells := construct( NewOfflinePlanCells, constcontent );
    baseOfflinePlanCellIndexTree := NamedValueTree::Create();
    compareOfflinePlanCells := construct( NewOfflinePlanCells, constcontent );
    compareOfflinePlanCellIndexTree := NamedValueTree::Create();
    
    localRows := construct( LocalRows );
    localRowIndexTree := NamedValueTree::Create();
    
    traverse( baseOfflinePlan,NewOfflinePlanRow,row,row.Type() = "1" ){
      rowKey := row.ProductID() + "@$#" + row.ProductionLine()+ "@$#";
      localRowHandle := localRowIndexTree.GetHandle( rowKey );
      localRowIndex := guard( localRowIndexTree.Root().Child( localRowHandle ),null( NamedValue ));
      if( isnull( localRowIndex )){
        localRow := table.LocalRow( relnew,Index := table.GetRowIndexCache(),ProductID := row.ProductID(),ProductionLine := row.ProductionLine(),CustomName := row.ProductID() + " > " + row.ProductionLine(),Remark := rowKey );
        localRows.Add( localRow );
        localRowIndexTree.Root().AddChild( localRowHandle,localRows.Size() - 1 );
      }
      traverse( row,NewOfflinePlanCell,cell ){
        columnKey := cell.NewOfflinePlanColumn().StartDate().AsQUILL();
        cellKey := rowKey + columnKey;
        cellHandle := baseOfflinePlanCellIndexTree.GetHandle( cellKey );
        baseOfflinePlanCells.Add( cell );
        baseOfflinePlanCellIndexTree.Root().AddChild( cellHandle,baseOfflinePlanCells.Size() - 1 );
      }
    }
    traverse( compareOfflinePlan,NewOfflinePlanRow,row,row.Type() = "1" ){
      rowKey := row.ProductID() + "@$#" + row.ProductionLine()+ "@$#";
      localRowHandle := localRowIndexTree.GetHandle( rowKey );
      localRowIndex := guard( localRowIndexTree.Root().Child( localRowHandle ),null( NamedValue ));
      if( isnull( localRowIndex )){
        localRow := table.LocalRow( relnew,Index := table.GetRowIndexCache(),ProductID := row.ProductID(),ProductionLine := row.ProductionLine(),CustomName := row.ProductID() + " > " + row.ProductionLine(),Remark := rowKey );
        localRows.Add( localRow );
        localRowIndexTree.Root().AddChild( localRowHandle,localRows.Size() - 1 );
      }
      traverse( row,NewOfflinePlanCell,cell ){
        columnKey := cell.NewOfflinePlanColumn().StartDate().AsQUILL();
        cellHandle := compareOfflinePlanCellIndexTree.GetHandle( rowKey + columnKey );
        compareOfflinePlanCells.Add( cell );
        compareOfflinePlanCellIndexTree.Root().AddChild( cellHandle,compareOfflinePlanCells.Size() - 1 );
      }
    }
    
    traverse( localRows,Elements,localRow ){
      info( localRow.ProductionLine() );
      
      rowKey := localRow.Remark();
      traverse( localColumns,Elements,localColumn ){
        columnKey := localColumn.CustomDate().AsQUILL();
        cellKey := rowKey + columnKey;
        
        baseOfflinePlanCell := constnull( NewOfflinePlanCell );
        compareOfflinePlanCell := constnull( NewOfflinePlanCell );
        baseOfflinePlanCellIndex := guard( baseOfflinePlanCellIndexTree.Root().Child( baseOfflinePlanCellIndexTree.GetHandle( cellKey ) ),null( NamedValue ));
        compareOfflinePlanCellIndex := guard( compareOfflinePlanCellIndexTree.Root().Child( compareOfflinePlanCellIndexTree.GetHandle( cellKey ) ),null( NamedValue ));
        if( not isnull( baseOfflinePlanCellIndex )){
          baseOfflinePlanCell := baseOfflinePlanCells.Element( baseOfflinePlanCellIndex.GetValueAsNumber() ); 
        }
        if( not isnull( compareOfflinePlanCellIndex )){
          compareOfflinePlanCell := compareOfflinePlanCells.Element( compareOfflinePlanCellIndex.GetValueAsNumber() ); 
        }
        
        localRow.LocalCell( relnew,LocalCell_OfflinePlanComparison,LocalColumn := localColumn,
                            BaseVersion := ifexpr( isnull( baseOfflinePlanCell),0.0,baseOfflinePlanCell.Quantity()),
                            CompareVersion := ifexpr( isnull( compareOfflinePlanCell),0.0,compareOfflinePlanCell.Quantity()));
      }
    }
    
    startDate := minselect( table,LocalColumn,localColumn,localColumn.CustomDate() ).CustomDate();
    endDate := maxselect( table,LocalColumn,localColumn,localColumn.CustomDate() ).CustomDate();
    for( i := startDate.StartOfWeek(); i <= endDate.StartOfNextWeek(); i := i.StartOfNextWeek() ){
      timeUnit := "Week";
      localColumnHandle := localColumnIndexTree.GetHandle( i.AsQUILL() + timeUnit);
      localColumn := table.LocalColumn( relnew,CustomDate := i,TimeUnit := timeUnit );
      localColumns.Add( localColumn );
      localColumnIndexTree.Root().AddChild( localColumnHandle,localColumns.Size() - 1 );
    }
    for( i := startDate.StartOfMonth(); i <= endDate.StartOfNextMonth(); i := i.StartOfNextMonth() ){
      timeUnit := "Month";
      localColumnHandle := localColumnIndexTree.GetHandle( i.AsQUILL() + timeUnit);
      localColumn := table.LocalColumn( relnew,CustomDate := i,TimeUnit := timeUnit );
      localColumns.Add( localColumn );
      localColumnIndexTree.Root().AddChild( localColumnHandle,localColumns.Size() - 1 );
    }
    
    traverse( table,LocalRow,row ){
      traverse( table,LocalColumn,column,column.TimeUnit() = "Week"){
        baseVersionValue := sum( row,LocalCell.astype( LocalCell_OfflinePlanComparison ),cell,
                                 cell.LocalColumn().TimeUnit() = "Day" and
                                 cell.LocalColumn().CustomDate() >= column.CustomDate() and
                                 cell.LocalColumn().CustomDate() < column.CustomDate().StartOfNextWeek(), cell.BaseVersion());
        compareVersionValue := sum( row,LocalCell.astype( LocalCell_OfflinePlanComparison ),cell,
                                 cell.LocalColumn().TimeUnit() = "Day" and
                                 cell.LocalColumn().CustomDate() >= column.CustomDate() and
                                 cell.LocalColumn().CustomDate() < column.CustomDate().StartOfNextWeek(), cell.CompareVersion());
                                 
        row.LocalCell( relnew,LocalCell_OfflinePlanComparison,LocalColumn := column,BaseVersion := baseVersionValue,CompareVersion := compareVersionValue );
      } 
    }
    traverse( table,LocalRow,row ){
      traverse( table,LocalColumn,column,column.TimeUnit() = "Month"){
        baseVersionValue := sum( row,LocalCell.astype( LocalCell_OfflinePlanComparison ),cell,
                                 cell.LocalColumn().TimeUnit() = "Day" and
                                 cell.LocalColumn().CustomDate() >= column.CustomDate() and
                                 cell.LocalColumn().CustomDate() < column.CustomDate().StartOfNextMonth(), cell.BaseVersion());
        compareVersionValue := sum( row,LocalCell.astype( LocalCell_OfflinePlanComparison ),cell,
                                 cell.LocalColumn().TimeUnit() = "Day" and
                                 cell.LocalColumn().CustomDate() >= column.CustomDate() and
                                 cell.LocalColumn().CustomDate() < column.CustomDate().StartOfNextMonth(), cell.CompareVersion());
                                 
        row.LocalCell( relnew,LocalCell_OfflinePlanComparison,LocalColumn := column,BaseVersion := baseVersionValue,CompareVersion := compareVersionValue );
      } 
    }
    
    traverse( table,LocalColumn,column ){
      if( column.LocalCell( relsize ) = 0 ){
        column.Delete(); 
      } 
    }
    
    return table;
  *]
}