using System.Collections.Generic;
using System.Linq;
using Centron.BusinessLogic;
using Centron.BusinessLogic.EmployeeArea;
using Centron.BusinessLogic.Warehousing;
using Centron.BusinessLogic.WebServices.Warehousing;
using Centron.Data.Entities.Administration.Logins;
using Centron.Data.Entities.Warehousing.StockManagement;
using Centron.Interfaces.BL;
using Centron.Tests.EndToEnd.Infrastructure;
using Xunit;
using Xunit.Abstractions;
namespace Centron.Tests.EndToEnd.TicketTests.Ticket155581;
///
/// Ticket 155581: Miet-/Portal-Artikel must not persist Abbuchung / SN-Pflicht when inconsistent with WG-Übernahme or API payload.
/// After PR 20939 the override was silent; ticket 155581 follow-up adds the audit trail (ARTIKlog) for each flipped flag.
///
public class Ticket155581RentArticleSaveNormalizesDebitingAndSerialTest : EndToEndTest
{
private const int ArticleI3D = 52;
private const int AppUserI3D = 11;
public Ticket155581RentArticleSaveNormalizesDebitingAndSerialTest(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}
public override void Execute()
{
var user = WithSession(f => f.GetBL().GetAppUser(f => f.I3D == AppUserI3D));
var loggedIn = new LoggedInUser(user);
// Align with Ticket126856: SN-Flag changes require no active stock / open barcode rows
base.Sql($"UPDATE Barcode SET Status = 20 WHERE ArtikelI3D = {ArticleI3D} AND Status IN (1,2, 8)");
base.Sql($"UPDATE ARTIK SET Menge = 0 WHERE I3D = {ArticleI3D}");
var beforeSnapshot = WithSession(f => f.GetBL().GetArticleLogs(new ArticleLogFilter { ArticleI3Ds = new[] { ArticleI3D } }) ?? new List())
.Select(l => (l.I3D, l.Kind, l.NewValue, l.Message))
.ToHashSet();
var article = WithSession(f => f.GetBL().GetArticleByArticleI3D(ArticleI3D));
article.IsRentArticle = true;
article.ChangeStock = true;
article.ScanBarcode = true;
var saveResult = WithSession(f => f.GetBL().SaveArticle(article, loggedIn));
Assert.Equal(ResultStatus.Success, saveResult.Status);
var reloaded = WithSession(f => f.GetBL().GetArticleByArticleI3D(ArticleI3D));
Assert.True(reloaded.IsRentArticle);
Assert.False(reloaded.ChangeStock);
Assert.False(reloaded.ScanBarcode);
var logsAfter = WithSession(f => f.GetBL().GetArticleLogs(new ArticleLogFilter { ArticleI3Ds = new[] { ArticleI3D } }));
var newLogs = logsAfter
.Where(l => !beforeSnapshot.Contains((l.I3D, l.Kind, l.NewValue, l.Message)))
.ToList();
Assert.Contains(newLogs, l => l.Kind == ArticleLogKind.DebigEntry && l.NewValue == "aus" && l.Message.Contains("Speichern"));
Assert.Contains(newLogs, l => l.Kind == ArticleLogKind.SerialNumberAtOutflow && l.NewValue == "aus" && l.Message.Contains("Speichern"));
}
}