Skip to content

Commit

Permalink
vinh/adding create csv file for salary payment
Browse files Browse the repository at this point in the history
  • Loading branch information
AnataAria committed Nov 16, 2023
1 parent 534a1bd commit 70061e5
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 6 deletions.
10 changes: 10 additions & 0 deletions api/drawingcouseselling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.session</groupId>-->
<!-- <artifactId>spring-session-jdbc</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +23,16 @@ public ResponseEntity<InstructorFinanceDto> getInstructorFinance(@RequestHeader(
String email = jwtService.extractUserEmail(instructor.substring(7));
return ResponseEntity.ok(salaryService.getInstructorFinance(email));
}

@GetMapping("/salarys/exports")
public ResponseEntity<byte[]> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,4 +17,8 @@ public interface SalaryRepository extends JpaRepository<Salary, BigDecimal> {
"AND YEAR(s.salaryDate) = YEAR(CURRENT_DATE)" +
" AND MONTH(s.salaryDate) = MONTH(CURRENT_DATE )")
public Optional<Salary> 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<Salary> getSalariesByYearsAndMonth(@Param(value = "salaryDate")Date salaryDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Staff, BigDecimal> {

@Query(value = "SELECT s FROM staff s INNER JOIN account a ON a.id = s.account.id WHERE a.email = :email")
public Optional<Staff> getStaffByEmail(@Param(value = "email") String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.group1.drawingcouseselling.service;

import com.group1.drawingcouseselling.model.entity.Staff;

public interface StaffService {
public Staff searchStaffByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 70061e5

Please sign in to comment.