diff --git a/LaaProductionWeb/LaaProductionWeb.API/Areas/Shipment/Models/ScanModel.cs b/LaaProductionWeb/LaaProductionWeb.API/Areas/Shipment/Models/ScanModel.cs index 9d640660..fa155c98 100644 --- a/LaaProductionWeb/LaaProductionWeb.API/Areas/Shipment/Models/ScanModel.cs +++ b/LaaProductionWeb/LaaProductionWeb.API/Areas/Shipment/Models/ScanModel.cs @@ -21,13 +21,13 @@ public async Task SerialScannedAsync(string serialNo) { - const string TEST_STATE = "30"; + const string TEST_STATE = "25"; var watermeterTestPassed = this.sqlClient .FirstOrDefault( query: $@" SELECT [PS].[AuftragNr] - , CASE WHEN MAX([APS].[StatusFertigung]) = '{TEST_STATE}' + , CASE WHEN MAX([APS].[StatusFertigung]) > '{TEST_STATE}' THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END FROM [PalettenScan] AS [PS] @@ -57,14 +57,18 @@ AND (REPLACE([APS].[KundeneigeneSerienNr], ' ', '') = [PS].[SerienNr] OR CAST([APS].[SerienNr] AS VARCHAR) = [PS].[SerienNr]) WHERE [PS].[AuftragNr] = @{nameof(orderNo)} - AND [APS].[StatusFertigung] = '{TEST_STATE}' + AND [APS].[StatusFertigung] > '{TEST_STATE}' AND [PS].[ID] IS NULL", parameters: parameters => parameters.Add(nameof(orderNo), orderNo), reader: reader => reader.GetValue()); if (unpackedWatermeterCount == 0) { - return await this.smtpClient.SendAsync("Subject", "Html Body", "from", "1", "2"); + return await this.smtpClient.SendAsync( + subject: "Subject", + body: "Html or text body", + from: "laa_production@xylem.com", + to: new[] { "Roland.Drabesch@Xylem.com", "Stoyan.Zlatev@Xylem.com" }); } } diff --git a/LaaProductionWeb/LaaProductionWeb.API/Models/SMTP/SMTPClient.cs b/LaaProductionWeb/LaaProductionWeb.API/Models/SMTP/SMTPClient.cs index 2693328d..4c8a290f 100644 --- a/LaaProductionWeb/LaaProductionWeb.API/Models/SMTP/SMTPClient.cs +++ b/LaaProductionWeb/LaaProductionWeb.API/Models/SMTP/SMTPClient.cs @@ -3,6 +3,9 @@ using MailKit.Net.Smtp; using MimeKit; + using System; + using System.Collections.Generic; + using System.IO; using System.Linq; using System.Threading.Tasks; @@ -13,27 +16,40 @@ public SMTPClient(SMTPSettings settings) => this.settings = settings; - public async Task SendAsync(string subject, string body, string from, params string[] to) + public async Task SendAsync(string subject, string body, string from, IEnumerable to) { - //using (var client = new SmtpClient()) - //{ - // await client.AuthenticateAsync(this.settings.Username, this.settings.Password); - // await client.ConnectAsync(this.settings.Host, this.settings.Port); + try + { + using (var client = new SmtpClient()) + { + await client.AuthenticateAsync(this.settings.Username, this.settings.Password); + await client.ConnectAsync(this.settings.Host, this.settings.Port); - // var message = new MimeMessage - // { - // Subject = subject, - // Body = new BodyBuilder - // { - // HtmlBody = body - // }.ToMessageBody() - // }; - // var sender = MailboxAddress.Parse(from); - // var recipients = to.Select(x => MailboxAddress.Parse(x)); - // var response = await client.SendAsync(message, sender, recipients); - //} + var message = new MimeMessage + { + Subject = subject, + Body = new BodyBuilder + { + HtmlBody = body + }.ToMessageBody(), + }; + message.From.Add(MailboxAddress.Parse(from)); + message.To.Add(MailboxAddress.Parse(to.FirstOrDefault())); + message.Cc.AddRange(to.Skip(1).Select(x => MailboxAddress.Parse(x))); - return await Task.FromResult(true); + var response = await client.SendAsync(message); + + File.AppendAllLines("smtp.log", new[] { $"[SMTP_RESPONSE] {response}" }); + } + + return true; + } + catch (Exception e) + { + File.AppendAllLines("smtp.log", new[] { $"[SMTP_ERROR] {e.Message}" }); + + return false; + } } } } diff --git a/LaaProductionWeb/LaaProductionWeb.API/appsettings.json b/LaaProductionWeb/LaaProductionWeb.API/appsettings.json index 1ad5b452..294ad199 100644 --- a/LaaProductionWeb/LaaProductionWeb.API/appsettings.json +++ b/LaaProductionWeb/LaaProductionWeb.API/appsettings.json @@ -3,8 +3,8 @@ "DefaultConnection": "Data Source=SLASQL01.emea.sensus.net;Initial Catalog=Auftrag;Password=ServiceParingfile;Persist Security Info=True;User ID=ServiceParingfile;" }, "SMTPSettings": { - "Host": "", - "Port": 0, + "Host": "smtp.xylem.com", + "Port": 587, "Username": "", "Password": "" } diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Interfaces/IShipmentsService.cs b/LaaProductionWeb/LaaProductionWeb.Services/Interfaces/IShipmentsService.cs index fa8d19b2..e8b3ff8f 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Interfaces/IShipmentsService.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Interfaces/IShipmentsService.cs @@ -1,17 +1,18 @@ namespace LaaProductionWeb.Services.Interfaces { using LaaProductionWeb.Services.Models; + using System.Collections.Generic; public interface IShipmentsService : ITransient { OrderScanModel DeletePalletEntry(int id); - int GetPalletsCount(OrderScanModel model); - byte[] LoadBatchBuffer(OrderScanModel model); PalletsBatchModel LoadBatchModel(OrderScanModel model); + IEnumerable LoadPallets(OrderScanModel model); + Result UpdateBatchModel(OrderScanModel model); } } diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/BatchPrintModel.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/BatchPrintModel.cs index ea652345..a61a7ac3 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/BatchPrintModel.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/BatchPrintModel.cs @@ -11,7 +11,7 @@ => this.batchModel = batchModel; public string ClientName - => this.batchModel.ClientName; + => this.batchModel.Customer; public int OrderNr => this.batchModel.OrderNr; @@ -21,17 +21,5 @@ public IEnumerable Positions => this.batchModel.Positions; - - public int TotlalItemsCount - => this.batchModel.TotlalItemsCount; - - public int ItemsCount - => this.batchModel.ItemsCount; - - public SerialNr[] Items - => this.batchModel - .Items - .Where(x => x.IsScaned) - .ToArray(); } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderInfoModel.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderInfoModel.cs index f54f068a..7f38d3f7 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderInfoModel.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderInfoModel.cs @@ -8,7 +8,7 @@ public int ProductionOrderNr { get; set; } - public string ClientName { get; set; } + public string Customer { get; set; } public string Nr => this.OrderNr > 0 diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderScanModel.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderScanModel.cs index 99c9f8f4..a9dd884f 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderScanModel.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/OrderScanModel.cs @@ -1,5 +1,6 @@ namespace LaaProductionWeb.Services.Models { + using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -13,7 +14,7 @@ public int PalletsCount { get; set; } [Display(Name = "Paletten-Nr.")] - public int PalletNr { get; set; } = 1; + public int PalletNr { get; set; } public IDictionary Pallets { get; set; } = new Dictionary { { 1, false } }; @@ -21,16 +22,21 @@ public string PrintUrl => $"~/Shipments/Print?ordernr={this.OrderNr}&positionnr={this.PositionNr}&palletnr={this.PalletNr}"; - public string DownloadUrl - => $"~/Shipments/Download?ordernr={this.OrderNr}&positionnr={this.PositionNr}&palletnr={this.PalletNr}"; + public string PalletUrl + => $"~/Shipments/Pallet?ordernr={this.OrderNr}&positionnr={this.PositionNr}"; - public void UpdatePalletsCount(int palletsCount) + public void UpdatePallets(IEnumerable pallets) { - this.PalletsCount = palletsCount; - this.Pallets = Enumerable - .Range(1, palletsCount) - .ToDictionary(x => x, x => false); - this.Pallets[this.PalletNr] = true; + this.Pallets = pallets.ToDictionary(x => x, x => x == this.PalletNr); + + if (this.PalletNr == 0) + { + this.PalletNr = this.Pallets.Keys.Last(); + } + else if (!this.Pallets.ContainsKey(this.PalletNr)) + { + this.Pallets[this.PalletNr] = true; + } } } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletPosition.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletPosition.cs index c1f8dee0..25327cc6 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletPosition.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletPosition.cs @@ -1,5 +1,8 @@ namespace LaaProductionWeb.Services.Models { + using System; + using System.Collections.Generic; + public class PalletPosition { public int PositionNr { get; set; } @@ -7,5 +10,10 @@ public string Description { get; set; } public int ItemsCount { get; set; } + + public int TotalItemsCount { get; set; } + + public IEnumerable Items { get; set; } + = Array.Empty(); } } diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletsBatchModel.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletsBatchModel.cs index adc8eea8..ec31a0ef 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletsBatchModel.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/PalletsBatchModel.cs @@ -2,26 +2,17 @@ { using System; using System.Collections.Generic; - using System.Linq; public class PalletsBatchModel { - public string ClientName { get; set; } + public string Customer { get; set; } public int OrderNr { get; set; } public int ProductionOrderNr { get; set; } - public int TotlalItemsCount { get; set; } - public string AdditionalText { get; set; } - public int ItemsCount - => this.Positions.Sum(x => x.ItemsCount); - - public IEnumerable Items { get; set; } - = Array.Empty(); - public IEnumerable Positions { get; set; } = Array.Empty(); } diff --git a/LaaProductionWeb/LaaProductionWeb.Services/Models/SerialNr.cs b/LaaProductionWeb/LaaProductionWeb.Services/Models/SerialNr.cs index 8532caa5..0a4b2c17 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/Models/SerialNr.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/Models/SerialNr.cs @@ -2,17 +2,7 @@ { public class SerialNr { - private int id; - - public int Id - { - get => this.id; - set - { - this.id = value; - this.IsScaned = this.id > 0; - } - } + public int Id { get; set; } public int Nr { get; set; } @@ -20,10 +10,10 @@ public int? PalletNr { get; set; } - public string ClientNr { get; set; } + public string CustomerNr { get; set; } public bool IsScaned { get; set; } - public string BarcodeNr => this.ClientNr ?? $"{this.Nr}"; + public string BarcodeNr => this.CustomerNr ?? $"{this.Nr}"; } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb.Services/OrdersService.cs b/LaaProductionWeb/LaaProductionWeb.Services/OrdersService.cs index 5df21ba3..0aa541d9 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/OrdersService.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/OrdersService.cs @@ -24,14 +24,14 @@ WHERE [AlleAuftragPositionen].[Versand_Ist_datum] IS NOT NULL AND [AlleAuftragPositionen].[AuftragNr] LIKE @{nameof(search)} ORDER BY [AlleAuftragPositionen].[AuftragNr] - , [AlleAuftragPositionen].[FertigungsauftragNr] - , [Kunde].[Name]", + , [AlleAuftragPositionen].[FertigungsauftragNr] + , [Kunde].[Name]", parameters: parameters => parameters.Add(nameof(search), $"%{search}%"), reader: reader => new OrderInfoModel { - OrderNr = reader.GetValue(0), - ProductionOrderNr = reader.GetValue(1), - ClientName = reader.GetValue(2), + OrderNr = reader.GetValue(), + ProductionOrderNr = reader.GetValue(), + Customer = reader.GetString(), }); return new OrdersFoundModel(ordersFound); diff --git a/LaaProductionWeb/LaaProductionWeb.Services/ShipmentsService.cs b/LaaProductionWeb/LaaProductionWeb.Services/ShipmentsService.cs index 9895c402..44800705 100644 --- a/LaaProductionWeb/LaaProductionWeb.Services/ShipmentsService.cs +++ b/LaaProductionWeb/LaaProductionWeb.Services/ShipmentsService.cs @@ -8,10 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; - - /// - /// - /// + public class ShipmentsService : IShipmentsService { private readonly ISqlClient sqlClient; @@ -25,34 +22,12 @@ if (orderScanModel != null) { - var updated = this.UpdatePalletsNr(orderScanModel); + var updated = this.UpdatePalletsNr(orderScanModel.OrderNr ?? 0); } return orderScanModel; } - public int GetPalletsCount(OrderScanModel model) - { - var palletsCount = this.sqlClient.FirstOrDefault( - query: $@" - SELECT COUNT(1) - FROM (SELECT [PalettenNr] AS [PalletNr] - , COUNT([SerienNr]) AS [Count] - FROM [PalettenScan] - WHERE [AuftragNr] = @{nameof(model.OrderNr)} - AND (@{nameof(model.PositionNr)} IS NULL - OR [PositionNr] = @{nameof(model.PositionNr)}) - GROUP BY [PalettenNr]) - AS [Paletten] - WHERE [Count] > 0", - parameters: parameters => parameters - .Add(nameof(model.OrderNr), model.OrderNr) - .Add(nameof(model.PositionNr), model.PositionNr), - reader: reader => reader.GetValue(0)); - - return Math.Max(palletsCount, 1); - } - public byte[] LoadBatchBuffer(OrderScanModel model) { var palletLoad = this.sqlClient.ExecuteReader( @@ -78,124 +53,188 @@ return Encoding.UTF8.GetBytes($"Pallet Nr.,Serial Nr.{Environment.NewLine}{csvContent}"); } - public IEnumerable LoadBatchItems(OrderScanModel model) - => this.sqlClient.ExecuteReader( - query: $@" - SELECT DISTINCT - [PalettenScan].[ID] -- 0 - , [AuftragPositionSerienNr].[SerienNr] -- 1 - , REPLACE(RTRIM(LTRIM([AuftragPositionSerienNr].[KundeneigeneSerienNr])), ' ', '') -- 2 - , [PalettenScan].[PalettenNr] -- 3 - , CAST(CASE WHEN [PalettenScan].[ID] IS NULL THEN 0 ELSE 1 END AS BIT) -- 4 - , ISNULL([PalettenScan].[PositionNr], [AuftragPositionSerienNr].[PositionNr]) -- 5 - FROM [AuftragPositionSerienNr] - LEFT JOIN [PalettenScan] - ON [PalettenScan].[AuftragNr] = [AuftragPositionSerienNr].[AuftragNr] - AND [PalettenScan].[PositionNr] = [AuftragPositionSerienNr].[PositionNr] - AND [PalettenScan].[SerienNr] = REPLACE(RTRIM(LTRIM([AuftragPositionSerienNr].[KundeneigeneSerienNr])), ' ', '') - WHERE [AuftragPositionSerienNr].[AuftragNr] = @{nameof(model.OrderNr)} - AND (@{nameof(model.PositionNr)} IS NULL - OR [AuftragPositionSerienNr].[PositionNr] = @{nameof(model.PositionNr)}) - AND ([PalettenScan].[PalettenNr] = @{nameof(model.PalletNr)} - OR [PalettenScan].[PalettenNr] IS NULL)", - parameters: parameters => parameters - .Add(nameof(model.OrderNr), model.OrderNr) - .Add(nameof(model.PositionNr), model.PositionNr) - .Add(nameof(model.PalletNr), model.PalletNr), - reader: reader => new SerialNr - { - Id = reader.GetValue(0), - Nr = reader.GetValue(1), - ClientNr = reader.GetString(2), - PalletNr = reader.GetValue(3), - IsScaned = reader.GetValue(4), - PositionNr = reader.GetValue(5), - }) - .OrderByDescending(x => x.IsScaned) - .ThenBy(x => x.PositionNr) - .ThenBy(x => x.ClientNr); - public PalletsBatchModel LoadBatchModel(OrderScanModel model) { - const string orderNr = nameof(model.OrderNr); - const string palNr = nameof(model.PalletNr); + var batchModel = new PalletsBatchModel(); + var pallets = this.LoadPallets(model); + model.UpdatePallets(pallets); - var batchModel = this.sqlClient.FirstOrDefault( - query: $@" - SELECT TOP 1 - [Auftrag].[AuftragNr] -- 0 - , [Auftrag].[FertigungsauftragNr] -- 1 - , (SELECT SUM([AAP].[Menge]) - FROM [AlleAuftragPositionen] AS [AAP] - WHERE [AAP].[AuftragNr] = [Auftrag].[AuftragNr]) -- 2 - , [Kunde].[Name] -- 3 - , [Auftrag].[ZusatzText] -- 4 - FROM [AlleAuftragPositionen] AS [Auftrag] - JOIN [Kunde] ON [Kunde].[KundenNr] = [Auftrag].[KundenNr] - WHERE [Auftrag].[AuftragNr] = @{nameof(model.OrderNr)} - AND (@{nameof(model.PositionNr)} IS NULL - OR [Auftrag].[PositionNr] = @{nameof(model.PositionNr)}) - ORDER BY [Auftrag].[FertigungsauftragNr] DESC", - parameters: parameters => parameters - .Add(nameof(model.OrderNr), model.OrderNr) - .Add(nameof(model.PositionNr), model.PositionNr), - reader: reader => new PalletsBatchModel - { - OrderNr = reader.GetValue(0), - ProductionOrderNr = reader.GetValue(1), - TotlalItemsCount = reader.GetValue(2), - ClientName = reader.GetValue(3), - AdditionalText = reader.GetValue(4), - }) - ?? new PalletsBatchModel(); - - if (batchModel.OrderNr > 0) + if (model != null && model.OrderNr != null) { - batchModel.Items = this.LoadBatchItems(model); - batchModel.Positions = this.LoadBatchPositions(model, orderNr, palNr); + batchModel = this.LoadBatchModel(model.OrderNr, model.PositionNr, model.PalletNr); + + if (model.PositionNr != null && !batchModel.Positions.Any()) + { + batchModel = this.LoadBatchModel(model.OrderNr, model.PositionNr, null); + } } return batchModel; } - private IEnumerable LoadBatchPositions(OrderScanModel model, string orderNr, string palNr) - => this.sqlClient.ExecuteReader($@" - SELECT DISTINCT - [Auftrag].[PositionNr] - , COUNT(1) - , [Identnr].[Typ] - , [Identnr].[Typzusatz] - , [Identnr].[Nennweite] - FROM [Identnr] - JOIN [AlleAuftragPositionen] AS [Auftrag] - ON [Auftrag].[IdentNr] = [Identnr].[IdentNr] - JOIN [PalettenScan] - ON [PalettenScan].[AuftragNr] = [Auftrag].[AuftragNr] - AND [PalettenScan].[PositionNr] = [Auftrag].[PositionNr] - AND [PalettenScan].[PalettenNr] = @{palNr} - WHERE [Auftrag].[AuftragNr] = @{orderNr} - GROUP BY [Auftrag].[PositionNr] - , [Identnr].[Typ] - , [Identnr].[Typzusatz] - , [Identnr].[Nennweite] - ORDER BY [Auftrag].[PositionNr] ", - parameters => parameters - .Add(orderNr, model.OrderNr) - .Add(palNr, model.PalletNr), - positionReader => new PalletPosition + private PalletsBatchModel LoadBatchModel(int? orderNo, int? posNo, int? palletNo) + => this.sqlClient.FirstOrDefault( + query: $@" + SELECT TOP 1 + [AAP].[AuftragNr] + , [AAP].[FertigungsauftragNr] + , [Kunde].[Name] + , [AAP].[ZusatzText] + FROM [AlleAuftragPositionen] AS [AAP] + JOIN [Kunde] ON [Kunde].[KundenNr] = [AAP].[KundenNr] + WHERE [AAP].[AuftragNr] = @{nameof(orderNo)} + AND (@{nameof(posNo)} IS NULL + OR [AAP].[PositionNr] = @{nameof(posNo)}) + ORDER BY [AAP].[FertigungsauftragNr] DESC", + parameters: parameters => parameters + .Add(nameof(orderNo), orderNo) + .Add(nameof(posNo), posNo), + reader: reader => new PalletsBatchModel { - PositionNr = positionReader.GetValue(0), - ItemsCount = positionReader.GetValue(1), - Description = string - .Join(" ", new[] - { - positionReader.GetString(2), - positionReader.GetString(3), - positionReader.GetString(4), - } - .Where(x => !string.IsNullOrWhiteSpace(x))) + OrderNr = reader.GetValue(), + ProductionOrderNr = reader.GetValue(), + Customer = reader.GetString(), + AdditionalText = reader.GetString(), + Positions = this.LoadBatchPositions(orderNo, posNo, palletNo) + }) + ?? new PalletsBatchModel(); + + private IEnumerable LoadBatchPositions(int? orderNo, int? posNo, int? palletNo) + => this.sqlClient.ExecuteReader( + query: $@"SELECT [AAP].[PositionNr] AS [Pos] + , [Identnr].[Typ] AS [Typ] + , [Identnr].[Typzusatz] AS [Typzusatz] + , [Identnr].[Nennweite] AS [Nennweite] + , (SELECT COUNT(1) + FROM [PalettenScan] AS [x] + WHERE [x].[AuftragNr] = [PS].[AuftragNr] + AND [x].[PositionNr] = [PS].[PositionNr] + AND [x].[PalettenNr] = [PS].[PalettenNr]) AS [Count] + , (SELECT TOP 1 [Menge] + FROM [AlleAuftragPositionen] AS [x] + WHERE [x].[AuftragNr] = [AAP].[AuftragNr] + AND [x].[PositionNr] = [AAP].[PositionNr]) AS [TotalCount] + FROM [Identnr] + JOIN [AlleAuftragPositionen] AS [AAP] + ON [AAP].[IdentNr] = [Identnr].[IdentNr] + LEFT JOIN [PalettenScan] AS [PS] + ON [PS].[AuftragNr] = [AAP].[AuftragNr] + AND [PS].[PositionNr] = [AAP].[PositionNr] + WHERE [AAP].[AuftragNr] = @{nameof(orderNo)} + AND (@{nameof(palletNo)} IS NULL + OR [PS].[PalettenNr] = @{nameof(palletNo)}) + AND (@{nameof(posNo)} IS NULL + OR [AAP].[PositionNr] = @{nameof(posNo)}) + GROUP BY [AAP].[AuftragNr] + , [PS].[AuftragNr] + , [AAP].[PositionNr] + , [PS].[PositionNr] + , [Identnr].[Typ] + , [Identnr].[Typzusatz] + , [Identnr].[Nennweite] + , [PS].[PalettenNr] + ORDER BY [AAP].[PositionNr]", + parameters: parameters => parameters + .Add(nameof(orderNo), orderNo) + .Add(nameof(posNo), posNo) + .Add(nameof(palletNo), palletNo), + reader: reader => new PalletPosition + { + PositionNr = reader.GetValue(), + Description = string + .Join(" ", new[] + { + reader.GetString(), + reader.GetString(), + reader.GetString(), + } + .Where(x => !string.IsNullOrWhiteSpace(x))), + ItemsCount = reader.GetValue(), + TotalItemsCount = reader.GetValue(), + Items = this.LoadBatchItems(orderNo, posNo, palletNo), + }); + + private IEnumerable LoadBatchItems(int? orderNo, int? posNo, int? palletNo) + => this.sqlClient.ExecuteReader( + query: $@"SELECT DISTINCT + [x].[Id] + , [x].[PositionNr] + , [x].[SerienNr] + , [x].[CustomerSerienNr] + , [x].[PalettenNr] + , [x].[IsScanned] + FROM (SELECT [PS].[ID] AS [Id] + , ISNULL([PS].[PositionNr], [APS].[PositionNr]) AS [PositionNr] + , [APS].[SerienNr] AS [SerienNr] + , REPLACE(RTRIM(LTRIM([APS].[KundeneigeneSerienNr])), ' ', '') AS [CustomerSerienNr] + , [PS].[PalettenNr] AS [PalettenNr] + , CAST(CASE WHEN [PS].[ID] IS NULL THEN 0 ELSE 1 END AS BIT) AS [IsScanned] + FROM [AuftragPositionSerienNr] AS [APS] + LEFT JOIN [PalettenScan] AS [PS] + ON [PS].[AuftragNr] = [APS].[AuftragNr] + AND [PS].[PositionNr] = [APS].[PositionNr] + AND [PS].[SerienNr] = REPLACE(RTRIM(LTRIM([APS].[KundeneigeneSerienNr])), ' ', '') + WHERE [APS].[AuftragNr] = @{nameof(orderNo)} + AND ([PS].[PalettenNr] IS NULL + OR @{nameof(palletNo)} IS NULL + OR [PS].[PalettenNr] = @{nameof(palletNo)}) + AND (@{nameof(posNo)} IS NULL + OR [APS].[PositionNr] = @{nameof(posNo)})) + AS [x] + ORDER BY [x].[IsScanned] DESC + , [x].[PositionNr] + , [x].[CustomerSerienNr] + , [x].[SerienNr]", + parameters: parameters => parameters + .Add(nameof(orderNo), orderNo) + .Add(nameof(posNo), posNo) + .Add(nameof(palletNo), palletNo), + reader: reader => new SerialNr + { + Id = reader.GetValue(), + PositionNr = reader.GetValue(), + Nr = reader.GetValue(), + CustomerNr = reader.GetString(), + PalletNr = reader.GetValue(), + IsScaned = reader.GetValue(), }); + public IEnumerable LoadPallets(OrderScanModel model) + { + this.UpdatePalletsNr(model.OrderNr ?? 0); + + var pallets = this.sqlClient.ExecuteReader( + query: $@"SELECT DISTINCT [PS].[PalettenNr] + FROM [PalettenScan] AS [PS] + WHERE [PS].[AuftragNr] = @{nameof(model.OrderNr)} + AND (@{nameof(model.PositionNr)} IS NULL + OR [PS].[PositionNr] = @{nameof(model.PositionNr)}) + ORDER BY [PS].[PalettenNr]", + parameters: parameters => parameters + .Add(nameof(model.OrderNr), model.OrderNr) + .Add(nameof(model.PositionNr), model.PositionNr), + reader: reader => (int)reader.GetValue()); + + if (!pallets.Any()) + { + return this.sqlClient.ExecuteReader( + query: $@"SELECT CAST(ISNULL(MAX([PS].[PalettenNr]) + , (SELECT ISNULL(MAX([PS].[PalettenNr]), 1) + FROM [PalettenScan] AS [PS] + WHERE [PS].[AuftragNr] = @{nameof(model.OrderNr)})) AS INT) + FROM [PalettenScan] AS [PS] + WHERE [PS].[AuftragNr] = @{nameof(model.OrderNr)} + AND (@{nameof(model.PositionNr)} IS NULL + OR [PS].[PositionNr] = @{nameof(model.PositionNr)})", + parameters: parameters => parameters + .Add(nameof(model.OrderNr), model.OrderNr) + .Add(nameof(model.PositionNr), model.PositionNr), + reader: reader => reader.GetValue()); + } + + return pallets; + } + public Result UpdateBatchModel(OrderScanModel model) { if (!string.IsNullOrWhiteSpace(model.SerialNr)) @@ -215,6 +254,9 @@ { model.SerialNr = orderIdentity.ClientSerialNr; model.PositionNr = orderIdentity.PositionNr; + + var nextPalletNr = this.NextPallet(model.OrderNr.Value); + model.PalletNr = Math.Min(model.PalletNr, nextPalletNr); } var rowsAffected = this.InsertPalletEntry(model); @@ -225,6 +267,12 @@ return false; } + private int NextPallet(int orderno) + => this.sqlClient.FirstOrDefault( + query: $@"SELECT MAX([PalettenNr]) + 1 FROM [PalettenScan] WHERE [AuftragNr] = @{nameof(orderno)}", + parameters: parameters => parameters.Add(nameof(orderno), orderno), + reader: reader => reader.GetValue()); + private OrderScanModel DeletePalletEntryById(int id) => this.sqlClient.FirstOrDefault( query: $@" @@ -296,40 +344,27 @@ SELECT CAST(CASE WHEN [PalettenScan].[ID] IS NULL THEN 0 ELSE 1 END AS BIT) FROM [PalettenScan] WHERE [PalettenScan].[AuftragNr] = @{nameof(orderNr)} - AND [PalettenScan].[SerienNr] = @{nameof(serialNr)}", + AND [PalettenScan].[SerienNr] = @{nameof(serialNr)} + OR @{nameof(serialNr)} LIKE '%' + (REPLACE(RTRIM(LTRIM([AuftragPositionSerienNr].[KundeneigeneSerienNr])), ' ', '')) + '%'", parameters => parameters .Add(nameof(orderNr), orderNr) .Add(nameof(serialNr), serialNr), - reader => reader.GetValue(0)); - - private bool UpdatePalletsNr(OrderScanModel model) - => this.sqlClient.FirstOrDefault($@" - BEGIN TRANSACTION - ------------------------------------------------------------------------ - DECLARE @rowsByAuftragPosition INT = ( - SELECT COUNT(1) - FROM [PalettenScan] - WHERE [AuftragNr] = @{nameof(model.OrderNr)} - AND [PositionNr] = @{nameof(model.PositionNr)}) - ------------------------------------------------------------------------ - UPDATE [PalettenScan] - SET [PalettenNr] = [OrderedPallets].[NewPalletNr] - FROM [PalettenScan] - JOIN (SELECT [ID] - , [PalettenNr] - , DENSE_RANK() OVER(ORDER BY [PalettenNr]) AS [NewPalletNr] - FROM [PalettenScan] - WHERE [AuftragNr] = @{nameof(model.OrderNr)} - AND [PositionNr] = @{nameof(model.PositionNr)}) - AS [OrderedPallets] - ON [PalettenScan].[ID] = [OrderedPallets].[ID] - ------------------------------------------------------------------------ - IF @rowsByAuftragPosition = @@ROWCOUNT - BEGIN SELECT CAST(1 AS BIT); COMMIT; END - ELSE BEGIN SELECT CAST(0 AS BIT); ROLLBACK; END", - parameters => parameters - .Add(nameof(model.OrderNr), model.OrderNr) - .Add(nameof(model.PositionNr), model.PositionNr), - reader => reader.GetValue(0)); + reader => reader.GetValue()); + + private bool UpdatePalletsNr(int orderNo) + => this.sqlClient.ExecuteNonQuery( + query: $@"UPDATE [PalettenScan] + SET [PalettenNr] = [P2].[RowNumber] + FROM [PalettenScan] AS [P1] + JOIN (SELECT [P].[PalettenNr] + , ROW_NUMBER() OVER (ORDER BY [P].[PalettenNr]) AS [RowNumber] + FROM (SELECT DISTINCT + [PalettenNr] + FROM [PalettenScan] + WHERE [AuftragNr] = @{nameof(orderNo)}) + AS [P]) + AS [P2] + ON [P2].[PalettenNr] = [P1].[PalettenNr]", + parameters: parameters => parameters.Add(nameof(orderNo), orderNo)) == 1; } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/SessionUser.cs b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/SessionUser.cs index acaa02bb..e560764f 100644 --- a/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/SessionUser.cs +++ b/LaaProductionWeb/LaaProductionWeb/App_Infrastructure/SessionUser.cs @@ -1,6 +1,9 @@ namespace LaaProductionWeb.App_Infrastructure { + using Newtonsoft.Json; + using System; + using System.Net; using System.Web; public class SessionUser @@ -25,14 +28,16 @@ { public static int UserId(this HttpContextBase httpContext) { - var cookieUserId = httpContext - .Request - .Cookies - .Get(nameof(SessionUser)) - ?.Value ?? string.Empty; + var cookies = httpContext.Request.Cookies; + var userCookie = cookies.Get(nameof(SessionUser)); + var cookieUserId = userCookie?.Value ?? string.Empty; if (int.TryParse(cookieUserId, out int userId)) { + userCookie.Expires = DateTime.Now.AddSeconds(ApplicationSettings.LoginTimeout); + + cookies.Set(userCookie); + return userId; } @@ -57,17 +62,58 @@ public static void SignOut(this HttpContextBase httpContext) { var cookieUserId = httpContext - .Request + .Response .Cookies .Get(nameof(SessionUser)) ?.Value ?? string.Empty; } + const string DATA = nameof(DATA); + + public static void SetUserData(this HttpContextBase httpContext, object data) + { + if (data != null) + { + var userId = httpContext.UserId(); + var json = JsonConvert.SerializeObject(data); + var encoded = WebUtility.UrlEncode(json); + var cookie = new HttpCookie($"{DATA}_{userId}") + { + Value = encoded, + Expires = DateTime.Now.AddHours(7) + }; + + httpContext.Response.Cookies.Set(cookie); + } + } + + public static T GetUserData(this HttpContextBase httpContext) + { + var userId = httpContext.UserId(); + var cookie = httpContext.Request.Cookies.Get($"{DATA}_{userId}"); + + if (cookie != null && cookie.Value != null) + { + try + { + var json = WebUtility.UrlDecode(cookie.Value); + + return JsonConvert.DeserializeObject(json); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + + return default(T); + } + const string URL_REFERER = nameof(URL_REFERER); public static void SetUrlReferer(this HttpContextBase httpContext) => httpContext - .Request + .Response .Cookies .Add(new HttpCookie(URL_REFERER, $"{httpContext.Request.Url}")); diff --git a/LaaProductionWeb/LaaProductionWeb/Controllers/ShipmentsController.cs b/LaaProductionWeb/LaaProductionWeb/Controllers/ShipmentsController.cs index 0676e67f..fdb23f70 100644 --- a/LaaProductionWeb/LaaProductionWeb/Controllers/ShipmentsController.cs +++ b/LaaProductionWeb/LaaProductionWeb/Controllers/ShipmentsController.cs @@ -9,6 +9,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; + using System.Net; + using System.Threading.Tasks; using System.Web.Mvc; using SystemFile = System.IO.File; @@ -17,17 +19,40 @@ public class ShipmentsController : Controller { private readonly IShipmentsService shipmentsService; + private readonly IHttpService httpService; - public ShipmentsController(IShipmentsService shipmentsService) - => this.shipmentsService = shipmentsService; + public ShipmentsController(IShipmentsService shipmentsService, IHttpService httpService) + { + this.shipmentsService = shipmentsService; + this.httpService = httpService; + } [HttpGet] - public ActionResult Index() - => this.View(new ShipmentModel()); + public ActionResult Index() + { + var shipmentModel = new ShipmentModel(); + + var orderScanModel = this.HttpContext.GetUserData(); + + if (orderScanModel != null) + { + shipmentModel = new ShipmentModel(orderScanModel); + } + + return this.View(shipmentModel); + } [HttpPost] - public ActionResult Index(ShipmentModel model) - => this.View(model); + public ActionResult Index(ShipmentModel model) + { + this.HttpContext.SetUserData(model.OrderScanModel as OrderScanModel); + + return this.View(model); + } + + [HttpGet] + public ActionResult Pallet(OrderScanModel model) + => this.View(nameof(this.Index), new ShipmentModel(model)); public ActionResult Batch(OrderScanModel model) { @@ -39,17 +64,26 @@ return this.PartialView(batchModel); } - public ActionResult Scan(OrderScanModel model) + [HttpGet] + public ActionResult Print(OrderScanModel model) { - var palletsCount = this.shipmentsService.GetPalletsCount(model); + var batchModel = this.shipmentsService.LoadBatchModel(model); + var batchPrintModel = new BatchPrintModel(batchModel); - model.UpdatePalletsCount(palletsCount); + return this.View(batchPrintModel); + } + + public ActionResult Scan(OrderScanModel model) + { + var pallets = this.shipmentsService.LoadPallets(model); + + model.UpdatePallets(pallets); return this.PartialView(model); } [HttpPost] - public ActionResult ScanPost(OrderScanModel model) + public async Task ScanPost(OrderScanModel model) { if (this.ModelState.IsValid) { @@ -61,7 +95,11 @@ if (result) { + await this.httpService.GetAsync($"/Shipment/Scanned/Serial/{WebUtility.UrlEncode(model.SerialNr)}"); + model.SerialNr = default(string); + + this.HttpContext.SetUserData(model); } else { @@ -79,15 +117,6 @@ return this.View(nameof(this.Index), new ShipmentModel(orderScanModel)); } - - [HttpGet] - public ActionResult Print(OrderScanModel model) - { - var batchModel = this.shipmentsService.LoadBatchModel(model); - var batchPrintModel = new BatchPrintModel(batchModel); - - return this.View(batchPrintModel); - } [HttpGet] public ActionResult Download(OrderScanModel model) @@ -112,7 +141,7 @@ { if (additionalText.Contains(kvp.Key)) { - batchModel.ClientName = kvp.Value; + batchModel.Customer = kvp.Value; break; } diff --git a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj index f5ef7306..64220d2a 100644 --- a/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj +++ b/LaaProductionWeb/LaaProductionWeb/LaaProductionWeb.csproj @@ -18,8 +18,8 @@ true - disabled - enabled + enabled + disabled diff --git a/LaaProductionWeb/LaaProductionWeb/Views/Search/Orders.cshtml b/LaaProductionWeb/LaaProductionWeb/Views/Search/Orders.cshtml index 7f32e665..b54dc988 100644 --- a/LaaProductionWeb/LaaProductionWeb/Views/Search/Orders.cshtml +++ b/LaaProductionWeb/LaaProductionWeb/Views/Search/Orders.cshtml @@ -2,5 +2,5 @@ @foreach (var order in this.Model) { -
  • @($"{order.OrderNr, 10} / {order.ProductionOrderNr, 10}") - @order.ClientName
  • +
  • @($"{order.OrderNr, 10} / {order.ProductionOrderNr, 10}") - @order.Customer
  • } diff --git a/LaaProductionWeb/LaaProductionWeb/Views/Shipments/Batch.cshtml b/LaaProductionWeb/LaaProductionWeb/Views/Shipments/Batch.cshtml index badd7eb3..ce4c365b 100644 --- a/LaaProductionWeb/LaaProductionWeb/Views/Shipments/Batch.cshtml +++ b/LaaProductionWeb/LaaProductionWeb/Views/Shipments/Batch.cshtml @@ -2,7 +2,7 @@ @model PalletsBatchModel -@if (!string.IsNullOrWhiteSpace(this.Model.ClientName)) +@if (!string.IsNullOrWhiteSpace(this.Model.Customer)) {