/* ** Views/Partial/LCA/HighLevelAnalyze.cshtml is using this controller ** this controller is responsible to provide data , function , in the view */ angular.module('CarbonFootprint') .controller('LCAHighLevelAnalyzeController', ['$q', '$scope', '$routeParams', 'LCADetailCacheService', 'HighLevelAnalyzeFactory', '$filter', '$http', '$location', 'MultiLanguageService', function ($q, $scope, $routeParams, LCADetailCacheService, HighLevelAnalyzeFactory, $filter, $http, $location, MultiLanguageService) { var materials = []; var cached; var resource; // 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.quoteModal = {}; $scope.getdata = LCADetailCacheService.getAllMaterialAsync; $scope.model = {}; $scope.meterialDetailModalOption = {}; $scope.fileImportOption = { show: false, modelBuilder: HighLevelAnalyzeFactory, saveAsync: batchCreateAndUpdateCache }; $scope.quoteOptions = { queryDataAsyncFn: LCADetailCacheService.getAllMaterialAsync, saveAsyncFn: batchCreateAndUpdateCache }; $scope.modelFactory = HighLevelAnalyzeFactory; MultiLanguageService.getResourceAsync() .then(function (response) { resource = response; }, function (error) { console.log(error); }); $q.all([LCADetailCacheService.getAllMaterialAsync($routeParams.LCAID), LCADetailCacheService.getPCRAsync($routeParams.LCAID), $http.get($scope.baseUrl+'/api/Parameter/GetSimaproCategories')]).then(function (result) { var materialTypeOptions; var pcr = $scope.pcr = result[1]; // Fetch data materials = $scope.model.materials = $filter('filter')(result[0], { MaterialType: 0 }); $scope.calcTotalKgCO2e(); cached = result[0]; // below is bad idea, the returned array is different from cached array, thus break the crud function from updating local cache // materials = $scope.model.materials = $filter('filter')(result[0], { MaterialType: 0}); materialTypeOptions = $scope.model.materialTypeOptions = result[2].data; $scope.totalKgCO2e = function () { var result = 0; angular.forEach(materials, function (entry) { result += entry.KgCO2e; }); return result; }; // Provide function $scope.createProcess = function () { // New an object & select it var toBeCreated = {}; toBeCreated.ID = 0; toBeCreated.LCAID = $routeParams.LCAID; toBeCreated.MaterialType = 0; toBeCreated.MaterialComposites = []; toBeCreated.WeightUnit = 'kg'; toBeCreated.VolumeUnit = 'm3'; $scope.toBeEdit = toBeCreated; $scope.meterialDetailModalOption.show = true; $scope.meterialDetailModalOption.onOkFn = function () { $scope.updateKgCO2e($scope.toBeEdit); LCADetailCacheService.updateMaterialsAsync([toBeCreated]).then(function (result) { $scope.model.materials.unshift(result[0]); cached.unshift(result[0]); $scope.meterialDetailModalOption.show = false; $scope.calcTotalKgCO2e(); }); }; $scope.dataQualityOption = {}; $scope.dataQualityOption.data = {}; $scope.isEdit = false; }; $scope.editProcess = function (selected) { var local = $scope.toBeEdit = angular.copy(selected); if ($scope.toBeEdit.WeightUnit == undefined || $scope.toBeEdit.WeightUnit == '') $scope.toBeEdit.WeightUnit = 'kg'; if ($scope.toBeEdit.VolumeUnit == undefined || $scope.toBeEdit.VolumeUnit == '') $scope.toBeEdit.VolumeUnit = 'm3'; local.MaterialType = 0; $scope.meterialDetailModalOption.show = true; $scope.meterialDetailModalOption.onOkFn = function () { $scope.updateKgCO2e($scope.toBeEdit); //if (selected.localDisplayParameterValue && local.Scalar) { // local.HighLevelAnalyzeResult = local.KgCO2e = selected.localDisplayParameterValue * local.Scalar; // local.HighLevelAnalyzeResult = local.HighLevelAnalyzeResult * (local.Quantity||0); //} LCADetailCacheService.updateMaterialsAsync([local]).then(function (result) { angular.copy(local, selected); $scope.meterialDetailModalOption.show = false; $scope.calcTotalKgCO2e(); }); }; $scope.dataQualityOption = {}; $scope.dataQualityOption.data = {}; $scope.isEdit = false; }; $scope.editProcess = function (selected) { var local = $scope.toBeEdit = angular.copy(selected); if ($scope.toBeEdit.WeightUnit == undefined || $scope.toBeEdit.WeightUnit == '') $scope.toBeEdit.WeightUnit = 'kg'; if ($scope.toBeEdit.VolumeUnit == undefined || $scope.toBeEdit.VolumeUnit == '') $scope.toBeEdit.VolumeUnit = 'm3'; local.MaterialType = 0; $scope.meterialDetailModalOption.show = true; $scope.meterialDetailModalOption.onOkFn = function () { $scope.updateKgCO2e($scope.toBeEdit); //if (selected.localDisplayParameterValue && local.Scalar) { // local.HighLevelAnalyzeResult = local.KgCO2e = selected.localDisplayParameterValue * local.Scalar; // local.HighLevelAnalyzeResult = local.HighLevelAnalyzeResult * (local.Quantity||0); //} LCADetailCacheService.updateMaterialsAsync([local]).then(function (result) { angular.copy(local, selected); $scope.meterialDetailModalOption.show = false; $scope.calcTotalKgCO2e(); }); }; $scope.dataQualityOption = {}; $scope.dataQualityOption.data = {}; $scope.isEdit = true; }; $scope.dataQualityShow = function (selected) { $http.get($scope.baseUrl+'/api/DataQualityLevel/GetByTableId/ProductLCAProductSurveyForm_Materials/' + selected.ID ).success(function (data) { angular.copy(data, $scope.dataQualityOption.data); $scope.dataQualityOption.data.Name = selected.Name; $scope.dataQualityOption.data.LCAID = selected.LCAID; //todo !!... load pre saved data //$scope.dataQualityOption.Fi = 0.0123;//test only; //todo !!... 還需要總碳排量才能算出 碳排量佔比 $scope.dataQualityOption.showSaveBtn = $scope.lcaDetail.Status <= 1; $scope.dataQualityOption.show = true; $scope.calcTotalKgCO2e(); }).error(function (data, status) { alert(data.ExceptionMessage); }); }; $scope.detailView = function (selected) { var local = $scope.toBeEdit = angular.copy(selected); $scope.meterialDetailModalOption.show = true; $scope.dataQualityOption = {}; $scope.dataQualityOption.data = {}; $scope.isEdit = true; }; $scope.deleteProcess = function (selected) { var isConfirm = confirm(resource.DeleteItemMsg); if (selected.ChildMaterials && selected.ChildMaterials.length != 0) { alert('please delete all child first!'); return; } if (isConfirm) { $http.delete($scope.baseUrl+'/api/Material/DeleteMaterial/' + selected.LCAID + '/' + selected.ID) .success(function () { $scope.model.materials.splice($scope.model.materials.indexOf(selected), 1); cached.splice(cached(selected), 1); $scope.calcTotalKgCO2e(); }); } }; $scope.selectSimaproProcess = function (selectedMaterial, isOutside = true) { $scope.simaproOption = {}; $scope.simaproOption.show = true; $scope.simaproOption.filter = 37; // SimaproOption.Material 原料 $scope.simaproOption.onOk = function (selectedParameter, selectedCategory) { //console.log(selectedParameter); selectedMaterial.ParameterID = selectedParameter.ID; selectedMaterial.ParameterTypeID = selectedCategory.ID; selectedMaterial.ParameterValue = selectedParameter.Value selectedMaterial.ParameterDescription = selectedParameter.DisplayNameTW + ': ' + selectedParameter.Remark; selectedMaterial.Unit = selectedParameter.Unit; selectedMaterial.localDisplayParameterValue = selectedParameter.Value; selectedMaterial.ParameterSource = 2; selectedMaterial.ParaSourceDesc = selectedParameter.Description; selectedMaterial.MaterialComposites = []; $scope.updateKgCO2e(selectedMaterial); if (isOutside) { LCADetailCacheService.updateMaterialsAsync([selectedMaterial]); $scope.calcTotalKgCO2e(); } //LCADetailCacheService.updateMaterialsAsync([selectedMaterial]); } }; $scope.selectSimaproProcess_Composition = function (selectedComposition) { $scope.simaproOption = {}; $scope.simaproOption.show = true; $scope.simaproOption.filter = 37; // SimaproOption.Material 原料 $scope.simaproOption.onOk = function (selectedParameter, selectedCategory) { selectedComposition.parameterValue = selectedParameter.Value selectedComposition.parameterDescription = selectedParameter.DisplayNameTW + ': ' + selectedParameter.Remark; selectedComposition.parameterUnit = selectedParameter.Unit; $scope.updateKgCO2e($scope.toBeEdit); //LCADetailCacheService.updateMaterialsAsync([selectedMaterial]); } }; $scope.passedPcr = function () { return (pcr == null) ? true : $scope.currentPercentage() >= pcr.Percentage; }; $scope.currentKgCO2e = function () { var current = 0; angular.forEach(materials, function (entry) { if (entry.IncludedInInterrogation) { current += entry.KgCO2e; } }); return current; }; $scope.currentPercentage = function () { var value = $scope.currentKgCO2e() / $scope.totalKgCO2e() * 100; return ((value == null) || isNaN(value)) ? 0 : $scope.currentKgCO2e() / $scope.totalKgCO2e() * 100; }; $scope.completeProcess = function (included) { //console.log('completeProcess', included); var local; $scope.modalFormConfirmOption = {}; //console.log('completeProcess1', $scope.modalFormConfirmOption.includedMaterials); // Fetch options from server local = $scope.modalFormConfirmOption.includedMaterials = angular.copy(included); //console.log('completeProcess2', local); $http.get($scope.baseUrl+'/api/Supplier/GetByMyCompany2') .success(function (data) { //console.log('completeProcess2.1', local); $scope.modalFormConfirmOption.supplierOptions = data; }); $scope.modalFormConfirmOption.show = true; //console.log('completeProcess3', local); // Add function call back // Note: there is some duplicate code between onOk and onNext $scope.modalFormConfirmOption.onNextFn = function () { //console.log('onNextFn', local); LCADetailCacheService.updateMaterialsAsync(local).then(function () { angular.forEach(local, function (entry, key) { angular.copy(entry, included[key]); }); LCADetailCacheService.setLCAStatusFromNewToProcessAsync($routeParams.LCAID).then(function () { $scope.modalFormConfirmOption.show = false; $location.path('/LCA/DirectMaterial/' + $routeParams.LCAID); $scope.calcTotalKgCO2e(); }); }); }; $scope.modalFormConfirmOption.onOkFn = function () { LCADetailCacheService.updateMaterialsAsync(local).then(function () { angular.forEach(local, function (entry, key) { angular.copy(entry, included[key]); }); $scope.modalFormConfirmOption.show = false; $scope.calcTotalKgCO2e(); }); }; }; $scope.manualSelect = function (selected) { selected.IncludedInInterrogation = !selected.IncludedInInterrogation; LCADetailCacheService.updateMaterialsAsync([selected]); $scope.calcTotalKgCO2e(); }; $scope.autoSelect = function (toBeSelect) { var toBeUpdate = []; angular.forEach(toBeSelect, function (entry) { if (!$scope.passedPcr()) { entry.IncludedInInterrogation = true; toBeUpdate.push(entry); } }); LCADetailCacheService.updateMaterialsAsync(toBeUpdate) $scope.calcTotalKgCO2e(); }; $scope.clear = function () { var toBeUpdate = []; angular.forEach(materials, function (entry) { if (entry.IncludedInInterrogation == true) { entry.IncludedInInterrogation = false; toBeUpdate.push(entry); } }); if (toBeUpdate.length > 0) LCADetailCacheService.updateMaterialsAsync(toBeUpdate); $scope.calcTotalKgCO2e(); }; $scope.reNumber = function () { $http.post(baseUrl + '/api/Material/ReNumber/' + $routeParams.LCAID + '/0').success(function (data) { console.log('reNumber', data); location.reload(true); }).error(function (data) { console.log('error', data); }) }; }); function batchCreateAndUpdateCache(saveList) { console.log('batchCreateAndUpdateCache', saveList); var deferred = $q.defer(); for (var i = 0; i < saveList.length; i++) saveList[i].MaterialType = 0; LCADetailCacheService.updateMaterialsAsync(saveList).then(function (result) { angular.forEach(result, function (entry) { cached.push(entry); if (entry.MaterialType == 0) { materials.push(entry); } $scope.calcTotalKgCO2e(); }); deferred.resolve(null); }); return deferred.promise; } // 新增材質組成項目 $scope.addComposite = function () { if ($scope.toBeEdit.MaterialComposites.length == 0) { $scope.toBeEdit.ParameterDescription = ''; $scope.toBeEdit.Unit = ''; } $scope.toBeEdit.MaterialComposites.push({ materialComposite: '', CASNo: '', compositePercentage: '', parameterValue: '', parameterUnit: '', parameterDescription: '' }) $scope.toBeEdit.ParameterSource = 2; $scope.updateParameter(); } $scope.deleteComposite = function (index) { $scope.toBeEdit.MaterialComposites.splice(index, 1) $scope.toBeEdit.ParameterSource = 2; $scope.updateParameter(); } $scope.updateScalar = function (num, mul) { $scope.updateDensity() $scope.updateActivityIntensity(); } $scope.updateActivityIntensity = function () { var weiMul = 1; if ($scope.toBeEdit.WeightUnit == 'mg') weiMul = 0.000001; else if ($scope.toBeEdit.WeightUnit == 'g') weiMul = 0.001; else if ($scope.toBeEdit.WeightUnit == 't') weiMul = 1000; $scope.toBeEdit.ActivityIntensity_BOM = $scope.toBeEdit.Scalar * weiMul * $scope.toBeEdit.Quantity_BOM; $scope.toBeEdit.ActivityIntensity = $scope.toBeEdit.Scalar * weiMul * $scope.toBeEdit.Quantity; //$scope.toBeEdit.oriKgCO2e = $scope.toBeEdit.KgCO2e; $scope.updateKgCO2e($scope.toBeEdit); } $scope.updateKgCO2e = function (selectedMaterial, isUpdateParameter) { selectedMaterial.KgCO2e_BOM = selectedMaterial.ActivityIntensity_BOM * selectedMaterial.ParameterValue; selectedMaterial.KgCO2e = selectedMaterial.ActivityIntensity * selectedMaterial.ParameterValue; //if (isUpdateParameter) selectedMaterial.ParameterSource = 2; //$scope.toBeEdit.oriKgCO2e = $scope.toBeEdit.KgCO2e; } $scope.updateParameter = function () { var total = 0; $scope.toBeEdit.MaterialComposites.forEach( m => total += m.parameterValue * m.compositePercentage / 100 ); $scope.toBeEdit.ParameterValue = total; $scope.updateKgCO2e($scope.toBeEdit); } $scope.updateDensity = function () { var volMul = 1; if ($scope.toBeEdit.VolumeUnit == 'ml') volMul = 0.000001; else if ($scope.toBeEdit.VolumeUnit == 'L') volMul = 0.001; var weiMul = 1; if ($scope.toBeEdit.WeightUnit == 'mg') weiMul = 0.000001; else if ($scope.toBeEdit.WeightUnit == 'g') weiMul = 0.001; else if ($scope.toBeEdit.WeightUnit == 't') weiMul = 1000; $scope.toBeEdit.Density = $scope.toBeEdit.Scalar * weiMul / ($scope.toBeEdit.Volume * volMul); } $scope.calcTotalKgCO2e = function () { $scope.sumKgCO2e_BOM = 0; $scope.sumKgCO2e = 0; angular.forEach($scope.model.materials, function (model) { $scope.sumKgCO2e_BOM += model.KgCO2e_BOM $scope.sumKgCO2e += model.KgCO2e }); } }])