diff --git a/src/main/java/com/rbkmoney/newway/dao/invoicing/iface/AdjustmentDao.java b/src/main/java/com/rbkmoney/newway/dao/invoicing/iface/AdjustmentDao.java index 6e7cdbf..e8fc06c 100644 --- a/src/main/java/com/rbkmoney/newway/dao/invoicing/iface/AdjustmentDao.java +++ b/src/main/java/com/rbkmoney/newway/dao/invoicing/iface/AdjustmentDao.java @@ -10,8 +10,6 @@ public interface AdjustmentDao extends GenericDao { Adjustment get(String invoiceId, String paymentId, String adjustmentId) throws DaoException; - void updateCommissions(Long adjId) throws DaoException; - void updateNotCurrent(Long id) throws DaoException; } diff --git a/src/main/java/com/rbkmoney/newway/dao/invoicing/impl/AdjustmentDaoImpl.java b/src/main/java/com/rbkmoney/newway/dao/invoicing/impl/AdjustmentDaoImpl.java index 9495481..2d69bcf 100644 --- a/src/main/java/com/rbkmoney/newway/dao/invoicing/impl/AdjustmentDaoImpl.java +++ b/src/main/java/com/rbkmoney/newway/dao/invoicing/impl/AdjustmentDaoImpl.java @@ -3,19 +3,16 @@ package com.rbkmoney.newway.dao.invoicing.impl; import com.rbkmoney.dao.impl.AbstractGenericDao; import com.rbkmoney.mapper.RecordRowMapper; import com.rbkmoney.newway.dao.invoicing.iface.AdjustmentDao; -import com.rbkmoney.newway.domain.enums.PaymentChangeType; import com.rbkmoney.newway.domain.tables.pojos.Adjustment; import com.rbkmoney.newway.domain.tables.records.AdjustmentRecord; import com.rbkmoney.newway.exception.DaoException; import org.jooq.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.stereotype.Component; import javax.sql.DataSource; - import java.util.Optional; import static com.rbkmoney.newway.domain.Tables.ADJUSTMENT; @@ -55,17 +52,6 @@ public class AdjustmentDaoImpl extends AbstractGenericDao implements AdjustmentD return fetchOne(query, adjustmentRowMapper); } - @Override - public void updateCommissions(Long adjId) throws DaoException { - MapSqlParameterSource params = new MapSqlParameterSource("adjId", adjId).addValue("objType", PaymentChangeType.adjustment.name()); - this.getNamedParameterJdbcTemplate().update( - "UPDATE nw.adjustment SET fee = (SELECT nw.get_adjustment_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = :adjId AND obj_type = CAST(:objType as nw.payment_change_type)), " + - "provider_fee = (SELECT nw.get_adjustment_provider_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = :adjId AND obj_type = CAST(:objType as nw.payment_change_type)), " + - "external_fee = (SELECT nw.get_adjustment_external_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = :adjId AND obj_type = CAST(:objType as nw.payment_change_type)) " + - "WHERE id = :adjId", - params); - } - @Override public void updateNotCurrent(Long id) throws DaoException { Query query = getDslContext().update(ADJUSTMENT).set(ADJUSTMENT.CURRENT, false).where(ADJUSTMENT.ID.eq(id)); diff --git a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentCreatedHandler.java b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentCreatedHandler.java index f4a492a..82ac6c8 100644 --- a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentCreatedHandler.java +++ b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentCreatedHandler.java @@ -99,6 +99,11 @@ public class InvoicePaymentAdjustmentCreatedHandler extends AbstractInvoicingHan } } + Long oldAmount = CashFlowUtil.computeMerchantAmount(invoicePaymentAdjustment.getOldCashFlowInverse()); + Long newAmount = CashFlowUtil.computeMerchantAmount(invoicePaymentAdjustment.getNewCashFlow()); + long amount = newAmount + oldAmount; + adjustment.setAmount(amount); + Long adjId = adjustmentDao.save(adjustment); if (adjId != null) { List newCashFlowList = CashFlowUtil.convertCashFlows( @@ -113,7 +118,6 @@ public class InvoicePaymentAdjustmentCreatedHandler extends AbstractInvoicingHan PaymentChangeType.adjustment, AdjustmentCashFlowType.old_cash_flow_inverse); cashFlowDao.save(oldCashFlowList); - adjustmentDao.updateCommissions(adjId); } log.info("Adjustment has been saved, sequenceId={}, invoiceId={}, paymentId={}, adjustmentId={}", diff --git a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentStatusChangedHandler.java b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentStatusChangedHandler.java index 148ce3b..296dc9c 100644 --- a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentStatusChangedHandler.java +++ b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/invoicing/adjustment/InvoicePaymentAdjustmentStatusChangedHandler.java @@ -13,12 +13,10 @@ import com.rbkmoney.geck.filter.rule.PathConditionRule; import com.rbkmoney.machinegun.eventsink.MachineEvent; import com.rbkmoney.newway.dao.invoicing.iface.AdjustmentDao; import com.rbkmoney.newway.dao.invoicing.iface.CashFlowDao; -import com.rbkmoney.newway.dao.invoicing.iface.PaymentDao; import com.rbkmoney.newway.domain.enums.AdjustmentCashFlowType; import com.rbkmoney.newway.domain.enums.AdjustmentStatus; import com.rbkmoney.newway.domain.tables.pojos.Adjustment; import com.rbkmoney.newway.domain.tables.pojos.CashFlow; -import com.rbkmoney.newway.domain.tables.pojos.Payment; import com.rbkmoney.newway.exception.NotFoundException; import com.rbkmoney.newway.poller.event_stock.impl.invoicing.AbstractInvoicingHandler; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/rbkmoney/newway/util/CashFlowUtil.java b/src/main/java/com/rbkmoney/newway/util/CashFlowUtil.java index 496af9d..4459793 100644 --- a/src/main/java/com/rbkmoney/newway/util/CashFlowUtil.java +++ b/src/main/java/com/rbkmoney/newway/util/CashFlowUtil.java @@ -2,6 +2,7 @@ package com.rbkmoney.newway.util; import com.rbkmoney.damsel.domain.FinalCashFlowAccount; import com.rbkmoney.damsel.domain.FinalCashFlowPosting; +import com.rbkmoney.damsel.domain.MerchantCashFlowAccount; import com.rbkmoney.geck.common.util.TBaseUtil; import com.rbkmoney.geck.common.util.TypeUtil; import com.rbkmoney.newway.domain.enums.AdjustmentCashFlowType; @@ -18,6 +19,26 @@ import java.util.function.Predicate; import java.util.stream.Collectors; public class CashFlowUtil { + + public static Long computeMerchantAmount(List finalCashFlow) { + long amountSource = computeAmount(finalCashFlow, FinalCashFlowPosting::getSource); + long amountDest = computeAmount(finalCashFlow, FinalCashFlowPosting::getDestination); + return amountDest - amountSource; + } + + private static long computeAmount(List finalCashFlow, + Function func) { + return finalCashFlow.stream() + .filter(f -> isMerchantSettlement(func.apply(f).getAccountType())) + .mapToLong(cashFlow -> cashFlow.getVolume().getAmount()) + .sum(); + } + + private static boolean isMerchantSettlement(com.rbkmoney.damsel.domain.CashFlowAccount cashFlowAccount) { + return cashFlowAccount.isSetMerchant() && + cashFlowAccount.getMerchant() == MerchantCashFlowAccount.settlement; + } + public static CashFlowAccount getCashFlowAccountType(FinalCashFlowAccount cfa) { CashFlowAccount sourceAccountType = TypeUtil.toEnumField(cfa.getAccountType().getSetField().getFieldName(), CashFlowAccount.class); if (sourceAccountType == null) { diff --git a/src/main/resources/db/migration/V60__add_adjustment_cashflow_amount.sql b/src/main/resources/db/migration/V60__add_adjustment_cashflow_amount.sql new file mode 100644 index 0000000..dce3c7f --- /dev/null +++ b/src/main/resources/db/migration/V60__add_adjustment_cashflow_amount.sql @@ -0,0 +1,17 @@ +ALTER TABLE nw.adjustment + ADD amount BIGINT; + +UPDATE nw.adjustment a +SET amount = p.fee - a.fee +FROM nw.payment p +WHERE p.payment_id = a.payment_id + AND p.invoice_id = a.invoice_id + AND p.current; + +ALTER TABLE nw.adjustment + ALTER COLUMN amount SET NOT NULL; + +ALTER TABLE nw.adjustment + DROP COLUMN fee, + DROP COLUMN external_fee, + DROP COLUMN provider_fee; diff --git a/src/test/java/com/rbkmoney/newway/dao/DaoTests.java b/src/test/java/com/rbkmoney/newway/dao/DaoTests.java index c3fb2f1..b5773a8 100644 --- a/src/test/java/com/rbkmoney/newway/dao/DaoTests.java +++ b/src/test/java/com/rbkmoney/newway/dao/DaoTests.java @@ -277,18 +277,6 @@ public class DaoTests extends AbstractAppDaoTests { CashFlow cashFlowAdjustmentAmount = createCashFlow(1L, 1000L, "RUB", 1L, CashFlowAccount.provider, "settlement", 2L, CashFlowAccount.merchant, "settlement", PaymentChangeType.adjustment); cashFlowAdjustmentAmount.setAdjFlowType(AdjustmentCashFlowType.new_cash_flow); cashFlows.add(cashFlowAdjustmentAmount); - CashFlow cashFlowAdjustmentFee = createCashFlow(1L, 10L, "RUB", 2L, CashFlowAccount.merchant, "settlement", 2L, CashFlowAccount.system, "settlement", PaymentChangeType.adjustment); - cashFlowAdjustmentFee.setAdjFlowType(AdjustmentCashFlowType.new_cash_flow); - cashFlows.add(cashFlowAdjustmentFee); - CashFlow cashFlowAdjustmentExternalIncomeFee = createCashFlow(1L, 3L, "RUB", 2L, CashFlowAccount.system, "settlement", 3L, CashFlowAccount.external, "income", PaymentChangeType.adjustment); - cashFlowAdjustmentExternalIncomeFee.setAdjFlowType(AdjustmentCashFlowType.new_cash_flow); - cashFlows.add(cashFlowAdjustmentExternalIncomeFee); - CashFlow cashFlowAdjustmentExternalOutcomeFee = createCashFlow(1L, 3L, "RUB", 2L, CashFlowAccount.system, "settlement", 4L, CashFlowAccount.external, "outcome", PaymentChangeType.adjustment); - cashFlowAdjustmentExternalOutcomeFee.setAdjFlowType(AdjustmentCashFlowType.new_cash_flow); - cashFlows.add(cashFlowAdjustmentExternalOutcomeFee); - CashFlow cashFlowAdjustmentProviderFee = createCashFlow(1L, 3L, "RUB", 2L, CashFlowAccount.system, "settlement", 5L, CashFlowAccount.provider, "settlement", PaymentChangeType.adjustment); - cashFlowAdjustmentProviderFee.setAdjFlowType(AdjustmentCashFlowType.new_cash_flow); - cashFlows.add(cashFlowAdjustmentProviderFee); cashFlowDao.save(cashFlows); @@ -306,11 +294,6 @@ public class DaoTests extends AbstractAppDaoTests { assertEquals(cashFlowPayoutAmount.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_payout_amount(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); assertEquals(cashFlowPayoutFixedFee.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_payout_fixed_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); assertEquals(cashFlowPayoutFee.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_payout_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); - - assertEquals(cashFlowAdjustmentAmount.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_adjustment_amount(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); - assertEquals(cashFlowAdjustmentFee.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_adjustment_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); - assertEquals(cashFlowAdjustmentExternalIncomeFee.getAmount() + cashFlowAdjustmentExternalOutcomeFee.getAmount(), (long) jdbcTemplate.queryForObject("SELECT nw.get_adjustment_external_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); - assertEquals(cashFlowAdjustmentProviderFee.getAmount(), jdbcTemplate.queryForObject("SELECT nw.get_adjustment_provider_fee(nw.cash_flow.*) FROM nw.cash_flow WHERE obj_id = 1", new SingleColumnRowMapper<>(Long.class))); } @Test