apollodec/Apollo/assets/js/controllers/courseCtrl.js
2017-11-30 19:33:38 +05:30

310 lines
10 KiB
JavaScript

'use strict';
/**
* controllers for ng-table
* Simple table with sorting and filtering on AngularJS
*/
app.controller('courseCtrl', ["$scope","$rootScope","toaster", "$filter", "ngTableParams", "API_POINTS", "$localStorage", "$http", "$state", function ($scope,$rootScope,toaster, $filter, ngTableParams, apiPoint, $localStorage, $http, $state) {
$scope.myModel = {
"courseCode": "",
"courseName": "",
"universityName":"",
"switchsetting": true,
"ListName": "",
"programListName":"",
"semYear":"",
"feesAmount":"",
"provFess":"",
"transferAmount":"",
"degreeAmount":"",
"migrationAmount":"",
"otherAmount":""
};
$rootScope.myModel1 = {
"ListName": "",
"programListName":""
};
/*
* edit course details
* created by kms
* */
$scope.editId = -1;
$rootScope.setEditId = function (pid,p) {
// find index for fees type drop down list
for(var i in $scope.feesDetails) {
if($scope.feesDetails[i].ListCode==p.FeesType){
$rootScope.myModel1.ListName=$scope.feesDetails[i];
}
}
$scope.editId = pid;
};
/*
* cancel edit div
* created by kms
* */
$scope.cancel = function () {
$rootScope.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
}
/*
* Submit,update and reset course details
* @params myModel
* created by kms
* */
$scope.courseForm = {
submit: function (form, myModel,loading,addMoreItems) {
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 {
if(myModel.programListName=='P001'){
$scope.addMoreItems.push({
program: myModel.programListName,
FeesName: 'Regular',
FeesAmount: myModel.feesAmount
});
}
else if(myModel.programListName=='P002'){
$scope.addMoreItems.push({
program: myModel.programListName,
FeesName: 'Lateral Entry',
FeesAmount: myModel.feesAmount
});
}
else if(myModel.programListName=='Y001'){
$scope.addMoreItems.push({
program: myModel.programListName,
FeesName: myModel.semYear,
FeesAmount: myModel.feesAmount
});
}
$scope.ldloading1 = {};
$scope.ldloading1[loading.replace('-', '_')] = true;
var addCourse = {
method: 'POST',
url: apiPoint.url + 'addCourseDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
courseID: myModel.courseCode,
courseName: myModel.courseName,
universityName: myModel.universityName,
feesType:myModel.ListName.ListCode,
status: myModel.switchsetting,
provFess:myModel.provFess,
transferAmount:myModel.transferAmount,
degreeAmount:myModel.degreeAmount,
migrationAmount:myModel.migrationAmount,
otherAmount:myModel.otherAmount,
feesAmounts:$scope.addMoreItems,
createdBy: JSON.parse(localStorage.getItem('localObj')).localUserID
}
};
$http(addCourse).then(function (response) {
if (response.data.status==200 && response.data.courseStatus) {
$scope.ldloading1[loading.replace('-', '_')] = false;
swal("Success!", response.data.message, "success");
$state.go($state.current, {}, {reload: true});
} else {
$scope.addMoreItems.splice($scope.addMoreItems.length-1);
$scope.ldloading1[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
},
reset: function (form) {
$scope.addMoreItems = [];
$scope.myModel.ListName=$scope.feesDetails[0];
$scope.myModel.programListName=$scope.myModel.ListName.programType[0].ListCode;
$scope.myModel = angular.copy($scope.master);
form.$setPristine(true);
}
};
/*
* update the course details
* created by kms
* */
$scope.updateCourse = 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 update = {
method: 'POST',
url: apiPoint.url + 'updateCourseDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
courseCode: myModel.CourseID,
courseName: myModel.CourseName,
provFess:myModel.PC,
transferAmount:myModel.TC,
degreeAmount:myModel.DC,
migrationAmount:myModel.MC,
otherAmount:myModel.OtherFees,
feesAmounts:myModel.feesStructures,
status: myModel.IsActive,
updatedBy: JSON.parse(localStorage.getItem('localObj')).localUserID
}
};
$http(update).then(function (response) {
if (response.data.status==200 && response.data.courseStatus) {
$scope.ldloading[loading.replace('-', '_')] = false;
swal("Success!", response.data.message, "success");
$scope.editId = -1;
$rootScope.init();
} else {
$scope.ldloading[loading.replace('-', '_')] = false;
swal("Failed!", response.data.message, "error");
}
});
}
};
/*
*get course details when page loading called from view file
*
* created by kms
* */
$scope.loader='';
$scope.emptyData='';
$rootScope.init = function () {
$scope.loader='';
var getCourse = {
method: 'POST',
url: apiPoint.url + 'getCourseDetails/',
headers: {
'Content-Type': 'application/json'
},
data: {
requestedBy: JSON.parse(localStorage.getItem('localObj')).localUserID
}
};
$http(getCourse).then(function (response) {
if (response.data.status==200 && response.data.courseStatus) {
$scope.emptyData=true;
$scope.loader=true;
$scope.data = response.data.details;
$scope.university=response.data.universityDetails;
$scope.feesDetails=response.data.feesDetails;
$scope.myModel.ListName=$scope.feesDetails[0];
$scope.myModel.programListName=$scope.myModel.ListName.programType[0].ListCode;
} else {
$scope.emptyData=false;
$scope.loader=true;
$scope.university=response.data.universityDetails;
$scope.feesDetails=response.data.feesDetails;
$scope.myModel.ListName=$scope.feesDetails[0];
$scope.myModel.programListName=$scope.myModel.ListName.programType[0].ListCode;
}
});
};
/*
* get fees type drop down
* created by kms
* */
$scope.getFeesProgram = function () {
$scope.myModel.programListName=$scope.myModel.ListName.programType[0].ListCode;
$scope.addMoreItems = [];
}
/*
* get add more fees item array
* created by kms
* */
$scope.addMoreItems = [];
$rootScope.addMoreFeesItems = function (model,form) {
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.addMoreItems.push({
program: model.programListName,
FeesName: model.semYear,
FeesAmount: model.feesAmount
});
$scope.myModel.semYear="";
$scope.myModel.feesAmount="";
}
}
}]);