|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+package com.zzsmart.qomo.kn.cost.manage.plugin;
|
|
|
2
|
+
|
|
|
3
|
+import com.alibaba.fastjson.JSON;
|
|
|
4
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
5
|
+import com.zzsmart.qomo.dao.entity.FlowDefinition;
|
|
|
6
|
+import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostResultValue;
|
|
|
7
|
+import com.zzsmart.qomo.kn.cost.manage.entity.CostMaterialBom;
|
|
|
8
|
+import com.zzsmart.qomo.kn.cost.manage.entity.CostMaterialProcessHours;
|
|
|
9
|
+import com.zzsmart.qomo.kn.cost.manage.entity.HourRate;
|
|
|
10
|
+import com.zzsmart.qomo.kn.cost.manage.enums.FeeTypeEnum;
|
|
|
11
|
+import com.zzsmart.qomo.kn.cost.manage.mapper.CostMaterialBomMapper;
|
|
|
12
|
+import com.zzsmart.qomo.kn.cost.manage.service.CostMaterialProcessHoursService;
|
|
|
13
|
+import com.zzsmart.qomo.kn.cost.manage.service.CostPurchasePriceService;
|
|
|
14
|
+import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostResultValueService;
|
|
|
15
|
+import com.zzsmart.qomo.kn.cost.manage.service.IHourRateService;
|
|
|
16
|
+import com.zzsmart.qomo.kn.cost.manage.vo.FlowTaskInfoVO;
|
|
|
17
|
+import com.zzsmart.qomo.plugin.task.api.TaskExecutionContext;
|
|
|
18
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
19
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
20
|
+import org.springframework.stereotype.Service;
|
|
|
21
|
+
|
|
|
22
|
+import javax.annotation.PostConstruct;
|
|
|
23
|
+import java.math.BigDecimal;
|
|
|
24
|
+import java.util.ArrayList;
|
|
|
25
|
+import java.util.List;
|
|
|
26
|
+import java.util.Map;
|
|
|
27
|
+import java.util.stream.Collectors;
|
|
|
28
|
+
|
|
|
29
|
+@Service
|
|
|
30
|
+@Slf4j
|
|
|
31
|
+public class StandardCostService {
|
|
|
32
|
+ /**
|
|
|
33
|
+ * 物料BOM信息
|
|
|
34
|
+ */
|
|
|
35
|
+ @Autowired
|
|
|
36
|
+ CostMaterialBomMapper costMaterialBomMapper;
|
|
|
37
|
+
|
|
|
38
|
+ /**
|
|
|
39
|
+ * 小时费率
|
|
|
40
|
+ */
|
|
|
41
|
+ @Autowired
|
|
|
42
|
+ IHourRateService iHourRateService;
|
|
|
43
|
+ /**
|
|
|
44
|
+ * 采购单价
|
|
|
45
|
+ */
|
|
|
46
|
+ @Autowired
|
|
|
47
|
+ CostPurchasePriceService costPurchasePriceService;
|
|
|
48
|
+ /**
|
|
|
49
|
+ * 工序
|
|
|
50
|
+ */
|
|
|
51
|
+ @Autowired
|
|
|
52
|
+ CostMaterialProcessHoursService costMaterialProcessHoursService;
|
|
|
53
|
+ /**
|
|
|
54
|
+ * 标准成本计算结果
|
|
|
55
|
+ */
|
|
|
56
|
+ @Autowired
|
|
|
57
|
+ IAppSceneCostResultValueService iAppSceneCostResultValueService;
|
|
|
58
|
+ public static StandardCostService standardCostService;
|
|
|
59
|
+
|
|
|
60
|
+ @PostConstruct
|
|
|
61
|
+ public void init() {
|
|
|
62
|
+ standardCostService = this;
|
|
|
63
|
+ standardCostService.costMaterialBomMapper = this.costMaterialBomMapper;
|
|
|
64
|
+ standardCostService.costPurchasePriceService = this.costPurchasePriceService;
|
|
|
65
|
+ standardCostService.iHourRateService = this.iHourRateService;
|
|
|
66
|
+ standardCostService.costMaterialProcessHoursService = this.costMaterialProcessHoursService;
|
|
|
67
|
+ standardCostService.iAppSceneCostResultValueService = this.iAppSceneCostResultValueService;
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+
|
|
|
71
|
+ /**
|
|
|
72
|
+ * 1.物料编号输入任务
|
|
|
73
|
+ */
|
|
|
74
|
+ public static void materialInputTask(TaskExecutionContext parameters) {
|
|
|
75
|
+ try {
|
|
|
76
|
+ //获取任务流程相关信息
|
|
|
77
|
+ FlowTaskInfoVO flowTaskInfoVO = analysisFlowTaskInfo(parameters);
|
|
|
78
|
+ //获取最上层的物料号
|
|
|
79
|
+ String topMaterialCode = flowTaskInfoVO.getTopMaterialCode();
|
|
|
80
|
+ //获取任务类型
|
|
|
81
|
+ String taskType = parameters.getTaskType();
|
|
|
82
|
+ //获取任务代码
|
|
|
83
|
+ String taskCode = flowTaskInfoVO.getTaskCode();
|
|
|
84
|
+ //获取流程id
|
|
|
85
|
+ String flowInstanceId = flowTaskInfoVO.getFlowInstanceId();
|
|
|
86
|
+ //1.查询出所有满足物料编号的物料(BOM物料)
|
|
|
87
|
+ String sql = flowTaskInfoVO.getSqlString();
|
|
|
88
|
+ List<CostMaterialBom> list = standardCostService.costMaterialBomMapper.customSelect(sql);
|
|
|
89
|
+ log.info("Full EmptyTask list: {}", list);
|
|
|
90
|
+ //2.把查询的结果存放在临时表里(表命名规则:前缀_流程id_组件code)
|
|
|
91
|
+ for (int i = 0; list != null && i < list.size(); i++) {
|
|
|
92
|
+ CostMaterialBom material = list.get(i);
|
|
|
93
|
+ AppSceneCostResultValue appSceneCostResultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(material.getMaterialCode()).parentMarterialNo(material.getParentMaterialCode()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).build();
|
|
|
94
|
+ standardCostService.iAppSceneCostResultValueService.save(appSceneCostResultValue);
|
|
|
95
|
+ }
|
|
|
96
|
+ } catch (Exception e) {
|
|
|
97
|
+ throw new RuntimeException(e);
|
|
|
98
|
+ }
|
|
|
99
|
+ }
|
|
|
100
|
+
|
|
|
101
|
+ /**
|
|
|
102
|
+ * 获取任务流程相关信息
|
|
|
103
|
+ *
|
|
|
104
|
+ * @param parameters
|
|
|
105
|
+ * @return
|
|
|
106
|
+ */
|
|
|
107
|
+ private static FlowTaskInfoVO analysisFlowTaskInfo(TaskExecutionContext parameters) {
|
|
|
108
|
+ FlowTaskInfoVO flowTaskInfoVO = new FlowTaskInfoVO();
|
|
|
109
|
+ if (parameters != null) {
|
|
|
110
|
+ String s = parameters.toString();
|
|
|
111
|
+ String[] split = s.split(",");
|
|
|
112
|
+ for (int i = 0; i < split.length; i++) {
|
|
|
113
|
+ String s1 = split[i];
|
|
|
114
|
+ if (s1.contains("taskType")) {
|
|
|
115
|
+ String[] split1 = s1.split("=");
|
|
|
116
|
+ flowTaskInfoVO.setTaskType(split1[1]);
|
|
|
117
|
+ } else if (s1.contains("taskCode")) {
|
|
|
118
|
+ String[] split1 = s1.split(":");
|
|
|
119
|
+ flowTaskInfoVO.setTaskCode(split1[1]);
|
|
|
120
|
+ } else if (s1.contains("flowInstanceId")) {
|
|
|
121
|
+ String[] split1 = s1.split("=");
|
|
|
122
|
+ flowTaskInfoVO.setFlowInstanceId(split1[1]);
|
|
|
123
|
+ } else if (s1.contains("sqlString")) {
|
|
|
124
|
+ String[] split1 = s1.split(":");
|
|
|
125
|
+ flowTaskInfoVO.setSqlString(split1[1]);
|
|
|
126
|
+ }
|
|
|
127
|
+ }
|
|
|
128
|
+ }
|
|
|
129
|
+ //通过任务流程id查询流程定义信息(qomo_flow_definition)
|
|
|
130
|
+ flowTaskInfoVO = queryFlowDefinitionByFlowInstanceId(flowTaskInfoVO);
|
|
|
131
|
+ return flowTaskInfoVO;
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ /**
|
|
|
135
|
+ * @param flowTaskInfoVO
|
|
|
136
|
+ */
|
|
|
137
|
+ private static FlowTaskInfoVO queryFlowDefinitionByFlowInstanceId(FlowTaskInfoVO flowTaskInfoVO) {
|
|
|
138
|
+ String sql = "SELECT A.id,A.`code`,A.`name`,A.version,A.release_state,A.model_json from qomo_flow_definition as A LEFT JOIN qomo_flow_instance AS B ON A.`code`=B.flow_definition_code WHERE B.id=" + flowTaskInfoVO.getFlowInstanceId() + " limit 1";
|
|
|
139
|
+ JSONObject jsonObject = standardCostService.costMaterialBomMapper.customSelectOne(sql);
|
|
|
140
|
+ FlowDefinition flowDefinition = JSON.toJavaObject(jsonObject, FlowDefinition.class);
|
|
|
141
|
+ if (flowDefinition != null) {
|
|
|
142
|
+ log.info("flowDefinition: {}", flowDefinition);
|
|
|
143
|
+ String modelJson = flowDefinition.getModelJson();
|
|
|
144
|
+ JSONObject jsonObject1 = JSON.parseObject(modelJson);
|
|
|
145
|
+ if (jsonObject1 != null) {
|
|
|
146
|
+ String topMaterialCode = jsonObject1.getString("topMaterialCode");
|
|
|
147
|
+ flowTaskInfoVO.setTopMaterialCode(topMaterialCode);
|
|
|
148
|
+ }
|
|
|
149
|
+ }
|
|
|
150
|
+ return flowTaskInfoVO;
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ /**
|
|
|
154
|
+ * 2. 物料成本任务
|
|
|
155
|
+ */
|
|
|
156
|
+ public static void materialCostTask(TaskExecutionContext parameters) {
|
|
|
157
|
+ try {
|
|
|
158
|
+ //获取任务流程相关信息
|
|
|
159
|
+ FlowTaskInfoVO flowTaskInfoVO = analysisFlowTaskInfo(parameters);
|
|
|
160
|
+ //获取最上层的物料号
|
|
|
161
|
+ String topMaterialCode = flowTaskInfoVO.getTopMaterialCode();
|
|
|
162
|
+ //获取任务类型
|
|
|
163
|
+ String taskType = parameters.getTaskType();
|
|
|
164
|
+ //获取任务代码
|
|
|
165
|
+ String taskCode = flowTaskInfoVO.getTaskCode();
|
|
|
166
|
+ //获取流程id
|
|
|
167
|
+ String flowInstanceId = flowTaskInfoVO.getFlowInstanceId();
|
|
|
168
|
+ //1.查询出物料编号对应的下所有的BOM信息
|
|
|
169
|
+ List<AppSceneCostResultValue> list = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, "MaterialInput", flowInstanceId);
|
|
|
170
|
+ //2.计算出所有的单个物料的物料成本
|
|
|
171
|
+ //2.1查询出物料的单价和采购数量
|
|
|
172
|
+ List<String> MarterialNoList = new ArrayList<>();
|
|
|
173
|
+ if (list != null && list.size() > 0) {
|
|
|
174
|
+ MarterialNoList = list.stream().filter(s -> s.getMarterialNo() != null).map(AppSceneCostResultValue::getMarterialNo).collect(Collectors.toList());
|
|
|
175
|
+ }
|
|
|
176
|
+ //查询物料的采购价
|
|
|
177
|
+ Map<String, BigDecimal> purchasePriceMap = standardCostService.costPurchasePriceService.getMinCostPurchasePriceByMaterialNo(MarterialNoList);
|
|
|
178
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
179
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
180
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
181
|
+ Integer num = appSceneCostResultValue.getNum();
|
|
|
182
|
+ if (marterialNo != null) {
|
|
|
183
|
+ BigDecimal price = purchasePriceMap.get(marterialNo);
|
|
|
184
|
+ BigDecimal number = new BigDecimal(num.toString());
|
|
|
185
|
+ //单价乘以数量
|
|
|
186
|
+ BigDecimal totalCost = price.multiply(number);
|
|
|
187
|
+ //2.2计算出人工成本并存储到成本结果数据表中
|
|
|
188
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
189
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
190
|
+ }
|
|
|
191
|
+ }
|
|
|
192
|
+ } catch (Exception e) {
|
|
|
193
|
+ throw new RuntimeException(e);
|
|
|
194
|
+ }
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ /**
|
|
|
198
|
+ * 3.人工成本任务
|
|
|
199
|
+ */
|
|
|
200
|
+ public static void laborCostTask(TaskExecutionContext parameters) {
|
|
|
201
|
+ try {
|
|
|
202
|
+ //获取任务流程相关信息
|
|
|
203
|
+ FlowTaskInfoVO flowTaskInfoVO = analysisFlowTaskInfo(parameters);
|
|
|
204
|
+ //获取最上层的物料号
|
|
|
205
|
+ String topMaterialCode = flowTaskInfoVO.getTopMaterialCode();
|
|
|
206
|
+ //获取任务类型
|
|
|
207
|
+ String taskType = parameters.getTaskType();
|
|
|
208
|
+ //获取任务代码
|
|
|
209
|
+ String taskCode = flowTaskInfoVO.getTaskCode();
|
|
|
210
|
+ //获取流程id
|
|
|
211
|
+ String flowInstanceId = flowTaskInfoVO.getFlowInstanceId();
|
|
|
212
|
+ String sql = flowTaskInfoVO.getSqlString();
|
|
|
213
|
+ JSONObject jsonObject = standardCostService.costMaterialBomMapper.customSelectOne(sql);
|
|
|
214
|
+ BigDecimal hourRate = new BigDecimal("0");
|
|
|
215
|
+ if (jsonObject != null) {
|
|
|
216
|
+ HourRate rate = JSON.toJavaObject(jsonObject, HourRate.class);
|
|
|
217
|
+ hourRate = rate.getLaborHourRate();
|
|
|
218
|
+ }
|
|
|
219
|
+ //1.查询出物料编号对应的下所有的BOM信息
|
|
|
220
|
+ List<AppSceneCostResultValue> list = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, "MaterialInput", flowInstanceId);
|
|
|
221
|
+ //2.计算出所有的单个物料的人工成本
|
|
|
222
|
+ //2.1查询出物料的人员工时和人工小时费率(涉及:小时费率表和工序表)
|
|
|
223
|
+ List<String> MarterialNoList = new ArrayList<>();
|
|
|
224
|
+ if (list != null && list.size() > 0) {
|
|
|
225
|
+ MarterialNoList = list.stream().filter(s -> s.getMarterialNo() != null).map(AppSceneCostResultValue::getMarterialNo).collect(Collectors.toList());
|
|
|
226
|
+ }
|
|
|
227
|
+ //查询工序工时
|
|
|
228
|
+ Map<String, List<CostMaterialProcessHours>> processHoursMap = standardCostService.costMaterialProcessHoursService.getProcessHoursByMaterialNo(MarterialNoList);
|
|
|
229
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
230
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
231
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
232
|
+ if (marterialNo != null) {
|
|
|
233
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
234
|
+ List<CostMaterialProcessHours> costMaterialProcessHours = processHoursMap.get(marterialNo);
|
|
|
235
|
+ if (costMaterialProcessHours != null && costMaterialProcessHours.size() > 0) {
|
|
|
236
|
+ for (int j = 0; j < costMaterialProcessHours.size(); j++) {
|
|
|
237
|
+ CostMaterialProcessHours processHours = costMaterialProcessHours.get(j);
|
|
|
238
|
+ BigDecimal laborHours = processHours.getLaborHours();
|
|
|
239
|
+ //计算总人工成本
|
|
|
240
|
+ totalCost = totalCost.add(laborHours.multiply(hourRate));
|
|
|
241
|
+ }
|
|
|
242
|
+ }
|
|
|
243
|
+ //2.2计算出人工成本并存储到成本结果数据表中
|
|
|
244
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
245
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
246
|
+ }
|
|
|
247
|
+ }
|
|
|
248
|
+ } catch (Exception e) {
|
|
|
249
|
+ throw new RuntimeException(e);
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+ }
|
|
|
253
|
+
|
|
|
254
|
+ /**
|
|
|
255
|
+ * 4.制造费用成本任务
|
|
|
256
|
+ */
|
|
|
257
|
+ public static void manufacturingCostTask(TaskExecutionContext parameters) {
|
|
|
258
|
+ try {
|
|
|
259
|
+ //获取任务流程相关信息
|
|
|
260
|
+ FlowTaskInfoVO flowTaskInfoVO = analysisFlowTaskInfo(parameters);
|
|
|
261
|
+ //获取最上层的物料号
|
|
|
262
|
+ String topMaterialCode = flowTaskInfoVO.getTopMaterialCode();
|
|
|
263
|
+ //获取任务类型
|
|
|
264
|
+ String taskType = parameters.getTaskType();
|
|
|
265
|
+ //获取任务代码
|
|
|
266
|
+ String taskCode = flowTaskInfoVO.getTaskCode();
|
|
|
267
|
+ //获取流程id
|
|
|
268
|
+ String flowInstanceId = flowTaskInfoVO.getFlowInstanceId();
|
|
|
269
|
+ /**
|
|
|
270
|
+ * 费用类别
|
|
|
271
|
+ */
|
|
|
272
|
+ String feeType = flowTaskInfoVO.getFeeType();
|
|
|
273
|
+ /**
|
|
|
274
|
+ * 小时费率
|
|
|
275
|
+ */
|
|
|
276
|
+ String sql = flowTaskInfoVO.getSqlString();
|
|
|
277
|
+ JSONObject jsonObject = standardCostService.costMaterialBomMapper.customSelectOne(sql);
|
|
|
278
|
+ BigDecimal hourRate = new BigDecimal("0");
|
|
|
279
|
+ if (jsonObject != null) {
|
|
|
280
|
+ HourRate rate = JSON.toJavaObject(jsonObject, HourRate.class);
|
|
|
281
|
+ hourRate = rate.getLaborHourRate();
|
|
|
282
|
+ }
|
|
|
283
|
+ //1.查询出物料编号对应的下所有的BOM信息
|
|
|
284
|
+ List<AppSceneCostResultValue> list = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, "MaterialInput", flowInstanceId);
|
|
|
285
|
+ //2.根据费用类型进行不同的费用计算
|
|
|
286
|
+ //查询工序工时
|
|
|
287
|
+ List<String> MarterialNoList = new ArrayList<>();
|
|
|
288
|
+ if (list != null && list.size() > 0) {
|
|
|
289
|
+ MarterialNoList = list.stream().filter(s -> s.getMarterialNo() != null).map(AppSceneCostResultValue::getMarterialNo).collect(Collectors.toList());
|
|
|
290
|
+ }
|
|
|
291
|
+ Map<String, List<CostMaterialProcessHours>> processHoursMap = standardCostService.costMaterialProcessHoursService.getProcessHoursByMaterialNo(MarterialNoList);
|
|
|
292
|
+ //2.1机器折旧费(机器工时*费率)
|
|
|
293
|
+ if (FeeTypeEnum.EquipmentFee.getCode().equals(feeType)) {
|
|
|
294
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
295
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
296
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
297
|
+ if (marterialNo != null) {
|
|
|
298
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
299
|
+ List<CostMaterialProcessHours> costMaterialProcessHours = processHoursMap.get(marterialNo);
|
|
|
300
|
+ if (costMaterialProcessHours != null && costMaterialProcessHours.size() > 0) {
|
|
|
301
|
+ for (int j = 0; j < costMaterialProcessHours.size(); j++) {
|
|
|
302
|
+ CostMaterialProcessHours processHours = costMaterialProcessHours.get(j);
|
|
|
303
|
+ BigDecimal laborHours = processHours.getDeviceHours();
|
|
|
304
|
+ //计算机器成本
|
|
|
305
|
+ totalCost = totalCost.add(laborHours.multiply(hourRate));
|
|
|
306
|
+ }
|
|
|
307
|
+ }
|
|
|
308
|
+ //2.2计算机器成本并存储到成本结果数据表中
|
|
|
309
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
310
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
311
|
+ }
|
|
|
312
|
+ }
|
|
|
313
|
+ } else if (FeeTypeEnum.SupplyMaterialFee.getCode().equals(feeType)) {
|
|
|
314
|
+ //机物料消耗(辅料工时*费率)
|
|
|
315
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
316
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
317
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
318
|
+ if (marterialNo != null) {
|
|
|
319
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
320
|
+ List<CostMaterialProcessHours> costMaterialProcessHours = processHoursMap.get(marterialNo);
|
|
|
321
|
+ if (costMaterialProcessHours != null && costMaterialProcessHours.size() > 0) {
|
|
|
322
|
+ for (int j = 0; j < costMaterialProcessHours.size(); j++) {
|
|
|
323
|
+ CostMaterialProcessHours processHours = costMaterialProcessHours.get(j);
|
|
|
324
|
+ //TODO 辅料工时来源
|
|
|
325
|
+ BigDecimal laborHours = processHours.getLaborHours();
|
|
|
326
|
+ //机物料消耗
|
|
|
327
|
+ totalCost = totalCost.add(laborHours.multiply(hourRate));
|
|
|
328
|
+ }
|
|
|
329
|
+ }
|
|
|
330
|
+ //2.2计算机物料消耗存储到成本结果数据表中
|
|
|
331
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
332
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
333
|
+ }
|
|
|
334
|
+ }
|
|
|
335
|
+ } else if (FeeTypeEnum.DriverFee.getCode().equals(feeType)) {
|
|
|
336
|
+ //2.3水电费(燃动力工时*费率)
|
|
|
337
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
338
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
339
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
340
|
+ if (marterialNo != null) {
|
|
|
341
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
342
|
+ List<CostMaterialProcessHours> costMaterialProcessHours = processHoursMap.get(marterialNo);
|
|
|
343
|
+ if (costMaterialProcessHours != null && costMaterialProcessHours.size() > 0) {
|
|
|
344
|
+ for (int j = 0; j < costMaterialProcessHours.size(); j++) {
|
|
|
345
|
+ CostMaterialProcessHours processHours = costMaterialProcessHours.get(j);
|
|
|
346
|
+ //TODO 燃动力工时来源
|
|
|
347
|
+ BigDecimal laborHours = processHours.getLaborHours();
|
|
|
348
|
+ //机物料消耗
|
|
|
349
|
+ totalCost = totalCost.add(laborHours.multiply(hourRate));
|
|
|
350
|
+ }
|
|
|
351
|
+ }
|
|
|
352
|
+ //2.2计算机物料消耗存储到成本结果数据表中
|
|
|
353
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
354
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
355
|
+ }
|
|
|
356
|
+ }
|
|
|
357
|
+ } else if (FeeTypeEnum.OtherFee.getCode().equals(feeType)) {
|
|
|
358
|
+ //2.4其他制费(其他工时*费率)
|
|
|
359
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
360
|
+ AppSceneCostResultValue appSceneCostResultValue = list.get(i);
|
|
|
361
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
362
|
+ if (marterialNo != null) {
|
|
|
363
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
364
|
+ List<CostMaterialProcessHours> costMaterialProcessHours = processHoursMap.get(marterialNo);
|
|
|
365
|
+ if (costMaterialProcessHours != null && costMaterialProcessHours.size() > 0) {
|
|
|
366
|
+ for (int j = 0; j < costMaterialProcessHours.size(); j++) {
|
|
|
367
|
+ CostMaterialProcessHours processHours = costMaterialProcessHours.get(j);
|
|
|
368
|
+ //TODO 其他工时来源
|
|
|
369
|
+ BigDecimal laborHours = processHours.getLaborHours();
|
|
|
370
|
+ //机物料消耗
|
|
|
371
|
+ totalCost = totalCost.add(laborHours.multiply(hourRate));
|
|
|
372
|
+ }
|
|
|
373
|
+ }
|
|
|
374
|
+ //2.2计算机物料消耗存储到成本结果数据表中
|
|
|
375
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(marterialNo).parentMarterialNo(appSceneCostResultValue.getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost + "").build();
|
|
|
376
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
377
|
+ }
|
|
|
378
|
+ }
|
|
|
379
|
+ } else if (FeeTypeEnum.LogisticsFee.getCode().equals(feeType)) {
|
|
|
380
|
+ //查出所有的机器折旧费、机物料消耗、水电费、其他制费
|
|
|
381
|
+ list = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, "ManufacturingCost", flowInstanceId);
|
|
|
382
|
+ //2.5物流费((机器折旧+机物料消耗+水电费+其他制费)*10.16%)
|
|
|
383
|
+ //按照物料编号进行分组
|
|
|
384
|
+ Map<String, List<AppSceneCostResultValue>> materialNoMap = list.stream().collect(Collectors.groupingBy(AppSceneCostResultValue::getMarterialNo));
|
|
|
385
|
+ for (String key : materialNoMap.keySet()) {
|
|
|
386
|
+ List<AppSceneCostResultValue> appSceneCostResultValues = materialNoMap.get(key);
|
|
|
387
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
388
|
+ for (int j = 0; j < appSceneCostResultValues.size(); j++) {
|
|
|
389
|
+ AppSceneCostResultValue appSceneCostResultValue = appSceneCostResultValues.get(j);
|
|
|
390
|
+ String countValue = appSceneCostResultValue.getCountValue();
|
|
|
391
|
+ if (countValue != null) {
|
|
|
392
|
+ totalCost = totalCost.add(new BigDecimal(countValue));
|
|
|
393
|
+ }
|
|
|
394
|
+ }
|
|
|
395
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(key).parentMarterialNo(appSceneCostResultValues.get(0).getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(totalCost.multiply(new BigDecimal("0.1016")).toString()).build();
|
|
|
396
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
397
|
+ }
|
|
|
398
|
+ }
|
|
|
399
|
+ } catch (Exception e) {
|
|
|
400
|
+ throw new RuntimeException(e);
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+ }
|
|
|
404
|
+
|
|
|
405
|
+ /**
|
|
|
406
|
+ * 5.标准成本计算任务
|
|
|
407
|
+ */
|
|
|
408
|
+ public static void standardCostTask(TaskExecutionContext parameters) {
|
|
|
409
|
+ //获取任务流程相关信息
|
|
|
410
|
+ FlowTaskInfoVO flowTaskInfoVO = analysisFlowTaskInfo(parameters);
|
|
|
411
|
+ //获取最上层的物料号
|
|
|
412
|
+ String topMaterialCode = flowTaskInfoVO.getTopMaterialCode();
|
|
|
413
|
+ //获取任务类型
|
|
|
414
|
+ String taskType = parameters.getTaskType();
|
|
|
415
|
+ //获取任务代码
|
|
|
416
|
+ String taskCode = flowTaskInfoVO.getTaskCode();
|
|
|
417
|
+ //获取流程id
|
|
|
418
|
+ String flowInstanceId = flowTaskInfoVO.getFlowInstanceId();
|
|
|
419
|
+ //1.查询出物料编号对应的下所有的BOM信息
|
|
|
420
|
+ List<AppSceneCostResultValue> list = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, null, flowInstanceId);
|
|
|
421
|
+ //2.计算出所有的单个物料的本阶的标准成本(标准成本=物料成本+人工成本+制造费用)
|
|
|
422
|
+ //按照物料编号进行分组
|
|
|
423
|
+ Map<String, List<AppSceneCostResultValue>> materialNoMap = list.stream().collect(Collectors.groupingBy(AppSceneCostResultValue::getMarterialNo));
|
|
|
424
|
+ for (String key : materialNoMap.keySet()) {
|
|
|
425
|
+ List<AppSceneCostResultValue> appSceneCostResultValues = materialNoMap.get(key);
|
|
|
426
|
+ //本阶成本
|
|
|
427
|
+ BigDecimal currentCost = new BigDecimal("0.0");
|
|
|
428
|
+ for (int j = 0; j < appSceneCostResultValues.size(); j++) {
|
|
|
429
|
+ AppSceneCostResultValue appSceneCostResultValue = appSceneCostResultValues.get(j);
|
|
|
430
|
+ String countValue = appSceneCostResultValue.getCountValue();
|
|
|
431
|
+ if (countValue != null) {
|
|
|
432
|
+ currentCost = currentCost.add(new BigDecimal(countValue));
|
|
|
433
|
+ }
|
|
|
434
|
+ }
|
|
|
435
|
+ AppSceneCostResultValue resultValue = AppSceneCostResultValue.builder().belongTopMaterialNo(topMaterialCode).marterialNo(key).parentMarterialNo(appSceneCostResultValues.get(0).getParentMarterialNo()).taskType(taskType).taskCode(taskCode).flowInstanceId(flowInstanceId).countValue(currentCost.toString()).build();
|
|
|
436
|
+ standardCostService.iAppSceneCostResultValueService.save(resultValue);
|
|
|
437
|
+ }
|
|
|
438
|
+ //3.计算出所有的单个物料的累计标准成本
|
|
|
439
|
+ List<AppSceneCostResultValue> standardCostList = standardCostService.iAppSceneCostResultValueService.getResultValueByTopMaterialNo(topMaterialCode, taskType, flowInstanceId);
|
|
|
440
|
+ //循环算出各个物料的累计成本
|
|
|
441
|
+ totalCountCost(standardCostList, topMaterialCode, taskType, taskCode, flowInstanceId);
|
|
|
442
|
+ }
|
|
|
443
|
+
|
|
|
444
|
+ /**
|
|
|
445
|
+ * 循环算出各个物料的累计成本
|
|
|
446
|
+ *
|
|
|
447
|
+ * @param standardCostList
|
|
|
448
|
+ * @param topMaterialCode
|
|
|
449
|
+ * @param taskType
|
|
|
450
|
+ * @param taskCode
|
|
|
451
|
+ * @param flowInstanceId
|
|
|
452
|
+ */
|
|
|
453
|
+ private static void totalCountCost(List<AppSceneCostResultValue> standardCostList, String topMaterialCode, String taskType, String taskCode, String flowInstanceId) {
|
|
|
454
|
+ Map<String, List<AppSceneCostResultValue>> parentMaterialNoMap = standardCostList.stream().filter(appSceneCostResultValue -> appSceneCostResultValue.getParentMarterialNo() != null).collect(Collectors.groupingBy(AppSceneCostResultValue::getParentMarterialNo));
|
|
|
455
|
+ for (int i = 0; i < standardCostList.size(); i++) {
|
|
|
456
|
+ AppSceneCostResultValue appSceneCostResultValue = standardCostList.get(i);
|
|
|
457
|
+ BigDecimal totalCost = new BigDecimal("0.0");
|
|
|
458
|
+ totalCost = loopCount(appSceneCostResultValue, parentMaterialNoMap, topMaterialCode, taskType, taskCode, flowInstanceId, totalCost);
|
|
|
459
|
+ appSceneCostResultValue.setTotalValue(totalCost.toString());
|
|
|
460
|
+ standardCostService.iAppSceneCostResultValueService.save(appSceneCostResultValue);
|
|
|
461
|
+ }
|
|
|
462
|
+ }
|
|
|
463
|
+
|
|
|
464
|
+ /**
|
|
|
465
|
+ * 递归计算出累计成本
|
|
|
466
|
+ *
|
|
|
467
|
+ * @param appSceneCostResultValue
|
|
|
468
|
+ * @param parentMaterialNoMap
|
|
|
469
|
+ * @param topMaterialCode
|
|
|
470
|
+ * @param taskType
|
|
|
471
|
+ * @param taskCode
|
|
|
472
|
+ * @param flowInstanceId
|
|
|
473
|
+ */
|
|
|
474
|
+ private static BigDecimal loopCount(AppSceneCostResultValue appSceneCostResultValue, Map<String, List<AppSceneCostResultValue>> parentMaterialNoMap, String topMaterialCode, String taskType, String taskCode, String flowInstanceId, BigDecimal totalCost) {
|
|
|
475
|
+ String marterialNo = appSceneCostResultValue.getMarterialNo();
|
|
|
476
|
+ if (parentMaterialNoMap.containsKey(marterialNo)) {
|
|
|
477
|
+ List<AppSceneCostResultValue> appSceneCostResultValues = parentMaterialNoMap.get(marterialNo);
|
|
|
478
|
+ for (int j = 0; j < appSceneCostResultValues.size(); j++) {
|
|
|
479
|
+ AppSceneCostResultValue appSceneCostResultValue1 = appSceneCostResultValues.get(j);
|
|
|
480
|
+ String countValue = appSceneCostResultValue1.getCountValue();
|
|
|
481
|
+ if (countValue != null) {
|
|
|
482
|
+ totalCost = totalCost.add(new BigDecimal(countValue));
|
|
|
483
|
+ }
|
|
|
484
|
+ loopCount(appSceneCostResultValue1, parentMaterialNoMap, topMaterialCode, taskType, taskCode, flowInstanceId, totalCost);
|
|
|
485
|
+ }
|
|
|
486
|
+ }
|
|
|
487
|
+ return totalCost;
|
|
|
488
|
+ }
|
|
|
489
|
+}
|