public ResponseEntity setPayrollConfiguration()

in service/src/main/java/org/apache/fineract/cn/payroll/service/rest/PayrollConfigurationRestController.java [77:105]


  public ResponseEntity<Void> setPayrollConfiguration(@PathVariable(value = "identifier") final String customerIdentifier,
                                                     @RequestBody @Valid final PayrollConfiguration payrollConfiguration) {
    this.payrollConfigurationService.findCustomer(customerIdentifier)
        .orElseThrow(() -> ServiceException
            .notFound("Customer {0} not available.", customerIdentifier)
    );

    this.payrollConfigurationService.findAccount(payrollConfiguration.getMainAccountNumber())
        .orElseThrow(() -> ServiceException.notFound("Main account {0} not available.", payrollConfiguration.getMainAccountNumber()));

    if (payrollConfiguration.getPayrollAllocations() != null) {

      final List<PayrollAllocation> payrollAllocations = payrollConfiguration.getPayrollAllocations();

      if (payrollAllocations.stream().anyMatch(payrollAllocation ->
          payrollAllocation.getAccountNumber().equals(payrollConfiguration.getMainAccountNumber()))) {
        throw ServiceException.conflict("Main account should not be used in allocations.");
      }

      if (payrollAllocations.stream().anyMatch(payrollAllocation ->
          !this.payrollConfigurationService.findAccount(payrollAllocation.getAccountNumber()).isPresent())) {
        throw ServiceException.notFound("Certain allocated accounts not available.");
      }
    }

    this.commandGateway.process(new PutPayrollConfigurationCommand(customerIdentifier, payrollConfiguration));

    return ResponseEntity.accepted().build();
  }