Kevin Kok Khah Whey
2023-10-18 d5e46a7a9f2cb9123b9aafb39a20e14059faa2e4
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
Quintiq file version 2.0
#parent: #root
Method CapacityAndSaleBudgeCompare (
  MacroPlans macroPlans,
  CapacityAndSaleBudgeFilterItems items,
  CapacityAndSaleBudgeFilterYears years,
  CapacityAndSaleBudgeFilterMonths months,
  const GlobalOTDTable otdTable
)
{
  Description: 'todo : mappingParent改为mpSync, random删了'
  TextBody:
  [*
    // yypsybs Sep-18-2023 (created)
    // ====不选时默认全选====
    if( years.Size() = 0 ) {
      years := selectsortedset( this, CapacityAndSaleBudgeFilterYear, item, item.YearNo() );
    }
    if( months.Size() = 0 ) {
      months := selectsortedset( this, CapacityAndSaleBudgeFilterMonth, item, item.MonthNo() );
    }
    traverse( macroPlans, Elements, item ) {
      debuginfo( "scenario : " + item.ScenarioName() );
    }
    traverse( items, Elements, item ) {
      debuginfo( "item : " + item.ItemName() );
    }
    traverse( years, Elements, item ) {
      debuginfo( "year : " + [String]item.YearNo() );
    }
    traverse( months, Elements, item ) {
      debuginfo( "month : " + [String]item.MonthNo() );
    }
    // ====清理旧数据====
    this.CapacityAndSaleBudgeCompareItemRow( relflush );
    this.CapacityAndSaleBudgeCompareItemColumn( relflush );
    // ====汇总所有出现的产品====
    historyData := selectset( otdTable, Global_MappingAnnualBudgetData, item, true );
    debuginfo( "historyData : " + [String]historyData.Size() );
    
    traverse( historyData, Elements, one ) {
      productCode := one.ProductID();
    //  Global_MappingOperationBOM::CreateTestData( otdTable, "事业部" + [String](productCode.Length() mod 3), "面板基地" + [String](productCode.Length() mod 4), productCode );
    //  boms := selectset( otdTable, Global_MappingOperationBOM, bom, bom.ProductCode() = productCode );
    //  if( boms.Size() > 0 ) {
    //    bom := boms.First();
    //    businessType := bom.BusinessType();
    //    placeOfProductionOfArray := bom.OrganCode();
        // 相同面板基地和事业部的放一行
        row := CapacityAndSaleBudgeCompareItemRow::CreateIfNotExist( this, one.BusinessType(), one.OrgCodeFromBom() );
        // 记录每行包含哪些product
        CapacityAndSaleBudgeCompareItemRowProduct::CreateIfNotExist( row, productCode );
    //  } else {
    //    debuginfo( "no boms for product : " + productCode );
    //  }
    }
    debuginfo( "CapacityAndSaleBudgeCompareItemRow : " + [String]selectset( this, CapacityAndSaleBudgeCompareItemRow, item, true ).Size() );
    // ====从左向右生成列头与内容====
    // 事业部,面板基地
    debuginfo( "dealing businessType and placeOfProductionOfArray" )
    columnBusinessType := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "事业部" );
    columnPlaceOfProductionOfArray := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "面板基地" );
    traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
      cell := columnBusinessType.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := row.BusinessType() );
      cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
      cell := columnPlaceOfProductionOfArray.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := row.PlaceOfProductionOfArray() );
      cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
    }
    // 面板分配量 - 年度预算 (sheets)
    // 格式: 面板分配量 - 年 - 月(1-12 & total) - 年度预算/scenarioName
    debuginfo( "dealing 面板分配量" )
    if( CapacityAndSaleBudgeFilterItem::Contains( items, "面板分配量" ) ) {
      traverse( years, Elements, year ) {
        debuginfo( "面板分配量" + [String]year.YearNo() + "年" );
        // 历史数据
        debuginfo( "面板分配量" + [String]year.YearNo() + "年历史数据" );
        traverse( months, Elements, month ) {
          columnMonth := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "面板分配量-" + [String]year.YearNo() + "年-" + [String]month.MonthNo() + "月-年度预算" );
          traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
            cellReal := Global_MappingForecast::GetQuantityByMonth( row.GetProductCodes(), otdTable, DateTime::Now().Year(), month.MonthNo() );
            cell := columnMonth.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
            cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
          }
        }
        columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "面板分配量-" + [String]year.YearNo() + "年-total-年度预算" );
        traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
          cellReal := Global_MappingForecast::GetQuantityByYear( row.GetProductCodes(), otdTable, year.YearNo() );
          cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
          cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
        }
        debuginfo( "面板分配量" + [String]year.YearNo() + "年S&OP数据" );
        // S&OP数据
        traverse( macroPlans, Elements, macroPlan ) {
          traverse( months, Elements, month ) {
            columnMonth := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, 
                                                                                    "面板分配量-" + [String]year.YearNo() + "年-" + [String]month.MonthNo() + "月-" + macroPlan.ScenarioName() );
            traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
              cellReal := Product_MP::GetNewSupplyByMonth( row.GetProductCodes(), macroPlan, year.YearNo(), month.MonthNo() );
              cell := columnMonth.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
              cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
            }
          }
          columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, 
                                                                                 "面板分配量-" + [String]year.YearNo() + "年-total-" + macroPlan.ScenarioName() );
          traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
            cellReal := Product_MP::GetNewSupplyByYear( row.GetProductCodes(), macroPlan, year.YearNo() );
            cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
            cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
          }
        }
      }
    }
    // 年度销售额
    debuginfo( "dealing 销售额" )
    if( CapacityAndSaleBudgeFilterItem::Contains( items, "销售额" ) ) {
      traverse( years, Elements, year ) {
        debuginfo( "销售额" + [String]year.YearNo() + "年" );
        // 历史数据
        debuginfo( "销售额" + [String]year.YearNo() + "年历史数据" );
        traverse( months, Elements, month ) {
          columnMonth := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "对外销售额-" + [String]year.YearNo() + "年-" + [String]month.MonthNo() + "月-年度预算" );
          traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
            cellReal := Global_MappingAnnualBudgetData::GetSaleByMonth( row.GetProductCodes(), otdTable, year.YearNo(), month.MonthNo() );
            cell := columnMonth.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
            cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
          }
        }
        columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "对外销售额-" + [String]year.YearNo() + "年-年度预算" );
        traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
          cellReal := Global_MappingAnnualBudgetData::GetSaleByYear( row.GetProductCodes(), otdTable, year.YearNo() );
          cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
          cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
        }
        debuginfo( "销售额" + [String]year.YearNo() + "年历史数据S&OP数据" );
        // S&OP数据
        traverse( macroPlans, Elements, macroPlan ) {
          traverse( months, Elements, month ) {
            columnMonth := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "对外销售额-" + [String]year.YearNo() + "年-" + [String]month.MonthNo() + "月-" + macroPlan.ScenarioName() );
            traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
              cellReal := Forecast::GetFulfilledBalanceByMonth( row.GetProductCodes(), macroPlan, year.YearNo(), month.MonthNo() );
              cell := columnMonth.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
              cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
            }
          }
          columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "对外销售额-" + [String]year.YearNo() + "年-total-" + macroPlan.ScenarioName() );
          traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
            // QID 23
            cellReal := Forecast::GetFulfilledBalanceByYear( row.GetProductCodes(), macroPlan, year.YearNo() );
            cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]cellReal );
            cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
          }
        }
      }
    }
    // 大张盈利额
    debuginfo( "dealing 大张盈利额" )
    traverse( years, Elements, year ) {
      // 历史数据
      columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "大张盈利额-" + [String]year.YearNo() + "年-年度预算" );
      traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
        // todo 
        // QID 23
        cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]Number::Random( 0, 0 ) );
        cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
      }
      // S&OP数据
      traverse( macroPlans, Elements, macroPlan ) {
        columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "大张盈利额-" + [String]year.YearNo() + "年-" + macroPlan.ScenarioName() );
        traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
          cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]Number::Random( 0, 0 ) );
          cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
        }
      }
    }
    // 产品盈利额
    debuginfo( "dealing 产品盈利额" )
    traverse( years, Elements, year ) {
      // 历史数据
      columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "产品盈利额-" + [String]year.YearNo() + "年-年度预算" );
      traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
        // todo
        cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]Number::Random( 0, 0 ) );
        cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
      }
      // S&OP数据
      traverse( macroPlans, Elements, macroPlan ) {
        columnYear := CapacityAndSaleBudgeCompareItemColumn::CreateIfNotExist( this, "产品盈利额-" + [String]year.YearNo() + "年-" + macroPlan.ScenarioName() );
        traverse( this, CapacityAndSaleBudgeCompareItemRow, row ) {
          // todo
          cell := columnYear.CapacityAndSaleBudgeCompareItemCell( relnew, CellContent := [String]Number::Random( 0, 0 ) );
          cell.CapacityAndSaleBudgeCompareItemRow( relset, row );
        }
      }
    }
    
    // 设置rowNo和columnNo
    debuginfo( "set rowNo and columnNo" );
    rowNo := 1;
    rowSorted := selectsortedset( this, CapacityAndSaleBudgeCompareItemRow, item, item.PlaceOfProductionOfArray() + "_" + item.BusinessType() );
    traverse( rowSorted, Elements, item ) {
      item.RowNo( rowNo );
      rowNo := rowNo + 1;
    }
    columnNo := 1;
    traverse( this, CapacityAndSaleBudgeCompareItemColumn, item ) {
      item.ColumnNo( columnNo );
      columnNo := columnNo + 1;
    }
    debuginfo( "done" );
  *]
}