using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.Administration.Settings; using Centron.BusinessLogic.EmployeeArea; using Centron.BusinessLogic.Sales.Receipts.DataAndResults; using Centron.BusinessLogic.WebServices.Sales.Receipts; using Centron.Data.Entities.Administration.Logins; using Centron.Data.WebServices.Sales.Receipts; using Centron.Data.WebServices.Sales.Receipts.Offers; using Centron.Data.WebServices.Sales.Receipts.Orders; using Centron.Interfaces; using Centron.Interfaces.Administration.Environment; using Centron.Interfaces.Sales.Receipts; using Centron.Interfaces.Sales.Receipts.InsertReceipts; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.EntitiesWrongPlace.Sales.Receipts.DataAndResults; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.TicketTests.Ticket110400 { public class Ticket110400Test : EndToEndTest { public Ticket110400Test(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { this.ValidateOfferStateAfterForwarding(CloseOfferSetting.Never, quantityComplete: 10, quantityForward: 0, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.Never, quantityComplete: 10, quantityForward: 5, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.Never, quantityComplete: 10, quantityForward: 10, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenPartlyForwarded, quantityComplete: 10, quantityForward: 0, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenPartlyForwarded, quantityComplete: 10, quantityForward: 5, ReceiptState.Completed); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenPartlyForwarded, quantityComplete: 10, quantityForward: 10, ReceiptState.Completed); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenCompletelyForwarded, quantityComplete: 10, quantityForward: 0, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenCompletelyForwarded, quantityComplete: 10, quantityForward: 5, ReceiptState.Active); this.ValidateOfferStateAfterForwarding(CloseOfferSetting.WhenCompletelyForwarded, quantityComplete: 10, quantityForward: 10, ReceiptState.Completed); } private enum CloseOfferSetting { Never = 1, WhenPartlyForwarded = 2, WhenCompletelyForwarded = 3 } private void ValidateOfferStateAfterForwarding(CloseOfferSetting setting, int quantityComplete, int quantityForward, ReceiptState expectedState) { this.UpdateSetting(setting); // Create Offer var offer = this.CreateNewOffer(); var item = this.CreateReceiptItem(offer); item.QuantityComplete = quantityComplete; offer.Items.Add((ReceiptOfferItemDTO)item); // Save Offer var savedOffer = (ReceiptOfferDTO)this.SaveReceipt(offer); // Forward to Order var order = this.ForwardToOrder(savedOffer); order.Items.Single(f => f.Kind == ReceiptItemKind.Article).QuantityComplete = quantityForward; // Save Order this.SaveReceipt(order); // Check Offer state var reloadedOffer = this.GetOffer(savedOffer.I3D); Assert.Equal(expectedState, reloadedOffer.State); } private void UpdateSetting(CloseOfferSetting setting) { using (var session = new BLSession()) { var updateSettings = session.GetBL().GetSettingsForUpdate(AppSettingsConst.CloseOfferAutomatically); updateSettings.UpdateInt(AppSettingsConst.CloseOfferAutomatically, (int)setting); updateSettings.SaveSettings(); } } private ReceiptOfferDTO CreateNewOffer() { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var result = session.GetBL().CreateNewReceipt(CentronObjectKindNumeric.OfferClass, new LoggedInUser(user), 10022, new CreateReceiptData(), null, null, true, false, true); return (ReceiptOfferDTO)result.Data.Receipt; } } private ReceiptItemBaseDTO CreateReceiptItem(ReceiptBaseDTO receipt) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var receiptItemsResult = session.GetBL().CreateArticleReceiptItems( receipt, false, 4348, null, null, -1, new LoggedInUser(user)); return receiptItemsResult.Data.ReceiptItems.Single(f => f.ArticleI3D == 4348); } } private ReceiptBaseDTO SaveReceipt(ReceiptBaseDTO originReceipt) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var saveReceiptResult = session.GetBL().SaveReceipt( originReceipt, user, new SaveReceiptData { IgnoreCallbacks = true }, autoLockIfNewReceipt: false, CreatedThroughApplication.WebServices, receiptTemplateFolderI3D: null); return saveReceiptResult.Data.Receipt; } } private ReceiptOrderDTO ForwardToOrder(ReceiptOfferDTO originReceipt) { using (var session = new BLSession()) { var user = session.GetBL().GetAppUser(f => f.I3D == 11); var forwardResult = session.GetBL().ForwardReceipt( CentronObjectKindNumeric.OrderClass, user, new ForwardReceiptData { CreateReceiptData = new CreateReceiptData { IgnoreCallbacks = true, }, CreateReceiptItemsFromExistingItemsData = new List { new() { IgnoreCallbacks = true, }, }, IgnoreCallbacks = true }, new List { new ReceiptToForward { ReceiptKind = originReceipt.ReceiptKind, ReceiptI3D = originReceipt.I3D } }, null, InsertReceiptTakeoverOptions.Reference, true, false); return (ReceiptOrderDTO)forwardResult.Data.Receipt; } } private ReceiptOfferDTO GetOffer(int offerI3D) { using (var session = new BLSession()) { var result = session.GetBL().GetReceiptByI3D(offerI3D, CentronObjectKindNumeric.OfferClass); return (ReceiptOfferDTO)result.Data; } } } }