From c5052922476026b4fbaeccdf9d140da253309c8f Mon Sep 17 00:00:00 2001 From: akulikov Date: Tue, 30 May 2023 13:34:46 +0300 Subject: [PATCH] http://jira.mfd.msk:8088/browse/CLS-283 some fixes --- .../bp/ExecutedDealReportBuilderCommon.java | 82 +++-- .../bp/LiabilitiesClaimsReportBuilder.java | 344 ++++++++++-------- .../reports/bp/MoneyChangeReportBuilder.java | 135 ++++--- 3 files changed, 317 insertions(+), 244 deletions(-) diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/ExecutedDealReportBuilderCommon.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/ExecutedDealReportBuilderCommon.java index 3affaeedb..d394f1d0c 100644 --- a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/ExecutedDealReportBuilderCommon.java +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/ExecutedDealReportBuilderCommon.java @@ -60,39 +60,59 @@ public abstract class ExecutedDealReportBuilderCommon extends CSVReportBuilde lines = new ArrayList<>(executionFonds.size()); for (ExecutionFond executionFond : executionFonds) { - Company company = companyImdg.getSingleObjectByID(executionFond.getCompanyId()); - if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; - STrades sTrade = sTradesImdg.getSingleObjectByFieldValues( - Map.of( - "tradeNum", executionFond.getExchangeExecutionId(), - "operation", executionFond.getSide() - ) - ); + try { + Company company = companyImdg.getSingleObjectByID(executionFond.getCompanyId()); + if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; - String id = String.valueOf(executionFond.getId()); - String securityId = String.valueOf(executionFond.getSecurityId()); - String tradeNo = String.valueOf(executionFond.getExchangeExecutionId()); - String tradeDate = dateFormatter.format(executionFond.getTradingDate()); - String tradeTime = dateFormatter.format(LocalDate.ofInstant(executionFond.getCreated(), ZoneId.systemDefault())); - String settleDate = dateFormatter.format(executionFond.getSettlementDate()); - String settleCode = sTrade != null ? sTrade.getSettleCode() : ""; - String quantity = String.valueOf(executionFond.getQuantity()); - String value = String.valueOf(executionFond.getSettlementAmount()); - String price = String.valueOf(executionFond.getSettlementAmount().divide(executionFond.getQuantity(), RoundingMode.HALF_UP)); + STrades sTrade = null; + if (executionFond.getSide() != null && executionFond.getExchangeExecutionId() != null) { + sTrade = sTradesImdg.getSingleObjectByFieldValues( + Map.of( + "tradeNum", executionFond.getExchangeExecutionId(), + "operation", executionFond.getSide() + ) + ); + } - int valueIdx = 0; - String[] line = new String[getHeaders().length]; - line[valueIdx++] = id; - line[valueIdx++] = securityId; - line[valueIdx++] = tradeNo; - line[valueIdx++] = tradeDate; - line[valueIdx++] = tradeTime; - line[valueIdx++] = settleDate; - line[valueIdx++] = settleCode; - line[valueIdx++] = price; - line[valueIdx++] = quantity; - line[valueIdx++] = value; - lines.add(line); + String id = String.valueOf(executionFond.getId()); + String securityId = String.valueOf(executionFond.getSecurityId()); + String tradeNo = String.valueOf(executionFond.getExchangeExecutionId()); + String tradeDate = ""; + String tradeTime = ""; + String settleDate = ""; + if (executionFond.getTradingDate() != null) { + tradeDate = dateFormatter.format(executionFond.getTradingDate()); + } + if (executionFond.getCreated() != null) { + tradeTime = dateFormatter.format(LocalDate.ofInstant(executionFond.getCreated(), ZoneId.systemDefault())); + } + if (executionFond.getSettlementDate() != null) { + settleDate = dateFormatter.format(executionFond.getSettlementDate()); + } + String settleCode = sTrade != null ? sTrade.getSettleCode() : ""; + String quantity = String.valueOf(executionFond.getQuantity()); + String value = String.valueOf(executionFond.getSettlementAmount()); + String price = ""; + if (executionFond.getSettlementAmount() != null && executionFond.getQuantity() != null) { + price = String.valueOf(executionFond.getSettlementAmount().divide(executionFond.getQuantity(), RoundingMode.HALF_UP)); + } + + int valueIdx = 0; + String[] line = new String[getHeaders().length]; + line[valueIdx++] = id; + line[valueIdx++] = securityId; + line[valueIdx++] = tradeNo; + line[valueIdx++] = tradeDate; + line[valueIdx++] = tradeTime; + line[valueIdx++] = settleDate; + line[valueIdx++] = settleCode; + line[valueIdx++] = price; + line[valueIdx++] = quantity; + line[valueIdx++] = value; + lines.add(line); + } catch (Throwable e) { + log.error("Can't create report row for execution_fond (id = %d), skipped".formatted(executionFond.getId()), e); + } } } diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/LiabilitiesClaimsReportBuilder.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/LiabilitiesClaimsReportBuilder.java index 97793f6c0..f723df32c 100644 --- a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/LiabilitiesClaimsReportBuilder.java +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/LiabilitiesClaimsReportBuilder.java @@ -89,177 +89,207 @@ public abstract class LiabilitiesClaimsReportBuilder

