java.lang.Object
es.kukenan.smartfi.microservice.smartficore.service.ContractService

@Service public class ContractService extends Object
Service to reply contracts invocations.
  • Field Details

    • applicationsFolderPath

      @Value("${applications.folder.path}") private String applicationsFolderPath
      Application files destination.
    • doUltraFaceValidation

      @Value("${ultraface.doValidation}") private boolean doUltraFaceValidation
      Do UltraFace validation flag.
    • doApcScoreValidation

      @Value("${apc.doValidation}") private boolean doApcScoreValidation
      Do APC Score validation flag.
    • ultrafacePath

      @Value("${ultraface.folder.path}") private String ultrafacePath
      Ultraface folder path.
    • OPEN_REQUEST_GROUP_NAME

      private static final String OPEN_REQUEST_GROUP_NAME
      Open request group name.
      See Also:
    • GRANTED_GROUP_NAME

      private static final String GRANTED_GROUP_NAME
      Granted group name.
      See Also:
    • CLOSED_GROUP_NAME

      private static final String CLOSED_GROUP_NAME
      Closed group name.
      See Also:
    • PENDING_COMMISSION_GROUP_NAME

      private static final String PENDING_COMMISSION_GROUP_NAME
      Pending Commission group name.
      See Also:
    • PRE_APPROVED_NAME

      private static final String PRE_APPROVED_NAME
      Document state pre approved name.
      See Also:
    • UNDER_REVIEW_NAME

      private static final String UNDER_REVIEW_NAME
      Document state under review.
      See Also:
    • PENDING_SIGNATURE_NAME

      private static final String PENDING_SIGNATURE_NAME
      Document state pending signature for credit.
      See Also:
    • REJECTED_NAME

      private static final String REJECTED_NAME
      Document state rejected name.
      See Also:
    • FINISHED_NAME

      private static final String FINISHED_NAME
      Document state finished name.
      See Also:
    • FINISHED_CLIENT_NAME

      private static final String FINISHED_CLIENT_NAME
      Document state finished client name.
      See Also:
    • WAITING_DOCUMENTS_NAME

      private static final String WAITING_DOCUMENTS_NAME
      Document state waiting documents name.
      See Also:
    • APPROVED_NAME

      private static final String APPROVED_NAME
      Document state approved name.
      See Also:
    • PAYMENT_PENDING_NAME

      private static final String PAYMENT_PENDING_NAME
      Document state payment pending name.
      See Also:
    • APPROVED_PAYMENT_PENDING_NAME

      private static final String APPROVED_PAYMENT_PENDING_NAME
      Document state approved payment pending name.
      See Also:
    • LATE_PAYMENT_NAME

      private static final String LATE_PAYMENT_NAME
      Document state late payment name.
      See Also:
    • COMPANY_AFFILIATION_NAME

      private static final String COMPANY_AFFILIATION_NAME
      Document state company affiliation name.
      See Also:
    • ACTIVE_NAME

      private static final String ACTIVE_NAME
      Document state active name.
      See Also:
    • COMMISSION_PAID_NAME

      private static final String COMMISSION_PAID_NAME
      Document state granted with commission name.
      See Also:
    • WAITING_CONTRACT_DOCUMENT_NAME

      private static final String WAITING_CONTRACT_DOCUMENT_NAME
      Document state waiting document submission name.
      See Also:
    • CONTRACT_REQUESTED_NAME

      private static final String CONTRACT_REQUESTED_NAME
      Contract requested file name.
      See Also:
    • INVOICE_REQUESTED_NAME

      private static final String INVOICE_REQUESTED_NAME
      Invoice requested file name.
      See Also:
    • ADDI_REQUESTED_NAME

      private static final String ADDI_REQUESTED_NAME
      Addi requested file name.
      See Also:
    • PROOF_OF_PAYMENT_REQUESTED_NAME

      private static final String PROOF_OF_PAYMENT_REQUESTED_NAME
      Proof of payment requested file name.
      See Also:
    • CREDIT_PRODUCT

      private static final String CREDIT_PRODUCT
      Credit constant.
      See Also:
    • MAIN_WALLET

      private static final int MAIN_WALLET
      Product main wallet.
      See Also:
    • ADVANCE_PRODUCT

      private static final String ADVANCE_PRODUCT
      Advance constant.
      See Also:
    • entity

      private static int entity
      TEMPORAL DATA FOR GENERATING CONTRACT(APPOINTMENT).
    • productCredit

      private static int productCredit
      Product CREDIT (Credito fijo).
    • productAdvance

      private static int productAdvance
      Product ADVANCE (Prestamo fijo).
    • APC_TYPE

      private static String APC_TYPE
      Apc type ("tipo") constant.
    • APC_PRODUCT

      private static String APC_PRODUCT
      Apc product ("producto") constant.
    • exampleMatcher

      private final org.springframework.data.domain.ExampleMatcher exampleMatcher
      Example matcher configuration.
    • contractRepository

      @Autowired private ContractRepository contractRepository
      ContractRepository contractRepository.
    • contractCacheOperation

      @Autowired private ContractCacheOperation contractCacheOperation
      ContractCacheOperation contractCacheOperation.
    • userRepository

      @Autowired private UserRepository userRepository
      Users repository.
    • creditProductService

      @Autowired private CreditProductService creditProductService
      Credit product service.
    • advanceProductService

      @Autowired private AdvanceProductService advanceProductService
      Advance product service.
    • userCompanyRepository

      @Autowired private UserCompanyRepository userCompanyRepository
      User Company repository.
    • entityRepository

      @Autowired private EntityRepository entityRepository
      Entity repository.
    • contractStateRepository

      @Autowired private ContractStateRepository contractStateRepository
      Contract State repository.
    • accountBankRepository

      @Autowired private AccountBankRepository accountBankRepository
      Bank Account repository.
    • contractDocumentRepository

      @Autowired private ContractDocumentRepository contractDocumentRepository
      Contract Document repository.
    • documentStateRepository

      @Autowired private DocumentStateRepository documentStateRepository
      Document State repository.
    • apcScoreRepository

      @Autowired private ApcScoreRepository apcScoreRepository
      ApcScore repository.
    • onboardingService

      @Autowired private OnboardingService onboardingService
      Onboarding service.
    • kafkaController

      @Autowired private KafkaController kafkaController
      Kafka Controller.
    • notificationCreatedEventMapper

      @Autowired private NotificationCreatedEventMapper notificationCreatedEventMapper
      Notification event mapper.
    • backofficeApiClient

      @Autowired private BackofficeApiClient backofficeApiClient
      Backoffice clients manager.
    • apcApiClient

      @Autowired private ApcApiClient apcApiClient
      Apc client manager.
    • companyRepository

      @Autowired private CompanyRepository companyRepository
      Company Repository.
    • ultraFaceApiClient

      @Autowired private UltraFaceApiClient ultraFaceApiClient
      UltraFace api client.
    • apiEFacturaPtyApiClient

      @Autowired private EFacturaPtyApiClient apiEFacturaPtyApiClient
      EFacturaPty api client.
    • requestContextManager

      @Autowired private es.kukenan.smartfi.common.context.RequestContextManager requestContextManager
      Request context manager.
    • auditService

      @Autowired private AuditService auditService
      Backoffice endpoints implementation.
    • userContractDocumentEventMapper

      @Autowired private UserContractDocumentEventMapper userContractDocumentEventMapper
      Contract Document event mapper.
    • userMongoRepository

      @Autowired private UserMongoRepository userMongoRepository
      User Mongo document repository.
    • paymentService

      @Autowired private PaymentService paymentService
      Payment service.
    • walletProductRepository

      @Autowired private WalletProductRepository walletProductRepository
      Wallet repository.
    • transfersUtils

      @Autowired private TransfersUtils transfersUtils
      Utilities for core transfers.
    • ruleService

      @Autowired private RuleService ruleService
      Rule service.
  • Constructor Details

    • ContractService

      public ContractService()
  • Method Details

    • getContractsCount

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.CountInfoDto>> getContractsCount(String userId, es.kukenan.smartfi.common.security.WalletAuthentication authentication)
      Retrieve contracts count.
      Parameters:
      userId - user id.
      authentication - user credentials.
      Returns:
      the count.
    • getCompanyContractCountAndTotal

      private void getCompanyContractCountAndTotal(List<String> contractStates, String product, Integer companyId, es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.CountInfoDto countInfoDto, String group)
      Get count with filter.
      Parameters:
      contractStates - states to filter count.
      product - product to count.
      companyId - company identifier.
      countInfoDto - count info dto to update.
      group - group to update.
    • getContractCountFiltered

      private List<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.BasicGraphInfoDto> getContractCountFiltered(List<String> contractStates, String product)
      Get count with filter.
      Parameters:
      contractStates - states to filter count.
      product - product to count.
      Returns:
      map response.
    • getContractCount

      private Long getContractCount(List<ContractState> contractStateList, User user, String sponsorId, String product, Integer companyId)
      Get count of Contracts by filter.
      Parameters:
      contractStateList - list of states to search.
      user - user entity.
      sponsorId - user sponsor identifier.
      product - product type.
      companyId - company identifier.
      Returns:
      contracts count value.
    • getContracts

      public es.kukenan.smartfi.common.api.dtos.ResultsPageDto<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract> getContracts(String userId, Integer pageNumber, Integer pageSize, String sortedBy, es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.GetApplicationsRequestDto getApplicationsRequestDto, es.kukenan.smartfi.common.security.WalletAuthentication authentication)
      Retrieve contracts.
      Parameters:
      userId - user id.
      pageNumber - number of page to get.
      pageSize - size of list to return.
      sortedBy - sort param.
      getApplicationsRequestDto - body for filtering.
      authentication - user credentials.
      Returns:
      the contracts.
    • getContractExample

      private Contract getContractExample(Integer contractStateId, User user, String type, String sponsorId, Company company, String contractNumber)
      Build contract example for query.
      Parameters:
      contractStateId - contract state identifier.
      user - User for search.
      type - contract type.
      sponsorId - Sponsor identifier.
      company - Company data.
      contractNumber - contract number.
      Returns:
      Contract example to search.
    • validFortnightDays

      private Boolean validFortnightDays(String product, Integer companyId)
      Check if can request this fortnight.
      Parameters:
      product - product type.
      companyId - company identifier.
      Returns:
      flag if can request.
    • validFortnightRequest

      private Boolean validFortnightRequest(String product, String userId)
      Check if user has active request this fortnight.
      Parameters:
      product - product type.
      userId - user identifier.
      Returns:
      flag if user has active request this fortnight.
    • validActiveRequest

      private Boolean validActiveRequest(String product, String userId, Integer advanceCount)
      Check if user has active request.
      Parameters:
      product - product type.
      userId - user identifier.
      advanceCount - company advance request count.
      Returns:
      flag if user has active request.
    • validNewContract

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<Boolean>> validNewContract(String userId, String type)
      Validation if user can create Contract.
      Parameters:
      userId - user identifier.
      type - contract type.
      Returns:
      the contract validation.
    • saveContract

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> saveContract(es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.NewApplicationRequestDto requestDto, String userIp, String userAgent)
      Create new application from Client/Sponsor.
      Parameters:
      requestDto - new application data.
      userIp - user real ip
      userAgent - user-agent header
      Returns:
      Contract created.
    • generateInvoiceFile

      public void generateInvoiceFile(Contract application, Company company)
      Create Invoice document with API.
      Parameters:
      application - application information.
      company - user company.
    • generateAddiFile

      private void generateAddiFile(Contract contract)
      Create Direct Discount Authorization file.
      Parameters:
      contract - application information.
    • getContractDocument

      private ContractDocument getContractDocument(Contract application, ContractDocument contractDocument, String contractDocumentId, DocumentState documentState, String savedFileName, String savedFilePath, es.kukenan.smartfi.microservice.backoffice.dtos.backoffice.requestedfiles.DataObject contractFile)
      Check contract document in contract documents list. Create if not exist or update if exist.
      Parameters:
      application - application entity.
      contractDocument - contract document created.
      contractDocumentId - contract document identifier.
      documentState - contract document state.
      savedFileName - contract document file name.
      savedFilePath - contract document file path.
      contractFile - contract file.
      Returns:
      contract document entity.
    • generalDataFromApplication

      private es.kukenan.smartfi.microservice.backoffice.dtos.core.invoice.request.InvoiceGeneralData generalDataFromApplication(Contract application)
      Create Invoice General Data from application.
      Parameters:
      application - application information.
      Returns:
      invoice general data object
    • generateItem

      private void generateItem(Contract application, List<es.kukenan.smartfi.microservice.backoffice.dtos.core.invoice.request.InvoiceItem> invoiceItems, Company company)
      Create an Invoice Item and Add to inoice items list.
      Parameters:
      application - application information.
      invoiceItems - list of items.
      company - user company.
    • totalDataFromApplication

      private es.kukenan.smartfi.microservice.backoffice.dtos.core.invoice.request.InvoiceTotalData totalDataFromApplication(Contract application)
      Create Invoice Total Data from application.
      Parameters:
      application - application information.
      Returns:
      invoice total data object
    • saveGeneratedProduct

      private void saveGeneratedProduct(User user, Contract application, String product, BigDecimal quota, int deadline, int deadlineUnit, LocalDate advancePaymentDay, es.kukenan.smartfi.microservice.onboarding.events.common.CompanyDto companyDto)
      Generate requested product.
      Parameters:
      user - user identification
      application - application created
      product - product type requested
      quota - application monthly quota
      deadline - application deadline
      deadlineUnit - application deadline unit
      advancePaymentDay - application calculated advance payment day
      companyDto - company information
    • calculatePaymentDate

      public LocalDateTime calculatePaymentDate(int addValue, int deadlineUnit)
      Calculate payment approximated date.
      Parameters:
      addValue - value to add actual date.
      deadlineUnit - unit to add to date.
      Returns:
      calculated date.
    • createContract

      public Contract createContract(String name, Entity entity, User user)
      Create contract for Entity and User.
      Parameters:
      name - contract name.
      entity - for contract.
      user - identifier.
      Returns:
      Contract.
    • addBodyDataToContract

      public Contract addBodyDataToContract(Contract contract, String product, Float amount, Integer deadline, Integer deadlineUnit, Integer userCode, String sponsor, Optional<AccountBank> accountBank, String initialStatus, String contractNumber)
      Add body data to contract.
      Parameters:
      contract - contract to add data.
      product - product type.
      amount - application amount.
      deadline - application deadline.
      deadlineUnit - application deadline unit.
      userCode - user code identifier.
      sponsor - sponsor user identifier.
      accountBank - account bank information.
      initialStatus - custom initial status.
      contractNumber - contract number.
      Returns:
      updated Contract.
    • getContractStates

      public List<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.ContractStateDto> getContractStates(String product)
      Retrieve contract states.
      Parameters:
      product - application product for filtering.
      Returns:
      the contract states.
    • findContract

      private Contract findContract(String contractId, String userId)
      Retrieve contract entity.
      Parameters:
      contractId - contract identifier.
      userId - user id.
      Returns:
      the contract entity.
    • getContractDetail

      public es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract getContractDetail(String userId, String contractId, String userToken)
      Retrieve contract detail.
      Parameters:
      userId - user id.
      contractId - contract identifier.
      userToken - user token.
      Returns:
      the contract detail.
    • uploadFile

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> uploadFile(String contractId, String userId, List<org.springframework.web.multipart.MultipartFile> files, Integer companyId)
      Save files to application.
      Parameters:
      contractId - application identifier.
      userId - user identifier.
      files - list of files received.
      companyId - company identifier.
      Returns:
      Contract updated.
    • uploadSpecificFile

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> uploadSpecificFile(String contractDocumentId, String userUpdateId, org.springframework.web.multipart.MultipartFile file, String userToken)
      Save specific contract document file.
      Parameters:
      contractDocumentId - contract document id.
      userUpdateId - user id.
      file - file received.
      userToken - user token.
      Returns:
      Contract updated.
    • uploadContractualFile

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> uploadContractualFile(String contractId, String userUpdateId, org.springframework.web.multipart.MultipartFile file, String process, String userToken)
      Save specific contractual document file.
      Parameters:
      contractId - contract id.
      userUpdateId - user id.
      file - file received.
      process - type of process called.
      userToken - user token.
      Returns:
      Contract updated.
    • saveFileAndUpdateContractDocument

      private void saveFileAndUpdateContractDocument(String userUpdateId, org.springframework.web.multipart.MultipartFile file, ContractDocument contractDocument, DocumentState documentState, String newContractState, Contract contract, List<es.kukenan.smartfi.microservice.backoffice.dtos.backoffice.requestedfiles.DataObject> requestedFiles, es.kukenan.smartfi.dto.BasicApiResponse result, es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract> responseDto, boolean fileNotFound, String userToken) throws IOException, ExecutionException, InterruptedException
      Throws:
      IOException
      ExecutionException
      InterruptedException
    • updateContractState

      private Contract updateContractState(String state, Contract contract)
      Update Contract state.
      Parameters:
      state - state tu update
      contract - contract to update
      Returns:
      updated contract.
    • saveFile

      public void saveFile(org.springframework.web.multipart.MultipartFile file, String destination, String userId, String contractId) throws IOException
      Save file to path.
      Parameters:
      file - file received.
      destination - file path destination.
      userId - user identifier.
      contractId - contract identifier.
      Throws:
      IOException - exception if destination fails.
    • checkFilePath

      private File checkFilePath(String userId, String contractId)
    • getApprovedContracts

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<List<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>>> getApprovedContracts(String userId)
      Retrieve approved contracts.
      Parameters:
      userId - user id.
      Returns:
      the contracts.
    • getContractsAndCompanyInfo

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.clients.dto.ContractCompanyInfoDto>> getContractsAndCompanyInfo(String userId)
      Retrieve approved contracts.
      Parameters:
      userId - user id.
      Returns:
      the contracts.
    • updateContractAppointment

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> updateContractAppointment(String contractId, String userId, es.kukenan.smartfi.microservice.backoffice.dtos.backoffice.appointments.AppointmentDto appointmentDto)
      Update contract with appointment.
      Parameters:
      contractId - contract id.
      userId - user id.
      appointmentDto - appointment information.
      Returns:
      ok.
    • updateContractStatus

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> updateContractStatus(String contractId, String userId, es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.ContractStatusRequestDto contractStatusRequestDto, String userToken)
      Change application status.
      Parameters:
      contractId - contract id.
      userId - user id.
      contractStatusRequestDto - list of files received.
      userToken - user token.
      Returns:
      ok.
    • updateContractDocumentStatus

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>> updateContractDocumentStatus(String contractDocumentId, String userId, es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.UpdateContractDocumentRequestDto requestDto, String userToken)
      Update contract document status.
      Parameters:
      contractDocumentId - contract document id.
      userId - user id.
      requestDto - Update contract document information.
      userToken - user token.
      Returns:
      ok.
    • getContractDocumentFile

      public void getContractDocumentFile(String contractDocumentId, String userId, javax.servlet.http.HttpServletResponse response) throws IOException
      Get contract document file.
      Parameters:
      contractDocumentId - contract document id.
      userId - user id.
      response - response.
      Throws:
      IOException - file to write response.
    • generateContractFile

      public void generateContractFile(Contract contract, String backofficeUserId, javax.servlet.http.HttpServletResponse response)
      Generate pdf contract document file.
      Parameters:
      contract - contract entity.
      backofficeUserId - backoffice user id.
      response - response.
    • calculateCostoOperativo

      public Float calculateCostoOperativo(Contract contract, Company company) throws ExecutionException, InterruptedException
      Calculate advance contract operative cost.
      Parameters:
      contract - Contract information.
      company - user company.
      Returns:
      Operative cost value.
      Throws:
      ExecutionException - Exception if config values endpoint fails.
      InterruptedException - Exception if config values endpoint fails.
    • calculateComission

      public Float calculateComission(Contract contract, Company company)
      Calculate advance contract comission.
      Parameters:
      contract - Contract information.
      company - user company.
      Returns:
      Comission value.
    • translateDocumentType

      private String translateDocumentType(String document)
      Transform document type name on DB to spanish.
      Parameters:
      document - document name on DB.
      Returns:
      Document name translated.
    • createContractDocument

      private ContractDocument createContractDocument(String contractDocumentId, Contract contract, DocumentState documentState, String savedFileName, String savedFilePath, Long documentId, int contractDocumentType)
    • changeApplicationsToPayed

      public org.springframework.http.ResponseEntity<es.kukenan.smartfi.dto.ResponseDTO<List<es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.Contract>>> changeApplicationsToPayed(es.kukenan.smartfi.microservice.backoffice.dtos.core.contract.UpdateListOfContractsRequestDto updateListOfContractsRequestDto, String userId, String product)
      Change a list of application identifier from PAYMENT_PENDING (credit) or APPROVED_PAYMENT_PENDING_NAME (advance) to PAYED (advance) or ACTIVE (credit).
      Parameters:
      updateListOfContractsRequestDto - contract id list.
      userId - user id.
      product - application type.
      Returns:
      ok.