/* ** Views/Partial/LCA/BOM.cshtml is using this controller */ angular.module('CarbonFootprint') .controller('WasteMaterialController', ['$scope', '$http', '$routeParams', 'Notification', 'JoinSurveyItemAndParameter', 'MultiLanguageService', '$q','$filter', function ($scope, $http, $routeParams, Notification, JoinService, MultiLanguageService, $q ,$filter) { // Multi-language resource object var resource, inited = false; // This property controlls modal form, will be passed to table-edit-modal directive $scope.modalFormOption = {}; $scope.model = []; // 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; } $scope.changeQuantity = function (model) { var janQty = model.JanQuantity || 0, febQty = model.FebQuantity || 0, marQty = model.MarQuantity || 0, aprQty = model.AprQuantity || 0, mayQty = model.MayQuantity || 0, junQty = model.JunQuantity || 0, julQty = model.JulQuantity || 0, augQty = model.AugQuantity || 0, septQty = model.SeptQuantity || 0, octQty = model.OctQuantity || 0, novQty = model.NovQuantity || 0, decQty = model.DecQuantity || 0; model.TotalQuantity = janQty + febQty + marQty + aprQty + mayQty + junQty + julQty + augQty + septQty + octQty + novQty + decQty; }; $scope.changeShippingDistance = function () { var shippingDistance = $scope.lca.WasteMaterialShippingDistance || 0; var totalQuantityT = $scope.lca.WasteMaterialTotalQuantity; $scope.lca.WasteMaterialTKM1 = totalQuantityT * shippingDistance; }; $scope.changeWasteMaterial = function () { var numOfBarrels = $scope.lca.WasteMaterialNumOfBarrels || 0, weight = $scope.lca.WasteMaterialWeight || 0, workingDays = $scope.lca.WasteMaterialWorkingDays || 0, shippingDistance = $scope.lca.WasteMaterialTreatmentPlantShippingDistance || 0; $scope.lca.WasteMaterialTotalOutputKg = numOfBarrels * weight * workingDays; $scope.lca.WasteMaterialTotalOutputT = $scope.lca.WasteMaterialTotalOutputKg / 1000; $scope.lca.WasteMaterialTKM2 = $scope.lca.WasteMaterialTotalOutputT * shippingDistance; }; $scope.changeTreatmentPlantShippingDistance = function () { $scope.lca.WasteMaterialTKM2 = ($scope.lca.WasteMaterialTotalOutputT || 0) * $scope.lca.WasteMaterialTreatmentPlantShippingDistance; }; $scope.createProcess = function () { // Create an object & select it var toBeCreated = {}; toBeCreated.ID = 0; toBeCreated.LCAID = $routeParams.LCAID; toBeCreated.Unit = "kg"; toBeCreated.JanQuantity = 0; toBeCreated.FebQuantity = 0; toBeCreated.MarQuantity = 0; toBeCreated.AprQuantity = 0; toBeCreated.MayQuantity = 0; toBeCreated.JunQuantity = 0; toBeCreated.JulQuantity = 0; toBeCreated.AugQuantity = 0; toBeCreated.SeptQuantity = 0; toBeCreated.OctQuantity = 0; toBeCreated.NovQuantity = 0; toBeCreated.DecQuantity = 0; // For modal form $scope.selectRow.toBeEdit = toBeCreated; $scope.modalFormOption.show = true; $scope.save = function () { if (!validateModel(toBeCreated)) { return; } $http.post($scope.baseUrl+'/api/WasteMaterial/Save/' + $routeParams.LCAID + "/" + toBeCreated.ID, toBeCreated) .success(function (data) { $scope.model.unshift(data.Model); $scope.lca.WasteMaterialTotalQuantity = data.TotalQuantity / 1000; $scope.changeShippingDistance(); $scope.modalFormOption.show = false; }) .error(function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }; }; $scope.editProcess = function (selected) { var toBeEdit = $scope.selectRow.toBeEdit = angular.copy(selected); $scope.modalFormOption.show = true; $scope.save = function () { if (!validateModel(toBeEdit)) { return; } $http.post($scope.baseUrl+'/api/WasteMaterial/Save/' + toBeEdit.LCAID + "/" + toBeEdit.ID, toBeEdit) .success(function (data) { angular.copy(data.Model, selected); $scope.lca.WasteMaterialTotalQuantity = data.TotalQuantity / 1000; $scope.changeShippingDistance(); $scope.modalFormOption.show = false; }) .error(function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }; }; $scope.deleteProcess = function (selected) { var isConfirm = confirm(resource.DeleteItemMsg); if (isConfirm) { $http.delete($scope.baseUrl+"/api/WasteMaterial/Delete/" + selected.ID) .success(function () { var index = $scope.model.indexOf(selected); $scope.lca.WasteMaterialTotalQuantity -= selected.TotalQuantity / 1000; $scope.model.splice(index, 1); // This operate is base on selectRow directive in TableEdit.js file $scope.selectRow.unSelect(); }); } }; $scope.updateLCA = function () { $http.post($scope.baseUrl+'/api/WasteMaterial/UpdateLCA/' + $routeParams.LCAID, $scope.lca) .success(function (data) { console.log(data); }) .error(function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }; $scope.$on('changeSelectedTab', function (event, args) { if (!inited && args.selectedTab == 'WasteMaterial') { inited = true; // Get data and prepare $scope.model getAllDataAsync($routeParams.LCAID) .then(function (response) { $scope.model = response.Materials; $scope.lca = response.LCA; }, function (error) { console.log(error); }); // Get multilanguage resource MultiLanguageService.getResourceAsync() .then(function (response) { resource = response; }, function (error) { console.log(error); }); } }); /** * Get all list data from server * @param {[Int]} : LCAID * @return {[promise]} */ function getAllDataAsync(LCAID) { var deferred = $q.defer(); $http.get($scope.baseUrl+'/api/WasteMaterial/GetByLcaId/' + LCAID) .success(function (data) { deferred.resolve(data); }) .error(function () { deferred.resolve(null); }) return deferred.promise; } function validateModel(model) { model.JanQuantity = model.JanQuantity || 0; model.FebQuantity = model.FebQuantity || 0; model.MarQuantity = model.MarQuantity || 0; model.AprQuantity = model.AprQuantity || 0; model.MayQuantity = model.MayQuantity || 0; model.JunQuantity = model.JunQuantity || 0; model.JulQuantity = model.JulQuantity || 0; model.AugQuantity = model.AugQuantity || 0; model.SeptQuantity = model.SeptQuantity || 0; model.OctQuantity = model.OctQuantity || 0; model.NovQuantity = model.NovQuantity || 0; model.DecQuantity = model.DecQuantity || 0; return true; } }]);