Bugfix: creates a copy of all matrix rates if only container rates are updated and vice versa
This commit is contained in:
parent
d10f301b3d
commit
67fc5607e3
4 changed files with 105 additions and 2 deletions
|
|
@ -257,6 +257,36 @@ public class ContainerRateRepository {
|
|||
jdbcTemplate.update(sql, ValidityPeriodState.DRAFT.name());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void copyCurrentToDraft() {
|
||||
String sql = """
|
||||
INSERT INTO container_rate (
|
||||
from_node_id,
|
||||
to_node_id,
|
||||
container_rate_type,
|
||||
rate_teu,
|
||||
rate_feu,
|
||||
rate_hc,
|
||||
lead_time,
|
||||
validity_period_id
|
||||
)
|
||||
SELECT
|
||||
cr.from_node_id,
|
||||
cr.to_node_id,
|
||||
cr.container_rate_type,
|
||||
cr.rate_teu,
|
||||
cr.rate_feu,
|
||||
cr.rate_hc,
|
||||
cr.lead_time,
|
||||
(SELECT id FROM validity_period WHERE state = 'DRAFT' LIMIT 1) as validity_period_id
|
||||
FROM container_rate cr
|
||||
INNER JOIN validity_period vp ON cr.validity_period_id = vp.id
|
||||
WHERE vp.state = 'VALID'
|
||||
""";
|
||||
|
||||
jdbcTemplate.update(sql);
|
||||
}
|
||||
|
||||
|
||||
private static class ContainerRateMapper implements RowMapper<ContainerRate> {
|
||||
|
||||
|
|
|
|||
|
|
@ -178,6 +178,23 @@ public class MatrixRateRepository {
|
|||
rate.getValidityPeriodId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void copyCurrentToDraft() {
|
||||
String sql = """
|
||||
INSERT INTO country_matrix_rate (from_country_id, to_country_id, rate, validity_period_id)
|
||||
SELECT
|
||||
cmr.from_country_id,
|
||||
cmr.to_country_id,
|
||||
cmr.rate,
|
||||
(SELECT id FROM validity_period WHERE state = 'DRAFT' LIMIT 1) AS validity_period_id
|
||||
FROM country_matrix_rate cmr
|
||||
INNER JOIN validity_period vp ON cmr.validity_period_id = vp.id
|
||||
WHERE vp.state = 'VALID'
|
||||
""";
|
||||
|
||||
jdbcTemplate.update(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps rows of a {@link ResultSet} to {@link MatrixRate} objects as required by
|
||||
* the {@link JdbcTemplate}.
|
||||
|
|
|
|||
|
|
@ -173,6 +173,45 @@ public class ValidityPeriodRepository {
|
|||
return totalCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there are any draft matrix rates associated with the current draft validity period.
|
||||
*
|
||||
* @return {@code true} if draft matrix rates exist for the current draft validity period;
|
||||
* {@code false} otherwise.
|
||||
*/
|
||||
@Transactional
|
||||
public boolean hasMatrixRateDrafts() {
|
||||
Integer id = getDraftPeriodId();
|
||||
if (id == null) return false;
|
||||
|
||||
String query = "SELECT COUNT(*) FROM country_matrix_rate WHERE validity_period_id = ?";
|
||||
var matrixCount = jdbcTemplate.queryForObject(query, Integer.class, id);
|
||||
|
||||
int totalCount = (matrixCount != null ? matrixCount : 0);
|
||||
|
||||
return totalCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there are any draft matrix rates associated with the current draft validity period.
|
||||
*
|
||||
* @return {@code true} if draft matrix rates exist for the current draft validity period;
|
||||
* {@code false} otherwise.
|
||||
*/
|
||||
@Transactional
|
||||
public boolean hasContainerRateDrafts() {
|
||||
Integer id = getDraftPeriodId();
|
||||
if (id == null) return false;
|
||||
|
||||
|
||||
String query = "SELECT COUNT(*) FROM container_rate WHERE validity_period_id = ?";
|
||||
var containerCount = jdbcTemplate.queryForObject(query, Integer.class, id);
|
||||
|
||||
int totalCount = (containerCount != null ? containerCount : 0);
|
||||
|
||||
return totalCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the draft validity period, making it the new valid period.
|
||||
* The current valid period is marked as expired, and timestamps are adjusted accordingly.
|
||||
|
|
|
|||
|
|
@ -3,9 +3,12 @@ package de.avatic.lcc.service.configuration;
|
|||
import de.avatic.lcc.dto.configuration.rates.StagedRatesDTO;
|
||||
import de.avatic.lcc.model.properties.SystemPropertyMappingId;
|
||||
import de.avatic.lcc.model.rates.ValidityPeriod;
|
||||
import de.avatic.lcc.repositories.rates.ContainerRateRepository;
|
||||
import de.avatic.lcc.repositories.rates.MatrixRateRepository;
|
||||
import de.avatic.lcc.repositories.rates.ValidityPeriodRepository;
|
||||
import de.avatic.lcc.service.access.PropertyService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
|
@ -22,10 +25,14 @@ public class RateApprovalService {
|
|||
|
||||
private final ValidityPeriodRepository validityPeriodRepository;
|
||||
private final PropertyService propertyService;
|
||||
private final MatrixRateRepository matrixRateRepository;
|
||||
private final ContainerRateRepository containerRateRepository;
|
||||
|
||||
public RateApprovalService(ValidityPeriodRepository validityPeriodRepository, PropertyService propertyService) {
|
||||
public RateApprovalService(ValidityPeriodRepository validityPeriodRepository, PropertyService propertyService, MatrixRateRepository matrixRateRepository, ContainerRateRepository containerRateRepository) {
|
||||
this.validityPeriodRepository = validityPeriodRepository;
|
||||
this.propertyService = propertyService;
|
||||
this.matrixRateRepository = matrixRateRepository;
|
||||
this.containerRateRepository = containerRateRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -78,9 +85,19 @@ public class RateApprovalService {
|
|||
/**
|
||||
* Approves and applies all staged (draft) rates.
|
||||
*/
|
||||
@Transactional
|
||||
public void approveRateDrafts() {
|
||||
|
||||
if (validityPeriodRepository.hasRateDrafts()) {
|
||||
var hasContainerDrafts = validityPeriodRepository.hasContainerRateDrafts();
|
||||
var hasMatrixDrafts = validityPeriodRepository.hasMatrixRateDrafts();
|
||||
|
||||
if (hasContainerDrafts || hasMatrixDrafts) {
|
||||
|
||||
if(hasContainerDrafts && !hasMatrixDrafts)
|
||||
matrixRateRepository.copyCurrentToDraft();
|
||||
else if(!hasContainerDrafts && hasMatrixDrafts)
|
||||
containerRateRepository.copyCurrentToDraft();
|
||||
|
||||
validityPeriodRepository.applyDraft();
|
||||
} else {
|
||||
Integer increase = getRenewalIncrease();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue