/* Logic Solution WeeeCarbonFootprint responsible for joining fab , product , pcr ,VerifierCompany , though LCA data used by views/partial/LCA/Detail.cshtml view included from LCADetailLayoutController.js */ angular.module('CarbonFootprint') .controller('LCADetailController', ['$scope', '$http', 'LCADetailCacheService', '$routeParams', 'ProductDetailCacheService', 'MultiLanguageService', '$timeout', function ($scope, $http, LCADetailCacheService, $routeParams, ProductDetailCacheService, MultiLanguageService, $timeout) { // Multi-language resource object var resource; $scope.pcsAllocationRatio = 0; $scope.weightAllocationRatio = 0; $scope.areaAllocationRatio = 0; $scope.economicAllocationRatio = 0; $scope.workhourAllocationRatio = 0; $scope.fileImportModal = { show: false }; var formData = new FormData; $scope.hasFile = false; $scope.selectFile = function (file) { formData.append($routeParams.LCAID + "_ImportOrganizationLCAReport_0", file); $scope.$apply(function () { $scope.hasFile = true; }); }; $scope.import = function () { $http.post('../api/LCA/ImportOrganizationAllData/' + $routeParams.LCAID, formData, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).success(function (data) { //console.log('success', data); alert(data) document.getElementById("importFile").value = ""; $scope.hasFile = false; formData = new FormData; $scope.close(); }).error(function (data) { console.log('error', data); document.getElementById("importFile").value = ""; formData = new FormData; $scope.hasFile = false; if (data?.ExceptionMessage != undefined) { $scope.importErrorMsg = data.ExceptionMessage; alert(data.ExceptionMessage); } else if (data?.Message != undefined) { alert('Import Failed!'); $scope.importErrorMsg = data.Message } else alert('Oops! This is something happen. Maybe you can try it later'); });; } $scope.close = function () { formData = new FormData; $scope.hasFile = false; $scope.fileImportModal.show = false; document.getElementById("importFile").value = ""; } // 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; } MultiLanguageService.getResourceAsync() .then(function (response) { resource = response; }, function (error) { console.log(error); }); $scope.getLCADetail = function () { LCADetailCacheService.getLCADetailAsync($routeParams.LCAID) .then(function (lcaData) { //console.log('getLCADetailAsync', lcaData); //$scope.selectedARversion = lcaData.ARversion; //console.log('1','>'+$scope.selectedARversion+'<'); //pcr if (lcaData.PCRID) { LCADetailCacheService.getPCRAsync($routeParams.LCAID) .then(function (data) { $scope.pcr = data; }); } $http.get($scope.baseUrl+'/api/ARversion/GetAll') .success(function (data, status, header, config) { $scope.lcaDetail.ARversionOptions = data; //$scope.lcaDetail.ARversion = lcaData.ARversion;// data[0].ARversion; //console.log(data); //console.log('2', '>' + $scope.selectedARversion + '<'); $scope.lcaDetail.ARversionOptions.forEach((x) => { if (x.ARversion == lcaData.ARversion) $scope.lcaDetail.ARversion = x; //console.log(x); }); $scope.updatePcsAllocationRatio(); }) .error(function (data, status, headers, config) { showServerError(data, status, headers, config); }); //VerifierCompany if (lcaData.VerifierCompanyID) { $http.get('api/Company/GetCertificationCompanyByID/' + lcaData.VerifierCompanyID) .success(function (data, status, header, config) { $scope.certificationConpamy = data; }); } //Product if (lcaData.ProductID) { ProductDetailCacheService.getProductDetailAsync(lcaData.ProductID) .then(function (result) { $scope.product = result; $scope.updatePcsAllocationRatio(); }); } //Fab if (lcaData.FabID) { $http.get($scope.baseUrl+'/api/Fab/Get/' + lcaData.FabID) .success(function (data, status, header, config) { $scope.fab = data; }); } $http.get($scope.baseUrl+'/api/Company/getCertificationCompany') .success(function (data, status, header, config) { $scope.certificationCompanyOptions = data; }); $scope.VerifierCompanyDownloadLink = ''; if (lcaData != null && lcaData.VerifierCompanyID != null && lcaData.VerifierCompanyID != '') { var getCompanyLinkUrl = $scope.baseUrl+'/api/LCA/GetCertificationCompanyFile/';//{Type}/{VerifierCompanyID}'; if (lcaData.LCAType.includes("ProductLCA")) { $http.get(getCompanyLinkUrl + 'ProductLCA/' + lcaData.VerifierCompanyID) .success(function (data, status, header, config) { $scope.VerifierCompanyDownloadLink = data.Url; }); } else if (lcaData.LCAType.includes("OrganizationLCA")) { $http.get(getCompanyLinkUrl + 'OrganizationLCA/' + lcaData.VerifierCompanyID) .success(function (data, status, header, config) { $scope.VerifierCompanyDownloadLink = data.Url; }); } else { } } }); } $scope.getLCADetail(); // Update LCA info $scope.updateLCA = function (isProductLCA) { $scope.saveResultMsg = ''; //$scope.lcaDetail.ARversion = $scope.lcaDetail.ARversion.ARversion; var toBeEdit = $scope.lcaDetail; toBeEdit.ARversion = $scope.lcaDetail.ARversion.ARversion; // change api url by 'isProductLCA' parameter var apiUrl; if (isProductLCA) apiUrl = $scope.baseUrl+'/api/LCA/SaveProductLCA'; else apiUrl = $scope.baseUrl+'/api/LCA/SaveOrganizationLCA'; $http.post(apiUrl, toBeEdit) .success(function (data) { $scope.saveResultMsg = resource['UpdateSuccess']; $scope.lcaDetail.ARversionOptions.forEach((x) => { if (x.ARversion == toBeEdit.ARversion) $scope.lcaDetail.ARversion = x; }); //console.log($scope.lcaDetail.ARversion); $scope.getLCADetail(); }) .error(function (error) { $scope.saveResultMsg = resource['UpdateFail'] + ': ' + error.Message; if (typeof (error) != 'undefined' && typeof (error.ModelState) != 'undefined') { var msg = ''; angular.forEach(error.ModelState, function (entry) { msg += entry + '; '; }); alert(msg); console.log('error.ModelState', error.ModelState); } //console.log(error); }) .finally(function () { // set timeout $timeout(function () { $scope.saveResultMsg = '' }, 3000); }); }; // recalculate all LCA (for any parameter change) $scope.recalculateAllLCA = function (isProductLCA) { $scope.saveResultMsg = ''; var toBeEdit = $scope.lcaDetail; // change api url by 'isProductLCA' parameter var apiUrl; if (isProductLCA) apiUrl = $scope.baseUrl+'/api/LCA/RecalculateProductLCA'; else apiUrl = $scope.baseUrl+'/api/LCA/RecalculateOrganizationLCA'; $http.post(apiUrl, toBeEdit) .success(function (data) { $scope.saveResultMsg = resource['UpdateSuccess']; }) .error(function (error) { $scope.saveResultMsg = resource['UpdateFail'] + ': ' + error.Message; console.log(error); }) .finally(function () { // set timeout $timeout(function () { $scope.saveResultMsg = '' }, 3000); }); }; $scope.ckParentLcaId = function () { var ID = $scope.lcaDetail.ID; var parentLCAID = $scope.lcaDetail.parentLCAID; if (parentLCAID == null || parentLCAID == '') { } else { var apiUrl = $scope.baseUrl+'/api/LCA/CkParentLcaId'; if (ID == parentLCAID) { $scope.lcaDetail.parentLCAID = ''; alert('不可將自己設定為父盤查'); } else { $http.get($scope.baseUrl+'/api/LCA/CkParentLcaId/' + parentLCAID) .success(function (data) { if (data != '""') { let msg = data.replaceAll('"', ''); $scope.lcaDetail.parentLCAID = ''; alert(msg); } }) .error(function (error) { $scope.lcaDetail.parentLCAID = ''; alert('查詢失敗, 請洽系統管理員'); }) } } } $scope.isoOptions = [ { value: true, text: 'Yes' }, { value: false, text: 'No' } ]; $scope.calculateRatio = function (a, b, c) { // 計算 a * b / c,若結果為nan回傳 0 var ret = a * b / c if (isNaN(ret)) return 0 if (ret == Infinity) return 0 //return Number(Math.round(ret + 'e' + 10) + 'e-' + 10) return ret } $scope.setAllocationRatioToNone = function (type) { console.log(type); switch (type) { case 'WorkHour': $scope.workhourAllocationRatio = $scope.calculateRatio(1, 1, 1); console.log($scope.workhourAllocationRatio); //$scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs $scope.lcaDetail.ProductProductionHour = 1; $scope.lcaDetail.FabProductionHour = 1; break; case 'ProductionQty': $scope.pcsAllocationRatio = $scope.calculateRatio(1, 1, 1); console.log($scope.pcsAllocationRatio); //$scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs $scope.lcaDetail.ProductProductionPcs = 1; $scope.lcaDetail.FabProductionPcs = 1; break; case 'Weight': $scope.weightAllocationRatio = $scope.calculateRatio(1, 1, 1); console.log($scope.weightAllocationRatio); //$scope.lcaDetail.ProductProductionPcs * $scope.product.Weight/ $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs; $scope.product.Weight = 1; $scope.lcaDetail.FabProductionWeight = 1; break; case 'AreaSize': $scope.areaAllocationRatio = $scope.calculateRatio(1, 1, 1); console.log($scope.areaAllocationRatio); //$scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs $scope.product.AreaSize = 1; $scope.lcaDetail.FabProductionArea = 1; break; case 'EconomicCost': $scope.economicAllocationRatio = $scope.calculateRatio(1, 1, 1); console.log($scope.economicAllocationRatio); //$scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs $scope.lcaDetail.FabProductionEconomic = 1; $scope.lcaDetail.ProductProductionEconomic = 1; break; default: break; } } $scope.updatePcsAllocationRatio = function () { $scope.pcsAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs); $scope.updateWeightAllocationRatio(); $scope.updateAreaAllocationRatio(); $scope.updateEconomicAllocationRatio(); $scope.updateWorkhourAllocationRatio(); } $scope.updateWeightAllocationRatio = function () { //$scope.weightAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs, $scope.product.Weight, $scope.lcaDetail.FabProductionWeight); $scope.weightAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs * $scope.product.Weight/ $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs); } $scope.updateAreaAllocationRatio = function () { //$scope.areaAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs, $scope.product.AreaSize, $scope.lcaDetail.FabProductionArea); $scope.areaAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs); } $scope.updateEconomicAllocationRatio = function () { //$scope.economicAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionEconomic, $scope.lcaDetail.FabProductionEconomic); $scope.economicAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs); } $scope.updateWorkhourAllocationRatio = function () { //$scope.workhourAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionHour, $scope.lcaDetail.FabProductionHour); $scope.workhourAllocationRatio = $scope.calculateRatio($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs); } $scope.checkRatioAndSetColor = function (ratio) { if (ratio <= 0 || ratio > 1) return { color: "red" }; return {} } $scope.checkAllocation = function (isproductLCA) { if (!isproductLCA) return false //回true代表驗證不通過 所以disable if ($scope.weightAllocationRatio <= 0 || $scope.weightAllocationRatio > 1) return true; if ($scope.areaAllocationRatio <= 0 || $scope.areaAllocationRatio > 1) return true; if ($scope.pcsAllocationRatio <= 0 || $scope.pcsAllocationRatio > 1) return true; if ($scope.economicAllocationRatio <= 0 || $scope.economicAllocationRatio > 1) return true; if ($scope.workhourAllocationRatio <= 0 || $scope.workhourAllocationRatio > 1) return true; return false } }]);