Blame view

src/main/java/com/huaheng/pc/u8/service/ICSShipmentApiService.java 41.7 KB
1
2
package com.huaheng.pc.u8.service;
tongzhonghao authored
3
import com.alibaba.fastjson.JSON;
4
5
6
7
8
9
10
11
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.api.general.service.ShipmentApiService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
tongzhonghao authored
12
import com.huaheng.common.utils.http.HttpUtils;
13
import com.huaheng.framework.web.domain.AjaxResult;
14
import com.huaheng.pc.config.company.domain.CompanyU8;
15
import com.huaheng.pc.config.company.service.CompanyService;
16
import com.huaheng.pc.config.company.service.CompanyU8Service;
17
18
19
20
21
import com.huaheng.pc.config.documentWarning.service.IDocumentWarningService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
22
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
tongzhonghao authored
23
24
import com.huaheng.pc.referenceCode.domain.U8Reference;
import com.huaheng.pc.referenceCode.service.U8ReferenceService;
25
26
27
28
29
30
import com.huaheng.pc.shipment.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
tongzhonghao authored
31
32
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
33
34
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
tongzhonghao authored
35
import com.huaheng.pc.u8.domain.*;
36
37
38
39
40
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
41
42
import java.util.*;
import java.util.stream.Collectors;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

@Service
public class ICSShipmentApiService {

    @Resource
    private IDocumentWarningService documentWarningService;
    @Resource
    private WarehouseU8Service warehouseU8Service;
    @Resource
    private ShipmentApiService shipmentApiService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ICSShipmentDetailService icsShipmentDetailService;
    @Resource
    private ICSShipmentHeaderService icsShipmentHeaderService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Resource
    private TaskDetailService taskDetailService;
67
68
    @Resource
    private CompanyU8Service companyU8Service;
tongzhonghao authored
69
70
71
72
    @Resource
    private ICSReceiptAPIService icsReceiptAPIService;
    @Resource
    private IUserService userService;
tongzhonghao authored
73
74
    @Resource
    private U8ReferenceService u8ReferenceService;
75
76
    @Resource
    private CompanyService companyService;
77
78
79
80
81
82
83
84




        //u8-wms出库下发
    @Transactional
    public AjaxResult procureShipment(ICSShipmentModel icsShipmentModel){
    //获取主单和子单列
tongzhonghao authored
85
        icsShipmentModel = assign(icsShipmentModel);
86
87
        ICSShipmentHeader icsShipmentHeader=icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails=icsShipmentModel.getIcsShipmentDetails();
88
89
90
91
        String currentWarehouseCode = icsShipmentHeader.getWarehouseCode();
        if(StringUtils.isEmpty(currentWarehouseCode)){
            return AjaxResult.error("仓库编码不能为空");
        }
tongzhonghao authored
92
        boolean isFlag = "0".equals(icsShipmentHeader.getIsNew()) ? true : false;
93
        String type = icsShipmentHeader.getType();
tongzhonghao authored
94
        String typeName = getShipmentTypeName(type);
95
96
97
98
99
100
101
        switch (type){
            case "QO" :
            case "MO" :
            case "OO" :
            case "PKO" :
            case "TO" :
                break;
tongzhonghao authored
102
103
            default:
                return AjaxResult.error("出库类型不匹配");
104
105
106
107
        }
        if(StringUtils.isEmpty(icsShipmentHeader.getIsNew())){
            return AjaxResult.error("isNew为空");
        }
108
109
110
111
112
113
        String u8WarehouseCode = icsShipmentHeader.getStock();
        String u8CompanyCode = icsShipmentHeader.getCompanyCode();
        //判断U8货主
        CompanyU8 companyU8 = companyU8Service.getCompanyByU8CodeWarehouseCode(u8CompanyCode, currentWarehouseCode);
        if(companyU8==null){
            return AjaxResult.error("系统没有此货主:【"+u8CompanyCode+"】");
114
        }
115
116
        if(StringUtils.isEmpty(u8WarehouseCode)){
            return AjaxResult.error("仓库编码为空:【"+u8WarehouseCode+"】");
117
        }
118
        companyService.checkwarehouseCompany(companyU8.getCompanyCode());
119
120
121
        //通过u8的货主和仓库匹配wms的仓库
        WarehouseU8 warehouseU8 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                .eq(WarehouseU8::getUCompanyCode, icsShipmentHeader.getCompanyCode())
122
                .eq(WarehouseU8::getUWarehouseCode, u8WarehouseCode));
123
124
125
        if (warehouseU8 == null) {
            return AjaxResult.error("系统没有此仓库");
        }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsShipmentHeader.getReferCode())
                .eq(U8Reference::getReferType, icsShipmentHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
        //入库单表 检查上游单号是否重复
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, icsShipmentHeader.getReferCode()));
        if(!isFlag){
            if(one != null && shipmentHeader1 != null){
                return AjaxResult.error(":请不要重复下发 "+icsShipmentHeader.getReferCode());
            }
        }
        checkShipmentBill(isFlag,icsShipmentHeader.getReferCode());
