demo20230512/Browser_Local/js/Management/ManagementController.js
2023-05-12 10:20:28 +08:00

282 lines
11 KiB
JavaScript

var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
angular.module('CarbonFootprint')
.config(function ($routeProvider) {
$routeProvider
.when('/Management/Index', {
templateUrl: baseUrl+'/Partials/Management-Management',
controller: 'ManagementController'
});
}).config(function ($routeProvider) {
$routeProvider
.when('/Management/Index/:tab', {
templateUrl: baseUrl+'/Partials/Management-Management',
controller: 'ManagementController'
})
})
.controller('ManagementController',
['$scope', '$q', '$http', 'Redirect', 'SupplierFactory','MultiLanguageService','$routeParams',
function ($scope, $q, $http, Redirect, SupplierFactory, MultiLanguageService, $routeParams) {
var redirect = new Redirect();
var resource;
// set $scope.baseUrl
var baseUrl = $scope.baseUrl;
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
$scope.baseUrl = baseUrl;
}
if ($routeParams.tab) $scope.selectedTab = $routeParams.tab;
else $scope.selectedTab = 'management';
MultiLanguageService.getResourceAsync()
.then(function (response) {
resource = response;
}, function (error) {
console.log(error);
});
$scope.fileImportOption = {
show: false,
modelBuilder: SupplierFactory,
saveAsync: function (parsedList) {
var deferred = $q.defer();
$http.post($scope.baseUrl+'/api/Supplier/BatchCreate', parsedList).then(function (result) {
//$scope.supplierList = $scope.supplierList.concat(parsedList) //DL-9
$scope.supplierList = $scope.supplierList.concat(result.data);//DL-9
});
$scope.fileImportOption.show = false;//DL-9
return deferred.promise;
}
};
$scope.logoImportOption = {};
$scope.templateImportOption = {};
$q.all([$http.get($scope.baseUrl+"/api/User/GetByMyCompany"), $http.get($scope.baseUrl+"/api/Supplier/GetByMyCompany"), $http.get($scope.baseUrl+"/api/User/isCompanyAdmin")])
.then(function (response) {
$scope.userList = response[0].data;
$scope.supplierList = response[1].data;
$scope.isCompanyAdmin = response[2].data;
//console.log($scope.isCompanyAdmin);
});
$scope.importSupplierProcess = function () {
$scope.fileImportOption.show = true;
};
$scope.editUser = function (user) {
redirect.goToEditUser(user.Id);
};
$scope.editSupplier = function (supplier) {
redirect.goToEditSupplier(supplier.ID);
};
$scope.goToUserCreate = function () {
redirect.goToCreateUser();
};
$scope.isCompanyAdmin = function () {
return $scope.isCompanyAdmin;
};
$scope.goToSupplierCreate = function () {
redirect.goToCreateSupplier();
};
$scope.uploadCompanyLogo = function () {
var selectedFile;
let p = document.getElementById("upload-logo-result-msg");
if (p != null)
p.innerHTML = "";
let f = document.getElementById("upload-logo-file");
if (f != null)
f.value = null;
$scope.logoImportOption.show = true;
$scope.logoImportOption.selectFile = function (file) {
selectedFile = file;
};
$scope.uploadLogo = function () {
var formData = new FormData();
formData.append('file', selectedFile);
if (p != null)
p.innerHTML = "";
// to do: Should refactor
$http.post($scope.baseUrl+'/api/Upload', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).success(function (data) {
var imgUrl = baseUrl + data[0];
//console.log('imgUrl 1', imgUrl);
$http.get($scope.baseUrl+'/api/Company/Get').success(function (data) {
var currentCompany = data;
//console.log('imgUrl 2', imgUrl);
currentCompany.LogoUrl = imgUrl;
$http.post($scope.baseUrl+'/api/Company/Save/' + currentCompany.ID, currentCompany).success(function (data) {
if (p != null) {
p.innerHTML = resource['UploadFileDone'];
p.style.color = "green";
//console.log('imgUrl 3', imgUrl);
angular.element('.survey_form_logo img').attr('src', imgUrl);
$http.post($scope.baseUrl+'/Profile/SetSession/', { sessionName: 'userLogoImg', sessionValue: currentCompany.LogoUrl }).success(function (data) {
});
}
}).error(function (data, status) {
if (p != null) {
p.innerHTML = resource['UploadFileFail'];
p.style.color = "red";
}
console.log(status);
});
});
}).error(function (data, status) {
if (p != null) {
p.innerHTML = resource['UploadFileFail'];
p.style.color = "red";
}
console.log(status);
});
};
};
$scope.uploadCompanyTemplates = function () {
var selectedFiles;
let p = document.getElementById("upload-file-result-msg");
if (p != null)
p.innerHTML = "";
let f = document.getElementById("upload-file-file");
if (f != null)
f.value = null;
$scope.templateImportOption.show = true;
$scope.templateImportOption.selectFile = function (files) {
selectedFiles = files;
};
$scope.uploadTemplates = function () {
var formData = new FormData();
formData.append('file', selectedFiles);
if (p != null)
p.innerHTML = "";
// to do: Should refactor
$http.post($scope.baseUrl+'/api/UploadUserFiles', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).success(function (data) {
if (data != null) {
if (p != null) {
p.innerHTML = data.Msg != null ? data.Msg : "";
if (data.Success == true) {
p.style.color = "green";
}
else {
p.style.color = "red";
}
}
}
}).error(function (data, status) {
if (p != null) {
p.innerHTML = resource['UploadFileFail'];
p.style.color = "red";
}
console.log(status);
});
};
};
$scope.createUser = function (ps) {
$scope.isCheckOnce = true;
if (this.form.$valid && $scope.password_match && $scope.correctUserName &&
!this.form.uPhoneNumber.$$validityState.patternMismatch &&
!this.form.uEmail.$error.email) {
var id = null;
ps.UserSelected.DefaultPassword = ps.UserSelected.Password;
if (typeof ps.UserSelected.Id != 'undefined' && ps.UserSelected.Id != null) id = ps.UserSelected.Id;
Save(MyDataService, ps.UserSelected, 'User', 'Save').then(function (response) {
if (mergeNumber(response) != -1) {
redirect.backToManagement();
} else {
$scope.account_repeat = true;
}
});
}
};
$scope.exportSupplier = function (list) {
var csv = '\uFEFF';
var link;
// header
csv += 'Vendor Code' + ','
+ resource['SupplierName'] + ','
+ resource['StaticLabelAddress'] + ','
+ resource['PhoneNumber'] + ','
+ resource['StaticLabelContactName'] + ','
+ resource['StaticLabelContactEmail'] + ','
+ resource['StaticLabelContactPhone'] + ','
+ resource['StaticLabelGlobal_Description']
// body
angular.forEach(list, function (entry) {
csv += '\r\n';
//column 1
csv += entry.VendorCode + ',';
//column 2
csv += entry.Name + ',';
//column 3
csv += entry.Address + ',';
//column 4
csv += entry.Phone + ',';
//column 5
csv += entry.ContactName + ',';
//column 6
csv += entry.ContactEmail + ',';
//column 7
csv += entry.ContactPhone + ',';
//column 8
csv += entry.Description
});
require([baseUrl+'/Scripts/FileSaver.js'],
function requrieSuccess(SaveAs) {
var textFileAsBlob = new Blob([csv], { type: 'text;charset=utf-8,' });
saveAs(textFileAsBlob, "Supplier.csv")
},
function requireError(error) {
}
);
}
}]);