427 lines
15 KiB
JavaScript
Executable File
427 lines
15 KiB
JavaScript
Executable File
'use strict';
|
|
/**
|
|
* controllers for ng-table
|
|
* Simple table with sorting and filtering on AngularJS
|
|
*/
|
|
|
|
app.controller('branchCtrl', ["$scope","toaster", "$filter", "ngTableParams", "API_POINTS", "$localStorage", "$http", "$state", function ($scope,toaster, $filter, ngTableParams, apiPoint, $localStorage, $http, $state) {
|
|
$scope.myModel = {
|
|
"branchCode": "",
|
|
"branchName": "",
|
|
"email": "",
|
|
"primaryPhone": "",
|
|
"secondaryPhone": "",
|
|
"landLine": "",
|
|
"address": "",
|
|
"logo": "",
|
|
"switchsetting": true
|
|
};
|
|
|
|
/*
|
|
* edit branch details
|
|
* created by kms
|
|
* */
|
|
$scope.editId = -1;
|
|
$scope.setEditId = function (pid) {
|
|
|
|
$scope.editId = pid;
|
|
$scope.viewId = -1;
|
|
};
|
|
|
|
/*
|
|
* cancel edit div
|
|
* created by kms
|
|
* */
|
|
$scope.cancel = function () {
|
|
$scope.init();
|
|
$scope.editId = -1;
|
|
|
|
};
|
|
|
|
// 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
|
|
}
|
|
|
|
/* $scope.viewId = -1;
|
|
$scope.viewMore = function (pid) {
|
|
$scope.viewId = pid;
|
|
$scope.editId = -1;
|
|
};*/
|
|
|
|
|
|
//-------------------------------------------------------------->
|
|
// 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;
|
|
}
|
|
|
|
|
|
/*
|
|
* Submit,update and reset branch details
|
|
* @params myModel
|
|
* created by kms
|
|
* */
|
|
$scope.branchForm = {
|
|
submit: function (form, myModel,loading) {
|
|
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 {
|
|
$scope.ldloading1 = {};
|
|
$scope.ldloading1[loading.replace('-', '_')] = true;
|
|
|
|
var formData = new FormData();
|
|
var logo = $scope.files[0];
|
|
|
|
formData.append("branchCode", myModel.branchCode);
|
|
formData.append("branchName", myModel.branchName);
|
|
formData.append("email", myModel.email);
|
|
formData.append("primaryPhone", myModel.primaryPhone);
|
|
formData.append("secondaryPhone", myModel.secondaryPhone);
|
|
formData.append("landLine", myModel.landLine);
|
|
formData.append("address", myModel.address);
|
|
formData.append("status", myModel.switchsetting);
|
|
|
|
var checkImg = logo == undefined ? 0 : 1;
|
|
formData.append("imageStatus", checkImg);
|
|
|
|
formData.append("createdBy", JSON.parse(localStorage.getItem('localObj')).localUserID);
|
|
formData.append('logo', logo);
|
|
|
|
$http.post(apiPoint.url + 'addBranchDetails/', formData, {
|
|
transformRequest: angular.identity,
|
|
headers: { 'Content-Type': undefined, 'Process-Data': false },
|
|
}).success(function (data) {
|
|
if (data.status==200 && data.branchStatus) {
|
|
$scope.ldloading1[loading.replace('-', '_')] = false;
|
|
swal("", "Branch added successfully.", "success");
|
|
$state.go($state.current, {}, {reload: true});
|
|
} else {
|
|
$scope.ldloading1[loading.replace('-', '_')] = false;
|
|
swal("", data.message, "error");
|
|
}
|
|
});
|
|
|
|
// var addBranch = {
|
|
// method: 'POST',
|
|
// url: apiPoint.url + 'addBranchDetails/',
|
|
// headers: {
|
|
// 'Content-Type': 'application/json'
|
|
// },
|
|
// data: {
|
|
// branchCode: myModel.branchCode,
|
|
// branchName: myModel.branchName,
|
|
// email: myModel.email,
|
|
// primaryPhone: myModel.primaryPhone,
|
|
// secondaryPhone: myModel.secondaryPhone,
|
|
// landLine: myModel.landLine,
|
|
// address: myModel.address,
|
|
// logo: myModel.logo,
|
|
// status: myModel.switchsetting,
|
|
// createdBy: JSON.parse(localStorage.getItem('localObj')).localUserID
|
|
// }
|
|
// };
|
|
// $http(addBranch).then(function (response) {
|
|
// if (response.data.status==200 && response.data.branchStatus) {
|
|
// $scope.ldloading1[loading.replace('-', '_')] = false;
|
|
// 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 = {
|
|
"branchcode": "",
|
|
"branchname": "",
|
|
"email": "",
|
|
"primaryPhone": "",
|
|
"secondaryPhone": "",
|
|
"landLine": "",
|
|
"address": "",
|
|
"status": "",
|
|
"logo": "",
|
|
"switchsetting": true
|
|
};
|
|
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.stepsModelEdit = [];
|
|
$scope.imageUploadEdit = 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.imageIsLoadedEdit;
|
|
reader.readAsDataURL(file);
|
|
}
|
|
}
|
|
|
|
$scope.imageIsLoadedEdit = function (e) {
|
|
$scope.$apply(function () {
|
|
// $scope.stepsModel.push(e.target.result);
|
|
$scope.stepsModelEdit[0] = e.target.result;
|
|
});
|
|
}
|
|
|
|
//an array of files selected
|
|
$scope.filesEdit = [];
|
|
//listen for the file selected event
|
|
$scope.$on("fileSelectedEdit", function (event, args) {
|
|
$scope.$apply(function () {
|
|
//add the file object to the scope's files collection
|
|
$scope.filesEdit[0] = args.file;
|
|
});
|
|
});
|
|
|
|
|
|
// reset image for Edit
|
|
$scope.resetImageEdit = function () {
|
|
$scope.filesEdit.length = 0;
|
|
$scope.stepsModelEdit.length = 0;
|
|
}
|
|
|
|
/*
|
|
* update the branch details
|
|
* created by kms
|
|
* */
|
|
$scope.updateBranch = function (myModel, form, loading) {
|
|
|
|
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].$stop_name;
|
|
}
|
|
|
|
if (form[field].$pristine) {
|
|
form[field].$dirty = true;
|
|
}
|
|
}
|
|
}
|
|
angular.element('.ng-invalid[name=' + firstError + ']').focus();
|
|
} else {
|
|
$scope.ldloading = {};
|
|
|
|
$scope.ldloading[loading.replace('-', '_')] = true;
|
|
|
|
var formData = new FormData();
|
|
var logoEdit = $scope.filesEdit[0];
|
|
|
|
formData.append("branchCode", myModel.BranchCode);
|
|
formData.append("branchName", myModel.BranchName);
|
|
formData.append("email", myModel.EmailID);
|
|
formData.append("primaryPhone", myModel.MobileNumber);
|
|
formData.append("secondaryPhone", myModel.AlternateNumber);
|
|
formData.append("landLine", myModel.LandlineNumber);
|
|
formData.append("address", myModel.Address);
|
|
formData.append("status", myModel.IsActive);
|
|
formData.append("logo", myModel.LogoPath);
|
|
|
|
var checkImg = logoEdit == undefined ? 0 : 1;
|
|
formData.append("imageStatus", checkImg);
|
|
|
|
formData.append("createdBy", JSON.parse(localStorage.getItem('localObj')).localUserID);
|
|
formData.append('logoEdit', logoEdit);
|
|
|
|
$http.post(apiPoint.url + 'updateBranchDetails/', formData, {
|
|
transformRequest: angular.identity,
|
|
headers: { 'Content-Type': undefined, 'Process-Data': false },
|
|
}).success(function (data) {
|
|
if (data.status==200 && data.branchStatus) {
|
|
$scope.ldloading[loading.replace('-', '_')] = false;
|
|
swal("", "Updated Successfully.", "success");
|
|
$scope.editId = -1;
|
|
$scope.init();
|
|
} else {
|
|
$scope.ldloading1[loading.replace('-', '_')] = false;
|
|
swal("", data.message, "error");
|
|
}
|
|
});
|
|
|
|
// var update = {
|
|
// method: 'POST',
|
|
// url: apiPoint.url + 'updateBranchDetails/',
|
|
// headers: {
|
|
// 'Content-Type': 'application/json'
|
|
// },
|
|
// data: {
|
|
// branchCode: myModel.BranchCode,
|
|
// branchName: myModel.BranchName,
|
|
// email: myModel.EmailID,
|
|
// primaryPhone: myModel.MobileNumber,
|
|
// secondaryPhone: myModel.AlternateNumber,
|
|
// landLine: myModel.LandlineNumber,
|
|
// address: myModel.Address,
|
|
// logo: myModel.LogoPath,
|
|
// status: myModel.IsActive,
|
|
// createdBy: JSON.parse(localStorage.getItem('localObj')).localUserID
|
|
// }
|
|
// };
|
|
// $http(update).then(function (response) {
|
|
// if (response.data.status==200 && response.data.branchStatus) {
|
|
// $scope.ldloading[loading.replace('-', '_')] = false;
|
|
// swal("Success!", response.data.message, "success");
|
|
// $scope.editId = -1;
|
|
// $scope.init();
|
|
|
|
// } else {
|
|
// $scope.ldloading[loading.replace('-', '_')] = false;
|
|
// swal("Failed!", response.data.message, "error");
|
|
// }
|
|
// });
|
|
}
|
|
};
|
|
/*
|
|
*get branch details when page loading called from view file
|
|
*
|
|
* created by kms
|
|
* */
|
|
$scope.loader='';
|
|
$scope.branchInfo='';
|
|
$scope.emptyData='';
|
|
|
|
|
|
$scope.init = function () {
|
|
|
|
var getBranch = {
|
|
method: 'POST',
|
|
url: apiPoint.url + 'getBranchDetails/',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
data: {
|
|
requestedBy: JSON.parse(localStorage.getItem('localObj')).localUserID
|
|
}
|
|
};
|
|
$http(getBranch).then(function (response) {
|
|
if (response.data.status==200 && response.data.branchStatus) {
|
|
$scope.emptyData=true;
|
|
$scope.loader=true;
|
|
// var data = response.data.details;
|
|
$scope.data = response.data.details;
|
|
// this for search box
|
|
// $scope.search = {BranchCode: ''};
|
|
// $scope.tableParams = new ngTableParams({
|
|
// page: 1,
|
|
// count: 10,
|
|
// filter: $scope.search
|
|
// }, {
|
|
// total: data.length,
|
|
// getData: function ($defer, params) {
|
|
// var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
|
|
// orderedData = params.filter() ? $filter('filter')(orderedData, params.filter().BranchCode) : orderedData;
|
|
// $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
|
|
// }
|
|
// });
|
|
} else {
|
|
$scope.emptyData=false;
|
|
$scope.loader=true;
|
|
|
|
$scope.branchInfo='';
|
|
|
|
}
|
|
});
|
|
};
|
|
|
|
}]);
|
|
|
|
// 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] });
|
|
}
|
|
});
|
|
}
|
|
};
|
|
});
|
|
|
|
// file upload directive
|
|
app.directive('fileUploadEdit', 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("fileSelectedEdit", { file: files[i] });
|
|
}
|
|
});
|
|
}
|
|
};
|
|
});
|