(function() { "use strict"; var serviceId = "licencingAdminDataContext"; var app = angular.module("licencingAdmin"); function datacontext( $q, $http, config, $modal, orgAdminService, paymentsDataContext, $location ) { var currentLicense; var service = { getCurrentLicense: getCurrentLicense, showSelectorModal: showSelectorModal, showUpgradeModal: showUpgradeModal, showCancelModal: showCancelModal, showCancelDowngradeModal: showCancelDowngradeModal, showReenableModal: showReenableModal, showRestartModal: showRestartModal }; return service; function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if (!angular.isObject(response.data) || !response.data.message) { return $q.reject("An unknown error occurred."); } // Otherwise, use expected error message. return $q.reject(response.data.message); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return response.data; } function showSelectorModal(planId, userAmount) { var modalOptions = { templateUrl: config.templatePath + "licenceSelectorModal.html?version=160718", controller: "licenceSelectorModalController as vm", controllerAs: "vm", size: "sm", resolve: { planId: function() { return planId; }, userAmount: function() { return userAmount; } } }; var modalInstance = $modal.open(modalOptions); modalInstance.result.then(function(result) { currentLicense = false; return getCurrentLicense(); }); } function showUpgradeModal() { var path = config.templatePath + "licenceUpgradeQuantityModal.html?version=160718"; if (appCode == "OB") { path = config.templatePath + "OBlicenceUpgradeQuantityModal.html?version=160718"; } var modalOptions = { templateUrl: path, controller: "licenceUpgradeQuantityModalController as vm", controllerAs: "vm", size: "sm" }; var modalInstance = $modal.open(modalOptions); return modalInstance.result.then(function(result) {}); } function showCancelModal() { var modalOptions = { templateUrl: config.templatePath + "licenceCancelModal.html?version=160718", controller: "licenceCancelModalController as vm", controllerAs: "vm", size: "sm" }; var modalInstance = $modal.open(modalOptions); return modalInstance.result.then(function(result) {}); } function showCancelDowngradeModal() { var modalOptions = { templateUrl: config.templatePath + "licenceCancelDowngradeModal.html?version=160718", controller: "licenceCancelDowngradeModalController as vm", controllerAs: "vm", size: "sm" }; var modalInstance = $modal.open(modalOptions); return modalInstance.result.then(function(result) {}); } function showReenableModal() { var modalOptions = { templateUrl: config.templatePath + "licenceReenableModal.html?version=160718", controller: "licenceReenableModalController as vm", controllerAs: "vm", size: "sm" }; var modalInstance = $modal.open(modalOptions); return modalInstance.result.then(function(result) {}); } function showRestartModal() { var modalOptions = { templateUrl: config.templatePath + "licenceRestartModal.html?version=160718", controller: "licenceRestartModalController as vm", controllerAs: "vm", size: "sm" }; var modalInstance = $modal.open(modalOptions); return modalInstance.result.then(function(result) {}); } function getCurrentLicense(refresh) { if (!currentLicense || refresh) { return orgAdminService.getTopLevelOrg().then(function(data) { if (data) { if (data.type == 0) { return getOrganisationLicences(data.id).then( function(data) { if (!data || data.length === 0) { return; } var licences = []; for (i in data) { licences[data[i].featureName] = data[i]; } return (currentLicense = licences); }, function(ex) { console.log(ex); return ex; } ); } } }); } return $q.resolve(currentLicense); } function getOrganisationLicences(orgId) { var request = $http({ method: "get", url: config.licencingRemoteServiceUrl + orgId + "/applications/" + config.applicationId + "/licence" + '?cache=' + Date.now() }); return request.then(handleSuccess, handleError); } } app.factory(serviceId, [ "$q", "$http", "licencingAdminConfig", "$modal", "OrganisationAdminService", "paymentsDataContext", datacontext ]); function licenceSelectorModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, $q, auth, planId, userAmount ) { var vm = this; console.log(vm); vm.currencyOptions = []; vm.originalCurrencyOptions = []; activate(); vm.originalCountryCode = ""; vm.originalAccountType = ""; function activate() { $q .all([ auth.getCurrentUser(), paymentsDataContext.getPlans(), paymentsDataContext.getCurrencyOptions() ]) .then(function(data) { vm.user = data[0].data; var filtered = data[1].filter(function(item) { return !item.isFreePlan; }); vm.allAccountOptions = filtered; var preselectedAccountType = "Government / For-profit"; if (vm.user.registerAccountType === "1") { preselectedAccountType = "Academic / Charity"; } vm.originalAccountType = vm.user.registerAccountType; vm.selectedAccount = filtered.filter(function(item) { return item.name === preselectedAccountType; })[0]; vm.currencyOptions = data[2]; vm.originalCurrencyOptions = data[2]; vm.originalCountryCode = vm.user.registerCountry; vm.countryChanged(); vm.currencyChanged(); if (planId) { vm.selectedAccount = filtered.filter(function(item) { return item.PlanId === planId; })[0]; vm.accountOptions = vm.allAccountOptions.filter(function(item) { return item.currency === vm.selectedAccount.currency; }); userAmount = parseFloat(userAmount); vm.currentCost = 0; vm.selectedUsers = userAmount; vm.checkCost(); $modalInstance.close(); paymentsDataContext.showStripeForNewAccount( vm.selectedAccount, vm.user.email, userAmount, vm.currentCost, vm.noVat, vm.euVAT ); } }); } var euCountriesCodes = [ "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "UK" ]; function isCountryEU(countryCode) { if (countryCode == "UK") return false; //we can ignore UK for now as this is treated a bit differently since we don't current need VAT registration numbers and such if (euCountriesCodes.indexOf(countryCode) !== -1) return true; else return false; } function getSelectedCurrency(countryCode, isEU) { // we can limit the amount of currencies if the user is in one of the countries we know the currnecy for (e.g uk us or an eu country). // we can then disable the selector on the modal with this value preset. if (countryCode == "GB") { vm.currencyOptions = [vm.originalCurrencyOptions[0]]; } else if (isEU) { vm.currencyOptions = [vm.originalCurrencyOptions[2]]; } else { vm.currencyOptions = [vm.originalCurrencyOptions[1]]; } return vm.currencyOptions[0]; } vm.countryChanged = function() { vm.euVATRequired = isCountryEU(vm.user.registerCountry); vm.selectedCurrency = getSelectedCurrency( vm.user.registerCountry, vm.euVATRequired ); vm.currencyChanged(); }; vm.currencyChanged = function() { vm.accountOptions = vm.allAccountOptions.filter(function(item) { return item.currency === vm.selectedCurrency.code; }); vm.selectedAccount = vm.accountOptions.filter(function(item) { return item.name === vm.selectedAccount.name; })[0]; vm.accountChanged(); vm.checkCost(); if (vm.selectedCurrency.code === "usd") { vm.noVat = true; } else { vm.noVat = false; } }; vm.currentCost = 0; vm.checkCost = function() { var currentUsers = vm.selectedUsers; if (!currentUsers) { currentUsers = vm.selectedAccount.minUsers; } vm.currentCost = currentUsers * (vm.selectedAccount.cost * 1.0 / 100) * 100; if (vm.currentCost < 100) { vm.belowMinimum = true; vm.currentCost = 100; } else { vm.belowMinimum = false; } }; vm.accountChanged = function() { if (!vm.selectedUsers) { vm.selectedUsers = vm.selectedAccount.minUsers; } if (vm.selectedUsers > vm.selectedAccount.maxUsers) { vm.selectedUsers = vm.selectedAccount.maxUsers; } if (vm.selectedUsers < vm.selectedAccount.minUsers) { vm.selectedUsers = vm.selectedAccount.minUsers; } vm.checkCost(); }; vm.ok = function(result) { vm.checkCost(); $modalInstance.close(); if ( vm.originalCountryCode != vm.user.registerCountry || vm.originalAccountType != vm.user.registerAccountType ) { //TODO: change profile details. } return paymentsDataContext.showStripeForNewAccount( vm.selectedAccount, vm.user.email, vm.selectedUsers, vm.currentCost, vm.noVat, vm.euVAT ); }; vm.checkVAT = function() { var result = jsvat.checkVAT(vm.euVAT); vm.euVATSuccess = result.isValid; vm.noVat = result.isValid; vm.checkedVAT = true; }; } function licenceUpgradeQuantityModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, oas, lc, $q, auth ) { var vm = this; vm.users = 50; vm.previewed = false; vm.showDetails = false; vm.currency = "£"; vm.activeUsers = 55; vm.Math = Math; vm.ready = true; activate(); function activate() { $q .all([ auth.getCurrentUser(), paymentsDataContext.getCurrentPaymentDetails(), oas.getCurrentUserCount() ]) .then(function(data) { vm.user = data[0].data; if (vm.user.paymentVatPercent === "0") { vm.noVat = true; } else { vm.noVat = false; } vm.selectedPlan = data[1]; vm.users = vm.selectedPlan.quantity + 1; vm.currentUsers = vm.selectedPlan.quantity; vm.currency = vm.selectedPlan.currencySymbol; vm.usersChanged(); vm.activeUsers = data[2]; vm.ready = true; }); } vm.toggleBreakdown = function() { vm.showDetails = !vm.showDetails; }; vm.usersChanged = function() { var currentUsers = vm.users; if (!currentUsers) { currentUsers = Math.max(vm.selectedPlan.minUsers, vm.activeUsers); } vm.fixedUsers = currentUsers; vm.previewed = false; }; vm.preview = function() { vm.previewedNumberOfUsers = vm.users; vm.previewed = false; if (vm.users < vm.selectedPlan.MinQuantity) { vm.users = vm.selectedPlan.MinQuantity; } if (vm.users > vm.selectedPlan.MaxQuantity) { vm.users = vm.selectedPlan.MaxQuantity; } if (vm.users < vm.activeUsers) { vm.users = vm.activeUsers; } return paymentsDataContext .previewPlanUpgrade({ planId: vm.selectedPlan.id, properties: { Users: vm.users } }) .then(function(data) { vm.previewed = true; vm.nextInvoice = data.Results.NextInvoice.SubTotal; if (vm.nextInvoice < 100) { vm.belowMinimum = true; vm.nextInvoice = 100; } else { vm.belowMinimum = false; } vm.breakdown = data.Results.NextInvoice.Breakdown; vm.normalPrice = data.Results.NormalInvoice; vm.nextInvoiceDate = data.Results.PaymentAttemptDate; vm.token = data.Token; }); }; vm.cancelAccount = function() { $modalInstance.close(); return lc.showCancelModal(); }; vm.cancel = function() { $modalInstance.close(); }; vm.confirm = function() { $modalInstance.close(); fsl.show("Processing upgrade"); return paymentsDataContext.planUpgrade(vm.token); }; } function licenceCancelModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, oas ) { var vm = this; activate(); function activate() {} vm.cancel = function() { $modalInstance.close(); }; vm.confirm = function() { $modalInstance.close(); fsl.show("Processing cancellation"); return paymentsDataContext.planCancel(); }; } function licenceCancelDowngradeModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, oas ) { var vm = this; activate(); function activate() {} vm.cancel = function() { $modalInstance.close(); }; vm.confirm = function() { $modalInstance.close(); fsl.show("Processing cancel downgrade"); return paymentsDataContext.planCancelDowngrade(); }; } function licenceReenableModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, oas ) { var vm = this; activate(); function activate() {} vm.cancel = function() { $modalInstance.close(); }; vm.confirm = function() { $modalInstance.close(); fsl.show("Processing re-enable"); return paymentsDataContext.planEnable(); }; } function licenceRestartModalController( $scope, $modalInstance, paymentsDataContext, user, fsl, oas, $q, auth ) { var vm = this; vm.selectedUsers = 50; vm.currency = "£"; vm.activeUsers = 55; vm.Math = Math; activate(); function activate() { $q .all([ auth.getCurrentUser(), paymentsDataContext.getCurrentPaymentDetails(), oas.getCurrentUserCount() ]) .then(function(data) { vm.user = data[0].data; vm.selectedPlan = data[1]; vm.selectedUsers = vm.selectedPlan.quantity + 1; vm.currentUsers = vm.selectedPlan.quantity; vm.currency = vm.selectedPlan.currencySymbol; vm.usersChanged(); vm.activeUsers = data[2]; vm.checkCost(); }); } vm.currentCost = 0; vm.checkCost = function() { var currentUsers = vm.selectedUsers; if (!currentUsers) { currentUsers = Math.max(vm.selectedPlan.minUsers, vm.activeUsers); } vm.currentCost = currentUsers * (vm.selectedPlan.cost * 1.0 / 100) * 100; if (vm.currentCost < 100) { vm.belowMinimum = true; vm.currentCost = 100; } else { vm.belowMinimum = false; } }; vm.usersChanged = function() {}; vm.cancel = function() { $modalInstance.close(); }; vm.confirm = function() { $modalInstance.close(); return paymentsDataContext.planRestart( vm.user.email, vm.selectedUsers, vm.currentCost ); }; } app.controller("licenceReenableModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "OrganisationAdminService", licenceReenableModalController ]); app.controller("licenceRestartModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "OrganisationAdminService", "$q", "auth", licenceRestartModalController ]); app.controller("licenceCancelModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "OrganisationAdminService", licenceCancelModalController ]); app.controller("licenceCancelDowngradeModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "OrganisationAdminService", licenceCancelDowngradeModalController ]); app.controller("licenceUpgradeQuantityModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "OrganisationAdminService", "licencingAdminDataContext", "$q", "auth", licenceUpgradeQuantityModalController ]); app.controller("licenceSelectorModalController", [ "$scope", "$modalInstance", "paymentsDataContext", "user", "fullScreenLoaderContext", "$q", "auth", "planId", "userAmount", licenceSelectorModalController ]); })();