Unverified Commit e38dc67d authored by mshaik-nisum-com's avatar mshaik-nisum-com Committed by GitHub

Merge pull request #189 from nisum-inc/FEATURE/ExportExcelAndPdf

Feature/export excel and pdf
parents 337099ad 1911ff3b
...@@ -48,22 +48,22 @@ public class AttendanceController { ...@@ -48,22 +48,22 @@ public class AttendanceController {
} }
@RequestMapping(value = "generatePdfReport/{id}/{fromDate}/{toDate}", @RequestMapping(value = "generatePdfReport/{id}/{fromDate}/{toDate}",
method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> generatePdfReport(@PathVariable("id") long id, public ResponseEntity<List> generatePdfReport(@PathVariable("id") long id,
@PathVariable("fromDate") String fromDate, @PathVariable("fromDate") String fromDate,
@PathVariable("toDate") String toDate) throws MyTimeException { @PathVariable("toDate") String toDate) throws MyTimeException {
String result = userService.generatePdfReport(id, fromDate, toDate); List result = userService.generatePdfReport(id, fromDate, toDate);
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);
} }
@RequestMapping(value = "generatePdfReport/{id}/{fromDate}/{toDate}/{fromTime}/{toTime}", @RequestMapping(value = "generatePdfReport/{id}/{fromDate}/{toDate}/{fromTime}/{toTime}",
method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> generatePdfReport(@PathVariable("id") long id, public ResponseEntity<List> generatePdfReport(@PathVariable("id") long id,
@PathVariable("fromDate") String fromDate, @PathVariable("fromDate") String fromDate,
@PathVariable("toDate") String toDate, @PathVariable("toDate") String toDate,
@PathVariable("fromTime") String fromTime, @PathVariable("fromTime") String fromTime,
@PathVariable("toTime") String toTime) throws MyTimeException, ParseException { @PathVariable("toTime") String toTime) throws MyTimeException, ParseException {
String result = userService.generatePdfReport(id, fromDate, toDate,fromTime,toTime); List result = userService.generatePdfReport(id, fromDate, toDate,fromTime,toTime);
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);
} }
......
...@@ -12,7 +12,7 @@ public interface AttendanceService { ...@@ -12,7 +12,7 @@ public interface AttendanceService {
List<AttendenceData> getAttendanciesReport(String reportDate,String shift) throws MyTimeException, SQLException; List<AttendenceData> getAttendanciesReport(String reportDate,String shift) throws MyTimeException, SQLException;
String generatePdfReport(long id, String fromDate, String toDate, String fromTime, String toTime) throws MyTimeException; List generatePdfReport(long id, String fromDate, String toDate, String fromTime, String toTime) throws MyTimeException;
List<EmpLoginData> employeeLoginReportBasedOnDateTime(long id, String fromDate, List<EmpLoginData> employeeLoginReportBasedOnDateTime(long id, String fromDate,
String toDate,String fromTime,String toTime) throws MyTimeException, ParseException; String toDate,String fromTime,String toTime) throws MyTimeException, ParseException;
......
...@@ -61,8 +61,8 @@ public class AttendanceServiceImpl implements AttendanceService { ...@@ -61,8 +61,8 @@ public class AttendanceServiceImpl implements AttendanceService {
} }
@Override @Override
public String generatePdfReport(long id, String fromDate, String toDate, String fromTime, String toTime) public List generatePdfReport(long id, String fromDate, String toDate, String fromTime, String toTime)
throws MyTimeException { throws MyTimeException {
return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate); return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate);
} }
......
...@@ -100,7 +100,7 @@ public class ProjectServiceImpl implements ProjectService { ...@@ -100,7 +100,7 @@ public class ProjectServiceImpl implements ProjectService {
@Override @Override
public String generatePdfReport(long id, String fromDate, String toDate) throws MyTimeException { public String generatePdfReport(long id, String fromDate, String toDate) throws MyTimeException {
return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate); return pdfReportGenerator.generateeReport(id, fromDate, toDate);
} }
@Override @Override
...@@ -771,8 +771,7 @@ public class ProjectServiceImpl implements ProjectService { ...@@ -771,8 +771,7 @@ public class ProjectServiceImpl implements ProjectService {
billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted( billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted(
Comparator.comparing(BillingDetails::getCreateDate).reversed()).collect(Collectors.toList()); Comparator.comparing(BillingDetails::getCreateDate).reversed()).collect(Collectors.toList());
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception }
}
return billingsSorted; return billingsSorted;
} }
...@@ -784,7 +783,6 @@ public class ProjectServiceImpl implements ProjectService { ...@@ -784,7 +783,6 @@ public class ProjectServiceImpl implements ProjectService {
billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted( billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted(
Comparator.comparing(BillingDetails::getCreateDate).reversed()).collect(Collectors.toList()); Comparator.comparing(BillingDetails::getCreateDate).reversed()).collect(Collectors.toList());
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception
} }
return billingsSorted; return billingsSorted;
} }
...@@ -801,7 +799,6 @@ public class ProjectServiceImpl implements ProjectService { ...@@ -801,7 +799,6 @@ public class ProjectServiceImpl implements ProjectService {
billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted( billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted(
Comparator.comparing(BillingDetails::getBillingStartDate).reversed()).collect(Collectors.toList()); Comparator.comparing(BillingDetails::getBillingStartDate).reversed()).collect(Collectors.toList());
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception
} }
return billingsSorted; return billingsSorted;
} }
...@@ -818,7 +815,6 @@ public class ProjectServiceImpl implements ProjectService { ...@@ -818,7 +815,6 @@ public class ProjectServiceImpl implements ProjectService {
billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted( billingsSorted = (billings == null || billings.size() == 0) ? billings : billings.stream().sorted(
Comparator.comparing(BillingDetails::getBillingStartDate).reversed()).collect(Collectors.toList()); Comparator.comparing(BillingDetails::getBillingStartDate).reversed()).collect(Collectors.toList());
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception
} }
return billingsSorted; return billingsSorted;
} }
......
...@@ -33,10 +33,10 @@ public interface UserService { ...@@ -33,10 +33,10 @@ public interface UserService {
EmployeeRoles assigingEmployeeRole(EmployeeRoles employeeRoles, String empId) EmployeeRoles assigingEmployeeRole(EmployeeRoles employeeRoles, String empId)
throws MyTimeException; throws MyTimeException;
String generatePdfReport(long id, String fromDate, String toDate) List generatePdfReport(long id, String fromDate, String toDate)
throws MyTimeException; throws MyTimeException;
String generatePdfReport(long id, String fromDate, String toDate,String fromTime,String toTime) List generatePdfReport(long id, String fromDate, String toDate,String fromTime,String toTime)
throws MyTimeException, ParseException; throws MyTimeException, ParseException;
EmployeeRoles getEmployeesRole(String emailId); EmployeeRoles getEmployeesRole(String emailId);
......
...@@ -128,13 +128,13 @@ public class UserServiceImpl implements UserService { ...@@ -128,13 +128,13 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public String generatePdfReport(long id, String fromDate, String toDate) public List generatePdfReport(long id, String fromDate, String toDate)
throws MyTimeException { throws MyTimeException {
return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate); return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate);
} }
@Override @Override
public String generatePdfReport(long id, String fromDate, String toDate,String fromTime,String toTime) public List generatePdfReport(long id, String fromDate, String toDate,String fromTime,String toTime)
throws MyTimeException, ParseException { throws MyTimeException, ParseException {
return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate,fromTime,toTime); return pdfReportGenerator.generateEmployeeReport(id, fromDate, toDate,fromTime,toTime);
} }
......
...@@ -3,6 +3,7 @@ package com.nisum.mytime.utils; ...@@ -3,6 +3,7 @@ package com.nisum.mytime.utils;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -39,23 +40,45 @@ public class PdfReportGenerator { ...@@ -39,23 +40,45 @@ public class PdfReportGenerator {
@Autowired @Autowired
private AttendanceService attendanceService; private AttendanceService attendanceService;
public String generateEmployeeReport(long employeeId, String startDate, String endDate) throws MyTimeException { public List generateEmployeeReport(long employeeId, String startDate, String endDate) throws MyTimeException {
String fileName = employeeId + "_" + startDate + "_" + endDate + ".pdf"; String fileName = employeeId + "_" + startDate + "_" + endDate + ".pdf";
List<EmpLoginData> empLoginDetails = getEmployeeData(employeeId, startDate, endDate); List<EmpLoginData> empLoginDetails = getEmployeeData(employeeId, startDate, endDate);
List filenameData=new ArrayList<>();
if (empLoginDetails.isEmpty()) { if (empLoginDetails.isEmpty()) {
return "No data available"; String message= "No data available";
} else { filenameData.add(message);
return createPDF(fileName, empLoginDetails, employeeId); return filenameData;
} } else {
String file= createPDF(fileName, empLoginDetails, employeeId);
filenameData.add(file);
filenameData.add(empLoginDetails);
return filenameData;
}
} }
public String generateEmployeeReport(long employeeId, String startDate, String endDate,String fromTime,String toTime) throws MyTimeException, ParseException { public String generateeReport(long employeeId, String startDate, String endDate) throws MyTimeException {
String fileName = employeeId + "_" + startDate + "_" + endDate + ".pdf";
List<EmpLoginData> empLoginDetails = getEmployeeData(employeeId, startDate, endDate);
if (empLoginDetails.isEmpty()) {
String message= "No data available";
return message;
} else {
return fileName;
}
}
public List generateEmployeeReport(long employeeId, String startDate, String endDate,String fromTime,String toTime) throws MyTimeException, ParseException {
String fileName = employeeId + "_" + startDate + "_" + endDate + ".pdf"; String fileName = employeeId + "_" + startDate + "_" + endDate + ".pdf";
List<EmpLoginData> empLoginDetails = getEmployeeData(employeeId, startDate, endDate,fromTime,toTime); List<EmpLoginData> empLoginDetails = getEmployeeData(employeeId, startDate, endDate,fromTime,toTime);
List filenameData=new ArrayList<>();
if (empLoginDetails.isEmpty()) { if (empLoginDetails.isEmpty()) {
return "No data available"; String message= "No data available";
filenameData.add(message);
return filenameData;
} else { } else {
return createPDF(fileName, empLoginDetails, employeeId); String file= createPDF(fileName, empLoginDetails, employeeId);
filenameData.add(file);
filenameData.add(empLoginDetails);
return filenameData;
} }
} }
......
myApp.controller("reportsController", function($scope, $http, myFactory, $mdDialog, appConfig, $timeout, $compile) { myApp
.directive('excelExport',
function () {
return {
restrict: 'A',
scope: {
fileName: "@",
data: "&exportData"
},
replace: true,
template: '<button class="btn btn-primary btn-ef btn-ef-3 btn-ef-3c mb-10" ng-click="download()">Export to Excel <i class="fa fa-download"></i></button>',
link: function (scope, element) {
scope.download = function() {
function datenum(v, date1904) {
if(date1904) v+=1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
};
function getSheet(data, opts) {
var ws = {};
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
for(var R = 0; R != data.length; ++R) {
for(var C = 0; C != data[R].length; ++C) {
if(range.s.r > R) range.s.r = R;
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var cell = {v: data[R][C] };
if(cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {
cell.t = 'n'; cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
}
else cell.t = 's';
ws[cell_ref] = cell;
}
}
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
};
function Workbook() {
if(!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
var wb = new Workbook(), ws = getSheet(scope.data());
/* add worksheet to workbook */
wb.SheetNames.push(scope.fileName);
wb.Sheets[scope.fileName] = ws;
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), scope.fileName+'.xlsx');
};
}
};
}
)
.controller("reportsController", function($scope, $http, myFactory, $mdDialog, appConfig, $timeout, $compile) {
$scope.records = []; $scope.records = [];
$scope.searchId=""; $scope.searchId="";
// Date picker related code // Date picker related code
...@@ -7,6 +83,7 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial ...@@ -7,6 +83,7 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial
$scope.fromDate = today; $scope.fromDate = today;
$scope.toDate = today; $scope.toDate = today;
$scope.reportMsg ="Please generate a report for preview."; $scope.reportMsg ="Please generate a report for preview.";
$scope.isDataAvailable = false ;
$scope.validateDates = function(dateValue, from) { $scope.validateDates = function(dateValue, from) {
if(from == "FromDate"){ if(from == "FromDate"){
var toDat = $scope.toDate; var toDat = $scope.toDate;
...@@ -225,10 +302,14 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial ...@@ -225,10 +302,14 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial
method : "GET", method : "GET",
url : url url : url
}).then(function mySuccess(response) { }).then(function mySuccess(response) {
if(response.data == "No data available"){ if(response.data[0] == "No data available"){
$scope.pdfUrl = response.data; $scope.pdfUrl = response.data;
$scope.isDataAvailable = false ;
}else{ }else{
$scope.pdfUrl = "reports/"+response.data; $scope.pdfUrl = "reports/"+response.data[0];
$scope.jsonToExport = response.data[1];
excelDataFormation();
$scope.isDataAvailable = true ;
} }
}, function myError(response) { }, function myError(response) {
showAlert("Something went wrong while generating report!!!"); showAlert("Something went wrong while generating report!!!");
...@@ -236,6 +317,38 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial ...@@ -236,6 +317,38 @@ myApp.controller("reportsController", function($scope, $http, myFactory, $mdDial
}); });
} }
// function generateExcelReport(id,data) {
// debugger;
//
// var defaultURL= appConfig.appUri + "attendance/employeeLoginReportBasedOnDateTime?empId=" + id + "&fromDate=" + data.fromDate + "&toDate=" + data.toDate + "&fromTime=" + data.fromTime + "&toTime=" + data.toTime
// $http({
// method : "GET",
// url : defaultURL
// }).then(function mySuccess(response) {
// if(response.data){
// console.log(response.data);
// $scope.jsonToExport = response.data;
// excelDataFormation();
// }
// }, function myError(response) {
// showAlert("Something went wrong while generating report!!!");
// $scope.pdfUrl = "";
// });
// }
function excelDataFormation(){
// Prepare Excel data:
$scope.fileName = "report";
$scope.exportData = [];
// Headers:
$scope.exportData.push([ "ID", "Name", "Date", "Login Time", "Logout Time", "Total Hours"]);
// Data:
angular.forEach($scope.jsonToExport, function(value, key) {
$scope.exportData.push([value.employeeId, value.employeeName,value.dateOfLogin, value.firstLogin,value.lastLogout,value.totalLoginTime]);
});
}
function getFormattedDate(date){ function getFormattedDate(date){
var day = date.getDate(); var day = date.getDate();
var month = date.getMonth() + 1; var month = date.getMonth() + 1;
......
...@@ -235,4 +235,7 @@ md-select { ...@@ -235,4 +235,7 @@ md-select {
} }
.my-css-class{ .my-css-class{
margin-left: 20px; margin-left: 20px;
} }
\ No newline at end of file .align-button{
margin-left: -2%;
}
...@@ -81,6 +81,18 @@ ...@@ -81,6 +81,18 @@
</div> </div>
</div> </div>
</div> </div>
<div class="form-horizontal">
<div class="form-group">
<div class="form-inline col-lg-12 align-button">
<div class="form-group col-lg-9">
</div>
<div class="form-group col-lg-2">
<div excel-export export-data="exportData" file-name="{{fileName}}" ng-disabled = "!isDataAvailable"></div>
</div>
<div class="form-group col-lg-1">
<button class="btn btn-primary btn-ef btn-ef-3 btn-ef-3c mb-10" ng-click="print(pdfUrl)" ng-disabled = "!isDataAvailable" > Save as PDF <i class="fa fa-download"></i></button>
</div>
</div>
</div>
</div>
</div> </div>
...@@ -55,7 +55,8 @@ public class AttendanceControllerTest { ...@@ -55,7 +55,8 @@ public class AttendanceControllerTest {
@Test @Test
public void testgeneratePdfReport() throws Exception { public void testgeneratePdfReport() throws Exception {
when(userService.generatePdfReport(12345,"2017-11-18","2017-12-18")).thenReturn("empLoginDetails"); List list = new ArrayList();
when(userService.generatePdfReport(12345,"2017-11-18","2017-12-18")).thenReturn(list);
mockMvc.perform(get("/attendance/generatePdfReport/12345/2017-11-18/2017-12-18")).andExpect(MockMvcResultMatchers.status().isOk()); mockMvc.perform(get("/attendance/generatePdfReport/12345/2017-11-18/2017-12-18")).andExpect(MockMvcResultMatchers.status().isOk());
verify(userService).generatePdfReport(12345,"2017-11-18" , "2017-12-18"); verify(userService).generatePdfReport(12345,"2017-11-18" , "2017-12-18");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment