diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java index 097b8668a..36a053966 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/CleanService.java @@ -84,4 +84,8 @@ public interface CleanService { JSONObject findLogSettleByDate(Date date); JSONObject validTransactions(Date dt, boolean fix, boolean b, boolean b1); + + void distributeBank(Date dt, int clearingId, JSONObject bankDistribution); + + void lockClearingLog(Date dt, int clearingId); } diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java index fd8336fd0..0779043c8 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/core/impl/CleanServiceImpl.java @@ -15,6 +15,7 @@ import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.notice.core.MailService; import au.com.royalpay.payment.manage.signin.beans.TodoNotice; import au.com.royalpay.payment.manage.signin.core.ManagerTodoNoticeProvider; +import au.com.royalpay.payment.manage.support.abafile.ABAConfig; import au.com.royalpay.payment.manage.support.abafile.ABAFile; import au.com.royalpay.payment.manage.support.abafile.ABATemplate; import au.com.royalpay.payment.manage.tradelog.beans.ClearingLogQuery; @@ -57,6 +58,7 @@ import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.format.number.CurrencyStyleFormatter; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.thymeleaf.context.Context; @@ -223,6 +225,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider return data; }).collect(Collectors.toList()); log.put("bank_statistics", bankStatistics); + log.put("editable", DateUtils.isSameDay(log.getDate("settle_date"), new Date()) && log.getBooleanValue("editable")); } total.put("logs", logs); total.put("details", details); @@ -998,6 +1001,7 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider } } sendTaskFinishMessages(ManagerRole.FINANCIAL_STAFF, "清算文件已发送清算方", "发送清算通知"); + clearingLogMapper.lockSettlements(date); } catch (IOException e) { logger.error("生成excel字节数组发生错误"); } catch (URISyntaxException e) { @@ -1070,6 +1074,60 @@ public class CleanServiceImpl implements CleanService, ManagerTodoNoticeProvider return report; } + @Override + @Transactional + public void distributeBank(Date date, int clearingId, JSONObject bankDistribution) { + if (!DateUtils.isSameDay(date, new Date())) { + throw new ForbiddenException("Only today's settlement file can be modified"); + } + JSONObject log = clearingLogMapper.findById(clearingId); + if (log == null || !DateUtils.isSameDay(log.getDate("settle_date"), date)) { + throw new NotFoundException("Settlement log not found"); + } + if (!log.getBooleanValue("editable")) { + throw new ForbiddenException("Settlement log has been sent and unable to edit"); + } + ABAConfig config = ABATemplate.getConfig(); + String defaultBank = config.getRemainsTo(); + clearingDetailMapper.updateAllBanks(defaultBank, clearingId); + List details = clearingDetailMapper.listReportsOfSettlement(clearingId); + details.sort((log1, log2) -> log2.getBigDecimal("clearing_amount").compareTo(log1.getBigDecimal("clearing_amount"))); + bankDistribution.remove(defaultBank); + for (String bank : bankDistribution.keySet()) { + List detailIds = new ArrayList<>(); + ABAConfig.ABABase base = config.getBankBase(bank); + if (base == null) { + throw new BadRequestException("Invalid bank code:" + bank); + } + BigDecimal bankAmount = bankDistribution.getBigDecimal(bank); + for (JSONObject detail : details) { + String detailBank = detail.getString("settle_bank"); + if (defaultBank.equals(detailBank)) { + BigDecimal amount = detail.getBigDecimal("clearing_amount"); + if (amount.compareTo(BigDecimal.ZERO) > 0 && bankAmount.compareTo(amount) > 0) { + detailIds.add(detail.getString("clear_detail_id")); + bankAmount = bankAmount.subtract(amount); + detail.put("settle_bank", bank); + } + } + } + clearingDetailMapper.updateBanks(bank, detailIds); + + } + } + + @Override + public void lockClearingLog(Date date, int clearingId) { + if (!DateUtils.isSameDay(date, new Date())) { + throw new ForbiddenException("Only today's settlement file can be modified"); + } + JSONObject log = clearingLogMapper.findById(clearingId); + if (log == null || !DateUtils.isSameDay(log.getDate("settle_date"), date)) { + throw new NotFoundException("Settlement log not found"); + } + clearingLogMapper.setLogEditable(false, clearingId); + } + private byte[] getZipByteArr(List fileByteArrWithName) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(bos); diff --git a/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java b/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java index d07bb5ea7..74f00e973 100644 --- a/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java +++ b/src/main/java/au/com/royalpay/payment/manage/management/clearing/web/SettlementDevController.java @@ -2,6 +2,7 @@ package au.com.royalpay.payment.manage.management.clearing.web; import au.com.royalpay.payment.manage.management.clearing.core.CleanService; import au.com.royalpay.payment.manage.permission.manager.ManagerMapping; +import au.com.royalpay.payment.manage.support.abafile.ABATemplate; import au.com.royalpay.payment.tools.permission.enums.ManagerRole; import au.com.royalpay.payment.tools.CommonConsts; import au.com.royalpay.payment.tools.exceptions.BadRequestException; @@ -49,6 +50,35 @@ public class SettlementDevController { } } + @GetMapping("/available_banks") + public JSONObject getAvailableBanks() { + List banks = ABATemplate.getConfig().availableBanks(); + JSONObject res = new JSONObject(); + res.put("remains_to", ABATemplate.getConfig().getRemainsTo()); + res.put("banks", banks); + return res; + } + + @PutMapping("/reports/{date}/clearings/{clearingId}/bank_distribution") + public void modifyClearingBank(@PathVariable String date, @PathVariable int clearingId, @RequestBody JSONObject bankDistribution) { + try { + Date dt = dateFormat.parse(date); + cleanService.distributeBank(dt, clearingId, bankDistribution); + } catch (ParseException e) { + throw new BadRequestException("error.payment.valid.invalid_date_format"); + } + } + + @PutMapping("/reports/{date}/clearings/{clearingId}/lock") + public void lockClearingLog(@PathVariable String date,@PathVariable int clearingId){ + try { + Date dt = dateFormat.parse(date); + cleanService.lockClearingLog(dt, clearingId); + } catch (ParseException e) { + throw new BadRequestException("error.payment.valid.invalid_date_format"); + } + } + @RequestMapping("/reports/{date}/settlement_csv") public void getSettlementCsv(@PathVariable String date, HttpServletResponse resp) throws IOException { try { @@ -79,23 +109,23 @@ public class SettlementDevController { } } - @ManagerMapping(value = "/reports/{date}/send_settlement_xlsx/{checkCode}",role = {ManagerRole.FINANCIAL_STAFF}) + @ManagerMapping(value = "/reports/{date}/send_settlement_xlsx/{checkCode}", role = {ManagerRole.FINANCIAL_STAFF}) public JSONObject sendSettlementMail(@PathVariable("date") String date, @PathVariable("checkCode") String checkCode) { Date dt = null; try { dt = dateFormat.parse(date); } catch (ParseException e) { } - return cleanService.sendSettlementMail(dt,checkCode); + return cleanService.sendSettlementMail(dt, checkCode); } - @ManagerMapping(value = "/reports/send_checkcode",role = {ManagerRole.FINANCIAL_STAFF}) - public void sendCheckCode(@ModelAttribute(CommonConsts.MANAGER_STATUS) JSONObject manager){ - cleanService.sendCheckCode(new Date(),manager); + @ManagerMapping(value = "/reports/send_checkcode", role = {ManagerRole.FINANCIAL_STAFF}) + public void sendCheckCode(@ModelAttribute(CommonConsts.MANAGER_STATUS) JSONObject manager) { + cleanService.sendCheckCode(new Date(), manager); } @RequestMapping(value = "/reports/{date}/send_status") - public JSONObject sendStatus(@PathVariable String date){ + public JSONObject sendStatus(@PathVariable String date) { Date dt = null; try { dt = dateFormat.parse(date); diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.java index b4542d52c..e37ac76e9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.java @@ -40,4 +40,9 @@ public interface ClearingDetailMapper { JSONObject listReportByDate(@Param("report_date") Date date, @Param("client_id") int clientId); PageList listClientSettlementLog(JSONObject params, PageBounds pagination); + + void updateAllBanks(@Param("settle_bank") String bank, @Param("clearing_id") int clearingId); + + @AutoSql(type = SqlType.UPDATE) + void updateBanks(@Param("settle_bank") String bank,@Param("clear_detail_id") List clearingDetailIds); } diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingLogMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingLogMapper.java index fa7395a18..ca402dab9 100644 --- a/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingLogMapper.java +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/log/ClearingLogMapper.java @@ -31,11 +31,19 @@ public interface ClearingLogMapper { @AutoSql(type = SqlType.UPDATE) void update(JSONObject update); - List getSettlementChannelsMonthReport(@Param("year") int year, @Param("month") int month); + List getSettlementChannelsMonthReport(@Param("year") int year, @Param("month") int month); List getMondayClearing(@Param("begin") Date begin, @Param("end") Date end); List getSettlementChannelsDailyReport(@Param("date") Date date); JSONObject getSettleDataDailyReport(@Param("date") Date date); + + void lockSettlements(@Param("date") Date date); + + @AutoSql(type = SqlType.SELECT) + JSONObject findById(@Param("clearing_id") int clearingId); + + @AutoSql(type = SqlType.UPDATE) + void setLogEditable(@Param("editable") boolean editable, @Param("clearing_id") int clearingId); } diff --git a/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAConfig.java b/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAConfig.java index 37fff489b..a7b70591f 100644 --- a/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAConfig.java +++ b/src/main/java/au/com/royalpay/payment/manage/support/abafile/ABAConfig.java @@ -3,9 +3,7 @@ package au.com.royalpay.payment.manage.support.abafile; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import org.springframework.boot.context.properties.ConfigurationProperties; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.util.*; /** * Create by yixian at 2018-06-25 17:39 @@ -15,6 +13,7 @@ public class ABAConfig { private Map bank = new HashMap<>(); private String defaultBank; + private String remainsTo; public ABAFile initFile(String bank, Date settleDate) { ABABase base = this.bank.get(bank); @@ -37,11 +36,28 @@ public class ABAConfig { return bank; } + public ABABase getBankBase(String bank) { + return this.bank.get(bank); + } + public ABAConfig setBank(Map bank) { this.bank = bank; return this; } + public String getRemainsTo() { + return remainsTo; + } + + public ABAConfig setRemainsTo(String remainsTo) { + this.remainsTo = remainsTo; + return this; + } + + public List availableBanks() { + return new ArrayList<>(bank.keySet()); + } + public static class ABABase { private boolean manualSending = false; private String bank; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 055ba877d..4c7992c26 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -115,6 +115,7 @@ royalpay.sms.appkey=43390d81e20c5191c278fbf4cd275be2 #清算银行配置 settle.abafile.default-bank=CBA +settle.abafile.remains-to=ANZ settle.abafile.bank.CBA.bank=CBA settle.abafile.bank.CBA.apca=301500 diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml index ef0029c88..76f5845c9 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/log/ClearingDetailMapper.xml @@ -1,6 +1,9 @@ + + update log_clearing_detail set settle_bank=#{bank} where clearing_id=#{clearing_id} +