diff --git a/retail-managment-system/rms-api/src/main/java/com/rms/repository/InstallmentRepository.java b/retail-managment-system/rms-api/src/main/java/com/rms/repository/InstallmentRepository.java index 417d24ac..2bd41620 100644 --- a/retail-managment-system/rms-api/src/main/java/com/rms/repository/InstallmentRepository.java +++ b/retail-managment-system/rms-api/src/main/java/com/rms/repository/InstallmentRepository.java @@ -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; @@ -16,18 +17,18 @@ public interface InstallmentRepository extends JpaRepository getDueInstallments(@Param("startDate") Date startDate, @Param("endDate") Date endDate); + Page 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 getByCustomerId(@Param("customerId") Integer customerId); + List 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 getByCustomerNationalId(@Param("nationalId") String nationalId, Pageable pageable); + Page findByCustomerNationalId(@Param("nationalId") String nationalId, Pageable pageable); @Query("select i from Installment i where i.order.id = :orderId") - List findInstallmentsByOrderId(@Param("orderId") Integer orderId ); + List findByOrderId(@Param("orderId") Integer orderId); } diff --git a/retail-managment-system/rms-api/src/main/java/com/rms/rest/controller/InstallmentController.java b/retail-managment-system/rms-api/src/main/java/com/rms/rest/controller/InstallmentController.java index efae5c1d..3957bbee 100644 --- a/retail-managment-system/rms-api/src/main/java/com/rms/rest/controller/InstallmentController.java +++ b/retail-managment-system/rms-api/src/main/java/com/rms/rest/controller/InstallmentController.java @@ -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; @@ -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); } @@ -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") @@ -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); } } diff --git a/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/CustomerHandler.java b/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/CustomerHandler.java index 8df5e91f..ab1e2e2d 100644 --- a/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/CustomerHandler.java +++ b/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/CustomerHandler.java @@ -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; @@ -41,7 +39,7 @@ public ResponseEntity getById(Integer id) { } public ResponseEntity getCustomerInstallments(Integer id) { - List installments = installmentService.getCustomerInstallments(id); + List installments = installmentService.getByCustomerInstallments(id); List installmentDtos = installmentMapper.toDto(installments); return ResponseEntity.ok(installmentDtos); @@ -68,27 +66,25 @@ public ResponseEntity update(CustomerDto customerDto, Integer id) { Optional 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 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 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) { diff --git a/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/InstallmentHandler.java b/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/InstallmentHandler.java index 1984f391..02f50727 100644 --- a/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/InstallmentHandler.java +++ b/retail-managment-system/rms-api/src/main/java/com/rms/rest/handler/InstallmentHandler.java @@ -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; @@ -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; @@ -28,28 +29,30 @@ 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 installments = installmentService.getDueInstallments(new Date(), Date.from(endDate.atStartOfDay(defaultZoneId).toInstant())); - List dtos = mapper.toDto(installments); + LocalDate monthBegin = LocalDate.now().withDayOfMonth(1); + LocalDate monthEnd = LocalDate.now().plusMonths(1).withDayOfMonth(1).minusDays(1); + Page installments = installmentService.getByDueDate(Date.from(monthBegin.atStartOfDay(defaultZoneId).toInstant()), Date.from(monthEnd.atStartOfDay(defaultZoneId).toInstant()), page, size); + List dtos = mapper.toDto(installments.getContent()); + PaginatedResultDto paginatedResult = new PaginatedResultDto<>(); + paginatedResult.setData(dtos); + paginatedResult.setPagination(paginationMapper.toDto(installments)); return ResponseEntity.ok(dtos); } - public ResponseEntity getAll(Integer page ,Integer size) - { - Page installments = installmentService.getAll(page,size); + + public ResponseEntity getAll(Integer page, Integer size) { + Page installments = installmentService.getAll(page, size); List dtos = mapper.toDto(installments.getContent()); PaginatedResultDto paginatedResult = new PaginatedResultDto<>(); paginatedResult.setData(dtos); @@ -57,6 +60,7 @@ public ResponseEntity getAll(Integer page ,Integer size) return ResponseEntity.ok(paginatedResult); } + public ResponseEntity save(InstallmentDto installmentDto) { Installment installment = mapper.toEntity(installmentDto); InstallmentDto dto = mapper.toDto(installmentService.save(installment)); @@ -64,8 +68,7 @@ public ResponseEntity save(InstallmentDto installmentDto) { } 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); @@ -73,8 +76,7 @@ public ResponseEntity update(InstallmentDto installmentDto, Integer id) { } 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) { @@ -82,8 +84,9 @@ public ResponseEntity delete(Integer id) { } return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new Response("deleted")); } + public ResponseEntity deleteAll(Integer id) { - List installments = installmentService.getInstallmentsByOrderId(id); + List installments = installmentService.getByOrderId(id); try { installmentService.deleteALL(installments); } catch (Exception exception) { @@ -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 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 installments = installmentService.getByCustomerNationalId(customer.getNationalId(), page, size); List dtos = mapper.toDto(installments.getContent()); PaginatedResultDto paginatedResult = new PaginatedResultDto<>(); paginatedResult.setData(dtos); diff --git a/retail-managment-system/rms-api/src/main/java/com/rms/service/InstallmentService.java b/retail-managment-system/rms-api/src/main/java/com/rms/service/InstallmentService.java index 3c252e98..0532a3a5 100644 --- a/retail-managment-system/rms-api/src/main/java/com/rms/service/InstallmentService.java +++ b/retail-managment-system/rms-api/src/main/java/com/rms/service/InstallmentService.java @@ -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; @@ -21,21 +20,20 @@ public Optional getById(Integer id) { return installmentRepository.findById(id); } - public Page getAll(Integer page , Integer size ) { - return installmentRepository.findAll(PageRequest.of(page,size)); + public Page getAll(Integer page, Integer size) { + return installmentRepository.findAll(PageRequest.of(page, size)); } - public List getDueInstallments(Date startDate, Date endDate) { - return installmentRepository.getDueInstallments(startDate, endDate); + public Page getByDueDate(Date startDate, Date endDate ,Integer page, Integer size) { + return installmentRepository.findByDueDate(startDate, endDate,PageRequest.of(page, size)); } - public List getCustomerInstallments(Integer customerId) { - return installmentRepository.getByCustomerId(customerId); + public List getByCustomerInstallments(Integer customerId) { + return installmentRepository.findByCustomerId(customerId); } - public Page getByCustomerNationalId(String nationalId , Integer page , Integer size) - { - return installmentRepository.getByCustomerNationalId(nationalId, PageRequest.of(page,size)); + public Page getByCustomerNationalId(String nationalId, Integer page, Integer size) { + return installmentRepository.findByCustomerNationalId(nationalId, PageRequest.of(page, size)); } public Installment save(Installment installment) { @@ -49,13 +47,14 @@ public List save(List installments) { public void delete(Installment installment) { installmentRepository.delete(installment); } + public void deleteALL(List installments) { installmentRepository.deleteAll(installments); } - public List getInstallmentsByOrderId(Integer orderId){ - return installmentRepository.findInstallmentsByOrderId(orderId); + public List getByOrderId(Integer orderId) { + return installmentRepository.findByOrderId(orderId); }