apollo_developers/assets/js/controllers/employeeCtrl.js
venbatechnologies@gmail.com b46afdc48c multiple role branch access
2018-04-26 16:11:56 +05:30

851 lines
31 KiB
JavaScript
Executable File

'use strict';
/**
* controllers for ng-table
* Simple table with sorting and filtering on AngularJS
*/
app.controller('staffCtrl', ["$scope", "toaster", "$filter", "ngTableParams", "$http", "API_POINTS", "$state", '$rootScope', '$cookies', 'md5', 'ipCookie', '$timeout', '$window', "flowFactory", function ($scope, toaster, $filter, ngTableParams, $http, apiPoint, $state, $rootScope, $cookies, md5, ipCookie, $timeout, $window, flowFactory) {
// $scope.removeImage = function () {
// $scope.noImage = true;
// };
// $scope.obj = new Flow();
// if ($scope.userInfo.avatar == '') {
// $scope.noImage = true;
// }
/**
* staff details capture
* by : kdk
*/
$scope.myModel = {
'employeeId': '',
'firstname': '',
'lastname': '',
'fathername': '',
'mothername': '',
'emailId': '',
'primaryPhone': '',
'secondaryPhone': '',
'dateofbirth': '',
'gender': '',
'qualificaion': '',
'aadharNumber': '',
'otherId': '',
'otherIdDetails': '',
'address1': '',
'address2': '',
'homeBranch': '',
'dateofjoining': '',
'dateofjoining': '',
'role': '',
'jobResponsibility': '',
'switchsetting': true,
'finaceModuleAccess': false
};
var data = '';
$scope.roleDetails = '';
$scope.branchDetails = '';
$scope.employeeDetails = '';
$scope.loader = false;
$scope.emptyData;
// 2017-11-23T00:00:00+0530
// sorting function for table params
$scope.sort = function (keyname) {
$scope.sortKey = keyname; //set the sortKey to the param passed
$scope.reverse = !$scope.reverse; //if true make it false and vice versa
}
// get local cliend details
var localDetail = JSON.parse(localStorage.getItem('localObj'));
var localDetails = ipCookie('cookiechk');
// load when html page load
$scope.init = function () {
if (localDetail != null) {
$scope.currentDate = new Date();
$scope.localUserListCode = localDetail.localType;
$scope.localBranchID = localDetails.localBranchID;
// $scope.currentPage = 1;
$scope.getEmployeeList();
$scope.getRoleList();
$scope.getBranchDetails();
} else {
}
}
$scope.getEmployeeList = function () {
$scope.loader = true;
var empListreq = {
method: 'POST',
url: apiPoint.url + 'getEmployeeList/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: localDetails
}
};
$http(empListreq).then(function (response) {
if (response.data.employeeList) {
$scope.loader = false;
$scope.emptyData = true;
$scope.data = response.data.employees_details;
// data = response.data.employees_details;
// $scope.$watch('result', function () {
// $scope.tableParams.settings().$scope = $scope;
// $scope.tableParams.reload();
// });
// if( data.length <= 2) {
// $scope.currentPage = $scope.currentPage;
// } else {
// $scope.currentPage = $scope.currentPage-1;
// }
// $scope.intial = response.data.employees_details;
// $scope.search = { StaffID: '' };
// $scope.tableParams = new ngTableParams({
// page: 1,
// count: 10,
// filter: $scope.search
// }, {
// // total: data.length,
// getData: function ($defer, params) {
// $scope.currentPage = params.page()
// data;
// params.total(data.length)
// var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
// orderedData = params.filter() ? $filter('filter')(orderedData, params.filter().StaffID) : orderedData;
// $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
// }
// });
} else {
$scope.loader = false;
$scope.emptyData = false;
}
});
}
$scope.editId = -1;
$scope.editBranchId = -1;
$scope.setEditId = function (id) {
$scope.editId = id;
$scope.editBranchId = -1;
}
$scope.getEditDetails = function(p) {
$scope.myModelEdit = {
"StaffID": p.StaffID,
"Firstname": p.Firstname,
"Lastname": p.Lastname,
"Fathername": p.Fathername,
"MotherName": p.MotherName,
"EmailID": p.EmailID,
"MobileNumber": p.MobileNumber,
"AlternateNumber": p.AlternateNumber,
"DOB": p.DOB,
"Gender": p.Gender,
"Qualification":p.Qualification,
"AadharNumber": p.AadharNumber,
"OtherID": p.OtherID,
"OtherIDDetails": p.OtherIDDetails,
"PermanentAddress": p.PermanentAddress,
"PresentAddress": p.PresentAddress,
"DOJ": p.DOJ,
"IsActive": p.IsActive,
"ProfilePicPath": p.ProfilePicPath
}
}
$scope.setEditBranchId = function (id) {
$scope.getEmpBranchDetails(id);
$scope.editBranchId = id;
$scope.editId = -1;
$scope.branchEditLoading = true;
}
$scope.cancel = function () {
$scope.getEmployeeList();
$scope.editId = -1;
$scope.editBranchId = -1;
}
$scope.empBranchDetails = '';
$scope.empRoleDetail = '';
$scope.branchListView = false;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.getEmpBranchDetails = function (id) {
// alert(id);
$scope.empEditBranchDetails = {
'ID': '',
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': ''
}
var req = {
method: 'POST',
url: apiPoint.url + 'getEmpBranchDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
staffID: id
}
};
$http(req).then(function (response) {
if (response.data.empBranchDetails) {
// $scope.editBranchId = id;
$scope.branchEditLoading = false;
$scope.branchListView = true;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.empBranchs = response.data.branch_details;
// $scope.branchArr = [];
// $scope.BranchName = '';
// angular.forEach($scope.empBranchs, function (item, index) {
// // alert(item.BranchName);
// $scope.BranchName = item.BranchName + '-' + item.BranchCode;
// $scope.branchArr.push($scope.BranchName);
// });
// $scope.empBranchDetails = {
// arr: $scope.branchArr,
// }
// response.data.role_details[0].FinanceModuleAccess = response.data.role_details[0].FinanceModuleAccess == 1 ? true : false;
// $scope.empRoleDetail = response.data.role_details[0];
} else {
$scope.NoBranchDetailsFound = response.data.message ;
}
});
}
$scope.empEditBranchDetails = {
'ID': '',
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': '',
'FinanceModuleAccess': '',
'HomeBranch': '',
'IsActive': ''
}
$scope.getEmpEditBranchDetails = function(p) {
$scope.branchListView = false;
$scope.empEditBranchView = true;
$scope.empAddNewBranchView = false;
$scope.empEditBranchDetails.ID = p.ID;
$scope.empEditBranchDetails.StaffID = p.StaffID;
$scope.empEditBranchDetails.ListCode = p.ListCode;
$scope.empEditBranchDetails.BranchCode = p.BranchCode;
$scope.empEditBranchDetails.JobResponsibility = p.JobResponsibility;
$scope.empEditBranchDetails.FinanceModuleAccess = p.FinanceModuleAccess == 1 ? true : false;
$scope.empEditBranchDetails.HomeBranch = p.HomeBranch;
$scope.empEditBranchDetails.IsActive = p.IsActive == 1 ? true : false ;
}
$scope.getEmpEditBranchDetailsCancel = function() {
$scope.branchListView = true;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.empEditBranchDetails = {
'ID': '',
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': '',
'FinanceModuleAccess': '',
'HomeBranch': '',
'IsActive': ''
}
}
$scope.updateEmpBranchDetails = function (form) {
if (form.$invalid) {
var field = null, firstError = null;
for (field in form) {
if (field[0] != '$') {
if (firstError === null && !form[field].$valid) {
firstError = form[field].$name;
}
if (form[field].$pristine) {
form[field].$dirty = true;
}
}
}
angular.element('.ng-invalid[name=' + firstError + ']').focus();
// swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error");
} else {
// angular.forEach(empBranchs.arr, function (item) {
// var newString = item.split("-").pop();
// $scope.arr1.push(newString);
// });
// alert(JSON.stringify($scope.arr1));
$scope.sendBranchData = {
'ID': $scope.empEditBranchDetails.ID,
'empFor': $scope.empEditBranchDetails.StaffID ,
'updateRole': $scope.empEditBranchDetails.ListCode,
'JobResponsibility': $scope.empEditBranchDetails.JobResponsibility,
'updateBranch': $scope.empEditBranchDetails.BranchCode,
'updateBy': localDetails.localUserID,
'FinanceModuleAccess': $scope.empEditBranchDetails.FinanceModuleAccess == true ? 1 : 0,
'HomeBranch': $scope.empEditBranchDetails.HomeBranch,
'IsActive': $scope.empEditBranchDetails.IsActive == true ? 1 : 0
}
// p.IsActive = p.IsActive === true ? 1 : 0;
var req = {
method: 'POST',
url: apiPoint.url + 'updateEmpBranchDetail/',
headers: {
'Content-Type': 'application/json'
},
data: $scope.sendBranchData
};
$http(req).then(function (response) {
if (response.data.updateEmpbranchStatus == true) {
swal("", "Updated Successfully.", "success");
$scope.getEmployeeList();
$scope.getEmpBranchDetails($scope.empEditBranchDetails.StaffID);
$scope.branchListView = true;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.empEditBranchDetails = {
'ID': '',
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': '',
'FinanceModuleAccess': '',
'HomeBranch': '',
'IsActive': ''
};
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("", response.data.message, "error");
}
});
}
}
$scope.addNewBranch = function () {
$scope.branchListView = false;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = true;
}
$scope.addNewBranchcancel = function() {
$scope.branchListView = true;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.empAddBranchDetails = {
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': '',
'FinanceModuleAccess': ''
}
}
$scope.empAddBranchDetails = {
'StaffID': '',
'ListCode': '',
'BranchCode': '',
'JobResponsibility': '',
'IsActive': '',
'FinanceModuleAccess': ''
}
$scope.addEmpBranchDetails = function(emp, form) {
if (form.$invalid) {
var field = null, firstError = null;
for (field in form) {
if (field[0] != '$') {
if (firstError === null && !form[field].$valid) {
firstError = form[field].$name;
}
if (form[field].$pristine) {
form[field].$dirty = true;
}
}
}
angular.element('.ng-invalid[name=' + firstError + ']').focus();
// swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error");
} else {
$scope.addEmpNewBranch = {
'empFor': emp,
'updateRole': $scope.empAddBranchDetails.ListCode,
'JobResponsibility': $scope.empAddBranchDetails.JobResponsibility,
'updateBranch': $scope.empAddBranchDetails.BranchCode,
'updateBy': localDetails.localUserID,
'FinanceModuleAccess': $scope.empAddBranchDetails.FinanceModuleAccess == true ? 1 : 0,
}
var req = {
method: 'POST',
url: apiPoint.url + 'addEmpBranchDetail/',
headers: {
'Content-Type': 'application/json'
},
data: $scope.addEmpNewBranch
};
$http(req).then(function (response) {
if (response.data.addEmpbranchStatus == true) {
swal("", "Added Successfully.", "success");
$scope.getEmployeeList();
$scope.getEmpBranchDetails(emp);
$scope.branchListView = true;
$scope.empEditBranchView = false;
$scope.empAddNewBranchView = false;
$scope.empAddBranchDetails = {
'empFor': emp,
'updateRole': $scope.empAddBranchDetails.ListCode,
'JobResponsibility': $scope.empAddBranchDetails.JobResponsibility,
'updateBranch': $scope.empAddBranchDetails.BranchCode,
'updateBy': localDetails.localUserID,
'FinanceModuleAccess': $scope.empAddBranchDetails.FinanceModuleAccess
}
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("", response.data.message, "error");
}
});
}
}
$scope.getRoleList = function () {
var req = {
method: 'POST',
url: apiPoint.url + 'getRoleDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
reqRoleID: localDetails.localType
}
};
$http(req).then(function (response) {
if (response.data.RoleDetailStatus) {
$scope.roleDetails = response.data.role_details;
} else {
}
});
}
$scope.getBranchDetails = function () {
var req = {
method: 'POST',
url: apiPoint.url + 'getMasterBranchDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
reqRoleID: localDetails.localType,
reqUserID: localDetails.localUserID,
branchId : localDetails.localBranchID,
}
};
$http(req).then(function (response) {
if (response.data.MeasterBranchDetailStatus == true) {
$scope.branchDetails = response.data.branch_details;
// alert(JSON.stringify($scope.branchDetails));
// $scope.branchDetailsName = [];
// $scope.BranchName = '';
// angular.forEach($scope.branchDetails, function (item, index) {
// // alert(item.BranchName);
// $scope.BranchName = item.BranchName + '-' + item.BranchCode;
// $scope.branchDetailsName.push($scope.BranchName);
// });
} else {
}
});
}
// check employee Id existing
$scope.checkEmpIdExist = function () {
var checkReq = {
method: 'POST',
url: apiPoint.url + 'chkEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: $scope.myModel.employeeId,
check: 'employeeId'
}
};
$http(checkReq).then(function (response) {
if (response.data.existEmpId == true) {
swal("", response.data.message, "warning");
$scope.myModel.employeeId = '';
} else {
}
});
}
// check mobile number existing
$scope.checkMobileExist = function () {
var checkReq = {
method: 'POST',
url: apiPoint.url + 'chkEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: $scope.myModel.primaryPhone,
check: 'mobileNumber'
}
};
$http(checkReq).then(function (response) {
if (response.data.existMobile == true) {
swal("", response.data.message, "warning");
$scope.myModel.primaryPhone = '';
} else {
}
});
}
// check emailId existing
$scope.checkEmailExist = function () {
var checkReq = {
method: 'POST',
url: apiPoint.url + 'chkEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: $scope.myModel.emailId,
check: 'emailId'
}
};
$http(checkReq).then(function (response) {
if (response.data.existEmailId == true) {
swal("", response.data.message, "warning");
$scope.myModel.emailId = '';
} else {
}
});
}
//-------------------------------------------------------------->
// getting for image to show
//-------------------------------------------------------------->
$scope.stepsModel = [];
$scope.imageUpload = function (event) {
var files = event.target.files; //FileList object
for (var i = 0; i < files.length; i++) {
var file = files[i];
var reader = new FileReader();
reader.onload = $scope.imageIsLoaded;
reader.readAsDataURL(file);
}
}
$scope.imageIsLoaded = function (e) {
$scope.$apply(function () {
// $scope.stepsModel.push(e.target.result);
$scope.stepsModel[0] = e.target.result;
});
}
//an array of files selected
$scope.files = [];
//listen for the file selected event
$scope.$on("fileSelected", function (event, args) {
$scope.$apply(function () {
//add the file object to the scope's files collection
$scope.files[0] = args.file;
});
});
// reset image
$scope.resetImage = function () {
$scope.files.length = 0;
$scope.stepsModel.length = 0;
}
// add empolyee
$scope.employeeForm = {
submit: function (form, myModel) {
// var formate = "yyyy-MM-dd'T'HH:mm:ssZ";
// var date = $filter('date')(new Date($scope.myModel.dateofbirth), formate);
// alert(date);
var firstError = null;
if (form.$invalid) {
var field = null, firstError = null;
for (field in form) {
if (field[0] != '$') {
if (firstError === null && !form[field].$valid) {
firstError = form[field].$name;
}
if (form[field].$pristine) {
form[field].$dirty = true;
}
}
}
angular.element('.ng-invalid[name=' + firstError + ']').focus();
// swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error");
} else {
// alert($scope.files);
// alert($scope.myModel.dateofbirth);
var formate = "dd-MM-yyyy";
// var date = $filter('date')(new Date($scope.myModel.dateofbirth), formate);
$scope.myModel.switchsetting = $scope.myModel.switchsetting === true ? 1 : 0;
$scope.myModel.dateofbirth = $filter('date')(new Date($scope.myModel.dateofbirth), formate);
$scope.myModel.dateofjoining = $filter('date')(new Date($scope.myModel.dateofjoining), formate);
// alert($scope.myModel.dateofbirth)
// alert(JSON.stringify($scope.myModel));
var idProof = $scope.files[0];
if (idProof !== undefined) {
var formData = new FormData();
var idProof = $scope.files[0];
// alert(idProof);
formData.append("data", JSON.stringify($scope.myModel));
formData.append("createdBy", localDetails.localUserID);
formData.append('idProof', idProof);
$http.post(apiPoint.url + 'insertEmployeeImage/', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined, 'Process-Data': false },
}).success(function (data) {
if (data.addEmpStatus == true) {
swal("Success!", data.message, "success");
$state.go($state.current, {}, { reload: true });
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", data.message, "error");
}
});
} else {
var req = {
method: 'POST',
url: apiPoint.url + 'insertEmployee/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: $scope.myModel,
createdBy: localDetails.localUserID,
},
};
$http(req).then(function (response) {
if (response.data.addEmpStatus == true) {
swal("Success!", "Employee details added successfully.", "success");
$state.go($state.current, {}, { reload: true });
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
}
},
reset: function (form) {
$scope.files.length = 0;
$scope.stepsModel.length = 0;
// $scope.myModel = angular.copy($scope.master);
form.$setPristine(true);
$scope.myModel = {
'employeeId': '',
'firstname': '',
'lastname': '',
'fathername': '',
'mothername': '',
'emailId': '',
'primaryPhone': '',
'secondaryPhone': '',
'dateofbirth': '',
'gender': '',
'qualificaion': '',
'aadharNumber': '',
'otherId': '',
'otherIdDetails': '',
'address1': '',
'address2': '',
'homeBranch': '',
'dateofjoining': '',
'dateofjoining': '',
'role': '',
'jobResponsibility': '',
'switchsetting': true,
'finaceModuleAccess': false
};
}
}
// update check mobile exist
$scope.updateCheckMobileExist = function (p) {
var checkReq = {
method: 'POST',
url: apiPoint.url + 'chkUpdateEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
exceptId: p.StaffID,
datas: p.MobileNumber,
check: 'mobileNumber'
}
};
$http(checkReq).then(function (response) {
if (response.data.existMobile == true) {
swal("", response.data.message, "warning");
p.MobileNumber = response.data.resetValue.MobileNumber;
} else {
}
});
}
$scope.updateCheckEmailExist = function (p) {
var checkReq = {
method: 'POST',
url: apiPoint.url + 'chkUpdateEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
exceptId: p.StaffID,
datas: p.EmailID,
check: 'emailId'
}
};
$http(checkReq).then(function (response) {
if (response.data.existEmailId == true) {
swal("", response.data.message, "warning");
p.EmailID = response.data.resetValue.EmailID;
} else {
}
});
}
$scope.hidden_DOB = '';
$scope.hidden_DOJ = '';
$scope.updateEmpDetails = function (form, p, hide_dob, hide_doj) {
var firstError = null;
if (form.$invalid) {
var field = null, firstError = null;
for (field in form) {
if (field[0] != '$') {
if (firstError === null && !form[field].$valid) {
firstError = form[field].$name;
}
if (form[field].$pristine) {
form[field].$dirty = true;
}
}
}
angular.element('.ng-invalid[name=' + firstError + ']').focus();
// swal("The form cannot be submitted because it contains validation errors!", "Errors are marked with a red, dashed border!", "error");
} else {
var formate = "dd-MM-yyyy";
// var checkDOB = $filter('date')(new Date(p.DOB), formate);
// var checkDOJ = $filter('date')(new Date(p.DOJ), formate);
// alert(checkDOB);
// alert(checkDOJ);
p.IsActive = p.IsActive === true ? 1 : 0;
p.DOB = p.DOB == hide_dob ? p.DOB : $filter('date')(new Date(p.DOB), formate);
p.DOJ = p.DOJ == hide_doj ? p.DOJ : $filter('date')(new Date(p.DOJ), formate);
var idProof = $scope.files[0];
// alert(idProof);
// alert(p.ProfilePicPath);
if (idProof === undefined) {
var req = {
method: 'POST',
url: apiPoint.url + 'updateEmpExistDetail/',
headers: {
'Content-Type': 'application/json'
},
data: {
data: p,
updatedBy: localDetails.localUserID,
}
};
$http(req).then(function (response) {
if (response.data.updateEmpStatus == true) {
swal("", "Updated Successfully.", "success");
$scope.getEmployeeList();
$scope.editId = -1;
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("", response.data.message, "error");
}
});
} else {
// update with image
var formData = new FormData();
var idProof = $scope.files[0];
// alert(idProof);
formData.append("data", JSON.stringify(p));
formData.append("updatedBy", localDetails.localUserID);
formData.append('idProof', idProof);
$http.post(apiPoint.url + 'updateEmpImgExistDetail/', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined, 'Process-Data': false },
}).success(function (data) {
if (data.updateEmpStatus == true) {
swal("", "Updated Successfully.", "success");
$scope.getEmployeeList();
$scope.editId = -1;
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("", data.message, "error");
}
});
}
}
}
}]);
// file upload directive
app.directive('fileUpload', function () {
return {
scope: true, //create a new scope
link: function (scope, el, attrs) {
el.bind('change', function (event) {
var files = event.target.files;
//iterate files since 'multiple' may be specified on the element
for (var i = 0; i < files.length; i++) {
//emit event upward
scope.$emit("fileSelected", { file: files[i] });
}
});
}
};
});