Creata procedura di importazione pesate da bilancia
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:
@@ -24,7 +24,8 @@ public enum EntityImportType {
|
||||
MOVIMENTI_CONTABILI("MOVIMENTI CONTABILI"),
|
||||
GRIGLIA_VENDITA("GRIGLIA VENDITA"),
|
||||
GRIGLIA_ACQUISTO("GRIGLIA ACQUISTO"),
|
||||
SCADENZE("SCADENZE");
|
||||
SCADENZE("SCADENZE"),
|
||||
BILANCE("BILANCE");
|
||||
|
||||
private String text;
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20250807114849 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
if (isHistoryDB())
|
||||
return;
|
||||
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "ATTIVO", "N",
|
||||
"Inserire S/N per attivare/disattivare il tipo di importazione", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "EMAIL_FOR_LOG", null,
|
||||
"Inserire il file l'indirizzo email per inviare il log dell'importazione", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "FILE_FILTER", null,
|
||||
"Inserire il criterio di filtro dei file o il nome del file specifio (seguire la sintassi di java)", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "GG_CANC_FILE", null,
|
||||
"Giorni per la cancellazione dei file dalla cartella di appoggio", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "IMPORT_REST", "S",
|
||||
"Inserire S/N per attivare/disattivare il salvataggio con i servizi REST", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "LISTENING", "N",
|
||||
"Se LISTENIG = 'N' la procedura di importazione non si potrà mai mettere in ascolto sulla directory perchè nella procedura vengono prese in considerazione più file.", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "PATH_FILE", null,
|
||||
"Directory contente i file da importare.", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
createSetup("IMPORT_BILANCE", "DIGI", "PATH_FILE_IMPORTED", null,
|
||||
"Directory dove vengono spostati i file dopo l'importazione.", false, null, false, false,
|
||||
false, false, false, null, false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package it.integry.ems.migration.model;
|
||||
|
||||
import it.integry.ems.migration._base.BaseMigration;
|
||||
import it.integry.ems.migration._base.MigrationModelInterface;
|
||||
|
||||
public class Migration_20250807114902 extends BaseMigration implements MigrationModelInterface {
|
||||
|
||||
@Override
|
||||
public void up() throws Exception {
|
||||
if (isHistoryDB())
|
||||
return;
|
||||
|
||||
executeStatement("CREATE TABLE ntb_pesate\n" +
|
||||
"(\n" +
|
||||
" id BIGINT IDENTITY PRIMARY KEY,\n" +
|
||||
" cod_mdep VARCHAR(5) NOT NULL,\n" +
|
||||
" data_pesate DATETIME NOT NULL,\n" +
|
||||
" cod_mart VARCHAR(15) NOT NULL,\n" +
|
||||
" qta_pesata NUMERIC(10, 5) NOT NULL DEFAULT 0,\n" +
|
||||
" prz_vend NUMERIC(20, 5) NOT NULL DEFAULT 0,\n" +
|
||||
"\n" +
|
||||
" CONSTRAINT fk_ntb_pesate_mtb_depo\n" +
|
||||
" FOREIGN KEY (cod_mdep) REFERENCES mtb_depo (cod_mdep),\n" +
|
||||
" CONSTRAINT fk_ntb_pesate_mtb_aart\n" +
|
||||
" FOREIGN KEY (cod_mart) REFERENCES mtb_aart (cod_mart)\n" +
|
||||
");");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down() throws Exception {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package it.integry.ems_model.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import it.integry.ems_model.annotation.*;
|
||||
import it.integry.ems_model.base.EntityBase;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.kie.api.definition.type.PropertyReactive;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Master
|
||||
@PropertyReactive
|
||||
@Table(NtbPesate.ENTITY)
|
||||
@JsonTypeName(NtbPesate.ENTITY)
|
||||
public class NtbPesate extends EntityBase {
|
||||
|
||||
private final static Logger logger = LogManager.getLogger();
|
||||
|
||||
public static final String ENTITY = "ntb_pesate";
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Identity
|
||||
@PK
|
||||
@SqlField(value = "id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@FK(tableName = MtbDepo.ENTITY, columnName = "cod_mdep")
|
||||
@SqlField(value = "cod_mdep", maxLength = 5, nullable = false)
|
||||
private String codMdep;
|
||||
|
||||
@SqlField(value = "data_pesate", nullable = false)
|
||||
private LocalDateTime dataPesate;
|
||||
|
||||
@FK(tableName = MtbAart.ENTITY, columnName = "cod_mart")
|
||||
@SqlField(value = "cod_mart", maxLength = 15, nullable = false)
|
||||
private String codMart;
|
||||
|
||||
@SqlField(value = "qta_pesata", nullable = false, defaultObjectValue = "0")
|
||||
private BigDecimal qtaPesata;
|
||||
|
||||
@SqlField(value = "prz_vend", nullable = false, defaultObjectValue = "0")
|
||||
private BigDecimal przVend;
|
||||
|
||||
public NtbPesate() {
|
||||
super(logger);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public NtbPesate setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getCodMdep() {
|
||||
return codMdep;
|
||||
}
|
||||
|
||||
public NtbPesate setCodMdep(String codMdep) {
|
||||
this.codMdep = codMdep;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LocalDateTime getDataPesate() {
|
||||
return dataPesate;
|
||||
}
|
||||
|
||||
public NtbPesate setDataPesate(LocalDateTime dataPesate) {
|
||||
this.dataPesate = dataPesate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getCodMart() {
|
||||
return codMart;
|
||||
}
|
||||
|
||||
public NtbPesate setCodMart(String codMart) {
|
||||
this.codMart = codMart;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BigDecimal getQtaPesata() {
|
||||
return qtaPesata;
|
||||
}
|
||||
|
||||
public NtbPesate setQtaPesata(BigDecimal qtaPesata) {
|
||||
this.qtaPesata = qtaPesata;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BigDecimal getPrzVend() {
|
||||
return przVend;
|
||||
}
|
||||
|
||||
public NtbPesate setPrzVend(BigDecimal przVend) {
|
||||
this.przVend = przVend;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.integry.ems_model.utility;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -8,10 +9,37 @@ import java.util.List;
|
||||
|
||||
public class UtilityCSV {
|
||||
|
||||
/**
|
||||
* Reads a CSV content from a string and returns its content as a list of lists of strings.
|
||||
*
|
||||
* @param csvContent the CSV content as a string
|
||||
* @param delimiter the delimiter used in the CSV content
|
||||
* @return a list of lists, where each inner list represents a row in the CSV content
|
||||
* @throws Exception if an error occurs while reading the content
|
||||
*/
|
||||
public static List<List<String>> readCSV(String csvContent, String delimiter) throws Exception {
|
||||
List<List<String>> records = new ArrayList<>();
|
||||
return readCSV(new BufferedReader(new StringReader(csvContent)), delimiter);
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(new StringReader(csvContent));
|
||||
|
||||
/**
|
||||
* Reads a CSV file and returns its content as a list of lists of strings.
|
||||
*
|
||||
* @param file the CSV file to read
|
||||
* @param delimiter the delimiter used in the CSV file
|
||||
* @return a list of lists, where each inner list represents a row in the CSV file
|
||||
* @throws Exception if an error occurs while reading the file
|
||||
*/
|
||||
public static List<List<String>> readCSVFile(File file, String delimiter) throws Exception {
|
||||
try (BufferedReader br = new BufferedReader(new java.io.FileReader(file))) {
|
||||
return readCSV(br, delimiter);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Error reading CSV file: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<List<String>> readCSV(BufferedReader br, String delimiter) throws Exception {
|
||||
List<List<String>> records = new ArrayList<>();
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package it.integry.ems.retail.Import;
|
||||
|
||||
import it.integry.ems.Import.base.BaseEntityImporter;
|
||||
import it.integry.ems.Import.base.IEntityImporter;
|
||||
import it.integry.ems.retail.service.BilanceService;
|
||||
import it.integry.ems_model.base.EntityBase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BilanceImporter extends BaseEntityImporter<List<EntityBase>> implements IEntityImporter<List<EntityBase>> {
|
||||
|
||||
@Override
|
||||
public List<EntityBase> doImport() throws Exception {
|
||||
Format enumFormat = Format.fromString(super.format);
|
||||
List<EntityBase> entities;
|
||||
|
||||
if (enumFormat != null){
|
||||
switch (enumFormat) {
|
||||
case DIGI:
|
||||
entities = getContextBean(BilanceService.class).importBilance(type, format, requestDto);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Tipo " + format + " non supportato");
|
||||
}
|
||||
} else throw new Exception("Tipo " + format + " non definito");
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public enum Format {
|
||||
DIGI("DIGI");
|
||||
|
||||
private final String text;
|
||||
|
||||
Format(String text) { this.text = text; }
|
||||
|
||||
public static Format fromString(String text) {
|
||||
for (Format b : Format.values()) {
|
||||
if (b.text.equalsIgnoreCase(text)) return b;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getText() { return this.text; }
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import it.integry.ems.Import.base.EntityImporterUtility;
|
||||
import it.integry.ems.Import.enums.EntityImportType;
|
||||
import it.integry.ems.export.base.EntityExporterUtility;
|
||||
import it.integry.ems.export.enums.EntityExportType;
|
||||
import it.integry.ems.retail.Import.BilanceImporter;
|
||||
import it.integry.ems.retail.Import.RapportiniFgImporter;
|
||||
import it.integry.ems.retail.Import.ScontriniImporter;
|
||||
import it.integry.ems.retail.export.ScontriniExporter;
|
||||
@@ -28,6 +29,7 @@ public class EmsRetailContext {
|
||||
|
||||
EntityImporterUtility.addTypeBinding(EntityImportType.RAPPORTINI_FG, RapportiniFgImporter.class);
|
||||
EntityImporterUtility.addTypeBinding(EntityImportType.SCONTRINI, ScontriniImporter.class);
|
||||
EntityImporterUtility.addTypeBinding(EntityImportType.BILANCE, BilanceImporter.class);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package it.integry.ems.retail.service;
|
||||
|
||||
import it.integry.ems.Import.dto.ImportRequestDTO;
|
||||
import it.integry.ems.service.EntityProcessor;
|
||||
import it.integry.ems.sync.MultiDBTransaction.Connection;
|
||||
import it.integry.ems.sync.MultiDBTransaction.MultiDBTransactionManager;
|
||||
import it.integry.ems.utility.UtilityEntity;
|
||||
import it.integry.ems.utility.UtilityFile;
|
||||
import it.integry.ems_model.base.EntityBase;
|
||||
import it.integry.ems_model.entity.NtbPesate;
|
||||
import it.integry.ems_model.service.SetupGest;
|
||||
import it.integry.ems_model.types.OperationType;
|
||||
import it.integry.ems_model.utility.*;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Scope(value = "request")
|
||||
public class BilanceService {
|
||||
|
||||
@Autowired
|
||||
private MultiDBTransactionManager multiDBTransactionManager;
|
||||
|
||||
@Autowired
|
||||
private EntityProcessor entityProcessor;
|
||||
|
||||
@Autowired
|
||||
private SetupGest setupGest;
|
||||
|
||||
public List<EntityBase> importBilance(String type, String format, ImportRequestDTO requestDto) throws Exception {
|
||||
Connection conn = multiDBTransactionManager.getPrimaryConnection();
|
||||
List<EntityBase> returnList = new ArrayList<>();
|
||||
|
||||
String pathFile = setupGest.getImportSetup(conn, type, format, "PATH_FILE");
|
||||
if (UtilityString.isNullOrEmpty(pathFile)) throw new Exception(("Directory non configurata per l'importazione delle bilance"));
|
||||
if (!UtilityFile.directoryExists(pathFile)) throw new Exception("La directory specificata non esiste: " + pathFile);
|
||||
|
||||
String pathFileImported = setupGest.getImportSetup(conn, type, format, "PATH_FILE_IMPORTED");
|
||||
if (UtilityString.isNullOrEmpty(pathFileImported)) throw new Exception("Directory per i file importati non configurata");
|
||||
if (!UtilityFile.directoryExists(pathFileImported)) UtilityFile.directoryCreate(pathFileImported);
|
||||
|
||||
List<File> listFileToImport = new ArrayList<>();
|
||||
File directory = new File(pathFile);
|
||||
File[] filteredFiles = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".csv"));
|
||||
if (filteredFiles != null) listFileToImport.addAll(Arrays.asList(filteredFiles));
|
||||
|
||||
if (listFileToImport.isEmpty()) throw new Exception("Nessun file CSV trovato nella directory: " + pathFile);
|
||||
|
||||
String sql = "SELECT cod_mdep FROM mtb_depo WHERE cod_vlis IS NOT NULL AND flag_movimentabile = 'S'";
|
||||
List<String> codMdepList = UtilityDB.executeSimpleQueryOnlyFirstColumn(conn, sql);
|
||||
|
||||
for (File file : listFileToImport) {
|
||||
String fileName = file.getName();
|
||||
String fileCodMdep = fileName.substring(0, 2);
|
||||
|
||||
if (!codMdepList.contains(fileCodMdep))
|
||||
continue; // Skip files not matching any deposit code
|
||||
|
||||
List<NtbPesate> pesate = new ArrayList<>();
|
||||
List<List<String>> csvRows = UtilityCSV.readCSVFile(file, ";");
|
||||
for (int i = 0; i < csvRows.size(); i++) {
|
||||
if (i == 0) continue; // Skip header row
|
||||
|
||||
NtbPesate pesata = new NtbPesate();
|
||||
pesata.setOperation(OperationType.INSERT);
|
||||
|
||||
List<String> row = csvRows.get(i);
|
||||
if (row.isEmpty() || row.get(0).isEmpty()) continue; // Skip empty rows
|
||||
|
||||
pesata.setCodMdep(fileCodMdep)
|
||||
.setDataPesate(UtilityString.parseLocalDateTime(row.get(1)))
|
||||
.setQtaPesata(BigDecimal.valueOf(Double.parseDouble(row.get(2))))
|
||||
.setPrzVend(BigDecimal.valueOf(Double.parseDouble(row.get(4))));
|
||||
|
||||
String plu = row.get(7).substring(1, 7);
|
||||
sql = Query.format(
|
||||
"SELECT cod_mart\n" +
|
||||
"FROM mtb_aart\n" +
|
||||
"WHERE plu = %s",
|
||||
plu
|
||||
);
|
||||
|
||||
pesata.setCodMart(UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(conn, sql));
|
||||
pesate.add(pesata);
|
||||
}
|
||||
|
||||
if (pesate.isEmpty()) continue; // Skip files with no valid data
|
||||
|
||||
List<EntityBase> entityList = entityProcessor.processEntityList(pesate, true);
|
||||
UtilityEntity.throwEntitiesException(entityList);
|
||||
|
||||
// Move a file to imported directory
|
||||
File importedFile = new File(pathFileImported + File.separator + fileName);
|
||||
FileUtils.moveFile(file, importedFile);
|
||||
|
||||
returnList.addAll(entityList);
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user