Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- angular
- .module('calculatorsApp')
- .controller('CalculatorsCtrl', CalculatorsCtrl);
- CalculatorsCtrl.$inject = ['$scope', '$state', '$uibModalInstance', '$q', 'calcMethodService', 'categoryService', 'filterFilter', 'notesService', 'errorHandler'];
- function CalculatorsCtrl($scope, $state, $uibModalInstance, $q, calcMethodService, categoryService, filterFilter, notesService, errorHandler) {
- var vm = this;
- vm.sParams = $state.transition ? $state.transition.targetState().params() : $state.params;
- vm.dp = vm.sParams.dp;
- vm.submit = submit;
- vm.cancel = cancel;
- vm.selectCalculator = selectCalculator;
- vm.result = {};
- var statePrefix;
- init();
- function init() {
- statePrefix = $state.current.name.split('.')[0] == 'delegation' ? 'delegation.' : '';
- loadCalcMethods();
- }
- /**
- * Populate the calculated methods drop down and set the selected method
- */
- function loadCalcMethods() {
- var promise = calcMethodService.getCalcMethods(vm.sParams.delegationId);
- promise.then(function (data) {
- vm.calcMethods = data;
- if (vm.dp.CalcMethodId) { //Set the selected method based on the pervious user selection
- vm.selectedMethod = filterFilter(vm.calcMethods, { id: vm.dp.CalcMethodId })[0];
- }
- else if (vm.dp.PreselectedCalcMethod){ //If no calc method used then select the previous year calc method as default
- vm.selectedMethod = filterFilter(vm.calcMethods, { id: vm.dp.PreselectedCalcMethod })[0];
- }
- else { //If no calc method used then select the first one as default
- vm.selectedMethod = !!vm.calcMethods ? vm.calcMethods[0] : {};
- }
- vm.selectCalculator(vm.selectedMethod);
- })
- .catch(function (err) {
- errorHandler.handle('Failed to load calculation method list: ', err);
- });
- }
- function submit(calcResult) {
- if (calcResult.type === 'paste') {
- vm.save.paste(calcResult)
- $uibModalInstance.close({ data: calcResult });
- $state.go(statePrefix + 'collect', null, { reload: true }); //reload the parent state when closing the modal
- } else {
- vm.save.math(calcResult)
- .then(function () {
- console.log("submitted", calcResult);
- $uibModalInstance.close({ data: calcResult });
- $state.go(statePrefix + 'collect', null, { reload: true }); //reload the parent state when closing the modal
- });
- }
- }
- function cancel() {
- $uibModalInstance.dismiss('cancel');
- $state.go(statePrefix + 'collect'); //back to the parent state when closing the modal without reloading
- }
- $scope.$on('modal.closing', function (event, reason, closed) {
- //console.log(event);
- console.log('Reason:', reason);
- $state.go(statePrefix + 'collect'); //back to the parent state when closing the modal without reloading
- });
- function selectCalculator(method) {
- if (!method) return;
- //console.log('Calculator:', method);
- switch (method.id) { //This is a temporary allocation of calculators. Need to formalize as configuration rather than code changes
- case 11:
- $state.go(statePrefix + 'collect.calculator.paste');
- break;
- default:
- $state.go(statePrefix + 'collect.calculator.math');
- };
- }
- vm.save = {
- math: function (result) {
- vm.dp.ActualValue = result.totalValue;
- vm.dp.CalcMethodId = vm.selectedMethod.id;
- vm.dp.CalcComment = result.comment ? result.comment : '';
- var dpList = [];
- dpList.push(vm.dp);
- //return categoryService.putDP(vm.sParams.responseFormat, vm.dp, vm.sParams.assetId, vm.sParams.month, vm.sParams.year);
- return categoryService.saveDps(vm.sParams.month, vm.sParams.year, vm.sParams.assetId, dpList, vm.sParams.delegationId)
- .then(function (pl) {
- //console.log('Saved', result);
- var note = {
- dpId: vm.dp.DPId,
- assetId: vm.sParams.assetId,
- month: vm.sParams.month,
- year: vm.sParams.year,
- commentTxt: vm.dp.CalcComment,
- delegationId: vm.sParams.delegationId
- };
- return result.comment ? notesService.addNote(note) : $q.when(false);
- })
- .then(function (response) {
- console.log(response ? 'Note successfully added' : 'There was no note');
- //$uibModalInstance.close({ data: calcResult });
- })
- .catch(function (err) {
- console.log('Calculator save failed', err.data);
- });
- },
- paste: function (result) {
- console.log('Paste: ', result);
- return categoryService.saveDps(vm.sParams.month, vm.sParams.year, vm.sParams.assetId, result.dpList, vm.sParams.delegationId)
- .then(function (data) {
- console.log('saved information for a month range:', data);
- })
- .catch(function (error) {
- errorHandler.handle('Failed to update data', error);
- })
- }
- };
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement