demo20230512/Browser_Local/js/Model.js
2023-05-12 10:20:28 +08:00

439 lines
19 KiB
JavaScript

/*
Logic Solution
WeeeCarbonFootprint
Factory for CarbonFootprint
*/
/*
MaterialFactory.create : construct a material
data: []
LCAID : int
MaterialFactory.export : export the file
materialList: [] of material
typeOptions: [] of {ID:int , Name:string}
*/
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
angular.module('Services')
.factory('MaterialFactory', ['MultiLanguageService', '$cookies', 'ExportCsvService', function (MultiLanguageService, $cookies, ExportCsvService) {
var config;
var currentCutlture = 'tw';
if ($cookies._culture == 'zh-cn') { currentCutlture = 'cn' }
else if ($cookies._culture == 'en-us') { currentCutlture = 'en' }
var weightUnit = ['mg', 'g', 'kg', 't'];
var volumeUnit = ['ml', 'L', 'm3'];
this.create = function (data, LCAID) {
//console.log('create', data);
try {
var material = {};
material.Level = data[0];
material.PartNumber = data[1];
material.Name = data[2];
material.Length = parseFloat(data[3]) ? data[3] : 0;
material.Area = parseFloat(data[4]) ? data[4] : 0;
material.Scalar = parseFloat(data[5]) ? data[5] : 0;
material.WeightUnit = data[6];
material.Volume = parseFloat(data[7]) ? data[7] : 0;
material.VolumeUnit = data[8] == '' ? 'm3' : data[8];
material.Quantity = parseFloat(data[9]) ? data[9] : 0;;
material.SupplierCompanyName = data[10];
material.SupplierCompanyType = data[11];
material.ParameterValue = parseFloat(data[12]) ? data[12] : 0;;
material.Unit = data[13];
material.ParameterDescription = data[14];
material.ParaSourceDesc = data[15];
var DQIID;
var DQIName = data[16];
if (DQIName == Resource['MetaDataDQI1']) {
DQIID = 0;
} else if (DQIName == Resource['MetaDataDQI2']) {
DQIID = 1;
} else if (DQIName == Resource['MetaDataDQI3']) {
DQIID = 2;
}
material.DQI = DQIID;
material.Description = data[17];
material.LCAID = LCAID;
material.ParameterSource = 2;
if (data.length !== getObjectSize(material) - 2) {
throw Resource['EXCELFileFormatWrong'];
}
// if find incorrect DQI
if (material.DQI === undefined) {
throw Resource['MataDataDQI'] + Resource['ErrorText'] + '\'' + Resource['MetaDataDQI1'] + '\',\'' + Resource['MetaDataDQI2'] + '\',\'' + Resource['MetaDataDQI3'] + '\'';
}
if (material.Level == '' || material.Level == undefined) {
throw Resource['Level'] + Resource['RequiredMessage'];
}
if (material.PartNumber == '' || material.PartNumber == undefined) {
throw Resource['PartNumber'] + Resource['RequiredMessage'];
}
if (material.Name == '' || material.Name == undefined) {
throw Resource['MaterialName'] + Resource['RequiredMessage'];
}
if (weightUnit.indexOf(material.WeightUnit) == -1) {
throw Resource['Weight'] + Resource['MaterialUnit'] + Resource['ErrorText'] + '\'mg\', \'g\', \'kg\', \'t\'';
}
if (volumeUnit.indexOf(material.VolumeUnit) == -1) {
throw Resource['StaticLabelGlobal_MeasureOfVolume'] + Resource['MaterialUnit'] + Resource['ErrorText'] + '\'ml\', \'L\', \'m3\'';
}
var weiMul = 1;
if (material.WeightUnit == 'mg')
weiMul = 0.000001;
else if (material.WeightUnit == 'g')
weiMul = 0.001;
else if (material.WeightUnit == 't')
weiMul = 1000;
material.ActivityIntensity = material.Scalar * weiMul * material.Quantity;
material.KgCO2e = material.ActivityIntensity * material.ParameterValue;
return material;
}
catch (err) {
console.log(err);
return { error: err };
}
};
// Multi-language resource object
var Resource;
// Get multilanguage resource
MultiLanguageService.getResourceAsync()
.then(function (resource) {
Resource = resource;
}, function (error) {
console.log(error);
});
this.export = function (materialList, typeOptions, exportType = 'csv', suffixName = '') {
if (exportType == 'csv') {
//console.log("Model-export");
var data = [];
var transform = [];
var header = [];
// header
header.push(Resource['Level'] + Resource['HeaderMessageRequire']);
header.push(Resource['PartNumber'] + Resource['HeaderMessageRequire']);
header.push(Resource['MaterialName'] + Resource['HeaderMessageRequire']);
header.push(Resource['Length'] + ' (m)');
header.push(Resource['StaticLabelGlobal_MeasureOfArea'] + ' (m2)');
header.push(Resource['Weight'] + Resource['HeaderMessageRequire']);
header.push(Resource['Weight'] + Resource['MaterialUnit'] + Resource['HeaderMessageRequire']);
header.push(Resource['StaticLabelGlobal_MeasureOfVolume']);
header.push(Resource['StaticLabelGlobal_MeasureOfVolume'] + Resource['MaterialUnit']);
header.push(Resource['UsedQuantity_BOM'] + Resource['HeaderMessageRequire']);
header.push(Resource['MaterialSupplierCompany']);
header.push(Resource['MaterialSupplierCompanyType']);
header.push(Resource['Parameter'] + Resource['HeaderMessageRequire']);
header.push(Resource['ParameterUnit']);
header.push(Resource['ParaDesc']);
header.push(Resource['ParameterSourceDesc']);
header.push(Resource['MataDataDQI'] + Resource['HeaderMessageRequire']);
header.push(Resource['Remark']);
data.push(header);
angular.forEach(typeOptions, function (entry) {
transform[entry.ID] = entry.Name
})
// body
angular.forEach(materialList, function (entry) {
//console.log('for each', entry);
var csv = [];
//column 1
csv.push(entry.Level);
//column 2
csv.push(entry.PartNumber);
//column 3
csv.push(entry.Name);
//column 6
csv.push(entry.Length);
//column 7
csv.push(entry.Area);
//column 8
csv.push(entry.Scalar);
//column 9
csv.push(entry.WeightUnit);
//column 10
csv.push(entry.Volume);
//column 11
csv.push(entry.VolumeUnit);
//column 13
csv.push(entry.Quantity);
//column 14
csv.push(entry.SupplierCompanyName);
//column 15
csv.push(entry.SupplierCompanyType);
//column 16
csv.push(entry.ParameterValue);
//column 17
csv.push(entry.Unit);
//column 18
csv.push(entry.ParameterDescription);
//column 19
csv.push(entry.ParaSourceDesc);
//column 20
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']);
}
//column 21
csv.push(entry.Description);
data.push(csv);
})
ExportCsvService.startExport(data, "Material" + suffixName + ".csv");
}
else { }
}
return this;
}])
.factory('HighLevelAnalyzeFactory', ['MultiLanguageService', '$cookies', 'ExportCsvService', function (MultiLanguageService, $cookies, ExportCsvService) {
var config;
var currentCutlture = 'tw';
if ($cookies._culture == 'zh-cn') { currentCutlture = 'cn' }
else if ($cookies._culture == 'en-us') { currentCutlture = 'en' }
var weightUnit = ['mg', 'g', 'kg', 't'];
var volumeUnit = ['ml', 'L', 'm3'];
this.create = function (data, LCAID) {
try {
var material = {};
material.Level = data[0];
material.Name = data[1];
material.Components = data[2];
material.orderNo = parseFloat(data[3]) ? data[3] : 0;
material.Composite = data[4];
material.PartNumber = data[5];
material.Length = parseFloat(data[6]) ? data[6] : 0;
material.Area = parseFloat(data[7]) ? data[7] : 0;
material.Scalar = parseFloat(data[8]) ? data[8] : 0;
material.WeightUnit = data[9];
material.Volume = parseFloat(data[10]) ? data[10] : 0;
material.VolumeUnit = data[11] == '' ? 'm3' : data[11];
material.Quantity_BOM = data[12];
material.Quantity = parseFloat(data[13]) ? data[13] : 0;;
material.SupplierCompanyName = data[14];
material.SupplierCompanyType = data[15];
material.ParameterValue = parseFloat(data[16]) ? data[16] : 0;;
material.Unit = data[17];
material.ParameterDescription = data[18];
material.ParaSourceDesc = data[19];
var DQIID;
var DQIName = data[20];
if (DQIName == Resource['MetaDataDQI1']) {
DQIID = 0;
} else if (DQIName == Resource['MetaDataDQI2']) {
DQIID = 1;
} else if (DQIName == Resource['MetaDataDQI3']) {
DQIID = 2;
}
material.DQI = DQIID;
material.Description = data[21];
material.LCAID = LCAID;
material.ParameterSource = 2;
if (data.length !== getObjectSize(material) - 2) {
throw Resource['EXCELFileFormatWrong'];
}
// if find incorrect DQI
if (material.DQI === undefined) {
throw Resource['MataDataDQI'] + Resource['ErrorText'] + '\'' + Resource['MetaDataDQI1'] + '\',\'' + Resource['MetaDataDQI2'] + '\',\'' + Resource['MetaDataDQI3'] + '\'';
}
if (material.Level === '' || material.Level === undefined) {
throw Resource['Level'] + Resource['RequiredMessage'];
}
if (material.PartNumber === '' || material.PartNumber === undefined) {
throw Resource['PartNumber'] + Resource['RequiredMessage'];
}
if (material.Name === '' || material.Name === undefined) {
throw Resource['MaterialName'] + Resource['RequiredMessage'];
}
if (material.orderNo === '' || material.orderNo === undefined) {
throw Resource['OrderNo'] + Resource['RequiredMessage'];
}
if (material.Components === '' || material.Components === undefined) {
throw Resource['Components'] + Resource['RequiredMessage'];
}
if (material.Composite === '' || material.Composite === undefined) {
throw Resource['MaterialComposition1'] + Resource['RequiredMessage'];
}
if (weightUnit.indexOf(material.WeightUnit) == -1) {
throw Resource['Weight'] + Resource['MaterialUnit'] + Resource['ErrorText'] + '\'mg\', \'g\', \'kg\', \'t\'';
}
if (volumeUnit.indexOf(material.VolumeUnit) == -1) {
throw Resource['StaticLabelGlobal_MeasureOfVolume'] + Resource['MaterialUnit'] + Resource['ErrorText'] + '\'ml\', \'L\', \'m3\'';
}
var weiMul = 1;
if (material.WeightUnit == 'mg')
weiMul = 0.000001;
else if (material.WeightUnit == 'g')
weiMul = 0.001;
else if (material.WeightUnit == 't')
weiMul = 1000;
material.ActivityIntensity_BOM = material.Scalar * weiMul * material.Quantity_BOM;
material.ActivityIntensity = material.Scalar * weiMul * material.Quantity;
material.KgCO2e_BOM = material.ActivityIntensity_BOM * material.ParameterValue;
material.KgCO2e = material.ActivityIntensity * material.ParameterValue;
return material;
}
catch (err) {
console.log(err);
return { error: err };
}
};
// Multi-language resource object
var Resource;
// Get multilanguage resource
MultiLanguageService.getResourceAsync()
.then(function (resource) {
Resource = resource;
}, function (error) {
console.log(error);
});
this.export = function (materialList, typeOptions, exportType = 'csv', suffixName = '') {
if (exportType == 'csv') {
console.log("Model-export");
var data = [];
var transform = [];
var header = [];
// header
header.push(Resource['Level'] + Resource['HeaderMessageRequire']);
header.push(Resource['PartNumber'] + Resource['HeaderMessageRequire']);
header.push(Resource['MaterialName'] + Resource['HeaderMessageRequire']);
header.push(Resource['OrderNo'] + Resource['HeaderMessageRequire']);
header.push(Resource['Components'] + Resource['HeaderMessageRequire']);
header.push(Resource['MaterialComposition1'] + Resource['HeaderMessageRequire']);
header.push(Resource['Length'] + ' (m)');
header.push(Resource['StaticLabelGlobal_MeasureOfArea'] + ' (m2)');
header.push(Resource['Weight'] + Resource['HeaderMessageRequire']);
header.push(Resource['Weight'] + Resource['MaterialUnit'] + Resource['HeaderMessageRequire']);
header.push(Resource['StaticLabelGlobal_MeasureOfVolume']);
header.push(Resource['StaticLabelGlobal_MeasureOfVolume'] + Resource['MaterialUnit']);
header.push(Resource['UsedQuantity_BOM'] + Resource['HeaderMessageRequire']);
header.push(Resource['UsedQuantity'] + Resource['HeaderMessageRequire']);
header.push(Resource['MaterialSupplierCompany']);
header.push(Resource['MaterialSupplierCompanyType']);
header.push(Resource['Parameter'] + Resource['HeaderMessageRequire']);
header.push(Resource['ParameterUnit']);
header.push(Resource['ParaDesc']);
header.push(Resource['ParameterSourceDesc']);
header.push(Resource['MataDataDQI'] + Resource['HeaderMessageRequire']);
header.push(Resource['Remark']);
data.push(header);
angular.forEach(typeOptions, function (entry) {
transform[entry.ID] = entry.Name
})
// body
angular.forEach(materialList, function (entry) {
var csv = [];
//column 1
csv.push(entry.Level);
//column 2
csv.push(entry.PartNumber);
//column 3
csv.push(entry.Name);
//column 4
csv.push(entry.orderNo);
//column 5
csv.push(entry.Components);
//column 6
csv.push(entry.Composite);
//column 7
csv.push(entry.Length);
//column 8
csv.push(entry.Area);
//column 9
csv.push(entry.Scalar);
//column 10
csv.push(entry.WeightUnit);
//column 11
csv.push(entry.Volume);
//column 12
csv.push(entry.VolumeUnit);
//column 13
csv.push(entry.Quantity_BOM);
//column 14
csv.push(entry.Quantity);
//column 15
csv.push(entry.SupplierCompanyName);
//column 16
csv.push(entry.SupplierCompanyType);
//column 17
csv.push(entry.ParameterValue);
//column 18
csv.push(entry.Unit);
//column 19
csv.push(entry.ParameterDescription);
//column 20
csv.push(entry.ParaSourceDesc);
//column 21
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']);
}
//column 22
csv.push(entry.Description);
data.push(csv);
})
ExportCsvService.startExport(data, "Material" + suffixName +".csv");
}
else { }
}
return this;
}])
.factory('SupplierFactory', function () {
function Supplier() {
}
this.create = function (data) {
try {
var supplier = {};
supplier.VendorCode = data[0];
supplier.Name = data[1];
supplier.Address = data[2];
supplier.Phone = data[3];
supplier.ContactName = data[4];
supplier.ContactEmail = data[5];
supplier.ContactPhone = data[6];
supplier.Description = data[7];
return supplier;
}
catch (err) {
return { error: err };
}
}
return this;
})