140
141
142
        //根据icsShipmentHeader生成shipmentHeader
        ShipmentHeader shipmentHeader=new ShipmentHeader();
143
144
        String maker = icsShipmentHeader.getCMaker();
        shipmentHeader.setShipmentType(type);
145
146
        shipmentHeader.setReferCode(icsShipmentHeader.getReferCode());
        shipmentHeader.setReferId(icsShipmentHeader.getReferId());
147
148
149
        shipmentHeader.setId(null);
        shipmentHeader.setLastUpdatedBy(maker);
        shipmentHeader.setCreatedBy(maker);
150
        shipmentHeader.setCreated(icsShipmentHeader.getCreated());
151
152
153
        shipmentHeader.setCustomerCode(icsShipmentHeader.getCCusCode());
        shipmentHeader.setShipmentNote(icsShipmentHeader.getCMemo());
        shipmentHeader.setWarehouseCode(warehouseU8.getWarehouseCode());
154
        shipmentHeader.setUWarehouseCode(icsShipmentHeader.getStock());
155
        shipmentHeader.setCompanyCode(warehouseU8.getCompanyCode());
tongzhonghao authored
156
        shipmentHeader.setCDepName(icsShipmentHeader.getCDepName());
157
158
159
160
161
162
163
164
        shipmentHeader.setRecMobile(icsShipmentHeader.getRecMobile());
        shipmentHeader.setRecCompany(icsShipmentHeader.getRecCompany());
        shipmentHeader.setRecName(icsShipmentHeader.getRecName());
        shipmentHeader.setRecAddr(icsShipmentHeader.getRecAddr());
        shipmentHeader.setSendAddr(icsShipmentHeader.getSendAddr());
        shipmentHeader.setSendCompany(icsShipmentHeader.getSendCompany());
        shipmentHeader.setSendMobile(icsShipmentHeader.getSendMobile());
        shipmentHeader.setSendName(icsShipmentHeader.getSendName());
165
166
167
        switch (type){
            case "OO" :
                shipmentHeader.setReferCode(icsShipmentHeader.getMoCode());
168
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
169
170
171
                break;
            case "TO" :
                shipmentHeader.setReferCode(icsShipmentHeader.getCTVCode());
172
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
173
174
175
                break;
            default:
                shipmentHeader.setReferCode(icsShipmentHeader.getCCode());
176
                shipmentHeader.setReferId(icsShipmentHeader.getReferId());
177
178
179
180
181
182
183
184
185
186
187
                break;
        }
        if (StringUtils.isEmpty(shipmentHeader.getReferCode())) {
            throw new ServiceException("没有单号");
        }
        //根据icsShipmentDetail生成shipmentAetail
        List<ShipmentDetail> shipmentDetails = new ArrayList<ShipmentDetail>();
        for (ICSShipmentDetail icsShipmentDetail : icsShipmentDetails) {
            //查找物料
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>()
                    .eq(Material::getCode, icsShipmentDetail.getCInvCode())
188
                    .eq(Material::getWarehouseCode, warehouseU8.getWarehouseCode()));
189
190
191
192
193
            if (material == null) {
                throw new ServiceException("该物料不存在:" + icsShipmentDetail.getCInvCode());
            }
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setId(null);
194
            shipmentDetail.setReferId(icsShipmentDetail.getAutoID());
195
196
197
198
199
            shipmentDetail.setReferCode(shipmentHeader.getReferCode());
            shipmentDetail.setMoCode(icsShipmentDetail.getMoCode());
            shipmentDetail.setLot(icsShipmentDetail.getCDefine23());
            shipmentDetail.setWarehouseCode(warehouseU8.getWarehouseCode());
            shipmentDetail.setMaterialCode(icsShipmentDetail.getCInvCode());
200
201
            shipmentDetail.setMaterialName(icsShipmentDetail.getCInvName());
            shipmentDetail.setMaterialSpec(icsShipmentDetail.getCInvStd());
202
//                    shipmentDetail.setZoneCode(material.getZoneCode());
203
204
            shipmentDetail.setCreated(DateUtils.getNowDate());
            shipmentDetail.setBatch("");
tongzhonghao authored
205
            shipmentDetail.setUWarehouseCode(shipmentHeader.getUWarehouseCode());
206
207
            shipmentDetail.setMaterialUnit(icsShipmentDetail.getCComUnitName());
            shipmentDetail.setCreatedBy(maker);
208
            shipmentDetail.setLastUpdated(DateUtils.getNowDate());
209
210
211
212
213
214
            shipmentDetail.setLastUpdatedBy(maker);
            if (!icsShipmentDetail.getCompanyCode().equals(icsShipmentHeader.getCompanyCode())) {
                documentWarningService.ErpSendWarning(shipmentHeader.getReferCode(), "子单货主与主单不对应");
                throw new ServiceException("子单货主与主单不对应");
            }
            shipmentDetail.setCompanyCode(warehouseU8.getCompanyCode());
