From 70061e56a0be5d8497af8f52445a7cba05a09d75 Mon Sep 17 00:00:00 2001
From: AnataArisa <92080339+AnataAria@users.noreply.github.com>
Date: Fri, 17 Nov 2023 02:57:34 +0700
Subject: [PATCH] vinh/adding create csv file for salary payment
---
api/drawingcouseselling/pom.xml | 10 ++++
.../controller/SalaryController.java | 21 ++++++--
.../model/entity/Staff.java | 2 +-
.../repository/SalaryRepository.java | 6 +++
.../repository/StaffRepository.java | 6 ++-
.../service/SalaryService.java | 1 +
.../service/StaffService.java | 7 +++
.../service/impl/SalaryServiceImpl.java | 52 ++++++++++++++++++-
.../service/impl/StaffServiceImpl.java | 20 +++++++
9 files changed, 119 insertions(+), 6 deletions(-)
create mode 100644 api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/StaffService.java
create mode 100644 api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/StaffServiceImpl.java
diff --git a/api/drawingcouseselling/pom.xml b/api/drawingcouseselling/pom.xml
index 11dabd1a..699fa98a 100644
--- a/api/drawingcouseselling/pom.xml
+++ b/api/drawingcouseselling/pom.xml
@@ -62,6 +62,16 @@
org.springframework.session
spring-session-core
+
+ org.apache.poi
+ poi
+ 5.0.0
+
+
+ org.apache.poi
+ poi-ooxml
+ 5.0.0
+
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/SalaryController.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/SalaryController.java
index 91dd5117..9e36ae3b 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/SalaryController.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/SalaryController.java
@@ -5,10 +5,13 @@
import com.group1.drawingcouseselling.service.JwtService;
import com.group1.drawingcouseselling.service.SalaryService;
import lombok.RequiredArgsConstructor;
+import org.springframework.http.ContentDisposition;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.math.BigDecimal;
@RestController
@RequiredArgsConstructor
@@ -20,4 +23,16 @@ public ResponseEntity getInstructorFinance(@RequestHeader(
String email = jwtService.extractUserEmail(instructor.substring(7));
return ResponseEntity.ok(salaryService.getInstructorFinance(email));
}
+
+ @GetMapping("/salarys/exports")
+ public ResponseEntity getMonthPaymentCsv(@RequestParam Integer year,@RequestParam Integer month, @RequestHeader(value = "Authorization") String instructor){
+ HttpHeaders headers = new HttpHeaders();
+ String email = jwtService.extractUserEmail(instructor.substring(7));
+ headers.setContentType(MediaType.parseMediaType("text/csv"));
+ headers.setContentDisposition(ContentDisposition.builder("attachment").filename("salary_instructor.csv").build());
+ var data = salaryService.getSalaryExcelOfMonths(month,year,email);
+ return ResponseEntity.ok()
+ .headers(headers)
+ .body(data);
+ }
}
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Staff.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Staff.java
index 06372fbb..12a3153b 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Staff.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Staff.java
@@ -10,7 +10,7 @@
import java.math.BigDecimal;
import java.sql.Date;
-@Entity(name = " staff")
+@Entity(name = "staff")
@NoArgsConstructor(force = true)
public class Staff implements Serializable {
@Id
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/SalaryRepository.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/SalaryRepository.java
index f5075a90..0764fc2a 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/SalaryRepository.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/SalaryRepository.java
@@ -7,6 +7,8 @@
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
+import java.sql.Date;
+import java.util.Collection;
import java.util.Optional;
@Repository
@@ -15,4 +17,8 @@ public interface SalaryRepository extends JpaRepository {
"AND YEAR(s.salaryDate) = YEAR(CURRENT_DATE)" +
" AND MONTH(s.salaryDate) = MONTH(CURRENT_DATE )")
public Optional getSalariesByInstructorID(@Param(value = "instructorID") BigDecimal instructorID);
+ @Query(value = "SELECT s FROM salary s WHERE " +
+ "YEAR(s.salaryDate) = YEAR(:salaryDate)" +
+ " AND MONTH(s.salaryDate) = MONTH(:salaryDate)")
+ public Collection getSalariesByYearsAndMonth(@Param(value = "salaryDate")Date salaryDate);
}
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/StaffRepository.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/StaffRepository.java
index 3b144633..a0b7ac89 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/StaffRepository.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/repository/StaffRepository.java
@@ -2,11 +2,15 @@
import com.group1.drawingcouseselling.model.entity.Staff;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
+import java.util.Optional;
@Repository
public interface StaffRepository extends JpaRepository {
-
+ @Query(value = "SELECT s FROM staff s INNER JOIN account a ON a.id = s.account.id WHERE a.email = :email")
+ public Optional getStaffByEmail(@Param(value = "email") String email);
}
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/SalaryService.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/SalaryService.java
index 21521e36..1ec4943c 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/SalaryService.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/SalaryService.java
@@ -9,4 +9,5 @@ public interface SalaryService {
public InstructorFinanceDto getInstructorFinance(String instructorEmail);
public void updateSalary(String instructorEmail, BigDecimal income);
public void updateSalary(Instructor instructorInfo, BigDecimal income);
+ public byte[] getSalaryExcelOfMonths(Integer month, Integer year, String staffEmail);
}
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/StaffService.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/StaffService.java
new file mode 100644
index 00000000..4f0f88ac
--- /dev/null
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/StaffService.java
@@ -0,0 +1,7 @@
+package com.group1.drawingcouseselling.service;
+
+import com.group1.drawingcouseselling.model.entity.Staff;
+
+public interface StaffService {
+ public Staff searchStaffByEmail(String email);
+}
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java
index b6af2b00..eeb87b70 100644
--- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java
@@ -1,6 +1,7 @@
package com.group1.drawingcouseselling.service.impl;
import com.group1.drawingcouseselling.exception.EntityNotFoundException;
+import com.group1.drawingcouseselling.exception.SomethingWentWrongExceptions;
import com.group1.drawingcouseselling.model.dto.CourseDto;
import com.group1.drawingcouseselling.model.dto.InstructorFinanceDto;
import com.group1.drawingcouseselling.model.entity.Instructor;
@@ -9,11 +10,18 @@
import com.group1.drawingcouseselling.service.*;
import lombok.RequiredArgsConstructor;
import lombok.Value;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.math.BigDecimal;
+import java.sql.Date;
import java.time.LocalDate;
@Service
@@ -23,6 +31,7 @@ public class SalaryServiceImpl implements SalaryService {
private final InstructorService instructorService;
private final CourseService courseService;
private final MyLearningService myLearningService;
+ private final StaffService staffService;
private final BigDecimal PROFIT_PERCENTAGE = new BigDecimal("0.9");
@Scheduled(cron = "0 0 1 * * *")
@Transactional
@@ -76,4 +85,45 @@ public void updateSalary(Instructor instructorInfo, BigDecimal income){
salaryInfo.setAmount(salaryInfo.getAmount().add(income));
salaryRepository.save(salaryInfo);
}
-}
+ @Override
+ public byte[] getSalaryExcelOfMonths(Integer month, Integer year, String staffEmail){
+ var staff = staffService.searchStaffByEmail(staffEmail);
+ var date = Date.valueOf(LocalDate.of(year, month,1));
+ var salaryList = salaryRepository.getSalariesByYearsAndMonth(date);
+ try (Workbook workbook = new XSSFWorkbook()) {
+ Sheet sheet = workbook.createSheet("SalaryData");
+
+ Row headerRow = sheet.createRow(0);
+ headerRow.createCell(0).setCellValue("Email/Phone");
+ headerRow.createCell(1).setCellValue("Amount");
+ headerRow.createCell(2).setCellValue("Currency code");
+ headerRow.createCell(3).setCellValue("Reference ID (optional)");
+ headerRow.createCell(4).setCellValue("Note to recipient");
+ headerRow.createCell(5).setCellValue("Recipient wallet");
+ headerRow.createCell(6).setCellValue("Social Feed Privacy (optional)");
+ headerRow.createCell(7).setCellValue("Holler URL (deprecated)");
+ headerRow.createCell(8).setCellValue("Logo URL (optional)");
+
+ int rowNum = 1;
+ for (Salary salary : salaryList) {
+ Row row = sheet.createRow(rowNum++);
+ row.createCell(0).setCellValue(salary.getInstructor().getPaypalEmail());
+ row.createCell(1).setCellValue(salary.getAmount().doubleValue());
+ row.createCell(2).setCellValue(salary.getCurrency());
+ row.createCell(3).setCellValue("");
+ row.createCell(4).setCellValue("Here is your salary, " + salary.getInstructor().getFullName());
+ row.createCell(5).setCellValue(salary.getRecipientWallet());
+ row.createCell(6).setCellValue("PUBLIC");
+ row.createCell(7).setCellValue("");
+ row.createCell(8).setCellValue("");
+ }
+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
+ workbook.write(outputStream);
+ return outputStream.toByteArray();
+ }
+
+ } catch (IOException e) {
+ throw new SomethingWentWrongExceptions("Error when trying to export data to csv file");
+ }
+ }
+ }
diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/StaffServiceImpl.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/StaffServiceImpl.java
new file mode 100644
index 00000000..65bb087e
--- /dev/null
+++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/StaffServiceImpl.java
@@ -0,0 +1,20 @@
+package com.group1.drawingcouseselling.service.impl;
+
+import com.group1.drawingcouseselling.exception.UserNotFoundException;
+import com.group1.drawingcouseselling.model.entity.Staff;
+import com.group1.drawingcouseselling.repository.StaffRepository;
+import com.group1.drawingcouseselling.service.StaffService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+@Service
+@RequiredArgsConstructor
+public class StaffServiceImpl implements StaffService {
+ private final StaffRepository staffRepository;
+ @Override
+ public Staff searchStaffByEmail(String email){
+ return staffRepository.getStaffByEmail(email).orElseThrow(() ->new UserNotFoundException("Not found this staff"));
+ }
+}