extends CSVReportBuilder lines = new ArrayList<>(executions.size()); for (ExecutionCommon execution : executions) { - Company company = companyImdg.getSingleObjectByID(execution.getCompanyId()); - if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; + try { + Company company = companyImdg.getSingleObjectByID(execution.getCompanyId()); + if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; - if (execution.getSessionId() == null) { - log.warn("On Execution[{}] sessionId was null. Skip in report.", execution.getId()); - continue; - } - Registry registry = registryImdg.getSingleObjectByFieldValues(Map.of("companyId", execution.getCompanyId())); - Collection registriesForSessionId = registryImdg.getCollectionObjectsByFieldValues( - Map.of("sessionId", execution.getSessionId()) - ); - Listing listing = listingImdg.getSingleObjectByFieldValues(Map.of("securityId", execution.getSecurityId())); - FixedIncomeSecurity fixedIncomeSecurity = fixedIncomeSecurityImdg.getSingleObjectByFieldValues( - Map.of("securityId", execution.getSecurityId()) - ); - TradingClearingRegistry tradingClearingRegistry = tradingClearingRegistryImdg.getSingleObjectByID( - execution.getTradingClearingRegistryId() - ); - - if (registry == null) { - log.warn("registry not found for companyId={}", execution.getCompanyId()); - } - - String id = String.valueOf(execution.getId()); - String docDate = dateFormatter.format(nowDate); - String docTime = timeFormatter.format(nowTime); - String docName = "Отчет об обязательствах/требованиях Участника клиринга"; - String docType = "KS_FIRMID_LIABILITIES"; - String firmId = String.valueOf(execution.getCompanyId()); - String firName = company.getFullName(); - String sessionId = String.valueOf(execution.getSessionId()); - String clearingDate = dateFormatter.format(execution.getClearingDate()); - String clearingTime = dateFormatter.format(execution.getClearingDate()); - String trdAccId = registry == null? null : registry.getTradingClearingRegistry(); - String account = String.valueOf(tradingClearingRegistry.getMoneyAccountId()); - String depoAccount = String.valueOf(tradingClearingRegistry.getDepoAccountId()); - String currency = execution.getSettlementCurrency(); - String trId = String.valueOf(execution.getExchangeExecutionId()); - String securityId = String.valueOf(execution.getSecurityId()); - - String trBuySell = ""; - if (Side.BUY.equalsByKey(execution.getSide())) - trBuySell = "B"; - else if (Side.SELL.equalsByKey(execution.getSide())) - trBuySell = "S"; - String trDate = dateFormatter.format(execution.getTradingDate()); - - String trExecDate = ""; - String execStatus = ""; - String repaymDate = ""; - String trPrice = ""; - String nominal = ""; - String trQuantity = ""; - String trValue = ""; - String trAccruedInt = ""; - String accruedInt = ""; - String cashLiability = ""; - String depoLiability = ""; - if (execution instanceof ExecutionDeposit executionDeposit) { - trExecDate = dateFormatter.format(executionDeposit.getSecondLegSettlementDate()); - if (Market.mkrs.equalsByKey(execution.getMarket())) { - - String coverageStatus = executionDeposit.getCoverageStatus(); - if (Allowed.ALLOWED.equalsByKey(coverageStatus)) execStatus = "Y"; - else if (Allowed.DENIED.equalsByKey(coverageStatus)) execStatus = "N"; - else execStatus = "W"; - - repaymDate = dateFormatter.format(executionDeposit.getSecondLegSettlementDate()); - trPrice = String.valueOf(executionDeposit.getPrice()); - trValue = String.valueOf(executionDeposit.getFirstLegAmount()); - cashLiability = String.valueOf(executionDeposit.getFirstLegAmount()); + if (execution.getSessionId() == null) { + log.warn("On Execution[{}] sessionId was null. Skip in report.", execution.getId()); + continue; } - } else if (execution instanceof ExecutionFond executionFond) { - trExecDate = dateFormatter.format(executionFond.getSettlementDate()); - if (Market.fond.equalsByKey(execution.getMarket())) { - trPrice = String.valueOf( - executionFond.getSettlementAmount().divide(executionFond.getQuantity(), RoundingMode.HALF_UP) + Registry registry = null; + if (execution.getCompanyId() != null) { + registry = registryImdg.getSingleObjectByFieldValues(Map.of("companyId", execution.getCompanyId())); + } + Collection registriesForSessionId = null; + if (execution.getSessionId() != null) { + registriesForSessionId = registryImdg.getCollectionObjectsByFieldValues( + Map.of("sessionId", execution.getSessionId()) ); - trQuantity = String.valueOf(executionFond.getQuantity()); - nominal = String.valueOf(fixedIncomeSecurity.getNominalValue()); - trValue = String.valueOf(executionFond.getSettlementAmount()); - trAccruedInt = String.valueOf(fixedIncomeSecurity.getCoupon()); - accruedInt = String.valueOf( - fixedIncomeSecurity.getCoupon().divide(executionFond.getQuantity(), RoundingMode.HALF_UP) + } + Listing listing = null; + FixedIncomeSecurity fixedIncomeSecurity = null; + if (execution.getSecurityId() != null) { + listing = listingImdg.getSingleObjectByFieldValues(Map.of("securityId", execution.getSecurityId())); + fixedIncomeSecurity = fixedIncomeSecurityImdg.getSingleObjectByFieldValues( + Map.of("securityId", execution.getSecurityId()) ); - cashLiability = String.valueOf(executionFond.getSettlementAmount()); - depoLiability = String.valueOf(executionFond.getQuantity()); } - } + TradingClearingRegistry tradingClearingRegistry = tradingClearingRegistryImdg.getSingleObjectByID( + execution.getTradingClearingRegistryId() + ); - String lotValue = String.valueOf(listing.getLotSize()); - String trLot = String.valueOf(execution.getLots()); - String trClrComm = ""; - String trExhComm = ""; + if (registry == null) { + log.warn("registry not found for companyId={}", execution.getCompanyId()); + } - Registry tm_tRegistry = null; - Registry om_tRegistry = null; - Registry ts_tRegistry = null; - Registry os_tRegistry = null; - for (Registry currRegistry : registriesForSessionId) { - String registryCode = currRegistry.getRegistryCode(); - if (registryCode.endsWith("T")) { - if (registryCode.startsWith("TM")) { - if (tm_tRegistry != null && tm_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) continue; - tm_tRegistry = currRegistry; + String id = String.valueOf(execution.getId()); + String docDate = dateFormatter.format(nowDate); + String docTime = timeFormatter.format(nowTime); + String docName = "Отчет об обязательствах/требованиях Участника клиринга"; + String docType = "KS_FIRMID_LIABILITIES"; + String firmId = String.valueOf(execution.getCompanyId()); + String firName = company.getFullName(); + String sessionId = String.valueOf(execution.getSessionId()); + String clearingDate = dateFormatter.format(execution.getClearingDate()); + String clearingTime = dateFormatter.format(execution.getClearingDate()); + String trdAccId = registry == null ? null : registry.getTradingClearingRegistry(); + String account = String.valueOf(tradingClearingRegistry.getMoneyAccountId()); + String depoAccount = String.valueOf(tradingClearingRegistry.getDepoAccountId()); + String currency = execution.getSettlementCurrency(); + String trId = String.valueOf(execution.getExchangeExecutionId()); + String securityId = String.valueOf(execution.getSecurityId()); + + String trBuySell = ""; + if (Side.BUY.equalsByKey(execution.getSide())) + trBuySell = "B"; + else if (Side.SELL.equalsByKey(execution.getSide())) + trBuySell = "S"; + String trDate = dateFormatter.format(execution.getTradingDate()); + + String trExecDate = ""; + String execStatus = ""; + String repaymDate = ""; + String trPrice = ""; + String nominal = ""; + String trQuantity = ""; + String trValue = ""; + String trAccruedInt = ""; + String accruedInt = ""; + String cashLiability = ""; + String depoLiability = ""; + if (execution instanceof ExecutionDeposit executionDeposit) { + trExecDate = dateFormatter.format(executionDeposit.getSecondLegSettlementDate()); + if (Market.mkrs.equalsByKey(execution.getMarket())) { + + String coverageStatus = executionDeposit.getCoverageStatus(); + if (Allowed.ALLOWED.equalsByKey(coverageStatus)) execStatus = "Y"; + else if (Allowed.DENIED.equalsByKey(coverageStatus)) execStatus = "N"; + else execStatus = "W"; + + repaymDate = dateFormatter.format(executionDeposit.getSecondLegSettlementDate()); + trPrice = String.valueOf(executionDeposit.getPrice()); + trValue = String.valueOf(executionDeposit.getFirstLegAmount()); + cashLiability = String.valueOf(executionDeposit.getFirstLegAmount()); } - if (registryCode.startsWith("OM")) { - if (om_tRegistry != null && om_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) continue; - om_tRegistry = currRegistry; - } - if (registryCode.startsWith("TS")) { - if (ts_tRegistry != null && ts_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) continue; - ts_tRegistry = currRegistry; - } - if (registryCode.startsWith("OS")) { - if (os_tRegistry != null && os_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) continue; - os_tRegistry = currRegistry; + } else if (execution instanceof ExecutionFond executionFond) { + trExecDate = dateFormatter.format(executionFond.getSettlementDate()); + if (Market.fond.equalsByKey(execution.getMarket())) { + if (executionFond.getSettlementAmount() != null && executionFond.getQuantity() != null) { + trPrice = String.valueOf( + executionFond.getSettlementAmount().divide(executionFond.getQuantity(), RoundingMode.HALF_UP) + ); + } + trQuantity = String.valueOf(executionFond.getQuantity()); + if (fixedIncomeSecurity != null) { + nominal = String.valueOf(fixedIncomeSecurity.getNominalValue()); + if (executionFond.getQuantity() != null) { + accruedInt = String.valueOf( + fixedIncomeSecurity.getCoupon().divide(executionFond.getQuantity(), RoundingMode.HALF_UP) + ); + } + trAccruedInt = String.valueOf(fixedIncomeSecurity.getCoupon()); + } + trValue = String.valueOf(executionFond.getSettlementAmount()); + cashLiability = String.valueOf(executionFond.getSettlementAmount()); + depoLiability = String.valueOf(executionFond.getQuantity()); } } - } - String cashNetto = ""; - if (tm_tRegistry != null && om_tRegistry != null) { - cashNetto = String.valueOf(tm_tRegistry.getBalance().subtract(om_tRegistry.getBalance())); - } - String depoNetto = ""; - if (ts_tRegistry != null && os_tRegistry != null) { - depoNetto = String.valueOf(ts_tRegistry.getBalance().subtract(os_tRegistry.getBalance())); - } + String lotValue = ""; + if (listing != null) { + lotValue = String.valueOf(listing.getLotSize()); + } + String trLot = String.valueOf(execution.getLots()); + String trClrComm = ""; + String trExhComm = ""; + + Registry tm_tRegistry = null; + Registry om_tRegistry = null; + Registry ts_tRegistry = null; + Registry os_tRegistry = null; + if (registriesForSessionId != null) { + for (Registry currRegistry : registriesForSessionId) { + String registryCode = currRegistry.getRegistryCode(); + if (registryCode.endsWith("T")) { + if (registryCode.startsWith("TM")) { + if (tm_tRegistry != null && tm_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) + continue; + tm_tRegistry = currRegistry; + } + if (registryCode.startsWith("OM")) { + if (om_tRegistry != null && om_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) + continue; + om_tRegistry = currRegistry; + } + if (registryCode.startsWith("TS")) { + if (ts_tRegistry != null && ts_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) + continue; + ts_tRegistry = currRegistry; + } + if (registryCode.startsWith("OS")) { + if (os_tRegistry != null && os_tRegistry.getUpdated().isAfter(currRegistry.getUpdated())) + continue; + os_tRegistry = currRegistry; + } + } + } + } + String cashNetto = ""; + if (tm_tRegistry != null && om_tRegistry != null) { + cashNetto = String.valueOf(tm_tRegistry.getBalance().subtract(om_tRegistry.getBalance())); + } + + String depoNetto = ""; + if (ts_tRegistry != null && os_tRegistry != null) { + depoNetto = String.valueOf(ts_tRegistry.getBalance().subtract(os_tRegistry.getBalance())); + } - int valueIdx = 0; - String[] line = new String[getHeaders().length]; - line[valueIdx++] = id; - line[valueIdx++] = docDate; - line[valueIdx++] = docTime; - line[valueIdx++] = docName; - line[valueIdx++] = docType; - line[valueIdx++] = clearingHouse; - line[valueIdx++] = firmId; - line[valueIdx++] = firName; - line[valueIdx++] = sessionId; - line[valueIdx++] = clearingDate; - line[valueIdx++] = clearingTime; - line[valueIdx++] = trdAccId; - line[valueIdx++] = account; - line[valueIdx++] = depoAccount; - line[valueIdx++] = currency; - line[valueIdx++] = trId; - line[valueIdx++] = securityId; - line[valueIdx++] = trBuySell; - line[valueIdx++] = trDate; - line[valueIdx++] = trExecDate; - line[valueIdx++] = execStatus; - line[valueIdx++] = repaymDate; - line[valueIdx++] = trPrice; - line[valueIdx++] = nominal; - line[valueIdx++] = lotValue; - line[valueIdx++] = trLot; - line[valueIdx++] = trQuantity; - line[valueIdx++] = trValue; - line[valueIdx++] = trAccruedInt; - line[valueIdx++] = accruedInt; - line[valueIdx++] = trClrComm; - line[valueIdx++] = trExhComm; - line[valueIdx++] = cashLiability; - line[valueIdx++] = depoLiability; - line[valueIdx++] = cashNetto; - line[valueIdx] = depoNetto; - lines.add(line); + int valueIdx = 0; + String[] line = new String[getHeaders().length]; + line[valueIdx++] = id; + line[valueIdx++] = docDate; + line[valueIdx++] = docTime; + line[valueIdx++] = docName; + line[valueIdx++] = docType; + line[valueIdx++] = clearingHouse; + line[valueIdx++] = firmId; + line[valueIdx++] = firName; + line[valueIdx++] = sessionId; + line[valueIdx++] = clearingDate; + line[valueIdx++] = clearingTime; + line[valueIdx++] = trdAccId; + line[valueIdx++] = account; + line[valueIdx++] = depoAccount; + line[valueIdx++] = currency; + line[valueIdx++] = trId; + line[valueIdx++] = securityId; + line[valueIdx++] = trBuySell; + line[valueIdx++] = trDate; + line[valueIdx++] = trExecDate; + line[valueIdx++] = execStatus; + line[valueIdx++] = repaymDate; + line[valueIdx++] = trPrice; + line[valueIdx++] = nominal; + line[valueIdx++] = lotValue; + line[valueIdx++] = trLot; + line[valueIdx++] = trQuantity; + line[valueIdx++] = trValue; + line[valueIdx++] = trAccruedInt; + line[valueIdx++] = accruedInt; + line[valueIdx++] = trClrComm; + line[valueIdx++] = trExhComm; + line[valueIdx++] = cashLiability; + line[valueIdx++] = depoLiability; + line[valueIdx++] = cashNetto; + line[valueIdx] = depoNetto; + lines.add(line); + } catch (Throwable e) { + log.error("Can't create report row for execution (id = %d), skipped".formatted(execution.getId()), e); + } } + } } diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/MoneyChangeReportBuilder.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/MoneyChangeReportBuilder.java index b72a2a004..401c9f0fb 100644 --- a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/MoneyChangeReportBuilder.java +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/reports/bp/MoneyChangeReportBuilder.java @@ -66,65 +66,88 @@ public abstract class MoneyChangeReportBuilder

extends CSVReportBuilder

{ LocalTime nowTime = LocalTime.now(); lines = new ArrayList<>(registries.size()); for (Registry registry : registries) { - Company company = companyImdg.getSingleObjectByID(registry.getCompanyId()); - if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; + try { + Company company = companyImdg.getSingleObjectByID(registry.getCompanyId()); + if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) continue; - Listing listing = listingImdg.getSingleObjectByFieldValues(Map.of("securityId", registry.getSecurityId())); - Statement statement = statementImdg.getSingleObjectByFieldValues(Map.of("addresseeId", registry.getCompanyId())); - PaymentInstruction paymentInstruction = paymentInstructionImdg.getSingleObjectByFieldValues( - Map.of( - "id", registry.getPaymentId(), - "creditLeg_direction", InOutDirection.out.getKey() - ) - ); + Listing listing = null; + if (registry.getSecurityId() != null) { + listing = listingImdg.getSingleObjectByFieldValues(Map.of("securityId", registry.getSecurityId())); + } - String id = String.valueOf(registry.getId()); - String docDate = dateFormatter.format(nowDate); - String docTime = timeFormatter.format(nowTime); - String docName = "Отчет об изменении суммы денежных средств и (или) количества иного имущества Участника клиринга"; - String docType = "KS_FIRMID_ASSETS"; - String firmId = String.valueOf(registry.getCompanyId()); - String firName = company.getFullName(); - String trdAccId = registry.getTradingClearingRegistry(); - String account = String.valueOf(registry.getAccountId()); - String currency = listing.getTradingCurrency(); - String registryCode = String.valueOf(registry.getId()); - String registryName = registry.getRegistryCode(); - String openBalance = String.valueOf(registry.getOpenBalance()); - String changeBalance = String.valueOf(registry.getCloseBalance().subtract(registry.getOpenBalance())); - String addWithdraw = String.valueOf(registry.getCredit().subtract(registry.getDebit())); - String closeBalance = String.valueOf(registry.getCloseBalance()); - String remarks = ""; - String addSum = String.valueOf(registry.getCredit()); - String addTime = dateTimeFormatter.format(LocalDateTime.ofInstant(statement.getCreated(), ZoneId.systemDefault())); - String withdrawSum = String.valueOf(registry.getDebit()); - String withdrawTime = dateTimeFormatter.format(LocalDateTime.ofInstant(paymentInstruction.getPaymentDate(), ZoneId.systemDefault())); + Statement statement = null; + if (registry.getCompanyId() != null) { + statement = statementImdg.getSingleObjectByFieldValues(Map.of("addresseeId", registry.getCompanyId())); + } - int valueIdx = 0; - String[] line = new String[getHeaders().length]; - line[valueIdx++] = id; - line[valueIdx++] = docDate; - line[valueIdx++] = docTime; - line[valueIdx++] = docName; - line[valueIdx++] = docType; - line[valueIdx++] = clearingHouse; - line[valueIdx++] = firmId; - line[valueIdx++] = firName; - line[valueIdx++] = trdAccId; - line[valueIdx++] = account; - line[valueIdx++] = currency; - line[valueIdx++] = registryCode; - line[valueIdx++] = registryName; - line[valueIdx++] = openBalance; - line[valueIdx++] = changeBalance; - line[valueIdx++] = addWithdraw; - line[valueIdx++] = closeBalance; - line[valueIdx++] = remarks; - line[valueIdx++] = addSum; - line[valueIdx++] = addTime; - line[valueIdx++] = withdrawSum; - line[valueIdx++] = withdrawTime; - lines.add(line); + PaymentInstruction paymentInstruction = null; + if (registry.getPaymentId() != null) { + paymentInstruction = paymentInstructionImdg.getSingleObjectByFieldValues( + Map.of( + "id", registry.getPaymentId(), + "creditLeg_direction", InOutDirection.out.getKey() + ) + ); + } + + String id = String.valueOf(registry.getId()); + String docDate = dateFormatter.format(nowDate); + String docTime = timeFormatter.format(nowTime); + String docName = "Отчет об изменении суммы денежных средств и (или) количества иного имущества Участника клиринга"; + String docType = "KS_FIRMID_ASSETS"; + String firmId = String.valueOf(registry.getCompanyId()); + String firName = company.getFullName(); + String trdAccId = registry.getTradingClearingRegistry(); + String account = String.valueOf(registry.getAccountId()); + String currency = listing != null ? listing.getTradingCurrency() : ""; + String registryCode = String.valueOf(registry.getId()); + String registryName = registry.getRegistryCode(); + String openBalance = String.valueOf(registry.getOpenBalance()); + String changeBalance = String.valueOf(registry.getCloseBalance().subtract(registry.getOpenBalance())); + String addWithdraw = String.valueOf(registry.getCredit().subtract(registry.getDebit())); + String closeBalance = String.valueOf(registry.getCloseBalance()); + String remarks = ""; + String addSum = String.valueOf(registry.getCredit()); + String addTime = ""; + if (statement != null && statement.getCreated() != null) { + addTime = dateTimeFormatter.format(LocalDateTime.ofInstant(statement.getCreated(), ZoneId.systemDefault())); + } + String withdrawSum = String.valueOf(registry.getDebit()); + String withdrawTime = ""; + if (paymentInstruction != null) { + withdrawTime = dateTimeFormatter.format(LocalDateTime.ofInstant(paymentInstruction.getPaymentDate(), ZoneId.systemDefault())); + } else if (registry.getClearingDate() != null) { + withdrawTime = dateTimeFormatter.format(registry.getClearingDate()); + } + + int valueIdx = 0; + String[] line = new String[getHeaders().length]; + line[valueIdx++] = id; + line[valueIdx++] = docDate; + line[valueIdx++] = docTime; + line[valueIdx++] = docName; + line[valueIdx++] = docType; + line[valueIdx++] = clearingHouse; + line[valueIdx++] = firmId; + line[valueIdx++] = firName; + line[valueIdx++] = trdAccId; + line[valueIdx++] = account; + line[valueIdx++] = currency; + line[valueIdx++] = registryCode; + line[valueIdx++] = registryName; + line[valueIdx++] = openBalance; + line[valueIdx++] = changeBalance; + line[valueIdx++] = addWithdraw; + line[valueIdx++] = closeBalance; + line[valueIdx++] = remarks; + line[valueIdx++] = addSum; + line[valueIdx++] = addTime; + line[valueIdx++] = withdrawSum; + line[valueIdx++] = withdrawTime; + lines.add(line); + } catch (Throwable e) { + log.error("Can't create report row for registry (id = %d), skipped".formatted(registry.getId()), e); + } } }