215
            shipmentDetail.setAutoID(icsShipmentDetail.getAutoID());
216
217
218
            shipmentDetail.setProjectNo(icsShipmentDetail.getCItemName());
            shipmentDetail.setQty((icsShipmentDetail.getIQuantity()).abs());
            shipmentDetail.setQtyCompleted(icsShipmentDetail.getIQuantity());
tongzhonghao authored
219
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
220
221
222
223
224
225
226
227
228
229
230
231
232
233
            if (icsShipmentDetail.getIrowno() != null && StringUtils.isNotEmpty(icsShipmentDetail.getIrowno().toString())) {
                shipmentDetail.setReferLineNum(icsShipmentDetail.getIrowno().toString());
            }
            if (icsShipmentDetail.getSortSeq() != null && StringUtils.isNotEmpty(icsShipmentDetail.getSortSeq().toString())) {
                shipmentDetail.setReferLineNum(icsShipmentDetail.getSortSeq().toString());
            }
            shipmentDetails.add(shipmentDetail);
        }

        //调用公共API
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);
        AjaxResult ajaxResult = shipmentApiService.shipment(shipmentDomain);
234
235
236
237
238
239
240
241
242
243
244
        if(!ajaxResult.hasErr() && !isFlag){
            U8Reference reference = new U8Reference();
            reference.setReferType(icsShipmentHeader.getType());
            reference.setReferCode(icsShipmentHeader.getReferCode());
            reference.setStock(icsShipmentHeader.getStock());
            reference.setCompanyCode(warehouseU8.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
245
246
247
248
249
250
251
252
253
254
255
256
        if(ajaxResult.hasErr()){
            return ajaxResult;
        }
        if(icsShipmentHeader.getIsNew().equals("1")){
             return AjaxResult.success("出库单下发成功");
        }else if (icsShipmentHeader.getIsNew().equals("0")) {
            return AjaxResult.success("出库单更新成功");
        }
        return AjaxResult.success("出库单下发成功");
    }
257
258
259
260
261
262
263
264
265
        //出库单为销售出库或采购退货单时

    /**
     * 出库单为 销售出库单 | 采购退货单
     * @param icsShipmentModel
     * @return
     */
    @Transactional
    public AjaxResult saleShipmentOrPurchReturn(ICSShipmentModel icsShipmentModel){
266
        icsShipmentModel = assign(icsShipmentModel);
267
268
        ICSShipmentHeader icsShipmentHeader=icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> icsShipmentDetails=icsShipmentModel.getIcsShipmentDetails();
269
270
        boolean isFlag = "0".equals(icsShipmentHeader.getIsNew()) ? true : false;
        String type = icsShipmentHeader.getType();
271
        String currentWarehouseCode = icsShipmentHeader.getWarehouseCode();
272
273
274
275
276
277
278
        String typeName = getShipmentTypeName(type);
        switch (type){
            case "SO" :
            case "PRO" :
                break;
            default:
                return AjaxResult.error("出库类型不匹配");
279
280
281
282
        }
        if(StringUtils.isEmpty(icsShipmentHeader.getIsNew())){
            throw  new  ServiceException("isNew为空");
        }
283
        String code=icsShipmentHeader.getReferCode();
284
        if(StringUtils.isEmpty(code)){
285
            return AjaxResult.error("没有单号");
286
287
288
289
290
291
292
        }

        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getUCompanyCode, icsShipmentHeader.getCompanyCode()));
        if(companyWu == null){
            return AjaxResult.error("单据货主不存在"+icsShipmentHeader.getCompanyCode());
        }
293
294
295
296
297
        WarehouseU8 warehouseU81 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                .eq(WarehouseU8::getUWarehouseCode, icsShipmentHeader.getStock())
                .eq(WarehouseU8::getWarehouseCode, currentWarehouseCode));
        if(warehouseU81==null){
            return AjaxResult.error("仓库编码不错误:"+icsShipmentHeader.getStock());
298
        }
299
300
301
302
303
304
305
306
307
308
        //u8关联表 检查上游单号是否重复
        U8Reference one = u8ReferenceService.getOne(new LambdaQueryWrapper<U8Reference>()
                .eq(U8Reference::getReferCode, icsShipmentHeader.getReferCode())
                .eq(U8Reference::getReferType, icsShipmentHeader.getType())
                .eq(U8Reference::getWarehouseCode, currentWarehouseCode));
        //入库单表 检查上游单号是否重复
        ShipmentHeader shipmentHeader1 = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getReferCode, icsShipmentHeader.getReferCode()));
        if(one != null && !isFlag && shipmentHeader1 != null){
            return AjaxResult.error(typeName + ":请不要重复下发 "+icsShipmentHeader.getReferCode());
309
        }
310
311
        //判断出库单是否可以更新
        checkShipmentBill(isFlag,code);
