apollodec/Apollo/assets/js/controllers/employeeCtrl.js
2017-11-24 12:19:04 +05:30

511 lines
18 KiB
JavaScript

'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', function ($scope, toaster, $filter, ngTableParams, $http, apiPoint, $state, $rootScope, $cookies, md5, ipCookie, $timeout, $window) {
/**
* 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': '',
'switchsetting': true,
};
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.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.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.getEmpBranchDetails = function (id) {
// alert(id);
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.empBranchs = response.data.branch_details;
$scope.branchArr = [];
angular.forEach($scope.empBranchs, function (item, index) {
// alert(item.BranchName);
$scope.branchArr.push(item.BranchName);
});
$scope.empBranchDetails = {
arr: $scope.branchArr,
}
$scope.empRoleDetail = response.data.role_details[0];
} else {
}
});
}
$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,
}
};
$http(req).then(function (response) {
if (response.data.MeasterBranchDetailStatus == true) {
$scope.branchDetails = response.data.branch_details;
$scope.branchDetailsName = [];
angular.forEach($scope.branchDetails, function (item, index) {
// alert(item.BranchName);
$scope.branchDetailsName.push(item.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("Warning!", 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("Warning!", 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("Warning!", response.data.message, "warning");
$scope.myModel.emailId = '';
} else {
}
});
}
// 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.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)
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!", response.data.message, "success");
$state.go($state.current, {}, { reload: true });
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
},
reset: function (form) {
// $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': '',
'switchsetting': true,
};
}
}
// 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("Warning!", 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("Warning!", response.data.message, "warning");
p.EmailID = response.data.resetValue.EmailID;
} else {
}
});
}
$scope.updateEmpDetails = function (form, p) {
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(p.DOB);
var formate = "dd-MM-yyyy";
var checkDOB = $filter('date')(new Date(p.DOB), formate);
var checkDOJ = $filter('date')(new Date(p.DOJ), formate);
p.IsActive = p.IsActive === true ? 1 : 0;
p.DOB = checkDOB == "NaN-NaN-0NaN" ? p.DOB : $filter('date')(new Date(p.DOB), formate);
p.DOJ = checkDOJ == "NaN-NaN-0NaN" ? p.DOJ : $filter('date')(new Date(p.DOJ), formate);
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("Success!", response.data.message, "success");
$scope.getEmployeeList();
$scope.editId = -1;
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
}
$scope.updateEmpBranchDetails = function (form, empRole, empBranchs, p) {
// alert(empBranchs.arr.length);
if (empBranchs.arr.length >= 1) {
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(JSON.stringify(p));
// alert(JSON.stringify(empRole));
// alert(JSON.stringify(empBranchs));
$scope.sendBranchData = {
'empFor': p.StaffID,
'updateRole': empRole.ListCode,
'updateBranch': empBranchs.arr,
'updateBy': localDetails.localUserID
}
// 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("Success!", response.data.message, "success");
$scope.getEmployeeList();
$scope.editBranchId = -1;
} else {
// $scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
} else {
swal("Warning!", "should select atleast branch!!", "warning");
}
}
}]);