diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/launch.json b/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/launch.json new file mode 100644 index 0000000..cd4d5eb --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/launch.json @@ -0,0 +1,4 @@ +{ + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/settings.json b/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/settings.json new file mode 100644 index 0000000..1c72724 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "al.codeAnalyzers": [ + "${PerTenantExtensionCop}", + "${CodeCop}", + "${UICop}" + ] +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/Logo/PV_logo_240x240px.png b/Apps/SpencerMetrics/PrintVis SpencerMetrics/Logo/PV_logo_240x240px.png new file mode 100644 index 0000000..fd61b39 Binary files /dev/null and b/Apps/SpencerMetrics/PrintVis SpencerMetrics/Logo/PV_logo_240x240px.png differ diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/Permissions/PVSSMFULL.PermissionSet.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/Permissions/PVSSMFULL.PermissionSet.al new file mode 100644 index 0000000..1cafbe0 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/Permissions/PVSSMFULL.PermissionSet.al @@ -0,0 +1,36 @@ +namespace PrintVis.SpencerMetrics.Permissions; + +using PrintVis.SpencerMetrics.CounterManagement; +using PrintVis.SpencerMetrics.Configuration; +using PrintVis.SpencerMetrics.Services; +using PrintVis.SpencerMetrics; +using PrintVis.SpencerMetrics.API; + +permissionset 80290 "PVS SM FULL" +{ + Assignable = true; + Caption = 'SpencerMetrics Full'; + Permissions = table "PVS SM Cost Center Setup" = X, + tabledata "PVS SM Cost Center Setup" = RIMD, + table "PVS SM Setup" = X, + tabledata "PVS SM Setup" = RIMD, + table "PVS SM Counter Entry" = X, + tabledata "PVS SM Counter Entry" = RIMD, + table "PVS SM Count Summary Buffer" = X, + tabledata "PVS SM Count Summary Buffer" = RIMD, + table "PVS SM Session Parameter" = X, + tabledata "PVS SM Session Parameter" = RIMD, + table "PVS SM Session Log" = X, + tabledata "PVS SM Session Log" = RIMD, + codeunit "PVS SM Api Client" = X, + codeunit "PVS SM Counter Worker" = X, + codeunit "PVS SM Event Handler" = X, + codeunit "PVS SM Events" = X, + codeunit "PVS SM Session Manager" = X, + page "PVS SM Assign To Job Costing" = X, + page "PVS SM Cost Center Setup" = X, + page "PVS SM Counter Entries" = X, + page "PVS SM Count FactBox" = X, + page "PVS SM Order Sheet API" = X, + query "PVS SM Counts Sum By UOM" = X; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/app.json b/Apps/SpencerMetrics/PrintVis SpencerMetrics/app.json new file mode 100644 index 0000000..11431f5 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/app.json @@ -0,0 +1,44 @@ +{ + "id": "b43efe82-3cf5-4267-b39c-eae529da92df", + "name": "PrintVis Spencer Metrics Integration", + "publisher": "PrintVis A/S", + "version": "28.0.0.0", + "brief": "PrintVis SpencerMetrics", + "description": "PrintVis SpencerMetrics", + "privacyStatement": "http://www.printvis.com", + "EULA": "http://www.printvis.com", + "help": "https://support365.printvis.com/", + "url": "http://www.printvis.com", + "logo": "Logo/PV_logo_240x240px.png", + "contextSensitiveHelpUrl": "https://learn.printvis.com/", + "dependencies": [ + { + "id": "5452f323-059e-499a-9753-5d2c07eef904", + "name": "PrintVis", + "publisher": "PrintVis A/S", + "version": "28.0.0.0" + } + ], + "screenshots": [], + "platform": "28.0.0.0", + "application": "28.0.0.0", + "idRanges": [ + { + "from": 80290, + "to": 80300 + } + ], + "features": [ + "NoImplicitWith", + "TranslationFile" + ], + "supportedLocales": [ + "en-US" + ], + "resourceExposurePolicy": { + "allowDebugging": true, + "allowDownloadingSource": false, + "includeSourceInSymbolFile": false + }, + "runtime": "16.0" +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/API/PVSSMOrderSheetAPI.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/API/PVSSMOrderSheetAPI.Page.al new file mode 100644 index 0000000..480a5a1 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/API/PVSSMOrderSheetAPI.Page.al @@ -0,0 +1,34 @@ +namespace PrintVis.SpencerMetrics.API; + +page 80290 "PVS SM Order Sheet API" +{ + APIGroup = 'spencerMetrics'; + APIPublisher = 'printvis'; + APIVersion = 'v1.0'; + Editable = false; + EntityName = 'orderSheet'; + EntitySetName = 'orderSheets'; + ODataKeyFields = "PVS SM Key"; + PageType = API; + SourceTable = "PVS Job Sheet"; + + layout + { + area(content) + { + field(smKey; Rec."PVS SM Key") { Caption = 'smKey', Locked = true; } + field(active; Rec.Active) { Caption = 'active', Locked = true; } + field(jobStatus; Rec."Job Status") { Caption = 'jobStatus', Locked = true; } + field(orderNo; Rec."Order No.") { Caption = 'orderNo', Locked = true; } + field(sheetNo; Rec."Sheet No.") { Caption = 'sheetNo', Locked = true; } + field(paperItemNo; Rec."Paper Item No.") { Caption = 'paperItemNo', Locked = true; } + field(paperWeight; Rec.Weight) { Caption = 'paperWeight', Locked = true; } + field(paperWeightUnit; Rec."Weight Unit") { Caption = 'paperWeightUnit', Locked = true; } + field(paperQuality; Rec."Paper Quality") { Caption = 'paperQuality', Locked = true; } + field(sizeWidth; Rec."Full Sheet Format 1") { Caption = 'sizeWidth', Locked = true; } + field(sizeLength; Rec."Full Sheet Format 2") { Caption = 'sizeLength', Locked = true; } + field(grossQty; Rec."Gross Printed Sheets") { Caption = 'grossPrintedSheets', Locked = true; } + field(netQty; Rec."End Qty.") { Caption = 'netPrintedSheets', Locked = true; } + } + } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Page.al new file mode 100644 index 0000000..39137b8 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Page.al @@ -0,0 +1,88 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +using PrintVis.SpencerMetrics.CounterManagement; + +page 80291 "PVS SM Cost Center Setup" +{ + ApplicationArea = All; + Caption = 'SpencerMetrics Cost Center Setup'; + PageType = List; + SourceTable = "PVS SM Cost Center Setup"; + UsageCategory = Administration; + + layout + { + area(Content) + { + repeater(repeater) + { + field("Cost Center"; Rec."Cost Center") { } + field("API Key"; Rec."API Key") { } + field(Enabled; Rec."Enabled") { } + field("Interval (ms)"; Rec."Interval (ms)") { } + field("Session ID"; Rec."Session Id") + { + Editable = false; + } + field("Job Queue Entry ID"; Rec."Job Queue Entry ID") + { + Editable = false; + Visible = false; + } + field("Last Heartbeat"; Rec."Last Heartbeat") + { + Editable = false; + } + field("Session Error"; Rec."Last Error") + { + Editable = false; + } + } + } + } + + actions + { + area(Processing) + { + action(Counts) + { + Caption = 'Counts'; + Image = NumberGroup; + RunObject = page "PVS SM Counter Entries"; + ToolTip = 'Open the counts page for the selected cost center'; + } + + action("Delete All Records") + { + Caption = 'Delete All Records'; + Image = Delete; + ToolTip = 'Delete all count records for the selected cost center'; + + trigger OnAction() + var + CounterEntry: Record "PVS SM Counter Entry"; + Confirmed: Boolean; + ConfirmQst: Label 'Are you sure you want to delete all count records for %1?', Comment = '%1 = Cost Center Code'; + begin + Confirmed := Dialog.Confirm(ConfirmQst, false, Rec."Cost Center"); + if Confirmed then begin + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", Rec."Cost Center"); + if (CounterEntry.Count() > 0) then + CounterEntry.DeleteAll(); + end; + end; + } + + action("Session Log") + { + Caption = 'Session Log'; + Image = Log; + RunObject = page "PVS SM Session Log"; + RunPageLink = "Cost Center" = field("Cost Center"); + ToolTip = 'View session start/stop history for this cost center'; + } + } + } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Table.al new file mode 100644 index 0000000..63cf800 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/CostCenter/PVSSMCostCenterSetup.Table.al @@ -0,0 +1,70 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +table 80290 "PVS SM Cost Center Setup" +{ + Caption = 'SpencerMetrics Cost Center Setup'; + DataClassification = CustomerContent; + + fields + { + field(1; "Cost Center"; Code[20]) + { + Caption = 'Cost Center'; + NotBlank = true; + TableRelation = "PVS Cost Center".Code; + ToolTip = 'The Cost Center code for SpencerMetrics connection'; + } + field(2; "API Key"; Text[50]) + { + Caption = 'API Key'; + ExtendedDatatype = Masked; + ToolTip = 'The SpencerMetrics API Key for this cost center'; + } + field(3; Enabled; Boolean) + { + Caption = 'Enabled'; + Editable = false; + InitValue = false; + ToolTip = 'Set SpencerMetrics Timer On'; + } + field(4; "Interval (ms)"; Integer) + { + Caption = 'Interval (ms)'; + InitValue = 15000; + MinValue = 5000; + ToolTip = 'The interval for the timer to check SpencerMetrics for new counts. It is not recommended to set this less than 15000 (15 seconds)'; + } + field(5; "Session Id"; Integer) + { + Caption = 'Session Id'; + ToolTip = 'The session ID that is running the SpencerMetrics timer'; + } + field(6; "Login Key"; Text[100]) + { + Caption = 'Login Key'; + ExtendedDatatype = Masked; + } + field(7; "Last Heartbeat"; DateTime) + { + Caption = 'Last Heartbeat'; + ToolTip = 'The last heartbeat for the session running the SpencerMetrics timer'; + } + field(8; "Last Error"; Text[2048]) + { + Caption = 'Last Error'; + ToolTip = 'The last error for the session running the SpencerMetrics timer'; + } + field(9; "Job Queue Entry ID"; Guid) + { + Caption = 'Job Queue Entry ID'; + ToolTip = 'The Job Queue Entry ID for the session running the SpencerMetrics timer'; + } + } + + keys { key(Key1; "Cost Center") { Clustered = true; } } + + internal procedure SetLastHeartbeat() + begin + "Last Heartbeat" := CurrentDateTime(); + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionEventType.Enum.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionEventType.Enum.al new file mode 100644 index 0000000..acec9f4 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionEventType.Enum.al @@ -0,0 +1,16 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +enum 80290 "PVS SM Session Event Type" +{ + Caption = 'Session Event Type'; + Access = Internal; + + value(0; Start) + { + Caption = 'Start'; + } + value(1; Stop) + { + Caption = 'Stop'; + } +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Page.al new file mode 100644 index 0000000..43db4d9 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Page.al @@ -0,0 +1,52 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +page 80292 "PVS SM Session Log" +{ + ApplicationArea = All; + Caption = 'SpencerMetrics Session Log'; + Editable = false; + InsertAllowed = false; + ModifyAllowed = false; + DeleteAllowed = false; + PageType = List; + SourceTable = "PVS SM Session Log"; + SourceTableView = sorting("Entry No.") order(descending); + UsageCategory = None; + Extensible = false; + + layout + { + area(Content) + { + repeater(repeater) + { + field("Entry No."; Rec."Entry No.") { } + field("Cost Center"; Rec."Cost Center") { } + field("Job Queue Entry ID"; Rec."Job Queue Entry ID") { } + field("Event Type"; Rec."Event Type") { } + field("Event DateTime"; Rec."Event DateTime") { } + field("Expected End DateTime"; Rec."Expected End DateTime") { } + field("User ID"; Rec."User ID") { } + field(Success; Rec.Success) + { + StyleExpr = SuccessStyle; + } + field("Error Message"; Rec."Error Message") + { + Visible = false; + } + } + } + } + + var + SuccessStyle: Text; + + trigger OnAfterGetRecord() + begin + if Rec.Success then + SuccessStyle := 'Favorable' + else + SuccessStyle := 'Unfavorable'; + end; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Table.al new file mode 100644 index 0000000..7261a80 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionLog/PVSSMSessionLog.Table.al @@ -0,0 +1,73 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +table 80291 "PVS SM Session Log" +{ + Caption = 'SpencerMetrics Session Log'; + DataClassification = CustomerContent; + DrillDownPageId = "PVS SM Session Log"; + LookupPageId = "PVS SM Session Log"; + Access = Internal; + + fields + { + field(1; "Entry No."; BigInteger) + { + AutoIncrement = true; + Caption = 'Entry No.'; + ToolTip = 'Entry No. for SpencerMetrics Session Log'; + Editable = false; + } + field(2; "Cost Center"; Code[20]) + { + Caption = 'Cost Center'; + ToolTip = 'Cost Center for SpencerMetrics Session Log'; + NotBlank = true; + TableRelation = "PVS Cost Center".Code; + } + field(4; "Event Type"; Enum "PVS SM Session Event Type") + { + Caption = 'Event Type'; + ToolTip = 'Event Type for SpencerMetrics Session Log'; + } + field(5; "Event DateTime"; DateTime) + { + Caption = 'Event DateTime'; + ToolTip = 'Event DateTime for SpencerMetrics Session Log'; + } + field(6; "User ID"; Code[50]) + { + Caption = 'User ID'; + ToolTip = 'User ID for SpencerMetrics Session Log'; + DataClassification = EndUserIdentifiableInformation; + } + field(7; "Error Message"; Text[2048]) + { + Caption = 'Error Message'; + ToolTip = 'Error Message for SpencerMetrics Session Log'; + } + field(8; Success; Boolean) + { + Caption = 'Success'; + ToolTip = 'Success for SpencerMetrics Session Log'; + InitValue = true; + } + field(9; "Expected End DateTime"; DateTime) + { + Caption = 'Expected End DateTime'; + ToolTip = 'Expected End DateTime for SpencerMetrics Session Log'; + } + field(10; "Job Queue Entry ID"; Guid) + { + Caption = 'Job Queue Entry ID'; + ToolTip = 'Job Queue Entry ID for SpencerMetrics Session Log'; + } + } + + keys + { + key(PK; "Entry No.") + { + Clustered = true; + } + } +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionParameter/PVSSMSessionParameter.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionParameter/PVSSMSessionParameter.Table.al new file mode 100644 index 0000000..9284d7f --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/SessionParameter/PVSSMSessionParameter.Table.al @@ -0,0 +1,33 @@ +namespace PrintVis.SpencerMetrics.Configuration; + +table 80292 "PVS SM Session Parameter" +{ + Caption = 'SpencerMetrics Session Parameter'; + DataClassification = CustomerContent; + + fields + { + field(1; "Session Id"; Integer) + { + Caption = 'Session Id'; + } + field(2; "Setup Code"; Code[20]) + { + Caption = 'Setup Code'; + } + field(3; "Session Type"; Text[50]) + { + Caption = 'Session Type'; + } + field(4; "Case ID"; Integer) + { + Caption = 'Case ID'; + } + field(5; "Plan ID"; Integer) + { + Caption = 'Plan ID'; + } + } + + keys { key(PK; "Session Id") { Clustered = true; } } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Page.al new file mode 100644 index 0000000..c0d0448 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Page.al @@ -0,0 +1,33 @@ +page 80293 "PVS SM Setup" +{ + ApplicationArea = All; + Caption = 'SpencerMetrics Setup'; + InsertAllowed = false; + PageType = List; + SourceTable = "PVS SM Setup"; + UsageCategory = Administration; + layout + { + area(Content) + { + group(API) + { + Caption = 'API'; + field("Base URL"; Rec."Base URL") { } + } + group(Session) + { + Caption = 'Session'; + field("Max Session Duration (min)"; Rec."Max Session Duration (min)") + { + ToolTip = 'Maximum session duration in minutes. 0 means no limit. Default is 8 hours (480 min).'; + } + } + } + } + + trigger OnOpenPage() + begin + Rec.Initialize(); + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Table.al new file mode 100644 index 0000000..23d5aad --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Configuration/Setup/PVSSMSetup.Table.al @@ -0,0 +1,43 @@ +table 80293 "PVS SM Setup" +{ + Caption = 'SpencerMetrics Setup'; + DataClassification = CustomerContent; + + fields + { + field(1; "Primary Key"; Code[20]) + { + Caption = 'Primary Key'; + } + field(9; "Base URL"; Text[200]) + { + Caption = 'Base URL'; + InitValue = 'https://clientlogin.spencermetrics.com/api/'; + NotBlank = true; + ToolTip = 'The base URL for SpencerMetrics API endpoints'; + } + field(10; "Max Session Duration (min)"; Integer) + { + Caption = 'Max Session Duration (min)'; + InitValue = 480; + MinValue = 0; + ToolTip = 'Maximum session duration in minutes. 0 means no limit. Default is 8 hours (480 min).'; + } + } + + keys + { + key(Key1; "Primary Key") + { + Clustered = true; + } + } + + internal procedure Initialize() + begin + if not Rec.Get() then begin + Clear(Rec); + Rec.Insert(true); + end; + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEventHandler.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEventHandler.Codeunit.al new file mode 100644 index 0000000..4795902 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEventHandler.Codeunit.al @@ -0,0 +1,97 @@ +namespace PrintVis.SpencerMetrics; + +using PrintVis.SpencerMetrics.CounterManagement; +using Microsoft.Projects.TimeSheet; +using PrintVis.SpencerMetrics.Configuration; + +codeunit 80290 "PVS SM Event Handler" +{ + Access = Internal; + SingleInstance = true; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"PVS Shop Floor Management", 'OnAfterShopFloorUserLogin', '', false, false)] + local procedure ShopFloorManagement_OnAfterShopFloorUserLogin( + var UserRegisterRec: Record "User Time Register"; + var LaborEntryRec: Record "PVS Job Costing Labor Entry") + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + SessionManager: Codeunit "PVS SM Session Manager"; + begin + if SMCostCenterSetup.Get(UserRegisterRec."PVS Login Cost Center") then + SessionManager.Start(SMCostCenterSetup."Cost Center"); + end; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"PVS Shop Floor Management", 'OnAfterShopFloorUserLogout', '', false, false)] + local procedure ShopFloorManagement_OnAfterShopFloorUserLogout( + var in_PrintVisUserRec: Record "PVS User Setup"; + var UserRegisterRec: Record "User Time Register"; + var LaborEntryRec_Start: Record "PVS Job Costing Labor Entry") + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + SessionManager: Codeunit "PVS SM Session Manager"; + begin + if SMCostCenterSetup.Get(UserRegisterRec."PVS Login Cost Center") then + SessionManager.Stop(SMCostCenterSetup."Cost Center"); + end; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"PVS Shop Floor Management", 'OnBeforeReadMachineCounter', '', false, false)] + local procedure ShopFloorManagement_OnBeforeReadMachineCounter( + var in_Journal_EntryRec: Record "PVS Shop Floor Journal Entry"; + Is_Start: Boolean; + var Is_Handled: Boolean) + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + CounterEntry: Record "PVS SM Counter Entry"; + FirstEntryNo: Integer; + StartUOMDeviceStatusTok: Label 'Start UOM', Locked = true; + StopUOMDeviceStatusTok: Label 'Stop UOM', Locked = true; + begin + if not SMCostCenterSetup.Get(in_Journal_EntryRec."Cost Center Code") then + exit; + + if Is_Start then begin + Clear(CounterEntry); + CounterEntry."Cost Center Code" := in_Journal_EntryRec."Cost Center Code"; + CounterEntry."Entry No." := CounterEntry.GetNextEntryNo(in_Journal_EntryRec."Cost Center Code"); + CounterEntry."Case ID" := in_Journal_EntryRec.ID; + CounterEntry."Plan ID" := in_Journal_EntryRec."Plan ID"; + CounterEntry.UOM := in_Journal_EntryRec."UOM"; + CounterEntry."Current Count" := -1; + CounterEntry."Device Status" := StartUOMDeviceStatusTok; + CounterEntry."Include In Job Costing" := true; + CounterEntry.Insert(); + end else begin + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", in_Journal_EntryRec."Cost Center Code"); + CounterEntry.SetRange("Case ID", in_Journal_EntryRec.ID); + CounterEntry.SetRange("Plan ID", in_Journal_EntryRec."Plan ID"); + CounterEntry.SetRange(UOM, in_Journal_EntryRec."UOM"); + if CounterEntry.FindFirst() then begin + FirstEntryNo := CounterEntry."Entry No."; + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", in_Journal_EntryRec."Cost Center Code"); + CounterEntry.SetRange("Include In Job Costing", false); + CounterEntry.SetFilter("Entry No.", '>%1', FirstEntryNo); + if CounterEntry.FindSet(true) then + repeat + CounterEntry."Case ID" := in_Journal_EntryRec.ID; + CounterEntry."Plan ID" := in_Journal_EntryRec."Plan ID"; + CounterEntry.UOM := in_Journal_EntryRec.UOM; + in_Journal_EntryRec.Quantity := in_Journal_EntryRec.Quantity + CounterEntry."Difference Count"; + CounterEntry."Include In Job Costing" := true; + CounterEntry.Modify(); + until CounterEntry.Next() = 0; + CounterEntry.Reset(); + CounterEntry.Init(); + CounterEntry."Entry No." := CounterEntry.GetNextEntryNo(in_Journal_EntryRec."Cost Center Code"); + CounterEntry."Case ID" := 0; + CounterEntry."Plan ID" := 0; + CounterEntry.UOM := ''; + CounterEntry."Current Count" := -1; + CounterEntry."Device Status" := StopUOMDeviceStatusTok; + CounterEntry."Include In Job Costing" := true; + CounterEntry.Insert(); + end; + end; + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEvents.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEvents.Codeunit.al new file mode 100644 index 0000000..116aea9 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMEvents.Codeunit.al @@ -0,0 +1,17 @@ +namespace PrintVis.SpencerMetrics; + +codeunit 80291 "PVS SM Events" +{ + Access = Public; + + /// + /// Integration event to mock the web request. + /// + /// The service action. + /// The response. + /// The handled flag. + [IntegrationEvent(false, false)] + procedure OnMockWebRequest(in_ServiceAction: Text; var out_Response: Text; var out_Handled: Boolean) + begin + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMInstall.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMInstall.Codeunit.al new file mode 100644 index 0000000..abc8823 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Core/PVSSMInstall.Codeunit.al @@ -0,0 +1,30 @@ +codeunit 80292 "PVS SM Install" +{ + Access = Internal; + Subtype = Install; + + + trigger OnInstallAppPerDatabase() + var + MyAppInfo: ModuleInfo; + begin + NavApp.GetCurrentModuleInfo(MyAppInfo); + this.AllowHttpRequests(MyAppInfo.Id); + end; + + + local procedure AllowHttpRequests(in_AppID: Guid) + var + NAVAppSetting: Record "NAV App Setting"; + begin + if NAVAppSetting.Get(in_AppID) then begin + NAVAppSetting."Allow HttpClient Requests" := true; + NAVAppSetting.Modify(); + end else begin + Clear(NAVAppSetting); + NAVAppSetting."App ID" := in_AppID; + NAVAppSetting."Allow HttpClient Requests" := true; + NAVAppSetting.Insert(); + end; + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountFactBox.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountFactBox.Page.al new file mode 100644 index 0000000..2cbe3e8 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountFactBox.Page.al @@ -0,0 +1,83 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +using PrintVis.SpencerMetrics.Utilities; + +page 80295 "PVS SM Count FactBox" +{ + ApplicationArea = All; + Caption = 'SpencerMetrics Count Summary'; + Editable = false; + PageType = ListPart; + SourceTable = "PVS SM Count Summary Buffer"; + SourceTableTemporary = true; + + layout + { + area(content) + { + usercontrol(Timer; "PVS SM Timer AddIn") + { + trigger Ready() + begin + CurrPage.Timer.Start(5000); + end; + + trigger OnTick() + begin + this.RefreshLoop(); + end; + } + + repeater(repeater) + { + field(UOM; Rec."UOM") { } + field("Total Difference Count"; Rec."Total Difference Count") { } + } + } + } + + local procedure ResolveContext(): Boolean + var + ShopFloorMgmt: Codeunit "PVS Shop Floor Management"; + begin + CurrentCostCenter := ShopFloorMgmt.User_Get_Current_CostCenter(); + if (CurrentCostCenter = '') then + exit(false); + + exit(ShopFloorMgmt.Find_ActivePlanJournalEntry(CurrentPlan, 0, 0, 0, CurrentCostCenter)); + end; + + local procedure RefreshLoop() + var + SumByUOM: Query "PVS SM Counts Sum By UOM"; + begin + If not this.ResolveContext() then + exit; + + Rec.DeleteAll(); + + SumByUOM.SetFilter(CostCenter, CurrentCostCenter); + SumByUOM.SetRange(CaseID, CurrentPlan.ID); + SumByUOM.SetRange(PlanID, CurrentPlan."Plan ID"); + if not SumByUOM.Open() then + Error('Error opening Query'); + + while SumByUOM.Read() do begin + Clear(Rec); + Rec."Case ID" := CurrentPlan.ID; + Rec."Plan ID" := CurrentPlan."Plan ID"; + Rec."UOM" := SumByUOM.UOM; + Rec."Total Difference Count" := SumByUOM.DifferenceCountSum; + Rec."Cost Center Code" := CurrentCostCenter; + Rec.Insert(); + end; + + SumByUOM.Close(); + + CurrPage.Update(false); + end; + + var + CurrentPlan: Record "PVS Shop Floor Journal Entry"; + CurrentCostCenter: Code[20]; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountSummaryBuffer.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountSummaryBuffer.Table.al new file mode 100644 index 0000000..50c6f4b --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountSummaryBuffer.Table.al @@ -0,0 +1,41 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +table 80295 "PVS SM Count Summary Buffer" +{ + Caption = 'SpencerMetrics Count Summary Buffer'; + DataClassification = CustomerContent; + TableType = Temporary; + + fields + { + field(1; UOM; Code[20]) + { + Caption = 'UOM'; + ToolTip = 'The Unit of Measure for the Count entry.'; + } + field(2; "Total Difference Count"; Integer) + { + Caption = 'Counts'; + ToolTip = 'The total difference count for the Count entry.'; + } + field(3; "Cost Center Code"; Code[20]) + { + Caption = 'Cost Center Code'; + ToolTip = 'The Cost Center Code for the Count entry.'; + } + field(4; "Case ID"; Integer) + { + Caption = 'Case ID'; + ToolTip = 'The Case ID for the Count entry.'; + } + field(5; "Plan ID"; Integer) + { + Caption = 'Plan ID'; + ToolTip = 'The Plan ID for the Count entry.'; + } + } + keys + { + key(PK; "UOM", "Case ID", "Plan ID") { Clustered = true; } + } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntries.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntries.Page.al new file mode 100644 index 0000000..bee680f --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntries.Page.al @@ -0,0 +1,96 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +using PrintVis.SpencerMetrics.Configuration; + +page 80294 "PVS SM Counter Entries" +{ + ApplicationArea = All; + Caption = 'SpencerMetrics Counter Entries'; + InsertAllowed = false; + ModifyAllowed = false; + PageType = List; + SourceTable = "PVS SM Counter Entry"; + UsageCategory = Lists; + + layout + { + area(Content) + { + repeater(repeater) + { + field("Cost Center Code"; Rec."Cost Center Code") { } + field("Entry No."; Rec."Entry No.") { } + field("Current Count"; Rec."Current Count") { } + field("Difference Count"; Rec."Difference Count") { } + field("Current Time"; Rec."Current Time") { } + field("Device Status"; Rec."Device Status") { } + field(Speed; Rec.Speed) { } + field("Include In Job Costing"; Rec."Include In Job Costing") { } + field("Case ID"; Rec."Case ID") { } + field("Plan ID"; Rec."Plan ID") { } + field(UOM; Rec.UOM) { } + } + } + } + + actions + { + area(Processing) + { + action("Cost Center Setup") + { + Caption = 'Cost Center Setup'; + Image = Setup; + RunObject = page "PVS SM Cost Center Setup"; + ToolTip = 'Opens the setup page for SpencerMetrics'; + } + action(Assign) + { + Caption = 'Assign to Job Costing'; + Image = Link; + ToolTip = 'Allows the user to assign SpencerMetrics counts to job costing entries that were missed during shop floor recording'; + + trigger OnAction() + var + ConnectPage: Page "PVS SM Assign To Job Costing"; + ErrorLbl: Label 'Only count entries that have not already been included in a job costing entry can be assigned'; + begin + Rec.TestField("Entry No."); + Rec.TestField("Cost Center Code"); + Rec.TestField("Difference Count"); + if Rec."Include In Job Costing" then + Error(ErrorLbl); + + ConnectPage.SetContext(Rec); + ConnectPage.RunModal(); + end; + } + } + area(Navigation) + { + action(XMLContent) + { + Caption = 'XML Content'; + Image = XMLFile; + ToolTip = 'Displays the XML content of the counter entry'; + + trigger OnAction() + var + XmlContentNotFoundLbl: Label 'No XML content found'; + XMLContent: Text; + begin + XMLContent := Rec.GetXMLContent(); + if (XMLContent = '') then + Message(XmlContentNotFoundLbl) + else + Message(XMLContent); + end; + } + } + area(Promoted) + { + actionref(AssignPromoted; Assign) { } + actionref(XMLContentPromoted; XMLContent) { } + } + } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntry.Table.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntry.Table.al new file mode 100644 index 0000000..dc76b72 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCounterEntry.Table.al @@ -0,0 +1,122 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +using System.Reflection; + +table 80294 "PVS SM Counter Entry" +{ + Caption = 'SpencerMetrics Counter Entry'; + DataClassification = CustomerContent; + DrillDownPageId = "PVS SM Counter Entries"; + LookupPageId = "PVS SM Counter Entries"; + + fields + { + field(1; "Cost Center Code"; Code[20]) + { + Caption = 'Cost Center Code'; + TableRelation = "PVs Cost Center"; + ToolTip = 'Cost Center Code for SpencerMetrics'; + } + field(2; "Entry No."; BigInteger) + { + AutoIncrement = true; + Caption = 'Entry No.'; + ToolTip = 'Entry No. for SpencerMetrics'; + Editable = false; + } + field(3; "XML Content"; Blob) + { + Caption = 'XML Content'; + ToolTip = 'XML Content from SpencerMetrics'; + } + field(4; "Current Count"; Integer) + { + Caption = 'Current Count'; + ToolTip = 'Current count from SpencerMetrics'; + } + field(6; "Difference Count"; Integer) + { + Caption = 'Difference Count'; + ToolTip = 'Difference between previous count and current count'; + } + field(7; "Current Time"; DateTime) + { + Caption = 'Current Time'; + ToolTip = 'Current date/time for this SpencerMetrics count'; + } + field(8; "Device Status"; Code[50]) + { + Caption = 'Device Status'; + ToolTip = 'SpencerMetrics device status'; + } + field(9; Speed; Integer) + { + Caption = 'Speed'; + ToolTip = 'Calculated speed of the machine'; + } + field(10; "Include In Job Costing"; Boolean) + { + Caption = 'Included in job costing'; + ToolTip = 'This is checked once the count has been included in a job costing entry'; + } + field(13; "Case ID"; Integer) + { + Caption = 'Case ID'; + ToolTip = 'The Case ID associated with this count'; + } + field(14; "Plan ID"; Integer) + { + Caption = 'Plan ID'; + ToolTip = 'The Plan ID associated with this count'; + } + field(15; UOM; Code[20]) + { + Caption = 'UOM'; + ToolTip = 'The UOM associated with this count'; + } + } + + keys + { + key(PK; "Cost Center Code", "Entry No.") + { + Clustered = true; + } + } + + internal procedure GetNextEntryNo(in_CostCenterCode: Code[20]): BigInteger + var + CounterEntry: Record "PVS SM Counter Entry"; + NextEntryNo: BigInteger; + begin + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", in_CostCenterCode); + if CounterEntry.FindLast() then + NextEntryNo := CounterEntry."Entry No." + else + NextEntryNo := 0; + + NextEntryNo += 1; + + exit(NextEntryNo); + end; + + internal procedure SetXMLContent(in_XMLContent: Text) + var + OutStream: OutStream; + begin + Clear("XML Content"); + "XML Content".CreateOutStream(OutStream, TextEncoding::UTF8); + OutStream.WriteText(in_XMLContent); + end; + + internal procedure GetXMLContent() XMLContent: Text + var + TypeHelper: Codeunit "Type Helper"; + InStream: InStream; + begin + CalcFields("XML Content"); + "XML Content".CreateInStream(InStream, TextEncoding::UTF8); + exit(TypeHelper.TryReadAsTextWithSepAndFieldErrMsg(InStream, TypeHelper.LFSeparator(), FieldName("XML Content"))); + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountsSumByUOM.Query.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountsSumByUOM.Query.al new file mode 100644 index 0000000..c6a8b21 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/Entries/PVSSMCountsSumByUOM.Query.al @@ -0,0 +1,16 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +query 80290 "PVS SM Counts Sum By UOM" +{ + elements + { + dataitem(CounterEntry; "PVS SM Counter Entry") + { + filter(CostCenter; "Cost Center Code") { } + filter(CaseID; "Case ID") { } + filter(PlanID; "Plan ID") { } + column(UOM; UOM) { } + column(DifferenceCountSum; "Difference Count") { Method = Sum; } + } + } +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMAssignToJobCosting.Page.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMAssignToJobCosting.Page.al new file mode 100644 index 0000000..a0ca4c0 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMAssignToJobCosting.Page.al @@ -0,0 +1,116 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +page 80296 "PVS SM Assign To Job Costing" +{ + ApplicationArea = All; + Caption = 'Assign Count to Job Costing Entry'; + PageType = StandardDialog; + + layout + { + area(content) + { + group(CountInfo) + { + Caption = 'Count'; + field("Count Entry No."; CountEntryNo) + { + Caption = 'Count Entry No.'; + ToolTip = 'The Entry No. of the Count entry that will be added to the Job Costing Entry.'; + Editable = false; + } + field("Count Cost Center"; CountCostCenterTxt) + { + Caption = 'Count Cost Center'; + ToolTip = 'The Cost Center of the Count entry that will be added to the Job Costing Entry.'; + Editable = false; + } + field("Count Quantity"; CountQty) + { + Caption = 'Count Quantity'; + Editable = false; + ToolTip = 'Quantity from the Count entry that will be added to the Job Costing Entry.'; + } + } + + group(JobCosting) + { + Caption = 'Job Costing'; + field(JobCostingEntryNo; JobCostingEntryNo) + { + Caption = 'Job Costing Entry'; + ToolTip = 'Choose a Job Costing Entry with the same Cost Center; entries with Type = Login are excluded.'; + + trigger OnLookup(var Text: Text): Boolean + var + JobCostingEntry: Record "PVS Job Costing Entry"; + ActionChosen: Action; + begin + JobCostingEntry.Reset(); + if CountCostCenterTxt <> '' then + JobCostingEntry.SetRange("Cost Center Code", CountCostCenterTxt); + JobCostingEntry.SetFilter("Costing Type", '%1', JobCostingEntry."Costing Type"::Time); + ActionChosen := PAGE.RunModal(PAGE::"PVS Job Costing - Details", JobCostingEntry); + if ActionChosen = Action::LookupOK then begin + JobCostingEntryNo := JobCostingEntry."Entry No."; + Text := Format(JobCostingEntryNo); + exit(true); + end; + + exit(false); + end; + } + } + } + } + + procedure SetContext(in_CounterEntry: Record "PVS SM Counter Entry") + begin + this.CounterEntry := in_CounterEntry; + end; + + trigger OnOpenPage() + begin + this.CountEntryNo := this.CounterEntry."Entry No."; + this.CountCostCenterTxt := this.CounterEntry."Cost Center Code"; + this.CountQty := this.CounterEntry."Difference Count"; + end; + + trigger OnQueryClosePage(CloseAction: Action): Boolean + var + JobCostingEntry: Record "PVS Job Costing Entry"; + begin + if CloseAction <> Action::OK then + exit(true); + + if JobCostingEntryNo = 0 then + Error(SelectJobCostingEntryErr); + + if not JobCostingEntry.Get(JobCostingEntryNo) then + Error(JobCostingEntryNotFoundErr, JobCostingEntryNo); + + if (CountCostCenterTxt <> '') and (JobCostingEntry."Cost Center Code" <> CountCostCenterTxt) then + Error(CostCenterMismatchErr); + + JobCostingEntry.Validate("Quantity", JobCostingEntry."Quantity" + CountQty); + JobCostingEntry.Modify(true); + + this.CounterEntry.Validate("Include In Job Costing", true); + this.CounterEntry.Validate("Case ID", JobCostingEntry.ID); + this.CounterEntry.Validate("Plan ID", JobCostingEntry."Plan ID"); + this.CounterEntry.Validate(UOM, JobCostingEntry.UOM); + this.CounterEntry.Modify(true); + + exit(true); + end; + + var + CounterEntry: Record "PVS SM Counter Entry"; + CountEntryNo: BigInteger; + CountCostCenterTxt: Code[20]; + CountQty: Integer; + JobCostingEntryNo: Integer; + CostCenterMismatchErr: Label 'Selected Job Costing Entry has a different Cost Center.'; + JobCostingEntryNotFoundErr: Label 'Job Costing Entry %1 was not found.', Comment = '%1 = Job Costing Entry No.'; + SelectJobCostingEntryErr: Label 'Please select a Job Costing Entry before clicking OK.'; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMCounterWorker.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMCounterWorker.Codeunit.al new file mode 100644 index 0000000..6d38057 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMCounterWorker.Codeunit.al @@ -0,0 +1,91 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +using PrintVis.SpencerMetrics.Configuration; +using System.Threading; +using PrintVis.SpencerMetrics.Services; + +codeunit 80293 "PVS SM Counter Worker" +{ + Access = Internal; + TableNo = "Job Queue Entry"; + + trigger OnRun() + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + SessionParameter: Record "PVS SM Session Parameter"; + CostCenterCode: Code[20]; + KeepRunning: Boolean; + Interval: Integer; + MySessionId: Integer; + CostCenterSetupNotFoundErr: Label 'Cost center setup not found for %1', Comment = '%1 = RecordId'; + begin + if not SMCostCenterSetup.Get(Rec."Record ID to Process") then + Error(CostCenterSetupNotFoundErr, Rec."Record ID to Process"); + + CostCenterCode := SMCostCenterSetup."Cost Center"; + MySessionId := SessionId(); + KeepRunning := true; + + Clear(SessionParameter); + SessionParameter."Session Id" := MySessionId; + SessionParameter."Setup Code" := CostCenterCode; + if not SessionParameter.Insert(true) then + SessionParameter.Modify(true); + + SMCostCenterSetup."Session Id" := MySessionId; + SMCostCenterSetup.SetLastHeartbeat(); + SMCostCenterSetup.Modify(); + Commit(); + + if SMCostCenterSetup."Interval (ms)" < 5000 then + Interval := 5000 + else + Interval := SMCostCenterSetup."Interval (ms)"; + + while KeepRunning do begin + SMCostCenterSetup.Get(CostCenterCode); + SMCostCenterSetup.SetLastHeartbeat(); + SMCostCenterSetup.Modify(); + Commit(); + + if ShouldRotateSession(CostCenterCode, Rec.ID) then + if RotateSession(CostCenterCode) then + KeepRunning := false; + + if KeepRunning then begin + this.DoTick(CostCenterCode); + + if not SMCostCenterSetup.Enabled then + KeepRunning := false; + + if KeepRunning then + Sleep(Interval); + end; + end; + end; + + procedure DoTick(in_CostCenter: Code[20]) + var + SpencerMetricsApiClient: Codeunit "PVS SM Api Client"; + begin + SpencerMetricsApiClient.ProcessSpencerMetricsData(in_CostCenter); + end; + + local procedure ShouldRotateSession(in_CostCenter: Code[20]; in_JobQueueEntryId: Guid): Boolean + var + SessionManager: Codeunit "PVS SM Session Manager"; + ExpectedEnd: DateTime; + begin + ExpectedEnd := SessionManager.GetExpectedEndDateTime(in_CostCenter, in_JobQueueEntryId); + if ExpectedEnd = 0DT then + exit(false); + exit(CurrentDateTime() >= ExpectedEnd); + end; + + local procedure RotateSession(in_CostCenter: Code[20]): Boolean + var + SessionManager: Codeunit "PVS SM Session Manager"; + begin + exit(SessionManager.Rotate(in_CostCenter)); + end; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMSessionManager.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMSessionManager.Codeunit.al new file mode 100644 index 0000000..65a8be8 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/CounterManagement/PVSSMSessionManager.Codeunit.al @@ -0,0 +1,215 @@ +namespace PrintVis.SpencerMetrics.CounterManagement; + +using PrintVis.SpencerMetrics.Configuration; +using System.Threading; + +codeunit 80294 "PVS SM Session Manager" +{ + Access = Internal; + + var + SpencerMetricsLbl: Label 'SpencerMetrics: %1', Comment = '%1 = Error Message', Locked = true; + + procedure Start(in_CostCenter: Code[20]) + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + JobQueueEntry: Record "Job Queue Entry"; + EmptyGuid: Guid; + SessionStartErr: Label 'Failed to start session. %1', Comment = '%1 = Error Message'; + + begin + if not SMCostCenterSetup.Get(in_CostCenter) then + exit; + + if SMCostCenterSetup.Enabled then + exit; + + SMCostCenterSetup.Enabled := true; + SMCostCenterSetup."Last Error" := ''; + SMCostCenterSetup.Modify(true); + Commit(); + + ClearLastError(); + + Clear(JobQueueEntry); + JobQueueEntry.Init(); + JobQueueEntry.ID := CreateGuid(); + JobQueueEntry."Object Type to Run" := JobQueueEntry."Object Type to Run"::Codeunit; + JobQueueEntry."Object ID to Run" := Codeunit::"PVS SM Counter Worker"; + JobQueueEntry."Record ID to Process" := SMCostCenterSetup.RecordId; + JobQueueEntry."Run in User Session" := false; + JobQueueEntry."Record ID to Process" := SMCostCenterSetup.RecordId; + JobQueueEntry.Description := StrSubstNo(SpencerMetricsLbl, in_CostCenter); + if not JobQueueEntry.Insert(true) then begin + SMCostCenterSetup.Enabled := false; + SMCostCenterSetup."Last Error" := StrSubstNo(SessionStartErr, GetLastErrorText()); + SMCostCenterSetup.Modify(true); + Commit(); + LogSessionEvent(in_CostCenter, EmptyGuid, Enum::"PVS SM Session Event Type"::Start, false, SMCostCenterSetup."Last Error"); + exit; + end; + + Codeunit.Run(Codeunit::"Job Queue - Enqueue", JobQueueEntry); + + SMCostCenterSetup.SetLastHeartbeat(); + SMCostCenterSetup."Job Queue Entry ID" := JobQueueEntry.ID; + SMCostCenterSetup."Session Id" := 0; + SMCostCenterSetup.Modify(true); + Commit(); + LogSessionEvent(in_CostCenter, JobQueueEntry.ID, Enum::"PVS SM Session Event Type"::Start, true, ''); + end; + + procedure Stop(in_CostCenter: Code[20]) + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + SessionParameter: Record "PVS SM Session Parameter"; + JobQueueEntry: Record "Job Queue Entry"; + CounterEntry: Record "PVS SM Counter Entry"; + StoppedJobQueueEntryId: Guid; + StopSuccess: Boolean; + ErrorMsg: Text; + begin + if not SMCostCenterSetup.Get(in_CostCenter) then + exit; + + StoppedJobQueueEntryId := SMCostCenterSetup."Job Queue Entry ID"; + StopSuccess := true; + ErrorMsg := ''; + + SMCostCenterSetup.Enabled := false; + SMCostCenterSetup."Last Error" := ''; + + if not IsNullGuid(SMCostCenterSetup."Job Queue Entry ID") then + if JobQueueEntry.Get(SMCostCenterSetup."Job Queue Entry ID") then begin + JobQueueEntry.SetStatus(JobQueueEntry.Status::"On Hold"); + JobQueueEntry.Delete(true); + end; + + if (SMCostCenterSetup."Session Id" <> 0) and SessionParameter.Get(SMCostCenterSetup."Session Id") then + SessionParameter.Delete(); + + SMCostCenterSetup."Session Id" := 0; + Clear(SMCostCenterSetup."Job Queue Entry ID"); + SMCostCenterSetup.Modify(true); + + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", in_CostCenter); + CounterEntry.SetRange("Include In Job Costing", true); + if CounterEntry.Count() > 0 then + CounterEntry.DeleteAll(); + + CounterEntry.Reset(); + CounterEntry.SetRange("Cost Center Code", in_CostCenter); + CounterEntry.SetFilter("Difference Count", '=0'); + if CounterEntry.Count() > 0 then + CounterEntry.DeleteAll(); + + LogSessionEvent(in_CostCenter, StoppedJobQueueEntryId, Enum::"PVS SM Session Event Type"::Stop, StopSuccess, ErrorMsg); + end; + + procedure Rotate(in_CostCenter: Code[20]): Boolean + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + SessionParameter: Record "PVS SM Session Parameter"; + OldJobQueueEntry: Record "Job Queue Entry"; + NewJobQueueEntry: Record "Job Queue Entry"; + OldSessionId: Integer; + OldJobQueueEntryId: Guid; + EmptyGuid: Guid; + SessionStartErr: Label 'Failed to start rotated session. %1', Comment = '%1 = Error Message'; + begin + if not SMCostCenterSetup.Get(in_CostCenter) then + exit(false); + + if not SMCostCenterSetup.Enabled then + exit(false); + + OldSessionId := SMCostCenterSetup."Session Id"; + OldJobQueueEntryId := SMCostCenterSetup."Job Queue Entry ID"; + + ClearLastError(); + + Clear(NewJobQueueEntry); + NewJobQueueEntry.Init(); + NewJobQueueEntry.ID := CreateGuid(); + NewJobQueueEntry."Object Type to Run" := NewJobQueueEntry."Object Type to Run"::Codeunit; + NewJobQueueEntry."Object ID to Run" := Codeunit::"PVS SM Counter Worker"; + NewJobQueueEntry."Record ID to Process" := SMCostCenterSetup.RecordId; + NewJobQueueEntry."Run in User Session" := false; + NewJobQueueEntry.Description := StrSubstNo(SpencerMetricsLbl, in_CostCenter); + if not NewJobQueueEntry.Insert(true) then begin + SMCostCenterSetup."Last Error" := StrSubstNo(SessionStartErr, GetLastErrorText()); + SMCostCenterSetup.Modify(true); + Commit(); + LogSessionEvent(in_CostCenter, EmptyGuid, Enum::"PVS SM Session Event Type"::Start, false, SMCostCenterSetup."Last Error"); + exit(false); + end; + + Codeunit.Run(Codeunit::"Job Queue - Enqueue", NewJobQueueEntry); + + LogSessionEvent(in_CostCenter, OldJobQueueEntryId, Enum::"PVS SM Session Event Type"::Stop, true, ''); + + if not IsNullGuid(OldJobQueueEntryId) then + if OldJobQueueEntry.Get(OldJobQueueEntryId) then begin + OldJobQueueEntry.SetStatus(OldJobQueueEntry.Status::"On Hold"); + OldJobQueueEntry.Delete(true); + end; + + + if (OldSessionId <> 0) and SessionParameter.Get(OldSessionId) then + SessionParameter.Delete(); + + SMCostCenterSetup.SetLastHeartbeat(); + SMCostCenterSetup."Job Queue Entry ID" := NewJobQueueEntry.ID; + SMCostCenterSetup."Session Id" := 0; + SMCostCenterSetup."Last Error" := ''; + SMCostCenterSetup.Modify(true); + Commit(); + + LogSessionEvent(in_CostCenter, NewJobQueueEntry.ID, Enum::"PVS SM Session Event Type"::Start, true, ''); + + exit(true); + end; + + procedure GetExpectedEndDateTime(in_CostCenter: Code[20]; in_JobQueueEntryId: Guid): DateTime + var + SessionLog: Record "PVS SM Session Log"; + begin + SessionLog.Reset(); + SessionLog.SetRange("Cost Center", in_CostCenter); + SessionLog.SetRange("Job Queue Entry ID", in_JobQueueEntryId); + SessionLog.SetRange("Event Type", Enum::"PVS SM Session Event Type"::Start); + SessionLog.SetRange(Success, true); + if SessionLog.FindLast() then + exit(SessionLog."Expected End DateTime"); + exit(0DT); + end; + + local procedure LogSessionEvent( + in_CostCenter: Code[20]; + in_JobQueueEntryId: Guid; + in_EventType: Enum "PVS SM Session Event Type"; + in_Success: Boolean; + in_ErrorMessage: Text) + var + SessionLog: Record "PVS SM Session Log"; + SMSetup: Record "PVS SM Setup"; + begin + Clear(SessionLog); + SessionLog."Cost Center" := in_CostCenter; + SessionLog."Job Queue Entry ID" := in_JobQueueEntryId; + SessionLog."Event Type" := in_EventType; + SessionLog."Event DateTime" := CurrentDateTime(); + SessionLog."User ID" := CopyStr(UserId(), 1, MaxStrLen(SessionLog."User ID")); + SessionLog.Success := in_Success; + SessionLog."Error Message" := CopyStr(in_ErrorMessage, 1, MaxStrLen(SessionLog."Error Message")); + + if (in_EventType = Enum::"PVS SM Session Event Type"::Start) and in_Success then begin + SMSetup.Get(); + if SMSetup."Max Session Duration (min)" > 0 then + SessionLog."Expected End DateTime" := SessionLog."Event DateTime" + (SMSetup."Max Session Duration (min)" * 60000); + end; + + SessionLog.Insert(true); + end; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMElectronicTicket.PageExt.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMElectronicTicket.PageExt.al new file mode 100644 index 0000000..a5f520c --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMElectronicTicket.PageExt.al @@ -0,0 +1,32 @@ +namespace PrintVis.SpencerMetrics.Integration; + +using PrintVis.SpencerMetrics.Configuration; +using PrintVis.SpencerMetrics.CounterManagement; + +pageextension 80290 "PVS SM Electronic Ticket" extends "PVS SF Electronic Ticket" +{ + layout + { + addbefore(QtyPart) + { + part("PVS SM Count Summary"; "PVS SM Count FactBox") + { + ApplicationArea = All; + Caption = 'SpencerMetrics Count Summary'; + Visible = ShowCountsFactBox; + } + } + } + + var + ShowCountsFactBox: Boolean; + + trigger OnOpenPage() + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + ShopFloorManagement: Codeunit "PVS Shop Floor Management"; + begin + SMCostCenterSetup.SetRange("Cost Center", ShopFloorManagement.User_Get_Current_CostCenter()); + ShowCountsFactBox := not SMCostCenterSetup.IsEmpty; + end; +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMSheet.TableExt.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMSheet.TableExt.al new file mode 100644 index 0000000..a6bf520 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/PrintVisIntegration/PVSSMSheet.TableExt.al @@ -0,0 +1,35 @@ +namespace PrintVis.SpencerMetrics.Integration; + +tableextension 80290 "PVS SM Sheet" extends "PVS Job Sheet" +{ + fields + { + field(80290; "PVS SM Key"; Text[30]) + { + Caption = 'SpencerMetrics Key'; + DataClassification = CustomerContent; + Editable = false; + } + } + + keys + { + key("PVS SM Key"; "PVS SM Key") { } + } + + trigger OnBeforeInsert() + begin + this.GenerateSpencerMetricsKey(Rec); + end; + + trigger OnBeforeModify() + begin + this.GenerateSpencerMetricsKey(Rec); + end; + + local procedure GenerateSpencerMetricsKey(var in_JobSheet: Record "PVS Job Sheet") + begin + in_JobSheet.CalcFields("Order No."); + in_JobSheet."PVS SM Key" := Format(in_JobSheet."Order No.") + '_Se.' + Format(in_JobSheet."Sheet No."); + end; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Services/PVSSMApiClient.Codeunit.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Services/PVSSMApiClient.Codeunit.al new file mode 100644 index 0000000..2034056 --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Services/PVSSMApiClient.Codeunit.al @@ -0,0 +1,233 @@ +namespace PrintVis.SpencerMetrics.Services; + +using PrintVis.SpencerMetrics.CounterManagement; +using PrintVis.SpencerMetrics.Configuration; +using PrintVis.SpencerMetrics; + +codeunit 80295 "PVS SM Api Client" +{ + Access = Internal; + + var + CostCenterSetupNotFoundErr: Label 'Cost Center SpencerMetrics setup not found'; + InvalidJsonResponseErr: Label 'Invalid JSON response'; + InvalidLoginKeyErr: Label 'Unauthorized - invalid login key'; + UnableToProcessRequestErr: Label 'Unable to process request: Error Code = %1', Comment = '%1 = Error Code'; + + procedure ProcessSpencerMetricsData(in_CostCenterCode: Code[20]) + var + SMCostCenterSetup: Record "PVS SM Cost Center Setup"; + CounterEntry: Record "PVS SM Counter Entry"; + SMSetup: Record "PVS SM Setup"; + Prefix, RequestUrl, Response, SubString : Text; + begin + if (in_CostCenterCode = '') then + exit; + + SMSetup.Get(); + SMSetup.TestField("Base URL"); + + if SMCostCenterSetup.Get(in_CostCenterCode) then begin + this.Authenticate(SMSetup."Base URL", SMCostCenterSetup); + + RequestUrl := this.BuildRequestUrl(SMSetup."Base URL", 'getXMLData?ApiSid=' + SMCostCenterSetup."Login Key"); + Response := this.SendGetRequest(RequestUrl, Response); + + if StrLen(Response) >= 5 then + Prefix := CopyStr(Response, 1, 5) + else + Prefix := Response; + + if Prefix = ' '' then + exit(DataText); + if ErrorText <> '' then + exit(ErrorText); + end; + + procedure ParseXMLResponse( + in_ResponseText: Text; + var out_CounterEntry: Record "PVS SM Counter Entry"; + var out_SMCostCenterSetup: Record "PVS SM Cost Center Setup") + var + PrevCounterEntry: Record "PVS SM Counter Entry"; + Hours: Decimal; + ElapsedTime: Duration; + DiffCount, + TotalProductionCounter : Integer; + CounterTxt: Text; + DeviceStatus: Text; + Attribute: XmlAttribute; + XmlDoc: XmlDocument; + Node: XmlNode; + begin + // Parse XML into XmlDocument (static call) + if not XmlDocument.ReadFrom(in_ResponseText, XmlDoc) then begin + out_SMCostCenterSetup."Last Error" := 'Invalid XML.'; + exit; + end; + + // TimeStamp on root + out_CounterEntry."Current Time" := CurrentDateTime(); + + // DeviceStatus on + if XmlDoc.SelectSingleNode('/*[local-name()="JMF"]/*[local-name()="Signal"]/*[local-name()="DeviceInfo"]/@DeviceStatus', Node) then begin + Attribute := Node.AsXmlAttribute(); + DeviceStatus := Attribute.Value; + out_CounterEntry."Device Status" := CopyStr(DeviceStatus, 1, MaxStrLen(out_CounterEntry."Device Status")); + end else + DeviceStatus := ''; + + // TotalProductionCounter on + if XmlDoc.SelectSingleNode('/*[local-name()="JMF"]/*[local-name()="Signal"]/*[local-name()="DeviceInfo"]/@TotalProductionCounter', Node) then begin + Attribute := Node.AsXmlAttribute(); + CounterTxt := Attribute.Value; + if not Evaluate(TotalProductionCounter, CounterTxt) then + TotalProductionCounter := 0; + end else + TotalProductionCounter := 0; + + out_CounterEntry."Current Count" := TotalProductionCounter; + + //Compare against previous record + PrevCounterEntry.Reset(); + PrevCounterEntry.SetRange("Cost Center Code", out_CounterEntry."Cost Center Code"); + if PrevCounterEntry.FindLast() then begin + out_CounterEntry.UOM := PrevCounterEntry.UOM; + out_CounterEntry."Case ID" := PrevCounterEntry."Case ID"; + out_CounterEntry."Plan ID" := PrevCounterEntry."Plan ID"; + if PrevCounterEntry."Current Count" <> -1 then + DiffCount := out_CounterEntry."Current Count" - PrevCounterEntry."Current Count" + else + DiffCount := 0; + out_CounterEntry."Difference Count" := DiffCount; + if PrevCounterEntry."Current Time" = 0DT then + ElapsedTime := out_CounterEntry."Current Time" - out_CounterEntry."Current Time" + else + ElapsedTime := out_CounterEntry."Current Time" - PrevCounterEntry."Current Time"; + + if ElapsedTime = 0 then + out_CounterEntry.Speed := 0 + else begin + Hours := ElapsedTime / (1000 * 60 * 60); + if Hours > 0 then + out_CounterEntry.Speed := Round(DiffCount / Hours, 1); + end; + end; + end; + + local procedure BuildRequestUrl(in_BaseUrl: Text; in_Resource: Text): Text + var + RequestUrl: Text; + begin + RequestUrl := in_BaseUrl; + if not RequestUrl.EndsWith('/') then + RequestUrl := RequestUrl + '/'; + + RequestUrl := RequestUrl + in_Resource; + + exit(RequestUrl); + end; + + procedure SendGetRequest(in_Uri: Text; in_ServiceAction: Text): Text + var + IsHandled: Boolean; + Client: HttpClient; + RequestMessage: HttpRequestMessage; + ResponseMessage: HttpResponseMessage; + Response: Text; + begin + IsHandled := false; + this.MockWebRequest(in_ServiceAction, Response, IsHandled); + if IsHandled then + exit(Response); + + Client.DefaultRequestHeaders().Clear(); + RequestMessage.SetRequestUri(in_Uri); + RequestMessage.Method('GET'); + if Client.Send(RequestMessage, ResponseMessage) then begin + ResponseMessage.Content().ReadAs(Response); + exit(Response); + end else + Error(UnableToProcessRequestErr, ResponseMessage.HttpStatusCode()); + end; + + local procedure MockWebRequest(in_ServiceAction: Text; var out_Response: Text; var out_IsHandled: Boolean) + var + SpencerMetricsEvents: Codeunit "PVS SM Events"; + begin + if in_ServiceAction = '' then + exit; + + SpencerMetricsEvents.OnMockWebRequest(in_ServiceAction, out_Response, out_IsHandled); + end; +} diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/PVSSMTimerAddIn.ControlAddin.al b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/PVSSMTimerAddIn.ControlAddin.al new file mode 100644 index 0000000..5d5daac --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/PVSSMTimerAddIn.ControlAddin.al @@ -0,0 +1,15 @@ +namespace PrintVis.SpencerMetrics.Utilities; + +controladdin "PVS SM Timer AddIn" +{ + HorizontalStretch = false; + RequestedHeight = 1; + RequestedWidth = 1; + Scripts = 'src/Utilities/TimerAddIn/timer.js'; + VerticalStretch = false; + + event Ready(); + event OnTick(); + procedure Start(IntervalMs: Integer) + procedure Stop() +} \ No newline at end of file diff --git a/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/timer.js b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/timer.js new file mode 100644 index 0000000..c37eeea --- /dev/null +++ b/Apps/SpencerMetrics/PrintVis SpencerMetrics/src/Utilities/TimerAddIn/timer.js @@ -0,0 +1,65 @@ +(function () { + var state = { + interval: 30000, + running: false, + handle: null + }; + + function fireReady() { + try { + Microsoft.Dynamics.NAV.InvokeExtensibilityMethod( + 'Ready', // AL event name in your controladdin + [], // args to AL + false, // runOnServer + function onSuccess() { /* no-op */ }, + function onError(err) { /* optional logging/backoff */ } + ); + } catch (e) { /* ignore */ } + } + + function scheduleNext() { + if (!state.running) return; + state.handle = window.setTimeout(invokeTick, state.interval); + } + + function invokeTick() { + if (!state.running) return; + Microsoft.Dynamics.NAV.InvokeExtensibilityMethod( + 'OnTick', // AL event name + [], // args to AL (add items here if needed) + false, // runOnServer + function onSuccess() { + // AL finished handling the tick → schedule the next one + scheduleNext(); + }, + function onError(err) { + // Optionally implement backoff/retry strategy here + scheduleNext(); // still schedule the next tick + } + ); + } + + // Exposed to AL + window.Start = function (ms) { + if (typeof ms === 'number' && ms > 0) state.interval = ms; + + // restart cleanly + window.Stop(); + state.running = true; + + // fire first tick immediately; subsequent ticks are scheduled after AL completes + invokeTick(); + }; + + // Exposed to AL + window.Stop = function () { + state.running = false; + if (state.handle) { + window.clearTimeout(state.handle); + state.handle = null; + } + }; + + // Announce readiness once the script is loaded + fireReady(); +})(); \ No newline at end of file