312
313
314
315
316
317
318
        String maker = icsShipmentHeader.getCMaker();
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setShipmentType(icsShipmentHeader.getType());
        shipmentHeader.setId(null);
        if(StringUtils.isNotEmpty(icsShipmentHeader.getFromCompanyCode())){
            shipmentHeader.setUserDef3("跨账套");
319
        }
320
321
        shipmentHeader.setReferId(icsShipmentHeader.getReferId());
        shipmentHeader.setReferCode(icsShipmentHeader.getReferCode());
322
323
324
325
326
327
328
329
330
331
        shipmentHeader.setLastUpdatedBy(maker);
        shipmentHeader.setCreatedBy(maker);
        shipmentHeader.setCreated(icsShipmentHeader.getCreated());
        shipmentHeader.setCustomerCode(icsShipmentHeader.getCCusCode());
        shipmentHeader.setLastUpdated(icsShipmentHeader.getCreated());
        shipmentHeader.setReferCode(code);
        shipmentHeader.setCreatedBy(icsShipmentHeader.getCMemo());
        shipmentHeader.setWarehouseCode(currentWarehouseCode);
        shipmentHeader.setUWarehouseCode(icsShipmentHeader.getStock());
        shipmentHeader.setCompanyCode(companyWu.getCompanyCode());
332
333
        shipmentHeader.setSupplierCode(icsShipmentHeader.getCVenCode());
        shipmentHeader.setDeptCode(icsShipmentHeader.getCDepCode());
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
        shipmentHeader.setCDepName(icsShipmentHeader.getCDepName());
        shipmentHeader.setRecMobile(icsShipmentHeader.getRecMobile());
        shipmentHeader.setRecCompany(icsShipmentHeader.getRecCompany());
        shipmentHeader.setRecName(icsShipmentHeader.getRecName());
        shipmentHeader.setRecAddr(icsShipmentHeader.getRecAddr());
        shipmentHeader.setSendAddr(icsShipmentHeader.getSendAddr());
        shipmentHeader.setSendCompany(icsShipmentHeader.getSendCompany());
        shipmentHeader.setSendMobile(icsShipmentHeader.getSendMobile());
        shipmentHeader.setSendName(icsShipmentHeader.getSendName());
        List<ShipmentDetail> shipmentDetails = new ArrayList<>();
        for (ICSShipmentDetail item : icsShipmentDetails) {
            //查找物料
            Material material = materialService.getOne(new LambdaQueryWrapper<Material>()
                    .eq(Material::getCode, item.getCInvCode())
                    .eq(Material::getWarehouseCode, currentWarehouseCode));
            if(material==null){
                documentWarningService.ErpSendWarning(code, "该物料不存在:"+item.getCInvCode());
                throw new ServiceException("该物料不存在:"+item.getCInvCode());
352
            }
353
354
355
356
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setId(null);
            shipmentDetail.setReferCode(code);
            shipmentDetail.setAutoID(item.getAutoID());
357
            shipmentDetail.setReferLineNum(item.getIrowno()+"");
358
            shipmentDetail.setWarehouseCode(currentWarehouseCode);
359
            shipmentDetail.setUWarehouseCode(icsShipmentHeader.getUWarehouseCode());
360
361
362
363
364
365
366
367
            shipmentDetail.setMaterialCode(item.getCInvCode());
            shipmentDetail.setZoneCode(material.getZoneCode());
            shipmentDetail.setCreated(DateUtils.getNowDate());
            shipmentDetail.setUWarehouseCode(item.getCWHCode());
            shipmentDetail.setCreatedBy(maker);
            shipmentDetail.setMaterialSpec(item.getCComUnitName());
            shipmentDetail.setLastUpdated(DateUtils.getNowDate());
            shipmentDetail.setLastUpdatedBy(maker);
368
369
370
            shipmentDetail.setPoId(item.getPOID());
            shipmentDetail.setPodId(item.getPODID());
            shipmentDetail.setPoCode(item.getPOCode());
371
372
373
            if (!item.getCompanyCode().equals(icsShipmentHeader.getCompanyCode())) {
                documentWarningService.ErpSendWarning(code, "子单货主与主单不对应");
                throw new ServiceException("子单货主与主单不对应");
374
            }
375
376
377
378
379
380
381
382
383
384
            shipmentDetail.setMoCode(item.getCDefine22());
            shipmentDetail.setLot(item.getCDefine23());
            shipmentDetail.setCompanyCode(companyWu.getCompanyCode());
            shipmentDetail.setProjectNo(item.getCItemName());
            shipmentDetail.setQty((item.getIQuantity()).abs());
            shipmentDetail.setQtyCompleted(item.getIQuantity());
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
            shipmentDetail.setSoCode(item.getCSOCode());
            shipmentDetail.setSoId(item.getCSOID());
            shipmentDetail.setSoAutoId(item.getCSOAutoID());
385
            shipmentDetail.setBatch("");
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
            shipmentDetails.add(shipmentDetail);
        }
        //调用公共API
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetails);
        AjaxResult ajaxResult = shipmentApiService.shipment(shipmentDomain);
        if(!ajaxResult.hasErr() && !isFlag){
            U8Reference reference = new U8Reference();
            reference.setReferType(icsShipmentHeader.getType());
            reference.setReferCode(icsShipmentHeader.getReferCode());
            reference.setStock(icsShipmentHeader.getStock());
            reference.setCompanyCode(companyWu.getCompanyCode());
            reference.setWarehouseCode(currentWarehouseCode);
            reference.setTypeName(typeName);
            reference.setCreated(DateUtils.getNowDate());
            u8ReferenceService.save(reference);
        }
        if(ajaxResult.hasErr()){
            return ajaxResult;
        }
        String isNew = icsShipmentHeader.getIsNew();
        if("1".equals(isNew)){
            return AjaxResult.success("出库单下发成功");
        }else if ("0".equals(isNew)) {
            return AjaxResult.success("出库单更新成功");
        }else {
            return ajaxResult;
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
        }
    }



    //wms-u8出库回传

    /**
     * 出库单回传
     * @param url
     * @param status
     * @return
     */
    @Transactional
    public AjaxResult shipmentReturn(String url, Integer status) {
        //找出所有首状态为1000的出库单
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>()
                .lt(ShipmentHeader::getFirstStatus, 1000));
        //获取code集合
        List<String> codeList = shipmentHeaders.stream().map(ShipmentHeader::getCode).collect(Collectors.toList());

        //先找出处于完成状态的taskDetail
        List<TaskDetail> taskDetails = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, status)
                .eq(TaskDetail::getInternalTaskType, QuantityConstant.SHIPMENT_CONTAINER_FINISHED)
                .in(TaskDetail::getBillCode,codeList));

        if(taskDetails==null || taskDetails.size()<1){
tongzhonghao authored
442
            return AjaxResult.error("没有完成状态的子任务");
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
        }

        Set<String> billCodes = new HashSet<>();
        final List<String> collect = taskDetails.stream().map(t -> t.getBillCode()).distinct().collect(Collectors.toList());


        for(TaskDetail taskDetail:taskDetails) {
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                    .eq(ShipmentHeader::getCode, taskDetail.getBillCode()));
            if(shipmentHeader==null){
                continue;
            }
            if(shipmentHeader!=null && shipmentHeader.getFirstStatus()== 1000){
                continue;
            }
            shipmentHeader.setFirstStatus(1000);
            //更新状态为1000
            shipmentHeaderService.updateById(shipmentHeader);
            //修改任务明细状态
            taskDetail.setStatus(200);
            taskDetailService.updateById(taskDetail);
        }
        return AjaxResult.success();
    }
