Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using Microsoft.Sustainability.Journal;
codeunit 6218 "Sustainability Calc. Mgt."
{
var
EmissionScopeCache: Dictionary of [Code[20], Enum "Emission Scope"];
CalculationFoundationCache: Dictionary of [Code[20], Enum "Calculation Foundation"];
FromToFilterLbl: Label '%1..%2', Locked = true;

internal procedure CalculationEmissions(var SustainabilityJnlLine: Record "Sustainability Jnl. Line")
Expand Down Expand Up @@ -109,6 +111,102 @@ codeunit 6218 "Sustainability Calc. Mgt."
PurchaseLine.Validate("Emission N2O", 0);
end;

internal procedure GetFormulaInputEditability(SustainabilityJnlLine: Record "Sustainability Jnl. Line"; var FuelElectricityEditable: Boolean; var DistanceEditable: Boolean; var CustomAmountEditable: Boolean; var InstallationMultiplierEditable: Boolean; var TimeFactorEditable: Boolean)
begin
Clear(FuelElectricityEditable);
Clear(DistanceEditable);
Clear(CustomAmountEditable);
Clear(InstallationMultiplierEditable);
Clear(TimeFactorEditable);

if SustainabilityJnlLine."Manual Input" then
exit;

GetFormulaInputEditability(SustainabilityJnlLine."Account Category", false, FuelElectricityEditable, DistanceEditable, CustomAmountEditable, InstallationMultiplierEditable, TimeFactorEditable);
end;

internal procedure GetFormulaInputEditability(PurchaseLine: Record "Purchase Line"; var FuelElectricityEditable: Boolean; var DistanceEditable: Boolean; var CustomAmountEditable: Boolean; var InstallationMultiplierEditable: Boolean; var TimeFactorEditable: Boolean)
begin
Clear(FuelElectricityEditable);
Clear(DistanceEditable);
Clear(CustomAmountEditable);
Clear(InstallationMultiplierEditable);
Clear(TimeFactorEditable);

GetFormulaInputEditability(PurchaseLine."Sust. Account Category", true, FuelElectricityEditable, DistanceEditable, CustomAmountEditable, InstallationMultiplierEditable, TimeFactorEditable);
end;

local procedure GetFormulaInputEditability(AccountCategoryCode: Code[20]; PurchaseSurface: Boolean; var FuelElectricityEditable: Boolean; var DistanceEditable: Boolean; var CustomAmountEditable: Boolean; var InstallationMultiplierEditable: Boolean; var TimeFactorEditable: Boolean)
var
EmissionScope: Enum "Emission Scope";
CalculationFoundation: Enum "Calculation Foundation";
begin
Clear(FuelElectricityEditable);
Clear(DistanceEditable);
Clear(CustomAmountEditable);
Clear(InstallationMultiplierEditable);
Clear(TimeFactorEditable);

if not GetCalculationParameters(AccountCategoryCode, EmissionScope, CalculationFoundation) then
exit;

case EmissionScope of
Enum::"Emission Scope"::"Scope 1":
case CalculationFoundation of
Enum::"Calculation Foundation"::"Fuel/Electricity":
FuelElectricityEditable := true;
Enum::"Calculation Foundation"::Distance:
DistanceEditable := true;
Enum::"Calculation Foundation"::Installations:
begin
CustomAmountEditable := true;
InstallationMultiplierEditable := true;
TimeFactorEditable := true;
end;
end;
Enum::"Emission Scope"::"Scope 2":
case CalculationFoundation of
Enum::"Calculation Foundation"::"Fuel/Electricity":
FuelElectricityEditable := true;
Enum::"Calculation Foundation"::Custom:
CustomAmountEditable := true;
end;
Enum::"Emission Scope"::"Scope 3":
case CalculationFoundation of
Enum::"Calculation Foundation"::"Fuel/Electricity":
FuelElectricityEditable := true;
Enum::"Calculation Foundation"::Distance:
begin
DistanceEditable := true;
InstallationMultiplierEditable := true;
end;
Enum::"Calculation Foundation"::Custom:
CustomAmountEditable := true;
end;
Enum::"Emission Scope"::"Water/Waste":
if (not PurchaseSurface) and (CalculationFoundation = Enum::"Calculation Foundation"::Custom) then
CustomAmountEditable := true;
end;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$\textbf{🟡\ Medium\ Severity\ —\ Performance}$

GetCalculationParameters in 'Sustainability Calc. Mgt.' caches the 'Emission Scope' and 'Calculation Foundation' of a Sustain. Account Category in page-lifetime Dictionary fields (EmissionScopeCache / CalculationFoundationCache) keyed only by account category code, with no invalidation. Because the 'Sustainability Calc. Mgt.' codeunit instance is held as a page-level variable on both the Sustainability Journal page and the Purchase Order Subform, the cache lives for the whole page session. If a user edits an account category's Emission Scope or Calculation Foundation (e.g. via the Sustain. Account Categories page) while a Sustainability Journal or Purchase Order page is already open, the open page keeps showing stale field editability for that category until it is closed and reopened, since the cached values are never refreshed or invalidated. Consider keying/expiring the cache per page-open, or accepting this as a known limitation given account categories are typically static setup data (should be confirmed with the author).

Agent judgement — not directly backed by a BCQuality knowledge article.

👍 useful · ❤️ especially valuable · 👎 wrong - reply with why · AL review agent v1.33.4

end;

local procedure GetCalculationParameters(AccountCategoryCode: Code[20]; var EmissionScope: Enum "Emission Scope"; var CalculationFoundation: Enum "Calculation Foundation"): Boolean
var
SustainAccountCategory: Record "Sustain. Account Category";
begin
if EmissionScopeCache.Get(AccountCategoryCode, EmissionScope) and CalculationFoundationCache.Get(AccountCategoryCode, CalculationFoundation) then
exit(true);

SustainAccountCategory.SetLoadFields("Emission Scope", "Calculation Foundation");
if not SustainAccountCategory.Get(AccountCategoryCode) then
exit(false);

EmissionScope := SustainAccountCategory."Emission Scope";
CalculationFoundation := SustainAccountCategory."Calculation Foundation";
EmissionScopeCache.Set(AccountCategoryCode, EmissionScope);
CalculationFoundationCache.Set(AccountCategoryCode, CalculationFoundation);
exit(true);
end;

/// <summary>
/// Filter general ledger entries by criteria defined in sustainability category and by date and calculate the total
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ page 6219 "Sustainability Journal"
DimMgt: Codeunit DimensionManagement;
begin
DimMgt.GetShortcutDimensions(Rec."Dimension Set ID", ShortcutDimCode);
SetFormulaInputEditability();
CurrPage.Update();
end;
}
Expand Down Expand Up @@ -150,6 +151,11 @@ page 6219 "Sustainability Journal"
field("Manual Input"; Rec."Manual Input")
{
ToolTip = 'Specifies whether the amounts will be input manually.';

trigger OnValidate()
begin
SetFormulaInputEditability();
end;
}
field("Renewable Energy"; Rec."Renewable Energy")
{
Expand All @@ -166,7 +172,7 @@ page 6219 "Sustainability Journal"
}
field("Fuel/Electricity"; Rec."Fuel/Electricity")
{
Editable = not Rec."Manual Input";
Editable = FuelElectricityEditable;
ToolTip = 'Specifies the fuel or electricity of the journal line.';
trigger OnValidate()
begin
Expand All @@ -175,7 +181,7 @@ page 6219 "Sustainability Journal"
}
field(Distance; Rec.Distance)
{
Editable = not Rec."Manual Input";
Editable = DistanceEditable;
ToolTip = 'Specifies the distance of the journal line.';
trigger OnValidate()
begin
Expand All @@ -184,7 +190,7 @@ page 6219 "Sustainability Journal"
}
field("Custom Amount"; Rec."Custom Amount")
{
Editable = not Rec."Manual Input";
Editable = CustomAmountEditable;
ToolTip = 'Specifies the custom amount of the journal line.';
trigger OnValidate()
begin
Expand All @@ -193,12 +199,12 @@ page 6219 "Sustainability Journal"
}
field("Installation Multiplier"; Rec."Installation Multiplier")
{
Editable = not Rec."Manual Input";
Editable = InstallationMultiplierEditable;
ToolTip = 'Specifies the installation multiplier of the journal line.';
}
field("Time Factor"; Rec."Time Factor")
{
Editable = not Rec."Manual Input";
Editable = TimeFactorEditable;
ToolTip = 'Specifies the time factor of the journal line.';
}
field("Emission CO2"; Rec."Emission CO2")
Expand Down Expand Up @@ -698,10 +704,13 @@ page 6219 "Sustainability Journal"

