723 lines
35 KiB
JavaScript
723 lines
35 KiB
JavaScript
/*
|
|
** Views/Partial/LCA/Transport.cshtml is using this controller
|
|
*/
|
|
|
|
var arr = window.location.href.split("/");
|
|
if (arr[3].indexOf('app') == 0)
|
|
baseUrl = '';
|
|
else
|
|
baseUrl = '/' + arr[3];
|
|
|
|
angular.module('CarbonFootprint')
|
|
.controller('LCATransportCtrl', ['$scope', '$http', '$routeParams', '$filter', 'JoinSurveyItemAndParameter', '$q', 'MultiLanguageService', 'ExportCsvService', 'LCADetailCacheService',
|
|
function ($scope, $http, $routeParams, $filter, JoinService, $q, MultiLanguageService, ExportCsvService, LCADetailCacheService) {
|
|
|
|
// Multi-language resource object
|
|
var resource;
|
|
var options;
|
|
|
|
// 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.AllocationUpstream = -1;
|
|
$scope.AllocationDownstream = -1;
|
|
$scope.RatioUpstream = 0.00;
|
|
$scope.RatioDownstream = 0.00;
|
|
|
|
LCADetailCacheService.getLCADetailAsync($routeParams.LCAID).then(function (result) {
|
|
$scope.lcaDetail = result;
|
|
$scope.productLCA = result.productLCA;
|
|
$scope.AllocationUpstream = $scope.productLCA["ProductAllocation" + $routeParams.DetailType + "Upstream"];
|
|
$scope.AllocationDownstream = $scope.productLCA["ProductAllocation" + $routeParams.DetailType + "Downstream"];
|
|
|
|
/*
|
|
if ($scope.Allocation == 0) //重量分配
|
|
//$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.product.Weight, $scope.lcaDetail.FabProductionWeight);
|
|
$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.Weight / $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.Allocation == 1) //面積分配
|
|
//$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.product.AreaSize, $scope.lcaDetail.FabProductionArea);
|
|
$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.Allocation == 2) //產量分配
|
|
//$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
else if ($scope.Allocation == 3) //經濟分配
|
|
//$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionEconomic, $scope.lcaDetail.FabProductionEconomic);
|
|
$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.Allocation == 4) //工時分配
|
|
//$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionHour, $scope.lcaDetail.FabProductionHour);
|
|
$scope.Ratio = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
*/
|
|
if ($scope.AllocationUpstream == 0)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.Weight / $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 1)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 2)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
else if ($scope.AllocationUpstream == 3)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 4)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 99)
|
|
$scope.RatioUpstream = 1;
|
|
|
|
if ($scope.AllocationDownstream == 0)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.Weight / $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationDownstream == 1)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationDownstream == 2)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
else if ($scope.AllocationDownstream == 3)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationDownstream == 4)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationDownstream == 99)
|
|
$scope.RatioDownstream = 1;
|
|
calcTotalKgCO2e();
|
|
})
|
|
|
|
$scope.modifyCacheAllocationUpstream = function () {
|
|
var cache = LCADetailCacheService.getCache('LCADetail');
|
|
if (cache.get($routeParams.LCAID)) {
|
|
var data = cache.get($routeParams.LCAID)
|
|
data.productLCA["ProductAllocation" + $routeParams.DetailType + "Upstream"] = $scope.AllocationUpstream;
|
|
cache.put($routeParams.LCAID, data);
|
|
}
|
|
}
|
|
$scope.modifyCacheAllocationDownstream = function () {
|
|
var cache = LCADetailCacheService.getCache('LCADetail');
|
|
if (cache.get($routeParams.LCAID)) {
|
|
var data = cache.get($routeParams.LCAID)
|
|
data.productLCA["ProductAllocation" + $routeParams.DetailType + "Downstream"] = $scope.AllocationDownstream;
|
|
cache.put($routeParams.LCAID, data);
|
|
}
|
|
}
|
|
|
|
$scope.onAllocationUpstreamChange = function (Allocation) {
|
|
$scope.AllocationUpstream = Allocation
|
|
if ($scope.AllocationUpstream == 0)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.Weight / $scope.lcaDetail.FabProductionWeight, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 1)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.product.AreaSize / $scope.lcaDetail.FabProductionArea, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 2)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
else if ($scope.AllocationUpstream == 3)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionEconomic / $scope.lcaDetail.FabProductionEconomic, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 4)
|
|
$scope.RatioUpstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs * $scope.lcaDetail.ProductProductionHour / $scope.lcaDetail.FabProductionHour, 1, $scope.lcaDetail.ProductProductionPcs);
|
|
else if ($scope.AllocationUpstream == 99)
|
|
$scope.RatioUpstream = 1;
|
|
|
|
$http.post($scope.baseUrl+'/api/LCA/SaveAllocation/' + $routeParams.LCAID + '/' + $routeParams.DetailType + 'Upstream/' + $scope.AllocationUpstream)
|
|
.success(function (data) {
|
|
calcTotalKgCO2e();
|
|
$scope.modifyCacheAllocation();
|
|
})
|
|
}
|
|
|
|
$scope.onAllocationDownstreamChange = function (Allocation) {
|
|
$scope.AllocationDownstream = Allocation
|
|
if ($scope.AllocationDownstream == 0)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.product.Weight, $scope.lcaDetail.FabProductionWeight);
|
|
else if ($scope.AllocationDownstream == 1)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.product.AreaSize, $scope.lcaDetail.FabProductionArea);
|
|
else if ($scope.AllocationDownstream == 2)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, 1, $scope.lcaDetail.FabProductionPcs);
|
|
else if ($scope.AllocationDownstream == 3)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionEconomic, $scope.lcaDetail.FabProductionEconomic);
|
|
else if ($scope.AllocationDownstream == 4)
|
|
$scope.RatioDownstream = $scope.calculate($scope.lcaDetail.ProductProductionPcs, $scope.lcaDetail.ProductProductionHour, $scope.lcaDetail.FabProductionHour);
|
|
else if ($scope.AllocationDownstream == 99)
|
|
$scope.RatioDownstream = 1;
|
|
|
|
$http.post($scope.baseUrl+'/api/LCA/SaveAllocation/' + $routeParams.LCAID + '/' + $routeParams.DetailType + 'Downstream/' + $scope.AllocationDownstream)
|
|
.success(function (data) {
|
|
calcTotalKgCO2e();
|
|
$scope.modifyCacheAllocation();
|
|
})
|
|
}
|
|
|
|
$scope.createProcess = function () {
|
|
var toBeCreated = {};
|
|
|
|
toBeCreated.ID = 0;
|
|
toBeCreated.LCAID = $routeParams.LCAID;
|
|
|
|
// Reset transport type dropdown list options
|
|
$scope.transportTypes = [];
|
|
|
|
// For modal form
|
|
$scope.selectRow.toBeEdit = toBeCreated;
|
|
$scope.modalFormOption.show = true;
|
|
$scope.dataQualityOption = {};
|
|
$scope.dataQualityOption.data = {};
|
|
$scope.isEdit = false;
|
|
|
|
$scope.save = function () {
|
|
JoinService.joinAsync([toBeCreated]).then(function () {
|
|
// Update Ton*Km & KgCO2e properties
|
|
//setTonKm([toBeCreated]);
|
|
//setKgCO2e([toBeCreated]);
|
|
|
|
$http.post($scope.baseUrl+'/api/Transport/Save/' + toBeCreated.LCAID + '/' + toBeCreated.ID, toBeCreated)
|
|
.success(function (data) {
|
|
|
|
// Join transport data into model
|
|
data.ModeOfTransport = getModeOfTransport(options[data.NonYearlyParameterID]);
|
|
$scope.model.unshift(data);
|
|
$scope.modalFormOption.show = false;
|
|
calcTotalKgCO2e();
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
$scope.editProcess = function (selected) {
|
|
var toBeEdit = $scope.selectRow.toBeEdit = angular.copy(selected);
|
|
|
|
// When user edit one entity, transport date cannot display because transport date type is datetime.
|
|
// So we format transport date from datetime to date
|
|
toBeEdit.TransportDate = $filter('date')(toBeEdit.TransportDate, 'yyyy/MM/dd');
|
|
|
|
// Reset transport type dropdown list options
|
|
$scope.transportTypes = [];
|
|
|
|
angular.forEach(options, function (value) {
|
|
if (value.search(toBeEdit.ModeOfTransport) !== -1 && toBeEdit.ModeOfTransport) {
|
|
var transportType = getTransportType(value);
|
|
$scope.transportTypes.push(transportType);
|
|
}
|
|
});
|
|
|
|
$scope.transportTypes = filterDuplicatedData($scope.transportTypes);
|
|
|
|
$scope.modalFormOption.show = true;
|
|
$scope.dataQualityOption = {};
|
|
$scope.dataQualityOption.data = {};
|
|
$scope.isEdit = true;
|
|
|
|
$scope.save = function () {
|
|
JoinService.joinAsync([toBeEdit]).then(function () {
|
|
// Update Ton*Km & KgCO2e properties
|
|
//setTonKm([toBeEdit]);
|
|
//setKgCO2e([toBeEdit]);
|
|
|
|
$http.post($scope.baseUrl+'/api/Transport/Save/' + toBeEdit.LCAID + '/' + toBeEdit.ID, toBeEdit)
|
|
.success(function (data) {
|
|
|
|
// Update transport data
|
|
toBeEdit.ModeOfTransport = getModeOfTransport(options[toBeEdit.NonYearlyParameterID]);
|
|
angular.copy(data, selected);
|
|
$scope.modalFormOption.show = false;
|
|
calcTotalKgCO2e();
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
$scope.dataQualityShow = function (selected) {
|
|
$http.get($scope.baseUrl+'/api/DataQualityLevel/GetByTableId/ProductLCAFabSurveyForm_Transports/' + 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;
|
|
}).error(function (data, status) {
|
|
alert(data.ExceptionMessage);
|
|
});
|
|
};
|
|
|
|
$scope.deleteProcess = function (selected) {
|
|
var isConfirm = confirm(resource.DeleteItemMsg);
|
|
|
|
if (isConfirm) {
|
|
$http.delete($scope.baseUrl+'/api/Transport/Delete/' + selected.ID)
|
|
.success(function () {
|
|
var index = $scope.model.indexOf(selected);
|
|
$scope.model.splice(index, 1);
|
|
|
|
// This operate is base on selectRow directive in TableEdit.js file
|
|
$scope.selectRow.unSelect();
|
|
calcTotalKgCO2e();
|
|
})
|
|
.error(function (error) {
|
|
console.log(error);
|
|
});
|
|
}
|
|
};
|
|
|
|
/* modelBuilder for create & export */
|
|
/** Caution!: The 'ParameterID' property depends on 'options'.
|
|
This modelBuilder is binded with this controller.
|
|
Refactoring modelBuilder.create() with more arugments will be better **/
|
|
$scope.modelBuilder = {
|
|
create: function (data, LCAID) {
|
|
try {
|
|
// get DQIID by DQIName
|
|
var DQIID;
|
|
var DQIName = data[13];
|
|
if (DQIName == resource['MetaDataDQI1'])
|
|
DQIID = 0;
|
|
else if(DQIName == resource['MetaDataDQI2'])
|
|
DQIID = 1;
|
|
else if(DQIName == resource['MetaDataDQI3'])
|
|
DQIID = 2;
|
|
var TransportType = '';
|
|
var TransportTypeName = data[0];
|
|
if (TransportTypeName == resource['UpstreamTransport'])
|
|
TransportType = 0;
|
|
else if (TransportTypeName == resource['DownstreamTransport'])
|
|
TransportType = 1;
|
|
|
|
var model = {
|
|
TransportType: TransportType,
|
|
TransportDate: data[1],
|
|
TransportContent: data[2],
|
|
JourneyNO: data[3],
|
|
StartLocation: data[4],
|
|
EndLocation: data[5],
|
|
TransportDistance: data[6],
|
|
TransportQuantity: data[7],
|
|
TransportWeight: data[8],
|
|
Description: data[9],
|
|
ParameterValue: data[10],
|
|
ParameterUnit: data[11],
|
|
ParaDesc: data[12],
|
|
DQI: DQIID,
|
|
LCAID: LCAID
|
|
};
|
|
|
|
// check imported data length and model length (-1 for LCAID)
|
|
if (data.length !== getObjectSize(model) - 1) {
|
|
throw resource['EXCELFileFormatWrong'];
|
|
}
|
|
|
|
// if find incorrect DQI
|
|
if (model.DQI === undefined) {
|
|
throw resource['MataDataDQI'] + resource['ErrorText'] + '\'' + resource['MetaDataDQI1'] + '\',\'' + resource['MetaDataDQI2'] + '\',\'' + resource['MetaDataDQI3'] + '\'';
|
|
}
|
|
|
|
return model;
|
|
|
|
} catch (err) {
|
|
return { error: err };
|
|
}
|
|
|
|
},
|
|
export: function (list, exportType = 'txt') {
|
|
if (exportType == 'csv') {
|
|
var data = [];
|
|
|
|
// header
|
|
data.push([
|
|
resource['Type'],
|
|
resource['TransportDate'],
|
|
resource['TransportContent'],
|
|
resource['ShipmentNo'],
|
|
resource['StartLocation'],
|
|
resource['EndLocation'],
|
|
resource['TransportDistance'] + ' (km)',
|
|
resource['TransportShipmentQuantity'],
|
|
resource['ShipmentProductWeight'] + ' (kg)',
|
|
resource['RemarkAndExplanation'],
|
|
resource['Parameter'],
|
|
resource['ParameterUnit'],
|
|
resource['ParaDesc'],
|
|
resource['MataDataDQI']
|
|
]);
|
|
|
|
// body
|
|
angular.forEach(list, function (entry) {
|
|
var csv = [];
|
|
//column 1
|
|
if (entry.TransportType == 0) {
|
|
csv.push(resource['UpstreamTransport']);
|
|
} else if (entry.TransportType == 1) {
|
|
csv.push(resource['DownstreamTransport']);
|
|
}
|
|
//column 2
|
|
var localeDateString = new Date(entry.TransportDate).toLocaleDateString();
|
|
csv.push(localeDateString);
|
|
//column 3
|
|
csv.push(entry.TransportContent);
|
|
//column 4
|
|
csv.push(entry.JourneyNO);
|
|
//column 5
|
|
csv.push(entry.StartLocation);
|
|
//column 6
|
|
csv.push(entry.EndLocation);
|
|
//column 7
|
|
csv.push(entry.TransportDistance);
|
|
//column 8
|
|
csv.push(entry.TransportQuantity);
|
|
//column 9
|
|
csv.push(entry.TransportWeight);
|
|
//column 10
|
|
csv.push(entry.Description);
|
|
//column 11
|
|
csv.push(entry.ParameterValue);
|
|
//column 12
|
|
csv.push(entry.ParameterUnit);
|
|
//column 13
|
|
csv.push(entry.ParaDesc);
|
|
//column 14
|
|
if (entry.DQI == 0) {
|
|
csv.push(resource['MetaDataDQI1']);
|
|
} else if (entry.DQI == 1) {
|
|
csv.push(resource['MetaDataDQI2']);
|
|
} else if (entry.DQI == 2) {
|
|
csv.push(resource['MetaDataDQI3']);
|
|
}
|
|
|
|
data.push(csv);
|
|
});
|
|
ExportCsvService.startExport(data, "Export_Transport.csv");
|
|
}
|
|
else {
|
|
|
|
//var csv = '\uFEFF';
|
|
//var link;
|
|
//var delimeter = '\t';
|
|
|
|
//// header
|
|
//csv +=resource['Type'] + delimeter
|
|
// + resource['TransportDate'] + delimeter
|
|
// + resource['JourneyNO'] + delimeter
|
|
// + resource['StartLocation'] + delimeter
|
|
// + resource['EndLocation'] + delimeter
|
|
// + resource['TransportDistance'] + delimeter
|
|
// + resource['TransportVanType'] + delimeter
|
|
// + resource['TransportQuantity'] + delimeter
|
|
// + resource['ProductWeightIncludingWrappingMaterials'] + delimeter
|
|
// + resource['MataDataDQI'];
|
|
|
|
//// body
|
|
//angular.forEach(list, function (entry) {
|
|
// csv += '\r\n';
|
|
// //column 1
|
|
// csv += entry.TransportType + delimeter;
|
|
// //column 1
|
|
// if (entry.TransportType == 0) {
|
|
// csv += resource['UpstreamTransport'] + delimeter;
|
|
// } else if (entry.TransportType == 1) {
|
|
// csv += resource['DownstreamTransport'] + delimeter;
|
|
// }
|
|
// //column 2
|
|
// var localeDateString = new Date(entry.TransportDate).toLocaleDateString();
|
|
// csv += localeDateString + delimeter;
|
|
// //column 3
|
|
// csv += entry.JourneyNO + delimeter;
|
|
// //column 4
|
|
// csv += entry.StartLocation + delimeter;
|
|
// //column 5
|
|
// csv += entry.EndLocation + delimeter;
|
|
// //column 6
|
|
// csv += entry.TransportDistance + delimeter;
|
|
// //column 7
|
|
// csv += entry.TransportType + delimeter;
|
|
// //column 8
|
|
// csv += entry.TransportQuantity + delimeter;
|
|
// //column 9
|
|
// csv += entry.TransportWeight + delimeter;
|
|
// //column 10
|
|
// if (entry.DQI == 0) {
|
|
// csv += resource['MetaDataDQI1'];
|
|
// } else if (entry.DQI == 1) {
|
|
// csv += resource['MetaDataDQI2'];
|
|
// } else if (entry.DQI == 2) {
|
|
// csv += resource['MetaDataDQI3'];
|
|
// }
|
|
//});
|
|
//var filename = "Export_Transport.txt";
|
|
//require([baseUrl+'/Scripts/FileSaver.js'],
|
|
// function requrieSuccess(SaveAs) {
|
|
// var textFileAsBlob = new Blob([csv], { type: 'text;charset=utf-8,' });
|
|
// saveAs(textFileAsBlob, filename)
|
|
// },
|
|
// function requireError(error) {
|
|
// }
|
|
//);
|
|
}
|
|
}
|
|
};
|
|
|
|
/* file import options & modal control */
|
|
$scope.fileImportOptions = {
|
|
modelBuilder: $scope.modelBuilder,
|
|
saveAsync: batchCreateAndUpdateCache
|
|
};
|
|
|
|
$scope.fileImportModal = {
|
|
show: false
|
|
};
|
|
|
|
/* quote options & modal control */
|
|
$scope.quoteOptions = {
|
|
queryDataAsyncFn: function (LCAID) {
|
|
var deferred = $q.defer();
|
|
|
|
getAllDataAsync(LCAID).then(function (data) {
|
|
deferred.resolve(data.Transports);
|
|
});
|
|
|
|
return deferred.promise;
|
|
},
|
|
saveAsyncFn: batchCreateAndUpdateCache
|
|
};
|
|
|
|
$scope.quoteModal = {
|
|
show: false
|
|
};
|
|
|
|
// Get data and prepare $scope.model
|
|
getAllDataAsync($routeParams.LCAID)
|
|
.then(function (response) {
|
|
options = response.Options;
|
|
var sheetHeader = response.SheetHeader ? response.SheetHeader : {};
|
|
|
|
if (response.Transports) {
|
|
$scope.model = response.Transports;
|
|
}
|
|
|
|
// Get each model's ModeOfTransport and TransportType by "ParameterID"
|
|
angular.forEach($scope.model, function (model) {
|
|
model.ModeOfTransport = getModeOfTransport(options[model.NonYearlyParameterID]);
|
|
//model.TransportType = getTransportType(model.TransportType);
|
|
});
|
|
|
|
// Sources of dropdown lists
|
|
$scope.modeOfTransports = [];
|
|
|
|
// Extract modeOfTransports
|
|
angular.forEach(options, function (value) {
|
|
var modeOfTransport = getModeOfTransport(value);
|
|
$scope.modeOfTransports.push(modeOfTransport);
|
|
});
|
|
|
|
// Duplicated modeOfTransports
|
|
$scope.modeOfTransports = filterDuplicatedData($scope.modeOfTransports);
|
|
|
|
// $scope.sheetHeader is parent's object
|
|
$scope.sheetHeader.SheetFillerName = sheetHeader.SheetFillerName;
|
|
$scope.sheetHeader.Phone = sheetHeader.Phone;
|
|
$scope.sheetHeader.Department = sheetHeader.Department;
|
|
calcTotalKgCO2e();
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
|
|
|
|
// Get multilanguage resource
|
|
MultiLanguageService.getResourceAsync()
|
|
.then(function (response) {
|
|
resource = response;
|
|
}, function (error) {
|
|
console.log(error);
|
|
});
|
|
|
|
/**
|
|
* Private function: save all async
|
|
* @param {[Array]} : array of parsed objects
|
|
* @return {[promise]}
|
|
*/
|
|
function batchCreateAndUpdateCache(parsedList) {
|
|
var deferred = $q.defer();
|
|
|
|
// Calculate KgCO2e and sava all
|
|
JoinService.joinAsync(parsedList)
|
|
.then(function () {
|
|
// update KgCO2e properties
|
|
setTonKm(parsedList);
|
|
setKgCO2e(parsedList);
|
|
|
|
$http.post($scope.baseUrl+'/api/Transport/SaveAll/', parsedList)
|
|
.success(function (data) {
|
|
|
|
// Close modals
|
|
$scope.fileImportModal.show = false;
|
|
$scope.quoteModal.show = false;
|
|
|
|
// Update waste transport data
|
|
angular.forEach(data.result, function (model) {
|
|
model.ModeOfTransport = getModeOfTransport(options[model.NonYearlyParameterID]);
|
|
//model.TransportType = getTransportType(options[model.NonYearlyParameterID]);
|
|
});
|
|
|
|
// concat created data and $scope.model
|
|
$scope.model = data.result.concat($scope.model);
|
|
|
|
deferred.resolve(data);
|
|
calcTotalKgCO2e();
|
|
})
|
|
.error(function (e) {
|
|
console.log(e.Message);
|
|
});
|
|
});
|
|
return deferred.promise;
|
|
}
|
|
|
|
/**
|
|
* Update the KgCO2e property of the model objects
|
|
* @param {[Array]} : array of model objects
|
|
*/
|
|
function setKgCO2e(models) {
|
|
angular.forEach(models, function (entry) {
|
|
if (entry.Scalar && entry.parameterResult) {
|
|
entry.KgCO2e = entry.Scalar * parseFloat(entry.ParameterValue);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update the Ton * Km property of the model objects
|
|
* @param {[Array]} : array of model objects
|
|
*/
|
|
function setTonKm(models) {
|
|
angular.forEach(models, function (entry) {
|
|
if (entry.TransportWeight &&
|
|
entry.TransportDistance) {
|
|
entry.Scalar = entry.TransportWeight * entry.TransportDistance / 1000;
|
|
}
|
|
});
|
|
}
|
|
|
|
/* Private function: get all list data from server */
|
|
function getAllDataAsync(LCAID) {
|
|
var deferred = $q.defer();
|
|
$http.get($scope.baseUrl+'/api/Transport/GetByLcaId/' + LCAID)
|
|
.success(function (data) {
|
|
deferred.resolve(data);
|
|
})
|
|
.error(function () {
|
|
deferred.resolve(null);
|
|
});
|
|
return deferred.promise;
|
|
}
|
|
|
|
$scope.$watch('selectRow.toBeEdit.ModeOfTransport', function (modeOfTransport) {
|
|
// if modeOfTransport is changed then reset transportTypes
|
|
//$scope.transportTypes = [];
|
|
|
|
//angular.forEach(options, function (value, key) {
|
|
// if (value.search(modeOfTransport) !== -1 && modeOfTransport) {
|
|
// var transportType = getTransportType(value);
|
|
// $scope.transportTypes[key] = transportType;
|
|
// }
|
|
//});
|
|
});
|
|
|
|
$scope.$watch('selectRow.toBeEdit.TransportType', function () {
|
|
var toBeEdit = $scope.selectRow.toBeEdit;
|
|
|
|
//angular.forEach(options, function (value, key) {
|
|
// if (value.search(toBeEdit.ModeOfTransport) !== -1 &&
|
|
// value.search(toBeEdit.TransportType) !== -1 &&
|
|
// toBeEdit.ModeOfTransport &&
|
|
// toBeEdit.TransportType) {
|
|
// toBeEdit.NonYearlyParameterID = key;
|
|
// }
|
|
//});
|
|
});
|
|
|
|
/**
|
|
* Get substring "modeOfTransport" from a string
|
|
* @param String
|
|
* @returns String
|
|
*/
|
|
function getModeOfTransport(value) {
|
|
return angular.isString(value) ? value.slice(0, value.indexOf(',')) : null;
|
|
}
|
|
|
|
/**
|
|
* Get substring "transportType" from a string
|
|
* @param String
|
|
* @returns String
|
|
*/
|
|
function getTransportType(value) {
|
|
return angular.isString(value) ? value.slice(value.indexOf(',') + 1, value.length) : null;
|
|
}
|
|
|
|
/**
|
|
* Remove identical element from the array
|
|
* @parem Array
|
|
* @returns Array
|
|
*/
|
|
function filterDuplicatedData(array) {
|
|
var filteredArray = [];
|
|
|
|
angular.forEach(array, function (value) {
|
|
if ($.inArray(value, filteredArray) === -1) {
|
|
filteredArray.push(value);
|
|
}
|
|
});
|
|
|
|
return filteredArray;
|
|
}
|
|
|
|
$scope.selectSimaproProcess = function (selectedTransport, isOutside = true) {
|
|
$scope.simaproOption = {};
|
|
$scope.simaproOption.show = true;
|
|
$scope.simaproOption.filter = 13; // SimaproOption.GoodsTransportation 運輸
|
|
$scope.simaproOption.onOk = function (selectedParameter, selectedCategory) {
|
|
//console.log(selectedParameter);
|
|
selectedTransport.ParameterID = selectedParameter.ID;
|
|
selectedTransport.ParameterValue = selectedParameter.Value
|
|
selectedTransport.ParameterDescription =
|
|
selectedParameter.DisplayNameTW + ': ' + selectedParameter.Remark;
|
|
selectedTransport.ParameterUnit = selectedParameter.Unit;
|
|
//if (isOutside) {
|
|
// LCADetailCacheService.updateMaterialsAsync([selectedMaterial]);
|
|
//}
|
|
$scope.updateKgCO2e(selectedTransport);
|
|
}
|
|
};
|
|
|
|
|
|
$scope.updateScalar = function (toBeEdit) {
|
|
//toBeEdit.Scalar = toBeEdit.TransportDistance * toBeEdit.TransportQuantity * toBeEdit.TransportWeight / 1000
|
|
toBeEdit.Scalar = toBeEdit.TransportDistance * toBeEdit.TransportWeight / 1000
|
|
$scope.updateKgCO2e(toBeEdit);
|
|
}
|
|
|
|
$scope.updateKgCO2e = function (toBeEdit) {
|
|
toBeEdit.KgCO2e = toBeEdit.Scalar * toBeEdit.ParameterValue;
|
|
$scope.updateKgCO2eAvg(toBeEdit);
|
|
}
|
|
|
|
$scope.updateKgCO2eAvg = function (toBeEdit) {
|
|
toBeEdit.KgCO2eAvg = toBeEdit.KgCO2e / toBeEdit.TransportQuantity;
|
|
}
|
|
|
|
function calcTotalKgCO2e() {
|
|
$scope.summary = Enumerable.From($scope.model).GroupBy("$.TransportType", "$.KgCO2e", function (key, group) {
|
|
return { TransportType: key, totalKgCO2e: Enumerable.From(group).Sum(), AllocationKgCO2e: Enumerable.From(group).Sum()};
|
|
}).ToArray();
|
|
angular.forEach($scope.summary, function (item) {
|
|
if (item.TransportType == 0)
|
|
item.AllocationKgCO2e = item.totalKgCO2e * $scope.RatioUpstream;
|
|
if (item.TransportType == 1)
|
|
item.AllocationKgCO2e = item.totalKgCO2e * $scope.RatioDownstream;
|
|
});
|
|
}
|
|
}]); |