468
tongzhonghao authored
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    @Transactional
    public AjaxResult ConfirmShipment(TaskDetail taskDetail,String url,Integer status){
        String currentWarehouseCode = taskDetail.getWarehouseCode();
        AjaxResult ajaxResult=new AjaxResult();
        //根据子任务查找主单
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>()
                .eq(ShipmentHeader::getCode, taskDetail.getBillCode()));
        if(shipmentHeader==null){
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_REMOVE);
            taskDetailService.updateById(taskDetail);
            return AjaxResult.success("出库单已删除");
        }
        //查找相同单号的状态为100的子任务
        List<TaskDetail> taskDetails = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, status)
                .eq(TaskDetail::getBillCode, shipmentHeader.getCode()));
        if(taskDetails ==null || taskDetails.size()<1){
            return AjaxResult.success("已完成");
        }
        //查找相同单号状态为10的子任务
        List<TaskDetail> taskDetailList = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                .eq(TaskDetail::getStatus, QuantityConstant.TASK_STATUS_RELEASE)
                .eq(TaskDetail::getBillCode, shipmentHeader.getCode()));

        if(taskDetailList!=null && taskDetailList.size()>0){
tongzhonghao authored
494
            return AjaxResult.error("该单据还有任务未完成,请先完成该单据的任务");
tongzhonghao authored
495
496
497
498
499
500
501
502
503
504
505
506
507
        }

        String type=shipmentHeader.getShipmentType();
        String code=shipmentHeader.getReferCode();

        if (!type.equals("PRO") && !type.equals("MO") && !type.equals("QO") && !type.equals("SO") ) {
            for(TaskDetail item :taskDetails) {
                item.setStatus(110);
                taskDetailService.updateById(item);
            }
            if (shipmentHeader.getFirstStatus() == 300 && shipmentHeader.getLastStatus() == 300) {
                shipmentHeaderService.removeShipmentByHeaderId(shipmentHeader.getId());
            }
tongzhonghao authored
508
            return AjaxResult.error("该子任务所属单据无需上传");
tongzhonghao authored
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
        }


        //根据子任务查找子单
        Set<String> uwarehouseCodes = new HashSet<>();
        List<ShipmentDetail> shipmentDetails=new ArrayList<>();
        Integer number =0;
        for(TaskDetail item:taskDetails) {
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(item.getBillDetailId());

            if (shipmentDetails != null && shipmentDetails.size() > 0) {
                for (ShipmentDetail detail1 : shipmentDetails) {
                    if (shipmentDetail.getId() == detail1.getId()) {
                        detail1.setTaskQty(detail1.getTaskQty().add(item.getQty()));
                        number=1;
                        break;
                    }
                }
            }
            if(number == 0) {
                shipmentDetail.setTaskQty(item.getQty());
                shipmentDetails.add(shipmentDetail);
                uwarehouseCodes.add(shipmentDetail.getUWarehouseCode());
            }
            number=0;

        }

        Iterator<String> iterator=uwarehouseCodes.iterator();
        String uWarehouseCode=iterator.next();

        CompanyU8 companyWu = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getCompanyCode, shipmentHeader.getCompanyCode()));


        boolean isSameDate = icsReceiptAPIService.isSameMonth(taskDetail.getLastUpdated());

        if(isSameDate == false){
            for(TaskDetail item: taskDetails) {
                item.setStatus(180);
                taskDetailService.updateById(item);
            }
            documentWarningService.ErpReturnWarning(shipmentHeader.getCode(), shipmentHeader.getReferCode(),
                "回传超时" + shipmentHeader.getUserDef2());
tongzhonghao authored
553
            return AjaxResult.error("取消回传");
tongzhonghao authored
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
        }

        List<ICSShipmentDetail> icsShipmentDetails=new ArrayList<ICSShipmentDetail>();
        String dt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS_S,taskDetail.getLastUpdated());
        String userName =taskDetail.getLastUpdatedBy();
        User user =new User();
        user = userService.selectUserByLoginName(userName);
        if(user != null){
            userName = user.getUserName();
        }
        ICSShipmentHeader icsShipmentHeader= null;
        //出库类型为采购的主单和子单
        switch (type){
            case "PRO":
                 icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCCode, code)
                        .eq(ICSShipmentHeader::getCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setDPODate(dt);
                icsShipmentHeader.setCWHCode(uWarehouseCode);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setID(shipmentHeader.getId());
                icsShipmentHeader.setVoucherType("CreateICSRdRecord_01_Return");

                //子单
                for(ShipmentDetail item:shipmentDetails) {
                    if (uWarehouseCode.equals(item.getUWarehouseCode())) {
                        ICSShipmentDetail icsShipmentDetail = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                                .eq(ICSShipmentDetail::getCCode, code)
                                .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                                .eq(ICSShipmentDetail::getCInvCode, item.getMaterialCode()));
                        icsShipmentDetail.setID(item.getId());
                        icsShipmentDetail.setIArrsId(icsShipmentDetail.getAutoID());
                        icsShipmentDetail.setIPOsID(icsShipmentDetail.getPODID());
                        icsShipmentDetail.setIQuantity(item.getQtyCompleted().negate());
                        icsShipmentDetails.add(icsShipmentDetail);
                    }
                }
                break;
            case "MO":
                icsShipmentHeader.setVoucherType("UpdateICSRdRecord_11_Confirm");
            case "QO":
                //出库类型为材料或其他的主单和子单
                 icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCCode, code)
                        .eq(ICSShipmentHeader::getCWHCode, shipmentHeader.getUWarehouseCode())
                        .eq(ICSShipmentHeader::getCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setDDate(dt);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setVoucherType("UpdateICSRdRecord_09_Confirm");
                for(ShipmentDetail item:shipmentDetails){

                    ICSShipmentDetail icsShipmentDetailA = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                            .eq(ICSShipmentDetail::getCCode, code)
                            .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                            .eq(ICSShipmentDetail::getAutoID, item.getAutoID()));
                    icsShipmentDetailA.setIQuantity(item.getTaskQty());
                    icsShipmentDetails.add(icsShipmentDetailA);
                }
                break;
            case "SO":
                icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getCDLCode,code)
                        .eq(ICSShipmentHeader::getCompanyCode,companyWu.getUCompanyCode()));

                icsShipmentHeader.setDDate(dt);
                icsShipmentHeader.setCMaker(userName);
                icsShipmentHeader.setCWHCode(shipmentHeader.getUWarehouseCode());

                WarehouseU8 warehouseU8 = warehouseU8Service.getOne(new LambdaQueryWrapper<WarehouseU8>()
                        .eq(WarehouseU8::getUWarehouseCode, shipmentHeader.getUWarehouseCode())
                        .eq(WarehouseU8::getUCompanyCode, companyWu.getUCompanyCode()));
                icsShipmentHeader.setCWHName(warehouseU8.getUWarehouseName());
                icsShipmentHeader.setCCode(shipmentHeader.getSourceCode());
                icsShipmentHeader.setVoucherType("CreateICSRdRecord_32");
                icsShipmentHeader.setID(icsShipmentHeader.getDLID());
                for(ShipmentDetail item:shipmentDetails){

                    ICSShipmentDetail icsShipmentDetailB = icsShipmentDetailService.getOne(new LambdaQueryWrapper<ICSShipmentDetail>()
                            .eq(ICSShipmentDetail::getCCode, code)
                            .eq(ICSShipmentDetail::getCompanyCode, companyWu.getUCompanyCode())
                            .eq(ICSShipmentDetail::getCInvCode, item.getMaterialCode())
                            .eq(ICSShipmentDetail::getAutoID, item.getAutoID()));

                    icsShipmentDetailB.setCCode(code);
                    icsShipmentDetailB.setID(icsShipmentDetailB.getDLID());
                    icsShipmentDetailB.setAutoID(icsShipmentDetailB.getAutoID());
                    icsShipmentDetailB.setIQuantity(item.getTaskQty());
                    icsShipmentDetails.add(icsShipmentDetailB);
                }
                break;
        }


        if(type.equals("MO")){
            icsShipmentHeader.setVoucherType("UpdateICSRdRecord_11_Confirm");
        }
        if(type.equals("QO")){
            icsShipmentHeader.setVoucherType("UpdateICSRdRecord_09_Confirm");
        }

        ICSShipmentModel model = new ICSShipmentModel();
        model.setIcsShipmentHeader(icsShipmentHeader);
        model.setIcsShipmentDetails(icsShipmentDetails);
        String JsonParam = JSON.toJSONString(model);
        String result = HttpUtils.bodypost(url, JsonParam,currentWarehouseCode);
        if(StringUtils.isEmpty(result)){
            throw new ServiceException("接口地址错误或服务器连接不到或返回为空");
        }
        ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.getCode() ==200 || ajaxResult.getCode() == 400) {


            if(shipmentHeader.getShipmentType().equals("SO") || shipmentHeader.getShipmentType().equals("PRO")){
                shipmentHeader.setUserDef2(ajaxResult.getMsg());
                shipmentHeaderService.updateById(shipmentHeader);
            }

            for(TaskDetail item: taskDetails) {
                item.setStatus(120);
                taskDetailService.updateById(item);
            }

            /**
             *  生成快递单
             *  1U8返回值为200
             *  2wms出库单快递信息存在
             *  3、存在就生成快递单
             *
             */
//            if(ajaxResult.getCode() == 200 && shipmentHeader.getKdEable() == 1){
//                AjaxResult ajaxResult1 = kuaidiHeaderService.saveKD(shipmentDetails,shipmentHeader,ajaxResult.getMsg());
//            }

            if(ajaxResult.getCode() == 400) {
                documentWarningService.ErpReturnWarning(shipmentHeader.getCode(), shipmentHeader.getSourceCode(), ajaxResult.getMsg());
            }

            if (shipmentHeader.getFirstStatus() == 300 && shipmentHeader.getLastStatus() != 300) {
                shipmentHeader.setUploadStatus(15);
                shipmentHeaderService.updateById(shipmentHeader);
            }
            TaskDetail condition = new TaskDetail();
            condition.setBillCode(shipmentHeader.getCode());

            //select  * from task_detail where billCode = #{billCode} and billId = #{billId} and (status &lt;= 100 or status = 150 or status= 160)
            LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(TaskDetail::getBillCode,shipmentHeader.getCode())
                    .lt(TaskDetail::getStatus,100)
                    .or()
                    .eq(TaskDetail::getStatus,100)
                    .or()
                    .eq(TaskDetail::getStatus,150)
                    .or()
                    .eq(TaskDetail::getStatus,160);
            List<TaskDetail> taskDetails1 = taskDetailService.list(queryWrapper);
            if (shipmentHeader.getFirstStatus() == 300 && shipmentHeader.getLastStatus() == 300 && taskDetails1.isEmpty()) {
                shipmentHeader.setFirstStatus(900);
                shipmentHeader.setLastStatus(900);
                shipmentHeader.setUploadStatus(20);
                shipmentHeaderService.updateById(shipmentHeader);
            }
        }

        if(ajaxResult.getCode() == 300){
            shipmentHeader.setUserDef2(ajaxResult.getMsg());
            shipmentHeaderService.updateById(shipmentHeader);
            for(TaskDetail item: taskDetails) {
                item.setStatus(160);
                taskDetailService.updateById(item);
            }
        }
        return ajaxResult;

    }
