Commit 1f4c875074783fa291adcbfc364c755478e77af2

Authored by 房远帅
1 parent 5c027d07

采购-入库:仓库收料单打印

... ... @@ -40,6 +40,10 @@ public class GetWarehouseReceiveOrderBo {
40 40 @ApiModelProperty("计划编号集合")
41 41 private List<String> planCode = new ArrayList<>();
42 42
  43 + @ApiModelProperty("计划日期(所有关联计划中最早的计划日期)")
  44 + @JsonFormat(pattern = StringPool.DATE_PATTERN)
  45 + private LocalDate planDate;
  46 +
43 47 @ApiModelProperty("分厂ID")
44 48 private String workshopId;
45 49
... ...
1 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 5 import com.lframework.starter.web.core.annotations.security.HasPermission;
4 6 import com.lframework.starter.web.core.components.resp.InvokeResult;
5 7 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
... ... @@ -14,14 +16,33 @@ import com.lframework.xingyun.sc.procurement.vo.receive.CreateWarehouseReceiveOr
14 16 import com.lframework.xingyun.sc.procurement.vo.receive.QueryWarehouseReceiveOrderVo;
15 17 import com.lframework.xingyun.sc.procurement.vo.receive.UpdateWarehouseReceiveActualQuantityVo;
16 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 21 import io.swagger.annotations.Api;
18 22 import io.swagger.annotations.ApiImplicitParam;
19 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 35 import java.util.List;
  36 +import java.util.Map;
21 37 import java.util.stream.Collectors;
22 38 import javax.annotation.Resource;
  39 +import javax.servlet.http.HttpServletResponse;
23 40 import javax.validation.Valid;
24 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 46 import lombok.extern.slf4j.Slf4j;
26 47 import org.springframework.util.CollectionUtils;
27 48 import org.springframework.validation.annotation.Validated;
... ... @@ -42,6 +63,15 @@ import org.springframework.web.bind.annotation.RestController;
42 63 @RequestMapping("/procurement/warehouse/receive")
43 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 75 @Resource
46 76 private ProcurementWarehouseReceiveOrderService warehouseReceiveOrderService;
47 77
... ... @@ -131,6 +161,167 @@ public class ProcurementWarehouseReceiveOrderController extends DefaultBaseContr
131 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 325 private List<String> splitPlanCodes(String planCodes) {
135 326 if (planCodes == null || planCodes.isEmpty()) {
136 327 return java.util.Collections.emptyList();
... ...
... ... @@ -212,6 +212,12 @@ public class ProcurementWarehouseReceiveOrder extends BaseEntity implements Base
212 212 private LocalDateTime updateTime;
213 213
214 214 /**
  215 + * 计划日期(所有关联计划中最早的计划日期,联表 procurement_plan 带出,非数据库字段)
  216 + */
  217 + @TableField(exist = false)
  218 + private LocalDate planDate;
  219 +
  220 + /**
215 221 * 分厂编号(联表 base_data_workshop 带出,非数据库字段;分厂审核节点编码 = WAREHOUSE_RECEIVE_ORDER_ + 分厂前缀 + 环节缩写)
216 222 */
217 223 @TableField(exist = false)
... ...
... ... @@ -17,6 +17,7 @@
17 17 <result column="workshop_id" property="workshopId"/>
18 18 <result column="workshop_name" property="workshopName"/>
19 19 <result column="workshop_code" property="workshopCode"/>
  20 + <result column="plan_date" property="planDate"/>
20 21 <result column="remark" property="remark"/>
21 22 <result column="attachment_file_ids" property="attachmentFileIds"/>
22 23 <result column="attachment_file_names" property="attachmentFileNames"/>
... ... @@ -59,6 +60,11 @@
59 60 tb.workshop_id,
60 61 tb.workshop_name,
61 62 ws.code AS workshop_code,
  63 + (SELECT MIN(pp.date)
  64 + FROM procurement_warehouse_receive_order_line pwrol
  65 + LEFT JOIN procurement_plan pp ON pp.id = pwrol.procurement_plan_id
  66 + WHERE pwrol.receive_order_id = tb.id
  67 + ) AS plan_date,
62 68 tb.remark,
63 69 tb.attachment_file_ids,
64 70 tb.attachment_file_names,
... ...