sdf04 fix TRAN accounts

SDF57 & SDF06 fix currency on gateway
This commit is contained in:
ialbert 2024-05-03 14:31:26 +03:00
parent 1ff7fb4b23
commit c13f0d8270
4 changed files with 51 additions and 39 deletions

View file

@ -6,6 +6,8 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -86,36 +88,37 @@ public class Sdf04Executor extends AbstractExecutor<SDf04> {
for (SDf04 sdf04 : sdf) { for (SDf04 sdf04 : sdf) {
//обычно мы ищем по группу sdf04, здесь как будто всегда только одна запись, todo нужно прочекать этот момент //обычно мы ищем по группу sdf04, здесь как будто всегда только одна запись, todo нужно прочекать этот момент
log.debug("Process sdf04 record; sdf04.id: {}", sdf04.getId()); log.debug("Process sdf04 record; sdf04.id: {}", sdf04.getId());
Collection<Registry> registries;
Account anltAcc = accountImdg.getFirstObjectBySQL("account = '%s' and accountType = '%s' and currency = '%s'" Account accDeb = accountImdg.getFirstObjectBySQL("account = '%s'".formatted(sdf04.getC_acc_deb()));
.formatted(sdf04.getC_acc_deb(), Account accCred = accountImdg.getFirstObjectBySQL("account = '%s'".formatted(sdf04.getC_acc_cred()));
AccountType.Anlt.getKey(), BiFunction<Account, AccountType, Boolean> isType = (acc, type)
sdf04.getPay_val())); -> acc != null && type.equalsByKey(acc.getAccountType());
// Account tranAcc = accountImdg.getFirstObjectBySQL("account = '%s' and accountType = '%s'"
// .formatted(sdf04.getC_acc_cred(), AccountType.Tran.getKey())); Function<Account, Collection<Registry>> srch = (acc) -> {
// //если сдф04 не по аналитическому счету ни по tran счету, выбираем по тому c_acc_deb/c_acc_cred что пришло Collection<Registry> registries;
// boolean isAnltAcc = anltAcc != null; if (isType.apply(acc, AccountType.Clrn) || isType.apply(acc, AccountType.Tran)) {
// boolean isTranAcc = tranAcc != null; registries = selectRegistryForSDF04(acc.getAccount());
// if (isAnltAcc && isTranAcc) { } else if (isType.apply(acc, AccountType.Anlt)) {
// log.warn("Found ANLT and TRAN acc by sdf04.id: {}", sdf04.getId()); //в сдф04 указан ANLT или TRAN счет, ищем по контракту
// } AnltSearcher.AnltSearch anltSearch = anltSearcher.loadByAnlt(sdf04.getSpecif_1(), sdf04.getPay_val());
if (anltAcc == null) { if (!anltSearch.isFound()) {
registries = selectRegistryForSDF04(sdf04.getC_acc_deb()); log.error("sdf04.id={} search by specif1 failed {}", sdf04.getId(), msgRslv.resolve(anltSearch.getError()));
// String acc = anltAcc == null ? sdf04.getC_acc_deb() : sdf04.getC_acc_cred(); registries = Collections.emptyList();
// registries = selectRegistryForSDF04(acc); } else {
} else { registries = selectRegistryForSDF04(anltSearch.getAccount().getAccount());
//в сдф04 указан ANLT или TRAN счет, ищем по контракту }
AnltSearcher.AnltSearch anltSearch = anltSearcher.loadByAnlt(sdf04.getSpecif_1(), sdf04.getPay_val());
if (!anltSearch.isFound()) {
log.error("sdf04.id={} search by specif1 failed {}", sdf04.getId(), msgRslv.resolve(anltSearch.getError()));
registries = Collections.emptyList();
} else { } else {
registries = selectRegistryForSDF04(anltSearch.getAccount().getAccount()); log.error("FATAL error: no registry search defined for '{}' account type",
acc.getAccountType());
registries = Collections.emptyList();
} }
return registries;
};
srch.apply(accDeb).forEach(am_b -> assets.processByAm_b(am_b, parseString(sdf04.getSum_deb())));
if (isType.apply(accCred, AccountType.Tran)) {
srch.apply(accCred).forEach(am_b -> assets.processByAm_b(am_b, parseString(sdf04.getSum_deb()).negate()));
} }
registries.forEach(am_b -> assets.processByAm_b(am_b, parseString(sdf04.getSum_deb())));
} }
//todo переписать логику поиска на аналогичную по счетам(TRAN)
Session activeSession = sessionImdg.getSingleObjectByFieldValues(Map.of("workflowStatus", Status.Active.getKey())); Session activeSession = sessionImdg.getSingleObjectByFieldValues(Map.of("workflowStatus", Status.Active.getKey()));
Instant now = Instant.now(); Instant now = Instant.now();
if (activeSession != null) { if (activeSession != null) {

View file

@ -167,7 +167,8 @@ public class Sdf06Executor {
statementImdg.insert(stmt); statementImdg.insert(stmt);
log.debug("Statement created: {}", stmt.getId()); log.debug("Statement created: {}", stmt.getId());
if (InOutDirection.out.getKey().equals(stmt.getInOutDirection()) && tradingTimeService.isTradingTime()) { if (InOutDirection.out.getKey().equals(stmt.getInOutDirection()) && tradingTimeService.isTradingTime()) {
requests.add(GatewayRequestCreator.from(stmt, company.getTradingCode(), tcr.getCode())); requests.add(GatewayRequestCreator.from(stmt, company.getTradingCode(), tcr.getCode(),
currency.getCurrencyCode()));
} else { } else {
processedApproved(stmt, sDf06, now, sdf07GroupId); processedApproved(stmt, sDf06, now, sdf07GroupId);
if (tcr != null) { if (tcr != null) {

View file

@ -813,20 +813,26 @@ public class Sdf57Executor extends AbstractExecutor<SDf57> {
} }
String tradingCode = company.getTradingCode(); String tradingCode = company.getTradingCode();
AssetOperationListRequest gatewayRequest = new AssetOperationListRequest(); AssetOperationListRequest gatewayRequest = new AssetOperationListRequest();
AssetOperationRequest req = GatewayRequestCreator.from(stmt, tradingCode, tcrCode); Currency currency = currencyImdg.getSingleObjectByID(stmt.getSecurityId());
AssetOperationRequest req = GatewayRequestCreator.from(
stmt,
tradingCode,
tcrCode,
currency != null ? currency.getCurrencyCode() : CurrencyCode.RUB.getKey());
String specif = stmt.getComment(); String specif = stmt.getComment();
Matcher m; Matcher m;
if (!TextUtil.isEmpty(specif) && (m = csBlkd.matcher(specif)).find()) { if (!TextUtil.isEmpty(specif) && (m = csBlkd.matcher(specif)).find()) {
try { try {
Long sessionId = Long.valueOf(m.group(1)); Long sessionId = Long.valueOf(m.group(1));
List<Registry> d__vs = assets.searchByParams(account.getAccount(), CurrencyCode.RUB.getKey(), company.getId(), D__V) List<Registry> d__vs = assets.searchByParams(account.getAccount(),
.stream() currency != null ? currency.getCurrencyCode() : CurrencyCode.RUB.getKey(),
.filter(rgs -> sessionId.equals(rgs.getSessionId())) company.getId(), D__V)
.toList(); .stream()
.filter(rgs -> sessionId.equals(rgs.getSessionId()))
.toList();
BigDecimal d__vSum = d__vs.stream() BigDecimal d__vSum = d__vs.stream()
.map(rgs -> safeBD(rgs.getBalance())) .map(rgs -> safeBD(rgs.getBalance()))
.reduce(BigDecimal.ZERO, BigDecimal::add); .reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal amount = req.getAmount(); BigDecimal amount = req.getAmount();
req.setAmount(safeBD(amount).subtract(d__vSum)); req.setAmount(safeBD(amount).subtract(d__vSum));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {

View file

@ -1,5 +1,6 @@
package ru.spcex.clearing.service.integration; package ru.spcex.clearing.service.integration;
import java.math.BigDecimal;
import ru.clearing.classes.statics.data.payment.PaymentInstruction; import ru.clearing.classes.statics.data.payment.PaymentInstruction;
import ru.clearing.classes.statics.data.registry.Registry; import ru.clearing.classes.statics.data.registry.Registry;
import ru.clearing.classes.statics.data.statement.Statement; import ru.clearing.classes.statics.data.statement.Statement;
@ -8,14 +9,15 @@ import ru.spcex.platform.enumeration.CurrencyCode;
import ru.spcex.platform.enumeration.InOutDirection; import ru.spcex.platform.enumeration.InOutDirection;
import ru.spcex.platform.utils.number.BigDecimalUtil; import ru.spcex.platform.utils.number.BigDecimalUtil;
import java.math.BigDecimal;
public class GatewayRequestCreator { public class GatewayRequestCreator {
public static AssetOperationRequest from(Statement stmt, String tradingCode, String tcrCode) { public static AssetOperationRequest from(Statement stmt,
String tradingCode,
String tcrCode,
String currency) {
AssetOperationRequest req = new AssetOperationRequest(); AssetOperationRequest req = new AssetOperationRequest();
req.setEntityId(stmt.getId()); req.setEntityId(stmt.getId());
req.setAmount(stmt.getAmount()); req.setAmount(stmt.getAmount());
req.setSecuritySymbol(CurrencyCode.RUB.getKey()); //fixme retrieve security symbol from validator req.setSecuritySymbol(currency);
req.setTradingCode(tradingCode); req.setTradingCode(tradingCode);
req.setCode(tcrCode); req.setCode(tcrCode);
req.setDirection(stmt.getInOutDirection()); req.setDirection(stmt.getInOutDirection());