Kevin Kok Khah Whey
2023-11-07 5ae534ab606e6f2ba5ea60914224d665b0447d5a
_Main/BL/Type_GlobalOTDSOP/Method_CapacityAndSaleBudgeChart.qbl
@@ -4,299 +4,23 @@
  MacroPlans macroPlans,
  String saleOrCapacity,
  String groupBy,
  String byBusinessTypeOrByOrgCode,
  CapacityAndSaleBudgeFilterBusinessTypes businessTypes,
  CapacityAndSaleBudgeFilterPlaceOfProductionOfArrays placeOfProductionOfArrays,
  CapacityAndSaleBudgeFilterYears years,
  CapacityAndSaleBudgeFilterMonths months,
  String popoa,
  owning CapacityAndSaleBudgeFilterItems items,
  owning CapacityAndSaleBudgeFilterPlaceOfProductionOfArrays placeOfProductionOfArrays,
  owning CapacityAndSaleBudgeFilterBusinessTypes businessTypes,
  owning CapacityAndSaleBudgeFilterYears years,
  owning CapacityAndSaleBudgeFilterMonths months,
  GlobalOTDTable otdTable
)
{
  Description: '按入参处理数据,产生需要的CapacityAndSaleBudgeChartElement'
  TextBody:
  [*
    // yypsybs Sep-19-2023 (created)
    // true, false
    //info( saleOrCapacity );
    // 月;季度;半年;年
    //info( groupBy );
    // 面板基地;事业部
    //info( byBusinessTypeOrByOrgCode );
    //traverse( businessTypes, Elements, businessType ) {
    //  info( "businessType : " + businessType.BusinessType() );
    //}
    //traverse( placeOfProductionOfArrays, Elements, placeOfProductionOfArray ) {
    //  info( "placeOfProductionOfArray : " + placeOfProductionOfArray.PlaceOfProductionOfArray() );
    //}
    //traverse( macroPlans, Elements, macroPlan ) {
    //  info( "macroPlan : " + macroPlan.ScenarioName() );
    //}
    // 年份不选时全选
    if( years.Size() = 0 ) {
      years := selectsortedset( this, CapacityAndSaleBudgeFilterYear, item, true, item.YearNo() );
    }
    //traverse( years, Elements, year ) {
    //  debuginfo( "yearNo : " + [String]year.YearNo() );
    //}
    // 月份不选时全选
    if( months.Size() = 0 ) {
      months := selectsortedset( this, CapacityAndSaleBudgeFilterMonth, item, true, item.MonthNo() );
    }
    //traverse( months, Elements, month ) {
    //  debuginfo( "monthNo : " + [String]month.MonthNo() );
    //}
    // ====清理旧数据====
    this.CapacityAndSaleBudgeChartRow( relflush );
    this.CapacityAndSaleBudgeChartElement( relflush );
    
    // ====按面板基地和事业部对产品进行筛选分组====
    historyData := selectset( otdTable, Global_MappingAnnualBudgetData, item, true );
    if( businessTypes.Size() > 0 ) {
      businessTypeStrings := selectuniquevalues( businessTypes, Elements, item, true, item.BusinessType() );
      historyData := selectset( historyData, Elements, item, businessTypeStrings.Find( item.BusinessType() ) > -1 );
    }
    if( placeOfProductionOfArrays.Size() > 0 ) {
      placeOfProductionOfArrayStrings := selectuniquevalues( placeOfProductionOfArrays, Elements, item, true, item.PlaceOfProductionOfArray() );
      historyData := selectset( historyData, Elements, item, placeOfProductionOfArrayStrings.Find( item.OrgCodeFromBom() ) > -1 );
    }
    //info( "historyData : " + [String]historyData.Size() );
    traverse( historyData, Elements, item ) {
      row := CapacityAndSaleBudgeChartRow::CreateIfNotExist( this, item.BusinessType(), item.OrgCodeFromBom() );
      // 记录每行包含哪些product
      CapacityAndSaleBudgeChartRowProduct::CreateIfNotExist( row, item.ProductID() );
    }
    rows := selectset( this, CapacityAndSaleBudgeChartRow, item, true );
    //info( "rows : " + [String]rows.Size() );
    placeOfProductionOfArrayList := selectuniquevalues( historyData, Elements, item, item.OrgCodeFromBom() );
    //traverse( placeOfProductionOfArrayList, Elements, item ) {
    //  info( "placeOfProduction : " + item );
    //}
    businessTypeList := selectuniquevalues( historyData, Elements, item, item.BusinessType() );
    //traverse( businessTypeList, Elements, item ) {
    //  info( "businessType : " + item );
    //}
    // 生成图表元素
    traverse( years, Elements, year ) {
    //  debuginfo( "process year start : " + [String]year.YearNo() );
      if( byBusinessTypeOrByOrgCode = "面板基地" and groupBy = "年" ) {
        // 目标
        traverse( placeOfProductionOfArrayList, Elements, placeOfProductionOfArray ) {
          productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByPlaceOfProductionOfArray( rows, placeOfProductionOfArray );
          this.CapacityAndSaleBudgeChartElement( relnew,
                                                 TimeStringAndScenarioName := [String]year.YearNo() + "年-目标",
                                                 BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                 Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                     Global_MappingAnnualBudgetData::GetSaleByYear( productCodeList, otdTable, year.YearNo() ),
                                                                     Global_MappingForecast::GetQuantityByYear( productCodeList, otdTable, year.YearNo() ) )
                                                 );
          // S&OP
          traverse( macroPlans, Elements, macroPlan ) {
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-" + macroPlan.ScenarioName(),
                                                   BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Forecast::GetFulfilledBalanceByYear( productCodeList, macroPlan, year.YearNo() ),
                                                                       Product_MP::GetNewSupplyByYear( productCodeList, macroPlan, year.YearNo() )
                                                                       )
                                                   );
          }
        }
      }
      if ( byBusinessTypeOrByOrgCode = "面板基地" and groupBy = "半年" ) {
        for( halfNo := 1; halfNo <= 2; halfNo := halfNo + 1 ) {
          traverse( placeOfProductionOfArrayList, Elements, placeOfProductionOfArray ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByPlaceOfProductionOfArray( rows, placeOfProductionOfArray );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-" + ifexpr( halfNo = 1, "上", "下" ) + "半年-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleByHalfYear( productCodeList, otdTable, year.YearNo(), halfNo ),
                                                                       Global_MappingForecast::GetQuantityByHalfYear( productCodeList, otdTable, year.YearNo(), halfNo ) )
                                                   );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-" + ifexpr( halfNo = 1, "上", "下" ) + "半年-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceByHalfYear( productCodeList, macroPlan, year.YearNo(), halfNo ),
                                                                         Product_MP::GetNewSupplyByHalfYear( productCodeList, macroPlan, year.YearNo(), halfNo )
                                                                         )
                                                     );
            }
          }
        }
      }
      if ( byBusinessTypeOrByOrgCode = "面板基地" and groupBy = "季度" ) {
        for( seasonNo := 1; seasonNo <= 4; seasonNo := seasonNo + 1 ) {
          traverse( placeOfProductionOfArrayList, Elements, placeOfProductionOfArray ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByPlaceOfProductionOfArray( rows, placeOfProductionOfArray );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + [String]seasonNo + "季度-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleBySeason( productCodeList, otdTable, year.YearNo(), seasonNo ),
                                                                       Global_MappingForecast::GetQuantityBySeason( productCodeList, otdTable, year.YearNo(), seasonNo ) )
                                                   );
      //      info( "productCodeList : " + [String]productCodeList.Size() );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + [String]seasonNo + "季度-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceBySeason( productCodeList, macroPlan, year.YearNo(), seasonNo ) ,
                                                                         Product_MP::GetNewSupplyBySeason( productCodeList, macroPlan, year.YearNo(), seasonNo )
                                                                         )
                                                     );
            }
          }
        }
      }
      if ( byBusinessTypeOrByOrgCode = "面板基地" and groupBy = "月" ) {
        traverse( months, Elements, month ) {
    //      info( "process month start : " + [String]month.MonthNo() );
          monthNoString := ifexpr( month.MonthNo() <= 9, "0" + [String]month.MonthNo(), [String]month.MonthNo() );
          traverse( placeOfProductionOfArrayList, Elements, placeOfProductionOfArray ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByPlaceOfProductionOfArray( rows, placeOfProductionOfArray );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + monthNoString + "月-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleByMonth( productCodeList, otdTable, year.YearNo(), month.MonthNo() ),
                                                                       Global_MappingForecast::GetQuantityByMonth( productCodeList, otdTable, year.YearNo(), month.MonthNo() ) )
                                                   );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + monthNoString + "月-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := placeOfProductionOfArray,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceByMonth( productCodeList, macroPlan, year.YearNo(), month.MonthNo() ),
                                                                         Product_MP::GetNewSupplyByMonth( productCodeList, macroPlan, year.YearNo(), month.MonthNo() )
                                                                         )
                                                     );
            }
          }
    //      info( "process month end : " + [String]month.MonthNo() );
        }
      }
      if( byBusinessTypeOrByOrgCode = "事业部" and groupBy = "年" ) {
        // 目标
        traverse( businessTypeList, Elements, businessType ) {
          productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByBusinessType( rows, businessType );
          this.CapacityAndSaleBudgeChartElement( relnew,
                                                 TimeStringAndScenarioName := [String]year.YearNo() + "年-目标",
                                                 BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                 Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                     Global_MappingAnnualBudgetData::GetSaleByYear( productCodeList, otdTable, year.YearNo() ),
                                                                     Global_MappingForecast::GetQuantityByYear( productCodeList, otdTable, year.YearNo() ) )
                                                 );
          // S&OP
          traverse( macroPlans, Elements, macroPlan ) {
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-" + macroPlan.ScenarioName(),
                                                   BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Forecast::GetFulfilledBalanceByYear( productCodeList, macroPlan, year.YearNo() ) ,
                                                                       Product_MP::GetNewSupplyByYear( productCodeList, macroPlan, year.YearNo() )
                                                                       )
                                                   );
          }
        }
      }
      if ( byBusinessTypeOrByOrgCode = "事业部" and groupBy = "半年" ) {
        for( halfNo := 1; halfNo <= 2; halfNo := halfNo + 1 ) {
          traverse( businessTypeList, Elements, businessType ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByBusinessType( rows, businessType );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-" + ifexpr( halfNo = 1, "上", "下" ) + "半年-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleByHalfYear( productCodeList, otdTable, year.YearNo(), halfNo ),
                                                                       Global_MappingForecast::GetQuantityByHalfYear( productCodeList, otdTable, year.YearNo(), halfNo ) )
                                                   );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-" + ifexpr( halfNo = 1, "上", "下" ) + "半年-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceByHalfYear( productCodeList, macroPlan, year.YearNo(), halfNo ) ,
                                                                         Product_MP::GetNewSupplyByHalfYear( productCodeList, macroPlan, year.YearNo(), halfNo )
                                                                         )
                                                     );
            }
          }
        }
      }
      if ( byBusinessTypeOrByOrgCode = "事业部" and groupBy = "季度" ) {
        for( seasonNo := 1; seasonNo <= 4; seasonNo := seasonNo + 1 ) {
    //      debuginfo( "process season start : " + [String]seasonNo );
          traverse( businessTypeList, Elements, businessType ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByBusinessType( rows, businessType );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + [String]seasonNo + "季度-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleBySeason( productCodeList, otdTable, year.YearNo(), seasonNo ),
                                                                       Global_MappingForecast::GetQuantityBySeason( productCodeList, otdTable, year.YearNo(), seasonNo ) )
                                                   );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + [String]seasonNo + "季度-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceBySeason( productCodeList, macroPlan, year.YearNo(), seasonNo ),
                                                                         Product_MP::GetNewSupplyBySeason( productCodeList, macroPlan, year.YearNo(), seasonNo )
                                                                         )
                                                     );
            }
          }
    //      info( "process season end : " + [String]seasonNo );
        }
      }
      if ( byBusinessTypeOrByOrgCode = "事业部" and groupBy = "月" ) {
        traverse( months, Elements, month ) {
    //      debuginfo( "process month start : " + [String]month.MonthNo() );
          monthNoString := ifexpr( month.MonthNo() <= 9, "0" + [String]month.MonthNo(), [String]month.MonthNo() );
          traverse( businessTypeList, Elements, businessType ) {
            productCodeList := CapacityAndSaleBudgeChartRow::GetProductCodesByBusinessType( rows, businessType );
            this.CapacityAndSaleBudgeChartElement( relnew,
                                                   TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + monthNoString + "月-目标",
                                                   BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                   Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                       Global_MappingAnnualBudgetData::GetSaleByMonth( productCodeList, otdTable, year.YearNo(), month.MonthNo() ),
                                                                       Global_MappingForecast::GetQuantityByMonth( productCodeList, otdTable, year.YearNo(), month.MonthNo() ) )
                                                   );
            // S&OP
            traverse( macroPlans, Elements, macroPlan ) {
              this.CapacityAndSaleBudgeChartElement( relnew,
                                                     TimeStringAndScenarioName := [String]year.YearNo() + "年-第" + monthNoString + "月-" + macroPlan.ScenarioName(),
                                                     BusinessTypeOrPlaceOfProductionOfArray := businessType,
                                                     Quantity := ifexpr( saleOrCapacity = "销售额",
                                                                         Forecast::GetFulfilledBalanceByMonth( productCodeList, macroPlan, year.YearNo(), month.MonthNo() ) ,
                                                                         Product_MP::GetNewSupplyByMonth( productCodeList, macroPlan, year.YearNo(), month.MonthNo() )
                                                                         )
                                                     );
            }
          }
    //      debuginfo( "process month end : " + [String]month.MonthNo() );
        }
      }
    //  debuginfo( "process year end : " + [String]year.YearNo() )
    }
    //info( "CapacityAndSaleBudgeChartElement : " + [String]selectset( this, CapacityAndSaleBudgeChartElement, item, true ).Size() );
    //traverse( this, CapacityAndSaleBudgeChartRow, row ) {
    //  info( "==↓==" );
    //  info( row.BusinessType() );
    //  info( row.PlaceOfProductionOfArray() );
    //  elements := selectset( row, CapacityAndSaleBudgeChartRowProduct, cell, true );
    //  info( elements.Size() );
    //  info( row.GetProductCodes().Concatenate( " | " ) );
    //  info( "==↑==" );
    //}
    casbcirs := CapacityAndSaleBudgeChartElement::SelectGenerateChartRows( this, &items, &placeOfProductionOfArrays, &businessTypes, &years, &months );
    CapacityAndSaleBudgeChartElement::CreateData( this, macroPlans, casbcirs, popoa, groupBy, saleOrCapacity, years, months );
  *]
}