730
731
732
733
734

    /**
          * 判断出库单是否可以更新
          * @param code U8单号
          */
tongzhonghao authored
735
    private void checkShipmentBill(boolean isFlag,String code){
736
        //U8单据类型
tongzhonghao authored
737
738
739
        LambdaQueryWrapper<ShipmentHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShipmentHeader::getReferCode,code);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(queryWrapper);
740
741

        //WMS出库单 和 U8出库单 都不能为空 才能更新
tongzhonghao authored
742
743
        if (isFlag) {
            if(shipmentHeader!=null){
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
                if(StringUtils.isNotNull(shipmentHeader) ){
                    //找出 出库单未完成的任务
                    List<TaskDetail> taskDetailList = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>()
                            .eq(TaskDetail::getBillCode, shipmentHeader.getCode())
                            .lt(TaskDetail::getStatus,120));
                    if(taskDetailList!=null&&taskDetailList.size()>0){
                        throw  new ServiceException("更新失敗,此出库单存在未完成的任务");
                    }
                    //找出库配盘明细 未配盘的单据
                    List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(new LambdaQueryWrapper<ShipmentContainerDetail>()
                            .eq(ShipmentContainerDetail::getShipmentCode, shipmentHeader.getCode())
                            .lt(ShipmentContainerDetail::getStatus, QuantityConstant.SHIPMENT_CONTAINER_FINISHED));

                    if(shipmentContainerDetails!=null&&shipmentContainerDetails.size()>0){
                        throw  new ServiceException("更新失敗,此出库单存在未完成的组盘");
                    }
                    //删除wms出库单头表
                    shipmentHeaderService.removeById(shipmentHeader.getId());
                    //删除wms出库单明细表
                    shipmentDetailService.remove(new LambdaQueryWrapper<ShipmentDetail>()
                            .eq(ShipmentDetail::getShipmentCode,shipmentHeader.getCode()));
                }
            }
