lazhen
2024-07-01 17dc904834ee19f0e57db374aaadea932acdaf00
_Main/BL/Type_FinancialSalesSource/StaticMethod_Initialize.qbl
@@ -4,77 +4,96 @@
  MacroPlan owner
)
{
  Description: '初始化'
  TextBody:
  [*
    // 甄兰鸽 Jun-24-2024 (created)
    owner.FinancialProductionSource( relflush );
    ccunit                    := FinancialProductionReport::GetDefaultCCUnit();
    dlunit                    := FinancialProductionReport::GetDefaultDLUnit();
    allunit                   := FinancialProductionReport::GetDefaultAllUnit();
    source                    := owner.FinancialProductionSource( relnew, IsImport := false, Name := FinancialProductionReport::GetDefaultName() );
    table                     := source.FinancialProductionReport( relnew, ID := source.Name(), Name := source.Name(), IsImport := false );
    showtable                 := source.FinancialProductionReport( relnew, ID := source.Name() + 'Show', Name := source.Name(), IsImport := false, IsShow := true );
    owner.FinancialSalesSource( relflush );
    products                  := construct( Product_MPs );
    allsalessegment           := FinancialSalesReport::GetSalesSegmentAll();
    ccsalessegment            := FinancialSalesReport::GetSalesSegmentCC();
    dlsalessegment            := FinancialSalesReport::GetSalesSegmentDL();
    tjsalessegment            := FinancialSalesReport::GetSalesSegmentTJ();
    fssalessegment            := FinancialSalesReport::GetSalesSegmentFS();
    ccspline                  := FinancialSalesReport::GetStockingPointCCLine();
    dlspline                  := FinancialSalesReport::GetStockingPointDLLine();
    ccrent                    := FinancialSalesReport::GetStockingPointCCRent();
    source                    := owner.FinancialSalesSource( relnew, IsImport := false, Name := FinancialSalesReport::GetDefaultName() );
    table                     := source.FinancialSalesReport( relnew, ID := source.Name(), Name := source.Name(), IsImport := false );
    showtable                 := source.FinancialSalesReport( relnew, ID := source.Name() + 'Show', Name := source.Name(), IsImport := false, IsShow := true );
    startofplanning           := owner.StartOfPlanning();
    startofnextmonth          := owner.StartOfPlanning().StartOfNextMonth();
    startofyear               := startofplanning.StartOfYear();
    startofnextyear           := startofplanning.StartOfNextYear();
    
    search                    := source.FinancialProductionSearch( relnew, Unit := allunit, Generation := allunit, MqbMlb := allunit, Power := allunit );
    products                  := construct( Product_MPs );
    search                    := source.FinancialSalesSearch( relnew, Unit := allsalessegment, Generation := allsalessegment, MqbMlb := allsalessegment, Power := allsalessegment );
    
    table.GenerateColumn( owner );
    
    traverse( owner, Product_MP.ProductInStockingPoint_MP, pisp, pisp.Product_MP().IsLeaf() and ( pisp.StockingPoint_MP().UnitID() = ccunit or pisp.StockingPoint_MP().UnitID() = dlunit ) ){
      unit := pisp.StockingPoint_MP().UnitID();
      info( unit, pisp.ProductID() );
      ccrow := null( FinancialProductionRow );
      dlrow := null( FinancialProductionRow );
      allrow := selectobject( table, FinancialProductionRow, row, row.Name() = pisp.ProductID() and row.Unit() = allunit );
      if( unit = ccunit ){
        ccrow := table.FinancialProductionRow( relnew, Name := pisp.ProductID(), Unit := unit );
      }else{
        dlrow := table.FinancialProductionRow( relnew, Name := pisp.ProductID(), Unit := unit );
      }
      if( isnull( allrow ) ){
        products.Add( pisp.Product_MP() );
        allrow := table.FinancialProductionRow( relnew, Name := pisp.ProductID(), Unit := allunit );
      }
    //从Forecast查询
    traverse( owner, SalesDemand.astype( Forecast ), forecast, forecast.StartDate() < startofnextyear.Date() and ( ( forecast.SalesSegmentName() = ccsalessegment and forecast.Product_MP().MQBMLB() = 'MLB' )
              or forecast.SalesSegmentName() = tjsalessegment or forecast.SalesSegmentName() = fssalessegment ) ){
      product    := forecast.Product_MP();
      allrow     := table.GetRow( allsalessegment, product.ID(), startofyear, startofnextyear );
      periodtime := forecast.StartDate().StartOfMonth();
      periodname := periodtime.Format( "M2/D2/Y" );
      
      for( start := startofyear; start < startofnextyear; start := start.StartOfNextMonth() ){
        periodtime := start.Date();
        periodname := periodtime.Format( "M2/D2/Y" );
        column := selectobject( table, FinancialProductionColumn, column, column.Name() = periodname and column.Period() = periodtime );
        if( not isnull( ccrow ) ){
          ccrow.Initialize( column, ccunit );
        }
        if( not isnull( dlrow ) ){
          dlrow.Initialize( column, dlunit );
        }
        if( not exists( allrow, FinancialProductionCell, allcell, allcell.FinancialProductionColumn() = column ) ){
          allrow.Initialize( column, allunit );
        }
      if( products.Find( product ) < 0 ){
        products.Add( product );
      }
      traverse( pisp, ProductInStockingPointInPeriod, pispip, pispip.Start() >= startofplanning
                and ( ( pispip.Start() < startofnextmonth and pispip.Period_MP().TimeUnit() = Translations::MP_GlobalParameters_Day() )
                      or ( pispip.Start() < startofnextyear and pispip.Period_MP().TimeUnit() = Translations::MP_GlobalParameters_Month() ) ) ){
        periodtime := pispip.Start().StartOfMonth().Date();
        periodname := periodtime.Format( "M2/D2/Y" );
        column := selectobject( table, FinancialProductionColumn, column, column.Name() = periodname and column.Period() = periodtime );
        if( not isnull( ccrow ) ){
          ccrow.Initialize( column, pispip.NewSupplyProductionQuantity() );
    //  info( '------------------------', periodname, periodtime );
      column    := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
    //  info( '------------------------', column.Name() );
      //Forecast的Sales Segment为长春,识别出MLB的所有产品汇总
      if( forecast.SalesSegmentName() = ccsalessegment and product.MQBMLB() = 'MLB' ){
    //    info( '------------------------', column.Name() );
        ccrow := table.GetRow( ccsalessegment, product.ID(), startofyear, startofnextyear );
    //    info( '------------------------', ccrow.FinancialSalesCell( relsize ), column.FinancialSalesCell( relsize ) );
        ccrow.Initialize( column, forecast.Quantity() );
      }else if( forecast.SalesSegmentName() = tjsalessegment or forecast.SalesSegmentName() = fssalessegment ){
        //大连财务销量:首先在forecast界面查找Sales Segment是天津和佛山的所有需求,每个产品按月汇总需求数量
    //    info( '------------------------', column.Name() );
        dlrow := table.GetRow( dlsalessegment, product.ID(), startofyear, startofnextyear );
    //    info( '------------------------', dlrow.FinancialSalesCell( relsize ), column.FinancialSalesCell( relsize ) );
        dlrow.Initialize( column, forecast.Quantity() );
      }
      allrow.Initialize( column, forecast.Quantity() );
    }
    //从Product planning查找库存点为长春装配线边库的所有MQB产品,取Total Demand字段按月汇总需求数量
    traverse( owner, Product_MP, product, product.IsLeaf() and product.MQBMLB() = 'MQB' ){//产品为MQB
      traverse( product, ProductInStockingPoint_MP, pisp, pisp.StockingPointID() = ccspline ){//库存点为长春装配线边库
        traverse( pisp, ProductInStockingPointInPeriod, pispip, pispip.Start() >= startofplanning
                and pispip.Start() < startofnextyear and pispip.Period_MP().TimeUnit() = Translations::MP_GlobalParameters_Month() ){
          periodtime := pispip.Start().StartOfMonth().Date();
          periodname := periodtime.Format( "M2/D2/Y" );
          column    := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
          ccrow := table.GetRow( ccsalessegment, product.ID(), startofyear, startofnextyear );
          ccrow.Initialize( column, pispip.DependentDemandAndSalesDemandQuantity() );//取Total Demand字段按月汇总需求数量
          allrow     := table.GetRow( allsalessegment, product.ID(), startofyear, startofnextyear );
          allrow.Initialize( column, pispip.DependentDemandAndSalesDemandQuantity() );
        }
        if( not isnull( dlrow ) ){
          dlrow.Initialize( column, pispip.NewSupplyProductionQuantity() );
        }
        allrow.Initialize( column, pispip.NewSupplyProductionQuantity() );
      }
    }
    rows := selectsortedset( table, FinancialProductionRow, row, row.Name() );
    startofnextyearlead       := startofplanning.StartOfNextYear() + FinancialSalesReport::GetDefaultTripLeadingTime();
    //大连财务销量:长春的需求需要在trip plan里面找到起始库存点为大连装配线边库,目的地为长春外租库的产品,由于需要考虑产品运输lead time,每个月的汇总数据需要推迟两天进行计算
    traverse( owner, Unit.Lane.LaneLeg, laneleg, laneleg.OriginStockingPointID() = dlspline and laneleg.DestinationStockingPointID() = ccrent ){
      traverse( laneleg, Trip, trip, trip.Arrival() < startofnextyearlead ){
        periodtime := ( trip.Arrival() - FinancialSalesReport::GetDefaultTripLeadingTime() ).StartOfMonth().Date();
        periodname := periodtime.Format( "M2/D2/Y" );
        column    := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
        traverse( trip, ProductInTrip, pit ){
          dlrow      := table.GetRow( dlsalessegment, pit.ProductID(), startofyear, startofnextyear );
          dlrow.Initialize( column, pit.Quantity() );//取Total Demand字段按月汇总需求数量
          allrow     := table.GetRow( allsalessegment, pit.ProductID(), startofyear, startofnextyear );
          allrow.Initialize( column, pit.Quantity() );
        }
      }
    }
    rows := selectsortedset( table, FinancialSalesRow, row, row.Name() );
    i    := 0;
    traverse( rows, Elements, e ){
      e.RowNr( i );