Update SlipGajiService
Pembuatan service generate dan download template slip gaji rekap seluruh pegawai
This commit is contained in:
parent
ddc0bbb9eb
commit
f1ea106955
@ -1,6 +1,10 @@
|
||||
package com.jasamedika.medifirst2000.dao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import com.jasamedika.medifirst2000.entities.KomponenGaji;
|
||||
|
||||
@ -10,5 +14,7 @@ import com.jasamedika.medifirst2000.entities.KomponenGaji;
|
||||
*
|
||||
*/
|
||||
public interface KomponenGajiDao extends JpaRepository<KomponenGaji, Integer> {
|
||||
|
||||
@Query("select new Map(kg.id as id,kg.namaKomponen as namaKomponen) " + "from KomponenGaji kg "
|
||||
+ "order by kg.jenisKomponen,kg.id")
|
||||
List<Map<String, Object>> getAll();
|
||||
}
|
||||
|
||||
@ -353,8 +353,8 @@ public interface MapPegawaiJabatanToUnitKerjaDao extends CrudRepository<MapPegaw
|
||||
List<MapPegawaiJabatanToUnitKerja> findByPegawaiId(@Param("pegawaiId") Integer pegawaiId);
|
||||
|
||||
@Query("select model from MapPegawaiJabatanToUnitKerja model " + "inner join model.pegawai pg "
|
||||
+ "where model.statusEnabled is true " + "and pg.statusEnabled is true")
|
||||
List<MapPegawaiJabatanToUnitKerja> findAll();
|
||||
+ "where model.statusEnabled is true " + "and pg.statusEnabled is true " + "and pg.namaLengkap <> '-'")
|
||||
List<MapPegawaiJabatanToUnitKerja> getAll();
|
||||
|
||||
@Query("select new Map(mapp.id as idMapping, mapp.pegawaiId as idPegawai, mapp.atasanLangsungId as idAtasan, mapp.pejabatPenilaiId as idPenilai) "
|
||||
+ "from MapPegawaiJabatanToUnitKerja mapp "
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package com.jasamedika.medifirst2000.service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.jasamedika.medifirst2000.dto.SlipGajiDto;
|
||||
|
||||
/**
|
||||
@ -14,6 +16,6 @@ public interface SlipGajiService {
|
||||
void updateDetail(SlipGajiDto dto);
|
||||
|
||||
SlipGajiDto get(Integer pegawaiId, Long bulan);
|
||||
|
||||
String writeExcel(Long bulan);
|
||||
|
||||
String writeExcel(Long bulan) throws IOException;
|
||||
}
|
||||
|
||||
@ -1,24 +1,33 @@
|
||||
package com.jasamedika.medifirst2000.service.impl;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.jasamedika.medifirst2000.dao.KomponenGajiDao;
|
||||
import com.jasamedika.medifirst2000.dao.MapPegawaiJabatanToUnitKerjaDao;
|
||||
import com.jasamedika.medifirst2000.dao.PegawaiDao;
|
||||
import com.jasamedika.medifirst2000.dao.SlipGajiDao;
|
||||
@ -56,6 +65,9 @@ public class SlipGajiServiceImpl implements SlipGajiService {
|
||||
@Autowired
|
||||
private SlipGajiKomponenDao slipGajiKomponenDao;
|
||||
|
||||
@Autowired
|
||||
private KomponenGajiDao komponenGajiDao;
|
||||
|
||||
@Autowired
|
||||
private PegawaiDao pegawaiDao;
|
||||
|
||||
@ -68,7 +80,7 @@ public class SlipGajiServiceImpl implements SlipGajiService {
|
||||
Date startMonth = DateUtil.startMonth(now);
|
||||
Date endMonth = DateUtil.endMonth(now);
|
||||
List<Integer> listIdPegawaiSlip = slipGajiDao.findPegawaiByBulan(startMonth, endMonth);
|
||||
List<MapPegawaiJabatanToUnitKerja> listJabatan = mappingJabatanDao.findAll();
|
||||
List<MapPegawaiJabatanToUnitKerja> listJabatan = mappingJabatanDao.getAll();
|
||||
listJabatan = listJabatan.stream().filter(j -> !listIdPegawaiSlip.contains(j.getPegawaiId()))
|
||||
.sorted(Comparator.comparingInt(MapPegawaiJabatanToUnitKerja::getPegawaiId))
|
||||
.collect(Collectors.toList());
|
||||
@ -247,35 +259,86 @@ public class SlipGajiServiceImpl implements SlipGajiService {
|
||||
return dto.build();
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
@Override
|
||||
public String writeExcel(Long bulan) {
|
||||
public String writeExcel(Long bulan) throws IOException {
|
||||
Date month = new Date(bulan);
|
||||
Date start = DateUtil.startMonth(month);
|
||||
Date end = DateUtil.endMonth(month);
|
||||
List<Map<String, Object>> listPegawai = slipGajiDao.findAllByBulan(start, end);
|
||||
List<Map<String, Object>> allKomponen = komponenGajiDao.getAll();
|
||||
HSSFWorkbook workbook = new HSSFWorkbook();
|
||||
HSSFSheet sheet = workbook.createSheet(new SimpleDateFormat("MMMM yyyy", new Locale("in", "ID")).format(month));
|
||||
List<Object> listHeader = new ArrayList<>();
|
||||
listHeader.add("ID");
|
||||
listHeader.add("NAMA PEGAWAI");
|
||||
listHeader.add("GAJI");
|
||||
listHeader.add("REMUN KINERJA");
|
||||
listHeader.add("REMUN TAMBAHAN");
|
||||
listHeader.add("FEE FOR SERVICE");
|
||||
listHeader.add("FEE FOR ON SITE");
|
||||
listHeader.add("UANG MAKAN");
|
||||
listHeader.add("UANG LEMBUR");
|
||||
listHeader.add("PPH GAJI");
|
||||
listHeader.add("PPH REMUN");
|
||||
listHeader.add("PPH FEE FOR SERVICE");
|
||||
listHeader.add("PPH FEE FOR ON SITE");
|
||||
listHeader.add("PPH UANG MAKAN");
|
||||
listHeader.add("PPH UANG LEMBUR");
|
||||
listHeader.add("IURAN WAJIB KOPERASI");
|
||||
listHeader.add("POTONGAN PINJAMAN KOPERASI");
|
||||
listHeader.add("POTONGAN OBAT");
|
||||
listHeader.add("BIAYA RAWAT INAP");
|
||||
listHeader.add("BIAYA RAWAT JALAN");
|
||||
return null;
|
||||
List<Object> listIdKomponen = new ArrayList<>();
|
||||
List<Object> listKdKomponen = new ArrayList<>();
|
||||
listIdKomponen.add("ID");
|
||||
listIdKomponen.add("");
|
||||
listKdKomponen.add("");
|
||||
listKdKomponen.add("NAMA PEGAWAI / KOMPONEN");
|
||||
allKomponen.forEach(k -> {
|
||||
listIdKomponen.add(k.get("id"));
|
||||
listKdKomponen.add(k.get("namaKomponen"));
|
||||
});
|
||||
Object[] arrId = new Object[listIdKomponen.size()];
|
||||
Object[] arrkd = new Object[listKdKomponen.size()];
|
||||
listIdKomponen.toArray(arrId);
|
||||
listKdKomponen.toArray(arrkd);
|
||||
Integer i = 1;
|
||||
Map<Integer, Object[]> data = new HashMap<>();
|
||||
data.put(i++, arrId);
|
||||
data.put(i++, arrkd);
|
||||
for (Map<String, Object> pegawai : listPegawai) {
|
||||
data.put(i++, new Object[] { pegawai.get("id"), pegawai.get("namaLengkap") });
|
||||
}
|
||||
TreeMap<Integer, Object[]> sorted = new TreeMap<>();
|
||||
sorted.putAll(data);
|
||||
Set<Integer> keyset = sorted.keySet();
|
||||
int rownum = 0;
|
||||
for (Integer key : keyset) {
|
||||
Row row = sheet.createRow(rownum++);
|
||||
Object[] objArr = data.get(key);
|
||||
int cellnum = 0;
|
||||
for (Object obj : objArr) {
|
||||
Cell cell = row.createCell(cellnum++);
|
||||
if (obj instanceof String)
|
||||
cell.setCellValue((String) obj);
|
||||
else if (obj instanceof Integer)
|
||||
cell.setCellValue((Integer) obj);
|
||||
}
|
||||
}
|
||||
sheet.getRow(0).setZeroHeight(true);
|
||||
sheet.setColumnHidden(0, true);
|
||||
int colnums = sheet.getRow(0).getLastCellNum();
|
||||
for (int j = 0; j < colnums; j++) {
|
||||
sheet.autoSizeColumn(j);
|
||||
}
|
||||
sheet.createFreezePane(2, 2);
|
||||
DateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
||||
String timestampToString = df.format(new Date());
|
||||
String filename = "Template_Slip_Gaji_" + timestampToString + ".xls";
|
||||
String workingDir = System.getProperty("user.dir");
|
||||
String your_os = System.getProperty("os.name").toLowerCase();
|
||||
String absoluteFilePath = "";
|
||||
if (your_os.indexOf("win") >= 0) {
|
||||
// if_windows
|
||||
absoluteFilePath = workingDir + "\\" + filename;
|
||||
} else if (your_os.indexOf("nix") >= 0 || your_os.indexOf("nux") >= 0 || your_os.indexOf("mac") >= 0) {
|
||||
// if_unix_or_mac
|
||||
absoluteFilePath = workingDir + "//tmp//" + filename;
|
||||
// absoluteFilePath = workingDir + "/" + filename;
|
||||
} else {
|
||||
// unknown_os?
|
||||
absoluteFilePath = workingDir + "/" + filename;
|
||||
}
|
||||
FileOutputStream out = new FileOutputStream(new File(absoluteFilePath));
|
||||
try {
|
||||
workbook.write(out);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return absoluteFilePath;
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
return absoluteFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
package com.jasamedika.medifirst2000.util;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author salmanoe
|
||||
* @since Jan 31, 2023
|
||||
*/
|
||||
@Component
|
||||
public class ResourceUtils {
|
||||
public static Resource loadFile(String fileName) throws Exception {
|
||||
try {
|
||||
Path filePath = Paths.get(fileName).toAbsolutePath().normalize();
|
||||
Resource resource = new UrlResource(filePath.toUri());
|
||||
if (resource.exists()) {
|
||||
return resource;
|
||||
} else {
|
||||
throw new FileNotFoundException("File not found " + fileName);
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
throw new FileNotFoundException("File not found " + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,15 @@
|
||||
package com.jasamedika.medifirst2000.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -10,10 +17,15 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.jasamedika.medifirst2000.constants.Constants;
|
||||
import com.jasamedika.medifirst2000.dto.SlipGajiDto;
|
||||
import com.jasamedika.medifirst2000.service.SlipGajiService;
|
||||
import com.jasamedika.medifirst2000.util.CommonUtil;
|
||||
import com.jasamedika.medifirst2000.util.ResourceUtils;
|
||||
import com.jasamedika.medifirst2000.util.rest.RestUtil;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/slip-gaji")
|
||||
@ -59,4 +71,47 @@ public class SlipGajiController {
|
||||
SlipGajiDto entity = slipGajiService.get(pegawaiId, bulan);
|
||||
return new ResponseEntity<>(entity, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/template/generate/{bulan}", method = RequestMethod.GET)
|
||||
public ResponseEntity<Object> generateTemplate(@PathVariable Long bulan, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
try {
|
||||
String fileNamePath = slipGajiService.writeExcel(bulan);
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileNamePath);
|
||||
return new ResponseEntity<>(true, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/template/download", method = RequestMethod.GET)
|
||||
public ResponseEntity<Resource> downloadTemplate(@RequestParam(value = "filename", required = true) String fileName,
|
||||
HttpServletRequest request) {
|
||||
Resource resource = null;
|
||||
Map<String, String> mapHeaderMessage = new HashMap<String, String>();
|
||||
if (CommonUtil.isNotNullOrEmpty(fileName)) {
|
||||
try {
|
||||
resource = ResourceUtils.loadFile(fileName);
|
||||
} catch (Exception e) {
|
||||
mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
|
||||
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
|
||||
}
|
||||
String contentType = null;
|
||||
try {
|
||||
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
|
||||
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
|
||||
}
|
||||
if (CommonUtil.isNullOrEmpty(contentType))
|
||||
contentType = "application/octet-stream";
|
||||
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + resource.getFilename())
|
||||
.body(resource);
|
||||
} else {
|
||||
return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user