tongzhonghao authored
767
768
769
770
771
772
773
774
775
776
        }
    }

    private ICSShipmentModel assign(ICSShipmentModel icsShipmentModel){
        ICSShipmentHeader header = icsShipmentModel.getIcsShipmentHeader();
        List<ICSShipmentDetail> details = icsShipmentModel.getIcsShipmentDetails();
        String type = header.getType();
        Date created = null;
        if(header.getDDate() !=null){
            try {
777
                created = DateUtils.parseDate(header.getDDate(),DateUtils.YYYY_MM_DD_HH_MM_SS);
tongzhonghao authored
778
779
            } catch (ParseException e) {
                e.printStackTrace();
780
781
            }
        }
tongzhonghao authored
782
783
784
785
786
        switch (type){
            case "MO" :
                header.setReferId(header.getID());
                header.setReferCode(header.getCCode());
                header.setCreated(created);
787
                header.setStock(header.getCWHCode());
tongzhonghao authored
788
789
790
791
792
                for (ICSShipmentDetail detail : details) {
                    detail.setReferId(detail.getAutoID());
                    detail.setReferCode(detail.getPOCode());
                }
                break;
793
            case "SO" :
tongzhonghao authored
794
795
                header.setReferId(header.getDLID());
                header.setReferCode(header.getCDLCode());
796
797
798
799
800
801
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(),DateUtils.YYYY_MM_DD_HH_MM_SS_S));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String stock = "";
tongzhonghao authored
802
                for (ICSShipmentDetail detail : details) {
803
804
805
                    detail.setReferId(detail.getAutoID());
                    detail.setReferCode(detail.getPOCode());
                    stock = detail.getCWHCode();
tongzhonghao authored
806
                }
