Bonifico SEPA
This commit is contained in:
@@ -57,6 +57,8 @@ public class ContabilController {
|
||||
private RequestDataDTO requestDataDTO;
|
||||
@Autowired
|
||||
private AmmortamentiService ammortamentiService;
|
||||
@Autowired
|
||||
private SEPAService SEPAService;
|
||||
|
||||
@Autowired
|
||||
private DulciarSincronizzazione dulciarSincronizzazione;
|
||||
@@ -94,7 +96,7 @@ public class ContabilController {
|
||||
@RequestParam("dataDist") String dataDist, @RequestParam("rifDist") Integer rifDist) {
|
||||
ServiceRestResponse resp = null;
|
||||
try {
|
||||
resp = contabilService.getSepaXml(UtilityString.parseDate(dataDist), rifDist);
|
||||
resp =SEPAService.getSepaXml(UtilityString.parseDate(dataDist), rifDist);
|
||||
} catch (Exception e) {
|
||||
logger.error(request.getRequestURI(), e);
|
||||
resp = new ServiceRestResponse(EsitoType.KO, configuration, e);
|
||||
|
||||
@@ -16,7 +16,6 @@ import it.integry.ems.contabil.partitaIva.interfaces.CheckVatPortType;
|
||||
import it.integry.ems.contabil.partitaIva.services.CheckVatService;
|
||||
import it.integry.ems.contabil.sepa.adapter.DelegatingXMLStreamWriter;
|
||||
import it.integry.ems.contabil.sepa.adapter.PmtInfoAdapter;
|
||||
import it.integry.ems.contabil.sepa.dto.v00_04_01.*;
|
||||
import it.integry.ems.contabil.sepa.sdd.dto.ObjectFactory;
|
||||
import it.integry.ems.contabil.sepa.sdd.dto.*;
|
||||
import it.integry.ems.contabil.sepa.static_variables.Currency;
|
||||
@@ -79,293 +78,6 @@ public class ContabilService {
|
||||
@Autowired
|
||||
private MapService mapService;
|
||||
|
||||
public ServiceRestResponse getSepaXml(Date dataDist, Integer rifDist) throws Exception {
|
||||
|
||||
String codCuc = null, ragSoc = null, partIva = null;
|
||||
|
||||
String sqlCuc = "select cod_cuc, nome_ditta, part_iva from azienda";
|
||||
PreparedStatement ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlCuc);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
codCuc = rs.getString("cod_cuc");
|
||||
ragSoc = rs.getString("nome_ditta");
|
||||
partIva = rs.getString("part_iva");
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (UtilityString.isNullOrEmpty(codCuc))
|
||||
throw new Exception("codice CUC non impostato.");
|
||||
|
||||
//CBIBdyPaymentRequest000401 root = new CBIBdyPaymentRequest000401();
|
||||
|
||||
String ibanOrdinante = null, codAbi = null, codBancAzi = "";
|
||||
String datiDebitore = "select cod_iban, cod_abi, cod_banc_azi from vtb_dist, gtb_banc_azi"
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " and vtb_dist.cod_banc=gtb_banc_azi.cod_banc";
|
||||
PreparedStatement psDeb = multiDBTransactionManager.getPrimaryConnection().prepareStatement(datiDebitore);
|
||||
ResultSet rsDeb = psDeb.executeQuery();
|
||||
if (rsDeb.next()) {
|
||||
ibanOrdinante = rsDeb.getString("cod_iban");
|
||||
codAbi = rsDeb.getString("cod_abi");
|
||||
codBancAzi = rsDeb.getString("cod_banc_azi");
|
||||
}
|
||||
rsDeb.close();
|
||||
psDeb.close();
|
||||
if (UtilityString.isNullOrEmpty(ibanOrdinante) || UtilityString.isNullOrEmpty(codAbi))
|
||||
throw new Exception("IBAN o COD ABI non trovato per banca " + codBancAzi);
|
||||
|
||||
String sqlBonifici =
|
||||
"SELECT sum(imp_scad) tot_bonifico, " +
|
||||
"gtb_anag.rag_soc as rag_soc_benef, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc) as piva_cf_benef, " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END as bic_benef, " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END as abi_benef, " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) as iban_benef," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " from vtb_riba inner join atb_forn on vtb_riba.cod_anag = atb_forn.cod_anag "
|
||||
+ " INNER JOIN ctb_part ON ctb_part.tipo_anag = vtb_riba.tipo_anag AND " +
|
||||
"ctb_part.cod_anag = vtb_riba.cod_anag AND " +
|
||||
"ctb_part.data_doc = vtb_riba.data_doc AND " +
|
||||
"ctb_part.ser_doc = vtb_riba.ser_doc AND " +
|
||||
"ctb_part.num_doc = vtb_riba.num_doc "
|
||||
+ " inner join gtb_anag on atb_forn.cod_anag = gtb_anag.cod_anag, "
|
||||
+ "azienda "
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " group by gtb_anag.rag_soc, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc), " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END , " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END , " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) ," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " order by data_valuta";
|
||||
ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlBonifici);
|
||||
rs = ps.executeQuery();
|
||||
|
||||
Date dataValutaOld = null;
|
||||
Integer i = 0;
|
||||
BigDecimal somma = BigDecimal.ZERO;
|
||||
Boolean bonificoEstero = false;
|
||||
|
||||
String msgId = "DistintaXml-" + new SimpleDateFormat("ddMMyyyy-HH.mm.ss").format(new Date());
|
||||
CBIPaymentRequest000401 payment = setBasicPayment(msgId, ragSoc, codCuc);
|
||||
|
||||
List<CBIPaymentRequest000401> paymList = new ArrayList<CBIPaymentRequest000401>();
|
||||
while (rs.next()) {
|
||||
|
||||
if (dataValutaOld == null) {
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
if (dataValutaOld.compareTo(rs.getDate("data_valuta")) != 0) {
|
||||
//sommatoria
|
||||
payment.getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
payment.getGrpHdr().setNbOfTxs(i.toString());
|
||||
i = 0;
|
||||
somma = BigDecimal.ZERO;
|
||||
|
||||
bonificoEstero = false;
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
payment = setBasicPayment(msgId, ragSoc, codCuc);
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
GregorianCalendar dataValGreg = new GregorianCalendar();
|
||||
dataValGreg.setTime(rs.getDate("data_valuta"));
|
||||
XMLGregorianCalendar dataExec = DatatypeFactory.newInstance().newXMLGregorianCalendar(dataValGreg);
|
||||
|
||||
CBIPaymentInstructionInformation pmtInf = new CBIPaymentInstructionInformation();
|
||||
DateAndDateTime2Choice dateAndDateTime2Choice = new DateAndDateTime2Choice();
|
||||
dateAndDateTime2Choice.setDt(dataExec);
|
||||
payment.getPmtInf().setReqdExctnDt(dateAndDateTime2Choice);
|
||||
|
||||
|
||||
BigDecimal importo = rs.getBigDecimal("tot_bonifico").setScale(2);
|
||||
somma = somma.add(importo);
|
||||
i++;
|
||||
|
||||
CBIPaymentTypeInformation1 paymType = new CBIPaymentTypeInformation1();
|
||||
|
||||
paymType.setInstrPrty(Priority2Code.NORM);
|
||||
CBIServiceLevel1 cbiServiceLevel1 = new CBIServiceLevel1();
|
||||
cbiServiceLevel1.setCd(CBIServiceLevel1Code.SEPA);
|
||||
paymType.setSvcLvl(cbiServiceLevel1);
|
||||
payment.getPmtInf().setPmtTpInf(paymType);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification4 ordinantePayData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification4();
|
||||
ordinantePayData.setNm(ragSoc);
|
||||
CBIIdType2 ordinanteIdData = new CBIIdType2();
|
||||
CBIOrganisationIdentification3 ordinanteOrgData = new CBIOrganisationIdentification3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 ordinanteGenericIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
ordinanteGenericIdData.setId(partIva);
|
||||
ordinanteGenericIdData.setIssr("ADE");
|
||||
ordinanteOrgData.setOthr(ordinanteGenericIdData);
|
||||
|
||||
ordinanteIdData.setOrgId(ordinanteOrgData);
|
||||
ordinantePayData.setId(ordinanteIdData);
|
||||
payment.getPmtInf().setDbtr(ordinantePayData);
|
||||
|
||||
CBICashAccount2 ordinanteAccount = new CBICashAccount2();
|
||||
CBIAccountIdentification1 ordinanteAccountId = new CBIAccountIdentification1();
|
||||
ordinanteAccountId.setIBAN(ibanOrdinante);
|
||||
ordinanteAccount.setId(ordinanteAccountId);
|
||||
payment.getPmtInf().setDbtrAcct(ordinanteAccount);
|
||||
|
||||
CBIBranchAndFinancialInstitutionIdentification2 cbiBranch = new CBIBranchAndFinancialInstitutionIdentification2();
|
||||
CBIFinancialInstitutionIdentification3 cbiFin = new CBIFinancialInstitutionIdentification3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIClearingSystemMemberIdentification1 ordinanteMemberId = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIClearingSystemMemberIdentification1();
|
||||
|
||||
//abi
|
||||
ordinanteMemberId.setMmbId(codAbi);
|
||||
cbiFin.setClrSysMmbId(ordinanteMemberId);
|
||||
cbiBranch.setFinInstnId(cbiFin);
|
||||
payment.getPmtInf().setDbtrAgt(cbiBranch);
|
||||
|
||||
CBICreditTransferTransactionInformation creditTransaction = new CBICreditTransferTransactionInformation();
|
||||
PaymentIdentification1 payTrasactId = new PaymentIdentification1();
|
||||
payTrasactId.setInstrId(i.toString());
|
||||
payTrasactId.setEndToEndId(msgId + "-" + UtilityString.leftPad(i.toString(), 4, '0'));
|
||||
creditTransaction.setPmtId(payTrasactId);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation2 paymentTypeTransact = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation2();
|
||||
|
||||
CategoryPurpose1Choice category = new CategoryPurpose1Choice();
|
||||
category.setCd("SUPP");
|
||||
paymentTypeTransact.setCtgyPurp(category);
|
||||
creditTransaction.setPmtTpInf(paymentTypeTransact);
|
||||
CBIAmountType1 amountCurrency = new CBIAmountType1();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.ActiveOrHistoricCurrencyAndAmount currency = new it.integry.ems.contabil.sepa.dto.v00_04_01.ActiveOrHistoricCurrencyAndAmount();
|
||||
currency.setCcy("EUR");
|
||||
currency.setValue(importo);
|
||||
amountCurrency.setInstdAmt(currency);
|
||||
creditTransaction.setAmt(amountCurrency);
|
||||
|
||||
if (UtilityString.isNullOrEmpty(rs.getString("iban_benef")))
|
||||
throw new Exception("Iban non presente per " + rs.getString("rag_soc_benef"));
|
||||
|
||||
//beneficiario
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification3 benficiarioPayData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification3();
|
||||
|
||||
String ibanBeneficiario = rs.getString("iban_benef").replaceAll(" ", "");
|
||||
|
||||
if (!ibanBeneficiario.startsWith("IT"))
|
||||
bonificoEstero = true;
|
||||
|
||||
if (bonificoEstero && !UtilityString.isNullOrEmpty(rs.getString("bic_benef"))) {
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification3 benefBranch = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification3();
|
||||
CBIFinancialInstitutionIdentification2 benefFin = new CBIFinancialInstitutionIdentification2();
|
||||
benefFin.setBICFI(rs.getString("bic_benef"));
|
||||
benefBranch.setFinInstnId(benefFin);
|
||||
creditTransaction.setCdtrAgt(benefBranch);
|
||||
}
|
||||
|
||||
benficiarioPayData.setNm(rs.getString("rag_soc_benef"));
|
||||
|
||||
CBIPostalAddress24 addressInfo = new CBIPostalAddress24();
|
||||
//dati indirizzo
|
||||
AddressType3Choice addressType3Choice = new AddressType3Choice();
|
||||
addressType3Choice.setCd(it.integry.ems.contabil.sepa.dto.v00_04_01.AddressType2Code.ADDR);
|
||||
addressInfo.setAdrTp(addressType3Choice);
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("indirizzo")))
|
||||
addressInfo.setStrtNm(rs.getString("indirizzo"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("cap")))
|
||||
addressInfo.setPstCd(rs.getString("cap"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("citta")))
|
||||
addressInfo.setTwnNm(rs.getString("citta"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("prov")))
|
||||
addressInfo.setCtrySubDvsn(rs.getString("prov"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("nazione")))
|
||||
addressInfo.setCtry(rs.getString("nazione"));
|
||||
|
||||
benficiarioPayData.setPstlAdr(addressInfo);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIParty1Choice benefIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIParty1Choice();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification2 benefOrgData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification2();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 benefGenericIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
benefGenericIdData.setId(rs.getString("piva_cf_benef"));
|
||||
benefGenericIdData.setIssr("ADE");
|
||||
benefOrgData.setOthr(benefGenericIdData);
|
||||
benefIdData.setOrgId(benefOrgData);
|
||||
benficiarioPayData.setId(benefIdData);
|
||||
|
||||
|
||||
creditTransaction.setCdtr(benficiarioPayData);
|
||||
|
||||
CBICashAccount3 beneficiarioAccount = new CBICashAccount3();
|
||||
CBIAccountIdentification1 beneficiarioAccountId = new CBIAccountIdentification1();
|
||||
|
||||
beneficiarioAccountId.setIBAN(ibanBeneficiario);
|
||||
beneficiarioAccount.setId(beneficiarioAccountId);
|
||||
creditTransaction.setCdtrAcct(beneficiarioAccount);
|
||||
|
||||
if (bonificoEstero) {
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIRegulatoryReporting1 cbiReport = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIRegulatoryReporting1();
|
||||
cbiReport.setDbtCdtRptgInd(CBIRegulatoryReportingType1Code.DEBT);
|
||||
}
|
||||
|
||||
RemittanceInformation16 causale = new RemittanceInformation16();
|
||||
causale.getUstrd().add("PAGAMENTO " + rs.getString("rif_debito").trim());
|
||||
creditTransaction.setRmtInf(causale);
|
||||
|
||||
payment.getPmtInf().getCdtTrfTxInf().add(creditTransaction);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (paymList.size() == 1) {
|
||||
//sommatoria
|
||||
paymList.get(0).getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
paymList.get(0).getGrpHdr().setNbOfTxs(i.toString());
|
||||
}
|
||||
|
||||
List<Object> genericList = new ArrayList<>();
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(CBIPaymentRequest000401.class);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
||||
|
||||
for (CBIPaymentRequest000401 paym : paymList) {
|
||||
StringWriter writer = new StringWriter();
|
||||
jaxbMarshaller.marshal(paym, writer);
|
||||
|
||||
FileItem fileItem = new FileItem("sepa-" + dateFormat.format(paym.getPmtInf().getReqdExctnDt().getDt().toGregorianCalendar().getTime()) + ".xml", writer.toString(), "xml");
|
||||
genericList.add(fileItem);
|
||||
}
|
||||
ServiceRestResponse resp = new ServiceRestResponse(EsitoType.OK, genericList);
|
||||
|
||||
updateDataExport(dataDist, rifDist);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
private void updateDataExport(Date dataDist, Integer rifDist) throws Exception {
|
||||
VtbDist vtbDist = new VtbDist()
|
||||
.setDataDist(dataDist)
|
||||
@@ -376,38 +88,6 @@ public class ContabilService {
|
||||
entityProcessor.processEntity(vtbDist, false, multiDBTransactionManager);
|
||||
|
||||
}
|
||||
|
||||
private CBIPaymentRequest000401 setBasicPayment(String msgId, String ragSoc, String codSia) throws Exception {
|
||||
|
||||
CBIPaymentRequest000401 payment = new CBIPaymentRequest000401();
|
||||
CBIGroupHeader header = new CBIGroupHeader();
|
||||
header.setMsgId(msgId);
|
||||
GregorianCalendar nowGreg = new GregorianCalendar();
|
||||
nowGreg.setTime(new Date());
|
||||
XMLGregorianCalendar now = DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGreg);
|
||||
header.setCreDtTm(now);
|
||||
payment.setGrpHdr(header);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification1 ordinante = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification1();
|
||||
ordinante.setNm(ragSoc);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification1 org = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification1();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 genericIdOrdinante = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
genericIdOrdinante.setId(codSia);
|
||||
genericIdOrdinante.setIssr("CBI");
|
||||
|
||||
org.getOthr().add(genericIdOrdinante);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType1 cbiType = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType1();
|
||||
cbiType.setOrgId(org);
|
||||
ordinante.setId(cbiType);
|
||||
header.setInitgPty(ordinante);
|
||||
CBIPaymentInstructionInformation paymInfo = new CBIPaymentInstructionInformation();
|
||||
paymInfo.setChrgBr(it.integry.ems.contabil.sepa.dto.v00_04_01.CBIChargeBearerTypeCode.SLEV);
|
||||
paymInfo.setPmtInfId(msgId);
|
||||
paymInfo.setPmtMtd(PaymentMethod3Code.TRF);
|
||||
payment.setPmtInf(paymInfo);
|
||||
return payment;
|
||||
}
|
||||
|
||||
public List<EntityBase> registraDistintaIncassi(List<DistinteIncassiDTO> distinte) throws Exception {
|
||||
List<EntityBase> entities = new ArrayList<EntityBase>();
|
||||
for (DistinteIncassiDTO distinta : distinte) {
|
||||
|
||||
@@ -0,0 +1,740 @@
|
||||
package it.integry.ems.contabil.service;
|
||||
|
||||
import it.integry.common.var.CommonConstants;
|
||||
import it.integry.ems.contabil.sepa.dto.*;
|
||||
import it.integry.ems.contabil.sepa.dto.ActiveOrHistoricCurrencyAndAmount;
|
||||
import it.integry.ems.contabil.sepa.dto.AddressType2Code;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIAccountIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIAmountType1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIBranchAndFinancialInstitutionIdentification2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIBranchAndFinancialInstitutionIdentification3;
|
||||
import it.integry.ems.contabil.sepa.dto.CBICashAccount1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBICashAccount2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIChargeBearerTypeCode;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIClearingSystemMemberIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBICreditTransferTransactionInformation;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIFinancialInstitutionIdentification2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIFinancialInstitutionIdentification3;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIGenericIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIGroupHeader;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIIdType1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIIdType2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIOrganisationIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIOrganisationIdentification2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIOrganisationIdentification3;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIParty1Choice;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPartyIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPartyIdentification3;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPartyIdentification4;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPaymentInstructionInformation;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPaymentTypeInformation1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIPaymentTypeInformation2;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIRegulatoryReporting1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIRegulatoryReportingType1Code;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIServiceLevel1;
|
||||
import it.integry.ems.contabil.sepa.dto.CBIServiceLevel1Code;
|
||||
import it.integry.ems.contabil.sepa.dto.CategoryPurpose1Choice;
|
||||
import it.integry.ems.contabil.sepa.dto.PaymentIdentification1;
|
||||
import it.integry.ems.contabil.sepa.dto.PaymentMethod3Code;
|
||||
import it.integry.ems.contabil.sepa.dto.Priority2Code;
|
||||
import it.integry.ems.contabil.sepa.dto.v00_04_01.*;
|
||||
import it.integry.ems.response.EsitoType;
|
||||
import it.integry.ems.response.FileItem;
|
||||
import it.integry.ems.response.ServiceRestResponse;
|
||||
import it.integry.ems.service.EntityProcessor;
|
||||
import it.integry.ems.service.MapService;
|
||||
import it.integry.ems.sync.MultiDBTransaction.MultiDBTransactionManager;
|
||||
import it.integry.ems_model.entity.VtbDist;
|
||||
import it.integry.ems_model.service.SetupGest;
|
||||
import it.integry.ems_model.types.OperationType;
|
||||
import it.integry.ems_model.utility.UtilityDB;
|
||||
import it.integry.ems_model.utility.UtilityString;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.exolab.castor.types.DateTime;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.io.StringWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Scope("request")
|
||||
public class SEPAService {
|
||||
protected final static Logger logger = LogManager.getLogger();
|
||||
@Autowired
|
||||
private MultiDBTransactionManager multiDBTransactionManager;
|
||||
@Autowired
|
||||
private SetupGest setupGest;
|
||||
@Autowired
|
||||
private EntityProcessor entityProcessor;
|
||||
@Autowired
|
||||
private MapService mapService;
|
||||
|
||||
public ServiceRestResponse getSepaXml(Date dataDist, Integer rifDist) throws Exception {
|
||||
ServiceRestResponse resp = new ServiceRestResponse();
|
||||
String lastVersion = UtilityString.emptyStr2Null(setupGest.getSetup("SEPA", "BONIFICI", "LAST_VERSION"));
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date data = df.parse(lastVersion);
|
||||
if (lastVersion != null && new Date().after(df.parse(lastVersion))){
|
||||
resp = getSepaXmlLastVersion(dataDist, rifDist);
|
||||
} else {
|
||||
resp = getSepaXmlOld(dataDist, rifDist);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
private ServiceRestResponse getSepaXmlOld(Date dataDist, Integer rifDist) throws Exception {
|
||||
|
||||
String codCuc = null, ragSoc = null, partIva = null;
|
||||
|
||||
String sqlCuc = "select cod_cuc, nome_ditta, part_iva from azienda";
|
||||
PreparedStatement ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlCuc);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
codCuc = rs.getString("cod_cuc");
|
||||
ragSoc = rs.getString("nome_ditta");
|
||||
partIva = rs.getString("part_iva");
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (UtilityString.isNullOrEmpty(codCuc))
|
||||
throw new Exception("codice CUC non impostato.");
|
||||
|
||||
//CBIBdyPaymentRequest000400 root = new CBIBdyPaymentRequest000400();
|
||||
|
||||
String ibanOrdinante = null, codAbi = null, codBancAzi = "";
|
||||
String datiDebitore = "select cod_iban, cod_abi, cod_banc_azi from vtb_dist, gtb_banc_azi"
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " and vtb_dist.cod_banc=gtb_banc_azi.cod_banc";
|
||||
PreparedStatement psDeb = multiDBTransactionManager.getPrimaryConnection().prepareStatement(datiDebitore);
|
||||
ResultSet rsDeb = psDeb.executeQuery();
|
||||
if (rsDeb.next()) {
|
||||
ibanOrdinante = rsDeb.getString("cod_iban");
|
||||
codAbi = rsDeb.getString("cod_abi");
|
||||
codBancAzi = rsDeb.getString("cod_banc_azi");
|
||||
}
|
||||
rsDeb.close();
|
||||
psDeb.close();
|
||||
if (UtilityString.isNullOrEmpty(ibanOrdinante) || UtilityString.isNullOrEmpty(codAbi))
|
||||
throw new Exception("IBAN o COD ABI non trovato per banca " + codBancAzi);
|
||||
|
||||
String sqlBonifici =
|
||||
"SELECT sum(imp_scad) tot_bonifico, " +
|
||||
"gtb_anag.rag_soc as rag_soc_benef, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc) as piva_cf_benef, " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END as bic_benef, " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END as abi_benef, " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) as iban_benef," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " from vtb_riba inner join atb_forn on vtb_riba.cod_anag = atb_forn.cod_anag "
|
||||
+ " INNER JOIN ctb_part ON ctb_part.tipo_anag = vtb_riba.tipo_anag AND " +
|
||||
"ctb_part.cod_anag = vtb_riba.cod_anag AND " +
|
||||
"ctb_part.data_doc = vtb_riba.data_doc AND " +
|
||||
"ctb_part.ser_doc = vtb_riba.ser_doc AND " +
|
||||
"ctb_part.num_doc = vtb_riba.num_doc "
|
||||
+ " inner join gtb_anag on atb_forn.cod_anag = gtb_anag.cod_anag, "
|
||||
+ "azienda "
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " group by gtb_anag.rag_soc, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc), " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END , " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END , " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) ," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " order by data_valuta";
|
||||
ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlBonifici);
|
||||
rs = ps.executeQuery();
|
||||
|
||||
Date dataValutaOld = null;
|
||||
Integer i = 0;
|
||||
BigDecimal somma = BigDecimal.ZERO;
|
||||
Boolean bonificoEstero = false;
|
||||
|
||||
String msgId = "DistintaXml-" + new SimpleDateFormat("ddMMyyyy-HH.mm.ss").format(new Date());
|
||||
CBIPaymentRequest000400 payment = setBasicPaymentOld(msgId, ragSoc, codCuc);
|
||||
|
||||
List<CBIPaymentRequest000400> paymList = new ArrayList<CBIPaymentRequest000400>();
|
||||
while (rs.next()) {
|
||||
|
||||
if (dataValutaOld == null) {
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
if (dataValutaOld.compareTo(rs.getDate("data_valuta")) != 0) {
|
||||
//sommatoria
|
||||
payment.getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
payment.getGrpHdr().setNbOfTxs(i.toString());
|
||||
i = 0;
|
||||
somma = BigDecimal.ZERO;
|
||||
|
||||
bonificoEstero = false;
|
||||
/*
|
||||
CBIEnvelPaymentRequest000400 envelope = new CBIEnvelPaymentRequest000400();
|
||||
envelope.setCBIPaymentRequest(payment);
|
||||
root.getCBIEnvelPaymentRequest().add(envelope);
|
||||
*/
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
payment = setBasicPaymentOld(msgId, ragSoc, codCuc);
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
GregorianCalendar dataValGreg = new GregorianCalendar();
|
||||
dataValGreg.setTime(rs.getDate("data_valuta"));
|
||||
XMLGregorianCalendar dataExec = DatatypeFactory.newInstance().newXMLGregorianCalendar(dataValGreg);
|
||||
payment.getPmtInf().setReqdExctnDt(dataExec);
|
||||
|
||||
BigDecimal importo = rs.getBigDecimal("tot_bonifico").setScale(2);
|
||||
somma = somma.add(importo);
|
||||
i++;
|
||||
|
||||
CBIPaymentTypeInformation1 paymType = new CBIPaymentTypeInformation1();
|
||||
|
||||
paymType.setInstrPrty(Priority2Code.NORM);
|
||||
CBIServiceLevel1 cbiServiceLevel1 = new CBIServiceLevel1();
|
||||
cbiServiceLevel1.setCd(CBIServiceLevel1Code.SEPA);
|
||||
paymType.setSvcLvl(cbiServiceLevel1);
|
||||
payment.getPmtInf().setPmtTpInf(paymType);
|
||||
|
||||
CBIPartyIdentification4 ordinantePayData = new CBIPartyIdentification4();
|
||||
ordinantePayData.setNm(ragSoc);
|
||||
CBIIdType2 ordinanteIdData = new CBIIdType2();
|
||||
CBIOrganisationIdentification3 ordinanteOrgData = new CBIOrganisationIdentification3();
|
||||
CBIGenericIdentification1 ordinanteGenericIdData = new CBIGenericIdentification1();
|
||||
ordinanteGenericIdData.setId(partIva);
|
||||
ordinanteGenericIdData.setIssr("ADE");
|
||||
ordinanteOrgData.setOthr(ordinanteGenericIdData);
|
||||
ordinanteIdData.setOrgId(ordinanteOrgData);
|
||||
ordinantePayData.setId(ordinanteIdData);
|
||||
payment.getPmtInf().setDbtr(ordinantePayData);
|
||||
|
||||
CBICashAccount1 ordinanteAccount = new CBICashAccount1();
|
||||
CBIAccountIdentification1 ordinanteAccountId = new CBIAccountIdentification1();
|
||||
ordinanteAccountId.setIBAN(ibanOrdinante);
|
||||
ordinanteAccount.setId(ordinanteAccountId);
|
||||
payment.getPmtInf().setDbtrAcct(ordinanteAccount);
|
||||
|
||||
CBIBranchAndFinancialInstitutionIdentification2 cbiBranch = new CBIBranchAndFinancialInstitutionIdentification2();
|
||||
CBIFinancialInstitutionIdentification3 cbiFin = new CBIFinancialInstitutionIdentification3();
|
||||
CBIClearingSystemMemberIdentification1 ordinanteMemberId = new CBIClearingSystemMemberIdentification1();
|
||||
|
||||
//abi
|
||||
ordinanteMemberId.setMmbId(codAbi);
|
||||
cbiFin.setClrSysMmbId(ordinanteMemberId);
|
||||
cbiBranch.setFinInstnId(cbiFin);
|
||||
payment.getPmtInf().setDbtrAgt(cbiBranch);
|
||||
|
||||
CBICreditTransferTransactionInformation creditTransaction = new CBICreditTransferTransactionInformation();
|
||||
PaymentIdentification1 payTrasactId = new PaymentIdentification1();
|
||||
payTrasactId.setInstrId(i.toString());
|
||||
payTrasactId.setEndToEndId(msgId + "-" + UtilityString.leftPad(i.toString(), 4, '0'));
|
||||
creditTransaction.setPmtId(payTrasactId);
|
||||
|
||||
CBIPaymentTypeInformation2 paymentTypeTransact = new CBIPaymentTypeInformation2();
|
||||
|
||||
CategoryPurpose1Choice category = new CategoryPurpose1Choice();
|
||||
category.setCd("SUPP");
|
||||
paymentTypeTransact.setCtgyPurp(category);
|
||||
creditTransaction.setPmtTpInf(paymentTypeTransact);
|
||||
CBIAmountType1 amountCurrency = new CBIAmountType1();
|
||||
ActiveOrHistoricCurrencyAndAmount currency = new ActiveOrHistoricCurrencyAndAmount();
|
||||
currency.setCcy("EUR");
|
||||
currency.setValue(importo);
|
||||
amountCurrency.setInstdAmt(currency);
|
||||
creditTransaction.setAmt(amountCurrency);
|
||||
|
||||
if (UtilityString.isNullOrEmpty(rs.getString("iban_benef")))
|
||||
throw new Exception("Iban non presente per " + rs.getString("rag_soc_benef"));
|
||||
|
||||
//beneficiario
|
||||
CBIPartyIdentification3 benficiarioPayData = new CBIPartyIdentification3();
|
||||
|
||||
String ibanBeneficiario = rs.getString("iban_benef").replaceAll(" ", "");
|
||||
|
||||
if (!ibanBeneficiario.startsWith("IT"))
|
||||
bonificoEstero = true;
|
||||
|
||||
if (bonificoEstero && !UtilityString.isNullOrEmpty(rs.getString("bic_benef"))) {
|
||||
CBIBranchAndFinancialInstitutionIdentification3 benefBranch = new CBIBranchAndFinancialInstitutionIdentification3();
|
||||
CBIFinancialInstitutionIdentification2 benefFin = new CBIFinancialInstitutionIdentification2();
|
||||
benefFin.setBIC(rs.getString("bic_benef"));
|
||||
benefBranch.setFinInstnId(benefFin);
|
||||
creditTransaction.setCdtrAgt(benefBranch);
|
||||
}
|
||||
|
||||
benficiarioPayData.setNm(rs.getString("rag_soc_benef"));
|
||||
|
||||
CBIPostalAddress6 addressInfo = new CBIPostalAddress6();
|
||||
//dati indirizzo
|
||||
addressInfo.setAdrTp(AddressType2Code.ADDR);
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("indirizzo")))
|
||||
addressInfo.setStrtNm(rs.getString("indirizzo"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("cap")))
|
||||
addressInfo.setPstCd(rs.getString("cap"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("citta")))
|
||||
addressInfo.setTwnNm(rs.getString("citta"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("prov")))
|
||||
addressInfo.setCtrySubDvsn(rs.getString("prov"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("nazione")))
|
||||
addressInfo.setCtry(rs.getString("nazione"));
|
||||
|
||||
benficiarioPayData.setPstlAdr(addressInfo);
|
||||
|
||||
CBIParty1Choice benefIdData = new CBIParty1Choice();
|
||||
CBIOrganisationIdentification2 benefOrgData = new CBIOrganisationIdentification2();
|
||||
CBIGenericIdentification1 benefGenericIdData = new CBIGenericIdentification1();
|
||||
benefGenericIdData.setId(rs.getString("piva_cf_benef"));
|
||||
benefGenericIdData.setIssr("ADE");
|
||||
benefOrgData.setOthr(benefGenericIdData);
|
||||
benefIdData.setOrgId(benefOrgData);
|
||||
benficiarioPayData.setId(benefIdData);
|
||||
|
||||
|
||||
creditTransaction.setCdtr(benficiarioPayData);
|
||||
|
||||
CBICashAccount2 beneficiarioAccount = new CBICashAccount2();
|
||||
CBIAccountIdentification1 beneficiarioAccountId = new CBIAccountIdentification1();
|
||||
|
||||
beneficiarioAccountId.setIBAN(ibanBeneficiario);
|
||||
beneficiarioAccount.setId(beneficiarioAccountId);
|
||||
creditTransaction.setCdtrAcct(beneficiarioAccount);
|
||||
|
||||
if (bonificoEstero) {
|
||||
CBIRegulatoryReporting1 cbiReport = new CBIRegulatoryReporting1();
|
||||
cbiReport.setDbtCdtRptgInd(CBIRegulatoryReportingType1Code.DEBT);
|
||||
}
|
||||
|
||||
RemittanceInformation5 causale = new RemittanceInformation5();
|
||||
causale.getUstrd().add("PAGAMENTO " + rs.getString("rif_debito").trim());
|
||||
creditTransaction.setRmtInf(causale);
|
||||
|
||||
payment.getPmtInf().getCdtTrfTxInf().add(creditTransaction);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (paymList.size() == 1) {
|
||||
//sommatoria
|
||||
paymList.get(0).getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
paymList.get(0).getGrpHdr().setNbOfTxs(i.toString());
|
||||
}
|
||||
|
||||
List<Object> genericList = new ArrayList<>();
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(CBIPaymentRequest000400.class);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
||||
|
||||
for (CBIPaymentRequest000400 paym : paymList) {
|
||||
StringWriter writer = new StringWriter();
|
||||
jaxbMarshaller.marshal(paym, writer);
|
||||
|
||||
FileItem fileItem = new FileItem("sepa-" + dateFormat.format(paym.getPmtInf().getReqdExctnDt().toGregorianCalendar().getTime()) + ".xml", writer.toString(), "xml");
|
||||
genericList.add(fileItem);
|
||||
}
|
||||
ServiceRestResponse resp = new ServiceRestResponse(EsitoType.OK, genericList);
|
||||
|
||||
updateDataExport(dataDist, rifDist);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
public ServiceRestResponse getSepaXmlLastVersion(Date dataDist, Integer rifDist) throws Exception {
|
||||
|
||||
String codCuc = null, ragSoc = null, partIva = null;
|
||||
|
||||
String sqlCuc = "select cod_cuc, nome_ditta, part_iva from azienda";
|
||||
PreparedStatement ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlCuc);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
codCuc = rs.getString("cod_cuc");
|
||||
ragSoc = rs.getString("nome_ditta");
|
||||
partIva = rs.getString("part_iva");
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (UtilityString.isNullOrEmpty(codCuc))
|
||||
throw new Exception("codice CUC non impostato.");
|
||||
|
||||
//CBIBdyPaymentRequest000401 root = new CBIBdyPaymentRequest000401();
|
||||
|
||||
String ibanOrdinante = null, codAbi = null, codBancAzi = "";
|
||||
String datiDebitore = "select cod_iban, cod_abi, cod_banc_azi from vtb_dist, gtb_banc_azi"
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " and vtb_dist.cod_banc=gtb_banc_azi.cod_banc";
|
||||
PreparedStatement psDeb = multiDBTransactionManager.getPrimaryConnection().prepareStatement(datiDebitore);
|
||||
ResultSet rsDeb = psDeb.executeQuery();
|
||||
if (rsDeb.next()) {
|
||||
ibanOrdinante = rsDeb.getString("cod_iban");
|
||||
codAbi = rsDeb.getString("cod_abi");
|
||||
codBancAzi = rsDeb.getString("cod_banc_azi");
|
||||
}
|
||||
rsDeb.close();
|
||||
psDeb.close();
|
||||
if (UtilityString.isNullOrEmpty(ibanOrdinante) || UtilityString.isNullOrEmpty(codAbi))
|
||||
throw new Exception("IBAN o COD ABI non trovato per banca " + codBancAzi);
|
||||
|
||||
String sqlBonifici =
|
||||
"SELECT sum(imp_scad) tot_bonifico, " +
|
||||
"gtb_anag.rag_soc as rag_soc_benef, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc) as piva_cf_benef, " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END as bic_benef, " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END as abi_benef, " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) as iban_benef," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " from vtb_riba inner join atb_forn on vtb_riba.cod_anag = atb_forn.cod_anag "
|
||||
+ " INNER JOIN ctb_part ON ctb_part.tipo_anag = vtb_riba.tipo_anag AND " +
|
||||
"ctb_part.cod_anag = vtb_riba.cod_anag AND " +
|
||||
"ctb_part.data_doc = vtb_riba.data_doc AND " +
|
||||
"ctb_part.ser_doc = vtb_riba.ser_doc AND " +
|
||||
"ctb_part.num_doc = vtb_riba.num_doc "
|
||||
+ " inner join gtb_anag on atb_forn.cod_anag = gtb_anag.cod_anag, "
|
||||
+ "azienda "
|
||||
+ " where data_dist = " + UtilityDB.valueDateToString(dataDist, CommonConstants.DATE_FORMAT_YMD) + " and rif_dist=" + rifDist
|
||||
+ " group by gtb_anag.rag_soc, " +
|
||||
"isnull(gtb_anag.part_iva, gtb_anag.cod_fisc), " +
|
||||
"gtb_anag.indirizzo, " +
|
||||
"gtb_anag.cap, " +
|
||||
"gtb_anag.prov, " +
|
||||
"gtb_anag.citta, " +
|
||||
"gtb_anag.nazione, " +
|
||||
"CASE WHEN azienda.nazione = gtb_anag.nazione THEN null ELSE atb_forn.cod_bic END , " +
|
||||
"CASE WHEN ctb_part.iban is not null THEN Substring(ctb_part.iban, 6,5) ELSE atb_forn.cod_abi END , " +
|
||||
"IsNull(ctb_part.iban, atb_forn.iban) ," +
|
||||
"data_scad, " +
|
||||
"rif_debito, " +
|
||||
"id_gruppo, " +
|
||||
"vtb_riba.cod_divi_cont, " +
|
||||
"vtb_riba.cambio_divi_cont, " +
|
||||
"data_valuta "
|
||||
+ " order by data_valuta";
|
||||
ps = multiDBTransactionManager.getPrimaryConnection().prepareStatement(sqlBonifici);
|
||||
rs = ps.executeQuery();
|
||||
|
||||
Date dataValutaOld = null;
|
||||
Integer i = 0;
|
||||
BigDecimal somma = BigDecimal.ZERO;
|
||||
Boolean bonificoEstero = false;
|
||||
|
||||
String msgId = "DistintaXml-" + new SimpleDateFormat("ddMMyyyy-HH.mm.ss").format(new Date());
|
||||
CBIPaymentRequest000401 payment = setBasicPayment(msgId, ragSoc, codCuc);
|
||||
|
||||
List<CBIPaymentRequest000401> paymList = new ArrayList<CBIPaymentRequest000401>();
|
||||
while (rs.next()) {
|
||||
|
||||
if (dataValutaOld == null) {
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
if (dataValutaOld.compareTo(rs.getDate("data_valuta")) != 0) {
|
||||
//sommatoria
|
||||
payment.getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
payment.getGrpHdr().setNbOfTxs(i.toString());
|
||||
i = 0;
|
||||
somma = BigDecimal.ZERO;
|
||||
|
||||
bonificoEstero = false;
|
||||
dataValutaOld = rs.getDate("data_valuta");
|
||||
payment = setBasicPayment(msgId, ragSoc, codCuc);
|
||||
paymList.add(payment);
|
||||
}
|
||||
|
||||
GregorianCalendar dataValGreg = new GregorianCalendar();
|
||||
dataValGreg.setTime(rs.getDate("data_valuta"));
|
||||
XMLGregorianCalendar dataExec = DatatypeFactory.newInstance().newXMLGregorianCalendar(dataValGreg);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentInstructionInformation pmtInf = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentInstructionInformation();
|
||||
DateAndDateTime2Choice dateAndDateTime2Choice = new DateAndDateTime2Choice();
|
||||
dateAndDateTime2Choice.setDt(dataExec);
|
||||
payment.getPmtInf().setReqdExctnDt(dateAndDateTime2Choice);
|
||||
|
||||
|
||||
BigDecimal importo = rs.getBigDecimal("tot_bonifico").setScale(2);
|
||||
somma = somma.add(importo);
|
||||
i++;
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation1 paymType = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation1();
|
||||
|
||||
paymType.setInstrPrty(it.integry.ems.contabil.sepa.dto.v00_04_01.Priority2Code.NORM);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIServiceLevel1 cbiServiceLevel1 = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIServiceLevel1();
|
||||
cbiServiceLevel1.setCd(it.integry.ems.contabil.sepa.dto.v00_04_01.CBIServiceLevel1Code.SEPA);
|
||||
paymType.setSvcLvl(cbiServiceLevel1);
|
||||
payment.getPmtInf().setPmtTpInf(paymType);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification4 ordinantePayData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification4();
|
||||
ordinantePayData.setNm(ragSoc);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType2 ordinanteIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType2();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification3 ordinanteOrgData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 ordinanteGenericIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
ordinanteGenericIdData.setId(partIva);
|
||||
ordinanteGenericIdData.setIssr("ADE");
|
||||
ordinanteOrgData.setOthr(ordinanteGenericIdData);
|
||||
|
||||
ordinanteIdData.setOrgId(ordinanteOrgData);
|
||||
ordinantePayData.setId(ordinanteIdData);
|
||||
payment.getPmtInf().setDbtr(ordinantePayData);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBICashAccount2 ordinanteAccount = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBICashAccount2();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAccountIdentification1 ordinanteAccountId = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAccountIdentification1();
|
||||
ordinanteAccountId.setIBAN(ibanOrdinante);
|
||||
ordinanteAccount.setId(ordinanteAccountId);
|
||||
payment.getPmtInf().setDbtrAcct(ordinanteAccount);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification2 cbiBranch = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification2();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIFinancialInstitutionIdentification3 cbiFin = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIFinancialInstitutionIdentification3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIClearingSystemMemberIdentification1 ordinanteMemberId = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIClearingSystemMemberIdentification1();
|
||||
|
||||
//abi
|
||||
ordinanteMemberId.setMmbId(codAbi);
|
||||
cbiFin.setClrSysMmbId(ordinanteMemberId);
|
||||
cbiBranch.setFinInstnId(cbiFin);
|
||||
payment.getPmtInf().setDbtrAgt(cbiBranch);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBICreditTransferTransactionInformation creditTransaction = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBICreditTransferTransactionInformation();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.PaymentIdentification1 payTrasactId = new it.integry.ems.contabil.sepa.dto.v00_04_01.PaymentIdentification1();
|
||||
payTrasactId.setInstrId(i.toString());
|
||||
payTrasactId.setEndToEndId(msgId + "-" + UtilityString.leftPad(i.toString(), 4, '0'));
|
||||
creditTransaction.setPmtId(payTrasactId);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation2 paymentTypeTransact = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentTypeInformation2();
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CategoryPurpose1Choice category = new it.integry.ems.contabil.sepa.dto.v00_04_01.CategoryPurpose1Choice();
|
||||
category.setCd("SUPP");
|
||||
paymentTypeTransact.setCtgyPurp(category);
|
||||
creditTransaction.setPmtTpInf(paymentTypeTransact);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAmountType1 amountCurrency = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAmountType1();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.ActiveOrHistoricCurrencyAndAmount currency = new it.integry.ems.contabil.sepa.dto.v00_04_01.ActiveOrHistoricCurrencyAndAmount();
|
||||
currency.setCcy("EUR");
|
||||
currency.setValue(importo);
|
||||
amountCurrency.setInstdAmt(currency);
|
||||
creditTransaction.setAmt(amountCurrency);
|
||||
|
||||
if (UtilityString.isNullOrEmpty(rs.getString("iban_benef")))
|
||||
throw new Exception("Iban non presente per " + rs.getString("rag_soc_benef"));
|
||||
|
||||
//beneficiario
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification3 benficiarioPayData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification3();
|
||||
|
||||
String ibanBeneficiario = rs.getString("iban_benef").replaceAll(" ", "");
|
||||
|
||||
if (!ibanBeneficiario.startsWith("IT"))
|
||||
bonificoEstero = true;
|
||||
|
||||
if (bonificoEstero && !UtilityString.isNullOrEmpty(rs.getString("bic_benef"))) {
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification3 benefBranch = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIBranchAndFinancialInstitutionIdentification3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIFinancialInstitutionIdentification2 benefFin = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIFinancialInstitutionIdentification2();
|
||||
benefFin.setBICFI(rs.getString("bic_benef"));
|
||||
benefBranch.setFinInstnId(benefFin);
|
||||
creditTransaction.setCdtrAgt(benefBranch);
|
||||
}
|
||||
|
||||
benficiarioPayData.setNm(rs.getString("rag_soc_benef"));
|
||||
|
||||
CBIPostalAddress24 addressInfo = new CBIPostalAddress24();
|
||||
//dati indirizzo
|
||||
AddressType3Choice addressType3Choice = new AddressType3Choice();
|
||||
addressType3Choice.setCd(it.integry.ems.contabil.sepa.dto.v00_04_01.AddressType2Code.ADDR);
|
||||
addressInfo.setAdrTp(addressType3Choice);
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("indirizzo")))
|
||||
addressInfo.setStrtNm(rs.getString("indirizzo"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("cap")))
|
||||
addressInfo.setPstCd(rs.getString("cap"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("citta")))
|
||||
addressInfo.setTwnNm(rs.getString("citta"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("prov")))
|
||||
addressInfo.setCtrySubDvsn(rs.getString("prov"));
|
||||
|
||||
if (!UtilityString.isNullOrEmpty(rs.getString("nazione")))
|
||||
addressInfo.setCtry(rs.getString("nazione"));
|
||||
|
||||
benficiarioPayData.setPstlAdr(addressInfo);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIParty1Choice benefIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIParty1Choice();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification2 benefOrgData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification2();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 benefGenericIdData = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
benefGenericIdData.setId(rs.getString("piva_cf_benef"));
|
||||
benefGenericIdData.setIssr("ADE");
|
||||
benefOrgData.setOthr(benefGenericIdData);
|
||||
benefIdData.setOrgId(benefOrgData);
|
||||
benficiarioPayData.setId(benefIdData);
|
||||
|
||||
|
||||
creditTransaction.setCdtr(benficiarioPayData);
|
||||
|
||||
CBICashAccount3 beneficiarioAccount = new CBICashAccount3();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAccountIdentification1 beneficiarioAccountId = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIAccountIdentification1();
|
||||
|
||||
beneficiarioAccountId.setIBAN(ibanBeneficiario);
|
||||
beneficiarioAccount.setId(beneficiarioAccountId);
|
||||
creditTransaction.setCdtrAcct(beneficiarioAccount);
|
||||
|
||||
if (bonificoEstero) {
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIRegulatoryReporting1 cbiReport = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIRegulatoryReporting1();
|
||||
cbiReport.setDbtCdtRptgInd(it.integry.ems.contabil.sepa.dto.v00_04_01.CBIRegulatoryReportingType1Code.DEBT);
|
||||
}
|
||||
|
||||
RemittanceInformation16 causale = new RemittanceInformation16();
|
||||
causale.getUstrd().add("PAGAMENTO " + rs.getString("rif_debito").trim());
|
||||
creditTransaction.setRmtInf(causale);
|
||||
|
||||
payment.getPmtInf().getCdtTrfTxInf().add(creditTransaction);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
if (paymList.size() == 1) {
|
||||
//sommatoria
|
||||
paymList.get(0).getGrpHdr().setCtrlSum(somma);
|
||||
// numero
|
||||
paymList.get(0).getGrpHdr().setNbOfTxs(i.toString());
|
||||
}
|
||||
|
||||
List<Object> genericList = new ArrayList<>();
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(CBIPaymentRequest000401.class);
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
||||
|
||||
for (CBIPaymentRequest000401 paym : paymList) {
|
||||
StringWriter writer = new StringWriter();
|
||||
jaxbMarshaller.marshal(paym, writer);
|
||||
|
||||
FileItem fileItem = new FileItem("sepa-" + dateFormat.format(paym.getPmtInf().getReqdExctnDt().getDt().toGregorianCalendar().getTime()) + ".xml", writer.toString(), "xml");
|
||||
genericList.add(fileItem);
|
||||
}
|
||||
ServiceRestResponse resp = new ServiceRestResponse(EsitoType.OK, genericList);
|
||||
|
||||
updateDataExport(dataDist, rifDist);
|
||||
|
||||
return resp;
|
||||
}
|
||||
private CBIPaymentRequest000401 setBasicPayment(String msgId, String ragSoc, String codSia) throws Exception {
|
||||
|
||||
CBIPaymentRequest000401 payment = new CBIPaymentRequest000401();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGroupHeader header = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGroupHeader();
|
||||
header.setMsgId(msgId);
|
||||
GregorianCalendar nowGreg = new GregorianCalendar();
|
||||
nowGreg.setTime(new Date());
|
||||
XMLGregorianCalendar now = DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGreg);
|
||||
header.setCreDtTm(now);
|
||||
payment.setGrpHdr(header);
|
||||
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification1 ordinante = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPartyIdentification1();
|
||||
ordinante.setNm(ragSoc);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification1 org = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIOrganisationIdentification1();
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1 genericIdOrdinante = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIGenericIdentification1();
|
||||
genericIdOrdinante.setId(codSia);
|
||||
genericIdOrdinante.setIssr("CBI");
|
||||
|
||||
org.getOthr().add(genericIdOrdinante);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType1 cbiType = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIIdType1();
|
||||
cbiType.setOrgId(org);
|
||||
ordinante.setId(cbiType);
|
||||
header.setInitgPty(ordinante);
|
||||
it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentInstructionInformation paymInfo = new it.integry.ems.contabil.sepa.dto.v00_04_01.CBIPaymentInstructionInformation();
|
||||
paymInfo.setChrgBr(it.integry.ems.contabil.sepa.dto.v00_04_01.CBIChargeBearerTypeCode.SLEV);
|
||||
paymInfo.setPmtInfId(msgId);
|
||||
paymInfo.setPmtMtd(it.integry.ems.contabil.sepa.dto.v00_04_01.PaymentMethod3Code.TRF);
|
||||
payment.setPmtInf(paymInfo);
|
||||
return payment;
|
||||
}
|
||||
private CBIPaymentRequest000400 setBasicPaymentOld(String msgId, String ragSoc, String codSia) throws Exception {
|
||||
|
||||
CBIPaymentRequest000400 payment = new CBIPaymentRequest000400();
|
||||
CBIGroupHeader header = new CBIGroupHeader();
|
||||
header.setMsgId(msgId);
|
||||
GregorianCalendar nowGreg = new GregorianCalendar();
|
||||
nowGreg.setTime(new Date());
|
||||
XMLGregorianCalendar now = DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGreg);
|
||||
header.setCreDtTm(now);
|
||||
payment.setGrpHdr(header);
|
||||
|
||||
CBIPartyIdentification1 ordinante = new CBIPartyIdentification1();
|
||||
ordinante.setNm(ragSoc);
|
||||
CBIOrganisationIdentification1 org = new CBIOrganisationIdentification1();
|
||||
CBIGenericIdentification1 genericIdOrdinante = new CBIGenericIdentification1();
|
||||
genericIdOrdinante.setId(codSia);
|
||||
genericIdOrdinante.setIssr("CBI");
|
||||
|
||||
org.getOthr().add(genericIdOrdinante);
|
||||
CBIIdType1 cbiType = new CBIIdType1();
|
||||
cbiType.setOrgId(org);
|
||||
ordinante.setId(cbiType);
|
||||
header.setInitgPty(ordinante);
|
||||
CBIPaymentInstructionInformation paymInfo = new CBIPaymentInstructionInformation();
|
||||
paymInfo.setChrgBr(CBIChargeBearerTypeCode.SLEV);
|
||||
paymInfo.setPmtInfId(msgId);
|
||||
paymInfo.setPmtMtd(PaymentMethod3Code.TRF);
|
||||
payment.setPmtInf(paymInfo);
|
||||
return payment;
|
||||
}
|
||||
|
||||
private void updateDataExport(Date dataDist, Integer rifDist) throws Exception {
|
||||
VtbDist vtbDist = new VtbDist()
|
||||
.setDataDist(dataDist)
|
||||
.setRifDist(rifDist)
|
||||
.setDataEsportazione(new Date());
|
||||
|
||||
vtbDist.setOperation(OperationType.UPDATE);
|
||||
entityProcessor.processEntity(vtbDist, false, multiDBTransactionManager);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user