You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.7 KiB
JavaScript

var app = angular.module('instantQuote', []);
app.controller('Servers', function ($scope, $window, $http) {
$scope.servers = JSON.parse(localStorage.getItem("servers")) ?? [{ cost: 0 }]
$scope.extras = JSON.parse(localStorage.getItem("extras")) ?? { firewall: true, quota: 100 }
params = new URLSearchParams(window.location.search)
$scope.preload = params.get('preload')
if ($scope.preload && $scope.preload.match(/[0-9A-Z\-]+/g)) {
$http({
url: `preload/${$scope.preload}.json`
}).then((data) => {
console.log(data)
$scope.servers = data.data.servers;
$scope.extras = data.data.extras;
localStorage.setItem("servers", angular.toJson($scope.servers));
localStorage.setItem("extras", angular.toJson($scope.extras));
window.location = window.location.pathname
})
}
$scope.addServer = () => {
$scope.servers.push({ cost: 0 })
}
$scope.serverPricing = {
"vcpu": 10.5,
"ram": 22.5,
"disk": 0.15,
"windows": 10,
"sql": 329.6,
"ip": 8.25,
"quota": 0.09,
"rdscals": 14.25,
"firewall": 75
}
$scope.serverCost = (server) => {
server.cost = (
((server.vcpus || 0) * $scope.serverPricing.vcpu) +
((server.ram || 0) * $scope.serverPricing.ram) +
((server.disk || 0) * $scope.serverPricing.disk) +
(Math.floor((server.windows ? 1 : 0) * $scope.serverPricing.windows * (server.vcpus || 0) / 2) * 2) +
(Math.floor((server.sql ? 1 : 0) * $scope.serverPricing.sql * (server.vcpus || 0) / 2)) +
((server.ip ? 1 : 0) * $scope.serverPricing.ip)
);
// console.log(server.cost)
return Math.round((server.cost || 0) * 100) / 100;
}
$scope.serverTotals = (type) => {
localStorage.setItem("servers", angular.toJson($scope.servers));
return $scope.servers?.reduce(function (a, b) {
if (type == "windows") {
return a + ((b?.windows ?? 0) * (b.vcpus ?? 0) / 2 ?? 0);
} else if (type == "sql") {
return a + ((b?.sql ?? 0) * (b.vcpus ?? 0) / 2 ?? 0);
} else {
return a + (b?.[type] ?? 0);
}
}, 0);
}
$scope.servicesTotals = () => {
localStorage.setItem("extras", angular.toJson($scope.extras));
return (
($scope.extras.firewall * $scope.serverPricing.firewall) +
($scope.extras.rdscals * $scope.serverPricing.rdscals) +
($scope.extras.quota * $scope.serverPricing.quota)
)
}
$scope.deleteServer = (server) => {
$scope.servers.splice(server, 1);
}
});