var
SustApprovalMgmt: Codeunit "Sust. Approvals Mgmt.";
SustainabilityCalcMgt: Codeunit "Sustainability Calc. Mgt.";
CurrentJournalBatchName: Code[10];
ShortcutDimCode: array[8] of Code[20];
DimVisible1, DimVisible2, DimVisible3, DimVisible4, DimVisible5, DimVisible6, DimVisible7, DimVisible8 : Boolean;
IsRecurringView, EnableWater, EnableWaste : Boolean;
FuelElectricityEditable, DistanceEditable, CustomAmountEditable : Boolean;
InstallationMultiplierEditable, TimeFactorEditable : Boolean;
OpenApprovalEntriesOnBatchOrAnyJnlLineExist: Boolean;
EnabledSustJnlBatchWorkflowsExist: Boolean;
ShowWorkflowStatusOnBatch: Boolean;
Expand Down Expand Up @@ -729,12 +738,14 @@ page 6219 "Sustainability Journal"
begin
DimMgt.GetShortcutDimensions(Rec."Dimension Set ID", ShortcutDimCode);
InitializeAndEnableIntensityControl();
SetFormulaInputEditability();
SetControlAppearanceFromBatch();
end;

trigger OnAfterGetCurrRecord()
begin
InitializeAndEnableIntensityControl();
SetFormulaInputEditability();
SetControlAppearanceFromBatch();

SustApprovalMgmt.GetSustJnlBatchApprovalStatus(Rec, SustJnlBatchApprovalStatus, EnabledSustJnlBatchWorkflowsExist);
Expand Down Expand Up @@ -813,6 +824,11 @@ page 6219 "Sustainability Journal"
EnableWaste := false;
end;

local procedure SetFormulaInputEditability()
begin
SustainabilityCalcMgt.GetFormulaInputEditability(Rec, FuelElectricityEditable, DistanceEditable, CustomAmountEditable, InstallationMultiplierEditable, TimeFactorEditable);
end;

local procedure SetControlAppearanceFromBatch()
var
SustJournalBatch: Record "Sustainability Jnl. Batch";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Microsoft.Sustainability.Purchase;

using Microsoft.Purchases.Document;
using Microsoft.Sustainability.Calculation;
using Microsoft.Sustainability.Setup;

pageextension 6211 "Sust. Purch. Order Subform" extends "Purchase Order Subform"
Expand All @@ -14,6 +15,11 @@ pageextension 6211 "Sust. Purch. Order Subform" extends "Purchase Order Subform"
Visible = SustainabilityVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the value of the Sustainability Account No. field.';

trigger OnValidate()
begin
SetFormulaInputEditability();
end;
}
field("Energy Source Code"; Rec."Energy Source Code")
{
Expand All @@ -32,30 +38,35 @@ pageextension 6211 "Sust. Purch. Order Subform" extends "Purchase Order Subform"
}
field("Fuel/Electricity"; Rec."Fuel/Electricity")
{
Editable = FuelElectricityEditable;
Visible = SustainabilityFormulasFieldVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the fuel or electricity of the purchase line.';
}
field(Distance; Rec.Distance)
{
Editable = DistanceEditable;
Visible = SustainabilityFormulasFieldVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the distance of the purchase line.';
}
field("Custom Amount"; Rec."Custom Amount")
{
Editable = CustomAmountEditable;
Visible = SustainabilityFormulasFieldVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the custom amount of the purchase line.';
}
field("Installation Multiplier"; Rec."Installation Multiplier")
{
Editable = InstallationMultiplierEditable;
Visible = SustainabilityFormulasFieldVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the installation multiplier of the purchase line.';
}
field("Time Factor"; Rec."Time Factor")
{
Editable = TimeFactorEditable;
Visible = SustainabilityFormulasFieldVisible;
ApplicationArea = Basic, Suite;
ToolTip = 'Specifies the time factor of the purchase line.';
Expand Down Expand Up @@ -122,6 +133,16 @@ pageextension 6211 "Sust. Purch. Order Subform" extends "Purchase Order Subform"
VisibleSustainabilityControls();
end;

trigger OnAfterGetRecord()
begin
SetFormulaInputEditability();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$\textbf{🟡\ Medium\ Severity\ —\ Performance}$

SetFormulaInputEditability is invoked unconditionally from OnAfterGetRecord/OnAfterGetCurrRecord (and from the Sust. Account No. OnValidate trigger) on the Purchase Order Subform even when the sustainability formula fields are not visible (SustainabilityFormulasFieldVisible = false, i.e. 'Use Formulas In Purch. Docs' is off in Sustainability Setup). This causes an unnecessary lookup into 'Sustainability Calc. Mgt.' for every row rendered on a purchase order subform for customers who do not use this feature at all. Guard the call with the existing SustainabilityFormulasFieldVisible flag so the cost is only paid when the fields are actually shown.

Suggested fix (apply manually — could not be anchored as a one-click suggestion):

local procedure SetFormulaInputEditability()
    begin
        if not SustainabilityFormulasFieldVisible then
            exit;

        SustainabilityCalcMgt.GetFormulaInputEditability(Rec, FuelElectricityEditable, DistanceEditable, CustomAmountEditable, InstallationMultiplierEditable, TimeFactorEditable);
    end;

Knowledge:

👍 useful · ❤️ especially valuable · 👎 wrong - reply with why · AL review agent v1.33.4

end;

trigger OnAfterGetCurrRecord()
begin
SetFormulaInputEditability();
end;

local procedure VisibleSustainabilityControls()
var
SustainabilitySetup: Record "Sustainability Setup";
Expand All @@ -132,7 +153,15 @@ pageextension 6211 "Sust. Purch. Order Subform" extends "Purchase Order Subform"
SustainabilityFormulasFieldVisible := SustainabilitySetup."Use Formulas In Purch. Docs";
end;

local procedure SetFormulaInputEditability()
begin
SustainabilityCalcMgt.GetFormulaInputEditability(Rec, FuelElectricityEditable, DistanceEditable, CustomAmountEditable, InstallationMultiplierEditable, TimeFactorEditable);
end;

var
SustainabilityCalcMgt: Codeunit "Sustainability Calc. Mgt.";
SustainabilityVisible: Boolean;
SustainabilityFormulasFieldVisible: Boolean;
FuelElectricityEditable, DistanceEditable, CustomAmountEditable : Boolean;
InstallationMultiplierEditable, TimeFactorEditable : Boolean;
}
Loading
Loading