add more adjustment cashflow sum (#131)

This commit is contained in:
vitaxa 2020-05-27 10:34:27 +03:00 committed by GitHub
parent 3a563f1e16
commit 98579ccbbf
7 changed files with 43 additions and 36 deletions

View File

@ -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;
}

View File

@ -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));

View File

@ -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<CashFlow> 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={}",

View File

@ -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;

View File

@ -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<FinalCashFlowPosting> finalCashFlow) {
long amountSource = computeAmount(finalCashFlow, FinalCashFlowPosting::getSource);
long amountDest = computeAmount(finalCashFlow, FinalCashFlowPosting::getDestination);
return amountDest - amountSource;
}
private static long computeAmount(List<FinalCashFlowPosting> finalCashFlow,
Function<FinalCashFlowPosting, FinalCashFlowAccount> 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) {

View File

@ -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;

View File

@ -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