Skip to content

Instantly share code, notes, and snippets.

@trq
Created September 3, 2014 03:19
Show Gist options
  • Save trq/ffd1ba7d4f1f3b3cf72b to your computer and use it in GitHub Desktop.
Save trq/ffd1ba7d4f1f3b3cf72b to your computer and use it in GitHub Desktop.
(function() {
var calculator = angular.module('calculator', ['ui.check']);
calculator.controller('FormCtrl', ['$scope', '$http', function($scope, $http) {
$scope.selections = {
planType : null,
existingClient : null,
twins : null,
hospitalType : null,
birthingDueDate : null,
paymentTerm : null
}
$scope.hospitalTypeProxy = null;
$scope.page2 = false;
$scope.isCalculateBtnDisabled = true;
$scope.planTypes = [
{name: 'Cord Blood'},
{name: 'Cord Blood + Tissue'}
];
$scope.paymentTerms = [
{name: 'One Time Payment'},
{name: '12 monthly payments of'},
{name: '24 monthly payments of'}
];
$scope.discounts = {
existingClients: [
{name: 'Yes', values: []},
{name: 'No', values: []}
],
expectingTwins: [
{name: 'Yes', values: []},
{name: 'No', values: []}
],
hospitalTypes: [
{name: 'Public', values: []},
{name: 'Private', values: []}
]
};
$scope.isReadyToCalculate = function() {
var isDisabled = false;
angular.forEach($scope.selections, function(value, key) {
if (value === null) {
isDisabled = true;
}
});
$scope.isCalculateBtnDisabled = isDisabled;
};
$scope.turnPage = function() {
$scope.calculate();
$scope.page2 = true;
};
$scope.$watch('hospitalTypeProxy', function(value) {
if (value == 0 || value == 1) {
$scope.selections.hospitalType = $scope.discounts.hospitalTypes[value];
}
}, true);
$scope.$watch('hospitalType', function(value) {
if (value.name == 'Public') {
$scope.hospitalTypeProxy = 0;
} else {
$scope.hospitalTypeProxy = 1;
}
}, true);
$scope.calculate = function() {
$scope.results = [];
getPlans();
filterPlansByType($scope.selections.planType.name);
filterPlansByPlanTerm($scope.selections.paymentTerm.name);
setDiscountValues();
angular.forEach($scope.plans, function(value, key) {
total = value.total;
storage_fee = value.storage_free;
// Apply additions
if ('Yes' == $scope.selections.twins.name) {
total *= 2;
storage_fee *= 2;
}
// Apply discounts.
total -= $scope.selections.existingClient.values[key];
total -= $scope.selections.twins.values[key];
total -= $scope.selections.hospitalType.values[key];
if ($scope.selections.paymentTerm.name == '12 monthly payments of') {
payment_text = 'monthly over 12 months'
payments = total / 12;
} else if ($scope.selections.paymentTerm.name == '24 monthly payments of') {
payment_text = 'monthly over 24 months'
payments = total / 24;
} else if ($scope.selections.paymentTerm.name == 'One Time Payment') {
payment_text = 'in total'
payments = total;
}
result = {
total : total,
plan : value.plan,
deposit : value.deposit,
payments_text: payment_text,
payments : payments,
storage_fee : storage_fee
};
$scope.results.push(result);
});
};
getPlans = function() {
$http.get('/api/v1/pricings', {headers: {Accept: 'application/json'}}).success(function(response) {
$scope.plans = response.plans;
});
};
filterPlansByType = function(type) {
filtered = [];
angular.forEach($scope.plans, function(value, key) {
if (value.type == type) {
filtered.push(value);
}
});
return $scope.plans = filtered;
}
filterPlansByPlanTerm = function(term, plans) {
filtered = [];
angular.forEach($scope.plans, function(value, key) {
if (value.payment == term) {
filtered.push(value);
}
});
$scope.plans = filtered;
}
setDiscountValues = function() {
angular.forEach($scope.plans, function(value, key) {
$scope.discounts.existingClients[0].values[0] = value.existing_discount;
$scope.discounts.existingClients[1].values[1] = 0;
$scope.discounts.expectingTwins[0].values[0] = value.twin_discount;
$scope.discounts.expectingTwins[1].values[1] = 0;
$scope.discounts.hospitalTypes[0].values[0] = value.collector_discount;
$scope.discounts.hospitalTypes[1].values[1] = 0;
});
}
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment