demo20230512/Supports/CarbonEmissionCalculator.cs
2023-05-12 10:20:28 +08:00

313 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Weee.Models;
using Weee.Models.ExtensionMethods;
namespace Weee.Supports
{
public class CarbonEmissionCalculator
{
public static decimal GetMaterialProductionAndTransportPhase(ICollection<Material> materials)
{
return CalculateMaterialProductionAndTransportPhase(materials);
}
public static decimal GetProductionPhase(ProductLCA productLca)
{
//warning , this code is duplicated in ProductLCAInventoryExcelExporter.cs private function "InsertProductCO2DataInManufacturing"
var snapshot = productLca.SnapShotSurveyResult;
if (snapshot == null) return 0m;
var scopeOneData = snapshot.Scope1FabResult * snapshot.constantFactorInWorkHour;
var scopeTwoData = snapshot.Scope2FabResult * snapshot.constantFactorInWorkHour;
var scopeThreeData = snapshot.Scope3FabResult * snapshot.constantFactorInWorkHour;
var primaryData = scopeOneData + scopeThreeData + scopeTwoData;
var secondaryDataOfManufacturing = (snapshot.SecondaryWasteResult + snapshot.SecondaryWasteTransportResult) * snapshot.constantFactorInWorkHour;
var lcaResultCO2 = secondaryDataOfManufacturing + primaryData;
return lcaResultCO2.HasValue ? lcaResultCO2.Value : 0m;
}
public static decimal GetDeliveryPhase(ProductLCA productLca)
{
return CalculateDeliveryPhase(productLca);
}
public static decimal GetAbandonedPhase(InventoryStageData inventoryStageData)
{
return CalculateAbandonedPhase(inventoryStageData);
}
public static decimal GetSecondDataTypeOfPartsKgCO2e(ICollection<Material> materials)
{
//land mine ! this is related to ProductLCAInventoryExcelExporter.InsertCO2Inventory
return materials.Where(x => x.RequestSent != null)
.Where(x => x.RequestSent.AcceptReplyDate != null)
.Where(x => x.RequestSent.RepliedByWhichLCA != null)
.Sum(x => x.KgCO2e);
}
public static decimal GetPrimaryDataTypeOfPartsKgCO2e(ICollection<Material> materials)
{
decimal primaryDataTypeOfPartsKgCO2e = 0;
foreach (var material in materials)
{
var requestSent = material.RequestSent;
var repliedLca = requestSent != null ? requestSent.RepliedByWhichLCA : null;
if (requestSent == null || repliedLca == null) continue;
var totalKgCO2e = CalculateTotalKgCO2e(repliedLca);
var distributeWeight = CalculateDistributeWeight(repliedLca);
var distributeArea = CalculateDistributeArea(repliedLca);
var distributeHour = CalculateDistributeHour(repliedLca);
if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWeight &&
distributeWeight != 0)
{
primaryDataTypeOfPartsKgCO2e += totalKgCO2e / distributeWeight;
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byArea &&
distributeArea != 0)
{
primaryDataTypeOfPartsKgCO2e += totalKgCO2e / distributeArea;
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWorkHour &&
distributeHour != 0)
{
primaryDataTypeOfPartsKgCO2e += totalKgCO2e / distributeHour;
}
}
return primaryDataTypeOfPartsKgCO2e;
}
public static decimal GetMaterialScopeOneDistributeKgCO2e(ICollection<Material> materials)
{
decimal materialScopeOneDistributeKgCO2e = 0;
foreach (var material in materials)
{
var requestSent = material.RequestSent;
var repliedLca = requestSent != null ? requestSent.RepliedByWhichLCA : null;
if (requestSent == null || repliedLca == null || repliedLca.SnapShotSurveyResult == null) continue;
var scopeOneKgCO2e = repliedLca.SnapShotSurveyResult.Scope1FabResult;
if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWeight &&
CalculateDistributeWeight(repliedLca) != 0)
{
materialScopeOneDistributeKgCO2e += scopeOneKgCO2e / CalculateDistributeWeight(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byArea &&
CalculateDistributeArea(repliedLca) != 0)
{
materialScopeOneDistributeKgCO2e += scopeOneKgCO2e / CalculateDistributeArea(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWorkHour &&
CalculateDistributeHour(repliedLca) != 0)
{
materialScopeOneDistributeKgCO2e += scopeOneKgCO2e / CalculateDistributeHour(repliedLca);
}
}
return materialScopeOneDistributeKgCO2e;
}
public static decimal GetMaterialScopeTwoDistributeKgCO2e(ICollection<Material> materials)
{
decimal materialScopeTwoDistributeKgCO2e = 0;
foreach (var material in materials)
{
var requestSent = material.RequestSent;
var repliedLca = requestSent != null ? requestSent.RepliedByWhichLCA : null;
if (requestSent == null || repliedLca == null || repliedLca.SnapShotSurveyResult == null) continue;
var scopeTwoKgCO2e = repliedLca.SnapShotSurveyResult.Scope2FabResult;
if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWeight &&
CalculateDistributeWeight(repliedLca) != 0)
{
materialScopeTwoDistributeKgCO2e += scopeTwoKgCO2e / CalculateDistributeWeight(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byArea &&
CalculateDistributeArea(repliedLca) != 0)
{
materialScopeTwoDistributeKgCO2e += scopeTwoKgCO2e / CalculateDistributeArea(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWorkHour &&
CalculateDistributeHour(repliedLca) != 0)
{
materialScopeTwoDistributeKgCO2e += scopeTwoKgCO2e / CalculateDistributeHour(repliedLca);
}
}
return materialScopeTwoDistributeKgCO2e;
}
public static decimal GetMaterialScopeThreeDistributeKgCO2e(ICollection<Material> materials)
{
decimal materialScopeThreeDistributeKgCO2e = 0;
foreach (var material in materials)
{
var requestSent = material.RequestSent;
var repliedLca = requestSent != null ? requestSent.RepliedByWhichLCA : null;
if (requestSent == null || repliedLca == null || repliedLca.SnapShotSurveyResult == null) continue;
var scopeThreeKgCO2e = repliedLca.SnapShotSurveyResult.Scope3FabResult;
if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWeight &&
CalculateDistributeWeight(repliedLca) != 0)
{
materialScopeThreeDistributeKgCO2e += scopeThreeKgCO2e / CalculateDistributeWeight(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byArea &&
CalculateDistributeArea(repliedLca) != 0)
{
materialScopeThreeDistributeKgCO2e += scopeThreeKgCO2e / CalculateDistributeArea(repliedLca);
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWorkHour &&
CalculateDistributeHour(repliedLca) != 0)
{
materialScopeThreeDistributeKgCO2e += scopeThreeKgCO2e / CalculateDistributeHour(repliedLca);
}
}
return materialScopeThreeDistributeKgCO2e;
}
public static decimal GetWasteTransportDistributeByHour(ProductLCA productLca)
{
if (productLca == null ||
productLca.SnapShotSurveyResult == null ||
productLca.SnapShotSurveyResult.constantFactorInWorkHour == null) return 0;
return productLca.SnapShotSurveyResult.SecondaryWasteTransportResult * productLca.SnapShotSurveyResult.constantFactorInWorkHour.Value;
}
private static decimal CalculateMaterialProductionAndTransportPhase(ICollection<Material> materials)
{
decimal materialProductionAndTransportPhase = 0;
foreach (var material in materials)
{
var requestSent = material.RequestSent;
ProductLCA repliedLca = requestSent != null ? requestSent.RepliedByWhichLCA : new ProductLCA();
decimal totalKgCO2e = CalculateTotalKgCO2e(repliedLca);
if (requestSent == null || repliedLca == null)
{
materialProductionAndTransportPhase += material.KgCO2e;
continue;
}
if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWeight &&
CalculateDistributeWeight(repliedLca) != 0)
{
decimal totalKgCO2eDistributeByWeight = totalKgCO2e / CalculateDistributeWeight(repliedLca);
materialProductionAndTransportPhase += (totalKgCO2eDistributeByWeight +
repliedLca.SnapShotSurveyResult.SecondaryDirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryIndirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryWrapMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryTransportResult) * material.Quantity;
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byArea &&
CalculateDistributeArea(repliedLca) != 0)
{
decimal totalKgCO2eDistributeByArea = totalKgCO2e / CalculateDistributeArea(repliedLca);
materialProductionAndTransportPhase += (totalKgCO2eDistributeByArea +
repliedLca.SnapShotSurveyResult.SecondaryDirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryIndirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryWrapMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryTransportResult) * material.Quantity;
}
else if (requestSent.DivideBy == ProductLCAReplyRequest.DivideByOptions.byWorkHour &&
CalculateDistributeHour(repliedLca) != 0)
{
decimal totalKgCO2eDistributeByHour = totalKgCO2e / CalculateDistributeHour(repliedLca);
materialProductionAndTransportPhase += (totalKgCO2eDistributeByHour +
repliedLca.SnapShotSurveyResult.SecondaryDirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryIndirectMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryWrapMaterialResult +
repliedLca.SnapShotSurveyResult.SecondaryTransportResult) * material.Quantity;
}
}
return materialProductionAndTransportPhase;
}
private static decimal CalculateProductionPhase(ProductLCA productLca)
{
decimal distributeTotalKgCO2e = 0;
if (CalculateDistributeWeight(productLca) != 0)
{
distributeTotalKgCO2e = CalculateTotalKgCO2e(productLca) / CalculateDistributeWeight(productLca);
}
else if (CalculateDistributeArea(productLca) != 0)
{
distributeTotalKgCO2e = CalculateTotalKgCO2e(productLca) / CalculateDistributeArea(productLca);
}
else if (CalculateDistributeHour(productLca) != 0)
{
distributeTotalKgCO2e = CalculateDistributeArea(productLca) / CalculateDistributeHour(productLca);
}
return distributeTotalKgCO2e + productLca.Materials.Sum(x => x.KgCO2e);
}
private static decimal CalculateDeliveryPhase(ProductLCA productLca)
{
return productLca.SnapShotSurveyResult.SecondaryTransportResult;
}
private static decimal CalculateAbandonedPhase(InventoryStageData inventoryStageData)
{
return inventoryStageData != null ? inventoryStageData.ProductAbandonedStageCarbonFootprint : 0;
}
private static decimal CalculateTotalKgCO2e(ProductLCA productLca)
{
if (productLca == null || productLca.SnapShotSurveyResult == null) return 0;
return productLca.SnapShotSurveyResult.Scope1FabResult
+ productLca.SnapShotSurveyResult.Scope2FabResult
+ productLca.SnapShotSurveyResult.Scope3FabResult
+ productLca.SnapShotSurveyResult.SecondaryWasteResult
+ productLca.SnapShotSurveyResult.SecondaryWasteTransportResult;
}
private static decimal CalculateDistributeWeight(ProductLCA productLca)
{
if (productLca.SnapShotSurveyResult == null ||
productLca.SnapShotSurveyResult.constantFactorInWeight == null ||
productLca.SnapShotSurveyResult.constantFactorInWeight.Value == 0m) return 0;
return (1m / productLca.SnapShotSurveyResult.constantFactorInWeight.Value);
}
private static decimal CalculateDistributeArea(ProductLCA productLca)
{
if (productLca.SnapShotSurveyResult == null ||
productLca.SnapShotSurveyResult.constantFactorInArea == null ||
productLca.SnapShotSurveyResult.constantFactorInArea.Value == 0m) return 0;
return (1m / productLca.SnapShotSurveyResult.constantFactorInArea.Value);
}
private static decimal CalculateDistributeHour(ProductLCA productLca)
{
if (productLca.SnapShotSurveyResult == null ||
productLca.SnapShotSurveyResult.constantFactorInWorkHour == null ||
productLca.SnapShotSurveyResult.constantFactorInWorkHour.Value == 0m) return 0;
return (1m / productLca.SnapShotSurveyResult.constantFactorInWorkHour.Value);
}
}
}