Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rms.domain.sales.Installment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -16,18 +17,18 @@ public interface InstallmentRepository extends JpaRepository<Installment, Intege

@Query("select i from Installment i where ( i.dueDate BETWEEN :startDate AND :endDate ) OR " +
"( i.paymentDate IS NULL AND i.dueDate <=:startDate ) Order by i.dueDate")
List<Installment> getDueInstallments(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
Page<Installment> findByDueDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate, Pageable pageable);

@Query("select i from Installment i JOIN i.order o JOIN o.customer c " +
"WHERE c.id=:customerId Order by i.dueDate")
List<Installment> getByCustomerId(@Param("customerId") Integer customerId);
List<Installment> findByCustomerId(@Param("customerId") Integer customerId);


@Query(value = "select i from Installment i join i.order o join o.customer c where c.nationalId = :nationalId")
Page<Installment> getByCustomerNationalId(@Param("nationalId") String nationalId, Pageable pageable);
Page<Installment> findByCustomerNationalId(@Param("nationalId") String nationalId, Pageable pageable);

@Query("select i from Installment i where i.order.id = :orderId")
List<Installment> findInstallmentsByOrderId(@Param("orderId") Integer orderId );
List<Installment> findByOrderId(@Param("orderId") Integer orderId);


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rms.rest.controller;

