yanyuan
2023-08-30 eb958a9c253061d89ea524fe124b326369678557
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
Quintiq file version 2.0
#parent: #root
Method MappingOperationBOMDataRouting (
  Routing routing,
  MappingBOMs routingRows,
  StockingPoint_MP stockingPoint,
  Strings keyProductIds
)
{
  TextBody:
  [*
    // yypsybs Aug-21-2023 (created)
    
    // 根据工艺段汇总处理
    processSections := selectuniquevalues( routingRows, Elements, row, true, row.ProcessSection() );
    traverse( processSections, Elements, processSection ) {
        rows := selectset( routingRows, Elements, row, row.ProcessSection() = processSection );
        firstRow := rows.Element( 0 );
        // 找orgCode/productId/processSection匹配且sequence最小的一组数据
        minSeq := MappingOperation::FindMinSeq( this, firstRow.OrganCode(), firstRow.ProductCode(), processSection );
        routingStepId := processSection + "_" + [String]minSeq;
        // 找对应routingStep
        routingStep := RoutingStep::FindByName( routing, routingStepId );
        if( isnull( routingStep ) ) {
            error( "routing step : " + routing.ID() + "|" + routingStepId + " not found" );  
        }
        // 仅主料,统一处理
        noAlterRows := selectset( rows, Elements, noAlterRow, noAlterRow.AlternativeMaterialCode() = "" );
        traverse( noAlterRows, Elements, noAlterRow ) {
            traverse( routingStep, Operation, toLink ) {
                // 检查主料
                component := Product_MP::FindById( this, noAlterRow.ComponentCode() );
                if( isnull( component ) ) {
                    error( "component : " + noAlterRow.ComponentCode() + " not found" );
                }
                // 连接产品与operation
                pisp := ProductInStockingPoint_MP::CreateIfNotFound( component, stockingPoint );
                trash := construct( OperationBOMs );
                operationBOM := toLink.LinkProduct( component, true, BaseOperationLink::GetGroupID( toLink, true, false ), pisp, 
                                                    true, trash );
                operationBOM.MinQuantityInGroup( 0 );
                operationBOM.Quantity( noAlterRow.UnitUsageOfComponents() / noAlterRow.ComponentOutputRate() );
                operationBOM.MaxQuantityInGroup( noAlterRow.UnitUsageOfComponents() / noAlterRow.ComponentOutputRate() );
            }
        }
        // 替换料,按主料分组处理
        alterComponentIds := selectuniquevalues( rows, Elements, row, row.AlternativeMaterialCode() <> "", row.ComponentCode() );
        traverse( alterComponentIds, Elements, alterComponentId ) {
            alterRows := selectset( rows, Elements, row, row.ComponentCode() = alterComponentId );
            firstAlterRow := alterRows.Element( 0 );
            // 检查主料
            component := Product_MP::FindById( this, firstAlterRow.ComponentCode() );
            if( isnull( component ) ) {
                error( "component : " + firstAlterRow.ComponentCode() + " not found" );
            }
            traverse( routingStep, Operation, toLink ) {
                // 连接产品与operation
                pisp := ProductInStockingPoint_MP::CreateIfNotFound( component, stockingPoint );
                trash := construct( OperationBOMs );
                mainBOM := toLink.LinkProduct( component, true, BaseOperationLink::GetGroupID( toLink, true, false ), pisp, 
                                                    true, trash );
                mainBOM.Quantity( firstAlterRow.UnitUsageOfComponents() / firstAlterRow.ComponentOutputRate() );
                mainBOM.MaxQuantityInGroup( firstAlterRow.UnitUsageOfComponents() / firstAlterRow.ComponentOutputRate() );
                mainQty := mainBOM.Quantity();
                Transaction::Transaction().Propagate();
                // 添加辅料
                traverse( alterRows, Elements, alterRow ) {
                    alterProd := Product_MP::FindById( this, alterRow.AlternativeMaterialCode() );
                    if( isnull( alterProd ) ) {
                        error( "alterProd : " + alterRow.AlternativeMaterialCode() + " not found" );
                    }
                    stockingPointIdAlter := alterRow.OrganCode()  + "_" + alterRow.ProductType() + "_Stock";
                    stockingPointAlter := StockingPoint_MP::FindById( this, stockingPointIdAlter );
                    if( isnull( stockingPointAlter ) ) {
                        error( "stockingPoint : " + stockingPointIdAlter + " not found" );
                    }
                    pispAlter := ProductInStockingPoint_MP::CreateIfNotFound( alterProd, stockingPointAlter );
                    mainInput := toLink.LastOperationInput();
                    trash := construct( OperationBOMs );
                    if( not isnull( mainInput ) ) {
                        alterBom := mainInput.Operation().LinkPISP( pispAlter, true, mainInput.OperationLinkGroupID(), trash );
                        alterBom.Quantity( mainBOM.MaxQuantityInGroup() * alterRow.AlternativeRate() );
                        alterBom.MaxQuantityInGroup( mainBOM.MaxQuantityInGroup() );
                        info( "Quantity" + [String]alterBom.Quantity() )
                        info( "MaxQuantityInGroup" + [String]alterBom.MaxQuantityInGroup() )
                        mainQty := mainQty - alterBom.Quantity();
                    }
                }
                mainBOM.Quantity( mainQty );
            }
        }
    }
  *]
}