| Quintiq file version 2.0 | 
| #parent: #root | 
| StaticMethod InitializeNew ( | 
|   MacroPlan owner | 
| ) | 
| { | 
|   TextBody: | 
|   [* | 
|     //owner.DLEngineLogisticsCostReport( relflush ); | 
|     produtparent              := DLEngineLogisticsCostReport::GetDefaultProductParent(); | 
|     name                      := DLEngineLogisticsCostReport::GetDefaultName(); | 
|     allunit                   := DLEngineLogisticsCostReport::GetDefaultAllUnit(); | 
|     ccfactory                 := DLEngineLogisticsCostReport::GetCCFactory(); | 
|     dlfactory                 := DLEngineLogisticsCostReport::GetDLFactory(); | 
|      | 
|     startofplanning           := owner.StartOfPlanning().Date(); | 
|     startofnextyear           := startofplanning.StartOfNextYear(); | 
|      | 
|     table                     := selectobject(owner, DLEngineLogisticsCostReport, report, report.ID() = name ); | 
|     if( isnull( table ) ){ | 
|       table                   := owner.DLEngineLogisticsCostReport( relnew, ID := name, Name := name ); | 
|     } | 
|     table.Column( relflush ); | 
|     table.Row( relflush ); | 
|     showname                  := name + 'Show'; | 
|     if( not exists( owner, DLEngineLogisticsCostReport, report, report.ID() = showname and report.IsShow() ) ){ | 
|       owner.DLEngineLogisticsCostReport( relnew, ID := name + 'Show', Name := name, IsShow := true ); | 
|     } | 
|     if( isnull( owner.DLEngineLogisticsCostSearch() ) ){ | 
|       owner.DLEngineLogisticsCostSearch( relnew, Generation := allunit, MqbMlb := allunit, Power := allunit ); | 
|     } | 
|      | 
|     products                  := construct( Product_MPs ); | 
|      | 
|     table.GenerateColumn( owner ); | 
|     //外租库仓储费用:仓储数量/包装容量*仓储单价,在Actual inventories里取到长春外租库/大连外租库该发动机的仓储量,根据发动机号找到对应的Generation 和横纵制,在发动机成本参数表里找到对应的仓储单价和包装容量,用公式计算,月度进行汇总 | 
|     //长春外租库入库/出库费用,大连发动机的长春外租库的New supply | 
|     traverse( owner, StockingPoint_MP, stockingpoint, stockingpoint.ID().EndsWith( '外租库' ) ){ | 
|       isdltoccrent            := stockingpoint.ID().StartsWith( '大连发动机的长春' ); | 
|       isdlrent                := not isdltoccrent and stockingpoint.ID().StartsWith( '大连' ) or stockingpoint.ID().StartsWith( 'DL' ); | 
|       if( isdlrent or isdltoccrent){ | 
|         traverse( stockingpoint, ProductInStockingPoint_MP, pisp,  pisp.Product_MP().IsLeaf() | 
|                   and exists( pisp, ProductInStockingPointInPeriod, pispip, not pispip.Period_MP().IsHistorical() and pispip.Period_MP().StartDate() < startofnextyear and ( pispip.NewSupplyQuantity() <> 0 or pispip.PlannedInventoryLevelEnd() <> 0 ) ) ){ | 
|           //获取行 | 
|           product             := pisp.Product_MP(); | 
|           productparents      := product.GetAllParent(); | 
|           if( exists( productparents, Elements, e, e.ID() = produtparent ) ){//查询产品类型是发动机 | 
|             row               := table.GetRow( pisp.ProductID() ); | 
|             enginecosts       := selectset( owner, LogisticsCostEngine, engine, engine.PackagingCapacity() <> 0 and engine.Generation().ToLower() = product.Generation().ToLower() and engine.MLB_MQB() = product.MQBMLB() ); | 
|             transcosts        := selectset( owner, LogisticsCostTransport, trans, trans.LoadingCapacity() <> 0 and exists( productparents, Elements, e, ifexpr( trans.Product().FindString( '发动机', 0 ) >= 0, e.ID() = produtparent,  e.ID() = trans.Product() ) ) ); | 
|             if( not isnull( enginecosts ) and enginecosts.Size() > 0 ){ | 
|               traverse( table, Column, column ){ | 
|                 pispips       := selectset( pisp, ProductInStockingPointInPeriod, pispip, pispip.Start().Date() >= column.StartDate()  | 
|                                             and pispip.Start().Date() < column.StartDate().StartOfNextMonth()  | 
|                                             and ( pispip.NewSupplyQuantity() <> 0 or pispip.PlannedInventoryLevelEnd() <> 0 ) ); | 
|                 cell          := selectobject( row, Cell, cell, cell.Column() = column ); | 
|                 entercost     := 0.0; | 
|                 othercost     := 0.0; | 
|                 outcost       := 0.0; | 
|                 storecost     := 0.0; | 
|                 transcost     := 0.0; | 
|                 if( isdltoccrent){ | 
|                   othercost   := Real::MaxReal(); | 
|                   transset    := selectset( transcosts, Elements, trans, trans.Origin() = '长春外租库' and trans.Destination() = '客户库' ); | 
|                   column.CalcEngineCost( pispips, enginecosts, transset, ccfactory, entercost, othercost, outcost, storecost, transcost ); | 
|                   cell.CCRentQty( cell.CCRentQty() + sum( pispips, Elements, pispip, pispip.NewSupplyQuantity() ) ); | 
|                   cell.CCRentInCost( cell.CCRentInCost() + entercost ); | 
|                   cell.CCRentOutOfCost( cell.CCRentOutOfCost() + outcost ); | 
|                   cell.CCRentStorCost( cell.CCRentStorCost() + storecost );//长春外租库仓储费用 | 
|                   cell.CCShorTransCost( cell.CCShorTransCost() + transcost );//长春短途运输费用 | 
|                 } else { | 
|                   outcost     := Real::MaxReal(); | 
|                   transset    := selectset( transcosts, Elements, trans, trans.Origin() = '大连厂内库' and trans.Destination() = '大连外租库' ); | 
|                   column.CalcEngineCost( pispips, enginecosts, transset, dlfactory, entercost, othercost, outcost, storecost, transcost ); | 
|                   cell.DLRentStorCost( cell.DLRentStorCost() + storecost );//大连外租库仓储费用 | 
|                   cell.DLRentOtherCost( cell.DLRentOtherCost() + othercost );//大连外租库其他费用 | 
|                   cell.DLRentInCost( cell.DLRentInCost() + entercost );//大连外租库入库费用 | 
|                   cell.WerkToDLRentTransCost( cell.WerkToDLRentTransCost() + transcost );//厂内到大连外租库运输费用 | 
|                 } | 
|               } | 
|               products.Add( pisp.Product_MP() ); | 
|             } | 
|           } | 
|         } | 
|       } | 
|     } | 
|      | 
|     //长春长途运输费用:调拨数量/包装容量/装载容量*运输单价,调拨计划里找到该产品从DL到CC的调拨数量,即起始地大连装配线边库,目的地为长春外租库对应的运输数量,在发动机成本参数表找到对应的包装容量,在运输成本参数表里找到发动机对应运输单价和装载容量,用公式计算得出结果,月度进行汇总 | 
|     traverse( owner, Unit.Lane.LaneLeg, laneleg ){ | 
|       //起始库存点是否是大连(大连厂内库) | 
|       isdl                    := laneleg.OriginStockingPointID().EndsWith( '厂内库' ) and ( laneleg.OriginStockingPointID().StartsWith( '大连' ) or laneleg.OriginStockingPointID().StartsWith( 'DL' ) ); | 
|       //目的地库存点是否是长春(长春外租库) | 
|       iscc                    := laneleg.DestinationStockingPointID() = '大连发动机的长春外租库'; | 
|       if( isdl and iscc ){ | 
|         allenginecosts        := selectset( owner, LogisticsCostEngine, engine, engine.PackagingCapacity() <> 0 and engine.Factory() = ccfactory ); | 
|         alltranscosts         := selectset( owner, LogisticsCostTransport, trans, trans.LoadingCapacity() <> 0 and trans.Origin() = laneleg.OriginStockingPointID() and trans.Destination() = laneleg.DestinationStockingPointID() ); | 
|         if( allenginecosts.Size() > 0 and alltranscosts.Size() > 0 ){ | 
|           alltrips            := selectset( laneleg, Trip, trip, exists( trip, ProductInTrip, pit, pit.Quantity() <> 0 and exists( pit.Product_MP().GetAllParent(), Elements, e, e.ID() = produtparent ) ) ); | 
|           productids          := selectuniquevalues( alltrips, Elements.ProductInTrip, pit, pit.Quantity() <> 0 and exists( pit.Product_MP().GetAllParent(), Elements, e, e.ID() = produtparent ), pit.ProductID() ); | 
|           traverse( productids, Elements, productid ){ | 
|             product           := selectobject( owner, Product_MP, product, product.ID() = productid ); | 
|             row               := table.GetRow( productid ); | 
|             products.Add( product ); | 
|             producttrips      := selectset( alltrips, Elements.ProductInTrip, pit, pit.Quantity() > 0 and pit.ProductID() = productid ); | 
|             enginecosts       := selectset( allenginecosts, Elements, engine, engine.Generation().ToLower() = product.Generation().ToLower() and engine.MLB_MQB() = product.MQBMLB() ); | 
|             //在运输成本参数表里找到发动机对应运输单价和装载容量 | 
|             transcosts        := selectset( alltranscosts, Elements, trans, exists( product.GetAllParent(), Elements, e, ifexpr( trans.Product().FindString( '发动机', 0 ) >= 0, e.ID() = produtparent,  e.ID() = trans.Product() ) ) ); | 
|             if( not isnull( enginecosts ) and enginecosts.Size() > 0 and not isnull( transcosts ) and transcosts.Size() > 0 ){ | 
|               traverse( table, Column, column){//, column.StartDate().Month() = 1  | 
|                 columntrips   := selectset( producttrips, Elements, pit, pit.Trip().Arrival().Date() < column.StartDate().StartOfNextMonth() and pit.Trip().Arrival().Date() >= column.StartDate() ); | 
|                 cell          := selectobject( row, Cell, cell, cell.Column() = column ); | 
|                 cell.CCLongTransCost( column.CalcEngineCost( columntrips, enginecosts, transcosts ) ); | 
|               } | 
|             } | 
|           } | 
|         } | 
|     //traverse( owner, TransferPlanRow, tprow ){ | 
|     //  product                 := selectobject( owner, Product_MP, product, product.ID() = tprow.ProductID() ); | 
|     //  productparents          := product.GetAllParent(); | 
|     //  if( exists( productparents, Elements, e, e.ID() = produtparent ) ){//查询产品类型是发动机 | 
|     //    //起始库存点是否是大连(大连厂内库) | 
|     //    isdl                   := tprow.SourceStockpoingPointID().EndsWith( '厂内库' ) and ( tprow.TargetStockpoingPointID().StartsWith( '大连' ) or tprow.TargetStockpoingPointID().StartsWith( 'DL' ) ); | 
|     //    //目的地库存点是否是长春(长春外租库) | 
|     //    iscc                   := tprow.TargetStockpoingPointID().EndsWith( '外租库' ) and ( tprow.TargetStockpoingPointID().StartsWith( '长春' ) or tprow.TargetStockpoingPointID().StartsWith( 'CC' ) ); | 
|     //    if( isdl and iscc ){ | 
|     //      //查询对应的发动机成本 | 
|     //      enginecosts          := selectset( owner, LogisticsCostEngine, engine, engine.PackagingCapacity() <> 0 and engine.Generation().ToLower() = product.Generation().ToLower()  | 
|     //                                             and engine.MLB_MQB() = product.MQBMLB() | 
|     //                                             and engine.Factory() = ccfactory ); | 
|     //      //在运输成本参数表里找到发动机对应运输单价和装载容量 | 
|     //      transcosts           := selectset( owner, LogisticsCostTransport, trans, trans.LoadingCapacity() <> 0 and trans.Origin() = tprow.SourceStockpoingPointID()  | 
|     //                                              and trans.Destination() = tprow.TargetStockpoingPointID()  | 
|     //                                              and exists( productparents, Elements, e, ifexpr( trans.Product().FindString( '发动机', 0 ) >= 0, e.ID() = produtparent,  e.ID() = trans.Product() ) ) ); | 
|     //      if( not isnull( enginecosts ) and enginecosts.Size() > 0 and not isnull( transcosts ) and transcosts.Size() > 0 ){ | 
|     //        row                := table.GetRow( product.ID() ); | 
|     //        products.Add( product ); | 
|     //        traverse( tprow,TransferPlanCell, tpcell, tpcell.TransferPlanColumn().ColumnDate() <> Date::MinDate() and [Number]tpcell.Value() > 0){ | 
|     //          tpcolumndate     := tpcell.TransferPlanColumn().ColumnDate(); | 
|     //          enginecost       := selectobject( enginecosts, Elements, engine, engine.StartDate() <= tpcolumndate and engine.EndDate() >= tpcolumndate ); | 
|     //          transcost        := selectobject( transcosts, Elements, trans, trans.StartDate() <= tpcolumndate and trans.EndDate() >= tpcolumndate ); | 
|     //          if( not isnull( enginecost ) and not isnull( transcost ) ){ | 
|     //            periodtime     := tpcolumndate.StartOfMonth(); | 
|     //            periodname     := periodtime.Format( "M2/D2/Y" ); | 
|     //            column         := selectobject( table, Column, column, column.Name() = periodname and column.StartDate() = periodtime ); | 
|     //              | 
|     //            cost           := ceil( ceil( [Number]tpcell.Value() / enginecost.PackagingCapacity() ) / transcost.LoadingCapacity() ) * transcost.TransportPrice(); | 
|     //            cell           := selectobject( row, Cell, cell, cell.Column() = column ); | 
|     //            cell.CCLongTransCost( cell.CCLongTransCost() + cost ); | 
|     //          } | 
|     //        } | 
|     //      } | 
|     //    } | 
|       } | 
|     } | 
|     //长春短途运输费用:客户需求数量/包装容量/装载容量*运输单价,客户需求数量取自forecast里sales segment为长春的数量汇总,再用公式计算,月度进行汇总(取客户需求数量换算成车次) | 
|     allforecast               := selectset( owner, SalesDemand.astype( Forecast ), forecast, forecast.Quantity() > 0 and not isnull( forecast.SalesSegment_MP() ) and forecast.StartDate() < startofnextyear and //forecast.ProductID() = '6912' and forecast.StartDate().Month() = 1 and | 
|     //                                         ( forecast.SalesSegment_MP().Name().StartsWith( 'Changchun' ) or exists( forecast.SalesSegment_MP().GetAllParent(), Elements, psalessegment, psalessegment.Name().StartsWith( 'Changchun' ) ) or  | 
|                                                ( forecast.SalesSegment_MP().Name().StartsWith( 'Foshan' ) or forecast.SalesSegment_MP().Name().StartsWith( 'Tianjin' ) | 
|                                                 or exists( forecast.SalesSegment_MP().GetAllParent(), Elements, psalessegment, psalessegment.Name().StartsWith( 'Foshan' ) or psalessegment.Name().StartsWith( 'Tianjin' ) ) ) | 
|                                              and exists( forecast.Product_MP().GetAllParent(), Elements, e, e.ID() = produtparent ) ); | 
|     //ccforecasts               := selectset( allforecast, Elements, forecast, forecast.SalesSegment_MP().Name().StartsWith( 'Changchun' ) or exists( forecast.SalesSegment_MP().GetAllParent(), Elements, psalessegment, psalessegment.Name().StartsWith( 'Changchun' ) )  ); | 
|     //ccproducts                := selectuniquevalues( ccforecasts, Elements, forecast, forecast.ProductID() ); | 
|     dlforecasts               := selectset( allforecast, Elements, forecast, forecast.SalesSegment_MP().Name().StartsWith( 'Foshan' ) or forecast.SalesSegment_MP().Name().StartsWith( 'Tianjin' ) | 
|                                                 or exists( forecast.SalesSegment_MP().GetAllParent(), Elements, psalessegment, psalessegment.Name().StartsWith( 'Foshan' ) or psalessegment.Name().StartsWith( 'Tianjin' ) ) ); | 
|     dlproducts                := selectuniquevalues( dlforecasts, Elements, forecast, forecast.ProductID() );; | 
|     //traverse( ccproducts, Elements, ccproduct){ | 
|     //  product                 := selectobject( owner, Product_MP, product, product.ID() = ccproduct ); | 
|     ////  info( '************', product.ID(), exists( ccforecasts, Elements, forecast, forecast.Quantity() > 0 )); | 
|     //  //查询对应的发动机成本 | 
|     //      enginecosts         := selectset( owner, LogisticsCostEngine, engine, engine.PackagingCapacity() <> 0 and engine.Generation().ToLower() = product.Generation().ToLower()  | 
|     //                                             and engine.MLB_MQB() = product.MQBMLB()and engine.Factory() = ccfactory ); | 
|     //      //在运输成本参数表里找到发动机对应运输单价和装载容量 | 
|     //      transcosts           := selectset( owner, LogisticsCostTransport, trans, trans.LoadingCapacity() <> 0 and trans.Origin() = '长春外租库' | 
|     //                                              and trans.Destination() = '客户库' | 
|     //                                              and exists( product.GetAllParent(), Elements, e, ifexpr( trans.Product().FindString( '发动机', 0 ) >= 0, e.ID() = produtparent,  e.ID() = trans.Product() ) ) ); | 
|     //  if( not isnull( enginecosts ) and enginecosts.Size() > 0 and not isnull( transcosts ) and transcosts.Size() > 0 ){ | 
|     //    row               := table.GetRow( ccproduct ) | 
|     //    products.Add( product ); | 
|     //    traverse( table, Column, column ){ | 
|     //      allpsdips             := selectset( ccforecasts, Elements.PlanningSalesDemandInPeriod, psdip, psdip.ProductID() = ccproduct and psdip.Quantity() > 0 and psdip.StartDate() < column.StartDate().StartOfNextMonth() and psdip.StartDate() >= column.StartDate() ); | 
|     //      enginecost      := selectobject( enginecosts, Elements, engine, engine.StartDate() <= column.StartDate() and engine.EndDate() >= column.StartDate() ); | 
|     //      transcost       := selectobject( transcosts, Elements, trans, trans.StartDate() <= column.StartDate() and trans.EndDate() >= column.StartDate() ); | 
|     //      if( not isnull( enginecost ) and not isnull( transcost ) ){ | 
|     //        quantity        := sum( allpsdips, Elements, psdip, psdip.Quantity() ); | 
|     //        cost           := ceil( ceil( quantity / enginecost.PackagingCapacity() ) / transcost.LoadingCapacity() ) * transcost.TransportPrice(); | 
|     //         cell           := selectobject( row, Cell, cell, cell.Column() = column ); | 
|     //         cell.CCShorTransCost( cell.CCShorTransCost() + cost ); | 
|     //       } | 
|     //    } | 
|     //  } | 
|     //} | 
|     traverse( dlproducts, Elements, productid){ | 
|        | 
|       row                     := table.GetRow( productid ) | 
|       product                 := selectobject( owner, Product_MP, product, product.ID() = productid ); | 
|       //查询对应的发动机成本  | 
|       enginecosts             := selectset( owner, LogisticsCostEngine, engine, engine.PackagingCapacity() <> 0 and engine.Generation().ToLower() = product.Generation().ToLower()  | 
|                                                  and engine.MLB_MQB() = product.MQBMLB()and engine.Factory() = dlfactory ); | 
|       if( not isnull( enginecosts ) and enginecosts.Size() > 0 ){ | 
|         products.Add( product ); | 
|         allpsdips             := selectset( dlforecasts, Elements.PlanningSalesDemandInPeriod, psdip, psdip.ProductID() = productid and psdip.Quantity() > 0 ); | 
|         traverse( table, Column, column ){ | 
|           psdips              := selectset( allpsdips, Elements, psdip, psdip.StartDate() <= column.EndDate() and psdip.StartDate() >= column.StartDate() ); | 
|           row.SetDLRentOutCost( column, column.CalcEngineCost( psdips, enginecosts ) ); | 
|         } | 
|       } | 
|     } | 
|      | 
|     //rows := selectsortedset( table, Row, row, row.Name() ); | 
|     //i    := 0; | 
|     //traverse( rows, Elements, e ){ | 
|     //  e.RowNr( i ); | 
|     //  i := i + 1; | 
|     //} | 
|     info( '-------------------------------------end----------------------------------' ); | 
|     // | 
|     //showtable.Generate( search, products ); | 
|   *] | 
| } |