import com.rms.rest.dto.InstallmentDto;
import com.rms.rest.handler.InstallmentHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -14,11 +13,11 @@
@Tag(name = "Installment", description = "Rest Api For Installment")
public class InstallmentController {
private InstallmentHandler installmentHandler;

@GetMapping
@Operation(summary = "Get All", description = "this api for get all installment")
public ResponseEntity<?> getAll( @RequestParam(value = "page" , defaultValue = "0") Integer page ,
@RequestParam (value = "size" , defaultValue = "10") Integer size) {
return installmentHandler.getAll(page,size);
public ResponseEntity<?> getAll(@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
return installmentHandler.getAll(page, size);
}


Expand All @@ -28,6 +27,12 @@ public ResponseEntity<?> getById(@PathVariable("id") Integer id) {
return installmentHandler.getById(id);
}

@GetMapping("/due")
@Operation(summary = "Get By dueDate", description = "this api for get installments by dueDate")
public ResponseEntity<?> getDueInstallments( @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
return installmentHandler.getDueInstallments( page, size);
}


@DeleteMapping("/{id}")
@Operation(summary = "delete installment By Id")
Expand All @@ -38,10 +43,8 @@ public ResponseEntity<?> delete(@PathVariable Integer id) {

@GetMapping("/customer/{nationalId}")
@Operation(summary = "Get Installment By Customer National Id ", description = "this api for get installment by customer national id")
public ResponseEntity<?> getByCustomerNationalId(@PathVariable("nationalId") String nationalId ,
@RequestParam(value = "page" , defaultValue = "0") Integer page ,
@RequestParam (value = "size" , defaultValue = "10") Integer size) {
return installmentHandler.getByCustomerNationalId(nationalId,page,size);
public ResponseEntity<?> getByCustomerNationalId(@PathVariable("nationalId") String nationalId, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
return installmentHandler.getByCustomerNationalId(nationalId, page, size);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.rms.rest.handler;

import com.rms.domain.core.Employee;
import com.rms.domain.investor.Transaction;
import com.rms.domain.sales.Customer;
import com.rms.domain.sales.Installment;
import com.rms.rest.dto.CustomerDto;
Expand Down Expand Up @@ -41,7 +39,7 @@ public ResponseEntity<?> getById(Integer id) {
}

public ResponseEntity<?> getCustomerInstallments(Integer id) {
List<Installment> installments = installmentService.getCustomerInstallments(id);
List<Installment> installments = installmentService.getByCustomerInstallments(id);
List<InstallmentDto> installmentDtos = installmentMapper.toDto(installments);

return ResponseEntity.ok(installmentDtos);
Expand All @@ -68,27 +66,25 @@ public ResponseEntity<?> update(CustomerDto customerDto, Integer id) {
Optional<Customer> customerCodeExist = customerService.findCustomerCode(customerDto.getCustomerCode());
if (customerNationalIdExist.isPresent() && !customerNationalIdExist.get().getId().equals(id)) {
throw new ResourceAlreadyExistsException(Customer.class.getSimpleName(), "National Id", customerDto.getNationalId(), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
else if (customerCodeExist.isPresent() && !customerCodeExist.get().getId().equals(id)) {
} else if (customerCodeExist.isPresent() && !customerCodeExist.get().getId().equals(id)) {
throw new ResourceAlreadyExistsException(Customer.class.getSimpleName(), "Customer Code", customerDto.getCustomerCode(), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
Optional<Customer> exsitedTrustReceiptNo = customerService.getTrustReceiptNo(customerDto.getTrustReceiptNo());
if (exsitedTrustReceiptNo.isPresent() && !exsitedTrustReceiptNo.get().getId().equals(id)) {
throw new ResourceAlreadyExistsException(Customer.class.getSimpleName(),
"trustReceiptNo", Integer.toString(customerDto.getTrustReceiptNo()), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
mapper.updateEntityFromDto(customerDto, customer);
customerService.save(customer);
CustomerDto dto = mapper.toDto(customer);
return ResponseEntity.ok(dto);
Optional<Customer> exsitedTrustReceiptNo = customerService.getTrustReceiptNo(customerDto.getTrustReceiptNo());
if (exsitedTrustReceiptNo.isPresent() && !exsitedTrustReceiptNo.get().getId().equals(id)) {
throw new ResourceAlreadyExistsException(Customer.class.getSimpleName(),
"trustReceiptNo", Integer.toString(customerDto.getTrustReceiptNo()), ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
mapper.updateEntityFromDto(customerDto, customer);
customerService.save(customer);
CustomerDto dto = mapper.toDto(customer);
return ResponseEntity.ok(dto);

}



public ResponseEntity<?> delete (Integer id) {
Customer customer = customerService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Customer.class.getSimpleName(),id));
public ResponseEntity<?> delete(Integer id) {
Customer customer = customerService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Customer.class.getSimpleName(), id));
try {
customerService.delete(customer);
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.rms.rest.handler;

import com.rms.domain.sales.Customer;
import com.rms.domain.sales.Installment;
import com.rms.rest.dto.InstallmentDto;
Expand All @@ -11,12 +12,12 @@
import com.rms.rest.modelmapper.common.PaginationMapper;
import com.rms.service.CustomerService;
import com.rms.service.InstallmentService;
import com.rms.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
Expand All @@ -28,62 +29,64 @@ public class InstallmentHandler {
private InstallmentService installmentService;
private InstallmentMapper mapper;

private CustomerService customerService ;
private CustomerService customerService;

private PaginationMapper paginationMapper;

public ResponseEntity<?> getById(Integer id) {
Installment installment = installmentService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(),id));
Installment installment = installmentService.getById(id).orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(), id));
InstallmentDto dto = mapper.toDto(installment);
return ResponseEntity.ok(dto);
}

public ResponseEntity<?> getDueInstallments() {
public ResponseEntity<?> getDueInstallments(Integer page, Integer size) {
ZoneId defaultZoneId = ZoneId.systemDefault();
LocalDate endDate = LocalDate.now();
endDate = endDate.plusMonths(1);
List<Installment> installments = installmentService.getDueInstallments(new Date(), Date.from(endDate.atStartOfDay(defaultZoneId).toInstant()));
List<InstallmentDto> dtos = mapper.toDto(installments);
LocalDate monthBegin = LocalDate.now().withDayOfMonth(1);
LocalDate monthEnd = LocalDate.now().plusMonths(1).withDayOfMonth(1).minusDays(1);
Page<Installment> installments = installmentService.getByDueDate(Date.from(monthBegin.atStartOfDay(defaultZoneId).toInstant()), Date.from(monthEnd.atStartOfDay(defaultZoneId).toInstant()), page, size);
List<InstallmentDto> dtos = mapper.toDto(installments.getContent());
PaginatedResultDto<InstallmentDto> paginatedResult = new PaginatedResultDto<>();
paginatedResult.setData(dtos);
paginatedResult.setPagination(paginationMapper.toDto(installments));
return ResponseEntity.ok(dtos);
}
public ResponseEntity<?> getAll(Integer page ,Integer size)
{
Page<Installment> installments = installmentService.getAll(page,size);

public ResponseEntity<?> getAll(Integer page, Integer size) {
Page<Installment> installments = installmentService.getAll(page, size);
List<InstallmentDto> dtos = mapper.toDto(installments.getContent());
PaginatedResultDto<InstallmentDto> paginatedResult = new PaginatedResultDto<>();
paginatedResult.setData(dtos);
paginatedResult.setPagination(paginationMapper.toDto(installments));
return ResponseEntity.ok(paginatedResult);

}

public ResponseEntity<?> save(InstallmentDto installmentDto) {
Installment installment = mapper.toEntity(installmentDto);
InstallmentDto dto = mapper.toDto(installmentService.save(installment));
return ResponseEntity.ok(dto);
}

public ResponseEntity<?> update(InstallmentDto installmentDto, Integer id) {
Installment installment = installmentService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(),id));
Installment installment = installmentService.getById(id).orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(), id));
mapper.updateEntityFromDto(installmentDto, installment);
installmentService.save(installment);
InstallmentDto dto = mapper.toDto(installment);
return ResponseEntity.ok(dto);
}

public ResponseEntity<?> delete(Integer id) {
Installment installment = installmentService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(),id));
Installment installment = installmentService.getById(id).orElseThrow(() -> new ResourceNotFoundException(Installment.class.getSimpleName(), id));
try {
installmentService.delete(installment);
} catch (Exception exception) {
throw new ResourceRelatedException(Installment.class.getSimpleName(), "Id", id.toString(), ErrorCodes.RELATED_RESOURCE.getCode());
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new Response("deleted"));
}

public ResponseEntity<?> deleteAll(Integer id) {
List<Installment> installments = installmentService.getInstallmentsByOrderId(id);
List<Installment> installments = installmentService.getByOrderId(id);
try {
installmentService.deleteALL(installments);
} catch (Exception exception) {
Expand All @@ -93,11 +96,9 @@ public ResponseEntity<?> deleteAll(Integer id) {
}


public ResponseEntity<?> getByCustomerNationalId(String nationalId , Integer page , Integer size)
{
Customer customer = customerService.findNationalId(nationalId)
.orElseThrow(() -> new ResourceNotFoundException(Customer.class.getSimpleName(),nationalId));
Page<Installment> installments = installmentService.getByCustomerNationalId(customer.getNationalId() , page, size);
public ResponseEntity<?> getByCustomerNationalId(String nationalId, Integer page, Integer size) {
Customer customer = customerService.findNationalId(nationalId).orElseThrow(() -> new ResourceNotFoundException(Customer.class.getSimpleName(), nationalId));
Page<Installment> installments = installmentService.getByCustomerNationalId(customer.getNationalId(), page, size);
List<InstallmentDto> dtos = mapper.toDto(installments.getContent());
PaginatedResultDto<InstallmentDto> paginatedResult = new PaginatedResultDto<>();
paginatedResult.setData(dtos);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.rms.service;

import com.rms.domain.sales.Installment;
import com.rms.domain.sales.OrderItem;
import com.rms.repository.InstallmentRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -21,21 +20,20 @@ public Optional<Installment> getById(Integer id) {
return installmentRepository.findById(id);
}

public Page<Installment> getAll(Integer page , Integer size ) {
return installmentRepository.findAll(PageRequest.of(page,size));
public Page<Installment> getAll(Integer page, Integer size) {
return installmentRepository.findAll(PageRequest.of(page, size));
}

public List<Installment> getDueInstallments(Date startDate, Date endDate) {
return installmentRepository.getDueInstallments(startDate, endDate);
public Page<Installment> getByDueDate(Date startDate, Date endDate ,Integer page, Integer size) {
return installmentRepository.findByDueDate(startDate, endDate,PageRequest.of(page, size));
}

public List<Installment> getCustomerInstallments(Integer customerId) {
return installmentRepository.getByCustomerId(customerId);
public List<Installment> getByCustomerInstallments(Integer customerId) {
return installmentRepository.findByCustomerId(customerId);
}

public Page<Installment> getByCustomerNationalId(String nationalId , Integer page , Integer size)
{
return installmentRepository.getByCustomerNationalId(nationalId, PageRequest.of(page,size));
public Page<Installment> getByCustomerNationalId(String nationalId, Integer page, Integer size) {
return installmentRepository.findByCustomerNationalId(nationalId, PageRequest.of(page, size));
}

public Installment save(Installment installment) {
Expand All @@ -49,13 +47,14 @@ public List<Installment> save(List<Installment> installments) {
public void delete(Installment installment) {
installmentRepository.delete(installment);
}

public void deleteALL(List<Installment> installments) {
installmentRepository.deleteAll(installments);
}


public List<Installment> getInstallmentsByOrderId(Integer orderId){
return installmentRepository.findInstallmentsByOrderId(orderId);
public List<Installment> getByOrderId(Integer orderId) {
return installmentRepository.findByOrderId(orderId);
}


Expand Down