From 917fbc21106e910dfe0ae5b720495588f23d8131 Mon Sep 17 00:00:00 2001 From: Sharif Jubayer Arefin Date: Thu, 23 Jul 2020 16:51:45 +0600 Subject: [PATCH] Update for Issue #46 - Implements SalesOrder Transfer feature - Domain Test (WIP) - Updates Clone Function - Updates Derivation --- Base/Database/Diagrams/Temp2.cd | 111 +++--------- .../Order/SalesOrderTransferTests.cs | 161 ++++++++++++++++-- .../Accounting/PartyFinancialRelationship.cs | 8 + .../Domain/Base/Invoice/SalesInvoice.cs | 1 - .../Domain/Base/Order/SalesOrderTransfer.cs | 11 ++ .../Domain/Base/Relation/PartyExtensions.cs | 14 -- Core/Database/Domain/Core/ObjectExtensions.cs | 10 +- 7 files changed, 204 insertions(+), 112 deletions(-) diff --git a/Base/Database/Diagrams/Temp2.cd b/Base/Database/Diagrams/Temp2.cd index 3a61ac296f..0352ef5d08 100644 --- a/Base/Database/Diagrams/Temp2.cd +++ b/Base/Database/Diagrams/Temp2.cd @@ -1,120 +1,63 @@  - - - - CAAKJDkAEiAAAAAABAgAABAAAIABgCtAEACAATCAgAk= - Generated\domain.g.cs - - - - - - - - - - - - - - - AAAAAACAAAAAAAAIAAAAAAAAAAAAAAAAAAAAgAAAAAA= - Generated\domain.g.cs - - - - - - - - - - ACEAAAAAAUAgCAAAAAAAAAAAgBAAAAAAQIAAIAAAgQI= - Generated\domain.g.cs - - - - - - AAAAAAQAIgAAAIAAAEQIAAAAAIAgAEBSAAAAgCAAAAE= - Generated\domain.g.cs - - - - - - - + + - CAgAAogEAAAEEAAAAAAAAYAAAkAQQCBCAhZAIQAAAEA= + Cg9BAQhAAAQAAgQRABAABAAAoEAAtBgQAIQQGzLGQoA= Generated\domain.g.cs - + + + + + + + - - + + - CAoBAQhAhgCAggYRhBABACAAgEAGtAhQAAQQAWJGggQ= + WgTAAAGESRYAAAKAUExQJxMKBgmtgoIACAECgAgxSAA= Generated\domain.g.cs - - - - - + + - AAAAAAgAAAAAAAAAAAAAAACAAAAAAAgAAAAAgIAAAAA= + AEAACAABAAAABAAAEAAAAAAAAAAAAADgASCACAAgAIA= Generated\domain.g.cs - - + + - - + + - AAAIIAAAIAAAAAAAAAAAAQAAAAgAAAAAAAAAAAAAAAA= + wYAoGAAAAoAAAIAQgEgACAgAQIARAhAQFYIFQANAogA= Generated\domain.g.cs - - - - - + + - AAAIIAAAIAAIAAIAAACAgAAAAAgAAAAEAAAAkCAAAAA= + KACKAAkAAAAAAIAhhQhAQAiAAoABAQqAAQGiGCRAgAE= Generated\domain.g.cs - + + - + ABAAgACAAAAgAAAAAAAAAAQAAAAAAAABAAAAAEAAAAA= Generated\domain.g.cs - - - - AAAAAAgAAACAACAABAAAAAMAAAAAAogEAAAAAAACAAA= - Generated\domain.g.cs - - - - - - AAAAAAkAAAAgAAAAAAAAgAAAAAIAAIgAAAAAACAAAAA= - Generated\domain.g.cs - - \ No newline at end of file diff --git a/Base/Database/Domain.Tests/Order/SalesOrderTransferTests.cs b/Base/Database/Domain.Tests/Order/SalesOrderTransferTests.cs index 494643c13c..68027abedd 100644 --- a/Base/Database/Domain.Tests/Order/SalesOrderTransferTests.cs +++ b/Base/Database/Domain.Tests/Order/SalesOrderTransferTests.cs @@ -2690,20 +2690,161 @@ public class SalesOrderTransferSecurityTests : DomainTest [Fact] public void GivenSalesOrderBuilder_WhenProvisional_ThenOrderCanTransfer() { - var customer = new PersonBuilder(this.Session).WithFirstName("Koen").Build(); + var customer = new PersonBuilder(this.Session).WithFirstName("Jubayer").Build(); var internalOrganisation = this.InternalOrganisation; - var anotherInternalOrganisation = new OrganisationBuilder(this.Session) - .WithIsInternalOrganisation(true) - .WithDoAccounting(false) - .WithName("another internalOrganisation") - .WithPreferredCurrency(new Currencies(this.Session).CurrencyByCode["EUR"]) - .WithIncomingShipmentNumberPrefix("incoming shipmentno: ") - .WithPurchaseInvoiceNumberPrefix("incoming invoiceno: ") - .WithPurchaseOrderNumberPrefix("purchase orderno: ") - .WithSubAccountCounter(new CounterBuilder(this.Session).WithUniqueId(Guid.NewGuid()).WithValue(0).Build()) + //Another InternalOrg Begins + var session = this.Session; + var singleton = session.GetSingleton(); + + var belgium = new Countries(session).CountryByIsoCode["BE"]; + var euro = belgium.Currency; + + var bank = new BankBuilder(session).WithCountry(belgium).WithName("ING België").WithBic("BBRUBEBB").Build(); + + var ownBankAccount = new OwnBankAccountBuilder(session) + .WithBankAccount(new BankAccountBuilder(session).WithBank(bank) + .WithCurrency(euro) + .WithIban("BE24557215223438") + .WithNameOnAccount("Jubayer") + .Build()) + .WithDescription("Main bank account") + .Build(); + + var postalAddress = new PostalAddressBuilder(session) + .WithAddress1("Kleine Nieuwedijkstraat 2") + .WithLocality("Mechelen") + .WithCountry(belgium) + .Build(); + + var anotherInternalOrganisation = new OrganisationBuilder(session) + .WithIsInternalOrganisation(true) + .WithDoAccounting(false) + .WithName("internalOrganisation") + .WithPreferredCurrency(new Currencies(session).CurrencyByCode["EUR"]) + .WithIncomingShipmentNumberPrefix("incoming shipmentno: ") + .WithPurchaseInvoiceNumberPrefix("incoming invoiceno: ") + .WithPurchaseOrderNumberPrefix("purchase orderno: ") + .WithDefaultCollectionMethod(ownBankAccount) + .WithSubAccountCounter(new CounterBuilder(session).WithUniqueId(Guid.NewGuid()).WithValue(0).Build()) + .Build(); + + anotherInternalOrganisation.AddPartyContactMechanism(new PartyContactMechanismBuilder(session) + .WithUseAsDefault(true) + .WithContactMechanism(postalAddress) + .WithContactPurpose(new ContactMechanismPurposes(session).GeneralCorrespondence) + .WithContactPurpose(new ContactMechanismPurposes(session).BillingAddress) + .WithContactPurpose(new ContactMechanismPurposes(session).ShippingAddress) + .Build()); + + var facility = new FacilityBuilder(session) + .WithFacilityType(new FacilityTypes(session).Warehouse) + .WithName("facility") + .WithOwner(anotherInternalOrganisation) + .Build(); + + singleton.Settings.DefaultFacility = facility; + + var collectionMethod = new PaymentMethods(session).Extent().First; + + new StoreBuilder(session) + .WithName("store") + .WithBillingProcess(new BillingProcesses(session).BillingForShipmentItems) + .WithInternalOrganisation(anotherInternalOrganisation) + .WithOutgoingShipmentNumberPrefix("shipmentno: ") + .WithSalesInvoiceNumberPrefix("invoiceno: ") + .WithSalesOrderNumberPrefix("orderno: ") + .WithDefaultShipmentMethod(new ShipmentMethods(session).Ground) + .WithDefaultCarrier(new Carriers(session).Fedex) + .WithCreditLimit(500) + .WithPaymentGracePeriod(10) + .WithDefaultCollectionMethod(collectionMethod) + .WithIsImmediatelyPicked(false) + .WithAutoGenerateShipmentPackage(false) + .WithIsImmediatelyPacked(true) + .Build(); + + new ProductCategoryBuilder(session).WithName("Primary Category").Build(); + + anotherInternalOrganisation.CreateB2BCustomer(session.Faker()); + anotherInternalOrganisation.CreateB2CCustomer(session.Faker()); + anotherInternalOrganisation.CreateSupplier(session.Faker()); + anotherInternalOrganisation.CreateSubContractor(session.Faker()); + + var purchaser = new PersonBuilder(session).WithFirstName("Mr").WithLastName("Purchaser").WithUserName("anotherPurchaser").Build(); + var orderProcessor = new PersonBuilder(session).WithFirstName("Mr").WithLastName("OrderProcessor").WithUserName("anotherOrderProcessor").Build(); + + // Adding newly created persons to EmployeeUserGroup as employees do not have any permission when created + var employeesUserGroup = new UserGroups(session).Employees; + employeesUserGroup.AddMember(purchaser); + employeesUserGroup.AddMember(orderProcessor); + + new UserGroups(session).Creators.AddMember(purchaser); + new UserGroups(session).Creators.AddMember(orderProcessor); + + new EmploymentBuilder(session).WithFromDate(session.Now()).WithEmployee(purchaser).WithEmployer(anotherInternalOrganisation).Build(); + + new EmploymentBuilder(session).WithFromDate(session.Now()).WithEmployee(orderProcessor).WithEmployer(anotherInternalOrganisation).Build(); + + var good1 = new NonUnifiedGoodBuilder(session) + .WithProductIdentification(new ProductNumberBuilder(session) + .WithIdentification("1") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Good).Build()) + .WithName("good1") + .WithUnitOfMeasure(new UnitsOfMeasure(session).Piece) + .WithPart(new NonUnifiedPartBuilder(session) + .WithProductIdentification(new PartNumberBuilder(session) + .WithIdentification("1") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Part).Build()) + .WithInventoryItemKind(new InventoryItemKinds(session).NonSerialised).Build()) + .Build(); + + var good2 = new NonUnifiedGoodBuilder(session) + .WithProductIdentification(new ProductNumberBuilder(session) + .WithIdentification("2") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Good).Build()) + .WithName("good2") + .WithUnitOfMeasure(new UnitsOfMeasure(session).Piece) + .WithPart(new NonUnifiedPartBuilder(session) + .WithProductIdentification(new PartNumberBuilder(session) + .WithIdentification("2") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Part).Build()) + .WithInventoryItemKind(new InventoryItemKinds(session).NonSerialised).Build()) + .Build(); + + var good3 = new NonUnifiedGoodBuilder(session) + .WithProductIdentification(new ProductNumberBuilder(session) + .WithIdentification("3") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Good).Build()) + .WithName("good3") + .WithUnitOfMeasure(new UnitsOfMeasure(session).Piece) + .WithPart(new NonUnifiedPartBuilder(session) + .WithProductIdentification(new PartNumberBuilder(session) + .WithIdentification("3") + .WithProductIdentificationType(new ProductIdentificationTypes(session).Part).Build()) + .WithInventoryItemKind(new InventoryItemKinds(session).NonSerialised).Build()) + .Build(); + + var catMain = new ProductCategoryBuilder(session).WithName("main cat").Build(); + + new ProductCategoryBuilder(session) + .WithName("cat for good1") + .WithPrimaryParent(catMain) + .WithProduct(good1) .Build(); + new ProductCategoryBuilder(session) + .WithName("cat for good2") + .WithPrimaryParent(catMain) + .WithProduct(good2) + .WithProduct(good3) + .Build(); + + session.Derive(); + session.Commit(); + + //Another InternalOrg Ends + new CustomerRelationshipBuilder(this.Session).WithFromDate(this.Session.Now()).WithCustomer(customer).WithInternalOrganisation(internalOrganisation).Build(); var mechelen = new CityBuilder(this.Session).WithName("Mechelen").Build(); diff --git a/Base/Database/Domain/Base/Accounting/PartyFinancialRelationship.cs b/Base/Database/Domain/Base/Accounting/PartyFinancialRelationship.cs index ee662c68f1..498fd48347 100644 --- a/Base/Database/Domain/Base/Accounting/PartyFinancialRelationship.cs +++ b/Base/Database/Domain/Base/Accounting/PartyFinancialRelationship.cs @@ -9,6 +9,14 @@ namespace Allors.Domain public partial class PartyFinancialRelationship { + public void BaseOnPreDerive(ObjectOnPreDerive method) + { + var (iteration, changeSet, derivedObjects) = method; + + iteration.AddDependency(this, this.Party.SalesOrdersWhereBillToCustomer); + iteration.AddDependency(this, this.Party.SalesInvoicesWhereBillToCustomer); + } + public void BaseOnDerive(ObjectOnDerive method) { var party = this.Party; diff --git a/Base/Database/Domain/Base/Invoice/SalesInvoice.cs b/Base/Database/Domain/Base/Invoice/SalesInvoice.cs index 44460c76ad..f0daa7ccf9 100644 --- a/Base/Database/Domain/Base/Invoice/SalesInvoice.cs +++ b/Base/Database/Domain/Base/Invoice/SalesInvoice.cs @@ -101,7 +101,6 @@ public void BaseOnPreDerive(ObjectOnPreDerive method) if (iteration.IsMarked(this) || changeSet.IsCreated(this) || changeSet.HasChangedRoles(this)) { - if (this.ExistBillToCustomer) { var customerRelationships = this.BillToCustomer.CustomerRelationshipsWhereCustomer; diff --git a/Base/Database/Domain/Base/Order/SalesOrderTransfer.cs b/Base/Database/Domain/Base/Order/SalesOrderTransfer.cs index 541f4d0419..028756e348 100644 --- a/Base/Database/Domain/Base/Order/SalesOrderTransfer.cs +++ b/Base/Database/Domain/Base/Order/SalesOrderTransfer.cs @@ -44,6 +44,17 @@ public void BaseOnDerive(ObjectOnDerive method) else { this.To = this.From.Clone(this.From.Meta.SalesOrderItems); + this.To.TakenBy = this.InternalOrganisation; + + // TODO: Make sure 'from' customer is also a customer in 'to' internal organisation + if (!this.To.TakenBy.ActiveCustomers.Contains(this.To.BillToCustomer)) + { + new CustomerRelationshipBuilder(this.Strategy.Session) + .WithInternalOrganisation(this.To.TakenBy) + .WithCustomer(this.To.BillToCustomer) + .Build(); + } + this.From.SalesOrderState = new SalesOrderStates(this.strategy.Session).Transferred; } } diff --git a/Base/Database/Domain/Base/Relation/PartyExtensions.cs b/Base/Database/Domain/Base/Relation/PartyExtensions.cs index 317823c276..a2fbdbf312 100644 --- a/Base/Database/Domain/Base/Relation/PartyExtensions.cs +++ b/Base/Database/Domain/Base/Relation/PartyExtensions.cs @@ -22,20 +22,6 @@ public static void BaseOnBuild(this Party @this, ObjectOnBuild method) } } - public static void BaseOnPreDerive(this Party @this, ObjectOnPreDerive method) - { - var (iteration, changeSet, derivedObjects) = method; - - if (iteration.IsMarked(@this) || changeSet.IsCreated(@this) || changeSet.HasChangedRoles(@this)) - { - foreach (PartyFinancialRelationship partyFinancialRelationship in @this.PartyFinancialRelationshipsWhereParty) - { - iteration.AddDependency(partyFinancialRelationship, @this); - iteration.Mark(partyFinancialRelationship); - } - } - } - public static void BaseOnDerive(this Party @this, ObjectOnDerive method) { @this.DerivedRoles.BillingAddress = null; diff --git a/Core/Database/Domain/Core/ObjectExtensions.cs b/Core/Database/Domain/Core/ObjectExtensions.cs index 0c646f1f35..bce496a4bf 100644 --- a/Core/Database/Domain/Core/ObjectExtensions.cs +++ b/Core/Database/Domain/Core/ObjectExtensions.cs @@ -60,12 +60,16 @@ public static T Clone(this T @this, params IRoleType[] deepClone) where T: IO var session = strategy.Session; var @class = strategy.Class; - var clone = (T)session.Create(@class); + var clone = (T)ObjectBuilder.Build(session, @class); + foreach (var roleType in @class.RoleTypes.Where(v => !(v.RelationType.IsDerived || v.RelationType.IsSynced) && !deepClone.Contains(v) && (v.ObjectType.IsUnit || v.AssociationType.IsMany))) { var relationType = roleType.RelationType; - var role = @this.Strategy.GetRole(relationType); - clone.Strategy.SetRole(relationType, role); + if (!clone.Strategy.ExistRole(relationType)) + { + var role = @this.Strategy.GetRole(relationType); + clone.Strategy.SetRole(relationType, role); + } } foreach(var roleType in deepClone)