diff --git a/src/Backend/DiunaBI.Plugins.Morska/Processors/MorskaD6Processor.cs b/src/Backend/DiunaBI.Plugins.Morska/Processors/MorskaD6Processor.cs index 069d94f..35e4358 100644 --- a/src/Backend/DiunaBI.Plugins.Morska/Processors/MorskaD6Processor.cs +++ b/src/Backend/DiunaBI.Plugins.Morska/Processors/MorskaD6Processor.cs @@ -22,6 +22,7 @@ public class MorskaD6Processor : MorskaBaseProcessor private int Year { get; set; } private string? CostSource { get; set; } private string? SellSource { get; set; } + private List Mappings = new List(); public MorskaD6Processor( AppDbContext db, @@ -86,6 +87,12 @@ public class MorskaD6Processor : MorskaBaseProcessor { throw new InvalidOperationException("SourceLayerCosts record not found"); } + + Mappings = processWorker.Records.Where(x => x.Code!.StartsWith("Sell-Code-")).ToList(); + if (Mappings.Count == 0) + { + throw new InvalidOperationException("No Sell-Code- records found"); + } _logger.LogDebug( "{ProcessorType}: Configuration loaded - Year: {Year}, SourceCost: {CostSource}, SourceSell: {SellSource}", @@ -222,6 +229,8 @@ public class MorskaD6Processor : MorskaBaseProcessor ProcessorType); } + // COSTS + var firstDataSource = dataSources.First(); if (firstDataSource.Records == null || !firstDataSource.Records.Any()) @@ -283,6 +292,101 @@ public class MorskaD6Processor : MorskaBaseProcessor ProcessorType, newRecord.Code, newRecord.Value1, groupedRecord.Department, groupedRecord.DepartmentCode, groupedRecord.Type, groupedRecord.Month); } + + // SELLS + + var secondDataSource = dataSources.Last(); + + if (secondDataSource.Records == null || !secondDataSource.Records.Any()) + { + _logger.LogWarning("{ProcessorType}: Second data source has no records to process", ProcessorType); + return newRecords; + } + + // Create mapping dictionary from Mappings + var mappingDictionary = new Dictionary(); + foreach (var mapping in Mappings) + { + if (mapping.Code != null && mapping.Desc1 != null) + { + // Extract code from "Sell-Code-XXXX" -> "XXXX" + var sourceCode = mapping.Code.Replace("Sell-Code-", ""); + var targetCode = mapping.Desc1; + mappingDictionary[sourceCode] = targetCode; + + _logger.LogDebug("{ProcessorType}: Loaded mapping {SourceCode} -> {TargetCode}", + ProcessorType, sourceCode, targetCode); + } + } + + // Dictionary to collect results (key: target code with month, value: sum) + var sellResults = new Dictionary(); + + // Process records from secondDataSource + foreach (var record in secondDataSource.Records) + { + if (string.IsNullOrEmpty(record.Code) || record.Code.Length < 6 || !record.Value1.HasValue) + { + continue; + } + + // Extract month from last two digits + var monthStr = record.Code.Substring(record.Code.Length - 2); + if (!int.TryParse(monthStr, out var month) || month < 1 || month > 13) + { + _logger.LogDebug("{ProcessorType}: Invalid month in code {Code}", ProcessorType, record.Code); + continue; + } + + // Extract base code (without month) + var baseCode = record.Code.Substring(0, record.Code.Length - 2); + + // Check if we have mapping for this code + if (mappingDictionary.TryGetValue(baseCode, out var targetCode)) + { + // Create target code with month + var targetCodeWithMonth = $"{targetCode}{monthStr}"; + + // Add/sum value + if (sellResults.ContainsKey(targetCodeWithMonth)) + { + sellResults[targetCodeWithMonth] += record.Value1.Value; + _logger.LogDebug("{ProcessorType}: Added to existing record {TargetCode}: {Value} (total: {Total})", + ProcessorType, targetCodeWithMonth, record.Value1.Value, sellResults[targetCodeWithMonth]); + } + else + { + sellResults[targetCodeWithMonth] = record.Value1.Value; + _logger.LogDebug("{ProcessorType}: Created new record {TargetCode}: {Value}", + ProcessorType, targetCodeWithMonth, record.Value1.Value); + } + } + else + { + _logger.LogDebug("{ProcessorType}: No mapping found for code {BaseCode}", ProcessorType, baseCode); + } + } + + // Add results to newRecords + foreach (var sellResult in sellResults) + { + var newRecord = new Record + { + Id = Guid.NewGuid(), + Code = sellResult.Key, + CreatedAt = DateTime.UtcNow, + ModifiedAt = DateTime.UtcNow, + Value1 = sellResult.Value + }; + + newRecords.Add(newRecord); + + _logger.LogDebug("{ProcessorType}: Added sell record {Code} with value {Value}", + ProcessorType, newRecord.Code, newRecord.Value1); + } + + _logger.LogInformation("{ProcessorType}: Processed {SellRecordCount} sell records from {OriginalSellCount} source records", + ProcessorType, sellResults.Count, secondDataSource.Records.Count); _logger.LogInformation( "{ProcessorType}: Processed {GroupCount} unique grouped records from {OriginalCount} original records", diff --git a/tools/sql-scripts/utlis/CreateRecord.sql b/tools/sql-scripts/utlis/CreateRecord.sql index 89f75d5..982e544 100644 --- a/tools/sql-scripts/utlis/CreateRecord.sql +++ b/tools/sql-scripts/utlis/CreateRecord.sql @@ -1,5 +1,5 @@ INSERT INTO [diunabi-morska].[dbo].[Records] ([Id], [Code], [Desc1], [CreatedAt], [ModifiedAt], [CreatedById], [ModifiedById], [IsDeleted], [LayerId]) -VALUES ((SELECT NEWID()), 'SourceLayerSell', 'L4187-P-2025-R1-T1', +VALUES ((SELECT NEWID()), 'Sell-Code-9091', '1208/', GETDATE(), GETDATE(), '117be4f0-b5d1-41a1-a962-39dc30cce368', '117be4f0-b5d1-41a1-a962-39dc30cce368', 0, 'd1e6e8d2-a13b-421f-84cb-a9ccd5b1c6f5');