lazhen
2024-08-05 afbad399be481afbe886bc2ad1e22abbcd5df771
_Main/BL/Type_FinancialSalesSource/StaticMethod_Initialize.qbl
@@ -10,9 +10,10 @@
    // 甄兰鸽 Jun-24-2024 (created)
    owner.FinancialSalesSource( relflush );
    products                  := construct( Product_MPs );
    allsalessegment           := FinancialSalesReport::GetSalesSegmentAll();
    allunit                   := FinancialSalesReport::GetDefaultAllUnit();
    ccunit                    := FinancialSalesReport::GetDefaultCCUnit();
    dlunit                    := FinancialSalesReport::GetDefaultDLUnit();
    ccsalessegment            := FinancialSalesReport::GetSalesSegmentCC();
    dlsalessegment            := FinancialSalesReport::GetSalesSegmentDL();
    tjsalessegment            := FinancialSalesReport::GetSalesSegmentTJ();
    fssalessegment            := FinancialSalesReport::GetSalesSegmentFS();
    ccspline                  := FinancialSalesReport::GetStockingPointCCLine();
@@ -22,73 +23,94 @@
    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();
    startofplanning           := owner.StartOfPlanning().Date();
    //startofyear               := startofplanning.StartOfYear();
    startofnextyear           := startofplanning.StartOfNextYear();
    
    search                    := source.FinancialSalesSearch( relnew, Unit := allsalessegment, Generation := allsalessegment, MqbMlb := allsalessegment, Power := allsalessegment );
    search                    := source.FinancialSalesSearch( relnew, Unit := allunit, Generation := allunit, MqbMlb := allunit, Power := allunit );
    
    table.GenerateColumn( owner );
    
    //从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() );
      periodtime := forecast.StartDate().StartOfMonth();
      periodname := periodtime.Format( "M2/D2/Y" );
      if( products.Find( product ) < 0 ){
        products.Add( product );
      }
    //  info( '------------------------', periodname, periodtime );
      column    := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
    //  info( '------------------------', column.Name() );
    //首先在forecast界面查找sales segment为长春的所有需求,识别出MLB的所有产品,每个产品按月汇总需求数量
    traverse( owner, SalesDemand.astype( Forecast ), forecast, forecast.StartDate() < startofnextyear ){
      salessegment            := forecast.SalesSegment_MP();
      parentsalessegments     := salessegment.GetAllParent();
      //是否属于长春
      iscc                    := salessegment.Name() = ccsalessegment or exists( parentsalessegments, Elements, psalessegment, psalessegment.Name() = ccsalessegment );
      //是否属于大连, 即天津和佛山
      isdl                    := salessegment.Name() = tjsalessegment or salessegment.Name() = fssalessegment
                                  or exists( parentsalessegments, Elements, psalessegment, psalessegment.Name() = tjsalessegment )
                                  or exists( parentsalessegments, Elements, psalessegment, psalessegment.Name() = fssalessegment );
      //Forecast的Sales Segment为长春,识别出MLB的所有产品汇总
      if( forecast.SalesSegmentName() = ccsalessegment and product.MQBMLB() = 'MLB' ){
    //    info( '------------------------', column.Name() );
        ccrow := table.GetRow( ccsalessegment, product.ID() );
    //    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() );
    //    info( '------------------------', dlrow.FinancialSalesCell( relsize ), column.FinancialSalesCell( relsize ) );
        dlrow.Initialize( column, forecast.Quantity() );
      //大连财务销量:首先在forecast界面查找Sales Segment是天津和佛山的所有需求,每个产品按月汇总需求数量
      if( ( iscc and forecast.Product_MP().MQBMLB() = 'MLB' ) or isdl ){
        product              := forecast.Product_MP();
        //获取合计行
        allrow               := table.GetRow( allunit, product.ID() );
        //获取工厂行
        factoryrow           := table.GetRow( ifexpr( iscc and forecast.Product_MP().MQBMLB() = 'MLB', ccunit, dlunit ), product.ID() );
        if( products.Find( product ) < 0 ){
          products.Add( product );
        }
        traverse( forecast, PlanningSalesDemandInPeriod, psdip ){
          periodtime         := psdip.StartDate().StartOfMonth();
          periodname         := periodtime.Format( "M2/D2/Y" );
    //      info( '-------------------------', periodname, periodtime );
          column             := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
    //      info( '-------------------------', column.Name() );
          quantity   := ceil( psdip.Quantity() );//向上取整
          factoryrow.Initialize( column, quantity );
          allrow.Initialize( column, 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() );
          ccrow.Initialize( column, pispip.DependentDemandAndSalesDemandQuantity() );//取Total Demand字段按月汇总需求数量
          allrow     := table.GetRow( allsalessegment, product.ID() );
          allrow.Initialize( column, pispip.DependentDemandAndSalesDemandQuantity() );
    traverse( owner, StockingPoint_MP, stockingpoint ){
    //  unit                  := stockingpoint.Unit();
    //  parentunits           := unit.GetAllParent();
      //是否属于长春装配线
    //  iscc                  := unit.ID() = ccspline or exists( parentunits, Elements, punit, punit.ID() = ccspline );
      if( table.IsInUnit( stockingpoint, ccspline ) ){
        traverse( stockingpoint, ProductInStockingPoint_MP, pisp, pisp.Product_MP().IsLeaf() and pisp.Product_MP().MQBMLB() = 'MQB' ){//产品为MQB
          allrow            := table.GetRow( allunit, pisp.ProductID() );
          ccrow             := table.GetRow( ccunit, pisp.ProductID() );
          traverse( pisp, ProductInStockingPointInPeriod, pispip, not pispip.Period_MP().IsHistorical() and pispip.Period_MP().StartDate() < startofnextyear ){
            periodtime      := pispip.Start().StartOfMonth().Date();
            periodname      := periodtime.Format( "M2/D2/Y" );
            column          := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
            quantity        := ceil( pispip.DependentDemandAndSalesDemandQuantity() );//向上取整
            ccrow.Initialize( column, quantity );//取Total Demand字段按月汇总需求数量
            allrow.Initialize( column, quantity );
          }
        }
      }
    }
    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() );
          dlrow.Initialize( column, pit.Quantity() );//取Total Demand字段按月汇总需求数量
    traverse( owner, Unit.Lane.LaneLeg, laneleg ){
      //起始库存点是否是大连装配线边库
      originsp              := selectobject( owner, StockingPoint_MP, sp, sp.ID() = laneleg.OriginStockingPointID() );
      isdlspline            := table.IsInUnit( originsp, dlspline );
      //目的地是否是长春外租库
      destisp               := selectobject( owner, StockingPoint_MP, sp, sp.ID() = laneleg.DestinationStockingPointID() );
      isccrent              := table.IsInUnit( destisp, ccrent );
      if( isdlspline and isccrent ){
        traverse( laneleg, Trip, trip, trip.Arrival() < startofnextyearlead ){
          periodtime        := ( trip.Arrival() - FinancialSalesReport::GetDefaultTripLeadingTime() ).StartOfMonth().Date();
          periodname        := periodtime.Format( "M2/D2/Y" );
          
          allrow     := table.GetRow( allsalessegment, pit.ProductID() );
          allrow.Initialize( column, pit.Quantity() );
          column            := selectobject( table, FinancialSalesColumn, column, column.Name() = periodname and column.Period() = periodtime );
          traverse( trip, ProductInTrip, pit ){
            quantity        := ceil( pit.Quantity() );//向上取整
            dlrow           := table.GetRow( dlunit, pit.ProductID() );
            dlrow.Initialize( column, quantity );//汇总数量
            allrow          := table.GetRow( allunit, pit.ProductID() );
            allrow.Initialize( column, quantity );
          }
        }
      }
    }