326 lines
14 KiB
JavaScript
326 lines
14 KiB
JavaScript
/*
|
|
** Views/Partial/LCA/BOM.cshtml is using this controller
|
|
*/
|
|
|
|
angular.module('CarbonFootprint')
|
|
.controller('LCABOMController',
|
|
['$scope', '$http', '$routeParams', 'MaterialFactory', 'LCADetailCacheService', 'MultiLanguageService', '$q','$filter',
|
|
function ($scope, $http, $routeParams, MaterialFactory, LCADetailCacheService, MultiLanguageService, $q ,$filter) {
|
|
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;
|
|
}
|
|
|
|
// This property controlls modal form, will be passed to table-edit-modal directive
|
|
$scope.modalFormOption = {};
|
|
|
|
$scope.modelFactory = MaterialFactory;
|
|
|
|
$scope.fileImportOption = {
|
|
show: false,
|
|
modelBuilder: $scope.modelFactory,
|
|
saveAsync: function (parsedList) {
|
|
var deferred = $q.defer();
|
|
for (var i = 0; i < parsedList.length; i++)
|
|
parsedList[i].MaterialType = 0;
|
|
|
|
LCADetailCacheService.updateMaterialsAsync(parsedList).then(function (result) {
|
|
angular.forEach(result, function (entry) {
|
|
$scope.model.materials.push(entry)
|
|
});
|
|
|
|
deferred.resolve(null);
|
|
$scope.calcTotalKgCO2e();
|
|
});
|
|
|
|
return deferred.promise;
|
|
}
|
|
};
|
|
$scope.quoteOptions = {
|
|
queryDataAsyncFn: function (LCAID) {
|
|
var deferred = $q.defer();
|
|
LCADetailCacheService.getAllMaterialAsync(LCAID).then(function (result) {
|
|
var filtered = $filter('filter')(result, {MaterialType:0})
|
|
deferred.resolve(filtered);
|
|
$scope.calcTotalKgCO2e()
|
|
});
|
|
return deferred.promise;
|
|
},
|
|
saveAsyncFn: $scope.fileImportOption.saveAsync
|
|
};
|
|
|
|
$scope.quoteModal = {
|
|
show: false
|
|
};
|
|
|
|
$scope.createProcess = function () {
|
|
|
|
// New an object & select it
|
|
var toBeCreated = {};
|
|
toBeCreated.ID = 0;
|
|
toBeCreated.LCAID = $routeParams.LCAID;
|
|
toBeCreated.MaterialComposites = [];
|
|
toBeCreated.WeightUnit = 'kg';
|
|
toBeCreated.VolumeUnit = 'm3';
|
|
$scope.toBeEdit = toBeCreated;
|
|
$scope.modalFormOption.show = true;
|
|
$scope.modalFormOption.editable = true;
|
|
$scope.save = function () {
|
|
toBeCreated.MaterialType = 0;
|
|
LCADetailCacheService.updateMaterialsAsync([toBeCreated])
|
|
.then(function (result) {
|
|
$scope.model.materials.unshift(result[0]);
|
|
$scope.modalFormOption.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.modalFormOption.show = true;
|
|
$scope.modalFormOption.editable = true;
|
|
$scope.save = function () {
|
|
if (selected.ParameterValue && local.Scalar && local.Quantity) {
|
|
local.HighLevelAnalyzeResult = local.KgCO2e;
|
|
}
|
|
|
|
LCADetailCacheService.updateMaterialsAsync([local])
|
|
.then(function (result) {
|
|
angular.copy(local, selected);
|
|
$scope.modalFormOption.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.createChildProcess = function (selected) {
|
|
|
|
// // Do nothing, if the selected is child
|
|
// if (selected.isChild) return;
|
|
|
|
// var toBeCreated = {};
|
|
// toBeCreated.ID = 0;
|
|
// toBeCreated.ParentMaterialID = selected.ID;
|
|
// toBeCreated.LCAID = selected.LCAID
|
|
// toBeCreated.parentName = selected.Name;
|
|
|
|
// toBeCreated.isChild = true;
|
|
// $scope.toBeEdit = toBeCreated;
|
|
// $scope.modalFormOption.show = true;
|
|
// $scope.modalFormOption.editable = true;
|
|
// $scope.modalFormOption.onOkFn = function () {
|
|
// $http.post($scope.baseUrl+'/api/Material/SaveMaterial/' + $routeParams.LCAID + "/" + toBeCreated.ID, toBeCreated)
|
|
// .success(function (data) {
|
|
// data.show = true;
|
|
// selected.ChildMaterials.unshift(data)
|
|
// selected.expend = true;
|
|
// selected.expendable = true;
|
|
// $scope.modalFormOption.show = false;
|
|
// });
|
|
// };
|
|
//};
|
|
|
|
$scope.selectSimaproProcess = function (selectedMaterial) {
|
|
$scope.simaproOption = {};
|
|
$scope.simaproOption.show = true;
|
|
$scope.simaproOption.filter = 37; // SimaproOption.Material 原料
|
|
$scope.simaproOption.onOk = function (selectedParameter) {
|
|
selectedMaterial.ParameterValue = selectedParameter.Value;
|
|
selectedMaterial.ParameterID = selectedParameter.ID;
|
|
selectedMaterial.KgCO2e = selectedParameter.Value * selectedMaterial.Scalar
|
|
selectedMaterial.localDisplayEncoding = selectedParameter.Encoding;
|
|
selectedMaterial.Unit = selectedParameter.Unit;
|
|
selectedMaterial.ParameterDescription =
|
|
selectedParameter.DisplayNameTW + ': ' + selectedParameter.Remark;
|
|
selectedMaterial.localDisplayParameterValue = selectedParameter.Value;
|
|
selectedMaterial.ParaSourceDesc = selectedParameter.Description;
|
|
$scope.updateKgCO2e(selectedMaterial);
|
|
LCADetailCacheService.updateMaterialsAsync([selectedMaterial]);
|
|
$scope.calcTotalKgCO2e();
|
|
};
|
|
};
|
|
|
|
$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);
|
|
}
|
|
};
|
|
|
|
$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) return;
|
|
|
|
$http.delete($scope.baseUrl+"/api/Material/DeleteMaterial/" + selected.LCAID + "/" + selected.ID)
|
|
.success(function () {
|
|
$scope.model.materials.splice($scope.model.materials.indexOf(selected), 1);
|
|
$scope.calcTotalKgCO2e();
|
|
});
|
|
};
|
|
|
|
MultiLanguageService.getResourceAsync()
|
|
.then(function (response) {
|
|
resource = response;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
|
|
// Get the materials and join parameter in local
|
|
$q.all([LCADetailCacheService.getAllMaterialAsync($routeParams.LCAID),
|
|
$http.get($scope.baseUrl+'/api/BOM/GetSheetHeader/' + $routeParams.LCAID)])
|
|
.then(function (data) {
|
|
$scope.model = {
|
|
materials: $filter('filter')(data[0], { MaterialType: 0 })
|
|
};
|
|
var sheetHeader = data[1].data.SheetHeader ? data[1].data.SheetHeader : {};
|
|
// $scope.sheetHeader is parent's object
|
|
$scope.sheetHeader.SheetFillerName = sheetHeader.SheetFillerName;
|
|
$scope.sheetHeader.Phone = sheetHeader.Phone;
|
|
$scope.sheetHeader.Department = sheetHeader.Department;
|
|
$scope.calcTotalKgCO2e();
|
|
$scope.reNumber = function (materialType) {
|
|
$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);
|
|
})
|
|
};
|
|
});
|
|
|
|
|
|
// 新增材質組成項目
|
|
$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.updateParameter();
|
|
}
|
|
|
|
$scope.deleteComposite = function (index) {
|
|
$scope.toBeEdit.MaterialComposites.splice(index, 1)
|
|
$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) {
|
|
selectedMaterial.KgCO2e = selectedMaterial.ActivityIntensity * selectedMaterial.ParameterValue;
|
|
selectedMaterial.KgCO2e_BOM = selectedMaterial.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);
|
|
}
|
|
|
|
var calculateParentKgCO2e = function (material) {
|
|
var parentKgCO2e = 0;
|
|
|
|
if (material.isChild) return material.KgCO2e;
|
|
|
|
angular.forEach(material.ChildMaterials, function (childMaterial) {
|
|
parentKgCO2e += childMaterial.KgCO2e;
|
|
});
|
|
|
|
return parentKgCO2e;
|
|
};
|
|
|
|
$scope.calcTotalKgCO2e = function (){
|
|
$scope.materialsSumKgCO2e = 0;
|
|
angular.forEach($scope.model.materials, function (model) {
|
|
$scope.materialsSumKgCO2e += model.KgCO2e
|
|
});
|
|
}
|
|
}]); |