yypsybs
2023-09-09 3cb5a54def670d97301f07170fcaad213bfc54f2
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Quintiq file version 2.0
#parent: #root
Method MappingOperationBOMData (
  Strings businessTypes,
  Boolean isKeyProduct,
  Boolean createPurchaseSupplyMaterial
)
{
  TextBody:
  [*
    // keyProduct
    keyProductIds := selectuniquevalues( this, MappingProduct, prod, prod.KeyProduct() = true, prod.ID() );
    // 获取有序的待处理记录
    toDealOps := construct( MappingOperations );
    toDealBoms := construct( MappingBOMs );
    if( not isnull( businessTypes ) and businessTypes.Size() > 0 ) {
        toDealOps := selectsortedset( this, MappingOperation, item, 
                                      businessTypes.Find( item.BusinessType() ) >= 0 
                                      and ( not isKeyProduct or keyProductIds.Find( item.ProductID() ) >= 0 ), 
                                      item.SequenceNumber() );
        toDealBoms := selectset( this, MappingBOM, item, 
                                 businessTypes.Find( item.BusinessType() ) >= 0 
                                 and ( not isKeyProduct or keyProductIds.Find( item.ComponentCode() ) >= 0 ) );
    } else {
        toDealOps := selectsortedset( this, MappingOperation, item, 
                                      not isKeyProduct or keyProductIds.Find( item.ProductID() ) >= 0 , 
                                      item.SequenceNumber() );
        toDealBoms := selectset( this, MappingBOM, item, 
                                 businessTypes.Find( item.BusinessType() ) >= 0
                                 and ( not isKeyProduct or keyProductIds.Find( item.ComponentCode() ) >= 0 ) );
    }
    // 对每个routing计算inputGroupId
    inputGroupId := 1
    // 对每个Routing进行投入产出处理
    routingKeys := selectuniquevalues( toDealOps, Elements, item, item.OrganCode() + "_" + item.ProductID() );
    traverse( routingKeys, Elements, routingKey ) {
      // ==== 产出 ====
      // 找最大seq
      maxSeq := max( toDealOps, Elements, item, item.OrganCode() + "_" + item.ProductID() = routingKey, item.SequenceNumber() );
      maxSeqElements := selectset( toDealOps, Elements, item, 
                                   item.OrganCode() + "_" + item.ProductID() = routingKey and item.SequenceNumber() = maxSeq );
      traverse( maxSeqElements, Elements, maxSeqELement ) {
        operationId := maxSeqELement.OrganCode() + "_" + maxSeqELement.ProductID() + "_" + maxSeqELement.ProcessSection();
        if( maxSeqELement.Line() <> "" ) {
          operationId := operationId + "_" + maxSeqELement.Line();
        }
        this.IOOperationBOM( relnew, 
                             OperationID := operationId, 
                             ProductID := maxSeqELement.ProductID(), 
                             StockingPointID := maxSeqELement.OrganCode() + "_" + this.GetProductTypeByProductId( maxSeqELement.ProductID() ) + "_STOCK", 
                             Quantity := 1, 
                             IsInput := false );
      }
      // ==== 投入 ====
      // 对Operation中的每个ProcessSection, 找seq最小的list,每条bom对该list生成operationBOM
      processSectionList := selectuniquevalues( toDealOps, Elements, toDeal, 
                                                toDeal.OrganCode() + "_" + toDeal.ProductID() = routingKey,
                                                toDeal.ProcessSection() );
      traverse( processSectionList, Elements, processSection ) {
        minSeq := min( toDealOps, Elements, item, 
                       item.OrganCode() + "_" + item.ProductID() = routingKey 
                       and item.ProcessSection() = processSection,
                       item.SequenceNumber() );
        operationRecordsWithMinSeq := selectset( toDealOps, Elements, minSeqWithProcessSectionElement, 
                                                 minSeqWithProcessSectionElement.OrganCode() + "_" + minSeqWithProcessSectionElement.ProductID() = routingKey 
                                                 and minSeqWithProcessSectionElement.ProcessSection() = processSection
                                                 and minSeqWithProcessSectionElement.SequenceNumber() = minSeq );
        bomRecords := selectset( toDealBoms, Elements, bom, 
                                 bom.OrganCode() + "_" + bom.ProductCode() = routingKey 
                                 and bom.ProcessSection() = processSection );
        // 按是否有替换料分组
        bomWithoutAlters := selectset( bomRecords, Elements, bomRecord, bomRecord.AlternativeMaterialCode() = "" );
        bomWithAlters := selectset( bomRecords, Elements, bomRecord, bomRecord.AlternativeMaterialCode() <> "" );
        // 处理不带替换料的input
        traverse( operationRecordsWithMinSeq, Elements, op ) {
          traverse( bomWithoutAlters, Elements, bom ) {
            operationId := op.OrganCode() + "_" + op.ProductID() + "_" + op.ProcessSection();
            if( op.Line() <> "" ) {
              operationId := operationId + "_" + op.Line();
            }
            this.IOOperationBOM( relnew, 
                                 OperationID := operationId, 
                                 ProductID := op.ProductID(),
                                 StockingPointID := op.OrganCode() + "_" + bom.ComponentType() + "_STOCK", 
                                 IsInput := true, 
                                 Quantity := bom.UnitUsageOfComponents() / bom.ComponentOutputRate() );           
          }
        }
        // 处理带替换料的,按line和主料分组
        lineList := selectuniquevalues( operationRecordsWithMinSeq, Elements, item, item.Line() );
        mainProdList := selectuniquevalues( bomWithAlters, Elements, item, item.ComponentCode() );
        traverse( lineList, Elements, line ) {
          opWithLine := select( operationRecordsWithMinSeq, Elements, item, item.Line() = line );
          opId := opWithLine.OrganCode() + "_" + opWithLine.ProductID() + "_" + opWithLine.ProcessSection();
          if( opWithLine.Line() <> "" ) {
            opId := opId + "_" + opWithLine.Line();
          }
          traverse( mainProdList, Elements, mainProd ) {
            // 每种主料一个group
            inputGroupId := inputGroupId + 1;
            bomWithMainProdList := selectset( bomWithAlters, Elements, item, item.ComponentCode() = mainProd );
            bomRandom := bomWithMainProdList.First();
            // 总数量
            maxQuantityInGroup := bomRandom.UnitUsageOfComponents() / bomRandom.ComponentOutputRate();
            // 替代料总比例
            alterRateTotal := sum( bomWithMainProdList, Elements, item, item.AlternativeRate() );
            // 创建inputGroup
            this.IOOperationInputGroup( relnew, InputGroupID := inputGroupId, OperationID := opId, InputGroupQuantity := maxQuantityInGroup );
            // 创建主料的input bom
            this.IOOperationBOM( relnew, 
                                 OperationID := opId, 
                                 InputGroupID := inputGroupId,
                                 ProductID := bomRandom.ComponentCode(),
                                 StockingPointID := bomRandom.OrganCode() + "_" + bomRandom.ComponentType() + "_STOCK", 
                                 IsInput := true, 
                                 Quantity := maxQuantityInGroup * ( 1 - alterRateTotal ), 
                                 MinQuantityInGroup := 0.0, 
                                 MaxQuantityInGroup := maxQuantityInGroup );  
            // 替代料input bom
            traverse( bomWithMainProdList, Elements, bom ) {
              this.IOOperationBOM( relnew, 
                         OperationID := opId, 
                         InputGroupID := inputGroupId,
                         ProductID := bom.AlternativeMaterialCode(),
                         StockingPointID := bom.OrganCode() + "_" + bom.ComponentType() + "_STOCK", 
                         IsInput := true, 
                         Quantity := maxQuantityInGroup * bom.AlternativeRate(), 
                         MinQuantityInGroup := 0.0, 
                         MaxQuantityInGroup := maxQuantityInGroup );  
            }
          }
        }
      }
      // 针对每个routing 重置 inputGroupId
      inputGroupId := 1;
    }
    // 对每个OrgCode_ComponentCode组合,创建采购routing
    if( createPurchaseSupplyMaterial ) {
      toCreateKeys := selectuniquevalues( toDealBoms, Elements, bom,
                                          bom.ComponentType() = "P", 
                                          bom.OrganCode() + "_" + bom.ComponentCode() );
      traverse( toCreateKeys, Elements, toCreateKey ) {
        bomRandom := selectset( toDealBoms, Elements, bom, bom.ComponentType() = "P" and bom.OrganCode() + "_" + bom.ComponentCode() = toCreateKey ).First();
        routingId := bomRandom.OrganCode() + "_" + bomRandom.ComponentCode();
        routingName := routingId;
        routingStepName := "采购";
        routingStepSeq := 1;
        operationId := "PR_" + bomRandom.OrganCode() + bomRandom.ComponentCode();
        operationName := operationId;
        unitId := "供应商";
        // ==== 创建 ====
        // 创建routing
        this.IORouting( relnew, 
                        ID := routingId, Name := routingName, IsEnabled := true, Start := Date::MinDate(), End := Date::MaxDate() );
        // 创建routingStep
        IORoutingStep::CreateIfNotExist( this, 
                                     routingId,
                                     routingStepName,
                                     routingStepSeq );
        // 创建op
        ioOp := IOOperation::CreateIfNotExist( this, operationId );
        ioOp.UnitID( unitId );
        ioOp.RoutingID( routingId );
        ioOp.RoutingStepName( routingStepName );
        ioOp.Name( operationName );
        ioOp.UserLeadTime( Duration::Zero() );
        ioOp.Throughput( 1.0 );
        ioOp.HasUserMaximumQuantity( false );
        ioOp.UserMinimumQuantity( Real::MinReal() );
        ioOp.UserMaximumQuantity( Real::MaxReal() );
        // 创建opBOM
        this.IOOperationBOM( relnew, 
                   OperationID := operationId, 
                   ProductID := bomRandom.ComponentCode(),
                   StockingPointID := bomRandom.OrganCode() + "_" + bomRandom.ProductType() + "_STOCK", 
                   IsInput := true, 
                   Quantity := 1 );  
      }
    }
  *]
}