807
                header.setStock(stock);
tongzhonghao authored
808
                break;
809
810
811
812
813
814
815
816
817
818
            case "OO" :
                header.setStock("20");
                header.setCreated(created);
                header.setReferId(header.getMoId());
                header.setReferCode(header.getMoCode());
                for (ICSShipmentDetail detail : details) {
                    detail.setCWHCode("20");
                    detail.setReferId(detail.getMoDId());
                    detail.setAutoID(detail.getMoDId());
                }
tongzhonghao authored
819
                break;
820
821
822
823
824
825
            case "PRO" :
                try {
                    header.setCreated(DateUtils.parseDate(header.getDPODate(),DateUtils.YYYY_MM_DD_HH_MM_SS_S));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
826
827
828
829
830
831
832
833
834
835
836
                header.setStock("20");
                header.setReferId(header.getID());
                header.setReferCode(header.getCCode());
                String stock1 = "";
                for (ICSShipmentDetail detail : details) {
                    stock1 = detail.getCWHCode();
                    detail.setPOCode(detail.getPOCode());
                    detail.setPODID(detail.getPODID());
                    detail.setPOID(detail.getPOID());
                }
                header.setCWHCode(stock1);
tongzhonghao authored
837
838
839
840
841
842
843
844
845
846
847
848
849
                break;
        }
        icsShipmentModel.setIcsShipmentHeader(header);
        icsShipmentModel.setIcsShipmentDetails(details);
        return icsShipmentModel;
    }

    private String getShipmentTypeName(String code){
        String typeName = "";
        switch (code){
            case "MO" :
                typeName = "生产领料";
                break;
850
851
852
            case "SO" :
                typeName = "普通销售";
                break;
tongzhonghao authored
853
            case "OO" :
854
                typeName = "生产订单";
tongzhonghao authored
855
856
857
                break;
        }
        return typeName;
858
859
860
    }

}