Merge pull request 'Fixes' (#21) from fixes into dev

Reviewed-on: #21
This commit was merged in pull request #21.
This commit is contained in:
2026-09-10 10:30:04 +00:00
176 changed files with 17387 additions and 1809 deletions
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Distribution.Tests")] [assembly: System.Reflection.AssemblyCompanyAttribute("Distribution.Tests")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+aa7ea37ab61a149a38d83283c5dabd76bf5a39b3")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+85a16396aad31f1e82cf28f330a97dee2eafa51d")]
[assembly: System.Reflection.AssemblyProductAttribute("Distribution.Tests")] [assembly: System.Reflection.AssemblyProductAttribute("Distribution.Tests")]
[assembly: System.Reflection.AssemblyTitleAttribute("Distribution.Tests")] [assembly: System.Reflection.AssemblyTitleAttribute("Distribution.Tests")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
5c052f1f25bc37b6a51a3abe0e2b79d99d21422d5ab89151faea450c0ddb9cc0 100cae0f5643c7476420ef14e72b5b17dcb877fe4318c0b6cb2cff0baa955aa5
@@ -11,7 +11,7 @@ build_property.EnforceExtendedAnalyzerRules =
build_property.EntryPointFilePath = build_property.EntryPointFilePath =
build_property._SupportedPlatformList = Linux,macOS,Windows build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Distribution.Tests build_property.RootNamespace = Distribution.Tests
build_property.ProjectDir = C:\Users\lenovo\Desktop\NEWGITEA\ERP_Distribution\Distribution.Tests\ build_property.ProjectDir = D:\HexDrive Projects\Distribution\ERP_Distribution\Distribution.Tests\
build_property.EnableComHosting = build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop = build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EffectiveAnalysisLevelStyle = 9.0 build_property.EffectiveAnalysisLevelStyle = 9.0
@@ -1,23 +1,23 @@
{ {
"format": 1, "format": 1,
"restore": { "restore": {
"C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj": {} "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj": {}
}, },
"projects": { "projects": {
"C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj": { "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj": {
"version": "1.0.0", "version": "1.0.0",
"restore": { "restore": {
"projectUniqueName": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj", "projectUniqueName": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj",
"projectName": "Distribution.Tests", "projectName": "Distribution.Tests",
"projectPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj", "projectPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj",
"packagesPath": "C:\\Users\\lenovo\\.nuget\\packages\\", "packagesPath": "C:\\Users\\dell\\.nuget\\packages\\",
"outputPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\obj\\", "outputPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\obj\\",
"projectStyle": "PackageReference", "projectStyle": "PackageReference",
"fallbackFolders": [ "fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
], ],
"configFilePaths": [ "configFilePaths": [
"C:\\Users\\lenovo\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Users\\dell\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
], ],
@@ -26,6 +26,7 @@
], ],
"sources": { "sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"C:\\Program Files\\dotnet\\library-packs": {},
"https://api.nuget.org/v3/index.json": {} "https://api.nuget.org/v3/index.json": {}
}, },
"frameworks": { "frameworks": {
@@ -33,8 +34,8 @@
"framework": "net9.0", "framework": "net9.0",
"targetAlias": "net9.0", "targetAlias": "net9.0",
"projectReferences": { "projectReferences": {
"C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj": { "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj": {
"projectPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj" "projectPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj"
} }
} }
} }
@@ -49,7 +50,7 @@
"auditLevel": "low", "auditLevel": "low",
"auditMode": "direct" "auditMode": "direct"
}, },
"SdkAnalysisLevel": "10.0.300" "SdkAnalysisLevel": "10.0.400"
}, },
"frameworks": { "frameworks": {
"net9.0": { "net9.0": {
@@ -97,24 +98,24 @@
"privateAssets": "all" "privateAssets": "all"
} }
}, },
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.302/PortableRuntimeIdentifierGraph.json" "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.400/PortableRuntimeIdentifierGraph.json"
} }
} }
}, },
"C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj": { "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj": {
"version": "1.0.0", "version": "1.0.0",
"restore": { "restore": {
"projectUniqueName": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj", "projectUniqueName": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj",
"projectName": "Distribution", "projectName": "Distribution",
"projectPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj", "projectPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj",
"packagesPath": "C:\\Users\\lenovo\\.nuget\\packages\\", "packagesPath": "C:\\Users\\dell\\.nuget\\packages\\",
"outputPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\obj\\", "outputPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\obj\\",
"projectStyle": "PackageReference", "projectStyle": "PackageReference",
"fallbackFolders": [ "fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
], ],
"configFilePaths": [ "configFilePaths": [
"C:\\Users\\lenovo\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Users\\dell\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
], ],
@@ -123,6 +124,7 @@
], ],
"sources": { "sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"C:\\Program Files\\dotnet\\library-packs": {},
"https://api.nuget.org/v3/index.json": {} "https://api.nuget.org/v3/index.json": {}
}, },
"frameworks": { "frameworks": {
@@ -142,7 +144,7 @@
"auditLevel": "low", "auditLevel": "low",
"auditMode": "direct" "auditMode": "direct"
}, },
"SdkAnalysisLevel": "10.0.300" "SdkAnalysisLevel": "10.0.400"
}, },
"frameworks": { "frameworks": {
"net9.0": { "net9.0": {
@@ -191,7 +193,7 @@
"privateAssets": "all" "privateAssets": "all"
} }
}, },
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.302/PortableRuntimeIdentifierGraph.json" "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.400/PortableRuntimeIdentifierGraph.json"
} }
} }
} }
@@ -5,12 +5,12 @@
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool> <RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile> <ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot> <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\lenovo\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders> <NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\dell\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle> <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">7.0.0</NuGetToolVersion> <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">7.0.0</NuGetToolVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\lenovo\.nuget\packages\" /> <SourceRoot Include="C:\Users\dell\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" /> <SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup> </ItemGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
@@ -22,7 +22,7 @@
<Import Project="$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props')" /> <Import Project="$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props')" />
</ImportGroup> </ImportGroup>
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' "> <PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Pkgxunit_analyzers Condition=" '$(Pkgxunit_analyzers)' == '' ">C:\Users\lenovo\.nuget\packages\xunit.analyzers\1.18.0</Pkgxunit_analyzers> <Pkgxunit_analyzers Condition=" '$(Pkgxunit_analyzers)' == '' ">C:\Users\dell\.nuget\packages\xunit.analyzers\1.18.0</Pkgxunit_analyzers>
<PkgMicrosoft_Extensions_ApiDescription_Server Condition=" '$(PkgMicrosoft_Extensions_ApiDescription_Server)' == '' ">C:\Users\lenovo\.nuget\packages\microsoft.extensions.apidescription.server\6.0.5</PkgMicrosoft_Extensions_ApiDescription_Server> <PkgMicrosoft_Extensions_ApiDescription_Server Condition=" '$(PkgMicrosoft_Extensions_ApiDescription_Server)' == '' ">C:\Users\dell\.nuget\packages\microsoft.extensions.apidescription.server\6.0.5</PkgMicrosoft_Extensions_ApiDescription_Server>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
+11 -10
View File
@@ -2542,23 +2542,23 @@
] ]
}, },
"packageFolders": { "packageFolders": {
"C:\\Users\\lenovo\\.nuget\\packages\\": {}, "C:\\Users\\dell\\.nuget\\packages\\": {},
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {} "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
}, },
"project": { "project": {
"version": "1.0.0", "version": "1.0.0",
"restore": { "restore": {
"projectUniqueName": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj", "projectUniqueName": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj",
"projectName": "Distribution.Tests", "projectName": "Distribution.Tests",
"projectPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj", "projectPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj",
"packagesPath": "C:\\Users\\lenovo\\.nuget\\packages\\", "packagesPath": "C:\\Users\\dell\\.nuget\\packages\\",
"outputPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\obj\\", "outputPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\obj\\",
"projectStyle": "PackageReference", "projectStyle": "PackageReference",
"fallbackFolders": [ "fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
], ],
"configFilePaths": [ "configFilePaths": [
"C:\\Users\\lenovo\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Users\\dell\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
], ],
@@ -2567,6 +2567,7 @@
], ],
"sources": { "sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"C:\\Program Files\\dotnet\\library-packs": {},
"https://api.nuget.org/v3/index.json": {} "https://api.nuget.org/v3/index.json": {}
}, },
"frameworks": { "frameworks": {
@@ -2574,8 +2575,8 @@
"framework": "net9.0", "framework": "net9.0",
"targetAlias": "net9.0", "targetAlias": "net9.0",
"projectReferences": { "projectReferences": {
"C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj": { "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj": {
"projectPath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution\\Distribution.csproj" "projectPath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution\\Distribution.csproj"
} }
} }
} }
@@ -2590,7 +2591,7 @@
"auditLevel": "low", "auditLevel": "low",
"auditMode": "direct" "auditMode": "direct"
}, },
"SdkAnalysisLevel": "10.0.300" "SdkAnalysisLevel": "10.0.400"
}, },
"frameworks": { "frameworks": {
"net9.0": { "net9.0": {
@@ -2638,7 +2639,7 @@
"privateAssets": "all" "privateAssets": "all"
} }
}, },
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.302/PortableRuntimeIdentifierGraph.json" "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.400/PortableRuntimeIdentifierGraph.json"
} }
} }
} }
+50 -50
View File
@@ -1,57 +1,57 @@
{ {
"version": 2, "version": 2,
"dgSpecHash": "XdH8cqlThLk=", "dgSpecHash": "JPCR3IFZHKY=",
"success": true, "success": true,
"projectFilePath": "C:\\Users\\lenovo\\Desktop\\NEWGITEA\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj", "projectFilePath": "D:\\HexDrive Projects\\Distribution\\ERP_Distribution\\Distribution.Tests\\Distribution.Tests.csproj",
"expectedPackageFiles": [ "expectedPackageFiles": [
"C:\\Users\\lenovo\\.nuget\\packages\\castle.core\\5.1.1\\castle.core.5.1.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\castle.core\\5.1.1\\castle.core.5.1.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\coverlet.collector\\6.0.2\\coverlet.collector.6.0.2.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\coverlet.collector\\6.0.2\\coverlet.collector.6.0.2.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.aspnetcore.authentication.jwtbearer\\9.0.0\\microsoft.aspnetcore.authentication.jwtbearer.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.aspnetcore.authentication.jwtbearer\\9.0.0\\microsoft.aspnetcore.authentication.jwtbearer.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.aspnetcore.openapi\\9.0.0\\microsoft.aspnetcore.openapi.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.aspnetcore.openapi\\9.0.0\\microsoft.aspnetcore.openapi.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.codecoverage\\17.12.0\\microsoft.codecoverage.17.12.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.codecoverage\\17.12.0\\microsoft.codecoverage.17.12.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.entityframeworkcore\\9.0.0\\microsoft.entityframeworkcore.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.entityframeworkcore\\9.0.0\\microsoft.entityframeworkcore.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.entityframeworkcore.abstractions\\9.0.0\\microsoft.entityframeworkcore.abstractions.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.entityframeworkcore.abstractions\\9.0.0\\microsoft.entityframeworkcore.abstractions.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.entityframeworkcore.analyzers\\9.0.0\\microsoft.entityframeworkcore.analyzers.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.entityframeworkcore.analyzers\\9.0.0\\microsoft.entityframeworkcore.analyzers.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.entityframeworkcore.relational\\9.0.0\\microsoft.entityframeworkcore.relational.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.entityframeworkcore.relational\\9.0.0\\microsoft.entityframeworkcore.relational.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.apidescription.server\\6.0.5\\microsoft.extensions.apidescription.server.6.0.5.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.apidescription.server\\6.0.5\\microsoft.extensions.apidescription.server.6.0.5.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.caching.abstractions\\9.0.0\\microsoft.extensions.caching.abstractions.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.caching.abstractions\\9.0.0\\microsoft.extensions.caching.abstractions.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.caching.memory\\9.0.0\\microsoft.extensions.caching.memory.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.caching.memory\\9.0.0\\microsoft.extensions.caching.memory.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.configuration.abstractions\\9.0.0\\microsoft.extensions.configuration.abstractions.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.configuration.abstractions\\9.0.0\\microsoft.extensions.configuration.abstractions.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\9.0.0\\microsoft.extensions.dependencyinjection.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\9.0.0\\microsoft.extensions.dependencyinjection.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\9.0.0\\microsoft.extensions.dependencyinjection.abstractions.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\9.0.0\\microsoft.extensions.dependencyinjection.abstractions.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.logging\\9.0.0\\microsoft.extensions.logging.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.logging\\9.0.0\\microsoft.extensions.logging.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.logging.abstractions\\9.0.0\\microsoft.extensions.logging.abstractions.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.logging.abstractions\\9.0.0\\microsoft.extensions.logging.abstractions.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.options\\9.0.0\\microsoft.extensions.options.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.options\\9.0.0\\microsoft.extensions.options.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.extensions.primitives\\9.0.0\\microsoft.extensions.primitives.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.extensions.primitives\\9.0.0\\microsoft.extensions.primitives.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.abstractions\\8.0.1\\microsoft.identitymodel.abstractions.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.abstractions\\8.0.1\\microsoft.identitymodel.abstractions.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.jsonwebtokens\\8.0.1\\microsoft.identitymodel.jsonwebtokens.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.jsonwebtokens\\8.0.1\\microsoft.identitymodel.jsonwebtokens.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.logging\\8.0.1\\microsoft.identitymodel.logging.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.logging\\8.0.1\\microsoft.identitymodel.logging.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.protocols\\8.0.1\\microsoft.identitymodel.protocols.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.protocols\\8.0.1\\microsoft.identitymodel.protocols.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.protocols.openidconnect\\8.0.1\\microsoft.identitymodel.protocols.openidconnect.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.protocols.openidconnect\\8.0.1\\microsoft.identitymodel.protocols.openidconnect.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.identitymodel.tokens\\8.0.1\\microsoft.identitymodel.tokens.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.identitymodel.tokens\\8.0.1\\microsoft.identitymodel.tokens.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.net.test.sdk\\17.12.0\\microsoft.net.test.sdk.17.12.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.net.test.sdk\\17.12.0\\microsoft.net.test.sdk.17.12.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.openapi\\1.6.22\\microsoft.openapi.1.6.22.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.openapi\\1.6.22\\microsoft.openapi.1.6.22.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.testplatform.objectmodel\\17.12.0\\microsoft.testplatform.objectmodel.17.12.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.testplatform.objectmodel\\17.12.0\\microsoft.testplatform.objectmodel.17.12.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\microsoft.testplatform.testhost\\17.12.0\\microsoft.testplatform.testhost.17.12.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\microsoft.testplatform.testhost\\17.12.0\\microsoft.testplatform.testhost.17.12.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\moq\\4.20.72\\moq.4.20.72.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\moq\\4.20.72\\moq.4.20.72.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\mysqlconnector\\2.4.0\\mysqlconnector.2.4.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\mysqlconnector\\2.4.0\\mysqlconnector.2.4.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\newtonsoft.json\\13.0.1\\newtonsoft.json.13.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\newtonsoft.json\\13.0.1\\newtonsoft.json.13.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\pomelo.entityframeworkcore.mysql\\9.0.0\\pomelo.entityframeworkcore.mysql.9.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\pomelo.entityframeworkcore.mysql\\9.0.0\\pomelo.entityframeworkcore.mysql.9.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\swashbuckle.aspnetcore\\7.3.1\\swashbuckle.aspnetcore.7.3.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\swashbuckle.aspnetcore\\7.3.1\\swashbuckle.aspnetcore.7.3.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\swashbuckle.aspnetcore.swagger\\7.3.1\\swashbuckle.aspnetcore.swagger.7.3.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\swashbuckle.aspnetcore.swagger\\7.3.1\\swashbuckle.aspnetcore.swagger.7.3.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\swashbuckle.aspnetcore.swaggergen\\7.3.1\\swashbuckle.aspnetcore.swaggergen.7.3.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\swashbuckle.aspnetcore.swaggergen\\7.3.1\\swashbuckle.aspnetcore.swaggergen.7.3.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\swashbuckle.aspnetcore.swaggerui\\7.3.1\\swashbuckle.aspnetcore.swaggerui.7.3.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\swashbuckle.aspnetcore.swaggerui\\7.3.1\\swashbuckle.aspnetcore.swaggerui.7.3.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\system.diagnostics.eventlog\\6.0.0\\system.diagnostics.eventlog.6.0.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\system.diagnostics.eventlog\\6.0.0\\system.diagnostics.eventlog.6.0.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\system.identitymodel.tokens.jwt\\8.0.1\\system.identitymodel.tokens.jwt.8.0.1.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\system.identitymodel.tokens.jwt\\8.0.1\\system.identitymodel.tokens.jwt.8.0.1.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\system.reflection.metadata\\1.6.0\\system.reflection.metadata.1.6.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\system.reflection.metadata\\1.6.0\\system.reflection.metadata.1.6.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit\\2.9.3\\xunit.2.9.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit\\2.9.3\\xunit.2.9.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.abstractions\\2.0.3\\xunit.abstractions.2.0.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.abstractions\\2.0.3\\xunit.abstractions.2.0.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.analyzers\\1.18.0\\xunit.analyzers.1.18.0.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.analyzers\\1.18.0\\xunit.analyzers.1.18.0.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.assert\\2.9.3\\xunit.assert.2.9.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.assert\\2.9.3\\xunit.assert.2.9.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.core\\2.9.3\\xunit.core.2.9.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.core\\2.9.3\\xunit.core.2.9.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.extensibility.core\\2.9.3\\xunit.extensibility.core.2.9.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.extensibility.core\\2.9.3\\xunit.extensibility.core.2.9.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.extensibility.execution\\2.9.3\\xunit.extensibility.execution.2.9.3.nupkg.sha512", "C:\\Users\\dell\\.nuget\\packages\\xunit.extensibility.execution\\2.9.3\\xunit.extensibility.execution.2.9.3.nupkg.sha512",
"C:\\Users\\lenovo\\.nuget\\packages\\xunit.runner.visualstudio\\2.8.2\\xunit.runner.visualstudio.2.8.2.nupkg.sha512" "C:\\Users\\dell\\.nuget\\packages\\xunit.runner.visualstudio\\2.8.2\\xunit.runner.visualstudio.2.8.2.nupkg.sha512"
], ],
"logs": [] "logs": []
} }
@@ -1,3 +1,4 @@
using Distribution.Features.Stock;
using Distribution.Models; using Distribution.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -80,9 +81,9 @@ public static class OperationsExtrasSeed
); );
modelBuilder.Entity<StockMovement>().HasData( modelBuilder.Entity<StockMovement>().HasData(
new StockMovement { Id = StockMovement1Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = null, FromLocationType = null, ToLocationId = MasterDataSeed.ColomboWarehouseId, ToLocationType = LocationType.Warehouse, Quantity = 200m, Type = MovementType.PurchaseReceipt, ReasonCode = null, ReferenceId = ProcurementSeed.GoodsReceipt1Id, CreatedAt = new DateTime(2026, 8, 15), CreatedBy = MasterDataSeed.Supervisor1UserId }, new StockMovement { Id = StockMovement1Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = StockService.ExternalLocationId, FromLocationType = LocationType.External, ToLocationId = MasterDataSeed.ColomboWarehouseId, ToLocationType = LocationType.Warehouse, Quantity = 200m, Type = MovementType.PurchaseReceipt, ReasonCode = null, ReferenceId = ProcurementSeed.GoodsReceipt1Id, CreatedAt = new DateTime(2026, 8, 15), CreatedBy = MasterDataSeed.Supervisor1UserId },
new StockMovement { Id = StockMovement2Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = MasterDataSeed.ColomboWarehouseId, FromLocationType = LocationType.Warehouse, ToLocationId = MasterDataSeed.VanVehicleId, ToLocationType = LocationType.Vehicle, Quantity = 50m, Type = MovementType.Load, ReasonCode = null, ReferenceId = OperationsDataSeed.LoadingSheet1Id, CreatedAt = new DateTime(2026, 8, 25, 7, 30, 0), CreatedBy = MasterDataSeed.Supervisor1UserId }, new StockMovement { Id = StockMovement2Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = MasterDataSeed.ColomboWarehouseId, FromLocationType = LocationType.Warehouse, ToLocationId = MasterDataSeed.VanVehicleId, ToLocationType = LocationType.Vehicle, Quantity = 50m, Type = MovementType.Load, ReasonCode = null, ReferenceId = OperationsDataSeed.LoadingSheet1Id, CreatedAt = new DateTime(2026, 8, 25, 7, 30, 0), CreatedBy = MasterDataSeed.Supervisor1UserId },
new StockMovement { Id = StockMovement3Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = MasterDataSeed.VanVehicleId, FromLocationType = LocationType.Vehicle, ToLocationId = null, ToLocationType = null, Quantity = 30m, Type = MovementType.Sale, ReasonCode = null, ReferenceId = OperationsDataSeed.Sale1Id, CreatedAt = new DateTime(2026, 8, 25, 9, 30, 0), CreatedBy = MasterDataSeed.SalesRep1UserId } new StockMovement { Id = StockMovement3Id, FK_ItemId = MasterDataSeed.Coke500ItemId, FromLocationId = MasterDataSeed.VanVehicleId, FromLocationType = LocationType.Vehicle, ToLocationId = StockService.ExternalLocationId, ToLocationType = LocationType.External, Quantity = 30m, Type = MovementType.Sale, ReasonCode = null, ReferenceId = OperationsDataSeed.Sale1Id, CreatedAt = new DateTime(2026, 8, 25, 9, 30, 0), CreatedBy = MasterDataSeed.SalesRep1UserId }
); );
modelBuilder.Entity<StoreKycDocument>().HasData( modelBuilder.Entity<StoreKycDocument>().HasData(
+1 -1
View File
@@ -1,5 +1,5 @@
namespace Distribution.Features.Brand; namespace Distribution.Features.Brand;
public record CreateBrandRequest(string Name); public record CreateBrandRequest(string Name);
public record UpdateBrandRequest(string Name); public record UpdateBrandRequest(string Name, bool IsActive = true);
public record BrandResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt); public record BrandResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
+7 -4
View File
@@ -5,15 +5,16 @@ namespace Distribution.Features.Brand;
public class BrandService(IBrandRepository repo, IUnitOfWork uow) public class BrandService(IBrandRepository repo, IUnitOfWork uow)
{ {
public async Task<List<BrandResponse>> GetAllAsync() => public async Task<List<BrandResponse>> GetAllAsync() =>
// Deleted brands stay IsActive=false but remain visible showing Inactive — delete
// never removes the row, only marks it (same rule as Items/Stores/Categories).
(await repo.GetAllAsync()) (await repo.GetAllAsync())
.Where(b => b.IsActive)
.Select(b => new BrandResponse(b.Id, b.Name, b.IsActive, b.CreatedAt)) .Select(b => new BrandResponse(b.Id, b.Name, b.IsActive, b.CreatedAt))
.ToList(); .ToList();
public async Task<BrandResponse?> GetByIdAsync(Guid id) public async Task<BrandResponse?> GetByIdAsync(Guid id)
{ {
var brand = await repo.GetByIdAsync(id); var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return null; if (brand is null) return null;
return new BrandResponse(brand.Id, brand.Name, brand.IsActive, brand.CreatedAt); return new BrandResponse(brand.Id, brand.Name, brand.IsActive, brand.CreatedAt);
} }
@@ -29,9 +30,10 @@ public class BrandService(IBrandRepository repo, IUnitOfWork uow)
public async Task<BrandResponse?> UpdateAsync(Guid id, UpdateBrandRequest request, CancellationToken ct = default) public async Task<BrandResponse?> UpdateAsync(Guid id, UpdateBrandRequest request, CancellationToken ct = default)
{ {
var brand = await repo.GetByIdAsync(id); var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return null; if (brand is null) return null;
if (brand.ExternalErpId.HasValue) return null; if (brand.ExternalErpId.HasValue) return null;
brand.Name = request.Name; brand.Name = request.Name;
brand.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Update(brand); await uow.CommitAsync(ct); } try { repo.Update(brand); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -40,10 +42,11 @@ public class BrandService(IBrandRepository repo, IUnitOfWork uow)
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{ {
// Soft-delete only — never blocked by items still referencing this brand, same rule
// as Categories.
var brand = await repo.GetByIdAsync(id); var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return false; if (brand is null || !brand.IsActive) return false;
if (brand.ExternalErpId.HasValue) return false; if (brand.ExternalErpId.HasValue) return false;
if (await repo.HasActiveItemsAsync(id)) return false;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Remove(brand); await uow.CommitAsync(ct); } try { repo.Remove(brand); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -1,9 +1,9 @@
namespace Distribution.Features.Category; namespace Distribution.Features.Category;
public record CreateCategoryRequest(string Name); public record CreateCategoryRequest(string Name);
public record UpdateCategoryRequest(string Name); public record UpdateCategoryRequest(string Name, bool IsActive = true);
public record CreateSubCategoryRequest(string Name); public record CreateSubCategoryRequest(string Name);
public record UpdateSubCategoryRequest(string Name); public record UpdateSubCategoryRequest(string Name, bool IsActive = true);
public record SubCategoryResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt); public record SubCategoryResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
public record CategorySummaryResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt); public record CategorySummaryResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
@@ -4,18 +4,27 @@ namespace Distribution.Features.Category;
public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepository subCategoryRepo, IUnitOfWork uow) public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepository subCategoryRepo, IUnitOfWork uow)
{ {
public async Task<List<CategorySummaryResponse>> GetAllAsync() => public async Task<List<CategoryResponse>> GetAllAsync()
(await categoryRepo.GetAllAsync()) {
.Where(c => c.IsActive) // Deleted categories/sub-categories stay IsActive=false but remain visible showing
.Select(c => new CategorySummaryResponse(c.Id, c.Name, c.IsActive, c.CreatedAt)) // Inactive — delete never removes the row, only marks it (same rule as Orders/Stores/Items).
var categoryList = await categoryRepo.GetAllAsync();
var result = new List<CategoryResponse>();
foreach (var category in categoryList)
{
var subs = (await subCategoryRepo.GetByCategoryIdAsync(category.Id))
.Select(s => new SubCategoryResponse(s.Id, s.Name, s.IsActive, s.CreatedAt))
.ToList(); .ToList();
result.Add(new CategoryResponse(category.Id, category.Name, category.IsActive, category.CreatedAt, subs));
}
return result;
}
public async Task<CategoryResponse?> GetByIdAsync(Guid id) public async Task<CategoryResponse?> GetByIdAsync(Guid id)
{ {
var category = await categoryRepo.GetByIdAsync(id); var category = await categoryRepo.GetByIdAsync(id);
if (category is null || !category.IsActive) return null; if (category is null) return null;
var subs = (await subCategoryRepo.GetByCategoryIdAsync(id)) var subs = (await subCategoryRepo.GetByCategoryIdAsync(id))
.Where(s => s.IsActive)
.Select(s => new SubCategoryResponse(s.Id, s.Name, s.IsActive, s.CreatedAt)) .Select(s => new SubCategoryResponse(s.Id, s.Name, s.IsActive, s.CreatedAt))
.ToList(); .ToList();
return new CategoryResponse(category.Id, category.Name, category.IsActive, category.CreatedAt, subs); return new CategoryResponse(category.Id, category.Name, category.IsActive, category.CreatedAt, subs);
@@ -33,9 +42,10 @@ public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepos
public async Task<CategorySummaryResponse?> UpdateAsync(Guid id, UpdateCategoryRequest request, CancellationToken ct = default) public async Task<CategorySummaryResponse?> UpdateAsync(Guid id, UpdateCategoryRequest request, CancellationToken ct = default)
{ {
var category = await categoryRepo.GetByIdAsync(id); var category = await categoryRepo.GetByIdAsync(id);
if (category is null || !category.IsActive) return null; if (category is null) return null;
if (category.ExternalErpId.HasValue) return null; if (category.ExternalErpId.HasValue) return null;
category.Name = request.Name; category.Name = request.Name;
category.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { categoryRepo.Update(category); await uow.CommitAsync(ct); } try { categoryRepo.Update(category); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -44,11 +54,11 @@ public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepos
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{ {
// Soft-delete only — never blocked by child sub-categories/items, same rule as
// Items/Stores/Orders. The row stays visible showing Inactive.
var category = await categoryRepo.GetByIdAsync(id); var category = await categoryRepo.GetByIdAsync(id);
if (category is null || !category.IsActive) return false; if (category is null || !category.IsActive) return false;
if (category.ExternalErpId.HasValue) return false; if (category.ExternalErpId.HasValue) return false;
if (await categoryRepo.HasActiveSubCategoriesAsync(id)) return false;
if (await categoryRepo.HasActiveItemsAsync(id)) return false;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { categoryRepo.Remove(category); await uow.CommitAsync(ct); } try { categoryRepo.Remove(category); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -69,9 +79,10 @@ public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepos
public async Task<SubCategoryResponse?> UpdateSubCategoryAsync(Guid id, UpdateSubCategoryRequest request, CancellationToken ct = default) public async Task<SubCategoryResponse?> UpdateSubCategoryAsync(Guid id, UpdateSubCategoryRequest request, CancellationToken ct = default)
{ {
var sub = await subCategoryRepo.GetByIdAsync(id); var sub = await subCategoryRepo.GetByIdAsync(id);
if (sub is null || !sub.IsActive) return null; if (sub is null) return null;
if (sub.ExternalErpId.HasValue) return null; if (sub.ExternalErpId.HasValue) return null;
sub.Name = request.Name; sub.Name = request.Name;
sub.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { subCategoryRepo.Update(sub); await uow.CommitAsync(ct); } try { subCategoryRepo.Update(sub); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -80,10 +91,10 @@ public class CategoryService(ICategoryRepository categoryRepo, ISubCategoryRepos
public async Task<bool> DeleteSubCategoryAsync(Guid id, CancellationToken ct = default) public async Task<bool> DeleteSubCategoryAsync(Guid id, CancellationToken ct = default)
{ {
// Soft-delete only — never blocked by child items, same rule as the parent category.
var sub = await subCategoryRepo.GetByIdAsync(id); var sub = await subCategoryRepo.GetByIdAsync(id);
if (sub is null || !sub.IsActive) return false; if (sub is null || !sub.IsActive) return false;
if (sub.ExternalErpId.HasValue) return false; if (sub.ExternalErpId.HasValue) return false;
if (await subCategoryRepo.HasActiveItemsAsync(id)) return false;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { subCategoryRepo.Remove(sub); await uow.CommitAsync(ct); } try { subCategoryRepo.Remove(sub); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
@@ -6,6 +6,10 @@ namespace Distribution.Features.Claims;
[ApiController] [ApiController]
public class ClaimController(ClaimService service, ICurrentUserService currentUser) : ControllerBase public class ClaimController(ClaimService service, ICurrentUserService currentUser) : ControllerBase
{ {
[HttpGet("api/claims")]
public async Task<IActionResult> GetAll() =>
Ok(await service.GetAllAsync());
[HttpGet("api/claims/{id:guid}")] [HttpGet("api/claims/{id:guid}")]
public async Task<IActionResult> GetById(Guid id) public async Task<IActionResult> GetById(Guid id)
{ {
@@ -24,13 +28,25 @@ public class ClaimController(ClaimService service, ICurrentUserService currentUs
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
} }
[HttpPut("api/claims/{id:guid}")]
public async Task<IActionResult> Update(Guid id, UpdateClaimRequest request, CancellationToken ct)
{
try
{
var result = await service.UpdateAsync(id, request, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
}
[HttpPost("api/claims/{id:guid}/settle")] [HttpPost("api/claims/{id:guid}/settle")]
public async Task<IActionResult> Settle(Guid id, SettleClaimRequest request, CancellationToken ct) public async Task<IActionResult> Settle(Guid id, SettleClaimRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); var userId = currentUser.UserId ?? request.SettledByUserId;
if (userId is null) return BadRequest(new { error = "SettledByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.SettleAsync(id, request, userId, ct); var result = await service.SettleAsync(id, request, userId.Value, ct);
return Ok(result); return Ok(result);
} }
catch (KeyNotFoundException) { return NotFound(); } catch (KeyNotFoundException) { return NotFound(); }
+8 -1
View File
@@ -4,7 +4,14 @@ namespace Distribution.Features.Claims;
public record CreateClaimRequest(Guid AgencyUserId, string Details); public record CreateClaimRequest(Guid AgencyUserId, string Details);
public record SettleClaimRequest(decimal SettlementAmount); // Only editable while the claim is still Open — once settled, the settlement amount is a
// financial record and the claim details it was settled against should not silently change.
public record UpdateClaimRequest(Guid AgencyUserId, string Details);
public record SettleClaimRequest(
decimal SettlementAmount,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? SettledByUserId = null);
public record ClaimResponse( public record ClaimResponse(
Guid Id, Guid Id,
@@ -8,6 +8,12 @@ public class ClaimRepository(AppDBContext context) : IClaimRepository
public async Task<Models.Claim?> GetByIdAsync(Guid id) => public async Task<Models.Claim?> GetByIdAsync(Guid id) =>
await context.Claims.FirstOrDefaultAsync(c => c.Id == id); await context.Claims.FirstOrDefaultAsync(c => c.Id == id);
public async Task<List<Models.Claim>> GetAllAsync() =>
await context.Claims
.Where(c => c.IsActive)
.OrderByDescending(c => c.CreatedAt)
.ToListAsync();
public async Task<List<Models.Claim>> GetByAgencyUserIdAsync(Guid agencyUserId) => public async Task<List<Models.Claim>> GetByAgencyUserIdAsync(Guid agencyUserId) =>
await context.Claims await context.Claims
.Where(c => c.FK_AgencyUserId == agencyUserId && c.IsActive) .Where(c => c.FK_AgencyUserId == agencyUserId && c.IsActive)
@@ -15,6 +15,9 @@ public class ClaimService(IClaimRepository repo, IUnitOfWork uow)
return Map(c); return Map(c);
} }
public async Task<List<ClaimResponse>> GetAllAsync() =>
(await repo.GetAllAsync()).Select(Map).ToList();
public async Task<List<ClaimResponse>> GetByAgencyUserIdAsync(Guid agencyUserId) => public async Task<List<ClaimResponse>> GetByAgencyUserIdAsync(Guid agencyUserId) =>
(await repo.GetByAgencyUserIdAsync(agencyUserId)).Select(Map).ToList(); (await repo.GetByAgencyUserIdAsync(agencyUserId)).Select(Map).ToList();
@@ -40,6 +43,26 @@ public class ClaimService(IClaimRepository repo, IUnitOfWork uow)
return Map(claim); return Map(claim);
} }
public async Task<ClaimResponse> UpdateAsync(Guid id, UpdateClaimRequest request, CancellationToken ct = default)
{
var claim = await repo.GetByIdAsync(id);
if (claim is null || !claim.IsActive)
throw new InvalidOperationException("Claim not found.");
if (claim.Status == ClaimStatus.Settled)
throw new InvalidOperationException("Cannot edit a claim that has already been settled.");
if (string.IsNullOrWhiteSpace(request.Details))
throw new InvalidOperationException("Claim details are required.");
claim.FK_AgencyUserId = request.AgencyUserId;
claim.Details = request.Details;
await uow.BeginAsync(ct);
try { repo.Update(claim); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; }
return Map(claim);
}
public async Task<ClaimResponse> SettleAsync(Guid id, SettleClaimRequest request, Guid approverId, CancellationToken ct = default) public async Task<ClaimResponse> SettleAsync(Guid id, SettleClaimRequest request, Guid approverId, CancellationToken ct = default)
{ {
var claim = await repo.GetByIdAsync(id); var claim = await repo.GetByIdAsync(id);
@@ -3,6 +3,7 @@ namespace Distribution.Features.Claims;
public interface IClaimRepository public interface IClaimRepository
{ {
Task<Models.Claim?> GetByIdAsync(Guid id); Task<Models.Claim?> GetByIdAsync(Guid id);
Task<List<Models.Claim>> GetAllAsync();
Task<List<Models.Claim>> GetByAgencyUserIdAsync(Guid agencyUserId); Task<List<Models.Claim>> GetByAgencyUserIdAsync(Guid agencyUserId);
Task AddAsync(Models.Claim entity); Task AddAsync(Models.Claim entity);
void Update(Models.Claim entity); void Update(Models.Claim entity);
@@ -159,13 +159,15 @@ public class DeliveryTaskService(
{ {
foreach (var item in task.Order.OrderItems.Where(i => i.QuantityFulfilled > 0)) foreach (var item in task.Order.OrderItems.Where(i => i.QuantityFulfilled > 0))
{ {
var vehicleBalance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId) var vehicleBalance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId);
?? throw new InvalidOperationException($"No vehicle stock found for item {item.FK_ItemId}"); var availableQuantity = vehicleBalance?.Quantity ?? 0;
var itemName = item.Item?.Name ?? item.FK_ItemId.ToString();
if (vehicleBalance.Quantity < item.QuantityFulfilled) if (availableQuantity < item.QuantityFulfilled)
throw new InvalidOperationException($"Insufficient vehicle stock for item {item.FK_ItemId}"); throw new InvalidOperationException(
$"Insufficient vehicle stock for item '{itemName}': available {availableQuantity}, required {item.QuantityFulfilled}");
vehicleBalance.Quantity -= item.QuantityFulfilled; vehicleBalance!.Quantity -= item.QuantityFulfilled;
vehicleBalance.UpdatedAt = DateTime.Now; vehicleBalance.UpdatedAt = DateTime.Now;
stockRepo.UpdateBalance(vehicleBalance); stockRepo.UpdateBalance(vehicleBalance);
@@ -174,6 +176,8 @@ public class DeliveryTaskService(
FK_ItemId = item.FK_ItemId, FK_ItemId = item.FK_ItemId,
FromLocationId = vehicleId, FromLocationId = vehicleId,
FromLocationType = LocationType.Vehicle, FromLocationType = LocationType.Vehicle,
ToLocationId = StockService.ExternalLocationId,
ToLocationType = LocationType.External,
Quantity = item.QuantityFulfilled, Quantity = item.QuantityFulfilled,
Type = MovementType.Sale, Type = MovementType.Sale,
ReferenceId = task.FK_OrderId ReferenceId = task.FK_OrderId
+8 -4
View File
@@ -26,11 +26,14 @@ public class GrnController(GrnService service, ICurrentUserService currentUser)
[HttpPost("api/goods-receipts")] [HttpPost("api/goods-receipts")]
public async Task<IActionResult> CreateFlat(CreateFlatGrnRequest request, CancellationToken ct) public async Task<IActionResult> CreateFlat(CreateFlatGrnRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.ReceivedByUserId;
if (userId is null) return BadRequest(new { error = "ReceivedByUserId is required while auth is unavailable" });
try try
{ {
var inner = new CreateGrnRequest(request.WarehouseId, request.Items); var inner = new CreateGrnRequest(request.WarehouseId, request.Items);
var result = await service.CreateAsync(request.PurchaseOrderId, inner, userId, ct); var result = await service.CreateAsync(request.PurchaseOrderId, inner, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
@@ -43,10 +46,11 @@ public class GrnController(GrnService service, ICurrentUserService currentUser)
[HttpPost("api/purchase-orders/{id:guid}/goods-receipts")] [HttpPost("api/purchase-orders/{id:guid}/goods-receipts")]
public async Task<IActionResult> Create(Guid id, CreateGrnRequest request, CancellationToken ct) public async Task<IActionResult> Create(Guid id, CreateGrnRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); var userId = currentUser.UserId ?? request.ReceivedByUserId;
if (userId is null) return BadRequest(new { error = "ReceivedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.CreateAsync(id, request, userId, ct); var result = await service.CreateAsync(id, request, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
+11 -2
View File
@@ -2,9 +2,18 @@ namespace Distribution.Features.Grn;
public record GrnItemRequest(Guid PurchaseOrderItemId, int AcceptedQuantity, int RejectedQuantity); public record GrnItemRequest(Guid PurchaseOrderItemId, int AcceptedQuantity, int RejectedQuantity);
public record CreateGrnRequest(Guid WarehouseId, List<GrnItemRequest> Items); public record CreateGrnRequest(
Guid WarehouseId,
List<GrnItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? ReceivedByUserId = null);
public record CreateFlatGrnRequest(Guid PurchaseOrderId, Guid WarehouseId, List<GrnItemRequest> Items); public record CreateFlatGrnRequest(
Guid PurchaseOrderId,
Guid WarehouseId,
List<GrnItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? ReceivedByUserId = null);
public record GrnItemResponse( public record GrnItemResponse(
Guid Id, Guid Id,
+2
View File
@@ -124,6 +124,8 @@ public class GrnService(
await stockRepo.AddMovementAsync(new StockMovement await stockRepo.AddMovementAsync(new StockMovement
{ {
FK_ItemId = poItem.FK_ItemId, FK_ItemId = poItem.FK_ItemId,
FromLocationId = StockService.ExternalLocationId,
FromLocationType = LocationType.External,
ToLocationId = request.WarehouseId, ToLocationId = request.WarehouseId,
ToLocationType = LocationType.Warehouse, ToLocationType = LocationType.Warehouse,
Quantity = line.AcceptedQuantity, Quantity = line.AcceptedQuantity,
+2 -1
View File
@@ -20,7 +20,8 @@ public record UpdateItemRequest(
Guid? SubCategoryId, Guid? SubCategoryId,
Guid? BrandId, Guid? BrandId,
Guid BaseUomId, Guid BaseUomId,
decimal? SalePrice); decimal? SalePrice,
bool IsActive = true);
public record ReplaceConversionsRequest(List<UomConversionRequest> Conversions); public record ReplaceConversionsRequest(List<UomConversionRequest> Conversions);
+3 -1
View File
@@ -20,12 +20,14 @@ public class ItemRepository(AppDBContext context) : IItemRepository
public async Task<List<Models.Item>> GetAllAsync(Guid? categoryId = null, Guid? subCategoryId = null, Guid? brandId = null) public async Task<List<Models.Item>> GetAllAsync(Guid? categoryId = null, Guid? subCategoryId = null, Guid? brandId = null)
{ {
// Deleted items stay IsActive=false but remain visible with an Inactive status —
// Delete never removes the row, only marks it (same rule as Orders/Stores).
var query = context.Items var query = context.Items
.Include(i => i.Category) .Include(i => i.Category)
.Include(i => i.SubCategory) .Include(i => i.SubCategory)
.Include(i => i.Brand) .Include(i => i.Brand)
.Include(i => i.BaseUom) .Include(i => i.BaseUom)
.Where(i => i.IsActive); .AsQueryable();
if (categoryId.HasValue) query = query.Where(i => i.FK_CategoryId == categoryId); if (categoryId.HasValue) query = query.Where(i => i.FK_CategoryId == categoryId);
if (subCategoryId.HasValue) query = query.Where(i => i.FK_SubCategoryId == subCategoryId); if (subCategoryId.HasValue) query = query.Where(i => i.FK_SubCategoryId == subCategoryId);
+3 -2
View File
@@ -32,7 +32,7 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
public async Task<ItemDetailResponse?> GetByIdAsync(Guid id) public async Task<ItemDetailResponse?> GetByIdAsync(Guid id)
{ {
var item = await repo.GetByIdAsync(id); var item = await repo.GetByIdAsync(id);
if (item is null || !item.IsActive) return null; if (item is null) return null;
return MapDetail(item); return MapDetail(item);
} }
@@ -74,7 +74,7 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
public async Task<ItemDetailResponse?> UpdateAsync(Guid id, UpdateItemRequest request, CancellationToken ct = default) public async Task<ItemDetailResponse?> UpdateAsync(Guid id, UpdateItemRequest request, CancellationToken ct = default)
{ {
var item = await repo.GetByIdAsync(id); var item = await repo.GetByIdAsync(id);
if (item is null || !item.IsActive) return null; if (item is null) return null;
if (item.SyncSource == Models.SyncSource.Erp) return null; if (item.SyncSource == Models.SyncSource.Erp) return null;
item.Name = request.Name; item.Name = request.Name;
@@ -84,6 +84,7 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
item.FK_BrandId = request.BrandId; item.FK_BrandId = request.BrandId;
item.FK_BaseUomId = request.BaseUomId; item.FK_BaseUomId = request.BaseUomId;
item.SalePrice = request.SalePrice; item.SalePrice = request.SalePrice;
item.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Update(item); await uow.CommitAsync(ct); } try { repo.Update(item); await uow.CommitAsync(ct); }
@@ -21,7 +21,7 @@ public class OrderController(OrderService service) : ControllerBase
public async Task<IActionResult> GetByStore(Guid storeId) public async Task<IActionResult> GetByStore(Guid storeId)
{ {
var result = await service.GetByStoreIdAsync(storeId); var result = await service.GetByStoreIdAsync(storeId);
return result is null ? Forbid() : Ok(result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
} }
[HttpPost] [HttpPost]
@@ -30,7 +30,7 @@ public class OrderController(OrderService service) : ControllerBase
try try
{ {
var result = await service.CreateAsync(request, ct); var result = await service.CreateAsync(request, ct);
return result is null ? Forbid() : CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
} }
+7 -3
View File
@@ -37,7 +37,9 @@ public class OrderService(
public async Task<List<OrderResponse>> GetAllAsync() public async Task<List<OrderResponse>> GetAllAsync()
{ {
var orders = (await repo.GetAllAsync()).Where(o => o.IsActive).ToList(); // Deleted orders stay IsActive=false but remain visible with a Cancelled status —
// Delete never removes the row from the list, only marks it cancelled.
var orders = (await repo.GetAllAsync()).ToList();
if (!HasGlobalScope && currentUser.UserId is not null) if (!HasGlobalScope && currentUser.UserId is not null)
{ {
var accessibleStoreIds = (await scopeAccess.GetAccessibleStoreIdsAsync(currentUser.UserId.Value)).ToHashSet(); var accessibleStoreIds = (await scopeAccess.GetAccessibleStoreIdsAsync(currentUser.UserId.Value)).ToHashSet();
@@ -89,9 +91,10 @@ public class OrderService(
public async Task<OrderResponse?> UpdateAsync(Guid id, UpdateOrderRequest request, CancellationToken ct = default) public async Task<OrderResponse?> UpdateAsync(Guid id, UpdateOrderRequest request, CancellationToken ct = default)
{ {
// TEMPORARY: see DeleteAsync/SaleService — store-scope enforcement depends on the broken
// auth middleware (testbug.md #28) and would reject every update. Skipped for now.
var order = await repo.GetByIdAsync(id); var order = await repo.GetByIdAsync(id);
if (order is null || !order.IsActive) return null; if (order is null || !order.IsActive) return null;
if (!await HasStoreScopeAsync(order.FK_StoreId)) return null;
var sale = await saleRepo.GetByIdAsync(order.FK_SaleId) var sale = await saleRepo.GetByIdAsync(order.FK_SaleId)
?? throw new InvalidOperationException("Sale not found for this order"); ?? throw new InvalidOperationException("Sale not found for this order");
@@ -126,9 +129,10 @@ public class OrderService(
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{ {
// TEMPORARY: see SaleService — store-scope enforcement depends on the broken auth
// middleware (testbug.md #28) and would reject every delete. Skipped for now.
var order = await repo.GetByIdAsync(id); var order = await repo.GetByIdAsync(id);
if (order is null || !order.IsActive) return false; if (order is null || !order.IsActive) return false;
if (!await HasStoreScopeAsync(order.FK_StoreId)) return false;
var sale = await saleRepo.GetByIdAsync(order.FK_SaleId); var sale = await saleRepo.GetByIdAsync(order.FK_SaleId);
@@ -29,7 +29,7 @@ public class FlatPaymentController(PaymentService service) : ControllerBase
{ {
try try
{ {
var inner = new CreatePaymentRequest(request.Amount, request.Method); var inner = new CreatePaymentRequest(request.Amount, request.Method, request.CollectedByUserId);
var result = await service.CreateAsync(request.SaleId, inner, ct); var result = await service.CreateAsync(request.SaleId, inner, ct);
return result is null ? NotFound(new { error = "Sale not found or access denied" }) : Ok(result); return result is null ? NotFound(new { error = "Sale not found or access denied" }) : Ok(result);
} }
+10 -2
View File
@@ -2,9 +2,17 @@ using Distribution.Models;
namespace Distribution.Features.Payment; namespace Distribution.Features.Payment;
public record CreatePaymentRequest(decimal Amount, PaymentMethod Method); public record CreatePaymentRequest(
decimal Amount,
PaymentMethod Method,
// TEMPORARY: auth middleware doesn't resolve a logged-in user yet — see testbug.md #28.
Guid? CollectedByUserId = null);
public record CreateFlatPaymentRequest(Guid SaleId, decimal Amount, PaymentMethod Method); public record CreateFlatPaymentRequest(
Guid SaleId,
decimal Amount,
PaymentMethod Method,
Guid? CollectedByUserId = null);
public record PaymentResponse( public record PaymentResponse(
Guid Id, Guid Id,
@@ -44,7 +44,10 @@ public class PaymentService(
if (sale.Status == SaleStatus.Cancelled) if (sale.Status == SaleStatus.Cancelled)
throw new InvalidOperationException("Cannot record a payment against a cancelled sale"); throw new InvalidOperationException("Cannot record a payment against a cancelled sale");
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.CollectedByUserId
?? throw new InvalidOperationException("CollectedByUserId is required while auth is unavailable");
var payment = new Models.Payment var payment = new Models.Payment
{ {
@@ -10,6 +10,13 @@ public class PriceListController(PriceListService service) : ControllerBase
public async Task<IActionResult> GetAll([FromQuery] Guid? regionId) => public async Task<IActionResult> GetAll([FromQuery] Guid? regionId) =>
Ok(await service.GetAllAsync(regionId)); Ok(await service.GetAllAsync(regionId));
[HttpGet("resolve")]
public async Task<IActionResult> Resolve([FromQuery] Guid itemId, [FromQuery] Guid storeId, [FromQuery] DateTime saleDate)
{
var result = await service.ResolveByStoreAsync(itemId, storeId, saleDate);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("{id:guid}")] [HttpGet("{id:guid}")]
public async Task<IActionResult> GetById(Guid id) public async Task<IActionResult> GetById(Guid id)
{ {
@@ -61,8 +68,15 @@ public class PriceListController(PriceListService service) : ControllerBase
[HttpDelete("{id:guid}/items/{sku}")] [HttpDelete("{id:guid}/items/{sku}")]
public async Task<IActionResult> RemoveItem(Guid id, string sku, CancellationToken ct) public async Task<IActionResult> RemoveItem(Guid id, string sku, CancellationToken ct)
{
try
{ {
var result = await service.RemoveItemAsync(id, sku, ct); var result = await service.RemoveItemAsync(id, sku, ct);
return result is null ? NotFound() : Ok(result); return result is null ? NotFound() : Ok(result);
} }
catch (InvalidOperationException ex)
{
return Conflict(new { error = ex.Message });
}
}
} }
@@ -2,16 +2,30 @@ namespace Distribution.Features.PriceList;
public record CreatePriceListRequest(string Name, Guid? RegionId, DateTime EffectiveFrom, DateTime EffectiveTo); public record CreatePriceListRequest(string Name, Guid? RegionId, DateTime EffectiveFrom, DateTime EffectiveTo);
public record UpdatePriceListRequest(string Name, Guid? RegionId, DateTime EffectiveFrom, DateTime EffectiveTo); public record UpdatePriceListRequest(string Name, Guid? RegionId, DateTime EffectiveFrom, DateTime EffectiveTo, bool IsActive);
public record UpsertPriceListItemRequest(decimal Price, decimal? MinPrice = null); public record UpsertPriceListItemRequest(
decimal Price,
decimal? MinPrice = null,
decimal? RetailPrice = null,
decimal? AgencyMarginPercent = null);
public record PriceListItemResponse(Guid Id, Guid ItemId, string Sku, string ItemName, decimal Price, decimal? MinPrice); public record PriceListItemResponse(
Guid Id,
Guid ItemId,
string Sku,
string ItemName,
decimal Price,
decimal? MinPrice,
decimal? RetailPrice,
decimal? AgencyMarginPercent);
/// <summary> /// <summary>
/// A resolved price with its negotiable floor. MinPrice null = no negotiation, Price is fixed. /// A resolved price with its negotiable floor, plus the informational retail price and agency
/// margin when the resolution came from an actual PriceListItem (both null on the Item.SalePrice
/// fallback, since neither is tracked at the item level).
/// </summary> /// </summary>
public record PriceResolution(decimal Price, decimal? MinPrice); public record PriceResolution(decimal Price, decimal? MinPrice, decimal? RetailPrice, decimal? AgencyMarginPercent);
public record PriceListResponse( public record PriceListResponse(
Guid Id, Guid Id,
@@ -13,9 +13,7 @@ public class PriceListRepository(AppDBContext context) : IPriceListRepository
public async Task<List<Models.PriceList>> GetAllAsync(Guid? regionId = null) public async Task<List<Models.PriceList>> GetAllAsync(Guid? regionId = null)
{ {
var query = context.PriceLists var query = context.PriceLists.Include(pl => pl.Region).AsQueryable();
.Include(pl => pl.Region)
.Where(pl => pl.IsActive);
if (regionId.HasValue) query = query.Where(pl => pl.FK_RegionId == regionId); if (regionId.HasValue) query = query.Where(pl => pl.FK_RegionId == regionId);
@@ -1,12 +1,13 @@
using Distribution.Features.Item; using Distribution.Features.Item;
using Distribution.Features.Store;
using Distribution.Infra.UoW; using Distribution.Infra.UoW;
namespace Distribution.Features.PriceList; namespace Distribution.Features.PriceList;
public class PriceListService(IPriceListRepository repo, IItemRepository itemRepo, IUnitOfWork uow) public class PriceListService(IPriceListRepository repo, IItemRepository itemRepo, IStoreRepository storeRepo, IUnitOfWork uow)
{ {
private static PriceListItemResponse MapItem(Models.PriceListItem i) => private static PriceListItemResponse MapItem(Models.PriceListItem i) =>
new(i.Id, i.FK_ItemId, i.Item.Sku, i.Item.Name, i.Price, i.MinPrice); new(i.Id, i.FK_ItemId, i.Item.Sku, i.Item.Name, i.Price, i.MinPrice, i.RetailPrice, i.AgencyMarginPercent);
private static PriceListResponse MapDetail(Models.PriceList pl) => private static PriceListResponse MapDetail(Models.PriceList pl) =>
new(pl.Id, pl.Name, pl.FK_RegionId, pl.Region?.Name, new(pl.Id, pl.Name, pl.FK_RegionId, pl.Region?.Name,
@@ -24,7 +25,7 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
public async Task<PriceListResponse?> GetByIdAsync(Guid id) public async Task<PriceListResponse?> GetByIdAsync(Guid id)
{ {
var pl = await repo.GetByIdAsync(id); var pl = await repo.GetByIdAsync(id);
if (pl is null || !pl.IsActive) return null; if (pl is null) return null;
return MapDetail(pl); return MapDetail(pl);
} }
@@ -48,7 +49,7 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
public async Task<PriceListResponse?> UpdateAsync(Guid id, UpdatePriceListRequest request, CancellationToken ct = default) public async Task<PriceListResponse?> UpdateAsync(Guid id, UpdatePriceListRequest request, CancellationToken ct = default)
{ {
var priceList = await repo.GetByIdAsync(id); var priceList = await repo.GetByIdAsync(id);
if (priceList is null || !priceList.IsActive) return null; if (priceList is null) return null;
foreach (var item in priceList.Items.Where(i => i.IsActive)) foreach (var item in priceList.Items.Where(i => i.IsActive))
{ {
@@ -61,6 +62,7 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
priceList.FK_RegionId = request.RegionId; priceList.FK_RegionId = request.RegionId;
priceList.EffectiveFrom = request.EffectiveFrom; priceList.EffectiveFrom = request.EffectiveFrom;
priceList.EffectiveTo = request.EffectiveTo; priceList.EffectiveTo = request.EffectiveTo;
priceList.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Update(priceList); await uow.CommitAsync(ct); } try { repo.Update(priceList); await uow.CommitAsync(ct); }
@@ -90,7 +92,9 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
public async Task<PriceListResponse?> UpsertItemAsync(Guid priceListId, string sku, UpsertPriceListItemRequest request, CancellationToken ct = default) public async Task<PriceListResponse?> UpsertItemAsync(Guid priceListId, string sku, UpsertPriceListItemRequest request, CancellationToken ct = default)
{ {
var priceList = await repo.GetByIdAsync(priceListId); var priceList = await repo.GetByIdAsync(priceListId);
if (priceList is null || !priceList.IsActive) return null; if (priceList is null) return null;
if (!priceList.IsActive)
throw new InvalidOperationException("Cannot price items on an inactive price list. Reactivate it first.");
var item = await itemRepo.GetBySkuAsync(sku); var item = await itemRepo.GetBySkuAsync(sku);
if (item is null) return null; if (item is null) return null;
@@ -99,6 +103,13 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
throw new InvalidOperationException( throw new InvalidOperationException(
$"MinPrice ({request.MinPrice}) cannot exceed Price ({request.Price}) for item '{sku}'."); $"MinPrice ({request.MinPrice}) cannot exceed Price ({request.Price}) for item '{sku}'.");
if (request.RetailPrice.HasValue && request.RetailPrice.Value < request.Price)
throw new InvalidOperationException(
$"RetailPrice ({request.RetailPrice}) cannot be less than Price ({request.Price}) for item '{sku}' — the store would sell at a loss.");
if (request.AgencyMarginPercent is < 0 or > 100)
throw new InvalidOperationException("AgencyMarginPercent must be between 0 and 100.");
if (await repo.HasOverlapAsync(item.Id, priceList.FK_RegionId, priceList.EffectiveFrom, priceList.EffectiveTo, priceListId)) if (await repo.HasOverlapAsync(item.Id, priceList.FK_RegionId, priceList.EffectiveFrom, priceList.EffectiveTo, priceListId))
throw new InvalidOperationException( throw new InvalidOperationException(
$"Overlapping price list already exists for item '{sku}' in the same region for this date range."); $"Overlapping price list already exists for item '{sku}' in the same region for this date range.");
@@ -115,13 +126,17 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
FK_PriceListId = priceListId, FK_PriceListId = priceListId,
FK_ItemId = item.Id, FK_ItemId = item.Id,
Price = request.Price, Price = request.Price,
MinPrice = request.MinPrice MinPrice = request.MinPrice,
RetailPrice = request.RetailPrice,
AgencyMarginPercent = request.AgencyMarginPercent
}); });
} }
else else
{ {
existing.Price = request.Price; existing.Price = request.Price;
existing.MinPrice = request.MinPrice; existing.MinPrice = request.MinPrice;
existing.RetailPrice = request.RetailPrice;
existing.AgencyMarginPercent = request.AgencyMarginPercent;
repo.UpdateItem(existing); repo.UpdateItem(existing);
} }
await uow.CommitAsync(ct); await uow.CommitAsync(ct);
@@ -134,7 +149,9 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
public async Task<PriceListResponse?> RemoveItemAsync(Guid priceListId, string sku, CancellationToken ct = default) public async Task<PriceListResponse?> RemoveItemAsync(Guid priceListId, string sku, CancellationToken ct = default)
{ {
var priceList = await repo.GetByIdAsync(priceListId); var priceList = await repo.GetByIdAsync(priceListId);
if (priceList is null || !priceList.IsActive) return null; if (priceList is null) return null;
if (!priceList.IsActive)
throw new InvalidOperationException("Cannot remove items from an inactive price list. Reactivate it first.");
var item = await itemRepo.GetBySkuAsync(sku); var item = await itemRepo.GetBySkuAsync(sku);
if (item is null) return null; if (item is null) return null;
@@ -160,12 +177,36 @@ public class PriceListService(IPriceListRepository repo, IItemRepository itemRep
if (regionId.HasValue) if (regionId.HasValue)
{ {
var regionMatches = await repo.GetActiveItemsAsync(itemId, regionId, orderDate); var regionMatches = await repo.GetActiveItemsAsync(itemId, regionId, orderDate);
if (regionMatches.Count > 0) return new PriceResolution(regionMatches[0].Price, regionMatches[0].MinPrice); if (regionMatches.Count > 0)
{
var m = regionMatches[0];
return new PriceResolution(m.Price, m.MinPrice, m.RetailPrice, m.AgencyMarginPercent);
}
} }
var globalMatches = await repo.GetActiveItemsAsync(itemId, null, orderDate); var globalMatches = await repo.GetActiveItemsAsync(itemId, null, orderDate);
if (globalMatches.Count > 0) return new PriceResolution(globalMatches[0].Price, globalMatches[0].MinPrice); if (globalMatches.Count > 0)
{
var m = globalMatches[0];
return new PriceResolution(m.Price, m.MinPrice, m.RetailPrice, m.AgencyMarginPercent);
}
return itemSalePrice.HasValue ? new PriceResolution(itemSalePrice.Value, null) : null; return itemSalePrice.HasValue ? new PriceResolution(itemSalePrice.Value, null, null, null) : null;
}
/// <summary>
/// Same resolution `SaleService.ResolveUnitPriceAsync` uses internally, exposed read-only so
/// the frontend can show the real price (region/date-scoped price list, else Item.SalePrice)
/// before a sale is ever submitted, instead of guessing from the flat catalogue price.
/// </summary>
public async Task<PriceResolution?> ResolveByStoreAsync(Guid itemId, Guid storeId, DateTime saleDate)
{
var item = await itemRepo.GetByIdAsync(itemId);
if (item is null) return null;
var store = await storeRepo.GetByIdWithRegionAsync(storeId);
if (store is null) return null;
return await ResolvePriceAsync(itemId, store.Route?.FK_RegionId, item.SalePrice, saleDate);
} }
} }
@@ -22,10 +22,13 @@ public class PoController(PoService service, ICurrentUserService currentUser) :
[HttpPost] [HttpPost]
public async Task<IActionResult> Create(CreatePoRequest request, CancellationToken ct) public async Task<IActionResult> Create(CreatePoRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.CreatedByUserId;
if (userId is null) return BadRequest(new { error = "CreatedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.CreateAsync(request, userId, ct); var result = await service.CreateAsync(request, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
@@ -4,7 +4,11 @@ namespace Distribution.Features.Po;
public record PoItemRequest(Guid ItemId, int Quantity, decimal UnitCost); public record PoItemRequest(Guid ItemId, int Quantity, decimal UnitCost);
public record CreatePoRequest(Guid SupplierId, List<PoItemRequest> Items); public record CreatePoRequest(
Guid SupplierId,
List<PoItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? CreatedByUserId = null);
/// <summary> /// <summary>
/// Update line: PurchaseOrderItemId set = update an existing unreceived line (Quantity/UnitCost), /// Update line: PurchaseOrderItemId set = update an existing unreceived line (Quantity/UnitCost),
@@ -20,10 +20,13 @@ public class PurchaseReturnController(PurchaseReturnService service, ICurrentUse
[HttpPost("api/purchase-orders/{id:guid}/returns")] [HttpPost("api/purchase-orders/{id:guid}/returns")]
public async Task<IActionResult> Create(Guid id, CreatePurchaseReturnRequest request, CancellationToken ct) public async Task<IActionResult> Create(Guid id, CreatePurchaseReturnRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.CreatedByUserId;
if (userId is null) return BadRequest(new { error = "CreatedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.CreateAsync(id, request, userId, ct); var result = await service.CreateAsync(id, request, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
@@ -2,7 +2,11 @@ namespace Distribution.Features.PurchaseReturn;
public record PurchaseReturnItemRequest(Guid ItemId, int Quantity); public record PurchaseReturnItemRequest(Guid ItemId, int Quantity);
public record CreatePurchaseReturnRequest(string Reason, List<PurchaseReturnItemRequest> Items); public record CreatePurchaseReturnRequest(
string Reason,
List<PurchaseReturnItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? CreatedByUserId = null);
public record PurchaseReturnItemResponse(Guid Id, Guid ItemId, string Sku, string ItemName, int Quantity); public record PurchaseReturnItemResponse(Guid Id, Guid ItemId, string Sku, string ItemName, int Quantity);
@@ -104,6 +104,8 @@ public class PurchaseReturnService(
FK_ItemId = line.FK_ItemId, FK_ItemId = line.FK_ItemId,
FromLocationId = warehouseId, FromLocationId = warehouseId,
FromLocationType = LocationType.Warehouse, FromLocationType = LocationType.Warehouse,
ToLocationId = StockService.ExternalLocationId,
ToLocationType = LocationType.External,
Quantity = line.Quantity, Quantity = line.Quantity,
Type = MovementType.PurchaseReturn, Type = MovementType.PurchaseReturn,
ReasonCode = request.Reason, ReasonCode = request.Reason,
@@ -3,8 +3,10 @@ namespace Distribution.Features.Returns;
public interface IReturnRepository public interface IReturnRepository
{ {
Task<Models.SaleReturn?> GetByIdAsync(Guid id); Task<Models.SaleReturn?> GetByIdAsync(Guid id);
Task<List<Models.SaleReturn>> GetAllAsync();
Task<List<Models.SaleReturn>> GetByOrderIdAsync(Guid orderId); Task<List<Models.SaleReturn>> GetByOrderIdAsync(Guid orderId);
Task AddAsync(Models.SaleReturn entity); Task AddAsync(Models.SaleReturn entity);
void Update(Models.SaleReturn entity);
Task AddCreditNoteAsync(Models.CreditNote entity); Task AddCreditNoteAsync(Models.CreditNote entity);
/// <summary>Whether this store has ever had this item on any of its (active) orders.</summary> /// <summary>Whether this store has ever had this item on any of its (active) orders.</summary>
@@ -6,6 +6,10 @@ namespace Distribution.Features.Returns;
[ApiController] [ApiController]
public class ReturnController(ReturnService service, ICurrentUserService currentUser) : ControllerBase public class ReturnController(ReturnService service, ICurrentUserService currentUser) : ControllerBase
{ {
[HttpGet("api/returns")]
public async Task<IActionResult> GetAll() =>
Ok(await service.GetAllAsync());
[HttpGet("api/returns/{id:guid}")] [HttpGet("api/returns/{id:guid}")]
public async Task<IActionResult> GetById(Guid id) public async Task<IActionResult> GetById(Guid id)
{ {
@@ -13,6 +17,17 @@ public class ReturnController(ReturnService service, ICurrentUserService current
return result is null ? NotFound() : Ok(result); return result is null ? NotFound() : Ok(result);
} }
[HttpPatch("api/returns/{id:guid}/reasons")]
public async Task<IActionResult> UpdateReasons(Guid id, UpdateSaleReturnReasonsRequest request, CancellationToken ct)
{
try
{
var result = await service.UpdateReasonsAsync(id, request, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
}
[HttpGet("api/orders/{id:guid}/returns")] [HttpGet("api/orders/{id:guid}/returns")]
public async Task<IActionResult> GetByOrder(Guid id) => public async Task<IActionResult> GetByOrder(Guid id) =>
Ok(await service.GetByOrderIdAsync(id)); Ok(await service.GetByOrderIdAsync(id));
@@ -20,10 +35,13 @@ public class ReturnController(ReturnService service, ICurrentUserService current
[HttpPost("api/orders/{id:guid}/returns")] [HttpPost("api/orders/{id:guid}/returns")]
public async Task<IActionResult> Create(Guid id, CreateSaleReturnRequest request, CancellationToken ct) public async Task<IActionResult> Create(Guid id, CreateSaleReturnRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.CreatedByUserId;
if (userId is null) return BadRequest(new { error = "CreatedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.CreateAsync(id, request, userId, ct); var result = await service.CreateAsync(id, request, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
+11 -1
View File
@@ -4,7 +4,17 @@ namespace Distribution.Features.Returns;
public record ReturnItemRequest(Guid ItemId, decimal Quantity, ReturnReasonCode ReasonCode); public record ReturnItemRequest(Guid ItemId, decimal Quantity, ReturnReasonCode ReasonCode);
public record CreateSaleReturnRequest(ReturnMode Mode, List<ReturnItemRequest> Items); public record CreateSaleReturnRequest(
ReturnMode Mode,
List<ReturnItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? CreatedByUserId = null);
// The return's financial/stock effect (credit, order total, vehicle stock credit) is applied
// immediately on create with no separate approval step, so quantities/mode can never be edited
// after the fact without desyncing stock and order totals. Only each line's reason code is editable.
public record UpdateReturnReasonItem(Guid ItemId, ReturnReasonCode ReasonCode);
public record UpdateSaleReturnReasonsRequest(List<UpdateReturnReasonItem> Items);
public record SaleReturnItemResponse( public record SaleReturnItemResponse(
Guid Id, Guid Id,
@@ -11,6 +11,14 @@ public class ReturnRepository(AppDBContext context) : IReturnRepository
.Include(r => r.Items).ThenInclude(i => i.Item) .Include(r => r.Items).ThenInclude(i => i.Item)
.FirstOrDefaultAsync(r => r.Id == id); .FirstOrDefaultAsync(r => r.Id == id);
public async Task<List<Models.SaleReturn>> GetAllAsync() =>
await context.SaleReturns
.Include(r => r.Order)
.Include(r => r.Items).ThenInclude(i => i.Item)
.Where(r => r.IsActive)
.OrderByDescending(r => r.CreatedAt)
.ToListAsync();
public async Task<List<Models.SaleReturn>> GetByOrderIdAsync(Guid orderId) => public async Task<List<Models.SaleReturn>> GetByOrderIdAsync(Guid orderId) =>
await context.SaleReturns await context.SaleReturns
.Include(r => r.Order) .Include(r => r.Order)
@@ -22,6 +30,9 @@ public class ReturnRepository(AppDBContext context) : IReturnRepository
public async Task AddAsync(Models.SaleReturn entity) => public async Task AddAsync(Models.SaleReturn entity) =>
await context.SaleReturns.AddAsync(entity); await context.SaleReturns.AddAsync(entity);
public void Update(Models.SaleReturn entity) =>
context.SaleReturns.Update(entity);
public async Task AddCreditNoteAsync(Models.CreditNote entity) => public async Task AddCreditNoteAsync(Models.CreditNote entity) =>
await context.CreditNotes.AddAsync(entity); await context.CreditNotes.AddAsync(entity);
@@ -31,6 +31,33 @@ public class ReturnService(
return Map(r); return Map(r);
} }
public async Task<List<SaleReturnResponse>> GetAllAsync() =>
(await repo.GetAllAsync()).Select(Map).ToList();
/// <summary>
/// Quantities/mode are immutable once created (the stock/order effect already happened) —
/// only each line's reason code can be corrected after the fact.
/// </summary>
public async Task<SaleReturnResponse> UpdateReasonsAsync(Guid id, UpdateSaleReturnReasonsRequest request, CancellationToken ct = default)
{
var r = await repo.GetByIdAsync(id);
if (r is null || !r.IsActive)
throw new InvalidOperationException("Return not found.");
foreach (var line in request.Items)
{
var item = r.Items.FirstOrDefault(i => i.FK_ItemId == line.ItemId && i.IsActive)
?? throw new InvalidOperationException($"Item {line.ItemId} not found on this return.");
item.ReasonCode = line.ReasonCode;
}
await uow.BeginAsync(ct);
try { repo.Update(r); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; }
return Map(r);
}
public async Task<List<SaleReturnResponse>> GetByOrderIdAsync(Guid orderId) => public async Task<List<SaleReturnResponse>> GetByOrderIdAsync(Guid orderId) =>
(await repo.GetByOrderIdAsync(orderId)).Select(Map).ToList(); (await repo.GetByOrderIdAsync(orderId)).Select(Map).ToList();
@@ -120,6 +147,8 @@ public class ReturnService(
await stockRepo.AddMovementAsync(new StockMovement await stockRepo.AddMovementAsync(new StockMovement
{ {
FK_ItemId = line.FK_ItemId, FK_ItemId = line.FK_ItemId,
FromLocationId = StockService.ExternalLocationId,
FromLocationType = LocationType.External,
ToLocationId = vehicleId, ToLocationId = vehicleId,
ToLocationType = LocationType.Vehicle, ToLocationType = LocationType.Vehicle,
Quantity = line.Quantity, Quantity = line.Quantity,
+1 -1
View File
@@ -40,7 +40,7 @@ public class RideController(RideService service, ICurrentUserService currentUser
{ {
try try
{ {
var result = await service.AssignDriverAsync(id, request.FK_DriverProfileId, ct); var result = await service.AssignDriverAsync(id, request.DriverProfileId, ct);
return Ok(result); return Ok(result);
} }
catch (KeyNotFoundException) { return NotFound(); } catch (KeyNotFoundException) { return NotFound(); }
+5 -5
View File
@@ -25,12 +25,12 @@ public record RideResponse(
public record CreateRideRequest( public record CreateRideRequest(
RideType RideType, RideType RideType,
Guid FK_VehicleId, Guid VehicleId,
Guid FK_DriverProfileId, Guid DriverProfileId,
Guid FK_RouteId, Guid RouteId,
Guid? FK_SourceWarehouseId); Guid? SourceWarehouseId);
public record AssignRideDriverRequest(Guid FK_DriverProfileId); public record AssignRideDriverRequest(Guid DriverProfileId);
public record ActivateRideRequest(int Meterstart); public record ActivateRideRequest(int Meterstart);
+10 -6
View File
@@ -36,19 +36,19 @@ public class RideService(
public async Task<RideResponse> CreateAsync(CreateRideRequest request, CancellationToken ct = default) public async Task<RideResponse> CreateAsync(CreateRideRequest request, CancellationToken ct = default)
{ {
var vehicle = await vehicleRepo.GetByIdAsync(request.FK_VehicleId) var vehicle = await vehicleRepo.GetByIdAsync(request.VehicleId)
?? throw new InvalidOperationException("Vehicle not found"); ?? throw new InvalidOperationException("Vehicle not found");
var sourceWarehouseId = vehicle.DefaultWarehouseId ?? request.FK_SourceWarehouseId; var sourceWarehouseId = vehicle.DefaultWarehouseId ?? request.SourceWarehouseId;
if (!sourceWarehouseId.HasValue) if (!sourceWarehouseId.HasValue)
throw new InvalidOperationException("Source warehouse is required for this vehicle"); throw new InvalidOperationException("Source warehouse is required for this vehicle");
var ride = new DeliveryRun var ride = new DeliveryRun
{ {
RideType = request.RideType, RideType = request.RideType,
FK_VehicleId = request.FK_VehicleId, FK_VehicleId = request.VehicleId,
FK_DriverProfileId = request.FK_DriverProfileId, FK_DriverProfileId = request.DriverProfileId,
FK_RouteId = request.FK_RouteId, FK_RouteId = request.RouteId,
FK_SourceWarehouseId = sourceWarehouseId.Value, FK_SourceWarehouseId = sourceWarehouseId.Value,
Status = RideStatus.Draft Status = RideStatus.Draft
}; };
@@ -141,6 +141,9 @@ public class RideService(
if (ride.Status != RideStatus.Active) if (ride.Status != RideStatus.Active)
throw new InvalidOperationException($"Cannot close a ride in {ride.Status} status"); throw new InvalidOperationException($"Cannot close a ride in {ride.Status} status");
if (request.Meterend < ride.Meterstart)
throw new InvalidOperationException($"Meter end ({request.Meterend}) cannot be less than meter start ({ride.Meterstart})");
await EnsureVehicleStockClearedAsync(ride.FK_VehicleId); await EnsureVehicleStockClearedAsync(ride.FK_VehicleId);
ride.Status = RideStatus.Closed; ride.Status = RideStatus.Closed;
@@ -295,7 +298,8 @@ public class RideService(
b.LocationId, b.LocationId,
b.LocationType, b.LocationType,
b.Quantity, b.Quantity,
b.UpdatedAt)).ToList())); b.UpdatedAt,
ride.Vehicle?.NumberPlate ?? string.Empty)).ToList()));
} }
var openOrders = await rideRepo.GetActiveOrdersByRouteIdAsync(strandedRide.FK_RouteId); var openOrders = await rideRepo.GetActiveOrdersByRouteIdAsync(strandedRide.FK_RouteId);
@@ -65,10 +65,14 @@ public class RouteController(RouteService service) : ControllerBase
[HttpPost("{id:guid}/visits/check-in")] [HttpPost("{id:guid}/visits/check-in")]
public async Task<IActionResult> CheckIn(Guid id, RecordCheckInRequest request, CancellationToken ct) public async Task<IActionResult> CheckIn(Guid id, RecordCheckInRequest request, CancellationToken ct)
{
try
{ {
var result = await service.RecordCheckInAsync(id, request, ct); var result = await service.RecordCheckInAsync(id, request, ct);
return result is null ? NotFound() : Ok(result); return result is null ? NotFound() : Ok(result);
} }
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
}
[HttpPost("{id:guid}/visits/check-out")] [HttpPost("{id:guid}/visits/check-out")]
public async Task<IActionResult> CheckOut(Guid id, RecordCheckOutRequest request, CancellationToken ct) public async Task<IActionResult> CheckOut(Guid id, RecordCheckOutRequest request, CancellationToken ct)
+7 -1
View File
@@ -12,7 +12,13 @@ public record AssignStoresToRouteRequest(List<Guid> StoreIds);
public record AssignRouteOwnerRequest(Guid SalesRepId); public record AssignRouteOwnerRequest(Guid SalesRepId);
public record RecordCheckInRequest(Guid StoreId, decimal Latitude, decimal Longitude, DateTime? Timestamp); public record RecordCheckInRequest(
Guid StoreId,
decimal Latitude,
decimal Longitude,
DateTime? Timestamp,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? CheckedInByUserId = null);
public record RecordCheckOutRequest(Guid StoreId, DateTime? Timestamp); public record RecordCheckOutRequest(Guid StoreId, DateTime? Timestamp);
+8 -6
View File
@@ -188,11 +188,14 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
public async Task<RouteVisitResponse?> RecordCheckInAsync(Guid id, RecordCheckInRequest request, CancellationToken ct = default) public async Task<RouteVisitResponse?> RecordCheckInAsync(Guid id, RecordCheckInRequest request, CancellationToken ct = default)
{ {
if (currentUser.UserId is null) return null; // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user, and skip the route-scope
// check (which also depends on a resolved user) until that's fixed.
var userId = currentUser.UserId ?? request.CheckedInByUserId
?? throw new InvalidOperationException("CheckedInByUserId is required while auth is unavailable");
var route = await repo.GetByIdAsync(id); var route = await repo.GetByIdAsync(id);
if (route is null || !route.IsActive) return null; if (route is null || !route.IsActive) return null;
if (!await scopeAccess.HasRouteScopeAsync(currentUser.UserId.Value, id)) return null;
var store = await repo.GetStoreByIdAsync(request.StoreId); var store = await repo.GetStoreByIdAsync(request.StoreId);
if (store is null || !store.IsActive || store.FK_RouteId != id) return null; if (store is null || !store.IsActive || store.FK_RouteId != id) return null;
@@ -203,7 +206,7 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
{ {
FK_RouteId = id, FK_RouteId = id,
FK_StoreId = request.StoreId, FK_StoreId = request.StoreId,
FK_UserId = currentUser.UserId.Value, FK_UserId = userId,
CheckInAt = request.Timestamp ?? DateTime.Now, CheckInAt = request.Timestamp ?? DateTime.Now,
CheckInLatitude = request.Latitude, CheckInLatitude = request.Latitude,
CheckInLongitude = request.Longitude, CheckInLongitude = request.Longitude,
@@ -225,11 +228,10 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
public async Task<RouteVisitResponse?> RecordCheckOutAsync(Guid id, RecordCheckOutRequest request, CancellationToken ct = default) public async Task<RouteVisitResponse?> RecordCheckOutAsync(Guid id, RecordCheckOutRequest request, CancellationToken ct = default)
{ {
if (currentUser.UserId is null) return null; // TEMPORARY: the route-scope check depends on the broken auth middleware
// (testbug.md #28) — skipped for now, same as check-in.
var route = await repo.GetByIdAsync(id); var route = await repo.GetByIdAsync(id);
if (route is null || !route.IsActive) return null; if (route is null || !route.IsActive) return null;
if (!await scopeAccess.HasRouteScopeAsync(currentUser.UserId.Value, id)) return null;
var log = await repo.GetOpenVisitAsync(id, request.StoreId); var log = await repo.GetOpenVisitAsync(id, request.StoreId);
if (log is null) return null; if (log is null) return null;
+6 -2
View File
@@ -24,14 +24,18 @@ public class SaleController(SaleService service) : ControllerBase
public async Task<IActionResult> GetByStore(Guid storeId) public async Task<IActionResult> GetByStore(Guid storeId)
{ {
var result = await service.GetByStoreIdAsync(storeId); var result = await service.GetByStoreIdAsync(storeId);
return result is null ? Forbid() : Ok(result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Create(CreateSaleRequest request, CancellationToken ct) public async Task<IActionResult> Create(CreateSaleRequest request, CancellationToken ct)
{
try
{ {
var result = await service.CreateAsync(request, ct); var result = await service.CreateAsync(request, ct);
return result is null ? Forbid() : CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
}
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
} }
[HttpPatch("{id:guid}/cancel")] [HttpPatch("{id:guid}/cancel")]
+4 -1
View File
@@ -10,7 +10,10 @@ public record CreateSaleRequest(
decimal TotalAmount, decimal TotalAmount,
bool FulfillImmediately, bool FulfillImmediately,
Guid? RideId, Guid? RideId,
List<SaleItemRequest> Items); List<SaleItemRequest> Items,
// TEMPORARY: auth middleware doesn't attach a resolvable user yet (see testbug.md #28), so the
// caller supplies the acting user explicitly. Remove once currentUser.UserId is reliable again.
Guid? CreatedByUserId = null);
public record SaleItemResponse(Guid ItemId, int Quantity, int QuantityFulfilled, decimal UnitPrice); public record SaleItemResponse(Guid ItemId, int Quantity, int QuantityFulfilled, decimal UnitPrice);
+49 -29
View File
@@ -121,9 +121,10 @@ public class SaleService(
public async Task<SaleResponse?> CancelAsync(Guid id, CancellationToken ct = default) public async Task<SaleResponse?> CancelAsync(Guid id, CancellationToken ct = default)
{ {
// TEMPORARY: see CreateAsync — store-scope enforcement depends on the broken auth
// middleware (testbug.md #28) and would reject every cancel. Skipped for now.
var sale = await repo.GetByIdAsync(id); var sale = await repo.GetByIdAsync(id);
if (sale is null || !sale.IsActive) return null; if (sale is null || !sale.IsActive) return null;
if (!await HasStoreScopeAsync(sale.FK_StoreId)) return null;
if (sale.Status == SaleStatus.Cancelled) if (sale.Status == SaleStatus.Cancelled)
throw new InvalidOperationException("Sale is already cancelled"); throw new InvalidOperationException("Sale is already cancelled");
@@ -141,46 +142,45 @@ public class SaleService(
public async Task<SaleResponse?> CreateAsync(CreateSaleRequest request, CancellationToken ct = default) public async Task<SaleResponse?> CreateAsync(CreateSaleRequest request, CancellationToken ct = default)
{ {
if (!await HasStoreScopeAsync(request.StoreId)) return null; // TEMPORARY: store-scope enforcement and currentUser resolution both depend on the broken
// auth middleware (see testbug.md #28) — currentUser.UserId/RoleLevel are always unset, so
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // both checks would reject every sale. Skipped for now; restore once auth is fixed.
var store = await storeRepo.GetByIdWithRegionAsync(request.StoreId); var userId = currentUser.UserId ?? request.CreatedByUserId
if (store is null || !store.IsActive) return null; ?? throw new InvalidOperationException("CreatedByUserId is required while auth is unavailable");
var store = await storeRepo.GetByIdWithRegionAsync(request.StoreId)
?? throw new InvalidOperationException("Store not found");
if (!store.IsActive)
throw new InvalidOperationException("Store is inactive");
if (store.KycStatus != KycStatus.Verified) if (store.KycStatus != KycStatus.Verified)
{ {
var threshold = configuration.GetValue<decimal>("StoreManagement:UnverifiedKycOrderThreshold"); var threshold = configuration.GetValue<decimal>("StoreManagement:UnverifiedKycOrderThreshold");
if (request.TotalAmount > threshold) return null; if (request.TotalAmount > threshold)
throw new InvalidOperationException(
$"Store KYC is not verified — sale amount ({request.TotalAmount}) exceeds the unverified-store threshold ({threshold})");
} }
var saleStatus = request.FulfillImmediately ? SaleStatus.Completed : SaleStatus.Pending;
var orderStatus = request.FulfillImmediately ? OrderStatus.Fulfilled : OrderStatus.Pending;
var fulfilledAt = request.FulfillImmediately ? DateTime.Now : (DateTime?)null;
var sale = new Models.Sale var sale = new Models.Sale
{ {
FK_StoreId = request.StoreId, FK_StoreId = request.StoreId,
SaleDate = request.SaleDate, SaleDate = request.SaleDate,
CreatedByUserId = userId, CreatedByUserId = userId,
FK_RideId = request.RideId, FK_RideId = request.RideId,
TotalAmount = request.TotalAmount, FulfillImmediately = request.FulfillImmediately
Status = saleStatus,
FulfillImmediately = request.FulfillImmediately,
CompletedAt = request.FulfillImmediately ? DateTime.Now : null
}; };
var orderItems = new List<Models.OrderItem>(); var orderItems = new List<Models.OrderItem>();
foreach (var item in request.Items) foreach (var item in request.Items)
{ {
var unitPrice = await ResolveUnitPriceAsync(item.ItemId, item.UnitPrice, store, request.SaleDate); var unitPrice = await ResolveUnitPriceAsync(item.ItemId, item.UnitPrice, store, request.SaleDate)
if (unitPrice is null) return null; ?? throw new InvalidOperationException(
$"Could not resolve a price for item {item.ItemId} on {request.SaleDate:yyyy-MM-dd} — check the item exists, an active price list covers this date/region, and any supplied unit price is within its min/max range");
orderItems.Add(new Models.OrderItem orderItems.Add(new Models.OrderItem
{ {
FK_ItemId = item.ItemId, FK_ItemId = item.ItemId,
Quantity = item.Quantity, Quantity = item.Quantity,
QuantityFulfilled = request.FulfillImmediately ? item.Quantity : 0, UnitPrice = unitPrice
UnitPrice = unitPrice.Value
}); });
} }
@@ -189,29 +189,47 @@ public class SaleService(
FK_SaleId = sale.Id, FK_SaleId = sale.Id,
FK_StoreId = request.StoreId, FK_StoreId = request.StoreId,
OrderDate = request.SaleDate, OrderDate = request.SaleDate,
TotalAmount = request.TotalAmount,
Status = orderStatus,
FulfilledAt = fulfilledAt
}; };
foreach (var orderItem in orderItems) foreach (var orderItem in orderItems)
orderItem.FK_OrderId = order.Id; orderItem.FK_OrderId = order.Id;
// For a van sale against a specific ride, sell whatever quantity is actually on that
// vehicle instead of rejecting the whole sale over one short item — the sale is always a
// clean, completed sale of what's handed over, never a "partially fulfilled" order.
Guid? vehicleId = null; Guid? vehicleId = null;
if (request.FulfillImmediately && request.RideId.HasValue) if (request.FulfillImmediately && request.RideId.HasValue)
{ {
var ride = await rideRepo.GetByIdAsync(request.RideId.Value); var ride = await rideRepo.GetByIdAsync(request.RideId.Value)
if (ride is null) return null; ?? throw new InvalidOperationException("Ride not found");
vehicleId = ride.FK_VehicleId; vehicleId = ride.FK_VehicleId;
foreach (var item in orderItems) foreach (var item in orderItems)
{ {
var balance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId.Value); var balance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId.Value);
if (balance is null || balance.Quantity < item.Quantity) item.Quantity = Math.Min(item.Quantity, (int)(balance?.Quantity ?? 0));
return null;
} }
} }
foreach (var item in orderItems)
item.QuantityFulfilled = request.FulfillImmediately ? item.Quantity : 0;
if (request.FulfillImmediately)
{
sale.Status = SaleStatus.Completed;
sale.CompletedAt = DateTime.Now;
order.Status = OrderStatus.Fulfilled;
order.FulfilledAt = DateTime.Now;
}
else
{
sale.Status = SaleStatus.Pending;
order.Status = OrderStatus.Pending;
}
sale.TotalAmount = orderItems.Sum(i => i.Quantity * i.UnitPrice);
order.TotalAmount = sale.TotalAmount;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try try
{ {
@@ -221,10 +239,10 @@ public class SaleService(
if (vehicleId.HasValue) if (vehicleId.HasValue)
{ {
foreach (var item in orderItems) foreach (var item in orderItems.Where(i => i.QuantityFulfilled > 0))
{ {
var balance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId.Value); var balance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId.Value);
balance!.Quantity -= item.Quantity; balance!.Quantity -= item.QuantityFulfilled;
balance.UpdatedAt = DateTime.Now; balance.UpdatedAt = DateTime.Now;
stockRepo.UpdateBalance(balance); stockRepo.UpdateBalance(balance);
@@ -233,7 +251,9 @@ public class SaleService(
FK_ItemId = item.FK_ItemId, FK_ItemId = item.FK_ItemId,
FromLocationId = vehicleId.Value, FromLocationId = vehicleId.Value,
FromLocationType = LocationType.Vehicle, FromLocationType = LocationType.Vehicle,
Quantity = item.Quantity, ToLocationId = StockService.ExternalLocationId,
ToLocationType = LocationType.External,
Quantity = item.QuantityFulfilled,
Type = MovementType.Sale, Type = MovementType.Sale,
ReferenceId = order.Id ReferenceId = order.Id
}); });
+2 -1
View File
@@ -46,7 +46,8 @@ public record UpdateSchemeRequest(
DateTime DateRangeFrom, DateTime DateRangeFrom,
DateTime DateRangeTo, DateTime DateRangeTo,
bool AgentLimitExempt, bool AgentLimitExempt,
decimal? BaselineExpectedSales); decimal? BaselineExpectedSales,
bool IsActive);
public record ApplySchemeRequest(Guid SchemeId); public record ApplySchemeRequest(Guid SchemeId);
@@ -20,7 +20,7 @@ public class SchemeRepository(AppDBContext context) : ISchemeRepository
.Include(s => s.TriggerItem) .Include(s => s.TriggerItem)
.Include(s => s.FreeItem) .Include(s => s.FreeItem)
.Include(s => s.BundleComponents).ThenInclude(c => c.Item) .Include(s => s.BundleComponents).ThenInclude(c => c.Item)
.Where(s => s.IsActive); .AsQueryable();
if (regionId.HasValue) query = query.Where(s => s.FK_RegionId == regionId); if (regionId.HasValue) query = query.Where(s => s.FK_RegionId == regionId);
@@ -41,7 +41,7 @@ public class SchemeService(
public async Task<SchemeResponse?> GetByIdAsync(Guid id) public async Task<SchemeResponse?> GetByIdAsync(Guid id)
{ {
var scheme = await repo.GetByIdAsync(id); var scheme = await repo.GetByIdAsync(id);
return scheme is null || !scheme.IsActive ? null : ToResponse(scheme); return scheme is null ? null : ToResponse(scheme);
} }
/// <summary> /// <summary>
@@ -114,7 +114,7 @@ public class SchemeService(
public async Task<SchemeResponse?> UpdateAsync(Guid id, UpdateSchemeRequest request, CancellationToken ct = default) public async Task<SchemeResponse?> UpdateAsync(Guid id, UpdateSchemeRequest request, CancellationToken ct = default)
{ {
var scheme = await repo.GetByIdAsync(id); var scheme = await repo.GetByIdAsync(id);
if (scheme is null || !scheme.IsActive) return null; if (scheme is null) return null;
ValidatePercentRuleConfig(scheme.RuleType, request.RuleConfig); ValidatePercentRuleConfig(scheme.RuleType, request.RuleConfig);
if (request.DateRangeFrom > request.DateRangeTo) if (request.DateRangeFrom > request.DateRangeTo)
@@ -128,6 +128,7 @@ public class SchemeService(
scheme.DateRangeTo = request.DateRangeTo; scheme.DateRangeTo = request.DateRangeTo;
scheme.AgentLimitExempt = request.AgentLimitExempt; scheme.AgentLimitExempt = request.AgentLimitExempt;
scheme.BaselineExpectedSales = request.BaselineExpectedSales; scheme.BaselineExpectedSales = request.BaselineExpectedSales;
scheme.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Update(scheme); await uow.CommitAsync(ct); } try { repo.Update(scheme); await uow.CommitAsync(ct); }
@@ -255,7 +256,7 @@ public class SchemeService(
var orderItems = await orderRepo.GetItemsByOrderIdAsync(orderId); var orderItems = await orderRepo.GetItemsByOrderIdAsync(orderId);
var regionId = store.Route?.FK_RegionId; var regionId = store.Route?.FK_RegionId;
var candidates = await repo.GetAllAsync(); var candidates = (await repo.GetAllAsync()).Where(s => s.IsActive);
var result = new List<SchemeEligibilityResponse>(); var result = new List<SchemeEligibilityResponse>();
foreach (var scheme in candidates) foreach (var scheme in candidates)
@@ -15,6 +15,8 @@ public interface IStockRepository
// Movements // Movements
Task AddMovementAsync(StockMovement movement); Task AddMovementAsync(StockMovement movement);
Task<StockMovement?> GetMovementByIdAsync(Guid id);
void UpdateMovement(StockMovement movement);
Task<List<StockMovement>> GetMovementsAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to, int page, int pageSize); Task<List<StockMovement>> GetMovementsAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to, int page, int pageSize);
Task<int> GetMovementsCountAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to); Task<int> GetMovementsCountAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to);
} }
+16 -2
View File
@@ -85,10 +85,24 @@ public class StockController(StockService service, ICurrentUserService currentUs
[HttpPost("adjustment")] [HttpPost("adjustment")]
public async Task<IActionResult> Adjust(AdjustmentRequest request, CancellationToken ct) public async Task<IActionResult> Adjust(AdjustmentRequest request, CancellationToken ct)
{ {
if (currentUser.UserId is null) return Unauthorized(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.AdjustedByUserId;
if (userId is null) return BadRequest(new { error = "AdjustedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.AdjustAsync(request, currentUser.UserId.Value, ct); var result = await service.AdjustAsync(request, userId.Value, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return BadRequest(ex.Message); }
}
[HttpPatch("movements/{id}/reason")]
public async Task<IActionResult> UpdateMovementReason(Guid id, UpdateMovementReasonRequest request, CancellationToken ct)
{
try
{
var result = await service.UpdateMovementReasonAsync(id, request, ct);
return Ok(result); return Ok(result);
} }
catch (InvalidOperationException ex) { return BadRequest(ex.Message); } catch (InvalidOperationException ex) { return BadRequest(ex.Message); }
+12 -3
View File
@@ -12,7 +12,8 @@ public record StockBalanceResponse(
Guid LocationId, Guid LocationId,
LocationType LocationType, LocationType LocationType,
decimal Quantity, decimal Quantity,
DateTime UpdatedAt); DateTime UpdatedAt,
string LocationName);
public record StockTotalResponse( public record StockTotalResponse(
Guid ItemId, Guid ItemId,
@@ -34,7 +35,9 @@ public record StockMovementResponse(
string? ReasonCode, string? ReasonCode,
Guid? ReferenceId, Guid? ReferenceId,
DateTime CreatedAt, DateTime CreatedAt,
Guid CreatedBy); Guid CreatedBy,
string? FromLocationName,
string? ToLocationName);
// Write requests // Write requests
public record TransferRequest( public record TransferRequest(
@@ -64,4 +67,10 @@ public record AdjustmentRequest(
Guid LocationId, Guid LocationId,
LocationType LocationType, LocationType LocationType,
decimal Quantity, decimal Quantity,
string ReasonCode); string ReasonCode,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? AdjustedByUserId = null);
// Movements are an immutable ledger — quantity, locations, and type can never change once recorded
// (the running stock balance is the sum of all movements). Only the free-text reason/note is editable.
public record UpdateMovementReasonRequest(string ReasonCode);
@@ -50,6 +50,12 @@ public class StockRepository(AppDBContext db) : IStockRepository
public async Task AddMovementAsync(StockMovement movement) => public async Task AddMovementAsync(StockMovement movement) =>
await db.StockMovements.AddAsync(movement); await db.StockMovements.AddAsync(movement);
public Task<StockMovement?> GetMovementByIdAsync(Guid id) =>
db.StockMovements.Include(m => m.Item).FirstOrDefaultAsync(m => m.Id == id);
public void UpdateMovement(StockMovement movement) =>
db.StockMovements.Update(movement);
public Task<List<StockMovement>> GetMovementsAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to, int page, int pageSize) public Task<List<StockMovement>> GetMovementsAsync(Guid? locationId, Guid? itemId, MovementType? type, DateTime? from, DateTime? to, int page, int pageSize)
{ {
var query = db.StockMovements.Include(m => m.Item).AsQueryable(); var query = db.StockMovements.Include(m => m.Item).AsQueryable();
+110 -16
View File
@@ -1,19 +1,79 @@
using Distribution.Features.Category; using Distribution.Features.Category;
using Distribution.Features.Item; using Distribution.Features.Item;
using Distribution.Features.Vehicle;
using Distribution.Features.Warehouse;
using Distribution.Infra.UoW; using Distribution.Infra.UoW;
using Distribution.Models; using Distribution.Models;
namespace Distribution.Features.Stock; namespace Distribution.Features.Stock;
public class StockService(IStockRepository repo, IItemRepository itemRepo, ICategoryRepository categoryRepo, IUnitOfWork uow) public class StockService(
IStockRepository repo,
IItemRepository itemRepo,
ICategoryRepository categoryRepo,
IWarehouseRepository warehouseRepo,
IVehicleRepository vehicleRepo,
IUnitOfWork uow)
{ {
private static StockBalanceResponse MapBalance(StockBalance b) => private static StockBalanceResponse MapBalance(StockBalance b, string locationName) =>
new(b.FK_ItemId, b.Item.Name, b.LocationId, b.LocationType, b.Quantity, b.UpdatedAt); new(b.FK_ItemId, b.Item.Name, b.LocationId, b.LocationType, b.Quantity, b.UpdatedAt, locationName);
private static StockMovementResponse MapMovement(StockMovement m) => // Sentinel id used for the "External" side of a movement (customer, supplier, write-off, adjustment
// source/sink) that has no real Warehouse/Vehicle record — every movement must always show both a
// From and a To, even when one side is outside the tracked system.
public static readonly Guid ExternalLocationId = Guid.Empty;
private static string ExternalLocationLabel(MovementType type, bool isFrom) => type switch
{
MovementType.Sale => "Customer",
MovementType.CustomerReturn => "Customer",
MovementType.PurchaseReceipt => "Supplier",
MovementType.PurchaseReturn => "Supplier",
MovementType.Damage => "Written Off",
MovementType.Adjustment => "Stock Adjustment",
_ => isFrom ? "External Source" : "External Destination",
};
private static StockMovementResponse MapMovement(StockMovement m, string? fromLocationName, string? toLocationName) =>
new(m.Id, m.FK_ItemId, m.Item.Name, m.FromLocationId, m.FromLocationType, new(m.Id, m.FK_ItemId, m.Item.Name, m.FromLocationId, m.FromLocationType,
m.ToLocationId, m.ToLocationType, m.Quantity, m.Type, m.ReasonCode, m.ToLocationId, m.ToLocationType, m.Quantity, m.Type, m.ReasonCode,
m.ReferenceId, m.CreatedAt, m.CreatedBy); m.ReferenceId, m.CreatedAt, m.CreatedBy, fromLocationName, toLocationName);
private async Task<StockMovementResponse> MapMovementAsync(StockMovement m)
{
var locations = new List<(Guid, LocationType)>();
if (m.FromLocationId is Guid fromId && m.FromLocationType is LocationType fromType && fromType != LocationType.External) locations.Add((fromId, fromType));
if (m.ToLocationId is Guid toId && m.ToLocationType is LocationType toType && toType != LocationType.External) locations.Add((toId, toType));
var names = await ResolveLocationNamesAsync(locations);
return MapMovement(m, ResolveMovementSideName(m, names, isFrom: true), ResolveMovementSideName(m, names, isFrom: false));
}
private static string? ResolveMovementSideName(StockMovement m, Dictionary<Guid, string> names, bool isFrom)
{
var id = isFrom ? m.FromLocationId : m.ToLocationId;
var type = isFrom ? m.FromLocationType : m.ToLocationType;
if (id is not Guid locId || type is not LocationType locType) return null;
return locType == LocationType.External ? ExternalLocationLabel(m.Type, isFrom) : names.GetValueOrDefault(locId, "—");
}
private async Task<Dictionary<Guid, string>> ResolveLocationNamesAsync(IEnumerable<(Guid Id, LocationType Type)> locations)
{
var warehouseIds = locations.Where(l => l.Type == LocationType.Warehouse).Select(l => l.Id).Distinct().ToList();
var vehicleIds = locations.Where(l => l.Type == LocationType.Vehicle).Select(l => l.Id).Distinct().ToList();
var names = new Dictionary<Guid, string>();
foreach (var id in warehouseIds)
{
var warehouse = await warehouseRepo.GetByIdAsync(id);
names[id] = warehouse?.Name ?? "Unknown Warehouse";
}
foreach (var id in vehicleIds)
{
var vehicle = await vehicleRepo.GetByIdAsync(id);
names[id] = vehicle?.RegNumber ?? "Unknown Vehicle";
}
return names;
}
// ── Queries ──────────────────────────────────────────────────────────── // ── Queries ────────────────────────────────────────────────────────────
@@ -22,7 +82,9 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
{ {
var items = await repo.GetBalancesAsync(locationId, locationType, itemId, page, pageSize); var items = await repo.GetBalancesAsync(locationId, locationType, itemId, page, pageSize);
var total = await repo.GetBalancesCountAsync(locationId, locationType, itemId); var total = await repo.GetBalancesCountAsync(locationId, locationType, itemId);
return new PagedResponse<StockBalanceResponse>(page, pageSize, total, items.Select(MapBalance).ToList()); var names = await ResolveLocationNamesAsync(items.Select(b => (b.LocationId, b.LocationType)));
return new PagedResponse<StockBalanceResponse>(page, pageSize, total,
items.Select(b => MapBalance(b, names.GetValueOrDefault(b.LocationId, "—"))).ToList());
} }
public async Task<StockTotalResponse?> GetItemTotalAsync(Guid itemId) public async Task<StockTotalResponse?> GetItemTotalAsync(Guid itemId)
@@ -32,7 +94,9 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
var balances = await repo.GetBalancesByItemAsync(itemId); var balances = await repo.GetBalancesByItemAsync(itemId);
var total = balances.Sum(b => b.Quantity); var total = balances.Sum(b => b.Quantity);
return new StockTotalResponse(itemId, item.Name, total, balances.Select(MapBalance).ToList()); var names = await ResolveLocationNamesAsync(balances.Select(b => (b.LocationId, b.LocationType)));
return new StockTotalResponse(itemId, item.Name, total,
balances.Select(b => MapBalance(b, names.GetValueOrDefault(b.LocationId, "—"))).ToList());
} }
public async Task<PagedResponse<StockMovementResponse>> GetMovementsAsync( public async Task<PagedResponse<StockMovementResponse>> GetMovementsAsync(
@@ -40,7 +104,20 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
{ {
var items = await repo.GetMovementsAsync(locationId, itemId, type, from, to, page, pageSize); var items = await repo.GetMovementsAsync(locationId, itemId, type, from, to, page, pageSize);
var total = await repo.GetMovementsCountAsync(locationId, itemId, type, from, to); var total = await repo.GetMovementsCountAsync(locationId, itemId, type, from, to);
return new PagedResponse<StockMovementResponse>(page, pageSize, total, items.Select(MapMovement).ToList());
var locations = new List<(Guid, LocationType)>();
foreach (var m in items)
{
if (m.FromLocationId is Guid fromId && m.FromLocationType is LocationType fromType && fromType != LocationType.External) locations.Add((fromId, fromType));
if (m.ToLocationId is Guid toId && m.ToLocationType is LocationType toType && toType != LocationType.External) locations.Add((toId, toType));
}
var names = await ResolveLocationNamesAsync(locations);
return new PagedResponse<StockMovementResponse>(page, pageSize, total,
items.Select(m => MapMovement(
m,
ResolveMovementSideName(m, names, isFrom: true),
ResolveMovementSideName(m, names, isFrom: false))).ToList());
} }
// ── Writes ───────────────────────────────────────────────────────────── // ── Writes ─────────────────────────────────────────────────────────────
@@ -71,7 +148,7 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
} }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
return MapMovement(movement); return await MapMovementAsync(movement);
} }
public async Task<StockMovementResponse> RecordDamageAsync(DamageRequest request, Guid userId, CancellationToken ct = default) public async Task<StockMovementResponse> RecordDamageAsync(DamageRequest request, Guid userId, CancellationToken ct = default)
@@ -83,6 +160,8 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
FK_ItemId = request.ItemId, FK_ItemId = request.ItemId,
FromLocationId = request.LocationId, FromLocationId = request.LocationId,
FromLocationType = request.LocationType, FromLocationType = request.LocationType,
ToLocationId = ExternalLocationId,
ToLocationType = LocationType.External,
Quantity = request.Quantity, Quantity = request.Quantity,
Type = MovementType.Damage, Type = MovementType.Damage,
ReasonCode = request.ReasonCode, ReasonCode = request.ReasonCode,
@@ -98,7 +177,7 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
} }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
return MapMovement(movement); return await MapMovementAsync(movement);
} }
public async Task<StockMovementResponse> RecordCustomerReturnAsync(CustomerReturnRequest request, Guid userId, CancellationToken ct = default) public async Task<StockMovementResponse> RecordCustomerReturnAsync(CustomerReturnRequest request, Guid userId, CancellationToken ct = default)
@@ -115,6 +194,8 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
var movement = new StockMovement var movement = new StockMovement
{ {
FK_ItemId = request.ItemId, FK_ItemId = request.ItemId,
FromLocationId = ExternalLocationId,
FromLocationType = LocationType.External,
ToLocationId = request.VehicleId, ToLocationId = request.VehicleId,
ToLocationType = LocationType.Vehicle, ToLocationType = LocationType.Vehicle,
Quantity = request.Quantity, Quantity = request.Quantity,
@@ -132,7 +213,7 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
} }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
return MapMovement(movement); return await MapMovementAsync(movement);
} }
public async Task<StockMovementResponse> AdjustAsync(AdjustmentRequest request, Guid userId, CancellationToken ct = default) public async Task<StockMovementResponse> AdjustAsync(AdjustmentRequest request, Guid userId, CancellationToken ct = default)
@@ -144,10 +225,10 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
var movement = new StockMovement var movement = new StockMovement
{ {
FK_ItemId = request.ItemId, FK_ItemId = request.ItemId,
FromLocationId = request.Quantity < 0 ? request.LocationId : null, FromLocationId = request.Quantity < 0 ? request.LocationId : ExternalLocationId,
FromLocationType = request.Quantity < 0 ? request.LocationType : null, FromLocationType = request.Quantity < 0 ? request.LocationType : LocationType.External,
ToLocationId = request.Quantity > 0 ? request.LocationId : null, ToLocationId = request.Quantity > 0 ? request.LocationId : ExternalLocationId,
ToLocationType = request.Quantity > 0 ? request.LocationType : null, ToLocationType = request.Quantity > 0 ? request.LocationType : LocationType.External,
Quantity = Math.Abs(request.Quantity), Quantity = Math.Abs(request.Quantity),
Type = MovementType.Adjustment, Type = MovementType.Adjustment,
ReasonCode = request.ReasonCode, ReasonCode = request.ReasonCode,
@@ -178,7 +259,20 @@ public class StockService(IStockRepository repo, IItemRepository itemRepo, ICate
} }
catch { await uow.RollbackAsync(ct); throw; } catch { await uow.RollbackAsync(ct); throw; }
return MapMovement(movement); return await MapMovementAsync(movement);
}
// Movements are an immutable ledger — only the reason/note can be edited, never quantity/locations/type.
public async Task<StockMovementResponse> UpdateMovementReasonAsync(Guid movementId, UpdateMovementReasonRequest request, CancellationToken ct = default)
{
var movement = await repo.GetMovementByIdAsync(movementId)
?? throw new InvalidOperationException("Movement not found.");
movement.ReasonCode = request.ReasonCode;
repo.UpdateMovement(movement);
await uow.SaveChangesAsync(ct);
return await MapMovementAsync(movement);
} }
// ── Helpers ──────────────────────────────────────────────────────────── // ── Helpers ────────────────────────────────────────────────────────────
@@ -1,3 +1,5 @@
using Distribution.Models;
namespace Distribution.Features.Store; namespace Distribution.Features.Store;
public interface IStoreRepository public interface IStoreRepository
@@ -5,6 +7,8 @@ public interface IStoreRepository
Task<Models.Store?> GetByIdAsync(Guid id); Task<Models.Store?> GetByIdAsync(Guid id);
Task<Models.Store?> GetByIdWithRegionAsync(Guid id); Task<Models.Store?> GetByIdWithRegionAsync(Guid id);
Task<List<Models.Store>> GetAllAsync(); Task<List<Models.Store>> GetAllAsync();
Task<List<Models.Store>> GetAllAsync(Guid? routeId, KycStatus? kycStatus, string? search, int page, int pageSize);
Task<int> GetCountAsync(Guid? routeId, KycStatus? kycStatus, string? search);
Task AddAsync(Models.Store entity); Task AddAsync(Models.Store entity);
void Update(Models.Store entity); void Update(Models.Store entity);
void Remove(Models.Store entity); void Remove(Models.Store entity);
+18 -5
View File
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Distribution.Models;
using Microsoft.AspNetCore.Mvc;
namespace Distribution.Features.Store; namespace Distribution.Features.Store;
@@ -7,8 +8,13 @@ namespace Distribution.Features.Store;
public class StoreController(StoreService service) : ControllerBase public class StoreController(StoreService service) : ControllerBase
{ {
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAll() => public async Task<IActionResult> GetAll(
Ok(await service.GetAllAsync()); [FromQuery] Guid? routeId,
[FromQuery] KycStatus? kycStatus,
[FromQuery] string? search,
[FromQuery] int page = 1,
[FromQuery] int pageSize = 20) =>
Ok(await service.GetAllAsync(routeId, kycStatus, search, page, pageSize));
[HttpGet("{id:guid}")] [HttpGet("{id:guid}")]
public async Task<IActionResult> GetById(Guid id) public async Task<IActionResult> GetById(Guid id)
@@ -42,14 +48,14 @@ public class StoreController(StoreService service) : ControllerBase
public async Task<IActionResult> UpdateCreditLimit(Guid id, UpdateCreditLimitRequest request, CancellationToken ct) public async Task<IActionResult> UpdateCreditLimit(Guid id, UpdateCreditLimitRequest request, CancellationToken ct)
{ {
var result = await service.UpdateCreditLimitAsync(id, request, ct); var result = await service.UpdateCreditLimitAsync(id, request, ct);
return result is null ? Forbid() : Ok(result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
} }
[HttpPut("{id:guid}/outstanding-balance")] [HttpPut("{id:guid}/outstanding-balance")]
public async Task<IActionResult> UpdateOutstandingBalance(Guid id, UpdateOutstandingBalanceRequest request, CancellationToken ct) public async Task<IActionResult> UpdateOutstandingBalance(Guid id, UpdateOutstandingBalanceRequest request, CancellationToken ct)
{ {
var result = await service.UpdateOutstandingBalanceAsync(id, request, ct); var result = await service.UpdateOutstandingBalanceAsync(id, request, ct);
return result is null ? Forbid() : Ok(result); return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
} }
[HttpGet("{id:guid}/outstanding-balance")] [HttpGet("{id:guid}/outstanding-balance")]
@@ -100,4 +106,11 @@ public class StoreController(StoreService service) : ControllerBase
var result = await service.VerifyKycAsync(id, new VerifyKycRequest(request.KycStatus, null), ct); var result = await service.VerifyKycAsync(id, new VerifyKycRequest(request.KycStatus, null), ct);
return result is null ? NotFound() : Ok(result); return result is null ? NotFound() : Ok(result);
} }
[HttpPatch("{id:guid}/status")]
public async Task<IActionResult> PatchStatus(Guid id, PatchStoreStatusRequest request, CancellationToken ct)
{
var result = await service.SetActiveStatusAsync(id, request.IsActive, ct);
return result is null ? NotFound() : Ok(result);
}
} }
+4 -1
View File
@@ -20,7 +20,8 @@ public record UpdateStoreRequest(
decimal Latitude, decimal Latitude,
decimal Longitude, decimal Longitude,
decimal OneTimeAmount, decimal OneTimeAmount,
decimal CreditAmount); decimal CreditAmount,
bool IsActive = true);
public record StoreResponse( public record StoreResponse(
Guid Id, Guid Id,
@@ -55,4 +56,6 @@ public record SchemeEligibilityResponse(bool Eligible, string? Reason);
public record PatchKycStatusRequest(KycStatus KycStatus); public record PatchKycStatusRequest(KycStatus KycStatus);
public record PatchStoreStatusRequest(bool IsActive);
public record StoreDashboardResponse(Guid StoreId, string Period, int OrderCount, decimal TotalOrderValue, DateTime? LastOrderDate); public record StoreDashboardResponse(Guid StoreId, string Period, int OrderCount, decimal TotalOrderValue, DateTime? LastOrderDate);
@@ -1,4 +1,5 @@
using Distribution.Data; using Distribution.Data;
using Distribution.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Distribution.Features.Store; namespace Distribution.Features.Store;
@@ -16,6 +17,25 @@ public class StoreRepository(AppDBContext context) : IStoreRepository
public async Task<List<Models.Store>> GetAllAsync() => public async Task<List<Models.Store>> GetAllAsync() =>
await context.Stores.ToListAsync(); await context.Stores.ToListAsync();
private static IQueryable<Models.Store> ApplyFilters(
IQueryable<Models.Store> query, Guid? routeId, KycStatus? kycStatus, string? search) =>
query
.Where(s => routeId == null || s.FK_RouteId == routeId)
.Where(s => kycStatus == null || s.KycStatus == kycStatus)
.Where(s => string.IsNullOrWhiteSpace(search)
|| s.StoreName.Contains(search)
|| s.OwnerName.Contains(search));
public async Task<List<Models.Store>> GetAllAsync(Guid? routeId, KycStatus? kycStatus, string? search, int page, int pageSize) =>
await ApplyFilters(context.Stores, routeId, kycStatus, search)
.OrderBy(s => s.StoreName)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
public async Task<int> GetCountAsync(Guid? routeId, KycStatus? kycStatus, string? search) =>
await ApplyFilters(context.Stores, routeId, kycStatus, search).CountAsync();
public async Task AddAsync(Models.Store entity) => public async Task AddAsync(Models.Store entity) =>
await context.Stores.AddAsync(entity); await context.Stores.AddAsync(entity);
+34 -11
View File
@@ -1,4 +1,5 @@
using Distribution.Features.Scheme; using Distribution.Features.Scheme;
using Distribution.Features.Stock;
using Distribution.Infra.Authorization; using Distribution.Infra.Authorization;
using Distribution.Infra.UoW; using Distribution.Infra.UoW;
using Distribution.Models; using Distribution.Models;
@@ -23,21 +24,20 @@ public class StoreService(IStoreRepository repo, IUnitOfWork uow, ICurrentUserSe
new(s.Id, s.FK_RouteId, s.OwnerName, s.StoreName, s.StoreAddress, s.Latitude, s.Longitude, s.OneTimeAmount, s.CreditAmount, new(s.Id, s.FK_RouteId, s.OwnerName, s.StoreName, s.StoreAddress, s.Latitude, s.Longitude, s.OneTimeAmount, s.CreditAmount,
s.CreditLimit, s.OutstandingBalance, s.Segment, s.KycStatus, s.IsActive, s.CreatedAt); s.CreditLimit, s.OutstandingBalance, s.Segment, s.KycStatus, s.IsActive, s.CreatedAt);
public async Task<List<StoreResponse>> GetAllAsync() public async Task<PagedResponse<StoreResponse>> GetAllAsync(
Guid? routeId, KycStatus? kycStatus, string? search, int page, int pageSize)
{ {
var stores = (await repo.GetAllAsync()).Where(s => s.IsActive).ToList(); // Deactivated stores stay visible in the list showing Inactive — deactivate never
if (!HasGlobalScope && currentUser.UserId is not null) // removes the row, only marks it (same rule as Orders, see testbug.md #42).
{ var total = await repo.GetCountAsync(routeId, kycStatus, search);
var accessibleStoreIds = (await scopeAccess.GetAccessibleStoreIdsAsync(currentUser.UserId.Value)).ToHashSet(); var stores = await repo.GetAllAsync(routeId, kycStatus, search, page, pageSize);
stores = stores.Where(s => accessibleStoreIds.Contains(s.Id)).ToList(); return new PagedResponse<StoreResponse>(page, pageSize, total, stores.Select(ToResponse).ToList());
}
return stores.Select(ToResponse).ToList();
} }
public async Task<StoreResponse?> GetByIdAsync(Guid id) public async Task<StoreResponse?> GetByIdAsync(Guid id)
{ {
var store = await repo.GetByIdAsync(id); var store = await repo.GetByIdAsync(id);
if (store is null || !store.IsActive) return null; if (store is null) return null;
//if (!await HasStoreScopeAsync(id)) return null; //if (!await HasStoreScopeAsync(id)) return null;
return ToResponse(store); return ToResponse(store);
} }
@@ -72,7 +72,7 @@ public class StoreService(IStoreRepository repo, IUnitOfWork uow, ICurrentUserSe
public async Task<StoreResponse?> UpdateAsync(Guid id, UpdateStoreRequest request, CancellationToken ct = default) public async Task<StoreResponse?> UpdateAsync(Guid id, UpdateStoreRequest request, CancellationToken ct = default)
{ {
var store = await repo.GetByIdAsync(id); var store = await repo.GetByIdAsync(id);
if (store is null || !store.IsActive) return null; if (store is null) return null;
if (!await HasStoreScopeAsync(id)) return null; if (!await HasStoreScopeAsync(id)) return null;
store.FK_RouteId = request.RouteId; store.FK_RouteId = request.RouteId;
store.OwnerName = request.OwnerName; store.OwnerName = request.OwnerName;
@@ -82,6 +82,7 @@ public class StoreService(IStoreRepository repo, IUnitOfWork uow, ICurrentUserSe
store.Longitude = request.Longitude; store.Longitude = request.Longitude;
store.OneTimeAmount = request.OneTimeAmount; store.OneTimeAmount = request.OneTimeAmount;
store.CreditAmount = request.CreditAmount; store.CreditAmount = request.CreditAmount;
store.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try try
{ {
@@ -205,10 +206,32 @@ public class StoreService(IStoreRepository repo, IUnitOfWork uow, ICurrentUserSe
return ToResponse(store); return ToResponse(store);
} }
public async Task<StoreResponse?> SetActiveStatusAsync(Guid id, bool isActive, CancellationToken ct = default)
{
var store = await repo.GetByIdAsync(id);
if (store is null) return null;
if (!await HasStoreScopeAsync(id)) return null;
store.IsActive = isActive;
await uow.BeginAsync(ct);
try
{
repo.Update(store);
await uow.CommitAsync(ct);
}
catch
{
await uow.RollbackAsync(ct);
throw;
}
return ToResponse(store);
}
public async Task<List<StoreVisitHistoryItem>?> GetVisitHistoryAsync(Guid id) public async Task<List<StoreVisitHistoryItem>?> GetVisitHistoryAsync(Guid id)
{ {
var store = await repo.GetByIdAsync(id); var store = await repo.GetByIdAsync(id);
if (store is null || !store.IsActive) return null; if (store is null) return null;
if (!await HasStoreScopeAsync(id)) return null; if (!await HasStoreScopeAsync(id)) return null;
var visits = await routeRepo.GetVisitsByStoreIdAsync(id); var visits = await routeRepo.GetVisitsByStoreIdAsync(id);
@@ -2,7 +2,7 @@ namespace Distribution.Features.Supplier;
public record CreateSupplierRequest(string Name, string? ContactInfo); public record CreateSupplierRequest(string Name, string? ContactInfo);
public record UpdateSupplierRequest(string Name, string? ContactInfo); public record UpdateSupplierRequest(string Name, string? ContactInfo, bool IsActive);
public record SupplierResponse( public record SupplierResponse(
Guid Id, Guid Id,
@@ -9,7 +9,7 @@ public class SupplierRepository(AppDBContext context) : ISupplierRepository
await context.Suppliers.FindAsync(id); await context.Suppliers.FindAsync(id);
public async Task<List<Models.Supplier>> GetAllAsync() => public async Task<List<Models.Supplier>> GetAllAsync() =>
await context.Suppliers.Where(s => s.IsActive).ToListAsync(); await context.Suppliers.ToListAsync();
public async Task AddAsync(Models.Supplier entity) => public async Task AddAsync(Models.Supplier entity) =>
await context.Suppliers.AddAsync(entity); await context.Suppliers.AddAsync(entity);
@@ -19,8 +19,7 @@ public class SupplierService(
public async Task<SupplierResponse?> GetByIdAsync(Guid id) public async Task<SupplierResponse?> GetByIdAsync(Guid id)
{ {
var supplier = await repo.GetByIdAsync(id); var supplier = await repo.GetByIdAsync(id);
if (supplier is null || !supplier.IsActive) return null; return supplier is null ? null : Map(supplier);
return Map(supplier);
} }
public async Task<SupplierResponse> CreateAsync(CreateSupplierRequest request, CancellationToken ct = default) public async Task<SupplierResponse> CreateAsync(CreateSupplierRequest request, CancellationToken ct = default)
@@ -41,10 +40,11 @@ public class SupplierService(
public async Task<SupplierResponse?> UpdateAsync(Guid id, UpdateSupplierRequest request, CancellationToken ct = default) public async Task<SupplierResponse?> UpdateAsync(Guid id, UpdateSupplierRequest request, CancellationToken ct = default)
{ {
var supplier = await repo.GetByIdAsync(id); var supplier = await repo.GetByIdAsync(id);
if (supplier is null || !supplier.IsActive) return null; if (supplier is null) return null;
supplier.Name = request.Name; supplier.Name = request.Name;
supplier.ContactInfo = request.ContactInfo; supplier.ContactInfo = request.ContactInfo;
supplier.IsActive = request.IsActive;
await uow.BeginAsync(ct); await uow.BeginAsync(ct);
try { repo.Update(supplier); await uow.CommitAsync(ct); } try { repo.Update(supplier); await uow.CommitAsync(ct); }
@@ -3,6 +3,7 @@ namespace Distribution.Features.SupplierClaims;
public interface ISupplierClaimRepository public interface ISupplierClaimRepository
{ {
Task<Models.SupplierClaim?> GetByIdAsync(Guid id); Task<Models.SupplierClaim?> GetByIdAsync(Guid id);
Task<List<Models.SupplierClaim>> GetAllAsync();
Task<List<Models.SupplierClaim>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId); Task<List<Models.SupplierClaim>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId);
Task AddAsync(Models.SupplierClaim entity); Task AddAsync(Models.SupplierClaim entity);
void Update(Models.SupplierClaim entity); void Update(Models.SupplierClaim entity);
@@ -6,6 +6,10 @@ namespace Distribution.Features.SupplierClaims;
[ApiController] [ApiController]
public class SupplierClaimController(SupplierClaimService service, ICurrentUserService currentUser) : ControllerBase public class SupplierClaimController(SupplierClaimService service, ICurrentUserService currentUser) : ControllerBase
{ {
[HttpGet("api/supplier-claims")]
public async Task<IActionResult> GetAll() =>
Ok(await service.GetAllAsync());
[HttpGet("api/supplier-claims/{id:guid}")] [HttpGet("api/supplier-claims/{id:guid}")]
public async Task<IActionResult> GetById(Guid id) public async Task<IActionResult> GetById(Guid id)
{ {
@@ -20,22 +24,37 @@ public class SupplierClaimController(SupplierClaimService service, ICurrentUserS
[HttpPost("api/purchase-orders/{id:guid}/claims")] [HttpPost("api/purchase-orders/{id:guid}/claims")]
public async Task<IActionResult> Create(Guid id, CreateSupplierClaimRequest request, CancellationToken ct) public async Task<IActionResult> Create(Guid id, CreateSupplierClaimRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
var userId = currentUser.UserId ?? request.RaisedByUserId;
if (userId is null) return BadRequest(new { error = "RaisedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.CreateAsync(id, request, userId, ct); var result = await service.CreateAsync(id, request, userId.Value, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
} }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
} }
[HttpPut("api/supplier-claims/{id:guid}")]
public async Task<IActionResult> Update(Guid id, UpdateSupplierClaimRequest request, CancellationToken ct)
{
try
{
var result = await service.UpdateAsync(id, request, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
}
[HttpPost("api/supplier-claims/{id:guid}/approve")] [HttpPost("api/supplier-claims/{id:guid}/approve")]
public async Task<IActionResult> Approve(Guid id, ApproveSupplierClaimRequest request, CancellationToken ct) public async Task<IActionResult> Approve(Guid id, ApproveSupplierClaimRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); var userId = currentUser.UserId ?? request.ApprovedByUserId;
if (userId is null) return BadRequest(new { error = "ApprovedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.ApproveAsync(id, request, userId, ct); var result = await service.ApproveAsync(id, request, userId.Value, ct);
return Ok(result); return Ok(result);
} }
catch (KeyNotFoundException) { return NotFound(); } catch (KeyNotFoundException) { return NotFound(); }
@@ -45,10 +64,11 @@ public class SupplierClaimController(SupplierClaimService service, ICurrentUserS
[HttpPost("api/supplier-claims/{id:guid}/reject")] [HttpPost("api/supplier-claims/{id:guid}/reject")]
public async Task<IActionResult> Reject(Guid id, RejectSupplierClaimRequest request, CancellationToken ct) public async Task<IActionResult> Reject(Guid id, RejectSupplierClaimRequest request, CancellationToken ct)
{ {
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException(); var userId = currentUser.UserId ?? request.RejectedByUserId;
if (userId is null) return BadRequest(new { error = "RejectedByUserId is required while auth is unavailable" });
try try
{ {
var result = await service.RejectAsync(id, request, userId, ct); var result = await service.RejectAsync(id, request, userId.Value, ct);
return Ok(result); return Ok(result);
} }
catch (KeyNotFoundException) { return NotFound(); } catch (KeyNotFoundException) { return NotFound(); }
@@ -6,6 +6,15 @@ public record SupplierClaimItemRequest(Guid ItemId, decimal Quantity, SupplierCl
public record CreateSupplierClaimRequest( public record CreateSupplierClaimRequest(
Guid? GoodsReceiptId, Guid? GoodsReceiptId,
string Details,
string? EvidenceUrl,
List<SupplierClaimItemRequest> Items,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? RaisedByUserId = null);
// Only editable while the claim is still Open — Approve/Reject resolve it, and a resolved
// claim's details/items shouldn't silently change after the fact.
public record UpdateSupplierClaimRequest(
string Details, string Details,
string? EvidenceUrl, string? EvidenceUrl,
List<SupplierClaimItemRequest> Items); List<SupplierClaimItemRequest> Items);
@@ -13,9 +22,14 @@ public record CreateSupplierClaimRequest(
public record ApproveSupplierClaimRequest( public record ApproveSupplierClaimRequest(
SupplierClaimResolutionType ResolutionType, SupplierClaimResolutionType ResolutionType,
decimal? ResolutionAmount, decimal? ResolutionAmount,
string? ResolutionNotes); string? ResolutionNotes,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? ApprovedByUserId = null);
public record RejectSupplierClaimRequest(string? ResolutionNotes); public record RejectSupplierClaimRequest(
string? ResolutionNotes,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? RejectedByUserId = null);
public record SupplierClaimItemResponse( public record SupplierClaimItemResponse(
Guid Id, Guid Id,
@@ -10,6 +10,13 @@ public class SupplierClaimRepository(AppDBContext context) : ISupplierClaimRepos
.Include(c => c.Items).ThenInclude(i => i.Item) .Include(c => c.Items).ThenInclude(i => i.Item)
.FirstOrDefaultAsync(c => c.Id == id); .FirstOrDefaultAsync(c => c.Id == id);
public async Task<List<Models.SupplierClaim>> GetAllAsync() =>
await context.SupplierClaims
.Include(c => c.Items).ThenInclude(i => i.Item)
.Where(c => c.IsActive)
.OrderByDescending(c => c.CreatedAt)
.ToListAsync();
public async Task<List<Models.SupplierClaim>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId) => public async Task<List<Models.SupplierClaim>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId) =>
await context.SupplierClaims await context.SupplierClaims
.Include(c => c.Items).ThenInclude(i => i.Item) .Include(c => c.Items).ThenInclude(i => i.Item)
@@ -26,6 +26,9 @@ public class SupplierClaimService(
return Map(c); return Map(c);
} }
public async Task<List<SupplierClaimResponse>> GetAllAsync() =>
(await repo.GetAllAsync()).Select(Map).ToList();
public async Task<List<SupplierClaimResponse>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId) => public async Task<List<SupplierClaimResponse>> GetByPurchaseOrderIdAsync(Guid purchaseOrderId) =>
(await repo.GetByPurchaseOrderIdAsync(purchaseOrderId)).Select(Map).ToList(); (await repo.GetByPurchaseOrderIdAsync(purchaseOrderId)).Select(Map).ToList();
@@ -83,6 +86,44 @@ public class SupplierClaimService(
return Map(saved!); return Map(saved!);
} }
public async Task<SupplierClaimResponse> UpdateAsync(Guid id, UpdateSupplierClaimRequest request, CancellationToken ct = default)
{
var claim = await repo.GetByIdAsync(id)
?? throw new InvalidOperationException("Supplier claim not found.");
if (claim.Status != SupplierClaimStatus.Open)
throw new InvalidOperationException("Only an open claim can be edited.");
if (string.IsNullOrWhiteSpace(request.Details))
throw new InvalidOperationException("Claim details are required.");
if (request.Items is null || request.Items.Count == 0)
throw new InvalidOperationException("A supplier claim must have at least one line item.");
claim.Details = request.Details;
claim.EvidenceUrl = request.EvidenceUrl;
foreach (var existing in claim.Items.Where(i => i.IsActive))
existing.IsActive = false;
foreach (var line in request.Items)
{
if (line.Quantity <= 0)
throw new InvalidOperationException("Claim quantity must be greater than zero.");
claim.Items.Add(new SupplierClaimItem
{
FK_ItemId = line.ItemId,
Quantity = line.Quantity,
IssueType = line.IssueType
});
}
await uow.BeginAsync(ct);
try { repo.Update(claim); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; }
var saved = await repo.GetByIdAsync(claim.Id);
return Map(saved!);
}
public async Task<SupplierClaimResponse> ApproveAsync(Guid id, ApproveSupplierClaimRequest request, Guid approverId, CancellationToken ct = default) public async Task<SupplierClaimResponse> ApproveAsync(Guid id, ApproveSupplierClaimRequest request, Guid approverId, CancellationToken ct = default)
{ {
var claim = await repo.GetByIdAsync(id) var claim = await repo.GetByIdAsync(id)
@@ -1,185 +0,0 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using Distribution.Features.User;
using Distribution.Infra.Authorization;
using Microsoft.IdentityModel.Tokens;
namespace Distribution.Infra.Authorization.Middleware;
/// <summary>
/// Ported from OSMCore's System/Authentication/AuthMiddleware.cs (global path-skip-based
/// middleware). NOT registered in Program.cs — this project's actual token validation is the
/// standard AddJwtBearer handler (see Program.cs), same as OSMCore's own recommendation. Kept here
/// unregistered, exactly like OSMCore's own copy, for reference/reuse if a project needs a
/// middleware-based (rather than AddJwtBearer-based) validation entry point.
///
/// Two adaptations from the OSMCore original, both required for this to actually work against
/// this project's authorization system rather than just compiling:
/// 1. RSA key parsing goes through <see cref="RsaXmlKeyReader"/> instead of RSA.FromXmlString,
/// which is Windows-only in modern .NET (see docs/authentication_flow.md §2.2 Gotcha).
/// 2. After validating the token, the user id is mapped onto a local User/Role row and
/// role_id/role_level/username claims are attached — the same step Program.cs's
/// OnTokenValidated performs for the AddJwtBearer path. Without this, a request authenticated
/// through this middleware would have no role_id claim, and every
/// [Authorize(Policy = "resource.action")] check (PermissionAuthorizationHandler) would fail.
/// </summary>
public sealed class AuthMiddleware(RequestDelegate next, IConfiguration configuration, ILogger<AuthMiddleware> logger)
{
public async Task InvokeAsync(HttpContext context)
{
if (ShouldSkipAuthentication(context.Request.Path))
{
await next(context);
return;
}
if (!TryGetToken(context, out var token))
{
await RejectAsync(context, "Authentication token is missing.");
return;
}
try
{
var principal = ValidateToken(token);
var userRepo = context.RequestServices.GetRequiredService<IUserRepository>();
if (!await TryAttachLocalRoleClaimsAsync(principal, userRepo))
{
await RejectAsync(context, "User not found or inactive.");
return;
}
context.User = principal;
PopulateContextItems(context, principal);
await next(context);
}
catch (Exception ex) when (ex is SecurityTokenException or ArgumentException or CryptographicException)
{
logger.LogInformation(ex, "JWT validation failed for request path {Path}", context.Request.Path);
await RejectAsync(context, "Invalid or expired token.");
}
}
private ClaimsPrincipal ValidateToken(string token)
{
var jwtOptions = configuration.GetSection("Jwt").Get<JwtOptions>()
?? throw new InvalidOperationException("Jwt configuration is missing.");
if (string.IsNullOrWhiteSpace(jwtOptions.PublicKey))
{
throw new InvalidOperationException("Jwt:PublicKey configuration is missing.");
}
using var rsa = RsaXmlKeyReader.ReadPublicKey(jwtOptions.PublicKey);
var handler = new JwtSecurityTokenHandler();
var parameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtOptions.Issuer,
ValidateAudience = true,
ValidAudience = jwtOptions.Audience,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new RsaSecurityKey(rsa),
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(1)
};
return handler.ValidateToken(token, parameters, out _);
}
/// <summary>Mirrors Program.cs's OnTokenValidated mapping — see class remarks.</summary>
private static async Task<bool> TryAttachLocalRoleClaimsAsync(ClaimsPrincipal principal, IUserRepository userRepo)
{
var subClaim = principal.FindFirst("sub")?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (!Guid.TryParse(subClaim, out var userId))
return false;
var user = await userRepo.GetByIdAsync(userId);
if (user is null || !user.IsActive)
return false;
var identity = (ClaimsIdentity)principal.Identity!;
identity.AddClaim(new Claim("username", user.Username));
identity.AddClaim(new Claim("role_id", user.FK_RoleId.ToString()));
identity.AddClaim(new Claim("role_level", (user.Role?.Level ?? 0).ToString()));
return true;
}
private static bool TryGetToken(HttpContext context, out string token)
{
token = string.Empty;
if (TryGetBearerToken(context.Request.Headers.Authorization, out token))
{
return true;
}
var headerToken = context.Request.Headers["MobileAuthToken"].FirstOrDefault();
if (TryGetBearerToken(headerToken, out token))
{
return true;
}
var cookieToken = context.Request.Cookies["access_token"];
if (!string.IsNullOrWhiteSpace(cookieToken))
{
token = cookieToken.Trim();
return true;
}
return false;
}
private static bool TryGetBearerToken(string? authorizationHeader, out string token)
{
token = string.Empty;
if (string.IsNullOrWhiteSpace(authorizationHeader))
{
return false;
}
const string bearerPrefix = "Bearer ";
if (!authorizationHeader.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase))
{
token = authorizationHeader.Trim();
return !string.IsNullOrWhiteSpace(token);
}
token = authorizationHeader[bearerPrefix.Length..].Trim();
return !string.IsNullOrWhiteSpace(token);
}
/// <summary>
/// Adapted from OSMCore's path list: this project has no Shopify-style webhook route, so that
/// skip is dropped; /api/auth is narrowed to just login/register (this project's /api/auth/me
/// and /api/auth/logout are meant to require auth, unlike a blanket "/api/auth" skip).
/// </summary>
private static bool ShouldSkipAuthentication(PathString path)
=> path.Equals("/health", StringComparison.OrdinalIgnoreCase)
|| path.Equals("/api/auth/login", StringComparison.OrdinalIgnoreCase)
|| path.Equals("/api/auth/register", StringComparison.OrdinalIgnoreCase)
|| path.StartsWithSegments("/swagger", StringComparison.OrdinalIgnoreCase);
private static void PopulateContextItems(HttpContext context, ClaimsPrincipal principal)
{
context.Items["UserId"] = principal.FindFirst("sub")?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
context.Items["Username"] = principal.FindFirst("username")?.Value;
context.Items["RoleId"] = principal.FindFirst("role_id")?.Value;
context.Items["RoleLevel"] = principal.FindFirst("role_level")?.Value;
}
private static async Task RejectAsync(HttpContext context, string message)
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsJsonAsync(new ApiResponse<object>
{
StatusCode = StatusCodes.Status401Unauthorized,
Success = false,
Message = message,
Data = null
});
}
}
@@ -1,191 +0,0 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using Distribution.Features.User;
using Distribution.Infra.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.IdentityModel.Tokens;
namespace Distribution.Infra.Authorization.Middleware;
/// <summary>
/// Ported from OSMCore's System/Authentication/JwtMiddleware.cs (action-filter attribute pattern,
/// applied per controller/action as [JwtMiddleware.CustomMiddleware]). Not applied anywhere in
/// this project — the standard AddJwtBearer handler (Program.cs) is this project's actual
/// validation path.
///
/// docs/authentication_flow.md explicitly calls out not running this alongside AuthMiddleware or
/// AddJwtBearer at the same time — all three independently re-validate the same RSA-signed token,
/// which is wasteful and error-prone (this is the same duplication OSMCore's own guide flags as a
/// Gotcha). Kept here, unregistered, only as a ported reference implementation.
///
/// Same two adaptations as AuthMiddleware: portable RSA parsing via
/// <see cref="RsaXmlKeyReader"/>, and local User/Role → role_id/role_level claim mapping after
/// validation (see AuthMiddleware's remarks for why this is required, not optional).
/// </summary>
public sealed class JwtMiddleware
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class CustomMiddlewareAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var configuration = context.HttpContext.RequestServices.GetService<IConfiguration>();
if (!TryGetToken(context.HttpContext, out var token))
{
await RejectAsync(context.HttpContext, "Authentication token is missing.");
return;
}
try
{
if (configuration is null)
{
throw new InvalidOperationException("Jwt configuration is missing.");
}
var principal = ValidateToken(token, configuration);
var userRepo = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
if (!await TryAttachLocalRoleClaimsAsync(principal, userRepo))
{
await RejectAsync(context.HttpContext, "User not found or inactive.");
return;
}
context.HttpContext.User = principal;
PopulateContextItems(context.HttpContext, principal);
await next();
}
catch (Exception ex) when (ex is SecurityTokenException or ArgumentException or CryptographicException or InvalidOperationException)
{
await RejectAsync(context.HttpContext, "Invalid or expired token.");
}
}
private static ClaimsPrincipal ValidateToken(string token, IConfiguration configuration)
{
var xmlKey = configuration["Jwt:PublicKey"];
if (string.IsNullOrWhiteSpace(xmlKey))
throw new SecurityTokenException("JWT public key not configured.");
using var rsa = RsaXmlKeyReader.ReadPublicKey(xmlKey);
var rsaParameters = rsa.ExportParameters(false);
var key = new RsaSecurityKey(rsaParameters)
{
KeyId = ComputeKeyId(rsaParameters)
};
var handler = new JwtSecurityTokenHandler();
var parameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = configuration["Jwt:Issuer"],
ValidateAudience = true,
ValidAudience = configuration["Jwt:Audience"],
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(1)
};
return handler.ValidateToken(token, parameters, out _);
}
/// <summary>Mirrors Program.cs's OnTokenValidated mapping — see class remarks.</summary>
private static async Task<bool> TryAttachLocalRoleClaimsAsync(ClaimsPrincipal principal, IUserRepository userRepo)
{
var subClaim = principal.FindFirst("sub")?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (!Guid.TryParse(subClaim, out var userId))
return false;
var user = await userRepo.GetByIdAsync(userId);
if (user is null || !user.IsActive)
return false;
var identity = (ClaimsIdentity)principal.Identity!;
identity.AddClaim(new Claim("username", user.Username));
identity.AddClaim(new Claim("role_id", user.FK_RoleId.ToString()));
identity.AddClaim(new Claim("role_level", (user.Role?.Level ?? 0).ToString()));
return true;
}
private static bool TryGetToken(HttpContext context, out string token)
{
token = string.Empty;
if (TryGetBearerToken(context.Request.Headers.Authorization, out token))
{
return true;
}
var headerToken = context.Request.Headers["MobileAuthToken"].FirstOrDefault();
if (TryGetBearerToken(headerToken, out token))
{
return true;
}
var cookieToken = context.Request.Cookies["access_token"];
if (!string.IsNullOrWhiteSpace(cookieToken))
{
token = cookieToken.Trim();
return true;
}
return false;
}
private static bool TryGetBearerToken(string? authorizationHeader, out string token)
{
token = string.Empty;
if (string.IsNullOrWhiteSpace(authorizationHeader))
{
return false;
}
const string bearerPrefix = "Bearer ";
if (!authorizationHeader.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase))
{
token = authorizationHeader.Trim();
return !string.IsNullOrWhiteSpace(token);
}
token = authorizationHeader[bearerPrefix.Length..].Trim();
return !string.IsNullOrWhiteSpace(token);
}
private static void PopulateContextItems(HttpContext context, ClaimsPrincipal principal)
{
context.Items["UserId"] = principal.FindFirst("sub")?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
context.Items["Username"] = principal.FindFirst("username")?.Value;
context.Items["RoleId"] = principal.FindFirst("role_id")?.Value;
context.Items["RoleLevel"] = principal.FindFirst("role_level")?.Value;
}
private static async Task RejectAsync(HttpContext context, string message)
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsJsonAsync(new ApiResponse<object>
{
StatusCode = StatusCodes.Status401Unauthorized,
Success = false,
Message = message,
Data = null
});
}
private static string ComputeKeyId(RSAParameters publicParameters)
{
var modulus = publicParameters.Modulus ?? [];
var exponent = publicParameters.Exponent ?? [];
var payload = $"{Convert.ToBase64String(modulus)}.{Convert.ToBase64String(exponent)}";
var hash = SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(payload));
return Base64UrlEncoder.Encode(hash);
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,47 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Distribution.Migrations
{
/// <inheritdoc />
public partial class FixSeedStockMovementExternalLocations : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000001"),
columns: new[] { "FromLocationId", "FromLocationType" },
values: new object[] { new Guid("00000000-0000-0000-0000-000000000000"), 2 });
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000003"),
columns: new[] { "ToLocationId", "ToLocationType" },
values: new object[] { new Guid("00000000-0000-0000-0000-000000000000"), 2 });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000001"),
columns: new[] { "FromLocationId", "FromLocationType" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000003"),
columns: new[] { "ToLocationId", "ToLocationType" },
values: new object[] { null, null });
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,53 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Distribution.Migrations
{
/// <inheritdoc />
public partial class AddRetailPriceAndAgencyMargin : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "AgencyMarginPercent",
table: "PriceListItems",
type: "decimal(65,30)",
nullable: true);
migrationBuilder.AddColumn<decimal>(
name: "RetailPrice",
table: "PriceListItems",
type: "decimal(65,30)",
nullable: true);
migrationBuilder.UpdateData(
table: "PriceListItems",
keyColumn: "Id",
keyValue: new Guid("40000000-0000-0000-0000-000000000001"),
columns: new[] { "AgencyMarginPercent", "RetailPrice" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "PriceListItems",
keyColumn: "Id",
keyValue: new Guid("40000000-0000-0000-0000-000000000002"),
columns: new[] { "AgencyMarginPercent", "RetailPrice" },
values: new object[] { null, null });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AgencyMarginPercent",
table: "PriceListItems");
migrationBuilder.DropColumn(
name: "RetailPrice",
table: "PriceListItems");
}
}
}
@@ -1023,6 +1023,9 @@ namespace Distribution.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("char(36)"); .HasColumnType("char(36)");
b.Property<decimal?>("AgencyMarginPercent")
.HasColumnType("decimal(65,30)");
b.Property<DateTime>("CreatedAt") b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)"); .HasColumnType("datetime(6)");
@@ -1041,6 +1044,9 @@ namespace Distribution.Migrations
b.Property<decimal>("Price") b.Property<decimal>("Price")
.HasColumnType("decimal(65,30)"); .HasColumnType("decimal(65,30)");
b.Property<decimal?>("RetailPrice")
.HasColumnType("decimal(65,30)");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("FK_ItemId"); b.HasIndex("FK_ItemId");
@@ -2299,6 +2305,8 @@ namespace Distribution.Migrations
CreatedAt = new DateTime(2026, 8, 15, 0, 0, 0, 0, DateTimeKind.Unspecified), CreatedAt = new DateTime(2026, 8, 15, 0, 0, 0, 0, DateTimeKind.Unspecified),
CreatedBy = new Guid("29000000-0000-0000-0000-000000000001"), CreatedBy = new Guid("29000000-0000-0000-0000-000000000001"),
FK_ItemId = new Guid("23000000-0000-0000-0000-000000000001"), FK_ItemId = new Guid("23000000-0000-0000-0000-000000000001"),
FromLocationId = new Guid("00000000-0000-0000-0000-000000000000"),
FromLocationType = 2,
Quantity = 200m, Quantity = 200m,
ReferenceId = new Guid("43000000-0000-0000-0000-000000000001"), ReferenceId = new Guid("43000000-0000-0000-0000-000000000001"),
ToLocationId = new Guid("27000000-0000-0000-0000-000000000001"), ToLocationId = new Guid("27000000-0000-0000-0000-000000000001"),
@@ -2329,6 +2337,8 @@ namespace Distribution.Migrations
FromLocationType = 1, FromLocationType = 1,
Quantity = 30m, Quantity = 30m,
ReferenceId = new Guid("32000000-0000-0000-0000-000000000001"), ReferenceId = new Guid("32000000-0000-0000-0000-000000000001"),
ToLocationId = new Guid("00000000-0000-0000-0000-000000000000"),
ToLocationType = 2,
Type = 0 Type = 0
}); });
}); });
+1 -1
View File
@@ -1,7 +1,7 @@
namespace Distribution.Models namespace Distribution.Models
{ {
public enum LocationType public enum LocationType
{ Warehouse, Vehicle } { Warehouse, Vehicle, External }
public enum KycStatus { Pending,Verified, Rejected } public enum KycStatus { Pending,Verified, Rejected }
+14
View File
@@ -19,5 +19,19 @@ namespace Distribution.Models
/// Must be less than or equal to Price when set. /// Must be less than or equal to Price when set.
/// </summary> /// </summary>
public decimal? MinPrice { get; set; } public decimal? MinPrice { get; set; }
/// <summary>
/// MRP — the price the store is expected to charge the end consumer. Informational (not
/// enforced on sales), but must be &gt;= Price so the store always has a positive margin.
/// Null = not tracked for this item/list.
/// </summary>
public decimal? RetailPrice { get; set; }
/// <summary>
/// The regional agency's commission, as a percentage (0-100) of Price, for operating
/// distribution in that region. Informational — used for agency settlement/payout
/// reporting, not enforced on sales. Null = not tracked for this item/list.
/// </summary>
public decimal? AgencyMarginPercent { get; set; }
} }
} }
+53 -5
View File
@@ -50,6 +50,36 @@ default — the same negotiation-boundary pattern already used for discount limi
(400, via a null resolution) if the requested price falls outside the band, or if no price can be (400, via a null resolution) if the requested price falls outside the band, or if no price can be
resolved at all. resolved at all.
### Retail price and agency margin (informational, not enforced on sales)
Two more numbers can be tracked per `(PriceListItem)` beyond the wholesale `Price`/`MinPrice` band,
covering the rest of the chain from distributor down to the end consumer:
- **`RetailPrice` (decimal?)** — the MRP the store is expected to charge the end consumer. Not
enforced anywhere (a store can charge whatever it wants at the till) — it exists so the
wholesale `Price` can be validated against it (a store must always have a positive margin) and so
the sales rep can see it while negotiating with a store.
- **`AgencyMarginPercent` (decimal?)** — the percentage of `Price` the regional agency operating
that territory retains as commission for running local distribution (sales force, vehicles,
warehousing). Purely informational/reporting — not deducted from anything automatically; it feeds
agency settlement/payout reporting, the same `RegionalAgency` role that raises Agency Claims
([18_returns_claims.md](./18_returns_claims.md)).
Neither field participates in `ResolveUnitPriceAsync`'s pricing logic or the `[MinPrice, Price]`
negotiation band — they ride along in the same `PriceListItem` row and `PriceResolution` response
purely for display and reporting.
- `PriceListItem.RetailPrice (decimal?)` — must be `>= Price` when set (409 if violated); `null` =
not tracked for this item/list.
- `PriceListItem.AgencyMarginPercent (decimal?)` — must be between 0 and 100 when set (409 if
violated); `null` = not tracked for this item/list.
- `PUT /api/price-lists/{id}/items/{sku}` accepts the extended body
`{ "price": ..., "minPrice": ..., "retailPrice": ..., "agencyMarginPercent": ... }` — both new
fields are optional and independent of each other and of `minPrice`.
- `PriceResolution` (returned by `ResolvePriceAsync`/`ResolveByStoreAsync`) carries both fields
through so the frontend can display them without a second lookup; both are `null` on the
`Item.SalePrice` fallback path, since neither is tracked outside an actual `PriceListItem`.
--- ---
## Data Model Changes ## Data Model Changes
@@ -75,6 +105,10 @@ PriceListItem
│ the parent PriceList is active and applicable to the order's region │ the parent PriceList is active and applicable to the order's region
├── MinPrice (decimal?) — nullable floor for negotiated pricing; null = Price is fixed. ├── MinPrice (decimal?) — nullable floor for negotiated pricing; null = Price is fixed.
│ Must be <= Price when set. │ Must be <= Price when set.
├── RetailPrice (decimal?) — nullable MRP the store charges the end consumer; informational,
│ not enforced on sales. Must be >= Price when set.
├── AgencyMarginPercent (decimal?) — nullable, 0-100, the regional agency's commission % of Price;
│ informational only, feeds agency settlement reporting.
├── IsActive, CreatedAt — from Template ├── IsActive, CreatedAt — from Template
``` ```
Unique constraint on `(PriceListId, ItemId)` — one price per item per price list. Unique constraint on `(PriceListId, ItemId)` — one price per item per price list.
@@ -138,10 +172,11 @@ One migration needed:
### PUT `/api/price-lists/{id}/items/{sku}` ### PUT `/api/price-lists/{id}/items/{sku}`
```json ```json
{ "price": 125.50, "minPrice": 115.00 } { "price": 125.50, "minPrice": 115.00, "retailPrice": 150.00, "agencyMarginPercent": 15 }
``` ```
Resolves `sku` to `ItemId`, upserts the `PriceListItem` row. `minPrice` is optional — omit or send Resolves `sku` to `ItemId`, upserts the `PriceListItem` row. `minPrice`, `retailPrice`, and
`null` for a fixed, non-negotiable price. Rejected with 409 if `minPrice > price`. `agencyMarginPercent` are all optional — omit or send `null` for none. Rejected with 409 if
`minPrice > price`, `retailPrice < price`, or `agencyMarginPercent` is outside `[0, 100]`.
### POST `/api/suppliers` ### POST `/api/suppliers`
```json ```json
@@ -178,6 +213,14 @@ Resolves `sku` to `ItemId`, upserts the `PriceListItem` row. `minPrice` is optio
`[MinPrice ?? Price, Price]` for the item's resolved `PriceListItem` (region-scoped, then `[MinPrice ?? Price, Price]` for the item's resolved `PriceListItem` (region-scoped, then
global). The `Item.SalePrice` fallback has no `MinPrice` — always fixed, no negotiation. An global). The `Item.SalePrice` fallback has no `MinPrice` — always fixed, no negotiation. An
out-of-band price, or no resolvable price at all, rejects the whole order (400). out-of-band price, or no resolvable price at all, rejects the whole order (400).
8. `PriceListItem.RetailPrice`, when set, must be `>= Price` — enforced on every write via
`PUT /api/price-lists/{id}/items/{sku}` (409 if violated); a store must never be expected to
sell below what it paid.
9. `PriceListItem.AgencyMarginPercent`, when set, must be within `[0, 100]` — enforced the same
way (409 if violated).
10. Neither `RetailPrice` nor `AgencyMarginPercent` participates in order pricing/validation
(rule 7) — both are informational only, carried through `PriceResolution` for display and
reporting, and are `null` on the `Item.SalePrice` fallback path.
--- ---
@@ -311,9 +354,14 @@ POST /api/price-lists
→ PriceList A (RegionId = North) → PriceList A (RegionId = North)
PUT /api/price-lists/{A.Id}/items/COKE-330 PUT /api/price-lists/{A.Id}/items/COKE-330
{ "price": 90, "minPrice": 80 } { "price": 90, "minPrice": 80, "retailPrice": 120, "agencyMarginPercent": 15 }
``` ```
This publishes a band for `COKE-330` in the North region: ceiling 90, floor 80. This publishes a band for `COKE-330` in the North region: ceiling 90, floor 80. It also records that
the store is expected to retail it at 120 (so the store's own margin is 90–120, or up to 40 if the
rep negotiated down to 80), and that the North region's agency retains 15% of whatever wholesale
price was actually charged (e.g. 15% of 90 = 13.50 if sold at the ceiling) as their distribution
commission — informational only, used for agency settlement reporting, not deducted automatically
from anything.
**Order at the default (no negotiation):** **Order at the default (no negotiation):**
``` ```
+32
View File
@@ -0,0 +1,32 @@
Module,Feature,Description
Auth,Authentication,"Handles user login, JWT token issuance, refresh tokens, and session management."
User,User Management,"Create, update, and manage system users, their roles, and account status."
Role,Role Management,"Define roles and manage permission assignments for access control."
Brand,Brand Management,"Maintain product brands used to categorize items."
Category,Category Management,"Maintain product categories for organizing items."
Uom,Unit of Measure (UOM),"Manage units of measure (e.g., box, piece, kg) used for items and conversions."
Item,Item Management,"Manage product/item master data including pricing, stock units, and attributes."
PriceList,Price List Management,"Create and manage price lists with item-wise pricing for customers/regions, including view and update flows."
Scheme,Scheme Management,"Manage promotional schemes/discounts applicable to items or orders."
Stock,Stock Management,"Track and manage inventory stock levels across warehouses."
Warehouse,Warehouse Management,"Manage warehouse master data and locations for stock storage."
Store,Store Management,"Manage store/outlet master data for customers or sales points."
Supplier,Supplier Management,"Manage supplier master data used for purchase orders and GRNs."
SupplierClaims,Supplier Claims Management,"Manage claims raised against suppliers, including claim creation, viewing, and settlement."
Claims,Claims Management,"General claims management module for tracking and settling claims."
PurchaseOrder,Purchase Order Management,"Create and manage purchase orders placed with suppliers."
PurchaseReturn,Purchase Return Management,"Manage returns of purchased goods back to suppliers."
Grn,Goods Receipt Note (GRN),"Record and manage receipt of goods from suppliers against purchase orders."
Order,Order Management,"Manage customer sales orders from creation through fulfillment."
Sale,Sales Management,"Manage sales transactions and related sales records."
Returns,Sales Returns Management,"Manage return of goods from customers after sale/delivery."
Payment,Payment Management,"Record and track payments received/made against orders, sales, or claims."
Region,Region Management,"Manage geographic regions used for organizing routes, stores, and sales areas."
Route,Route Management,"Manage delivery/sales routes used for planning rides and deliveries."
Ride,Ride Management,"Manage delivery rides/trips assigned to drivers and vehicles."
DeliveryRide,Delivery Ride Tracking,"Track delivery ride execution, status, and progress."
LoadingEvent,Loading Event Tracking,"Track vehicle loading events for goods dispatched on rides."
Vehicle,Vehicle Management,"Manage vehicle master data used for deliveries."
DriverProfile,Driver Profile Management,"Manage driver master data and profile details."
DriverComplianceConfig,Driver Compliance Configuration,"Configure compliance rules and requirements for drivers."
Sync,Data Synchronization,"Handle data sync operations, likely for offline/mobile clients syncing with the server."
1 Module Feature Description
2 Auth Authentication Handles user login, JWT token issuance, refresh tokens, and session management.
3 User User Management Create, update, and manage system users, their roles, and account status.
4 Role Role Management Define roles and manage permission assignments for access control.
5 Brand Brand Management Maintain product brands used to categorize items.
6 Category Category Management Maintain product categories for organizing items.
7 Uom Unit of Measure (UOM) Manage units of measure (e.g., box, piece, kg) used for items and conversions.
8 Item Item Management Manage product/item master data including pricing, stock units, and attributes.
9 PriceList Price List Management Create and manage price lists with item-wise pricing for customers/regions, including view and update flows.
10 Scheme Scheme Management Manage promotional schemes/discounts applicable to items or orders.
11 Stock Stock Management Track and manage inventory stock levels across warehouses.
12 Warehouse Warehouse Management Manage warehouse master data and locations for stock storage.
13 Store Store Management Manage store/outlet master data for customers or sales points.
14 Supplier Supplier Management Manage supplier master data used for purchase orders and GRNs.
15 SupplierClaims Supplier Claims Management Manage claims raised against suppliers, including claim creation, viewing, and settlement.
16 Claims Claims Management General claims management module for tracking and settling claims.
17 PurchaseOrder Purchase Order Management Create and manage purchase orders placed with suppliers.
18 PurchaseReturn Purchase Return Management Manage returns of purchased goods back to suppliers.
19 Grn Goods Receipt Note (GRN) Record and manage receipt of goods from suppliers against purchase orders.
20 Order Order Management Manage customer sales orders from creation through fulfillment.
21 Sale Sales Management Manage sales transactions and related sales records.
22 Returns Sales Returns Management Manage return of goods from customers after sale/delivery.
23 Payment Payment Management Record and track payments received/made against orders, sales, or claims.
24 Region Region Management Manage geographic regions used for organizing routes, stores, and sales areas.
25 Route Route Management Manage delivery/sales routes used for planning rides and deliveries.
26 Ride Ride Management Manage delivery rides/trips assigned to drivers and vehicles.
27 DeliveryRide Delivery Ride Tracking Track delivery ride execution, status, and progress.
28 LoadingEvent Loading Event Tracking Track vehicle loading events for goods dispatched on rides.
29 Vehicle Vehicle Management Manage vehicle master data used for deliveries.
30 DriverProfile Driver Profile Management Manage driver master data and profile details.
31 DriverComplianceConfig Driver Compliance Configuration Configure compliance rules and requirements for drivers.
32 Sync Data Synchronization Handle data sync operations, likely for offline/mobile clients syncing with the server.
@@ -0,0 +1,196 @@
# [Refactor] Order/Invoice Lifecycle — Unified PO → Invoice Pipeline
**Status**: Proposed (design only — no code changes yet)
**Refactors**: [../05_delivery_ride.md](../05_delivery_ride.md) and
[../06_sale_ride.md](../06_sale_ride.md) (both kept as-is, this is the target design)
**Related but independent**: [ride_lifecycle.md](./ride_lifecycle.md) — that refactor removes
`RideType`/`Route`/`Status` from `Ride`; this document does not attempt to resolve its open
questions, and this document's design does not depend on it landing first.
---
## Goal
Replace the Delivery-vs-Sale ride-type split as the organizing concept for how a store's order gets
turned into money. Today `05_delivery_ride.md` (rep takes an order now, delivers days later) and
`06_sale_ride.md` (rep+driver visit together and sell on the spot) are two separate flows. Underneath,
both are the same shape: a rep collects a **PO** from the shop — whether fulfilled immediately or
days later — and once the PO exists, an **Invoice** is generated from it. That invoice always moves
through the same three statuses regardless of which scenario produced it:
1. **Pending** — invoice just created from the PO; the PO's line items are freely editable.
2. **Draft** — the customer signs once the order is confirmed, moving it out of Pending. The PO's
line items **remain editable** in this state too — Draft is a working document, not a lock.
3. **Finalize** — the reconciliation step: item add/delete and amount deductions (returns, damage,
short-delivery, discounts) get applied here, producing the final, locked invoice amount. This is
the only state where free-form editing stops.
The two ride scenarios differ only in *timing* through this one pipeline, not in structure — no
ride-type-specific branching is needed in the PO/Invoice model itself.
---
## Data Model Changes
### Reshaped Entity — `Order` (business term: "PO")
The existing `Order` entity ([Order.cs](../../Models)) is reshaped into this role, keeping its class
name as `Order` in code.
> **Naming note**: `Distribution/Models/PurchaseOrder.cs` and `Distribution/Features/PurchaseOrder/`
> already exist in the codebase — that's the *supplier-facing* procurement flow (the distributor
> buying from its own suppliers), completely unrelated to this store-facing PO. To avoid confusion,
> this document deliberately keeps the class name `Order`, using "PO" only as the business-facing
> term for what the store ordered. Do **not** name the reshaped entity/class `PurchaseOrder`.
```
Order ("PO")
├── Id (Guid) — PK, from Template
├── FK_StoreId (Guid) — required, unchanged
├── FK_SaleId (Guid?) — now OPTIONAL (was required). See Impact section — a PO can now
│ exist before any Sale/payment event completes.
├── TotalAmount (decimal) — unchanged, though only authoritative once the linked Invoice is
│ Finalized (see below)
├── Status (OrderStatus) — unchanged: Pending, PartiallyFulfilled, Fulfilled, Cancelled.
│ This tracks fulfillment progress, a separate axis from the new
│ InvoiceStatus below — not merged in this pass.
├── FulfilledAt (DateTime?) — unchanged
├── IsActive, CreatedAt — from Template
```
`OrderItem` (the PO's line items) is unchanged in shape (`FK_ItemId`, `Quantity`,
`QuantityFulfilled`, `UnitPrice`) — editability is now gated by the linked `Invoice`'s status (see
below), not by `Order.Status` directly.
### New Entity — `Invoice`
```
Invoice
├── Id (Guid) — PK, from Template
├── FK_OrderId (Guid) — required. One PO → one Invoice for v1 (see Open Questions).
├── Status (InvoiceStatus) — Pending, Draft, Finalized
├── SignedAt (DateTime?) — set on Pending → Draft
├── FinalizedAt (DateTime?) — set on Draft → Finalized
├── FinalizedBy (Guid?) — UserId of whoever finalized it
├── TotalAmount (decimal) — mirrors the PO total while Pending/Draft; becomes the
│ authoritative, locked amount once Finalized
├── IsActive, CreatedAt — from Template
```
### New Enum — `InvoiceStatus`
```csharp
public enum InvoiceStatus { Pending, Draft, Finalized }
```
An `Invoice` is created automatically the moment a PO (`Order`) is created, starting at `Pending` —
there is no separate "create invoice" action.
---
## State Machine
```
Pending → Draft → Finalized
```
No branch/cancel path is designed in this pass (unlike the Ride state machine) — cancellation of a
PO/Invoice is not addressed here; flagged as an open question below.
| Transition | Trigger | What it does |
|---|---|---|
| (implicit) → Pending | PO (`Order`) created | Invoice is created automatically, `Status = Pending`. `OrderItem` lines fully editable. |
| Pending → Draft | `POST /api/invoices/{id}/draft` | Captures customer sign-off. Sets `SignedAt`. **`OrderItem` lines remain editable** — Draft is not a lock. Signature/OTP mechanism itself is an open question (see below), matching `05_delivery_ride.md`'s own prior deferral on customer confirmation. |
| Draft → Finalized | `POST /api/invoices/{id}/finalize` | Accepts an item-adjustment/deduction payload (add/delete lines, amount deductions for returns/damage/short-delivery/discounts), recomputes and locks `TotalAmount`. This is a distinct, audited action — not the same add/edit/remove path used in Pending/Draft. Once Finalized, the Invoice and its PO's items are fully locked. |
---
## Business Rules
1. `OrderItem` add/edit/remove is allowed while the linked `Invoice.Status` is `Pending` **or**
`Draft` — both are working-document states.
2. An `Invoice` cannot be `Finalized` without first passing through `Draft` — `Pending → Finalized`
directly is not a valid transition.
3. `Order.TotalAmount`/`Invoice.TotalAmount` is only authoritative once `Finalized`; while
Pending/Draft it reflects the current (still-changeable) line items.
4. `Order.Status` (fulfillment tracking: Pending/PartiallyFulfilled/Fulfilled/Cancelled) is
untouched and operates on a separate axis from `Invoice.Status` (billing/document lifecycle) —
whether these two should eventually merge is an open question, not resolved here.
5. `Order.FK_SaleId` is now optional — a PO/Invoice can exist and progress through Pending/Draft
before any `Sale` is recorded, covering the take-order-now/deliver-later scenario. See Impact
section for what this touches.
6. Both ride scenarios (take-order-now-deliver-later vs. on-the-spot sale) create a PO the same way
and pass through the same three Invoice statuses — the only difference is how quickly they move
through Pending → Draft → Finalized. No `RideType`-specific branching applies to the PO/Invoice
model.
---
## Impact on Dependent Features
Reshaping `Order` and introducing `Invoice` touches several existing behaviors. Each is flagged here
as **known-affected, deferred follow-up work**, not solved in this pass:
1. **`SaleService.CreateAsync`** — currently the only path that creates an `Order`, and it always
sets a non-null `FK_SaleId`. With `FK_SaleId` now optional, a new PO-creation path (not
necessarily originating from a `Sale`) needs to exist for the take-order-now/deliver-later
scenario. Any other code that assumes `Order.Sale` is always non-null also needs auditing —
not enumerated exhaustively here.
2. **`Payment` attachment** — per `06_sale_ride.md`, `Payment` currently attaches to `Sale`. Once
`Invoice` exists as the real billing document, `Payment` more naturally attaches to the `Invoice`
instead. Flagged as a design question for [19_finance_accounting.md](../19_finance_accounting.md)
to reconcile — not decided here.
3. **`19_finance_accounting.md`'s proposed Invoice status** (`Issued, PartiallyPaid, Paid`) needs
reconciling with this document's `Pending/Draft/Finalized`. These are likely two different axes
on the same `Invoice` — document lifecycle status (this doc) vs. payment-collection status (the
finance doc) — but that reconciliation is not attempted here.
4. **`DeliveryTask.CustomerConfirmed`** (boolean, from `05_delivery_ride.md`) overlaps conceptually
with the new Pending → Draft transition (both represent "the customer confirmed this"). Whether
they merge into one confirmation event, or stay deliberately separate (delivery confirmation vs.
invoice sign-off, which could happen at different times), is not decided here.
5. **`05_delivery_ride.md` and `06_sale_ride.md`** both go stale the moment this design ships and
need their own rewrite/retirement — tracked as follow-up, not attempted in this pass.
None of the above are implemented or redesigned by this document. They're called out so the gap is
visible and intentional rather than discovered later as a silent regression.
---
## Open Questions
1. **Signature/OTP mechanism for the Pending → Draft transition** — same deferral as
`05_delivery_ride.md`'s original "signature (image) or OTP (SMS code) — one, or both, in v1?"
question. Not resolved here.
2. **One Invoice per PO, or multiple?** — this document assumes a 1:1 `Order`→`Invoice` relationship
for v1. Whether a PO could ever need multiple invoices (e.g. split billing) is not designed here.
3. **Should `Order.Status` and `Invoice.Status` eventually merge into one status axis?** — kept
deliberately separate in this pass (fulfillment progress vs. document/billing lifecycle); not
decided whether that separation should be permanent.
4. **How does `Payment` attach once `Invoice` exists?** — see Impact item 2. Not designed here.
5. **Is there a cancellation/void path for a PO or Invoice?** — the state machine in this document
has no cancel branch (unlike the Ride state machine's `CancelRequested`/`Cancelled`). Not
addressed here.
6. **Does `DeliveryTask.CustomerConfirmed` merge with the Draft transition?** — see Impact item 4.
---
## Migration Notes (for when this is implemented)
Not part of this docs-only pass, but captured for the eventual implementation step:
- Make `Order.FK_SaleId` nullable in `Models/Order.cs` and its EF/Fluent configuration; audit all
code paths that currently assume it's non-null.
- Add `Models/Invoice.cs` (`FK_OrderId`, `Status`, `SignedAt`, `FinalizedAt`, `FinalizedBy`,
`TotalAmount`, plus `Template` base fields) and the `InvoiceStatus` enum.
- New `Features/Invoice/` folder (`InvoiceController`, `InvoiceService`, `IInvoiceRepository`,
`InvoiceRepository`, `InvoiceDTOs`) — do not place this under `Features/PurchaseOrder/` or reuse
any of its DTOs/names, to avoid the supplier-PO naming collision flagged above.
- `POST /api/invoices/{id}/draft` and `POST /api/invoices/{id}/finalize` endpoints; the finalize
endpoint's adjustment/deduction payload should reuse the `CreditNote` shape from
[18_returns_claims.md](../18_returns_claims.md) where applicable rather than inventing a parallel
mechanism.
- A new PO-creation path independent of `SaleService.CreateAsync`, to support creating an
`Order`/`Invoice` before any `Sale` exists (Impact item 1) — needs its own design, not specified
here.
- Resolve Open Questions 1–2 before implementing the Draft-transition endpoint and the
one-Invoice-per-PO assumption respectively.
@@ -0,0 +1,221 @@
# [Refactor] Ride Lifecycle — Route-Free, Type-Free Rides
**Status**: Proposed (design only — no code changes yet)
**Refactors**: [../03_ride_lifecycle.md](../03_ride_lifecycle.md) (kept as-is, this is the target design)
**Depends On**: 02 — Stock Ledger
---
## Goal
Ride should not require a `Route`, should not carry a `RideType`, should not be gated by a warehouse's `Type`, and should not even pin down a source warehouse on the ride record itself. Per direction from the business owner, the ride record carries only what a real dispatch needs to track — **"a ride only carries vehicle, driver assign, and meter reading from start to end."** Everything route-shaped (which stores get visited, which orders/items travel on this trip) and everything warehouse-shaped (where stock loads from/returns to) moves out of the Ride entity's responsibility entirely; neither is replaced by another matching mechanism here.
This is a genuine pivot from [03_ride_lifecycle.md](../03_ride_lifecycle.md), where `Route` and `FK_SourceWarehouseId` are both **required** on every ride, and `Route` is the *sole* mechanism two other features use to resolve "which orders belong to this ride." See [Impact on Dependent Features](#impact-on-dependent-features) below — those gaps are flagged, not solved, in this pass. The Draft → Open → Active → Closed workflow and the cancel-request flow are kept — only the fields that used to back warehouse reconciliation and ride classification are removed; how those transitions work without them is intentionally left as an open question (see below), not resolved by this document.
---
## Data Model Changes
### Updated Entity — Ride (`DeliveryRun`)
```
Ride (DeliveryRun)
├── Id (Guid) — PK, from Template
├── FK_VehicleId (Guid) — required
├── FK_DriverProfileId (Guid) — required
├── Meterstart (int)
├── Meterend (int)
├── StartedAt (DateTime?) — set when Draft → Open
├── ActivatedAt (DateTime?) — set when Open → Active
├── ClosedAt (DateTime?) — set when Active → Closed
├── IsActive, CreatedAt — from Template
```
That's the whole record: a vehicle, a driver, a meter start/end reading, and the three workflow
timestamps. Everything else is removed:
- **`FK_RouteId`** — gone (see the original goal above; unchanged from the first pass of this doc).
- **`RideType`** — gone. Nothing in the Ride record itself needs to distinguish Delivery from Sale
once the ride no longer drives order/task auto-population (that was the only place `RideType`
mattered inside the Ride entity's own logic — see
[Impact on Dependent Features](#impact-on-dependent-features)).
- **`Status` (`RideStatus`)** — gone as a stored column. The workflow stages (Draft, Open, Active,
CancelRequested, Cancelled, Closed) are **kept as a concept** — see
[State Machine](#state-machine) — but this document does not resolve how "current state" is
represented without a stored enum. Left as an open question (see below), not solved here.
- **`FK_SourceWarehouseId`** — gone. The ride record no longer pins down a warehouse to load from
or reconcile stock against at all. `Warehouse.Type` accordingly becomes moot for rides (there's no
ride-side warehouse selection left to gate); the enum itself is untouched on `Warehouse`.
### Warehouse
No changes to the `Warehouse` entity — `Warehouse.Type` (`WareHouseType`: `DistributionCenter`,
`FulfillmentCenter`, `CrossDocking`, `ColdStorage`, `BondedWarehouse`, `AutomatedWarehouse`) stays
exactly as-is; it's still useful for warehouse management/reporting elsewhere in the system. What's
different from [03_ride_lifecycle.md](../03_ride_lifecycle.md) is that **rides no longer reference
a warehouse at all**, so there's nothing on the ride side left to filter or gate by `Type` — this
supersedes the first pass of this document, which kept `FK_SourceWarehouseId` and only dropped the
type constraint on it. See [Impact on Dependent Features](#impact-on-dependent-features) for what
that means for stock reconciliation, which previously depended on `FK_SourceWarehouseId`.
---
## State Machine
The workflow stages and endpoints are **kept** from [03_ride_lifecycle.md](../03_ride_lifecycle.md#state-machine)
— none of the transitions reference `Route`, `RideType`, or a warehouse by name. What's removed is
the *storage*: there is no `Status` column to hold "which stage is this ride in." This document does
not resolve what replaces it (e.g., deriving state purely from which timestamp fields are set —
`StartedAt` null = Draft, `StartedAt` set + `ActivatedAt` null = Open, etc. — is one option, but not
decided here). Flagged as [Open Question 4](#open-questions).
```
Draft → Open → Active → Closed
↘ CancelRequested → Cancelled
```
| Transition | Trigger | What it does |
|---|---|---|
| Draft → Open | `POST /api/rides/{id}/open` | Validates driver compliance. Checks vehicle has no other Open/Active ride. Sets `StartedAt`. |
| Open → Active | `POST /api/rides/{id}/activate` | Called by loading event confirmation. Sets `ActivatedAt`. |
| Active → Closed | `POST /api/rides/{id}/close` | Called after ride reconciliation. Sets `ClosedAt`. Previously required "all stock returned to source warehouse" — with no `FK_SourceWarehouseId` on the ride, this check has nothing to reconcile against; see [Impact on Dependent Features](#impact-on-dependent-features). |
| Active → CancelRequested | `POST /api/rides/{id}/cancel-request` | Driver submits cancel request with type code and reason. |
| CancelRequested → Cancelled | `POST /api/rides/cancel-requests/{id}/approve` | Supervisor approves. Previously required stock returned to source warehouse first — same gap as Close above. |
| CancelRequested → Active | `POST /api/rides/cancel-requests/{id}/reject` | Supervisor rejects. Ride returns to `Active`. |
---
## Business Rules
Carried over from [03_ride_lifecycle.md](../03_ride_lifecycle.md#business-rules), with the
route-dependent rules removed:
1. A ride cannot transition Draft → Open if `IsActive = false` on the driver profile, or (when the
relevant `DriverComplianceConfig` flags are enabled) if license/medical clearance is expired.
2. A vehicle cannot be on more than one Open or Active ride at a time.
3. A CancelRequested ride cannot close or activate until the request is resolved.
4. All cancel requests must have a `TypeCode` — free text reason alone is not sufficient.
5. All list endpoints return paginated responses.
6. **A ride carries no order, task, route, or warehouse linkage of its own.** Creating a ride never
touches `Order`, `DeliveryTask`, `LoadingSheet`, or `StockBalance`/`StockMovement` records —
those remain entirely separate concerns, attached (if at all) by whatever future mechanism
replaces route-derived linkage and warehouse reconciliation.
**Removed rules** (no longer apply once `Route`, `RideType`, `Status`, and `FK_SourceWarehouseId`
are gone):
- ~~"A ride uses a single route for now."~~
- ~~"Orders can only be assigned to a Delivery ride while it is in Draft status."~~ — there is no
order-assignment mechanism on the ride at all anymore, in Draft or otherwise.
- ~~"Loading and unloading warehouse must be the same — enforced at close/cancel."~~ — there is no
warehouse on the ride record to compare "loading" against "unloading" anymore. See
[Impact on Dependent Features](#impact-on-dependent-features).
---
## API Endpoints
Same routes as [03_ride_lifecycle.md](../03_ride_lifecycle.md#api-endpoints), with `routeId`,
`rideType`, and `sourceWarehouseId` removed from the request/response shapes:
| Method | Route | Description |
|---|---|---|
| POST | `/api/rides` | Create ride (Draft): `{ vehicleId, driverProfileId }`. Nothing else required. |
| GET | `/api/rides` | List rides (paginated). Filter by driverProfileId, vehicleId. (Filtering by `status`/`type` depends on how [Open Question 4](#open-questions) resolves.) |
| GET | `/api/rides/{id}` | Get ride detail — response no longer includes `routeId`/`routeName`, `rideType`, or `sourceWarehouseId`/`sourceWarehouseName`. |
| PUT | `/api/rides/{id}/driver` | Assign or change driver profile |
| POST | `/api/rides/{id}/open` | Transition Draft → Open |
| POST | `/api/rides/{id}/activate` | Transition Open → Active |
| POST | `/api/rides/{id}/close` | Transition Active → Closed |
| POST | `/api/rides/{id}/cancel-request` | Submit cancel request (Active rides only) |
| GET | `/api/rides/cancel-requests` | List cancel requests (paginated). Filter by status. |
| POST | `/api/rides/cancel-requests/{id}/approve` | Approve cancel request |
| POST | `/api/rides/cancel-requests/{id}/reject` | Reject cancel request |
| GET | `/api/rides/breakdown-assist/{rideId}` | See [Impact on Dependent Features](#impact-on-dependent-features) — this endpoint currently keys off the stranded ride's route and needs rework. |
`DriverComplianceConfig` and `DriverProfile` endpoints are unaffected by this refactor.
---
## Impact on Dependent Features
Removing `FK_RouteId`, `RideType`, `Status`, and `FK_SourceWarehouseId` from `Ride` is not a purely
additive/subtractive field change — several existing behaviors currently derive their entire input
from these fields, and none of them get a replacement in this pass. Each is flagged here as
**known-broken, deferred follow-up work**, not solved:
1. **Delivery task generation** (`Features/DeliveryRide/DeliveryTaskService.GenerateAsync`) —
currently calls `orderRepo.GetPendingByRouteIdAsync(ride.FK_RouteId)` to auto-create delivery
tasks for a Delivery ride. With no route (and no `RideType` to even tell Delivery from Sale) on
the ride, this call has nothing to key off. Needs a redesign once "how orders attach to a ride"
is decided — out of scope here.
2. **Loading sheet auto-population** (`Features/LoadingEvent/LoadingSheetService` +
`LoadingSheetRepository.GetAggregatedOrderItemsByRouteIdAsync`) — auto-fills a Delivery ride's
loading-sheet items by aggregating orders on `Order.Store.FK_RouteId == ride.FK_RouteId`. Same
gap as above; documented in [04_loading_event.md](../04_loading_event.md), which will need its
own refactor pass once this lands.
3. **Breakdown-assist endpoint** (`RideService.GetBreakdownAssistAsync`) — uses
`strandedRide.FK_RouteId` to list the stranded route's open orders for manual reassignment. With
no route, this listing has no scope; the endpoint's shape needs to change (e.g., list all open
orders regardless of route, or drop this listing) — not decided here.
4. **Stock reconciliation at Close/Cancel** — [03_ride_lifecycle.md](../03_ride_lifecycle.md) closes
or cancels a ride only once "all stock is returned to source warehouse," checked against
`FK_SourceWarehouseId`. With no warehouse on the ride, there is nothing to reconcile the vehicle's
stock balance against at all. This is the biggest open gap in this pass — closing/cancelling a
ride previously guaranteed the vehicle ends up empty (stock-wise); without a target warehouse,
that guarantee has no mechanism to enforce it. Needs its own design — not attempted here.
5. **[05_delivery_ride.md](../05_delivery_ride.md)** describes the whole order-to-ride relationship
as `Ride → Route → Stores → Pending Orders`. That document goes stale the moment this refactor
ships and needs its own rewrite — tracked as follow-up, not attempted in this pass.
6. **[04_loading_event.md](../04_loading_event.md)**'s loading-event flow currently records which
warehouse stock was loaded from via the ride's `FK_SourceWarehouseId`; without that field, each
loading event presumably needs to carry its own warehouse reference directly instead of inheriting
it from the ride — flagged, not designed, here.
None of the above are implemented or redesigned by this document. They're called out so the gap is
visible and intentional rather than discovered later as a silent regression.
---
## Open Questions
1. **How does a Delivery ride's orders/tasks/items get attached, if not via Route?** —
Deliberately unresolved. Per the business owner's direction, the Ride entity itself should not
carry that responsibility going forward; whatever mechanism replaces route-derived linkage (if
any) is a separate design effort, not part of slimming down the Ride record.
2. **What happens to `GetBreakdownAssistAsync`'s route-scoped order listing?** — Needs its own
decision once (1) is resolved; not designed here.
3. **`04_loading_event.md` / `05_delivery_ride.md` rewrite** — scheduled as follow-up work once (1)
is resolved; explicitly out of scope for this document.
4. **How is a ride's current workflow stage tracked without a stored `Status` column?** — The
Draft/Open/Active/CancelRequested/Cancelled/Closed stages are kept conceptually (see
[State Machine](#state-machine)), but this document doesn't decide whether state is derived from
the existing timestamp fields (`StartedAt`/`ActivatedAt`/`ClosedAt` plus the presence of an open
`RideCancelRequest`), computed some other way, or whether a status-like field comes back under a
different name. Left for the business owner/implementer to resolve.
5. **What replaces stock-return enforcement at Close/Cancel without `FK_SourceWarehouseId`?** — See
[Impact on Dependent Features](#impact-on-dependent-features) item 4. Not designed here.
6. **Does `RideType` (Delivery vs Sale) need to exist somewhere else** — e.g. on whatever
loading-event or order-linkage mechanism eventually replaces the route-derived flow — even though
it's removed from `Ride` itself? Not decided here.
---
## Migration Notes (for when this is implemented)
Not part of this docs-only pass, but captured for the eventual implementation step:
- Drop the `FK_RouteId`, `RideType`, `Status`, and `FK_SourceWarehouseId` columns from
`DeliveryRun`/`Ride` and their Fluent/EF configuration.
- Remove `RouteId`, `RideType`, `Status`, and `SourceWarehouseId`/`SourceWarehouseName` from
`CreateRideRequest`, `RideResponse`, and `BreakdownAssistRideResponse` in `RideDTOs.cs`.
- Remove the `.Include(r => r.Route)`/`.Include(r => r.SourceWarehouse)` calls and
`GetActiveOrdersByRouteIdAsync` usage in `RideRepository.cs`/`RideService.cs` once the
breakdown-assist redesign (Open Question 2) lands.
- Remove the required `Route` and `Source Warehouse` `<Select>` fields (and the `RideType` picker)
from `ride-sheet.tsx` (frontend create form).
- `DeliveryTaskService.GenerateAsync` and `LoadingSheetService`'s route-based aggregation need a
redesign or removal before this ships — this is the load-bearing blocker, not a cleanup item.
- Resolve Open Question 4 (how workflow stage is tracked without `Status`) before touching the
`RideController`/`RideService` transition methods — they currently branch on the stored enum.
- Resolve Open Question 5 (stock-return enforcement without a source warehouse) before touching
`Close`/`CancelRequest` approval logic in `RideService.cs`.
+55 -14
View File
@@ -6,20 +6,9 @@ Running log of backend API failures found during testing and the corresponding f
| # | Area | Backend API Failure | Frontend Change Needed | Date | | # | Area | Backend API Failure | Frontend Change Needed | Date |
|---|------|----------------------|--------------------------|------| |---|------|----------------------|--------------------------|------|
| 1 | Operations → Rides (Create Ride) | On create (not save) | Add a View icon to the action column in Operations → Rides table | 2026-09-01 |
| 2 | Operations → Loading Sheets | | Add a View icon to the action column in Operations → Loading Sheets table | 2026-09-01 | | 11 | Procurement → GRN | Record Receipt throws `var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();` (same #28 pattern, not yet given a temp workaround) | Added a View icon (see Resolved #44). No Edit/Delete — a GRN has no backend update/delete endpoint by design, since it already wrote stock balance + a `PurchaseReceipt` movement + the Po's `ReceivedQuantity` on create; reversing one safely needs a dedicated endpoint, not a plain CRUD delete. Still pending: the broken-auth throw on Record Receipt | 2026-09-02 |
| 3 | Sales (Create Sale) | Sale create not working | Update the Create Sale modal UI |Sale summary calculation not display | Add view icon to the action | 2026-09-01 |
| 4 | Orders | | Add a View icon to the action column in Orders table | 2026-09-01 |
| 5 | Payments (Record Payment) | Cannot add new payment | Add a View icon to the action column in Payments table | 2026-09-01 |
| 6 | Sales → Visits (Route dropdown) | Selecting a specific route in the route dropdown does not filter route sales — all routes still show; check-in also not working | Fix route filter and check-in on Sales Visits page | 2026-09-02 |
| 7 | Catalogue → Categories (Subcategories) | Added subcategories don't display in the FE | Fix subcategory list rendering | Add a View icon to the action column in Categories table | 2026-09-02 |
| 8 | Stock Balances | | Add a View icon to the action column in Stock Balances table; allow Edit | 2026-09-02 |
| 9 | Stock Movements | Some rows missing From/To locations — every movement should always have both | Add a View icon and an Edit icon to the action column in Stock Movements table | 2026-09-02 |
| 10 | Procurement → Orders (Place Order) | Placing a new order throws: `var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();` | Add action icons to the action column in Procurement Orders table | 2026-09-02 |
| 11 | Procurement → GRN | | Full module CRUD check pending (CRUD + action icons) | 2026-09-02 |
| 12 | Pricing | | Full module CRUD check pending (frontend + CRUD + action icons) | 2026-09-02 |
| 13 | Pricing → Schemes (Add Scheme modal) | | Add Scheme modal should show a rule-type-specific "value" input field that changes based on the selected Scheme Name/Rule Type (currently no dynamic value field for the chosen rule type) | 2026-09-02 | | 13 | Pricing → Schemes (Add Scheme modal) | | Add Scheme modal should show a rule-type-specific "value" input field that changes based on the selected Scheme Name/Rule Type (currently no dynamic value field for the chosen rule type) | 2026-09-02 |
| 14 | Suppliers | | Add a View icon and a Cancel icon to the action column in Suppliers table | 2026-09-02 |
| 15 | Driver Profiles | Delete doesn't work | Add a View icon to the action column in Driver Profiles table | 2026-09-02 | | 15 | Driver Profiles | Delete doesn't work | Add a View icon to the action column in Driver Profiles table | 2026-09-02 |
| 16 | Users | Delete action not working | Add View icon | 2026-09-02 | | 16 | Users | Delete action not working | Add View icon | 2026-09-02 |
| 17 | Users → Roles | | For now, hide the Add Role icon and the Edit/Cancel (deactivate) icons in the action column; keep only the View icon | 2026-09-02 | | 17 | Users → Roles | | For now, hide the Add Role icon and the Edit/Cancel (deactivate) icons in the action column; keep only the View icon | 2026-09-02 |
@@ -28,12 +17,64 @@ Running log of backend API failures found during testing and the corresponding f
| 20 | Network → Routes | Inactive status doesn't work properly | Check Manage Owner and Manage Stores actions; add View icon to the action column; check Cancel action too | 2026-09-02 | | 20 | Network → Routes | Inactive status doesn't work properly | Check Manage Owner and Manage Stores actions; add View icon to the action column; check Cancel action too | 2026-09-02 |
| 21 | Network → Vehicles | | Add a View icon to the action column; check Cancel action | 2026-09-02 | | 21 | Network → Vehicles | | Add a View icon to the action column; check Cancel action | 2026-09-02 |
| 22 | Network → Warehouses | Delete doesn't work | Add a View icon to the action column; check Inactive status | 2026-09-02 | | 22 | Network → Warehouses | Delete doesn't work | Add a View icon to the action column; check Inactive status | 2026-09-02 |
| 25 | Orders (CRUD gap) | Create/Update/Delete endpoints exist and work | Orders page is read-only in the frontend — only list/get are wired up; add Create/Update/Delete UI | 2026-09-02 |
| 26 | Pricing → Schemes (unused capability) | Eligibility check, spend/ROI analytics, and apply-scheme-to-order endpoints all exist | None of these are wired into the frontend — only basic scheme CRUD is; add UI for eligibility check, spend/ROI view, and applying a scheme to an order | 2026-09-02 |
| 27 | Misc missing Delete actions | Delete/soft-delete endpoints exist for Users, Stores, Price Lists, and Suppliers | No Delete UI wired up for these on their respective pages | 2026-09-02 |
| 28 | Auth (root cause of #10 and likely others) | The actually-registered middleware is `Distribution/Middleware/AuthMiddleware.cs` (`app.UseMiddleware<AuthMiddleware>()` in Program.cs) — a plain, symmetric-key JWT check, NOT the RSA-based `Infra/Authorization/Middleware/AuthMiddleware.cs`/`JwtMiddleware.cs`, which despite their doc comments claiming to be "reference-only," are actually unregistered/dead. The active middleware reads `config["Jwt:Key"]`, but `appsettings.json` only defines `Jwt:PublicKey` (RSA XML, used by AuthHex-issued tokens) — so `Jwt:Key` is always null, the whole validation block is skipped, and `context.User` is never set for ANY request, valid token or not. It also never attaches `role_id`/`role_level`/`username` claims the way the dead Infra middleware does. Net effect: `ICurrentUserService.UserId`/`RoleId` are always null, so every `currentUser.UserId ?? throw new UnauthorizedAccessException()` check fails (this is the actual cause of bug #10, Procurement → Orders) and every permission/role-level check silently fails too | Decision: keep `Distribution/Middleware/AuthMiddleware.cs` (outside Infra) as the one true auth flow going forward; delete the dead unregistered duplicates `Infra/Authorization/Middleware/AuthMiddleware.cs` and `JwtMiddleware.cs`. Then fix the active middleware to validate against `Jwt:PublicKey` (RSA), reject (not silently swallow) invalid/missing tokens, and attach `role_id`/`role_level`/`username` claims from the local User/Role row after validation | 2026-09-02 |
## Resolved ## Resolved
| # | Area | Backend API Failure | Frontend Change Needed | Fixed In | Date | | # | Area | Backend API Failure | Frontend Change Needed | Fixed In | Date |
|---|------|----------------------|--------------------------|----------|------| |---|------|----------------------|--------------------------|----------|------|
| | | | | | | | 1 | Operations → Rides (Create Ride) | Create Ride failed with "Vehicle not found" | — | Fixed DTO field naming mismatch | 2026-09-02 |
| 29 | Operations → Rides | Cancel-request API routes mismatched | Added View, Edit, and Cancel icons to Rides table | Fixed API routes + added action icons | 2026-09-02 |
| 30 | Operations → Delivery Tasks | Delivery task status update API mismatched | Added View, Confirm Delivery, and Mark as Failed icons | Fixed API routes + added action icons | 2026-09-02 |
| 32 | Sales (Create Sale, van sale) | If any item was short on the vehicle, the whole sale was rejected instead of selling what was available | — | Van sale now sells whatever quantity is on the vehicle per item and is always a clean Completed sale (no partial status) | 2026-09-03 |
| 33 | Operations → Rides (Close Ride) | No validation that meter end >= meter start | Close Ride dialog now enforces meter end >= meter start | Added backend + frontend validation | 2026-09-03 |
| 34 | Sales | — | No action icons on the Sales table | Added View and Cancel icons | 2026-09-03 |
| 2 | Operations → Loading Sheets | — | Missing View icon | Added View icon + details modal (status, dispatch/receipt confirmer + timestamps, item-level dispatched/received/resolved breakdown) | 2026-09-02 |
| 35 | Sales/Orders/Stores (scope-denied paths) | `Forbid()` crashed with `InvalidOperationException: No authenticationScheme was specified...` since no auth scheme is registered in Program.cs | — | Replaced all 6 `Forbid()` calls (SaleController, OrderController, StoreController) with `StatusCode(403)` | 2026-09-03 |
| 36 | Sales (Create Sale) — TEMPORARY workaround | Store-scope check and `currentUser.UserId` were always unresolved due to the broken auth middleware (#28), blocking every sale create | Added a required "Created By" user dropdown to the Create Sale modal | TEMP: bypassed store-scope check and added `CreatedByUserId` to `CreateSaleRequest` so the caller supplies it explicitly. Revert once auth (#28) is fixed | 2026-09-03 |
| 37 | Sales (Create Sale) | Every business-rule failure (store inactive, KYC threshold, price resolution failed, ride not found) silently returned a bare 403 with no explanation | — | Each failure now throws a specific `InvalidOperationException`, surfaced as a clear 400 error message in the create-sale toast | 2026-09-03 |
| 38 | Sales (Create Sale modal) | — | "Sale Summary" total box was frozen at Rs. 0 — `form.watch('items')` called inline doesn't subscribe the component to re-render on field changes | Replaced with `useWatch({ control, name: 'items' })`, which properly re-renders the total as items are entered | 2026-09-03 |
| 39 | Sales (Create Sale) | — | Moved Create Sale from a cramped modal to a dedicated `/sales/new` page: Sale Details and Items are each full-width, Items shown as an invoice-style table, and a compact Sale Summary card is right-aligned below the items | 2026-09-03 |
| 40 | Sales (Cancel Sale) — TEMPORARY workaround | `CancelAsync` still had the same broken store-scope check as Create (#36) — `HasStoreScopeAsync` always false due to broken auth middleware (#28), so cancel silently 404'd | — | TEMP: bypassed store-scope check in `CancelAsync`, same as Create. Revert once auth (#28) is fixed | 2026-09-03 |
| 3 | Sales (Create Sale) | Sale create always failed — `saleDate` was never sent, defaulted to year 0001, no price list ever active that date | Added a required Sale Date field to the Create Sale modal | Sent `saleDate` in the create payload; summary total was already computing correctly, just never reached the backend | 2026-09-03 |
| 6 | Sales → Visits (Route dropdown) | Selecting a specific route in the route dropdown does not filter route sales — all routes still show; check-in also not working | Route filter and check-in now work on the Sales Visits page | Fixed route filter and check-in (see TEMP `CheckedInByUserId` workaround, #40-style, pending real auth) | 2026-09-08 |
| 7 | Catalogue → Categories (Subcategories) | Added subcategories don't display in the FE | Added a View icon to the action column in Categories table | Fixed subcategory list rendering — root cause was `GetAllAsync` returning `CategorySummaryResponse`, which had no `subCategories` field at all | 2026-09-08 |
| 42 | Procurement → Orders (Place Order UI) | — | Moved Create Purchase Order from the `PurchaseOrderSheet` modal to a dedicated `/procurement/orders/new` page, matching the `/sales/new` layout: Order Details full-width card, Items as an invoice-style table, and a right-aligned Order Summary card below | Created `procurement/orders/new/page.tsx`, updated `PurchaseOrdersClient`'s "New Order" button to navigate there, removed the now-unused `purchase-order-sheet.tsx` | 2026-09-08 |
| 43 | Procurement → Orders (action icons) | Cancel API call was broken: frontend sent `PATCH /purchase-orders/{id}/cancel` with body `{cancelReason}`, but the backend only registers `POST .../cancel` and expects `{reason}` — the wrong HTTP verb alone would 405 | Added View (details modal, showing all lines with received/cancelled quantities) and Edit (a `/procurement/orders/[id]/edit` page, enabled only while the order is Issued and matching backend's `UpdatePoRequest` rule that already-received lines are locked). Delete was already present as the Cancel action (soft-cancel with a reason, per the no-hard-delete Implementation Guide rule) — relabeled "Delete order" | Fixed the cancel API client to `POST` with `{reason}` matching `CancelPoRequest`; added `purchaseOrders.update()`; created `PurchaseOrderViewSheet` and the Edit page | 2026-09-08 |
| 44 | Procurement → GRN (action icons) | — | Added a View icon (details modal showing warehouse, received date, and each line's accepted/rejected quantity). No Delete — deliberate, not a gap: a goods receipt already applied its effect (stock balance + `PurchaseReceipt` movement + the Po's `ReceivedQuantity`) at create time, so a generic delete would silently desync stock; reversing one needs a dedicated, explicit endpoint, which hasn't been requested/built | Created `GrnViewSheet`, wired a View row action into `GrnClient` | 2026-09-08 |
| 10 | Procurement (Create Purchase Order + Record Goods Receipt) | Both `POST /purchase-orders` and `POST /goods-receipts` (and the Po-scoped goods-receipts route) had `var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();` — with auth broken (#28) and no global exception handler registered, every Create Order / Record Receipt attempt crashed with an unhandled 500, not even a clean 401. This was a genuine, currently-broken business flow, not just a cosmetic gap | Added a "Created By" dropdown to `/procurement/orders/new` and a "Received By" dropdown to the Record Receipt (GRN) sheet | TEMP: added `CreatedByUserId` to `CreatePoRequest` and `ReceivedByUserId` to `CreateGrnRequest`/`CreateFlatGrnRequest`, same `currentUser.UserId ?? request.XxxUserId` fallback pattern as #36/#40/#8, with a clean `BadRequest` instead of the unhandled crash | 2026-09-08 |
| 23 | Returns & Claims Management | `ReturnController`, `ClaimController`, `SupplierClaimController` all exist and are fully implemented, but `Create`/`Settle`/`Approve`/`Reject` on all three had the same unhandled `?? throw new UnauthorizedAccessException()` crash as #10; `ClaimController` and `ReturnController` also had no `GetAll` endpoint at all, only per-order/per-agency lookups, so there was no way to list existing records for a page. None of the three had an Update/edit endpoint either | No frontend integration existed at all — built a new "Returns & Claims" nav group with 3 dedicated pages: **Order Returns** (`/returns-claims/order-returns` — list + View + Edit + a Record Return dialog that only allows returning items the selected order actually has, mode CreditDeduction/ItemExchange), **Agency Claims** (`/returns-claims/claims` — list + View + Edit (while Open) + Raise Claim + Settle), **Supplier Claims** (`/returns-claims/supplier-claims` — list + View + Edit (while Open) + Raise Claim scoped to a PO's received items + Approve/Reject). Edit scope per module: Order Return quantities/mode are applied to stock and the order total immediately on create with no approval step, so only each line's **reason code** is editable, not quantity/mode/items (same immutable-effect principle as Stock Movements, #9); Claims and Supplier Claims have no side effect until Settle/Approve, so their Details/Items are fully editable while still Open, locked out afterward. Purchase Returns (#24) already lives in-context inside the Purchase Order View sheet and was left as-is | Backend: added `GetAllAsync` to `IReturnRepository`/`IClaimRepository`/`ISupplierClaimRepository` + a `GET` list endpoint on each controller; fixed all 5 broken-auth crash sites (`ReturnController.Create`, `ClaimController.Settle`, `SupplierClaimController.Create`/`Approve`/`Reject`) with the same `CreatedByUserId`/`SettledByUserId`/`RaisedByUserId`/`ApprovedByUserId`/`RejectedByUserId` fallback pattern; added `PATCH /returns/{id}/reasons`, `PUT /claims/{id}` (blocked once Settled), `PUT /supplier-claims/{id}` (blocked once resolved). Frontend: added `saleReturns`/`claims`/`supplierClaims` to a new `lib/api/returns-claims.ts`, plus types in `types/api.ts` | 2026-09-08 |
| 24 | Procurement → Purchase Returns | `PurchaseReturnController.Create` had the same unhandled `?? throw new UnauthorizedAccessException()` crash as #10; also no `GetAll` endpoint exists — a return only ever makes sense in the context of one purchase order (must have a prior GoodsReceipt against it, and quantities are capped per-item at received-minus-already-returned), so there's no global "all returns" listing to build a standalone page around | Built the module in-context: the Purchase Order View sheet now shows a "Purchase Returns" section (reason, date, line items for every return already recorded against that order) plus a "Return Items" button (shown only once the order has any received quantity) that opens a Return dialog — item dropdown capped to each item's real receivable-and-not-already-returned quantity, reason, and a temporary "Created By" user dropdown | TEMP: added `CreatedByUserId` to `CreatePurchaseReturnRequest`, same fallback pattern; added `purchaseReturns` API client (`get`, `listByPurchaseOrder`, `create`); created `PurchaseReturnDialog`, wired into `PurchaseOrderViewSheet` | 2026-09-08 |
| 45 | Pricing → Price Lists (action icons) | `DeleteAsync` did a hard delete (`repo.Remove` → EF `Remove`), violating the no-hard-delete Implementation Guide rule (the repo method itself was already misleadingly named `Remove` but did set `IsActive = false` — the actual bug was `GetAllAsync` filtering `.Where(pl => pl.IsActive)`, hiding a deleted price list from the list entirely instead of showing it as Inactive, and `GetByIdAsync`/`UpdateAsync` blocking view/edit of an inactive one, matching the same recurring pattern as Stores/Items/Categories/Brands/Orders). Also `GetAll` returns a plain `List<PriceListResponse>` but the frontend was configured `serverSide: true` expecting a `PaginatedResponse`, so pagination silently did nothing | Added a View icon (details modal — fetches the full record incl. items via `GET /price-lists/{id}` since the list endpoint returns rows with an empty `items` array) and a Delete icon (soft-deactivate, reusing the existing "Active" checkbox in the Edit sheet to reactivate). Edit was already wired up and already sent `isActive` in its request body — just needed the backend to actually accept it | Removed the `IsActive` filters from `GetAllAsync`/`GetByIdAsync`/`UpdateAsync`; added `IsActive` to `UpdatePriceListRequest`; switched the frontend to client-side pagination (same fix as Orders/Brands) since the backend has no real paging; added `priceLists.remove()`; created `PriceListViewSheet` | 2026-09-08 |
| 46 | Pricing → Price Lists (item pricing) | `PUT /price-lists/{id}/items/{sku}` and `DELETE /price-lists/{id}/items/{sku}` were fully implemented and correctly validated (MinPrice ≤ Price, overlap check against other active lists) but **no frontend code called either endpoint** — the Create/Edit sheet only ever sent `items: []`. So every price list created through the UI was permanently empty, meaning `SaleService.ResolveUnitPriceAsync` could never resolve a real price-list price and always fell back to the flat `Item.SalePrice` — the whole point of the Price Lists module (region/date-scoped item pricing) was unreachable end-to-end | Added an "Items" section to the Edit sheet: a table of already-priced items (inline edit price/min-price, remove) plus an "Add item" row (item picker + price + min price). Since the backend upserts one SKU at a time (not a bulk array), each row change is its own immediate API call, same as the existing GRN/PO-style line-item tables but per-row-saved instead of submitted together. Creating a new price list now keeps the dialog open afterward (switching in place to "edit" mode) so items can be added immediately instead of requiring a reopen | Created `PriceListItemsSection`, embedded in `PriceListSheet` (edit mode only); added `priceLists.upsertItem()`/`priceLists.removeItem()` to the API client | 2026-09-09 |
| 47 | Sales (Create Sale) — wrong price prefilled | Even with #46 fixed, `/sales/new` still prefilled `unitPrice` straight from `item.salePrice` (the flat catalogue price) — no frontend code ever called price-list resolution, so a rep would see the wrong default price for any item actually covered by an active region/date-scoped price list, and the sale would only succeed if they happened to retype the exact resolved price manually. `PriceListService.ResolvePriceAsync` (region price list → global price list → `Item.SalePrice`) existed but was only ever called from inside `SaleService`, with no public endpoint to query it ahead of submission | Selecting an item now calls the new resolution endpoint (store → its route's region, so the exact same lookup `SaleService` uses) and prefills the real resolved price; changing the Store or Sale Date afterward re-resolves every already-selected line, since the applicable price list depends on both. When the resolved price has a `MinPrice` band, a "Negotiable down to Rs. X" hint shows under the field so the rep knows how far they can discount without the submit failing | Added `PriceListService.ResolveByStoreAsync(itemId, storeId, saleDate)` (looks up the store's region via `IStoreRepository.GetByIdWithRegionAsync`, then reuses `ResolvePriceAsync`) and `GET /price-lists/resolve?itemId=&storeId=&saleDate=`; added `priceLists.resolve()` to the frontend API client and a `PriceResolution` type | 2026-09-09 |
| 12 | Pricing → Schemes (action icons) | Same recurring pattern as Price Lists (#45): `DeleteAsync`'s `repo.Remove` already soft-deleted (`IsActive = false`), but `GetAllAsync`/`GetByIdAsync`/`UpdateAsync` filtered/blocked on `IsActive`, hiding a deleted scheme from the list entirely and blocking viewing/editing/reactivating it. `EvaluateEligibilityAsync` relied on that same repo-level filter to only ever consider active schemes as redemption candidates — removing the filter without care would have silently let inactive/deleted schemes be evaluated as eligible. Also `GetAll` returns a plain `List<SchemeResponse>` but the frontend was configured `serverSide: true` expecting a `PaginatedResponse`, so pagination silently did nothing | Added View (details modal — rule type, region, segment, date range, trigger/free-goods payout, bundle components) and Delete (soft-deactivate, reuses the existing "Active" checkbox in the Edit form to reactivate) icons | Removed the `IsActive` filter from `SchemeRepository.GetAllAsync`; moved the active-only filter into `SchemeService.EvaluateEligibilityAsync` itself (`.Where(s => s.IsActive)` on the fetched candidates) so eligibility evaluation is unaffected; removed the `IsActive` blocks from `GetByIdAsync`/`UpdateAsync`; added `IsActive` to `UpdateSchemeRequest`; switched the frontend to client-side pagination (same fix as Price Lists/Orders/Brands); added `schemes.remove()`; created `SchemeViewSheet` | 2026-09-09 |
| 14 | Suppliers (action icons) | Exact same recurring pattern again: `SupplierRepository.GetAllAsync` filtered `.Where(s => s.IsActive)` (hiding a deleted supplier from the list instead of showing it Inactive), `GetByIdAsync`/`UpdateAsync` blocked viewing/editing an inactive one, and `UpdateSupplierRequest` had no `IsActive` field so the Edit form's existing "Active" checkbox couldn't reactivate one. Also `suppliers.list()` was typed as `PaginatedResponse` against a backend `GetAll` that returns a plain array, same pagination-shape bug as Price Lists/Schemes/Orders/Brands | Added View (details modal — name, contact, status, performance score, created date) and Delete (soft-deactivate, reuses the existing "Active" checkbox in Edit to reactivate) icons | Removed the `IsActive` filter from `SupplierRepository.GetAllAsync`; removed the `IsActive` blocks from `GetByIdAsync`/`UpdateAsync` (kept on `EvaluateAsync`, a real business guard — no point recomputing a performance score for a deleted supplier); added `IsActive` to `UpdateSupplierRequest`; switched the frontend to client-side pagination; added `suppliers.remove()`; created `SupplierViewSheet` | 2026-09-09 |
| 48 | Pricing → Price Lists (new fields) | — | Added `RetailPrice` and `AgencyMarginPercent` to `PriceListItem`, alongside the existing `Price`/`MinPrice` band — see [14_pricing_and_suppliers.md](./14_pricing_and_suppliers.md) for the full business rules. `RetailPrice` is the MRP the store is expected to charge the end consumer (must be `>= Price`); `AgencyMarginPercent` is the regional agency's commission `%` of `Price` (must be `0-100`). Both are informational only — carried through `PriceResolution` for display/reporting, never enforced on order pricing | Added both columns to the Items table in the Price List Edit/View pages (add-row + inline edit + view-only table), and a "Retail price: Rs. X" hint under the unit price on Create Sale when the resolved price came from a real price list | Added `RetailPrice`/`AgencyMarginPercent` to `PriceListItem`, migration `AddRetailPriceAndAgencyMargin`; extended `UpsertPriceListItemRequest`/`PriceListItemResponse`/`PriceResolution` with validation (`RetailPrice >= Price`, `AgencyMarginPercent` in `[0,100]`); updated `PriceListItemsSection`, the View page's items table, and `priceLists.upsertItem()`; updated `docs/14_pricing_and_suppliers.md` | 2026-09-09 |
| 8 | Stock Balances | No way to correct a balance — stock is architecturally never edited directly, only through movement transactions (load, sale, return, damage, adjustment); the `POST /stock/adjustment` endpoint existed but its controller action always returned `Unauthorized()` because `currentUser.UserId` is unset (#28) | Added a View icon (details modal) and an Edit icon that opens an "Adjust Stock" dialog (signed quantity delta + reason + Adjusted By) which posts to `/stock/adjustment` — this is the correct way to change a balance, not a direct field edit | TEMP: added `AdjustedByUserId` fallback to `AdjustmentRequest`, same pattern as #36/#40; wired `StockBalanceViewSheet` + `StockAdjustDialog` into Stock Balances table | 2026-09-08 |
| 9 | Stock Movements | Some rows missing From/To locations — every movement should always have both | Added View and Edit icons. Movements are an immutable audit-trail ledger by design — quantity, locations, and type can never change once recorded (the running balance is the sum of all movements), so Edit only lets you correct the Reason/note via a new `PATCH /stock/movements/{id}/reason` endpoint; a wrong quantity/location must be corrected with a new Adjustment (see #8), not by editing history | Wired `StockMovementViewSheet` + `StockMovementEditDialog` into Stock Movements table | 2026-09-08 |
| 41 | Stock Balances / Stock Movements | Balances and Movements tables/view-sheets showed raw location GUIDs instead of names; Movements also had a genuinely missing side — Sale/PurchaseReceipt/PurchaseReturn/CustomerReturn/Damage/Adjustment each only ever wrote one of From/To, since the other side (customer, supplier, write-off, adjustment) has no real Warehouse/Vehicle record. Existing seeded movement rows still showed blank From/To after the code fix since it only affects newly-created movements | — | Added `LocationName` to `StockBalanceResponse` and `FromLocationName`/`ToLocationName` to `StockMovementResponse`, resolved server-side (`StockService`) by looking up the real Warehouse/Vehicle. Added a new `LocationType.External` plus a fixed `Guid.Empty` sentinel location so every movement's missing side is now always populated and labeled contextually ("Customer", "Supplier", "Written Off", "Stock Adjustment") instead of being left null. Updated all 6 movement-creation call sites (StockService Damage/CustomerReturn/Adjustment, SaleService, DeliveryTaskService, GrnService, PurchaseReturnService, ReturnService), and added migration `FixSeedStockMovementExternalLocations` to backfill the 2 affected seeded rows | 2026-09-08 |
## Future Fixes — Pending Real Auth
Everything below is a stopgap that only exists because `Distribution/Middleware/AuthMiddleware.cs` (see #28) never actually authenticates a request — `currentUser.UserId`/`RoleId`/`RoleLevel` are always unset. Once #28 is properly fixed (validate against `Jwt:PublicKey` via RSA, reject bad/missing tokens instead of silently skipping validation, and attach `role_id`/`role_level`/`username` claims from the local User/Role row), all of the following must be reverted/re-enabled:
1. **`SaleService.CreateAsync`** — restore the `HasStoreScopeAsync(request.StoreId)` check that was bypassed in #36.
2. **`SaleService.CancelAsync`** — restore the `HasStoreScopeAsync(sale.FK_StoreId)` check that was bypassed in #40.
3. **`CreateSaleRequest.CreatedByUserId`** (`SaleDTOs.cs`) — remove this field; `CreateAsync` should go back to using `currentUser.UserId` only (drop the `?? request.CreatedByUserId` fallback and the `InvalidOperationException` guard around it).
4. **Create Sale page** (`/sales/new`) — remove the "Created By" user dropdown and its form field/validation entirely; the acting user should come from the session, not be picked manually.
5. **Sweep for the same pattern elsewhere** — `HasStoreScopeAsync`/`HasGlobalScope`-style checks exist in other services too (Order, Store, etc., per #35's affected controllers) and likely have the same silent-fail behavior; audit and fix once auth is real, not just the Sales module.
6. **Re-verify `StatusCode(403)` usages from #35** — once real authentication exists, confirm whether any of those 6 call sites should become genuine ASP.NET `[Authorize]`-driven `403`/`401` responses instead of manual scope checks.
7. **`AdjustmentRequest.AdjustedByUserId`** (`StockDTOs.cs`) and the "Adjusted By" dropdown in the Adjust Stock dialog (#8) — remove once `StockController.Adjust` can resolve `currentUser.UserId` on its own.
8. **`CreatePoRequest.CreatedByUserId`** (`PoDTOs.cs`) and the "Created By" dropdown on `/procurement/orders/new` — remove once `PoController.Create` can resolve `currentUser.UserId` on its own.
9. **`CreateGrnRequest.ReceivedByUserId`/`CreateFlatGrnRequest.ReceivedByUserId`** (`GrnDTOs.cs`) and the "Received By" dropdown in the Record Receipt sheet — remove once `GrnController.Create`/`CreateFlat` can resolve `currentUser.UserId` on its own.
10. **`CreatePurchaseReturnRequest.CreatedByUserId`** (`PurchaseReturnDTOs.cs`) and the "Created By" dropdown in the Return Items dialog (#24) — remove once `PurchaseReturnController.Create` can resolve `currentUser.UserId` on its own.
11. **`CreateSaleReturnRequest.CreatedByUserId`** (`ReturnDTOs.cs`) and the "Created By" dropdown in the Record Order Return dialog (#23) — remove once `ReturnController.Create` can resolve `currentUser.UserId` on its own.
12. **`SettleClaimRequest.SettledByUserId`** (`ClaimDTOs.cs`) and the "Settled By" dropdown in the Settle Claim dialog (#23) — remove once `ClaimController.Settle` can resolve `currentUser.UserId` on its own.
13. **`CreateSupplierClaimRequest.RaisedByUserId`/`ApproveSupplierClaimRequest.ApprovedByUserId`/`RejectSupplierClaimRequest.RejectedByUserId`** (`SupplierClaimDTOs.cs`) and the corresponding user dropdowns in the Raise/Approve/Reject Supplier Claim dialogs (#23) — remove once `SupplierClaimController.Create`/`Approve`/`Reject` can resolve `currentUser.UserId` on their own.
## Implementation Guide ## Implementation Guide
@@ -0,0 +1,48 @@
'use client'
import { Badge } from '@/components/ui/badge'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import type { BrandResponse } from '@/types/api'
const fmtDate = (v: string) =>
new Date(v).toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })
interface BrandViewSheetProps {
open: boolean
onClose: () => void
brand: BrandResponse | null
}
function Row({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div className="flex items-center justify-between py-2 text-sm border-b last:border-0">
<span className="text-muted-foreground">{label}</span>
<span className="font-medium text-right">{value}</span>
</div>
)
}
export function BrandViewSheet({ open, onClose, brand }: BrandViewSheetProps) {
if (!brand) return null
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>Brand Details</DialogTitle>
</DialogHeader>
<div className="flex flex-col">
<Row label="Name" value={brand.name} />
<Row label="Status" value={<Badge variant={brand.isActive ? 'default' : 'secondary'}>{brand.isActive ? 'Active' : 'Inactive'}</Badge>} />
<Row label="Created At" value={fmtDate(brand.createdAt)} />
</div>
</DialogContent>
</Dialog>
)
}
@@ -9,27 +9,29 @@ import type { TableConfig } from '@/components/shared/data-table'
import { Badge } from '@/components/ui/badge' import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button' import { Button } from '@/components/ui/button'
import { toast } from '@/components/ui/toast' import { toast } from '@/components/ui/toast'
import { brands as brandsApi, getApiError, getPaginatedItems } from '@/lib/api/index' import { brands as brandsApi, getApiError } from '@/lib/api/index'
import type { BrandResponse } from '@/types/api' import type { BrandResponse } from '@/types/api'
import { BrandSheet } from './brand-sheet' import { BrandSheet } from './brand-sheet'
import { BrandViewSheet } from './brand-view-sheet'
export function BrandsClient() { export function BrandsClient() {
const [data, setData] = useState<BrandResponse[]>([]) const [data, setData] = useState<BrandResponse[]>([])
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [total, setTotal] = useState(0) const [total, setTotal] = useState(0)
const [page, setPage] = useState(1)
const [pageSize, setPageSize] = useState(20) const [pageSize, setPageSize] = useState(20)
const [sheetOpen, setSheetOpen] = useState(false) const [sheetOpen, setSheetOpen] = useState(false)
const [editing, setEditing] = useState<BrandResponse | null>(null) const [editing, setEditing] = useState<BrandResponse | null>(null)
const [viewOpen, setViewOpen] = useState(false)
const [viewing, setViewing] = useState<BrandResponse | null>(null)
const fetchData = useCallback(async (p: number, ps: number) => { const fetchData = useCallback(async () => {
setLoading(true) setLoading(true)
try { try {
const res = await brandsApi.list({ page: p, pageSize: ps }) const items = await brandsApi.list()
setData(getPaginatedItems(res)) setData(items)
setTotal(res.total) setTotal(items.length)
} catch (err) { } catch (err) {
toast.add({ title: 'Failed to load brands', description: getApiError(err), type: 'error' }) toast.add({ title: 'Failed to load brands', description: getApiError(err), type: 'error' })
} finally { } finally {
@@ -38,8 +40,8 @@ export function BrandsClient() {
}, []) }, [])
useEffect(() => { useEffect(() => {
fetchData(page, pageSize) fetchData()
}, [fetchData, page, pageSize]) }, [fetchData])
function openCreate() { function openCreate() {
setEditing(null) setEditing(null)
@@ -51,9 +53,23 @@ export function BrandsClient() {
setSheetOpen(true) setSheetOpen(true)
} }
function openView(brand: BrandResponse) {
setViewing(brand)
setViewOpen(true)
}
async function handleDelete(brand: BrandResponse) {
try {
await brandsApi.remove(brand.id)
toast.add({ title: 'Brand deactivated', type: 'success' })
fetchData()
} catch (err) {
toast.add({ title: 'Failed to deactivate brand', description: getApiError(err), type: 'error' })
}
}
const tableConfig = useMemo<TableConfig<BrandResponse>>(() => ({ const tableConfig = useMemo<TableConfig<BrandResponse>>(() => ({
rowKey: 'id', rowKey: 'id',
serverSide: true,
stickyHeader: true, stickyHeader: true,
emptyMessage: 'No brands yet. Add one to get started.', emptyMessage: 'No brands yet. Add one to get started.',
pagination: { enabled: true, pageSize, pageSizeOptions: [10, 20, 50] }, pagination: { enabled: true, pageSize, pageSizeOptions: [10, 20, 50] },
@@ -90,14 +106,25 @@ export function BrandsClient() {
], ],
rowActions: { rowActions: {
actions: () => [ actions: () => [
{
action: 'view',
label: 'View brand',
onClick: openView,
},
{ {
action: 'edit', action: 'edit',
label: 'Edit brand', label: 'Edit brand',
onClick: openEdit, onClick: openEdit,
}, },
{
action: 'cancel',
label: 'Delete brand',
onClick: handleDelete,
visible: (target) => target.isActive,
},
], ],
}, },
}), [pageSize, openEdit]) }), [pageSize])
return ( return (
<> <>
@@ -120,7 +147,6 @@ export function BrandsClient() {
loading={loading} loading={loading}
totalCount={total} totalCount={total}
onServerSideChange={({ page: p }) => { onServerSideChange={({ page: p }) => {
setPage(p.page)
setPageSize(p.pageSize) setPageSize(p.pageSize)
}} }}
/> />
@@ -129,7 +155,13 @@ export function BrandsClient() {
open={sheetOpen} open={sheetOpen}
onClose={() => setSheetOpen(false)} onClose={() => setSheetOpen(false)}
brand={editing} brand={editing}
onSuccess={() => fetchData(page, pageSize)} onSuccess={() => fetchData()}
/>
<BrandViewSheet
open={viewOpen}
onClose={() => setViewOpen(false)}
brand={viewing}
/> />
</> </>
) )
@@ -13,6 +13,7 @@ import { categories as categoriesApi, getApiError } from '@/lib/api/index'
import type { CategoryResponse } from '@/types/api' import type { CategoryResponse } from '@/types/api'
import { CategorySheet } from './category-sheet' import { CategorySheet } from './category-sheet'
import { CategoryViewSheet } from './category-view-sheet'
export function CategoriesClient() { export function CategoriesClient() {
const [data, setData] = useState<CategoryResponse[]>([]) const [data, setData] = useState<CategoryResponse[]>([])
@@ -21,6 +22,8 @@ export function CategoriesClient() {
const [sheetOpen, setSheetOpen] = useState(false) const [sheetOpen, setSheetOpen] = useState(false)
const [editing, setEditing] = useState<CategoryResponse | null>(null) const [editing, setEditing] = useState<CategoryResponse | null>(null)
const [viewOpen, setViewOpen] = useState(false)
const [viewing, setViewing] = useState<CategoryResponse | null>(null)
const fetchData = useCallback(async () => { const fetchData = useCallback(async () => {
setLoading(true) setLoading(true)
@@ -49,6 +52,21 @@ export function CategoriesClient() {
setSheetOpen(true) setSheetOpen(true)
} }
function openView(category: CategoryResponse) {
setViewing(category)
setViewOpen(true)
}
async function handleDelete(category: CategoryResponse) {
try {
await categoriesApi.remove(category.id)
toast.add({ title: 'Category deactivated', type: 'success' })
fetchData()
} catch (err) {
toast.add({ title: 'Failed to deactivate category', description: getApiError(err), type: 'error' })
}
}
const tableConfig = useMemo<TableConfig<CategoryResponse>>(() => ({ const tableConfig = useMemo<TableConfig<CategoryResponse>>(() => ({
rowKey: 'id', rowKey: 'id',
serverSide: false, serverSide: false,
@@ -99,11 +117,22 @@ export function CategoriesClient() {
], ],
rowActions: { rowActions: {
actions: () => [ actions: () => [
{
action: 'view',
label: 'View category',
onClick: openView,
},
{ {
action: 'edit', action: 'edit',
label: 'Edit category', label: 'Edit category',
onClick: openEdit, onClick: openEdit,
}, },
{
action: 'cancel',
label: 'Delete category',
onClick: handleDelete,
visible: (target) => target.isActive,
},
], ],
}, },
}), [openEdit]) }), [openEdit])
@@ -135,6 +164,12 @@ export function CategoriesClient() {
category={editing} category={editing}
onSuccess={() => fetchData()} onSuccess={() => fetchData()}
/> />
<CategoryViewSheet
open={viewOpen}
onClose={() => setViewOpen(false)}
category={viewing}
/>
</> </>
) )
} }
@@ -0,0 +1,67 @@
'use client'
import { Badge } from '@/components/ui/badge'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Separator } from '@/components/ui/separator'
import type { CategoryResponse } from '@/types/api'
const fmtDate = (v: string) =>
new Date(v).toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })
interface CategoryViewSheetProps {
open: boolean
onClose: () => void
category: CategoryResponse | null
}
function Row({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div className="flex items-center justify-between py-2 text-sm border-b last:border-0">
<span className="text-muted-foreground">{label}</span>
<span className="font-medium text-right">{value}</span>
</div>
)
}
export function CategoryViewSheet({ open, onClose, category }: CategoryViewSheetProps) {
if (!category) return null
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>Category Details</DialogTitle>
</DialogHeader>
<div className="flex flex-col">
<Row label="Name" value={category.name} />
<Row label="Status" value={<Badge variant={category.isActive ? 'default' : 'secondary'}>{category.isActive ? 'Active' : 'Inactive'}</Badge>} />
<Row label="Created At" value={fmtDate(category.createdAt)} />
</div>
<Separator />
<div>
<p className="text-sm font-medium mb-2">Sub-categories ({category.subCategories.length})</p>
{category.subCategories.length === 0 ? (
<p className="text-sm text-muted-foreground">No sub-categories yet.</p>
) : (
<div className="flex flex-col gap-1">
{category.subCategories.map((sub) => (
<div key={sub.id} className="flex items-center justify-between rounded-md border px-3 py-2 text-sm">
<span>{sub.name}</span>
<Badge variant={sub.isActive ? 'default' : 'secondary'}>{sub.isActive ? 'Active' : 'Inactive'}</Badge>
</div>
))}
</div>
)}
</div>
</DialogContent>
</Dialog>
)
}
@@ -23,7 +23,7 @@ import {
SelectValue, SelectValue,
} from '@/components/ui/select' } from '@/components/ui/select'
import { toast } from '@/components/ui/toast' import { toast } from '@/components/ui/toast'
import { items as itemsApi, categories as categoriesApi, brands as brandsApi, uoms as uomsApi, getApiError, getPaginatedItems } from '@/lib/api/index' import { items as itemsApi, categories as categoriesApi, brands as brandsApi, uoms as uomsApi, getApiError } from '@/lib/api/index'
import type { ItemSummaryResponse, CategoryResponse, BrandResponse, UomResponse } from '@/types/api' import type { ItemSummaryResponse, CategoryResponse, BrandResponse, UomResponse } from '@/types/api'
const schema = z.object({ const schema = z.object({
@@ -69,11 +69,11 @@ export function ItemSheet({ open, onClose, item, onSuccess }: ItemSheetProps) {
if (open) { if (open) {
Promise.all([ Promise.all([
categoriesApi.list(), categoriesApi.list(),
brandsApi.list({ page: 1, pageSize: 200 }), brandsApi.list(),
uomsApi.list(), uomsApi.list(),
]).then(([cats, brandsRes, uomsRes]) => { ]).then(([cats, brandsRes, uomsRes]) => {
setCategories(cats) setCategories(cats)
setBrandsList(getPaginatedItems(brandsRes)) setBrandsList(brandsRes)
setUomsList(uomsRes) setUomsList(uomsRes)
}).catch((err) => { }).catch((err) => {
toast.add({ title: 'Failed to load reference data', description: getApiError(err), type: 'error' }) toast.add({ title: 'Failed to load reference data', description: getApiError(err), type: 'error' })
@@ -0,0 +1,54 @@
'use client'
import { Badge } from '@/components/ui/badge'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import type { ItemSummaryResponse } from '@/types/api'
const fmtDate = (v: string) =>
new Date(v).toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })
interface ItemViewSheetProps {
open: boolean
onClose: () => void
item: ItemSummaryResponse | null
}
function Row({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div className="flex items-center justify-between py-2 text-sm border-b last:border-0">
<span className="text-muted-foreground">{label}</span>
<span className="font-medium text-right">{value}</span>
</div>
)
}
export function ItemViewSheet({ open, onClose, item }: ItemViewSheetProps) {
if (!item) return null
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>Item Details</DialogTitle>
</DialogHeader>
<div className="flex flex-col">
<Row label="SKU" value={item.sku} />
<Row label="Name" value={item.name} />
<Row label="Category" value={item.categoryName} />
<Row label="Brand" value={item.brandName ?? '—'} />
<Row label="Base UOM" value={item.baseUomName} />
<Row label="Sale Price" value={`Rs. ${item.salePrice?.toFixed(2) ?? '0.00'}`} />
<Row label="Sync Source" value={item.syncSource} />
<Row label="Status" value={<Badge variant={item.isActive ? 'default' : 'secondary'}>{item.isActive ? 'Active' : 'Inactive'}</Badge>} />
<Row label="Created At" value={fmtDate(item.createdAt)} />
</div>
</DialogContent>
</Dialog>
)
}
@@ -13,6 +13,7 @@ import { items as itemsApi, getApiError, getPaginatedItems } from '@/lib/api/ind
import type { ItemSummaryResponse } from '@/types/api' import type { ItemSummaryResponse } from '@/types/api'
import { ItemSheet } from './item-sheet' import { ItemSheet } from './item-sheet'
import { ItemViewSheet } from './item-view-sheet'
export function ItemsClient() { export function ItemsClient() {
const [data, setData] = useState<ItemSummaryResponse[]>([]) const [data, setData] = useState<ItemSummaryResponse[]>([])
@@ -23,6 +24,8 @@ export function ItemsClient() {
const [sheetOpen, setSheetOpen] = useState(false) const [sheetOpen, setSheetOpen] = useState(false)
const [editing, setEditing] = useState<ItemSummaryResponse | null>(null) const [editing, setEditing] = useState<ItemSummaryResponse | null>(null)
const [viewOpen, setViewOpen] = useState(false)
const [viewing, setViewing] = useState<ItemSummaryResponse | null>(null)
const fetchData = useCallback(async (p: number, ps: number) => { const fetchData = useCallback(async (p: number, ps: number) => {
setLoading(true) setLoading(true)
@@ -51,6 +54,21 @@ export function ItemsClient() {
setSheetOpen(true) setSheetOpen(true)
} }
function openView(item: ItemSummaryResponse) {
setViewing(item)
setViewOpen(true)
}
async function handleDelete(item: ItemSummaryResponse) {
try {
await itemsApi.remove(item.id)
toast.add({ title: 'Item deactivated', type: 'success' })
fetchData(page, pageSize)
} catch (err) {
toast.add({ title: 'Failed to deactivate item', description: getApiError(err), type: 'error' })
}
}
const tableConfig = useMemo<TableConfig<ItemSummaryResponse>>(() => ({ const tableConfig = useMemo<TableConfig<ItemSummaryResponse>>(() => ({
rowKey: 'id', rowKey: 'id',
serverSide: true, serverSide: true,
@@ -117,11 +135,22 @@ export function ItemsClient() {
], ],
rowActions: { rowActions: {
actions: () => [ actions: () => [
{
action: 'view',
label: 'View item',
onClick: openView,
},
{ {
action: 'edit', action: 'edit',
label: 'Edit item', label: 'Edit item',
onClick: openEdit, onClick: openEdit,
}, },
{
action: 'cancel',
label: 'Delete item',
onClick: handleDelete,
visible: (target) => target.isActive,
},
], ],
}, },
}), [pageSize]) }), [pageSize])
@@ -158,6 +187,12 @@ export function ItemsClient() {
item={editing} item={editing}
onSuccess={() => fetchData(page, pageSize)} onSuccess={() => fetchData(page, pageSize)}
/> />
<ItemViewSheet
open={viewOpen}
onClose={() => setViewOpen(false)}
item={viewing}
/>
</> </>
) )
} }
@@ -0,0 +1,131 @@
'use client'
import { useEffect, useState } from 'react'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from '@/components/ui/dialog'
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field'
import { toast } from '@/components/ui/toast'
import { deliveryTasks as deliveryTasksApi, stock as stockApi, getApiError, getPaginatedItems } from '@/lib/api/index'
import type { DeliveryTaskResponse } from '@/types/api'
interface DeliveryTaskDeliverDialogProps {
open: boolean
onClose: () => void
onSuccess: () => void
task: DeliveryTaskResponse | null
vehicleId: string | null
}
export function DeliveryTaskDeliverDialog({ open, onClose, onSuccess, task, vehicleId }: DeliveryTaskDeliverDialogProps) {
const [quantities, setQuantities] = useState<Record<string, number>>({})
const [available, setAvailable] = useState<Record<string, number>>({})
const [submitting, setSubmitting] = useState(false)
useEffect(() => {
if (!open || !task) return
setQuantities(Object.fromEntries(task.items.map((i) => [i.itemId, i.quantityFulfilled])))
setAvailable({})
if (!vehicleId) return
stockApi.balances({ locationId: vehicleId, locationType: 'Vehicle', pageSize: 200 })
.then((res) => {
const map = Object.fromEntries(getPaginatedItems(res).map((b) => [b.itemId, b.quantity]))
setAvailable(map)
})
.catch((err) => toast.add({ title: 'Failed to load vehicle stock', description: getApiError(err), type: 'error' }))
}, [open, task, vehicleId])
if (!task) return null
const hasShortfall = task.items.some((item) => (quantities[item.itemId] ?? 0) > (available[item.itemId] ?? 0))
const nothingToDeliver = task.items.every((item) => (quantities[item.itemId] ?? 0) === 0)
async function onSubmit() {
setSubmitting(true)
try {
const items = Object.entries(quantities).map(([itemId, quantityFulfilled]) => ({ itemId, quantityFulfilled }))
await deliveryTasksApi.setQuantities(task!.id, items)
await deliveryTasksApi.confirm(task!.id)
toast.add({ title: 'Delivery confirmed', type: 'success' })
onSuccess()
onClose()
} catch (err) {
toast.add({ title: 'Failed to confirm delivery', description: getApiError(err), type: 'error' })
} finally {
setSubmitting(false)
}
}
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>Confirm Delivery</DialogTitle>
<DialogDescription>
Enter the quantity actually delivered for each item — you can't hand over more than what's
currently loaded on the vehicle.
</DialogDescription>
</DialogHeader>
<FieldGroup>
{task.items.map((item) => {
const onVehicle = available[item.itemId] ?? 0
const requested = quantities[item.itemId] ?? 0
const short = requested > onVehicle
const cap = Math.min(item.quantity, onVehicle)
return (
<Field key={item.itemId}>
<FieldLabel htmlFor={`qty-${item.itemId}`}>
{item.itemName}{' '}
<span className="text-muted-foreground">
(ordered {item.quantity}, on vehicle {onVehicle})
</span>
</FieldLabel>
<Input
id={`qty-${item.itemId}`}
type="number"
min={0}
max={cap}
aria-invalid={short}
value={requested}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setQuantities((prev) => ({ ...prev, [item.itemId]: Number(e.target.value) }))
}
/>
{short && (
<p className="text-xs text-destructive">
Only {onVehicle} of this item is loaded on the vehicle.
</p>
)}
</Field>
)
})}
</FieldGroup>
{nothingToDeliver && (
<p className="text-sm text-destructive">
Nothing is available to deliver — no stock for this order is currently loaded on the
vehicle.
</p>
)}
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button type="button" onClick={onSubmit} disabled={submitting || hasShortfall || nothingToDeliver}>
{submitting ? 'Confirming…' : 'Confirm Delivery'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
@@ -0,0 +1,103 @@
'use client'
import { useEffect } from 'react'
import { useForm, zodResolver, z } from '@/lib/form'
import { FormField } from '@/components/shared/form-field'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from '@/components/ui/dialog'
import { FieldGroup } from '@/components/ui/field'
import { toast } from '@/components/ui/toast'
import { deliveryTasks as deliveryTasksApi, getApiError } from '@/lib/api/index'
import type { DeliveryTaskResponse } from '@/types/api'
const schema = z.object({
note: z.string().min(1, 'Reason is required'),
})
type FormValues = z.infer<typeof schema>
interface DeliveryTaskFailDialogProps {
open: boolean
onClose: () => void
onSuccess: () => void
task: DeliveryTaskResponse | null
}
export function DeliveryTaskFailDialog({ open, onClose, onSuccess, task }: DeliveryTaskFailDialogProps) {
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: { note: '' },
})
useEffect(() => {
if (open) {
form.reset({ note: '' })
}
}, [open])
if (!task) return null
async function onSubmit(values: FormValues) {
try {
await deliveryTasksApi.fail(task!.id, values.note)
toast.add({ title: 'Delivery task marked as failed', type: 'success' })
onSuccess()
onClose()
} catch (err) {
toast.add({ title: 'Failed to update task', description: getApiError(err), type: 'error' })
}
}
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>Mark Delivery Failed</DialogTitle>
<DialogDescription>
Record why this delivery could not be completed.
</DialogDescription>
</DialogHeader>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-6">
<FieldGroup>
<FormField
control={form.control}
name="note"
label="Reason"
required
render={({ field, invalid }) => (
<Input
id="note"
name={field.name}
ref={field.ref}
value={field.value as string}
onChange={field.onChange}
onBlur={field.onBlur}
placeholder="Reason the delivery failed..."
aria-invalid={invalid}
/>
)}
/>
</FieldGroup>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
Back
</Button>
<Button type="submit" variant="destructive" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? 'Submitting…' : 'Mark as Failed'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
)
}

Some files were not shown because too many files have changed in this diff Show More