ReceiptHeaderService.java
17.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.huaheng.pc.receipt.receiptHeader.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.check.checkDetail.domain.CheckDetail;
import com.huaheng.pc.check.checkDetail.service.CheckDetailService;
import com.huaheng.pc.check.checkHeader.domain.CheckHeader;
import com.huaheng.pc.check.checkHeader.service.CheckHeaderService;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.receiptType.domain.ReceiptType;
import com.huaheng.pc.config.receiptType.service.ReceiptTypeService;
import com.huaheng.pc.config.sendMail.service.SendMailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.mapper.ReceiptHeaderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
public class ReceiptHeaderService extends ServiceImpl<ReceiptHeaderMapper, ReceiptHeader> {
@Resource
private ReceiptTypeService receiptTypeService;
@Resource
private ReceiptDetailService receiptDetailService;
@Resource
private CheckHeaderService checkHeaderService;
@Resource
private CheckDetailService checkDetailService;
@Resource
private SendMailService sendMailService;
@Resource
private ReceiptHeaderMapper receiptHeaderMapper;
@Resource
private CompanyService companyService;
@Resource
private MaterialService materialService;
/**
* 保存入库单
* @param receiptHeader
* @return
*/
public AjaxResult saveReceiptHeader(ReceiptHeader receiptHeader){
//根据仓库和货主以及传入单据类型编码判断单据类型是否存在
LambdaQueryWrapper<ReceiptType> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptType::getCode, receiptHeader.getReceiptType())
.eq(ReceiptType::getWarehouseCode,ShiroUtils.getWarehouseCode())
.eq(ReceiptType::getCompanyCode, receiptHeader.getCompanyCode());
ReceiptType receiptType = receiptTypeService.getOne(lambdaQueryWrapper);
if(receiptType == null) {
throw new ServiceException(ShiroUtils.getWarehouseCode(),receiptHeader.getCompanyCode(),"没有对应的入库单类型");
}
//根据单据类型生成单号
String code = createCode(receiptHeader.getReceiptType());
if(StringUtils.isEmpty(code)){
throw new ServiceException("根据单据类型"+receiptHeader.getReceiptType()+"生成单号失败");
}
receiptHeader.setCode(code);
receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_BUILD);
receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
return AjaxResult.toAjax(this.save(receiptHeader));
}
/**
* 根据单据类型生成单号 规则:入库单类型 + 年月日 + 5位(排序号 + 1)
* @param receiptType
* @return
*/
//todo:huhai 单据生成规则,后续版本计划追加一个序列号生成规则表:统一设定前缀,填充位,流水码等
public String createCode(String receiptType) {
String code = null;
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
LambdaQueryWrapper<ReceiptHeader> lambda = Wrappers.lambdaQuery();
lambda.select(ReceiptHeader::getCode).eq(ReceiptHeader::getReceiptType, receiptType)
.orderByDesc(ReceiptHeader::getId).last("Limit 1");
//如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
String maxCode = null;
ReceiptHeader receiptHeader = this.getOne(lambda);
if ( receiptHeader != null){
maxCode = this.getOne(lambda).getCode();
}
//todo:隐藏了BUG,请修正。 修改好 去掉todo
if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now))) {
Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
code = receiptType + df.format(now) + String.format("%05d", Count + 1);
} else {
code = receiptType + df.format(now) + "00001";
}
return code;
}
/**
* 入库预约
* @param receiptHeader
* @return
*/
public AjaxResult reservation(ReceiptHeader receiptHeader) {
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//更新入库明细流程
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RESERVATION.toString());
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库明细处理标记失败");
}
receiptDetail = receiptDetailService.queryflow(receiptDetail);
if (!receiptDetailService.updateById(receiptDetail)){
throw new ServiceException("更新入库明细流程失败");
}
}
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
//更新入库单预约时间
if (!this.updateById(receiptHeader)){
throw new ServiceException("更新入库单预约时间失败");
}
receiptDetailService.updateReceiptHeaderLastStatus(receiptHeader.getId());
return AjaxResult.success("预约成功");
}
/**
* 入库到货
* @param id
* @return
*/
public AjaxResult arrival(Integer id) {
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id)
.eq(ReceiptDetail::getProcessStamp, QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//更新入库明细流程
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetailService.queryflow(receiptDetail);
}
//更新入库单头表
receiptDetailService.updateReceiptHeaderLastStatus(id);
return AjaxResult.success("预约成功");
}
/**
* 入库质检
*/
public AjaxResult check(Integer id){
ReceiptHeader receiptHeader = this.getById(id);
LambdaQueryWrapper<ReceiptDetail> receiptDetailLambda = Wrappers.lambdaQuery();
receiptDetailLambda.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambda);
//判断是否已经生成了质检单
LambdaQueryWrapper<CheckHeader> checkHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
checkHeaderLambdaQueryWrapper.eq(CheckHeader::getCode, receiptHeader.getCode());
CheckHeader checkHeaderQuery = checkHeaderService.getOne(checkHeaderLambdaQueryWrapper);
if (checkHeaderQuery != null){
throw new ServiceException("质检单已生成");
}
//创建质检头表
CheckHeader checkHeader = new CheckHeader();
checkHeader.setCode(receiptHeader.getCode());
checkHeader.setType(QuantityConstant.CHECK_TYPE_RECEIPT.toString());
checkHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
checkHeader.setReferCode(receiptHeader.getReferCode());
checkHeader.setEnable(1);
checkHeader.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
checkHeader.setCreatedBy(ShiroUtils.getLoginName());
checkHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
if (!checkHeaderService.save(checkHeader)){
throw new ServiceException("添加质检头表错误");
}
//查询保存后的质检头表
LambdaQueryWrapper<CheckHeader> checkHeaderLambda = Wrappers.lambdaQuery(checkHeader);
checkHeader = checkHeaderService.getOne(checkHeaderLambda);
for (ReceiptDetail receiptDetail : receiptDetails) {
CheckDetail checkDetail = new CheckDetail();
checkDetail.setCheckHeaderId(checkHeader.getId());
checkDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
checkDetail.setCheckCode(checkHeader.getCode());
checkDetail.setReceiptDetailId(receiptDetail.getId());
checkDetail.setReceiptCode(receiptDetail.getReceiptCode());
checkDetail.setReferCode(checkHeader.getReferCode());
checkDetail.setMaterialCode(receiptDetail.getMaterialCode());
checkDetail.setMaterialName(receiptDetail.getMaterialName());
checkDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
checkDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
checkDetail.setCompanyCode(receiptDetail.getCompanyCode());
checkDetail.setInventorySts(receiptDetail.getInventorySts());
checkDetail.setStatus(QuantityConstant.CHECK_STATUS_BUILD.toString());
checkDetail.setQty(receiptDetail.getTotalQty());
checkDetail.setCheckBy(ShiroUtils.getLoginName());
checkDetail.setCreatedBy(ShiroUtils.getLoginName());
checkDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
if ( !checkDetailService.save(checkDetail) ){
throw new ServiceException("保存质检明细失败");
}
}
return AjaxResult.success("创建质检单成功");
}
/**
* 提交审核入库单
*/
@Transactional
public AjaxResult submitReview(String ids){
String warehouseCode=ShiroUtils.getWarehouseCode();
if (StringUtils.isEmpty(ids)){
return AjaxResult.error("仓库:"+warehouseCode+",提交入库单审核,id不能为空");
}
Integer[] idArray = Convert.toIntArray(ids);
for (int i = 0; i<idArray.length; i++){
ReceiptHeader receiptHeader = this.getById(idArray[i]);
if (receiptHeader.getFirstStatus()>QuantityConstant.RECEIPT_HEADER_VERIFYING
||receiptHeader.getLastStatus()>QuantityConstant.RECEIPT_HEADER_VERIFYING){
return AjaxResult.error("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+"入库单不需要审核");
}
LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, idArray[i]);
List<ReceiptDetail> receiptDetailList = receiptDetailService.list(lambdaQueryWrapper);
if (receiptDetailList.isEmpty()){
return AjaxResult.error("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+"入库单没有明细");
}
receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_VERIFYING);
receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_VERIFYING);
if (!this.updateById(receiptHeader)){
throw new ServiceException("仓库:"+warehouseCode+",单据号:"+receiptHeader.getCode()+" 失败");
}
}
return AjaxResult.success("仓库:"+warehouseCode+",提交审核成功");
}
/**
* 到货通知
* @param id
* @return
*/
public AjaxResult arrivalNotice(String id){
LambdaQueryWrapper<ReceiptDetail> lambda = Wrappers.lambdaQuery();
lambda.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambda);
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_ARRIVAL.toString());
receiptDetailService.updateById(receiptDetail);
if (!receiptDetailService.updateById(receiptDetailService.queryflow(receiptDetail))){
throw new ServiceException("更新入库明细处理标记出错");
}
receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
}
return AjaxResult.success("");
}
/**
* 加入订单池
* @param ids 头表id字符串
* @return
*/
@Transactional
public AjaxResult addPool(String ids){
List<Integer> idList = Arrays.asList(Convert.toIntArray(ids));
for (Integer id : idList){
LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> list = receiptDetailService.list(lambdaQueryWrapper);
if (list.size() != 0){
for (ReceiptDetail receiptDetail : list){
if (Integer.parseInt(receiptDetail.getProcessStamp()) < QuantityConstant.RECEIPT_HEADER_POOL){
//判断入库明细是否需要审核
ReceiptDetail beforeReceiptDetail = receiptDetailService.queryflow(receiptDetail);
if (Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 5 || Integer.parseInt(beforeReceiptDetail.getProcessStamp()) == 100){
throw new ServiceException("请先提交审核");
}
//加入订单池
receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_POOL.toString());
ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
if (!receiptDetailService.updateById(receiptDetail1)){
throw new ServiceException("更新入库明细状态失败");
}
} else {
receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
}
}
}
//更新头表状态
receiptDetailService.updateReceiptHeaderLastStatus(id);
}
return AjaxResult.success("加入成功");
}
public List<ReceiptHeader> selectListByCreated() {
return receiptHeaderMapper.selectListByCreated();
}
@Transactional(rollbackFor = Exception.class)
public Integer createTodayHeader(String companyCode) {
String receiptType = "PPI";
String code = null;
Date now = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String maxCode = receiptHeaderMapper.createCode(receiptType);
//如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
{
Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
code = receiptType + df.format(now) + String.format("%05d", Count + 1);
}
else
{
code = receiptType + df.format(now) + "00001";
}
ReceiptHeader receiptHeader = new ReceiptHeader();
receiptHeader.setId(null);
receiptHeader.setCode(code);
receiptHeader.setReceiptType(receiptType);
receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
receiptHeader.setCreated(null);
receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
receiptHeader.setLastUpdated(null);
receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
receiptHeader.setFirstStatus(200);
receiptHeader.setLastStatus(200);
receiptHeader.setCompanyCode(companyCode);
this.save(receiptHeader);
return receiptHeader.getId();
}
@Transactional(rollbackFor = Exception.class)
public List<ReceiptHeader> getLatestReceipt(String warehouseCode, String companyCode) {
return receiptHeaderMapper.getLatestReceipt(warehouseCode, companyCode);
}
/**
* 修改回传后状态
* @param id
* @return
*/
private AjaxResult editReceiveStatus(Integer id) {
ReceiptHeader receiptHeader = new ReceiptHeader();
receiptHeader.setId(id);
receiptHeader.setFirstStatus(900);
receiptHeader.setLastStatus(900);
if (!updateById(receiptHeader)) {
throw new ServiceException("更新头表状态失败");
} else {
LambdaQueryWrapper<ReceiptDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, id);
List<ReceiptDetail> receiptDetails = receiptDetailService.list(lambdaQueryWrapper);
for (ReceiptDetail receiptDetail : receiptDetails) {
receiptDetail.setProcessStamp("900");
}
if (!receiptDetailService.updateBatchById(receiptDetails)) {
throw new ServiceException("更新明细失败");
}
}
return AjaxResult.success("");
}
}