Merge branch 'test-caojia' into test

This commit is contained in:
caojia
2026-11-20 13:45:08 +08:00
14 changed files with 427 additions and 69 deletions
@@ -167,6 +167,13 @@ public class ArBillController extends BaseController {
return success(account);
}
/** 生成账单时选择同币种启用账户,账号只返回脱敏值。 */
@PreAuthorize("@ss.hasPermi('ar:bill:generate')")
@GetMapping("/bank-account-options")
public AjaxResult bankAccountOptions(@RequestParam(defaultValue = "CNY") String currency) {
return success(billService.selectMaskedBankAccounts(currency));
}
/**
* 查询生成账单前的订单计费结果。
*/
@@ -0,0 +1,116 @@
package com.bms.receivable;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.sql.Date;
import java.util.List;
import com.bms.common.core.domain.model.LoginUser;
import com.bms.common.exception.ServiceException;
import com.bms.customer.customerinfo.domain.BmsCustomerOption;
import com.bms.customer.customerinfo.mapper.BmsCustomerMapper;
import com.bms.receivable.domain.ArBankAccount;
import com.bms.receivable.domain.ArBill;
import com.bms.receivable.domain.vo.ArBillGenerateRequest;
import com.bms.receivable.mapper.ArBankAccountMapper;
import com.bms.receivable.mapper.ArBillGenerateTaskMapper;
import com.bms.receivable.mapper.ArBillMapper;
import com.bms.receivable.security.ArBillScopeResolver;
import com.bms.receivable.service.IArBillLogService;
import com.bms.receivable.service.impl.ArBillServiceImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;
class ArBillGenerateBankAccountTest {
private final ArBillServiceImpl service = new ArBillServiceImpl();
private final ArBillMapper bills = mock(ArBillMapper.class);
private final ArBankAccountMapper accounts = mock(ArBankAccountMapper.class);
private final ArBillGenerateTaskMapper tasks = mock(ArBillGenerateTaskMapper.class);
private final ArBillScopeResolver scope = mock(ArBillScopeResolver.class);
@BeforeEach
void setUp() {
LoginUser login = new LoginUser();
login.setUserId(1L);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(login, null, List.of()));
ReflectionTestUtils.setField(service, "billMapper", bills);
ReflectionTestUtils.setField(service, "bankAccountMapper", accounts);
ReflectionTestUtils.setField(service, "generateTaskMapper", tasks);
ReflectionTestUtils.setField(service, "billScope", scope);
ReflectionTestUtils.setField(service, "customerMapper", mock(BmsCustomerMapper.class));
ReflectionTestUtils.setField(service, "eventPublisher", mock(ApplicationEventPublisher.class));
ReflectionTestUtils.setField(service, "logService", mock(IArBillLogService.class));
BmsCustomerOption customer = new BmsCustomerOption();
customer.setCustomerCode("C1");
customer.setCustomerName("客户");
when(scope.requireCustomerAccessible(1L)).thenReturn(customer);
}
@AfterEach
void clearLogin() { SecurityContextHolder.clearContext(); }
@Test
void selectedAccountIsValidatedAndSavedAsBillSnapshot() {
ArBankAccount account = account("CNY");
when(accounts.selectEnabledById(9L)).thenReturn(account);
ArBillGenerateRequest request = request();
request.setBankAccountId(9L);
request.setBankAccountNo("前端伪造的账号");
service.generateBill(request, "admin", "request-1");
ArgumentCaptor<ArBill> saved = ArgumentCaptor.forClass(ArBill.class);
verify(bills).insertArBill(saved.capture());
assertEquals(account.getAccountName(), saved.getValue().getBankAccountName());
assertEquals(account.getAccountNo(), saved.getValue().getBankAccountNo());
assertEquals(account.getBankName(), saved.getValue().getBankName());
verify(accounts, never()).selectDefaultByCurrency(anyString());
}
@Test
void missingOrWrongCurrencyAccountCannotCreateBillAndOldRequestsUseDefault() {
ArBillGenerateRequest request = request();
request.setBankAccountId(9L);
assertThrows(ServiceException.class, () -> service.generateBill(request, "admin", "missing"));
when(accounts.selectEnabledById(9L)).thenReturn(account("USD"));
assertThrows(ServiceException.class, () -> service.generateBill(request, "admin", "wrong-currency"));
verify(bills, never()).insertArBill(any());
request.setBankAccountId(null);
when(accounts.selectDefaultByCurrency("CNY")).thenReturn(account("CNY"));
assertDoesNotThrow(() -> service.generateBill(request, "admin", "legacy"));
verify(accounts).selectDefaultByCurrency("CNY");
}
@Test
void bankAccountOptionsMaskNumbersWithoutSpaces() {
ArBankAccount account = account("CNY");
when(accounts.selectEnabledList("CNY")).thenReturn(List.of(account));
assertEquals("****2050", service.selectMaskedBankAccounts("CNY").get(0).getAccountNo());
}
private static ArBankAccount account(String currency) {
ArBankAccount account = new ArBankAccount();
account.setAccountName("收款单位");
account.setAccountNo("8602 1110 0008 9205 0");
account.setBankName("开户行");
account.setCurrency(currency);
return account;
}
private static ArBillGenerateRequest request() {
ArBillGenerateRequest request = new ArBillGenerateRequest();
request.setCustomerId(1L);
request.setBillName("测试账单");
request.setBillPeriodEnd(Date.valueOf("2026-10-31"));
request.setSourceBillIds(List.of(1L));
return request;
}
}
@@ -111,6 +111,41 @@ class ArBillTemplateSelectionTest {
assertEquals("应收账单汇总模板", options.get(0).getTemplateName());
}
@Test
void fixedPayeeTemplatesRejectMismatchesBeforeCreatingExportTasks() {
ArExportTemplate fixed = template(3L, ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE);
fixed.setStorageKey("templates/receivable/ar_bill_template_gz.xlsx");
when(templates.selectEnabledById(3L, "AR_BILL", bill.getBillDate())).thenReturn(fixed);
bill.setBankAccountName("威狮国际供应链(深圳)有限公司广州分公司");
bill.setBankAccountNo("44050129162500001250");
bill.setBankName("中国建设银行股份有限公司广州设计之都支行");
assertEquals(3L, service.exportBill(7L, 3L, "admin", "matching").getTemplateId());
bill.setBankAccountNo("1234567890");
ServiceException error = assertThrows(ServiceException.class,
() -> service.exportBill(7L, 3L, "admin", "mismatch"));
assertTrue(error.getMessage().contains("收款信息"));
verify(exports, times(1)).insert(any());
}
@Test
void fixedPayeeTemplateValidationChecksNameAccountAndBankForBothTemplates() {
ArBillWorkbookWriters writers = new ArBillWorkbookWriters(new DefaultArBillWorkbookWriter(), new SummaryArBillWorkbookWriter());
ArExportTemplate fixed = template(3L, ArBillWorkbookWriters.JIYUN_TEMPLATE_CODE);
fixed.setStorageKey("templates/receivable/ar_bill_template_jy.xlsx");
bill.setBankAccountName("威狮国际集运物流(深圳)有限公司");
bill.setBankAccountNo("86021110000892050");
bill.setBankName("宁波银行深圳后海支行");
assertDoesNotThrow(() -> writers.validatePayee(bill, fixed));
bill.setBankName("其他银行");
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed));
bill.setBankName("宁波银行深圳后海支行");
bill.setBankAccountName("其他公司");
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed));
bill.setBankAccountName(null);
assertThrows(ServiceException.class, () -> writers.validatePayee(bill, fixed));
}
@Test
void mapperQueriesEnforceBusinessStatusEffectiveDatesAndTaskTemplate() throws Exception {
Configuration configuration = new Configuration();
@@ -11,6 +11,8 @@ import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -24,6 +26,8 @@ import com.bms.receivable.domain.ArExportTemplate;
import com.bms.receivable.mapper.ArBillDetailFeeMapper;
import com.bms.receivable.mapper.ArBillDetailMapper;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -35,12 +39,42 @@ class ArBillWorkbookWriterTest {
@TempDir
Path temporary;
@Test
void summaryDetailsKeepReceiptDateTimeAsExcelDateForNormalAndHighlightedRows() throws Exception {
ArBillDetail normal = detail(1, "P1", "1.00");
normal.setReceiveTime(Timestamp.valueOf("2026-10-01 21:32:51"));
ArBillDetail failed = detail(2, "P1", "1.00");
failed.setReceiveTime(Timestamp.valueOf("2026-10-02 00:01:02"));
failed.setBillingFailStatus(2);
ArBillDetail missing = detail(3, "P1", "1.00");
missing.setReceiveTime(null);
Path target = temporary.resolve("summary-receipt-times.xlsx");
summaryWriter(List.of(normal, failed, missing)).write(bill(3, "3.00", "CNY"), summaryTemplate(), target);
try (XSSFWorkbook workbook = new XSSFWorkbook(target.toFile())) {
XSSFSheet sheet = workbook.getSheetAt(1);
var normalCell = sheet.getRow(4).getCell(1);
var failedCell = sheet.getRow(5).getCell(1);
assertEquals(CellType.NUMERIC, normalCell.getCellType());
assertEquals(CellType.NUMERIC, failedCell.getCellType());
assertTrue(DateUtil.isCellDateFormatted(normalCell));
assertTrue(DateUtil.isCellDateFormatted(failedCell));
assertEquals("yyyy/mm/dd", normalCell.getCellStyle().getDataFormatString());
assertEquals("2026/10/01", new DataFormatter().formatCellValue(normalCell));
assertEquals(LocalDateTime.of(2026, 10, 1, 21, 32, 51), normalCell.getLocalDateTimeCellValue());
assertEquals("2026/10/02", new DataFormatter().formatCellValue(failedCell));
assertEquals(LocalDateTime.of(2026, 10, 2, 0, 1, 2), failedCell.getLocalDateTimeCellValue());
assertEquals(IndexedColors.YELLOW.getIndex(), failedCell.getCellStyle().getFillForegroundColor());
assertEquals("", sheet.getRow(6).getCell(1).getStringCellValue());
}
}
@Test
void summaryAndDetailsReconcileSnapshotAmountsAndKeepFreightFormulaAndBlankFields() throws Exception {
ArBill bill = bill(3, "13.20", "CNY");
List<ArBillDetail> details = List.of(detail(1, "P1", "10.12"), detail(2, "P1", "-2.22"),
detail(3, "P2", "5.30"));
// 相同名称的不同产品仍需分组;收货日期也不能替代账单所属期。
// 相同名称的不同产品仍需分组;日期范围统一按本账单明细的收货日期展示。
details.forEach(row -> row.setProductName("相同产品名"));
Path target = temporary.resolve("summary.xlsx");
SummaryArBillWorkbookWriter writer = summaryWriter(details);
@@ -54,7 +88,7 @@ class ArBillWorkbookWriterTest {
assertEquals("客户 & <测试>", summary.getRow(3).getCell(1).getStringCellValue());
assertEquals("相同产品名", summary.getRow(3).getCell(2).getStringCellValue());
assertEquals("相同产品名", summary.getRow(4).getCell(2).getStringCellValue());
assertEquals("20261001-20261031", summary.getRow(3).getCell(3).getStringCellValue());
assertEquals("20261105-20261105", summary.getRow(3).getCell(3).getStringCellValue());
assertEquals("国际货物运输代理服务费", summary.getRow(4).getCell(6).getStringCellValue());
assertEquals(7.90, summary.getRow(3).getCell(4).getNumericCellValue(), 0.00001);
assertEquals(5.30, summary.getRow(4).getCell(4).getNumericCellValue(), 0.00001);
@@ -65,7 +99,7 @@ class ArBillWorkbookWriterTest {
assertTrue(summary.getMergedRegions().stream().anyMatch(region -> "A11:B11".equals(region.formatAsString())));
assertEquals("客户 & <测试>", freight.getRow(2).getCell(1).getStringCellValue());
assertEquals("20261001-20261031", freight.getRow(2).getCell(18).getStringCellValue());
assertEquals("20261105-20261105", freight.getRow(2).getCell(18).getStringCellValue());
assertEquals("0000123", freight.getRow(4).getCell(0).getStringCellValue());
assertEquals("00000001", freight.getRow(4).getCell(2).getStringCellValue());
assertEquals("001234567890123456789", freight.getRow(4).getCell(3).getStringCellValue());
@@ -206,6 +240,53 @@ class ArBillWorkbookWriterTest {
}
}
@Test
void summaryDatesUseTheWholeBillsReceiveRangeInsteadOfItsStoredPeriod() throws Exception {
ArBill bill = bill(3, "3.00", "CNY");
bill.setBillPeriodStart(Date.valueOf("2026-09-29"));
bill.setBillPeriodEnd(Date.valueOf("2026-10-31"));
ArBillDetail middle = detail(1, "P1", "1.00");
middle.setReceiveTime(Date.valueOf("2026-10-20"));
ArBillDetail earliest = detail(2, "P2", "1.00");
earliest.setReceiveTime(Date.valueOf("2026-10-01"));
ArBillDetail latest = detail(3, "P1", "1.00");
latest.setReceiveTime(Date.valueOf("2026-10-30"));
SummaryArBillWorkbookWriter writer = summaryWriter(List.of(middle, earliest, latest));
when(writer.detailMapper.selectReceiveTimeRangeByBillId(7L)).thenReturn(Map.of(
"minReceiveTime", LocalDateTime.of(2026, 10, 1, 8, 30),
"maxReceiveTime", Timestamp.valueOf("2026-10-30 23:50:00")));
Path target = temporary.resolve("receive-dates.xlsx");
writer.write(bill, summaryTemplate(), target);
try (XSSFWorkbook workbook = new XSSFWorkbook(target.toFile())) {
XSSFSheet summary = workbook.getSheetAt(0);
XSSFSheet freight = workbook.getSheetAt(1);
assertEquals("20261001-20261030", summary.getRow(3).getCell(3).getStringCellValue());
assertEquals("20261001-20261030", summary.getRow(4).getCell(3).getStringCellValue());
assertEquals("20261001-20261030", freight.getRow(2).getCell(18).getStringCellValue());
assertEquals(LocalDateTime.of(2026, 10, 20, 0, 0), freight.getRow(4).getCell(1).getLocalDateTimeCellValue());
assertEquals(LocalDateTime.of(2026, 10, 1, 0, 0), freight.getRow(5).getCell(1).getLocalDateTimeCellValue());
assertEquals(LocalDateTime.of(2026, 10, 30, 0, 0), freight.getRow(6).getCell(1).getLocalDateTimeCellValue());
}
verify(writer.detailMapper, times(1)).selectReceiveTimeRangeByBillId(7L);
assertEquals(Date.valueOf("2026-09-29"), bill.getBillPeriodStart());
assertEquals(Date.valueOf("2026-10-31"), bill.getBillPeriodEnd());
}
@Test
void summaryDatesStayBlankWhenReceiveTimesAreMissing() throws Exception {
ArBillDetail detail = detail(1, "P1", "1.00");
detail.setReceiveTime(null);
SummaryArBillWorkbookWriter writer = summaryWriter(List.of(detail));
when(writer.detailMapper.selectReceiveTimeRangeByBillId(7L)).thenReturn(Map.of());
Path target = temporary.resolve("missing-receive-dates.xlsx");
writer.write(bill(1, "1.00", "CNY"), summaryTemplate(), target);
try (XSSFWorkbook workbook = new XSSFWorkbook(target.toFile())) {
assertEquals("", workbook.getSheetAt(0).getRow(3).getCell(3).getStringCellValue());
assertEquals("", workbook.getSheetAt(1).getRow(2).getCell(18).getStringCellValue());
assertEquals("", workbook.getSheetAt(1).getRow(4).getCell(1).getStringCellValue());
}
}
@Test
void fixedPayeeTemplatesKeepTheirOwnBankDetailsAndSecondSheetWhileFillingOrders() throws Exception {
for (String code : List.of(ArBillWorkbookWriters.GUANGZHOU_TEMPLATE_CODE,
@@ -277,6 +358,8 @@ class ArBillWorkbookWriterTest {
private SummaryArBillWorkbookWriter summaryWriter(List<ArBillDetail> details) {
SummaryArBillWorkbookWriter writer = new SummaryArBillWorkbookWriter();
writer.detailMapper = mapper(details);
when(writer.detailMapper.selectReceiveTimeRangeByBillId(7L)).thenReturn(Map.of(
"minReceiveTime", Date.valueOf("2026-11-05"), "maxReceiveTime", Date.valueOf("2026-11-05")));
writer.detailFeeMapper = mock(ArBillDetailFeeMapper.class);
when(writer.detailFeeMapper.selectByDetailIds(anyList())).thenReturn(List.of());
return writer;