|
|
|
|
@@ -69,13 +69,26 @@ public class CompanyDao {
|
|
|
|
|
@Autowired
|
|
|
|
|
private HttpServletRequest request;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private VatCheckDao vatCheckDao;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String NOT_FOUND_JSON = "{\"data\": \"not found\"}";
|
|
|
|
|
|
|
|
|
|
public CompanyResponse createCompany(UserEntity userEntity, CompanyRequest companyRequest) {
|
|
|
|
|
|
|
|
|
|
log.info("Initiating company creation by userId: {}", userEntity.getId());
|
|
|
|
|
CompanyEntity existingCompany = companyRepository.findByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
|
|
|
|
Boolean validVat=Boolean.FALSE;
|
|
|
|
|
if(companyRequest.getVatNumber()!=null){
|
|
|
|
|
VatCheckResponseBean vatCheckResponseBean=vatCheckDao.checkVatNumber(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
|
|
|
|
if(vatCheckResponseBean!=null && Boolean.TRUE.equals(vatCheckResponseBean.getValid())){
|
|
|
|
|
validVat=Boolean.TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CompanyEntity existingCompany = null;
|
|
|
|
|
if (!StringUtils.isEmpty(companyRequest.getVatNumber())) {
|
|
|
|
|
existingCompany = companyRepository.findByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
|
|
|
|
}
|
|
|
|
|
UserWithCompanyEntity userWithCompanyEntity = null;
|
|
|
|
|
if (existingCompany != null) {
|
|
|
|
|
UserWithCompanyEntity existingRelation = userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), existingCompany.getId()).orElse(null);
|
|
|
|
|
@@ -93,6 +106,7 @@ public class CompanyDao {
|
|
|
|
|
} else {
|
|
|
|
|
validateCompany(userEntity, companyRequest);
|
|
|
|
|
CompanyEntity companyEntity = convertCompanyRequestToCompanyEntity(userEntity, companyRequest);
|
|
|
|
|
companyEntity.setValidVat(validVat);
|
|
|
|
|
CompanyEntity companyData = companyRepository.save(companyEntity);
|
|
|
|
|
|
|
|
|
|
/** This code is responsible for adding a version history log for "creating company" operation. **/
|
|
|
|
|
@@ -112,12 +126,16 @@ public class CompanyDao {
|
|
|
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
|
|
|
Translator.toLocale(GepafinConstant.INVALID_EMAIL));
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isEmpty(companyRequest.getVatNumber())) {
|
|
|
|
|
// At least one identifier required
|
|
|
|
|
if (StringUtils.isEmpty(companyRequest.getVatNumber())
|
|
|
|
|
&& StringUtils.isEmpty(companyRequest.getCodiceFiscale())) {
|
|
|
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
|
|
|
Translator.toLocale(GepafinConstant.VATNUMBER_MANDATORY));
|
|
|
|
|
Translator.toLocale(GepafinConstant.VAT_OR_TAX_CODE_REQUIRED));
|
|
|
|
|
}
|
|
|
|
|
if (companyRepository.existsByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId())) {
|
|
|
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
|
|
|
// Only check VAT uniqueness if VAT provided
|
|
|
|
|
if (!StringUtils.isEmpty(companyRequest.getVatNumber())
|
|
|
|
|
&& companyRepository.existsByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId())) {
|
|
|
|
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
|
|
|
|
Translator.toLocale(GepafinConstant.VATNUMBER_ALREADY_EXISTS));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -140,7 +158,7 @@ public class CompanyDao {
|
|
|
|
|
UserWithCompanyEntity userWithCompany = userWithCompanyRepository.save(userWithCompanyEntity);
|
|
|
|
|
/** This code is responsible for adding a version history log for the "adding user with company" operation. **/
|
|
|
|
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.INSERT).oldData(null).newData(userWithCompany).build());
|
|
|
|
|
if (StringUtils.isEmpty(companyEntity.getJson())) {
|
|
|
|
|
if (StringUtils.isEmpty(companyEntity.getJson()) && companyRequest.getVatCheckResponse() != null) {
|
|
|
|
|
companyEntity.setJson(Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse()));
|
|
|
|
|
updateCodiceAtecoFieldWithNewJson(companyEntity);
|
|
|
|
|
companyEntity = companyRepository.save(companyEntity);
|
|
|
|
|
@@ -161,7 +179,11 @@ public class CompanyDao {
|
|
|
|
|
private CompanyEntity convertCompanyRequestToCompanyEntity(UserEntity userEntity, CompanyRequest request) {
|
|
|
|
|
CompanyEntity entity = new CompanyEntity();
|
|
|
|
|
entity.setCompanyName(request.getCompanyName());
|
|
|
|
|
entity.setVatNumber(request.getVatNumber());
|
|
|
|
|
if(request.getVatNumber()==null){
|
|
|
|
|
entity.setVatNumber(request.getCodiceFiscale());
|
|
|
|
|
}else {
|
|
|
|
|
entity.setVatNumber(request.getVatNumber());
|
|
|
|
|
}
|
|
|
|
|
entity.setCodiceFiscale(request.getCodiceFiscale());
|
|
|
|
|
entity.setAddress(request.getAddress());
|
|
|
|
|
entity.setPhoneNumber(request.getPhoneNumber());
|
|
|
|
|
@@ -182,7 +204,11 @@ public class CompanyDao {
|
|
|
|
|
CompanyResponse response = new CompanyResponse();
|
|
|
|
|
response.setId(entity.getId());
|
|
|
|
|
response.setCompanyName(entity.getCompanyName());
|
|
|
|
|
response.setVatNumber(entity.getVatNumber());
|
|
|
|
|
if(entity.getVatNumber()==null){
|
|
|
|
|
response.setVatNumber(entity.getCodiceFiscale());
|
|
|
|
|
}else {
|
|
|
|
|
response.setVatNumber(entity.getVatNumber());
|
|
|
|
|
}
|
|
|
|
|
response.setCodiceFiscale(entity.getCodiceFiscale());
|
|
|
|
|
response.setAddress(entity.getAddress());
|
|
|
|
|
response.setPhoneNumber(entity.getPhoneNumber());
|
|
|
|
|
@@ -231,6 +257,29 @@ public class CompanyDao {
|
|
|
|
|
// companyEntity.setVatNumber(companyRequest.getVatNumber());
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//allow adding VAT later
|
|
|
|
|
if(StringUtils.isNotBlank(companyRequest.getVatNumber())
|
|
|
|
|
&& StringUtils.isBlank(companyEntity.getVatNumber())) {
|
|
|
|
|
CompanyEntity existingCompany = companyRepository.findByVatNumberAndHubId(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
|
|
|
|
if(existingCompany!=null){
|
|
|
|
|
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.VATNUMBER_ALREADY_EXISTS));
|
|
|
|
|
}
|
|
|
|
|
Boolean validVat=Boolean.FALSE;
|
|
|
|
|
if(companyRequest.getVatNumber()!=null){
|
|
|
|
|
VatCheckResponseBean vatCheckResponseBean=vatCheckDao.checkVatNumber(companyRequest.getVatNumber(), userEntity.getHub().getId());
|
|
|
|
|
if(vatCheckResponseBean!=null && Boolean.TRUE.equals(vatCheckResponseBean.getValid())){
|
|
|
|
|
validVat=Boolean.TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
companyEntity.setVatNumber(companyRequest.getVatNumber());
|
|
|
|
|
companyEntity.setValidVat(validVat);
|
|
|
|
|
if(companyRequest.getVatCheckResponse() != null) {
|
|
|
|
|
String responseJson = Utils.convertMapIntoJsonString(companyRequest.getVatCheckResponse());
|
|
|
|
|
companyEntity.setJson(responseJson);
|
|
|
|
|
updateCodiceAtecoFieldWithNewJson(companyEntity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
companyRepository.save(companyEntity);
|
|
|
|
|
log.info("Company updated and saved. companyId: {}", companyEntity.getId());
|
|
|
|
|
|
|
|
|
|
|