Merge remote-tracking branch 'origin/develop' into develop
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
This commit is contained in:
@@ -337,19 +337,12 @@ public class EmsController {
|
||||
} catch (Exception e) {
|
||||
logger.error(request.getRequestURI(), e);
|
||||
response = new ServiceRestResponse(EsitoType.KO, e, multiDBTransactionManager.getPrimaryDatasource().getProfile(), entity);
|
||||
} finally {
|
||||
try {
|
||||
multiDBTransactionManager.closeAll();
|
||||
} catch (Exception e) {
|
||||
logger.error(request.getRequestURI(), e);
|
||||
response = new ServiceRestResponse(EsitoType.KO, e, multiDBTransactionManager.getPrimaryDatasource().getProfile(), entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (entity.getOperation() == OperationType.SELECT_OBJECT) {
|
||||
return obj;
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
if (entity.getOperation() == OperationType.SELECT_OBJECT) {
|
||||
return obj;
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,10 +30,8 @@ public class Migration_20230411163906 extends BaseMigration implements Migration
|
||||
"WHERE mime_type IS NULL");
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String profileDb = advancedDataSource.getProfileName();
|
||||
MultiDBTransactionManager mdb = new MultiDBTransactionManager(profileDb, false);
|
||||
|
||||
String profileDb = advancedDataSource.getProfileName();
|
||||
try (MultiDBTransactionManager mdb = new MultiDBTransactionManager(profileDb, false)) {
|
||||
for (HashMap<String, Object> stbFileAttached : stbFilesAttached) {
|
||||
final MediaType mimeType = mimeTypesHandler.getContentType((String) stbFileAttached.get("file_name"));
|
||||
|
||||
@@ -41,7 +39,6 @@ public class Migration_20230411163906 extends BaseMigration implements Migration
|
||||
Query.format("UPDATE stb_files_attached SET mime_type = {} WHERE id_attach = {}", mimeType.toString(), stbFileAttached.get("id_attach")));
|
||||
}
|
||||
mdb.commitAll();
|
||||
mdb.closeAll();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20250121123229 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
if (isHistoryDB())
|
||||
return;
|
||||
|
||||
enableSaveToRest("vdist_inca");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down() throws Exception {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20250121181622 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
if (isHistoryDB())
|
||||
return;
|
||||
|
||||
createSetup("w_vdist_inca_rc", "REGISTRAZIONE_CONTABILE", "EMAIL_FOR_LOG", null,
|
||||
null, false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -794,6 +794,7 @@ public class AccountingRules extends QueryRules {
|
||||
String flagRegAcc = "S", metodoFatturazione = "";
|
||||
|
||||
String sql = "SELECT flag_tipo_fatturazione FROM azienda ";
|
||||
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
|
||||
@@ -1068,15 +1068,10 @@ public class CommonRules extends QueryRules {
|
||||
String sql =
|
||||
"SELECT max(num_dist) " +
|
||||
"FROM vtb_dist_incat " +
|
||||
"WHERE DatePart(Year, data_dist) = DatePart(Year, " + UtilityDB.valueToString(vtbDistIncat.getDataDist()) + ")";
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
Integer numDist = 0;
|
||||
if (rs.next()) {
|
||||
numDist = rs.getInt(1);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
"WHERE DatePart(Year, data_dist) = DatePart(Year, " + UtilityDB.valueToString(vtbDistIncat.getDataDist()) + ") AND data_chk is null";
|
||||
|
||||
Integer numDist = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(conn, sql);
|
||||
if (numDist == null) numDist = 0;
|
||||
numDist++;
|
||||
return numDist;
|
||||
}
|
||||
|
||||
@@ -5,106 +5,230 @@ import it.integry.ems.settings.Model.AvailableConnectionsModel;
|
||||
import it.integry.ems.settings.Model.SettingsModel;
|
||||
import it.integry.ems.sync.MultiDBTransaction.exception.UnexpectedConnectionSwitchException;
|
||||
import it.integry.ems.utility.UtilityDebug;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Component
|
||||
public class BasicConnectionPool {
|
||||
private static final Logger logger = LogManager.getLogger(BasicConnectionPool.class);
|
||||
|
||||
@Autowired
|
||||
private SettingsModel settingsModel;
|
||||
|
||||
private final Map<String, List<DataSource>> connectionPool = Collections.synchronizedMap(new HashMap<>());
|
||||
private final Map<String, List<DataSource>> usedConnections = Collections.synchronizedMap(new HashMap<>());
|
||||
private final Map<String, List<DataSource>> connectionPool = new ConcurrentHashMap<>();
|
||||
private final Map<String, List<DataSource>> usedConnections = new ConcurrentHashMap<>();
|
||||
private final Map<String, AtomicInteger> extraConnectionCounters = new ConcurrentHashMap<>();
|
||||
private final Map<String, Set<String>> activeConnectionNames = new ConcurrentHashMap<>();
|
||||
|
||||
public synchronized void init() throws Exception {
|
||||
private final ReentrantLock poolLock = new ReentrantLock();
|
||||
|
||||
int poolSize =
|
||||
UtilityDebug.isIntegryServerMaster() ? 1 :
|
||||
UtilityDebug.isIntegryServerDev() ? 2 :
|
||||
UtilityDebug.isDebugExecution() ? 0 :
|
||||
Runtime.getRuntime().availableProcessors();
|
||||
public void init() throws Exception {
|
||||
poolLock.lock();
|
||||
try {
|
||||
closeAllConnections();
|
||||
int poolSize = calculatePoolSize();
|
||||
initializeConnections(poolSize);
|
||||
} finally {
|
||||
poolLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (!settingsModel.isPrimaryInstance()) poolSize = 0;
|
||||
private void closeAllConnections() {
|
||||
poolLock.lock();
|
||||
try {
|
||||
// Chiude tutte le connessioni esistenti
|
||||
connectionPool.values().forEach(connections ->
|
||||
connections.forEach(this::safeCloseConnection));
|
||||
usedConnections.values().forEach(connections ->
|
||||
connections.forEach(this::safeCloseConnection));
|
||||
|
||||
poolSize = Math.min(poolSize, 8);
|
||||
// Pulisce le mappe
|
||||
connectionPool.clear();
|
||||
usedConnections.clear();
|
||||
extraConnectionCounters.clear();
|
||||
activeConnectionNames.clear();
|
||||
} finally {
|
||||
poolLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void safeCloseConnection(DataSource ds) {
|
||||
try {
|
||||
if (ds != null) {
|
||||
ds.forceClose();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Errore durante la chiusura della connessione", e);
|
||||
}
|
||||
}
|
||||
|
||||
private int calculatePoolSize() {
|
||||
int poolSize = UtilityDebug.isIntegryServerMaster() ? 1 :
|
||||
UtilityDebug.isIntegryServerDev() ? 2 :
|
||||
UtilityDebug.isDebugExecution() ? 0 :
|
||||
Runtime.getRuntime().availableProcessors();
|
||||
|
||||
if (!settingsModel.isPrimaryInstance()) {
|
||||
poolSize = 0;
|
||||
}
|
||||
|
||||
return Math.min(poolSize, 8);
|
||||
}
|
||||
|
||||
private void initializeConnections(int poolSize) throws Exception {
|
||||
final List<AvailableConnectionsModel> availableConnections = settingsModel.getAvailableConnections(true);
|
||||
|
||||
for (final AvailableConnectionsModel availableConnectionsModel : availableConnections) {
|
||||
synchronized (connectionPool) {
|
||||
if (!connectionPool.containsKey(availableConnectionsModel.getProfileName()))
|
||||
connectionPool.put(availableConnectionsModel.getProfileName(), Collections.synchronizedList(new ArrayList<>()));
|
||||
for (final AvailableConnectionsModel model : availableConnections) {
|
||||
String dbName = model.getDbName();
|
||||
connectionPool.putIfAbsent(dbName, Collections.synchronizedList(new ArrayList<>()));
|
||||
usedConnections.putIfAbsent(dbName, Collections.synchronizedList(new ArrayList<>()));
|
||||
activeConnectionNames.putIfAbsent(dbName, ConcurrentHashMap.newKeySet());
|
||||
extraConnectionCounters.putIfAbsent(dbName, new AtomicInteger(0));
|
||||
|
||||
if (!usedConnections.containsKey(availableConnectionsModel.getProfileName()))
|
||||
usedConnections.put(availableConnectionsModel.getProfileName(), Collections.synchronizedList(new ArrayList<>()));
|
||||
for (int j = 0; j < poolSize; j++) {
|
||||
DataSource ds = createNewDataSource(model.getProfileName(), j);
|
||||
String connectionName = ds.getApplicationName();
|
||||
|
||||
int createdConnections = connectionPool.get(availableConnectionsModel.getProfileName()).size() +
|
||||
usedConnections.get(availableConnectionsModel.getProfileName()).size();
|
||||
|
||||
for (int j = 0; j < poolSize - createdConnections; j++) {
|
||||
DataSource ds = new DataSource();
|
||||
ds.initialize(availableConnectionsModel.getProfileName(), "EMS Pool Connection #" + j, true);
|
||||
connectionPool.get(availableConnectionsModel.getProfileName()).add(ds);
|
||||
if (!activeConnectionNames.get(dbName).contains(connectionName)) {
|
||||
connectionPool.get(dbName).add(ds);
|
||||
activeConnectionNames.get(dbName).add(connectionName);
|
||||
logger.trace("Created pool connection: {} for database: {}", connectionName, dbName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public synchronized DataSource getConnection(String profileName) throws Exception {
|
||||
DataSource ds = null;
|
||||
|
||||
synchronized (connectionPool) {
|
||||
if (!connectionPool.containsKey(profileName) || connectionPool.get(profileName).isEmpty()) {
|
||||
ds = createExtraConnection(profileName);
|
||||
} else {
|
||||
ds = connectionPool.get(profileName)
|
||||
.remove(connectionPool.get(profileName).size() - 1);
|
||||
|
||||
if (ds.isClosed()) ds.initialize(profileName, ds.getApplicationName(), true);
|
||||
|
||||
usedConnections.get(profileName).add(ds);
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource createNewDataSource(String profileName, int index) throws Exception {
|
||||
DataSource ds = new DataSource();
|
||||
ds.initialize(profileName, "EMS Pool Connection #" + index, true);
|
||||
return ds;
|
||||
}
|
||||
|
||||
public synchronized boolean releaseConnection(String profileName, DataSource ds) throws UnexpectedConnectionSwitchException, SQLException, IOException {
|
||||
String currentCatalog = ds.getConnection().getCatalog();
|
||||
final AvailableConnectionsModel availableConnectionsModel = settingsModel.getAvailableConnections().stream()
|
||||
.filter(x -> x.getProfileName().equalsIgnoreCase(profileName))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
public DataSource getConnection(String profileName) throws Exception {
|
||||
String dbName = settingsModel.getDbNameFromProfileDb(profileName);
|
||||
poolLock.lock();
|
||||
try {
|
||||
if (!connectionPool.containsKey(dbName) || connectionPool.get(dbName).isEmpty()) {
|
||||
return createExtraConnection(profileName);
|
||||
}
|
||||
|
||||
if (!currentCatalog.equalsIgnoreCase(availableConnectionsModel.getDbName())) {
|
||||
usedConnections.get(profileName).remove(ds);
|
||||
ds.forceClose();
|
||||
throw new UnexpectedConnectionSwitchException(availableConnectionsModel.getDbName(), currentCatalog);
|
||||
List<DataSource> pool = connectionPool.get(dbName);
|
||||
List<DataSource> used = usedConnections.get(dbName);
|
||||
|
||||
DataSource ds = pool.remove(0);
|
||||
|
||||
if (ds.isClosed()) {
|
||||
String oldName = ds.getApplicationName();
|
||||
activeConnectionNames.get(dbName).remove(oldName);
|
||||
|
||||
ds.initialize(profileName, oldName, true);
|
||||
activeConnectionNames.get(dbName).add(oldName);
|
||||
|
||||
logger.warn("Reinitializing closed connection: {} for database: {}", oldName, dbName);
|
||||
}
|
||||
|
||||
used.add(ds);
|
||||
logger.trace("Assigned connection: {} for database: {}", ds.getApplicationName(), dbName);
|
||||
return ds;
|
||||
} finally {
|
||||
poolLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean releaseConnection(String profileName, DataSource ds)
|
||||
throws UnexpectedConnectionSwitchException, SQLException, IOException {
|
||||
if (ds == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
poolLock.lock();
|
||||
try {
|
||||
String currentCatalog = ds.getConnection().getCatalog();
|
||||
AvailableConnectionsModel model = findConnectionModel(profileName);
|
||||
|
||||
if (ds.getApplicationName().equals("EMS Connection")) { //Extra connection
|
||||
ds.forceClose();
|
||||
return true;
|
||||
} else {
|
||||
synchronized (connectionPool) {
|
||||
connectionPool.get(profileName).add(ds);
|
||||
return usedConnections.get(profileName).remove(ds);
|
||||
if (model == null) {
|
||||
throw new IllegalArgumentException("Profile not found: " + profileName);
|
||||
}
|
||||
|
||||
if (!currentCatalog.equalsIgnoreCase(model.getDbName())) {
|
||||
handleCatalogMismatch(ds, model, currentCatalog);
|
||||
return false;
|
||||
}
|
||||
|
||||
return handleConnectionRelease(ds, model);
|
||||
} finally {
|
||||
poolLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource createExtraConnection(String profileName) throws Exception {
|
||||
DataSource ds = new DataSource();
|
||||
ds.initialize(profileName, "EMS Connection", true);
|
||||
String dbName = settingsModel.getDbNameFromProfileDb(profileName);
|
||||
poolLock.lock();
|
||||
try {
|
||||
// Calcola il numero totale di connessioni esistenti
|
||||
int baseCount = connectionPool.get(dbName).size() + usedConnections.get(dbName).size();
|
||||
int extraCount = extraConnectionCounters.get(dbName).incrementAndGet();
|
||||
|
||||
return ds;
|
||||
String connectionName = "EMS Extra Connection #" + (baseCount + extraCount);
|
||||
|
||||
// Verifica se la connessione esiste già
|
||||
if (activeConnectionNames.get(dbName).contains(connectionName)) {
|
||||
logger.warn("Extra connection name collision detected: {}", connectionName);
|
||||
extraConnectionCounters.get(dbName).decrementAndGet();
|
||||
return createExtraConnection(profileName); // Riprova con un nuovo nome
|
||||
}
|
||||
|
||||
DataSource ds = new DataSource();
|
||||
ds.initialize(profileName, connectionName, true);
|
||||
activeConnectionNames.get(dbName).add(connectionName);
|
||||
|
||||
logger.trace("Created extra connection: {} for database: {}", connectionName, dbName);
|
||||
return ds;
|
||||
} finally {
|
||||
poolLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private AvailableConnectionsModel findConnectionModel(String profileName) {
|
||||
return settingsModel.getAvailableConnections().stream()
|
||||
.filter(x -> x.getProfileName().equalsIgnoreCase(profileName))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private void handleCatalogMismatch(DataSource ds, AvailableConnectionsModel model, String currentCatalog)
|
||||
throws UnexpectedConnectionSwitchException, SQLException, IOException {
|
||||
String connectionName = ds.getApplicationName();
|
||||
usedConnections.get(model.getDbName()).remove(ds);
|
||||
activeConnectionNames.get(model.getDbName()).remove(connectionName);
|
||||
ds.forceClose();
|
||||
throw new UnexpectedConnectionSwitchException(model.getDbName(), currentCatalog);
|
||||
}
|
||||
|
||||
private boolean handleConnectionRelease(DataSource ds, AvailableConnectionsModel model)
|
||||
throws SQLException {
|
||||
String connectionName = ds.getApplicationName();
|
||||
String dbName = model.getDbName();
|
||||
|
||||
if (connectionName.startsWith("EMS Extra Connection #")) {
|
||||
activeConnectionNames.get(dbName).remove(connectionName);
|
||||
ds.forceClose();
|
||||
logger.trace("Closed extra connection: {} for database: {}", connectionName, dbName);
|
||||
return true;
|
||||
}
|
||||
|
||||
connectionPool.get(dbName).add(ds);
|
||||
boolean removed = usedConnections.get(dbName).remove(ds);
|
||||
logger.trace("Released connection: {} for database: {}", connectionName, dbName);
|
||||
return removed;
|
||||
}
|
||||
}
|
||||
@@ -233,19 +233,17 @@ public class MultiDBTransactionManager implements AutoCloseable {
|
||||
}
|
||||
|
||||
public void closeAll() throws Exception {
|
||||
if (dbDatasources != null && !dbDatasources.isEmpty())
|
||||
if (dbDatasources != null && !dbDatasources.isEmpty()) {
|
||||
for (AdvancedDataSource advancedDataSource : dbDatasources) {
|
||||
if (!advancedDataSource.isClosed()) {
|
||||
if (requestData != null && requestData.getRequestURI() != null) {
|
||||
String methodName = " [" + requestData.getRequestURI() + "]";
|
||||
if (shouldPrintLog(requestData.getRequestURI()))
|
||||
logger.debug("Closing automatically: " + advancedDataSource.getDataSource().getProfile() + " (#" + advancedDataSource.getDataSource().getSessionID() + ")" + methodName);
|
||||
} else if (enableLog) {
|
||||
logger.debug("Closing manually: " + advancedDataSource.getDataSource().getProfile() + " (#" + advancedDataSource.getDataSource().getSessionID() + ")");
|
||||
}
|
||||
advancedDataSource.commit();
|
||||
|
||||
if (requestData != null && requestData.getRequestURI() != null) {
|
||||
String methodName = " [" + requestData.getRequestURI() + "]";
|
||||
if (shouldPrintLog(requestData.getRequestURI()))
|
||||
logger.debug("Closing automatically: " + advancedDataSource.getDataSource().getProfile() + " (#" + advancedDataSource.getDataSource().getSessionID() + ")" + methodName);
|
||||
} else if (enableLog) {
|
||||
logger.debug("Closing manually: " + advancedDataSource.getDataSource().getProfile() + " (#" + advancedDataSource.getDataSource().getSessionID() + ")");
|
||||
}
|
||||
advancedDataSource.commit();
|
||||
|
||||
try {
|
||||
connectionPool.releaseConnection(advancedDataSource.getProfileName(), advancedDataSource.getDataSource());
|
||||
@@ -253,6 +251,9 @@ public class MultiDBTransactionManager implements AutoCloseable {
|
||||
logger.error("UnexpectedConnectionSwitchException: " + advancedDataSource.getProfileName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
dbDatasources.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -40,6 +40,18 @@ then
|
||||
}
|
||||
end
|
||||
|
||||
rule "completeCompilatoDaVtbDistIncar"
|
||||
no-loop
|
||||
when
|
||||
eval(completeRulesEnabled)
|
||||
$vtbDistIncar : VtbDistIncar(compilatoDa == null && operation != OperationType.DELETE && operation != OperationType.UPDATE )
|
||||
then
|
||||
String fullName = CommonRules.getFullName(conn, username);
|
||||
modify($vtbDistIncar){
|
||||
setCompilatoDa(fullName)
|
||||
}
|
||||
end
|
||||
|
||||
rule "completeFlagTipoFatturazione"
|
||||
no-loop
|
||||
when
|
||||
|
||||
@@ -75,23 +75,6 @@ public class ContabilController {
|
||||
return serviceChecker.getServiceStatus(methods, ContabilController.class);
|
||||
}
|
||||
|
||||
@RequestMapping(value = EmsRestConstants.PATH_REG_DISTINTA_INCASSI, method = RequestMethod.POST)
|
||||
public @ResponseBody
|
||||
List<ServiceRestResponse> registraDistintaIncassi(HttpServletRequest request,
|
||||
@RequestParam(CommonConstants.PROFILE_DB) String configuration,
|
||||
@RequestBody(required = true) List<DistinteIncassiDTO> datiDistinta) {
|
||||
|
||||
List<ServiceRestResponse> resp = new ArrayList<ServiceRestResponse>();
|
||||
|
||||
try {
|
||||
resp = UtilityEntity.toServiceRestResponse(contabilService.registraDistintaIncassi(datiDistinta));
|
||||
} catch (Exception e) {
|
||||
logger.error(request.getRequestURI(), e);
|
||||
resp.add(new ServiceRestResponse(EsitoType.KO, configuration, e));
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
@RequestMapping(value = EmsRestConstants.PATH_SEPA_XML, method = RequestMethod.POST)
|
||||
public @ResponseBody
|
||||
ServiceRestResponse getSepaXml(HttpServletRequest request,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package it.integry.ems.contabil.controller;
|
||||
|
||||
import it.integry.common.var.CommonConstants;
|
||||
import it.integry.ems.contabil.dto.DistinteIncassiDTO;
|
||||
import it.integry.ems.contabil.service.DistinteIncassiService;
|
||||
import it.integry.ems.response.ServiceRestResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@Scope("request")
|
||||
@RequestMapping("distinte-incassi")
|
||||
public class DistinteIncassiController {
|
||||
|
||||
@Autowired
|
||||
DistinteIncassiService distinteIncassiService;
|
||||
|
||||
@RequestMapping(value = "registra", method = RequestMethod.POST)
|
||||
public @ResponseBody
|
||||
ServiceRestResponse registra(HttpServletRequest request,
|
||||
@RequestParam(CommonConstants.PROFILE_DB) String configuration,
|
||||
@RequestBody(required = false) List<DistinteIncassiDTO> datiDistinta) throws Exception{
|
||||
|
||||
if (datiDistinta == null || datiDistinta.size() == 0) {
|
||||
return ServiceRestResponse.createEntityPositiveResponse(distinteIncassiService.registraDistintaIncassi());
|
||||
} else{
|
||||
return ServiceRestResponse.createEntityPositiveResponse(distinteIncassiService.registraDistintaIncassi(datiDistinta));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,9 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import java.util.Date;
|
||||
|
||||
public class DistinteIncassiDTO {
|
||||
@JsonProperty
|
||||
@JsonProperty("dataDist")
|
||||
private Date dataDist;
|
||||
@JsonProperty
|
||||
@JsonProperty("numDist")
|
||||
private Integer numDist;
|
||||
|
||||
public Date getDataDist() {
|
||||
|
||||
@@ -70,400 +70,6 @@ public class ContabilService {
|
||||
|
||||
}
|
||||
|
||||
public List<EntityBase> registraDistintaIncassi(List<DistinteIncassiDTO> distinte) throws Exception {
|
||||
List<EntityBase> entities = new ArrayList<EntityBase>();
|
||||
for (DistinteIncassiDTO distinta : distinte) {
|
||||
chkDistintaIncassi(distinta.getDataDist(), distinta.getNumDist());
|
||||
CtbMovt ctbMovt = registraDistintaIncassi(distinta.getDataDist(), distinta.getNumDist());
|
||||
entityProcessor.processEntity(ctbMovt, false, multiDBTransactionManager);
|
||||
entities.add(ctbMovt);
|
||||
if (ctbMovt.getException() != null) {
|
||||
throw new Exception(ctbMovt.getException().getMessage(), ctbMovt.getException());
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
private void chkDistintaIncassi(Date dataDist, Integer numDist) throws Exception {
|
||||
|
||||
String sql =
|
||||
"SELECT * "
|
||||
+ "FROM vtb_dist_incar LEFT OUTER JOIN dtb_doct ON vtb_dist_incar.cod_anag = dtb_doct.cod_anag AND "
|
||||
+ "vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip AND "
|
||||
+ "vtb_dist_incar.data_doc = dtb_doct.data_doc AND "
|
||||
+ "vtb_dist_incar.ser_doc = dtb_doct.ser_doc AND "
|
||||
+ "vtb_dist_incar.num_doc = dtb_doct.num_doc "
|
||||
+ "WHERE data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " AND "
|
||||
+ "num_dist = " + UtilityDB.valueToString(numDist) + " AND "
|
||||
+ "flag_reg_acc = 'N' AND "
|
||||
+ "cod_dtip_val is null";
|
||||
|
||||
PreparedStatement ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
new Exception("Impossibile registrare in contabilità la distinta n° " + numDist + " del " + new SimpleDateFormat("dd/MM/yyyy").format(dataDist) + "." +
|
||||
"Ci sono dei documenti di trasporto non ancora fatturati.");
|
||||
}
|
||||
}
|
||||
|
||||
private CtbMovt registraDistintaIncassi(Date dataDist, Integer numDist) throws Exception {
|
||||
Map<String, String> setup = setupGest.getSetupSection(multiDBTransactionManager.getPrimaryConnection(), "w_vdist_inca_rc", "REGISTRAZIONE_CONTABILE");
|
||||
|
||||
String codCcauAcc = setup.get("COD_CCAU_ACCONTO");
|
||||
if (UtilityString.isNullOrEmpty(codCcauAcc)) throw new Exception("Causale acconti non definita");
|
||||
|
||||
String codCcauInc = setup.get("COD_CCAU_INCA");
|
||||
if (UtilityString.isNullOrEmpty(codCcauInc)) throw new Exception("Causale incassi non definita");
|
||||
|
||||
String codCconAcc = setup.get("COD_CCON_ACCONTO");
|
||||
if (UtilityString.isNullOrEmpty(codCconAcc)) throw new Exception("Conto acconti non definito");
|
||||
|
||||
String codCconReso = setup.get("COD_CCON_RESO");
|
||||
if (UtilityString.isNullOrEmpty(codCconReso)) throw new Exception("Conto resi non definita");
|
||||
|
||||
String codCconCassa = setup.get("COD_CCON_CASSA");
|
||||
if (UtilityString.isNullOrEmpty(codCconCassa)) throw new Exception("Conto cassa non definito");
|
||||
|
||||
String codCconClie = setup.get("COD_CCON_CREDITO");
|
||||
if (UtilityString.isNullOrEmpty(codCconClie)) throw new Exception("Conto crediti non definito");
|
||||
|
||||
String escludiClienti = setup.get("ESCLUDI_CLIENTI");
|
||||
|
||||
String condEscludiClienti = null;
|
||||
if (!UtilityString.isNullOrEmpty(escludiClienti)) {
|
||||
escludiClienti = escludiClienti.replace(",", "','");
|
||||
condEscludiClienti = "vtb_dist_incar.cod_anag NOT IN ('" + escludiClienti + "')";
|
||||
}
|
||||
|
||||
String sql =
|
||||
|
||||
"SELECT Convert(integer, 1) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END as descr_paga , " +
|
||||
" Sum(vtb_dist_incar.imp_incasso) as imp_incasso, " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon as 'cod_ccon_paga', " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga, " +
|
||||
" dtb_doct, " +
|
||||
" dtb_tipi " +
|
||||
" WHERE vtb_dist_incar.cod_anag = dtb_doct.cod_anag and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip and " +
|
||||
" vtb_dist_incar.data_doc = dtb_doct.data_doc and " +
|
||||
" vtb_dist_incar.ser_doc = dtb_doct.ser_doc and " +
|
||||
" vtb_dist_incar.num_doc = dtb_doct.num_doc and " +
|
||||
" vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is not null and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip_val and " +
|
||||
" dtb_doct.cod_dtip = dtb_tipi.cod_dtip " +
|
||||
" GROUP BY vtb_dist_incat.cod_vage, " +
|
||||
" vtb_dist_incat.cod_vvet, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" UNION ALL " +
|
||||
" SELECT Convert(integer, 2) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END as descr_paga, " +
|
||||
" vtb_dist_incar.imp_incasso, " +
|
||||
" null as cod_dtip_val, " +
|
||||
" null as data_doc_val, " +
|
||||
" null as ser_doc_val, " +
|
||||
" null as num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE WHEN vtb_dist_incar.imp_incasso < 0 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga " +
|
||||
" WHERE vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is null " +
|
||||
" UNION ALL " +
|
||||
" SELECT Convert(integer, 3) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" Sum(vtb_dist_incar.imp_incasso), " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga, " +
|
||||
" dtb_doct, dtb_tipi " +
|
||||
" WHERE vtb_dist_incar.cod_anag = dtb_doct.cod_anag and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip and " +
|
||||
" vtb_dist_incar.data_doc = dtb_doct.data_doc and " +
|
||||
" vtb_dist_incar.ser_doc = dtb_doct.ser_doc and " +
|
||||
" vtb_dist_incar.num_doc = dtb_doct.num_doc and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_tipi.cod_dtip and " +
|
||||
" vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is not null and " +
|
||||
" ( vtb_dist_incar.cod_dtip <> dtb_doct.cod_dtip_val OR dtb_doct.cod_dtip_val is null ) " +
|
||||
" GROUP BY vtb_dist_incat.cod_vage, " +
|
||||
" vtb_dist_incat.cod_vvet, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas ";
|
||||
sql = UtilityDB.addwhereCond(sql, condEscludiClienti, false);
|
||||
|
||||
sql =
|
||||
"WITH distinta as (" + sql + ") " +
|
||||
"SELECT *, " +
|
||||
"DENSE_RANK() OVER (ORDER BY cod_anag, data_doc_val, cod_dtip_val, ser_doc_val, num_doc_val) as gruppo_doc " +
|
||||
" FROM distinta " +
|
||||
"ORDER BY cod_anag, data_doc_val, cod_dtip_val, ser_doc_val, num_doc_val ";
|
||||
|
||||
List<HashMap<String, Object>> incassi = UtilityDB.executeSimpleQuery(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
/*
|
||||
* GRUPPO 1 = FATTURE
|
||||
* GRUPPO 2 = RIGHE SENZA AGGANCIO A DOCUMENTO
|
||||
* GRUPPO 3 = BOLLE (FATTURATE E NON )
|
||||
*/
|
||||
CtbMovt ctbMovt = new CtbMovt().setDataCmov(dataDist);
|
||||
ctbMovt.setOperation(OperationType.INSERT);
|
||||
VtbDistIncat vtbDistIncat =
|
||||
new VtbDistIncat().setDataDist(dataDist).setNumDist(numDist);
|
||||
vtbDistIncat.setOperation(OperationType.UPDATE);
|
||||
ctbMovt.setVtbDistIncat(vtbDistIncat);
|
||||
|
||||
long gruppoDocOld = 0;
|
||||
String codCcau = null;
|
||||
Boolean existPartita = false;
|
||||
TipoPartita tipoPartita = TipoPartita.ATTIVA;
|
||||
|
||||
List<CtbScad> elencoScadPartita = null;
|
||||
List<CtbScad> ctbScadList = new ArrayList<>(); //Elenco delle scadenze modificate ma non chiuse
|
||||
for (HashMap riga : incassi) {
|
||||
String codAnag = UtilityString.streNull(UtilityHashMap.getValueIfExists(riga, "cod_anag"));
|
||||
String descrPaga = UtilityHashMap.getValueIfExists(riga, "descr_paga");
|
||||
BigDecimal impIncasso = UtilityHashMap.getValueIfExists(riga, "imp_incasso");
|
||||
Date dataDocVal = UtilityHashMap.getValueIfExists(riga, "data_doc_val");
|
||||
String serDocVal = UtilityString.streNull(UtilityHashMap.getValueIfExists(riga, "ser_doc_val"));
|
||||
Integer numDocVal = UtilityHashMap.getValueIfExists(riga, "num_doc_val");
|
||||
String codCconReg = UtilityHashMap.getValueIfExists(riga, "contoReg");
|
||||
String codCconPaga = UtilityHashMap.getValueIfExists(riga, "cod_ccon_paga");
|
||||
String codJfas = UtilityHashMap.getValueIfExists(riga, "cod_jfas");
|
||||
long gruppoDoc = UtilityHashMap.getValueIfExists(riga, "gruppo_doc");
|
||||
Integer annoPart;
|
||||
if (dataDocVal != null) {
|
||||
annoPart = Integer.parseInt(new SimpleDateFormat("yyyy").format(dataDocVal));
|
||||
} else {
|
||||
annoPart = null;
|
||||
}
|
||||
if (UtilityString.isNullOrEmpty(codCconPaga)) {
|
||||
codCconPaga = codCconCassa;
|
||||
}
|
||||
|
||||
if (gruppoDoc != gruppoDocOld) {
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() == null).toList();
|
||||
ctbScadList.addAll(scad);
|
||||
}
|
||||
|
||||
HashMap<String, Object> datiPartita = chkPartita(codAnag, annoPart, serDocVal, numDocVal);
|
||||
if (datiPartita != null && !datiPartita.isEmpty()) {
|
||||
tipoPartita = (TipoPartita) datiPartita.get("tipoPartita");
|
||||
elencoScadPartita = (List<CtbScad>) datiPartita.get("elencoScadenze");
|
||||
} else {
|
||||
tipoPartita = TipoPartita.ATTIVA;
|
||||
elencoScadPartita = null;
|
||||
}
|
||||
existPartita = (elencoScadPartita != null && !elencoScadPartita.isEmpty());
|
||||
}
|
||||
|
||||
if (tipoPartita == TipoPartita.ATTIVA && impIncasso.compareTo(BigDecimal.ZERO) < 0) {
|
||||
tipoPartita = TipoPartita.PASSIVA;
|
||||
} else if (tipoPartita == TipoPartita.PASSIVA && impIncasso.compareTo(BigDecimal.ZERO) > 0) {
|
||||
tipoPartita = TipoPartita.ATTIVA;
|
||||
}
|
||||
|
||||
BigDecimal impDare = BigDecimal.ZERO, impAvere = BigDecimal.ZERO;
|
||||
if (tipoPartita == TipoPartita.PASSIVA) {
|
||||
impDare = impIncasso.abs();
|
||||
} else {
|
||||
impAvere = impIncasso.abs();
|
||||
}
|
||||
|
||||
if (existPartita) {
|
||||
codCcau = codCcauInc;
|
||||
codCconReg = codCconClie;
|
||||
Integer nextIdRiga = getNextIdRiga(ctbMovt);
|
||||
impIncasso = AccountingBusinessLogic.pagaScadenze(dataDist, impIncasso.abs(), elencoScadPartita, nextIdRiga);
|
||||
if (impIncasso.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (impDare.compareTo(BigDecimal.ZERO) > 0) {
|
||||
impDare = impDare.subtract(impIncasso);
|
||||
} else {
|
||||
impAvere = impAvere.subtract(impIncasso);
|
||||
}
|
||||
} else {
|
||||
impIncasso = BigDecimal.ZERO;
|
||||
}
|
||||
if (impAvere.compareTo(BigDecimal.ZERO) > 0 || impDare.compareTo(BigDecimal.ZERO) > 0) {
|
||||
String descrAggRiga = "PART. " + String.valueOf(annoPart).substring(2) + '-' + serDocVal + numDocVal;
|
||||
if (!UtilityString.isNullOrEmpty(descrPaga)) {
|
||||
descrAggRiga += " " + descrPaga;
|
||||
}
|
||||
if (descrAggRiga.length() > 255) {
|
||||
descrAggRiga = descrAggRiga.substring(0, 254);
|
||||
}
|
||||
|
||||
CtbMovr ctbMovr =
|
||||
new CtbMovr()
|
||||
.setCodCcon(codCconReg)
|
||||
.setTipoAnag("C")
|
||||
.setCodAnag(codAnag)
|
||||
.setAnnoPart(annoPart)
|
||||
.setSerDoc(serDocVal)
|
||||
.setNumDoc(numDocVal)
|
||||
.setImpDare(impDare)
|
||||
.setImpAvere(impAvere)
|
||||
.setCodJfas(codJfas)
|
||||
.setDesAgg(descrAggRiga)
|
||||
.setIdRiga(nextIdRiga);
|
||||
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() != null && x.getIdRigaMov().equals(nextIdRiga)).toList();
|
||||
ctbMovr.setCtbScad(scad);
|
||||
}
|
||||
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
ctbMovr = new CtbMovr()
|
||||
.setCodCcon(codCconPaga)
|
||||
.setImpDare(impAvere)
|
||||
.setImpAvere(impDare)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
}
|
||||
}
|
||||
|
||||
/*if (impIncasso.compareTo(BigDecimal.ZERO) > 0) {*/
|
||||
if (impIncasso.compareTo(BigDecimal.ZERO) != 0) {
|
||||
if (UtilityString.isNullOrEmpty(codCcau)) {
|
||||
codCcau = codCcauAcc;
|
||||
}
|
||||
|
||||
if (tipoPartita == TipoPartita.PASSIVA) {
|
||||
impDare = impIncasso.abs();
|
||||
} else {
|
||||
impAvere = impIncasso.abs();
|
||||
}
|
||||
//Registra acconto
|
||||
CtbMovr ctbMovr =
|
||||
new CtbMovr()
|
||||
.setCodCcon(codCconReg)
|
||||
.setTipoAnag("C")
|
||||
.setCodAnag(codAnag)
|
||||
.setImpDare(impDare)
|
||||
.setImpAvere(impAvere)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
|
||||
//Registra acconto
|
||||
ctbMovr = new CtbMovr()
|
||||
.setCodCcon(codCconPaga)
|
||||
.setImpDare(impAvere)
|
||||
.setImpAvere(impDare)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
|
||||
}
|
||||
gruppoDocOld = gruppoDoc;
|
||||
}
|
||||
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() == null).toList();
|
||||
ctbScadList.addAll(scad);
|
||||
}
|
||||
|
||||
if (ctbMovt != null) {
|
||||
ctbMovt.setCodCcau(codCcau);
|
||||
BigDecimal totMov = BigDecimal.ZERO;
|
||||
sql = "SELECT descrizione FROM ctb_caus WHERE cod_ccau = " + UtilityDB.valueToString(codCcau);
|
||||
String descrCaus = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(descrCaus)) {
|
||||
descrCaus += " DIST.N.:" + numDist;
|
||||
sql =
|
||||
"SELECT IsNull(IsNull(cod_vage, cod_vvet), IsNull(cod_mdep,'')) as descrizione, sum(imp_incasso) as imp_incasso "
|
||||
+ "FROM vtb_dist_incat, vtb_dist_incar "
|
||||
+ "WHERE vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " AND "
|
||||
+ "vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " AND "
|
||||
+ "vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND "
|
||||
+ "vtb_dist_incat.num_dist = vtb_dist_incar.num_dist "
|
||||
+ " GROUP BY IsNull(IsNull(cod_vage, cod_vvet), IsNull(cod_mdep,''))";
|
||||
|
||||
HashMap<String, Object> datiDist = UtilityDB.executeSimpleQueryOnlyFirstRow(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
if (datiDist != null && !datiDist.isEmpty()) {
|
||||
descrCaus += " " + datiDist.get("descrizione");
|
||||
totMov = (BigDecimal) datiDist.get("imp_incasso");
|
||||
}
|
||||
|
||||
}
|
||||
if (descrCaus.length() > 40) {
|
||||
descrCaus = descrCaus.substring(0, 39);
|
||||
}
|
||||
ctbMovt.setDescrizioneCaus(descrCaus);
|
||||
ctbMovt.setImporto(totMov);
|
||||
//Aggancio scadenze non chiuse
|
||||
ctbMovt.setCtbScad(ctbScadList);
|
||||
}
|
||||
|
||||
return ctbMovt;
|
||||
}
|
||||
|
||||
private Integer getNextIdRiga(CtbMovt ctbMovt) {
|
||||
Integer idRigaMov = ctbMovt.getCtbMovr().size();
|
||||
if (idRigaMov == null) idRigaMov = 0;
|
||||
@@ -471,26 +77,7 @@ public class ContabilService {
|
||||
return idRigaMov;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> chkPartita(String codAnag, Integer annoPart, String serDoc, Integer numDoc) throws Exception {
|
||||
String sql =
|
||||
"SELECT Cast(tipo_partita as integer) as tipo_partita, tipo_anag "
|
||||
+ "FROM ctb_part "
|
||||
+ "WHERE ( ctb_part.cod_anag = " + UtilityDB.valueToString(codAnag) + " ) AND "
|
||||
+ " ( ctb_part.anno_part = " + UtilityDB.valueToString(annoPart) + " ) AND "
|
||||
+ " ( ctb_part.ser_doc = " + UtilityDB.valueToString(serDoc) + " ) AND "
|
||||
+ " ( ctb_part.num_doc = " + UtilityDB.valueToString(numDoc) + " ) AND "
|
||||
+ " ( ctb_part.dare_part <> ctb_part.avere_part ) ";
|
||||
HashMap<String, Object> dati = UtilityDB.executeSimpleQueryOnlyFirstRow(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
if (dati != null && !dati.isEmpty()) {
|
||||
TipoPartita tipoPartita = TipoPartita.fromValue((Integer) dati.get("tipo_partita"));
|
||||
List<CtbScad> elencoScadenze = AccountingBusinessLogic.loadScadenze(multiDBTransactionManager.getPrimaryConnection(), (String) dati.get("tipo_anag"), codAnag, annoPart, serDoc, numDoc);
|
||||
HashMap<String, Object> datiRet = new HashMap<>();
|
||||
datiRet.put("tipoPartita", tipoPartita);
|
||||
datiRet.put("elencoScadenze", elencoScadenze);
|
||||
return datiRet;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public CheckVatRestResponse checkVat(String countryCode, String vatNumber, boolean enableLocationCheck) throws Exception {
|
||||
String sql =
|
||||
|
||||
@@ -0,0 +1,465 @@
|
||||
package it.integry.ems.contabil.service;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import it.integry.common.var.CommonConstants;
|
||||
import it.integry.ems.contabil.dto.DistinteIncassiDTO;
|
||||
import it.integry.ems.rules.businessLogic.AccountingBusinessLogic;
|
||||
import it.integry.ems.rules.businessLogic.enums.TipoPartita;
|
||||
import it.integry.ems.service.EntityProcessor;
|
||||
import it.integry.ems.sync.MultiDBTransaction.MultiDBTransactionManager;
|
||||
import it.integry.ems_model.base.EntityBase;
|
||||
import it.integry.ems_model.entity.CtbMovr;
|
||||
import it.integry.ems_model.entity.CtbMovt;
|
||||
import it.integry.ems_model.entity.CtbScad;
|
||||
import it.integry.ems_model.entity.VtbDistIncat;
|
||||
import it.integry.ems_model.service.SetupGest;
|
||||
import it.integry.ems_model.types.OperationType;
|
||||
import it.integry.ems_model.utility.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Scope("request")
|
||||
public class DistinteIncassiService {
|
||||
@Autowired
|
||||
private MultiDBTransactionManager multiDBTransactionManager;
|
||||
|
||||
@Autowired
|
||||
private EntityProcessor entityProcessor;
|
||||
|
||||
@Autowired
|
||||
private SetupGest setupGest;
|
||||
|
||||
public List<EntityBase> registraDistintaIncassi() throws Exception {
|
||||
String sql = "SELECT data_dist as dataDist, num_dist as numDist FROM vtb_dist_incat WHERE num_cmov is null and data_chk is not null";
|
||||
List<DistinteIncassiDTO> distinteIncassiDTOS = UtilityDB.executeSimpleQueryDTO(multiDBTransactionManager.getPrimaryConnection(), sql, DistinteIncassiDTO.class);
|
||||
if (distinteIncassiDTOS == null || distinteIncassiDTOS.isEmpty()) return null;
|
||||
return registraDistintaIncassi(distinteIncassiDTOS);
|
||||
}
|
||||
|
||||
public List<EntityBase> registraDistintaIncassi(List<DistinteIncassiDTO> distinte) throws Exception {
|
||||
List<EntityBase> entities = new ArrayList<>();
|
||||
for (DistinteIncassiDTO distinta : distinte) {
|
||||
chkDistintaIncassi(distinta.getDataDist(), distinta.getNumDist());
|
||||
CtbMovt ctbMovt = registraDistintaIncassi(distinta.getDataDist(), distinta.getNumDist());
|
||||
entityProcessor.processEntity(ctbMovt, false, multiDBTransactionManager);
|
||||
entities.add(ctbMovt);
|
||||
if (ctbMovt.getException() != null) {
|
||||
throw new Exception(ctbMovt.getException().getMessage(), ctbMovt.getException());
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
private void chkDistintaIncassi(Date dataDist, Integer numDist) throws Exception {
|
||||
|
||||
String sql =
|
||||
"SELECT Cast(Count(*) as bit) "
|
||||
+ "FROM vtb_dist_incar LEFT OUTER JOIN dtb_doct ON vtb_dist_incar.cod_anag = dtb_doct.cod_anag AND "
|
||||
+ "vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip AND "
|
||||
+ "vtb_dist_incar.data_doc = dtb_doct.data_doc AND "
|
||||
+ "vtb_dist_incar.ser_doc = dtb_doct.ser_doc AND "
|
||||
+ "vtb_dist_incar.num_doc = dtb_doct.num_doc "
|
||||
+ "WHERE data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " AND "
|
||||
+ "num_dist = " + UtilityDB.valueToString(numDist) + " AND "
|
||||
+ "flag_reg_acc = 'N' AND "
|
||||
+ "cod_dtip_val is null";
|
||||
|
||||
boolean existBolleDaFatt = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
if (existBolleDaFatt) {
|
||||
new Exception("Impossibile registrare in contabilità la distinta n° " + numDist + " del " + new SimpleDateFormat("dd/MM/yyyy").format(dataDist) + "." +
|
||||
"Ci sono dei documenti di trasporto non ancora fatturati.");
|
||||
}
|
||||
}
|
||||
|
||||
private CtbMovt registraDistintaIncassi(Date dataDist, Integer numDist) throws Exception {
|
||||
Map<String, String> setup = setupGest.getSetupSection(multiDBTransactionManager.getPrimaryConnection(), "w_vdist_inca_rc", "REGISTRAZIONE_CONTABILE");
|
||||
|
||||
String codCcauAcc = setup.get("COD_CCAU_ACCONTO");
|
||||
if (UtilityString.isNullOrEmpty(codCcauAcc)) throw new Exception("Causale acconti non definita");
|
||||
|
||||
String codCcauInc = setup.get("COD_CCAU_INCA");
|
||||
if (UtilityString.isNullOrEmpty(codCcauInc)) throw new Exception("Causale incassi non definita");
|
||||
|
||||
String codCconAcc = setup.get("COD_CCON_ACCONTO");
|
||||
if (UtilityString.isNullOrEmpty(codCconAcc)) throw new Exception("Conto acconti non definito");
|
||||
|
||||
String codCconReso = setup.get("COD_CCON_RESO");
|
||||
if (UtilityString.isNullOrEmpty(codCconReso)) throw new Exception("Conto resi non definita");
|
||||
|
||||
String codCconCassa = setup.get("COD_CCON_CASSA");
|
||||
if (UtilityString.isNullOrEmpty(codCconCassa)) throw new Exception("Conto cassa non definito");
|
||||
|
||||
String codCconClie = setup.get("COD_CCON_CREDITO");
|
||||
if (UtilityString.isNullOrEmpty(codCconClie)) throw new Exception("Conto crediti non definito");
|
||||
|
||||
String escludiClienti = setup.get("ESCLUDI_CLIENTI");
|
||||
|
||||
String condEscludiClienti = null;
|
||||
if (!UtilityString.isNullOrEmpty(escludiClienti)) {
|
||||
escludiClienti = escludiClienti.replace(",", "','");
|
||||
condEscludiClienti = "vtb_dist_incar.cod_anag NOT IN ('" + escludiClienti + "')";
|
||||
}
|
||||
|
||||
String sql =
|
||||
"SELECT Convert(integer, 1) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END as descr_paga , " +
|
||||
" Sum(vtb_dist_incar.imp_incasso) as imp_incasso, " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon as 'cod_ccon_paga', " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga, " +
|
||||
" dtb_doct, " +
|
||||
" dtb_tipi " +
|
||||
" WHERE vtb_dist_incar.cod_anag = dtb_doct.cod_anag and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip and " +
|
||||
" vtb_dist_incar.data_doc = dtb_doct.data_doc and " +
|
||||
" vtb_dist_incar.ser_doc = dtb_doct.ser_doc and " +
|
||||
" vtb_dist_incar.num_doc = dtb_doct.num_doc and " +
|
||||
" vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is not null and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip_val and " +
|
||||
" dtb_doct.cod_dtip = dtb_tipi.cod_dtip " +
|
||||
" GROUP BY vtb_dist_incat.cod_vage, " +
|
||||
" vtb_dist_incat.cod_vvet, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" UNION ALL " +
|
||||
" SELECT Convert(integer, 2) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END as descr_paga, " +
|
||||
" vtb_dist_incar.imp_incasso, " +
|
||||
" null as cod_dtip_val, " +
|
||||
" null as data_doc_val, " +
|
||||
" null as ser_doc_val, " +
|
||||
" null as num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE WHEN vtb_dist_incar.imp_incasso < 0 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga " +
|
||||
" WHERE vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is null " +
|
||||
" UNION ALL " +
|
||||
" SELECT Convert(integer, 3) as gruppo, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" Sum(vtb_dist_incar.imp_incasso), " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END as contoReg, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas " +
|
||||
" FROM vtb_dist_incat INNER JOIN vtb_dist_incar ON vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND " +
|
||||
" vtb_dist_incat.num_dist = vtb_dist_incar.num_dist " +
|
||||
" LEFT OUTER JOIN mtb_depo ON vtb_dist_incat.cod_mdep = mtb_depo.cod_mdep " +
|
||||
" LEFT OUTER JOIN ntb_tipi_inca ON vtb_dist_incat.cod_mdep = ntb_tipi_inca.cod_mdep and " +
|
||||
" vtb_dist_incar.cod_paga = ntb_tipi_inca.tipo_inca " +
|
||||
" LEFT OUTER JOIN gtb_paga ON vtb_dist_incar.cod_paga = gtb_paga.cod_paga, " +
|
||||
" dtb_doct, dtb_tipi " +
|
||||
" WHERE vtb_dist_incar.cod_anag = dtb_doct.cod_anag and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_doct.cod_dtip and " +
|
||||
" vtb_dist_incar.data_doc = dtb_doct.data_doc and " +
|
||||
" vtb_dist_incar.ser_doc = dtb_doct.ser_doc and " +
|
||||
" vtb_dist_incar.num_doc = dtb_doct.num_doc and " +
|
||||
" vtb_dist_incar.cod_dtip = dtb_tipi.cod_dtip and " +
|
||||
" vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and " +
|
||||
" vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " and " +
|
||||
" vtb_dist_incar.cod_dtip is not null and " +
|
||||
" ( vtb_dist_incar.cod_dtip <> dtb_doct.cod_dtip_val OR dtb_doct.cod_dtip_val is null ) " +
|
||||
" GROUP BY vtb_dist_incat.cod_vage, " +
|
||||
" vtb_dist_incat.cod_vvet, " +
|
||||
" vtb_dist_incar.cod_anag, " +
|
||||
" vtb_dist_incar.cod_paga, " +
|
||||
" CASE WHEN IsNull(vtb_dist_incar.descriz_paga, '') <> gtb_paga.descrizione THEN vtb_dist_incar.descriz_paga ELSE NULL END , " +
|
||||
" dtb_doct.cod_dtip_val, " +
|
||||
" dtb_doct.data_doc_val, " +
|
||||
" dtb_doct.ser_doc_val, " +
|
||||
" dtb_doct.num_doc_val, " +
|
||||
" CASE WHEN " + UtilityDB.valueToString(codCconAcc) + " <> " + UtilityDB.valueToString(codCconReso) + " THEN " +
|
||||
" CASE dtb_tipi.segno_val_scar WHEN -1 THEN " + UtilityDB.valueToString(codCconReso) + " ELSE " + UtilityDB.valueToString(codCconAcc) + " END " +
|
||||
" ELSE " + UtilityDB.valueToString(codCconAcc) + " END, " +
|
||||
" ntb_tipi_inca.cod_ccon, " +
|
||||
" mtb_depo.cod_jfas ";
|
||||
sql = UtilityDB.addwhereCond(sql, condEscludiClienti, false);
|
||||
|
||||
sql =
|
||||
"WITH distinta as (" + sql + ") " +
|
||||
"SELECT *, " +
|
||||
"DENSE_RANK() OVER (ORDER BY cod_anag, data_doc_val, cod_dtip_val, ser_doc_val, num_doc_val) as gruppo_doc " +
|
||||
" FROM distinta " +
|
||||
"ORDER BY cod_anag, data_doc_val, cod_dtip_val, ser_doc_val, num_doc_val ";
|
||||
|
||||
List<HashMap<String, Object>> incassi = UtilityDB.executeSimpleQuery(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
/*
|
||||
* GRUPPO 1 = FATTURE
|
||||
* GRUPPO 2 = RIGHE SENZA AGGANCIO A DOCUMENTO
|
||||
* GRUPPO 3 = BOLLE (FATTURATE E NON )
|
||||
*/
|
||||
CtbMovt ctbMovt = new CtbMovt().setDataCmov(dataDist);
|
||||
ctbMovt.setOperation(OperationType.INSERT);
|
||||
VtbDistIncat vtbDistIncat =
|
||||
new VtbDistIncat().setDataDist(dataDist).setNumDist(numDist);
|
||||
vtbDistIncat.setOperation(OperationType.UPDATE);
|
||||
ctbMovt.setVtbDistIncat(vtbDistIncat);
|
||||
|
||||
long gruppoDocOld = 0;
|
||||
String codCcau = null;
|
||||
Boolean existPartita = false;
|
||||
TipoPartita tipoPartita = TipoPartita.ATTIVA;
|
||||
|
||||
List<CtbScad> elencoScadPartita = null;
|
||||
List<CtbScad> ctbScadList = new ArrayList<>(); //Elenco delle scadenze modificate ma non chiuse
|
||||
for (HashMap riga : incassi) {
|
||||
String codAnag = UtilityString.streNull(UtilityHashMap.getValueIfExists(riga, "cod_anag"));
|
||||
String descrPaga = UtilityHashMap.getValueIfExists(riga, "descr_paga");
|
||||
BigDecimal impIncasso = UtilityHashMap.getValueIfExists(riga, "imp_incasso");
|
||||
Date dataDocVal = UtilityHashMap.getValueIfExists(riga, "data_doc_val");
|
||||
String serDocVal = UtilityString.streNull(UtilityHashMap.getValueIfExists(riga, "ser_doc_val"));
|
||||
Integer numDocVal = UtilityHashMap.getValueIfExists(riga, "num_doc_val");
|
||||
String codCconReg = UtilityHashMap.getValueIfExists(riga, "contoReg");
|
||||
String codCconPaga = UtilityHashMap.getValueIfExists(riga, "cod_ccon_paga");
|
||||
String codJfas = UtilityHashMap.getValueIfExists(riga, "cod_jfas");
|
||||
long gruppoDoc = UtilityHashMap.getValueIfExists(riga, "gruppo_doc");
|
||||
Integer annoPart;
|
||||
if (dataDocVal != null) {
|
||||
annoPart = Integer.parseInt(new SimpleDateFormat("yyyy").format(dataDocVal));
|
||||
} else {
|
||||
annoPart = null;
|
||||
}
|
||||
if (UtilityString.isNullOrEmpty(codCconPaga)) {
|
||||
codCconPaga = codCconCassa;
|
||||
}
|
||||
|
||||
if (gruppoDoc != gruppoDocOld) {
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() == null).toList();
|
||||
ctbScadList.addAll(scad);
|
||||
}
|
||||
|
||||
HashMap<String, Object> datiPartita = chkPartita(codAnag, annoPart, serDocVal, numDocVal);
|
||||
if (datiPartita != null && !datiPartita.isEmpty()) {
|
||||
tipoPartita = (TipoPartita) datiPartita.get("tipoPartita");
|
||||
elencoScadPartita = (List<CtbScad>) datiPartita.get("elencoScadenze");
|
||||
} else {
|
||||
tipoPartita = TipoPartita.ATTIVA;
|
||||
elencoScadPartita = null;
|
||||
}
|
||||
existPartita = (elencoScadPartita != null && !elencoScadPartita.isEmpty());
|
||||
}
|
||||
|
||||
if (tipoPartita == TipoPartita.ATTIVA && impIncasso.compareTo(BigDecimal.ZERO) < 0) {
|
||||
tipoPartita = TipoPartita.PASSIVA;
|
||||
} else if (tipoPartita == TipoPartita.PASSIVA && impIncasso.compareTo(BigDecimal.ZERO) > 0) {
|
||||
tipoPartita = TipoPartita.ATTIVA;
|
||||
}
|
||||
|
||||
BigDecimal impDare = BigDecimal.ZERO, impAvere = BigDecimal.ZERO;
|
||||
if (tipoPartita == TipoPartita.PASSIVA) {
|
||||
impDare = impIncasso.abs();
|
||||
} else {
|
||||
impAvere = impIncasso.abs();
|
||||
}
|
||||
|
||||
if (existPartita) {
|
||||
codCcau = codCcauInc;
|
||||
codCconReg = codCconClie;
|
||||
Integer nextIdRiga = getNextIdRiga(ctbMovt);
|
||||
impIncasso = AccountingBusinessLogic.pagaScadenze(dataDist, impIncasso.abs(), elencoScadPartita, nextIdRiga);
|
||||
if (impIncasso.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (impDare.compareTo(BigDecimal.ZERO) > 0) {
|
||||
impDare = impDare.subtract(impIncasso);
|
||||
} else {
|
||||
impAvere = impAvere.subtract(impIncasso);
|
||||
}
|
||||
} else {
|
||||
impIncasso = BigDecimal.ZERO;
|
||||
}
|
||||
if (impAvere.compareTo(BigDecimal.ZERO) > 0 || impDare.compareTo(BigDecimal.ZERO) > 0) {
|
||||
String descrAggRiga = "PART. " + String.valueOf(annoPart).substring(2) + '-' + serDocVal + numDocVal;
|
||||
if (!UtilityString.isNullOrEmpty(descrPaga)) {
|
||||
descrAggRiga += " " + descrPaga;
|
||||
}
|
||||
if (descrAggRiga.length() > 255) {
|
||||
descrAggRiga = descrAggRiga.substring(0, 254);
|
||||
}
|
||||
|
||||
CtbMovr ctbMovr =
|
||||
new CtbMovr()
|
||||
.setCodCcon(codCconReg)
|
||||
.setTipoAnag("C")
|
||||
.setCodAnag(codAnag)
|
||||
.setAnnoPart(annoPart)
|
||||
.setSerDoc(serDocVal)
|
||||
.setNumDoc(numDocVal)
|
||||
.setImpDare(impDare)
|
||||
.setImpAvere(impAvere)
|
||||
.setCodJfas(codJfas)
|
||||
.setDesAgg(descrAggRiga)
|
||||
.setIdRiga(nextIdRiga);
|
||||
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() != null && x.getIdRigaMov().equals(nextIdRiga)).toList();
|
||||
ctbMovr.setCtbScad(scad);
|
||||
}
|
||||
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
ctbMovr = new CtbMovr()
|
||||
.setCodCcon(codCconPaga)
|
||||
.setImpDare(impAvere)
|
||||
.setImpAvere(impDare)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
}
|
||||
}
|
||||
|
||||
/*if (impIncasso.compareTo(BigDecimal.ZERO) > 0) {*/
|
||||
if (impIncasso.compareTo(BigDecimal.ZERO) != 0) {
|
||||
if (UtilityString.isNullOrEmpty(codCcau)) {
|
||||
codCcau = codCcauAcc;
|
||||
}
|
||||
|
||||
if (tipoPartita == TipoPartita.PASSIVA) {
|
||||
impDare = impIncasso.abs();
|
||||
} else {
|
||||
impAvere = impIncasso.abs();
|
||||
}
|
||||
//Registra acconto
|
||||
CtbMovr ctbMovr =
|
||||
new CtbMovr()
|
||||
.setCodCcon(codCconReg)
|
||||
.setTipoAnag("C")
|
||||
.setCodAnag(codAnag)
|
||||
.setImpDare(impDare)
|
||||
.setImpAvere(impAvere)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
|
||||
//Registra acconto
|
||||
ctbMovr = new CtbMovr()
|
||||
.setCodCcon(codCconPaga)
|
||||
.setImpDare(impAvere)
|
||||
.setImpAvere(impDare)
|
||||
.setIdRiga(getNextIdRiga(ctbMovt));
|
||||
ctbMovt.getCtbMovr().add(ctbMovr);
|
||||
|
||||
}
|
||||
gruppoDocOld = gruppoDoc;
|
||||
}
|
||||
|
||||
if (elencoScadPartita != null) {
|
||||
List<CtbScad> scad = Stream.of(elencoScadPartita).filter(x -> x.getIdRigaMov() == null).toList();
|
||||
ctbScadList.addAll(scad);
|
||||
}
|
||||
|
||||
if (ctbMovt != null) {
|
||||
ctbMovt.setCodCcau(codCcau);
|
||||
BigDecimal totMov = BigDecimal.ZERO;
|
||||
sql = "SELECT descrizione FROM ctb_caus WHERE cod_ccau = " + UtilityDB.valueToString(codCcau);
|
||||
String descrCaus = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(descrCaus)) {
|
||||
descrCaus += " DIST.N.:" + numDist;
|
||||
sql =
|
||||
"SELECT IsNull(IsNull(cod_vage, cod_vvet), IsNull(cod_mdep,'')) as descrizione, sum(imp_incasso) as imp_incasso "
|
||||
+ "FROM vtb_dist_incat, vtb_dist_incar "
|
||||
+ "WHERE vtb_dist_incat.data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " AND "
|
||||
+ "vtb_dist_incat.num_dist = " + UtilityDB.valueToString(numDist) + " AND "
|
||||
+ "vtb_dist_incat.data_dist = vtb_dist_incar.data_dist AND "
|
||||
+ "vtb_dist_incat.num_dist = vtb_dist_incar.num_dist "
|
||||
+ " GROUP BY IsNull(IsNull(cod_vage, cod_vvet), IsNull(cod_mdep,''))";
|
||||
|
||||
HashMap<String, Object> datiDist = UtilityDB.executeSimpleQueryOnlyFirstRow(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
|
||||
if (datiDist != null && !datiDist.isEmpty()) {
|
||||
descrCaus += " " + datiDist.get("descrizione");
|
||||
totMov = (BigDecimal) datiDist.get("imp_incasso");
|
||||
}
|
||||
|
||||
}
|
||||
if (descrCaus.length() > 40) {
|
||||
descrCaus = descrCaus.substring(0, 39);
|
||||
}
|
||||
ctbMovt.setDescrizioneCaus(descrCaus);
|
||||
ctbMovt.setImporto(totMov);
|
||||
//Aggancio scadenze non chiuse
|
||||
ctbMovt.setCtbScad(ctbScadList);
|
||||
}
|
||||
|
||||
return ctbMovt;
|
||||
}
|
||||
|
||||
private Integer getNextIdRiga(CtbMovt ctbMovt) {
|
||||
Integer idRigaMov = ctbMovt.getCtbMovr().size();
|
||||
if (idRigaMov == null) idRigaMov = 0;
|
||||
idRigaMov = idRigaMov + 1;
|
||||
return idRigaMov;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> chkPartita(String codAnag, Integer annoPart, String serDoc, Integer numDoc) throws Exception {
|
||||
String sql =
|
||||
"SELECT Cast(tipo_partita as integer) as tipo_partita, tipo_anag "
|
||||
+ "FROM ctb_part "
|
||||
+ "WHERE ( ctb_part.cod_anag = " + UtilityDB.valueToString(codAnag) + " ) AND "
|
||||
+ " ( ctb_part.anno_part = " + UtilityDB.valueToString(annoPart) + " ) AND "
|
||||
+ " ( ctb_part.ser_doc = " + UtilityDB.valueToString(serDoc) + " ) AND "
|
||||
+ " ( ctb_part.num_doc = " + UtilityDB.valueToString(numDoc) + " ) AND "
|
||||
+ " ( ctb_part.dare_part <> ctb_part.avere_part ) ";
|
||||
HashMap<String, Object> dati = UtilityDB.executeSimpleQueryOnlyFirstRow(multiDBTransactionManager.getPrimaryConnection(), sql);
|
||||
if (dati != null && !dati.isEmpty()) {
|
||||
TipoPartita tipoPartita = TipoPartita.fromValue((Integer) dati.get("tipo_partita"));
|
||||
List<CtbScad> elencoScadenze = AccountingBusinessLogic.loadScadenze(multiDBTransactionManager.getPrimaryConnection(), (String) dati.get("tipo_anag"), codAnag, annoPart, serDoc, numDoc);
|
||||
HashMap<String, Object> datiRet = new HashMap<>();
|
||||
datiRet.put("tipoPartita", tipoPartita);
|
||||
datiRet.put("elencoScadenze", elencoScadenze);
|
||||
return datiRet;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -584,14 +584,14 @@ public class DistribuzioneColliService {
|
||||
|
||||
DtbOrdt dtbOrdt = finalOrderTestate.stream()
|
||||
.filter(dtbOrdt1 -> dtbOrdt1.getNumOrd().equals(mtbColt.getNumOrd()) &&
|
||||
dtbOrdt1.getDataOrd().equals(mtbColt.getDataOrd()) &&
|
||||
UtilityLocalDate.localDateFromDate(dtbOrdt1.getDataOrd()).equals(mtbColt.getDataOrd()) &&
|
||||
dtbOrdt1.getGestione().equals(mtbColt.getGestione()))
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
DtbOrdr dtbOrdr = finalOrderRows.stream()
|
||||
.filter(dtbOrdr1 -> dtbOrdr1.getNumOrd().equals(mtbColt.getNumOrd()) &&
|
||||
dtbOrdr1.getDataOrd().equals(mtbColt.getDataOrd()) &&
|
||||
UtilityLocalDate.localDateFromDate(dtbOrdr1.getDataOrd()).equals(mtbColt.getDataOrd()) &&
|
||||
dtbOrdr1.getGestione().equals(mtbColt.getGestione()))
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
@@ -2634,7 +2634,7 @@ public class WMSGenericService {
|
||||
MtbColt mtbColt = null;
|
||||
|
||||
if (order != null) {
|
||||
mtbColt = Stream.of(mtbColtsToInsert).filter(x -> order.getDataOrd().equals(x.getDataOrd()) &&
|
||||
mtbColt = mtbColtsToInsert.stream().filter(x -> UtilityLocalDate.localDateFromDate(order.getDataOrd()).equals(x.getDataOrd()) &&
|
||||
order.getNumOrd().equals(x.getNumOrd()) &&
|
||||
order.getGestione().equals(x.getGestione()) &&
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user