|
1
|
package com.lframework.xingyun.sc.procurement.controller.receive;
|
1
|
package com.lframework.xingyun.sc.procurement.controller.receive;
|
|
2
|
|
2
|
|
|
|
|
3
|
+import cn.hutool.core.convert.NumberChineseFormatter;
|
|
|
|
4
|
+import cn.hutool.core.io.resource.ClassPathResource;
|
|
3
|
import com.lframework.starter.web.core.annotations.security.HasPermission;
|
5
|
import com.lframework.starter.web.core.annotations.security.HasPermission;
|
|
4
|
import com.lframework.starter.web.core.components.resp.InvokeResult;
|
6
|
import com.lframework.starter.web.core.components.resp.InvokeResult;
|
|
5
|
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
|
7
|
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
|
|
@@ -14,14 +16,33 @@ import com.lframework.xingyun.sc.procurement.vo.receive.CreateWarehouseReceiveOr |
|
@@ -14,14 +16,33 @@ import com.lframework.xingyun.sc.procurement.vo.receive.CreateWarehouseReceiveOr |
|
14
|
import com.lframework.xingyun.sc.procurement.vo.receive.QueryWarehouseReceiveOrderVo;
|
16
|
import com.lframework.xingyun.sc.procurement.vo.receive.QueryWarehouseReceiveOrderVo;
|
|
15
|
import com.lframework.xingyun.sc.procurement.vo.receive.UpdateWarehouseReceiveActualQuantityVo;
|
17
|
import com.lframework.xingyun.sc.procurement.vo.receive.UpdateWarehouseReceiveActualQuantityVo;
|
|
16
|
import com.lframework.xingyun.sc.procurement.vo.receive.UpdateWarehouseReceiveOrderVo;
|
18
|
import com.lframework.xingyun.sc.procurement.vo.receive.UpdateWarehouseReceiveOrderVo;
|
|
|
|
19
|
+import com.lframework.xingyun.sc.utils.ExcelUtil;
|
|
|
|
20
|
+import com.lframework.xingyun.sc.utils.ResponseUtil;
|
|
17
|
import io.swagger.annotations.Api;
|
21
|
import io.swagger.annotations.Api;
|
|
18
|
import io.swagger.annotations.ApiImplicitParam;
|
22
|
import io.swagger.annotations.ApiImplicitParam;
|
|
19
|
import io.swagger.annotations.ApiOperation;
|
23
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
24
|
+
|
|
|
|
25
|
+import java.io.File;
|
|
|
|
26
|
+import java.io.FileInputStream;
|
|
|
|
27
|
+import java.io.FileNotFoundException;
|
|
|
|
28
|
+import java.io.FileOutputStream;
|
|
|
|
29
|
+import java.io.IOException;
|
|
|
|
30
|
+import java.io.InputStream;
|
|
|
|
31
|
+import java.math.BigDecimal;
|
|
|
|
32
|
+import java.nio.file.Files;
|
|
|
|
33
|
+import java.time.LocalDate;
|
|
|
|
34
|
+import java.util.HashMap;
|
|
20
|
import java.util.List;
|
35
|
import java.util.List;
|
|
|
|
36
|
+import java.util.Map;
|
|
21
|
import java.util.stream.Collectors;
|
37
|
import java.util.stream.Collectors;
|
|
22
|
import javax.annotation.Resource;
|
38
|
import javax.annotation.Resource;
|
|
|
|
39
|
+import javax.servlet.http.HttpServletResponse;
|
|
23
|
import javax.validation.Valid;
|
40
|
import javax.validation.Valid;
|
|
24
|
import javax.validation.constraints.NotBlank;
|
41
|
import javax.validation.constraints.NotBlank;
|
|
|
|
42
|
+
|
|
|
|
43
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
44
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
45
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
25
|
import lombok.extern.slf4j.Slf4j;
|
46
|
import lombok.extern.slf4j.Slf4j;
|
|
26
|
import org.springframework.util.CollectionUtils;
|
47
|
import org.springframework.util.CollectionUtils;
|
|
27
|
import org.springframework.validation.annotation.Validated;
|
48
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -42,6 +63,15 @@ import org.springframework.web.bind.annotation.RestController; |
|
@@ -42,6 +63,15 @@ import org.springframework.web.bind.annotation.RestController; |
|
42
|
@RequestMapping("/procurement/warehouse/receive")
|
63
|
@RequestMapping("/procurement/warehouse/receive")
|
|
43
|
public class ProcurementWarehouseReceiveOrderController extends DefaultBaseController {
|
64
|
public class ProcurementWarehouseReceiveOrderController extends DefaultBaseController {
|
|
44
|
|
65
|
|
|
|
|
66
|
+ /**
|
|
|
|
67
|
+ * 收料明细首行(Excel第6行),模板固定10行(第6~15行)
|
|
|
|
68
|
+ */
|
|
|
|
69
|
+ private static final int RECEIVE_LINE_START_ROW = 5;
|
|
|
|
70
|
+ /**
|
|
|
|
71
|
+ * 收料明细固定行数上限
|
|
|
|
72
|
+ */
|
|
|
|
73
|
+ private static final int RECEIVE_LINE_MAX = 10;
|
|
|
|
74
|
+
|
|
45
|
@Resource
|
75
|
@Resource
|
|
46
|
private ProcurementWarehouseReceiveOrderService warehouseReceiveOrderService;
|
76
|
private ProcurementWarehouseReceiveOrderService warehouseReceiveOrderService;
|
|
47
|
|
77
|
|
|
@@ -131,6 +161,167 @@ public class ProcurementWarehouseReceiveOrderController extends DefaultBaseContr |
|
@@ -131,6 +161,167 @@ public class ProcurementWarehouseReceiveOrderController extends DefaultBaseContr |
|
131
|
return InvokeResultBuilder.success();
|
161
|
return InvokeResultBuilder.success();
|
|
132
|
}
|
162
|
}
|
|
133
|
|
163
|
|
|
|
|
164
|
+ /**
|
|
|
|
165
|
+ * 打印仓库收料单
|
|
|
|
166
|
+ *
|
|
|
|
167
|
+ * @param exportType 导出类型 PDF-导出PDF文件,其他/默认-导出Excel文件
|
|
|
|
168
|
+ */
|
|
|
|
169
|
+ @ApiOperation("打印仓库收料单(exportType=PDF导出PDF文件,默认Excel)")
|
|
|
|
170
|
+ @HasPermission({"warehouse-manage:warehouse-material:print"})
|
|
|
|
171
|
+ @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
|
|
|
|
172
|
+ @GetMapping("/export")
|
|
|
|
173
|
+ public void export(@NotBlank(message = "ID不能为空!") String id, String exportType,
|
|
|
|
174
|
+ HttpServletResponse response) throws IOException {
|
|
|
|
175
|
+
|
|
|
|
176
|
+ GetWarehouseReceiveOrderBo data = warehouseReceiveOrderService.findDetail(id);
|
|
|
|
177
|
+
|
|
|
|
178
|
+ String templateFile = "templates/warehouseReceiveOrderTemplate.xlsx";
|
|
|
|
179
|
+ try {
|
|
|
|
180
|
+ try (InputStream inputStream = new ClassPathResource(templateFile).getStream();
|
|
|
|
181
|
+ Workbook workbook = new XSSFWorkbook(inputStream)) {
|
|
|
|
182
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
183
|
+
|
|
|
|
184
|
+ // 收料明细:固定10行(模板Excel第6~15行),最多10条(超出取前10条),不足留空
|
|
|
|
185
|
+ List<GetWarehouseReceiveOrderBo.LineBo> lines = data.getLines();
|
|
|
|
186
|
+ int lineSize = lines == null ? 0 : Math.min(lines.size(), RECEIVE_LINE_MAX);
|
|
|
|
187
|
+ for (int i = 0; i < lineSize; i++) {
|
|
|
|
188
|
+ fillReceiveLineRow(sheet, RECEIVE_LINE_START_ROW + i, lines.get(i));
|
|
|
|
189
|
+ }
|
|
|
|
190
|
+
|
|
|
|
191
|
+ // 主表占位符替换(${字段} / #{字段},模板里用到哪个替换哪个)
|
|
|
|
192
|
+ LocalDate ticketDate = data.getTicketDate();
|
|
|
|
193
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
|
194
|
+ dataMap.put("year", ticketDate == null ? "" : String.valueOf(ticketDate.getYear()));
|
|
|
|
195
|
+ dataMap.put("month", ticketDate == null ? "" : String.valueOf(ticketDate.getMonthValue()));
|
|
|
|
196
|
+ dataMap.put("day", ticketDate == null ? "" : String.valueOf(ticketDate.getDayOfMonth()));
|
|
|
|
197
|
+ dataMap.put("materialSource", nvl(data.getMaterialSource()));
|
|
|
|
198
|
+ // 计划日期:所有关联计划中最早的计划日期(yyyy-MM-dd)
|
|
|
|
199
|
+ dataMap.put("planDate", data.getPlanDate() == null ? "" : data.getPlanDate().toString());
|
|
|
|
200
|
+ dataMap.put("workshopName", nvl(data.getWorkshopName()));
|
|
|
|
201
|
+ dataMap.put("planCodes", CollectionUtils.isEmpty(data.getPlanCode()) ? ""
|
|
|
|
202
|
+ : String.join(",", data.getPlanCode()));
|
|
|
|
203
|
+ // 合计-实收数/含税金额
|
|
|
|
204
|
+ dataMap.put("totalActualQuantity", formatDecimal(data.getTotalActualQuantity()));
|
|
|
|
205
|
+ dataMap.put("totalTaxAmount", formatDecimal(data.getTotalTaxAmount()));
|
|
|
|
206
|
+ // 合计-实收数中文大写按位拆分:${W}万/${Q}仟/${B}佰/${S}拾/${G}个(取整数部分)
|
|
|
|
207
|
+ BigDecimal totalActualQuantity = data.getTotalActualQuantity();
|
|
|
|
208
|
+ String[] cnDigits = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
|
|
|
209
|
+ long actualLong = totalActualQuantity == null ? 0L
|
|
|
|
210
|
+ : totalActualQuantity.abs().longValue();
|
|
|
|
211
|
+ dataMap.put("W", cnDigits[(int) (actualLong / 10000 % 10)]);
|
|
|
|
212
|
+ dataMap.put("Q", cnDigits[(int) (actualLong / 1000 % 10)]);
|
|
|
|
213
|
+ dataMap.put("B", cnDigits[(int) (actualLong / 100 % 10)]);
|
|
|
|
214
|
+ dataMap.put("S", cnDigits[(int) (actualLong / 10 % 10)]);
|
|
|
|
215
|
+ dataMap.put("G", cnDigits[(int) (actualLong % 10)]);
|
|
|
|
216
|
+ // 实收数整体大写(备用:模板如需整体大写可改用 ${totalActualQuantityUpper})
|
|
|
|
217
|
+ dataMap.put("totalActualQuantityUpper", totalActualQuantity == null ? ""
|
|
|
|
218
|
+ : NumberChineseFormatter.format(totalActualQuantity.doubleValue(), true) + "公斤");
|
|
|
|
219
|
+ // 签名区
|
|
|
|
220
|
+ dataMap.put("gybzgName", nvl(data.getGybzgName()));
|
|
|
|
221
|
+ dataMap.put("gyglkzgName", nvl(data.getGyglkzgName()));
|
|
|
|
222
|
+ dataMap.put("fzcktjyName", nvl(data.getFzcktjyName()));
|
|
|
|
223
|
+ dataMap.put("gyglkzjyName", nvl(data.getGyglkzjyName()));
|
|
|
|
224
|
+ dataMap.put("fzckzjyName", nvl(data.getFzckzjyName()));
|
|
|
|
225
|
+ dataMap.put("createBy", nvl(data.getCreateBy()));
|
|
|
|
226
|
+ ExcelUtil.processTemplateSheet(sheet, dataMap);
|
|
|
|
227
|
+
|
|
|
|
228
|
+ if ("PDF".equals(exportType)) {
|
|
|
|
229
|
+ // 导出 PDF:先写临时 .xlsx 文件,再调 LibreOffice 转换输出
|
|
|
|
230
|
+ ResponseUtil.setPDFResponseHead(response,
|
|
|
|
231
|
+ nvl(data.getWeighingTicketCode()) + "-仓库收料单.pdf");
|
|
|
|
232
|
+ File tempExcel = File.createTempFile(
|
|
|
|
233
|
+ "warehouse_receive_order_" + nvl(data.getWeighingTicketCode()), ".xlsx");
|
|
|
|
234
|
+ try {
|
|
|
|
235
|
+ try (FileOutputStream fos = new FileOutputStream(tempExcel)) {
|
|
|
|
236
|
+ workbook.write(fos);
|
|
|
|
237
|
+ }
|
|
|
|
238
|
+ File pdfFile = ExcelUtil.convertExcelToPdf(tempExcel,
|
|
|
|
239
|
+ "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s");
|
|
|
|
240
|
+ try (InputStream pdfIn = new FileInputStream(pdfFile)) {
|
|
|
|
241
|
+ byte[] buffer = new byte[8192];
|
|
|
|
242
|
+ int len;
|
|
|
|
243
|
+ while ((len = pdfIn.read(buffer)) != -1) {
|
|
|
|
244
|
+ response.getOutputStream().write(buffer, 0, len);
|
|
|
|
245
|
+ }
|
|
|
|
246
|
+ response.getOutputStream().flush();
|
|
|
|
247
|
+ } finally {
|
|
|
|
248
|
+ if (pdfFile != null && pdfFile.exists()) {
|
|
|
|
249
|
+ Files.delete(pdfFile.toPath());
|
|
|
|
250
|
+ }
|
|
|
|
251
|
+ }
|
|
|
|
252
|
+ } finally {
|
|
|
|
253
|
+ if (tempExcel != null && tempExcel.exists()) {
|
|
|
|
254
|
+ Files.delete(tempExcel.toPath());
|
|
|
|
255
|
+ }
|
|
|
|
256
|
+ }
|
|
|
|
257
|
+ } else {
|
|
|
|
258
|
+ // 默认导出 Excel
|
|
|
|
259
|
+ ResponseUtil.setExcelResponseHead(response,
|
|
|
|
260
|
+ nvl(data.getWeighingTicketCode()) + "-仓库收料单.xlsx");
|
|
|
|
261
|
+ workbook.write(response.getOutputStream());
|
|
|
|
262
|
+ response.getOutputStream().flush();
|
|
|
|
263
|
+ }
|
|
|
|
264
|
+ } catch (FileNotFoundException e) {
|
|
|
|
265
|
+ throw new RuntimeException("模板文件不存在: " + templateFile, e);
|
|
|
|
266
|
+ } catch (IOException e) {
|
|
|
|
267
|
+ throw new RuntimeException("无法读取模板文件: " + templateFile, e);
|
|
|
|
268
|
+ }
|
|
|
|
269
|
+ } catch (Exception e) {
|
|
|
|
270
|
+ log.error("打印仓库收料单失败, id={}, {}", id, e.getMessage(), e);
|
|
|
|
271
|
+ }
|
|
|
|
272
|
+ }
|
|
|
|
273
|
+
|
|
|
|
274
|
+ /**
|
|
|
|
275
|
+ * 填充一行收料明细(列布局固定:A材料编号、B材料名称、C规格型号、D单位(模板预填kg)、E采购数、F实收数、
|
|
|
|
276
|
+ * G含税单价、H含税金额、I不含税单价、J不含税金额)
|
|
|
|
277
|
+ */
|
|
|
|
278
|
+ private void fillReceiveLineRow(Sheet sheet, int rowIdx, GetWarehouseReceiveOrderBo.LineBo line) {
|
|
|
|
279
|
+ if (line == null) {
|
|
|
|
280
|
+ return;
|
|
|
|
281
|
+ }
|
|
|
|
282
|
+ if (line.getCategory() != null) {
|
|
|
|
283
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 0, line.getCategory());
|
|
|
|
284
|
+ }
|
|
|
|
285
|
+ if (line.getName() != null) {
|
|
|
|
286
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 1, line.getName());
|
|
|
|
287
|
+ }
|
|
|
|
288
|
+ if (line.getSpecModel() != null) {
|
|
|
|
289
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 2, line.getSpecModel());
|
|
|
|
290
|
+ }
|
|
|
|
291
|
+ if (line.getUnit() != null) {
|
|
|
|
292
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 3, line.getUnit());
|
|
|
|
293
|
+ }
|
|
|
|
294
|
+ if (line.getPurchaseQuantity() != null) {
|
|
|
|
295
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 4, line.getPurchaseQuantity());
|
|
|
|
296
|
+ }
|
|
|
|
297
|
+ if (line.getActualQuantity() != null) {
|
|
|
|
298
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 5, line.getActualQuantity());
|
|
|
|
299
|
+ }
|
|
|
|
300
|
+ if (line.getTaxPrice() != null) {
|
|
|
|
301
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 6, line.getTaxPrice());
|
|
|
|
302
|
+ }
|
|
|
|
303
|
+ if (line.getTaxAmount() != null) {
|
|
|
|
304
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 7, line.getTaxAmount());
|
|
|
|
305
|
+ }
|
|
|
|
306
|
+ if (line.getNoTaxPrice() != null) {
|
|
|
|
307
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 8, line.getNoTaxPrice());
|
|
|
|
308
|
+ }
|
|
|
|
309
|
+ if (line.getNoTaxAmount() != null) {
|
|
|
|
310
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 9, line.getNoTaxAmount());
|
|
|
|
311
|
+ }
|
|
|
|
312
|
+ }
|
|
|
|
313
|
+
|
|
|
|
314
|
+ private String formatDecimal(BigDecimal bd) {
|
|
|
|
315
|
+ if (bd == null) {
|
|
|
|
316
|
+ return "";
|
|
|
|
317
|
+ }
|
|
|
|
318
|
+ return bd.stripTrailingZeros().toPlainString();
|
|
|
|
319
|
+ }
|
|
|
|
320
|
+
|
|
|
|
321
|
+ private String nvl(Object o) {
|
|
|
|
322
|
+ return o == null ? "" : o.toString();
|
|
|
|
323
|
+ }
|
|
|
|
324
|
+
|
|
134
|
private List<String> splitPlanCodes(String planCodes) {
|
325
|
private List<String> splitPlanCodes(String planCodes) {
|
|
135
|
if (planCodes == null || planCodes.isEmpty()) {
|
326
|
if (planCodes == null || planCodes.isEmpty()) {
|
|
136
|
return java.util.Collections.emptyList();
|
327
|
return java.util.Collections.emptyList();
|