Example #1
0
        public void BaseOnDeriveQuantityDecreased(ShipmentItem shipmentItem, SalesOrderItem orderItem, decimal correction)
        {
            var remainingCorrection = correction;

            var inventoryAssignment = orderItem.SalesOrderItemInventoryAssignments.FirstOrDefault();

            if (inventoryAssignment != null)
            {
                inventoryAssignment.Quantity = orderItem.QuantityCommittedOut - correction;
            }

            foreach (OrderShipment orderShipment in shipmentItem.OrderShipmentsWhereShipmentItem)
            {
                if (orderShipment.OrderItem.Equals(orderItem) && remainingCorrection > 0)
                {
                    decimal quantity;
                    if (orderShipment.Quantity < remainingCorrection)
                    {
                        quantity             = orderShipment.Quantity;
                        remainingCorrection -= quantity;
                    }
                    else
                    {
                        quantity            = remainingCorrection;
                        remainingCorrection = 0;
                    }

                    shipmentItem.Quantity -= quantity;

                    var itemIssuanceCorrection = quantity;
                    foreach (ItemIssuance itemIssuance in shipmentItem.ItemIssuancesWhereShipmentItem)
                    {
                        decimal subQuantity;
                        if (itemIssuance.Quantity < itemIssuanceCorrection)
                        {
                            subQuantity             = itemIssuance.Quantity;
                            itemIssuanceCorrection -= quantity;
                        }
                        else
                        {
                            subQuantity            = itemIssuanceCorrection;
                            itemIssuanceCorrection = 0;
                        }

                        itemIssuance.Quantity -= subQuantity;

                        if (itemIssuanceCorrection == 0)
                        {
                            break;
                        }
                    }
                }
            }

            if (shipmentItem.Quantity == 0)
            {
                foreach (OrderShipment orderShipment in shipmentItem.OrderShipmentsWhereShipmentItem)
                {
                    foreach (ItemIssuance itemIssuance in orderShipment.ShipmentItem.ItemIssuancesWhereShipmentItem)
                    {
                        if (!itemIssuance.PickListItem.PickListWherePickListItem.ExistPicker && itemIssuance.Quantity == 0)
                        {
                            itemIssuance.Delete();
                        }
                    }

                    orderShipment.Delete();
                }

                shipmentItem.Delete();
            }

            if (!this.ExistShipmentItems)
            {
                this.Cancel();

                if (this.PendingPickList != null)
                {
                    this.PendingPickList.Cancel();
                }
            }
        }
Example #2
0
        private CustomerShipment AppsShipToAddress(IDerivation derivation, KeyValuePair <PostalAddress, Party> address)
        {
            var pendingShipment = address.Value.AppsGetPendingCustomerShipmentForStore(address.Key, this.Store, this.ShipmentMethod);

            if (pendingShipment == null)
            {
                pendingShipment = new CustomerShipmentBuilder(this.Strategy.Session)
                                  .WithShipFromAddress(this.TakenBy.ShippingAddress)
                                  .WithBillToParty(this.BillToCustomer)
                                  .WithBillToContactMechanism(this.BillToEndCustomerContactMechanism)
                                  .WithShipToAddress(address.Key)
                                  .WithShipToParty(address.Value)
                                  .WithShipmentPackage(new ShipmentPackageBuilder(this.Strategy.Session).Build())
                                  .WithStore(this.Store)
                                  .WithShipmentMethod(this.ShipmentMethod)
                                  .WithPaymentMethod(this.PaymentMethod)
                                  .Build();
            }

            foreach (SalesOrderItem orderItem in this.ValidOrderItems)
            {
                if (orderItem.ExistProduct && orderItem.ShipToAddress.Equals(address.Key) && orderItem.QuantityRequestsShipping > 0)
                {
                    var good = orderItem.Product as Good;

                    ShipmentItem shipmentItem = null;
                    foreach (ShipmentItem item in pendingShipment.ShipmentItems)
                    {
                        if (item.Good.Equals(good))
                        {
                            shipmentItem = item;
                            break;
                        }
                    }

                    if (shipmentItem != null)
                    {
                        shipmentItem.Quantity           += orderItem.QuantityRequestsShipping;
                        shipmentItem.ContentsDescription = $"{shipmentItem.Quantity} * {good}";
                    }
                    else
                    {
                        shipmentItem = new ShipmentItemBuilder(this.Strategy.Session)
                                       .WithGood(good)
                                       .WithQuantity(orderItem.QuantityRequestsShipping)
                                       .WithContentsDescription($"{orderItem.QuantityRequestsShipping} * {good}")
                                       .Build();

                        pendingShipment.AddShipmentItem(shipmentItem);
                    }

                    foreach (SalesOrderItem featureItem in orderItem.OrderedWithFeatures)
                    {
                        shipmentItem.AddProductFeature(featureItem.ProductFeature);
                    }

                    var orderShipmentsWhereShipmentItem = shipmentItem.OrderShipmentsWhereShipmentItem;
                    orderShipmentsWhereShipmentItem.Filter.AddEquals(M.OrderShipment.OrderItem, orderItem);

                    if (orderShipmentsWhereShipmentItem.First == null)
                    {
                        new OrderShipmentBuilder(this.Strategy.Session)
                        .WithOrderItem(orderItem)
                        .WithShipmentItem(shipmentItem)
                        .WithQuantity(orderItem.QuantityRequestsShipping)
                        .Build();
                    }
                    else
                    {
                        orderShipmentsWhereShipmentItem.First.Quantity += orderItem.QuantityRequestsShipping;
                    }

                    orderItem.AppsOnDeriveOnShip(derivation);
                }
            }

            // TODO: Check

            if (this.Store.IsAutomaticallyShipped)
            {
                pendingShipment.Ship();
            }

            pendingShipment.OnDerive(x => x.WithDerivation(derivation));
            return(pendingShipment);
        }
Example #3
0
        public void AppsOnDeriveQuantityDecreased(IDerivation derivation, ShipmentItem shipmentItem, SalesOrderItem orderItem, decimal correction)
        {
            var remainingCorrection = correction;

            foreach (OrderShipment orderShipment in shipmentItem.OrderShipmentsWhereShipmentItem)
            {
                if (orderShipment.OrderItem.Equals(orderItem) && remainingCorrection > 0)
                {
                    decimal quantity;
                    if (orderShipment.Quantity < remainingCorrection)
                    {
                        quantity             = orderShipment.Quantity;
                        remainingCorrection -= quantity;
                    }
                    else
                    {
                        quantity            = remainingCorrection;
                        remainingCorrection = 0;
                    }

                    orderShipment.Quantity -= quantity;
                    shipmentItem.Quantity  -= quantity;

                    var itemIssuanceCorrection = quantity;
                    foreach (ItemIssuance itemIssuance in shipmentItem.ItemIssuancesWhereShipmentItem)
                    {
                        if (!itemIssuance.PickListItem.PickListWherePickListItem.ExistPicker)
                        {
                            decimal subQuantity;
                            if (itemIssuance.Quantity < itemIssuanceCorrection)
                            {
                                subQuantity             = itemIssuance.Quantity;
                                itemIssuanceCorrection -= quantity;
                            }
                            else
                            {
                                subQuantity            = itemIssuanceCorrection;
                                itemIssuanceCorrection = 0;
                            }

                            itemIssuance.Quantity -= subQuantity;

                            if (itemIssuanceCorrection == 0)
                            {
                                break;
                            }
                        }
                    }

                    if (orderShipment.Quantity == 0)
                    {
                        foreach (ItemIssuance itemIssuance in orderShipment.ShipmentItem.ItemIssuancesWhereShipmentItem)
                        {
                            if (!itemIssuance.PickListItem.PickListWherePickListItem.ExistPicker)
                            {
                                itemIssuance.Delete();
                            }
                        }

                        orderShipment.Delete();
                    }
                }
            }

            if (this.PendingPickList == null)
            {
                var shipment = (CustomerShipment)shipmentItem.ShipmentWhereShipmentItem;
                this.CreateNegativePickList(derivation, shipment, orderItem, correction);
            }

            if (shipmentItem.Quantity == 0)
            {
                shipmentItem.Delete();
            }

            if (!this.ExistShipmentItems)
            {
                this.Cancel();

                if (this.PendingPickList != null)
                {
                    this.PendingPickList.Cancel();
                }
            }
        }
Example #4
0
        public void BaseQuickReceive(PurchaseOrderQuickReceive method)
        {
            var session = this.Session();

            if (this.ValidOrderItems.Any(v => ((PurchaseOrderItem)v).ExistPart))
            {
                var shipment = new PurchaseShipmentBuilder(session)
                               .WithShipmentMethod(new ShipmentMethods(session).Ground)
                               .WithShipToParty(this.OrderedBy)
                               .WithShipToAddress(this.ShipToAddress)
                               .WithShipFromParty(this.TakenViaSupplier)
                               .WithShipToFacility(this.Facility)
                               .Build();

                foreach (PurchaseOrderItem orderItem in this.ValidOrderItems)
                {
                    ShipmentItem shipmentItem = null;
                    if (orderItem.PurchaseOrderItemShipmentState.IsNotReceived && orderItem.ExistPart)
                    {
                        shipmentItem = new ShipmentItemBuilder(session)
                                       .WithPart(orderItem.Part)
                                       .WithQuantity(orderItem.QuantityOrdered)
                                       .WithUnitPurchasePrice(orderItem.UnitPrice)
                                       .WithContentsDescription($"{orderItem.QuantityOrdered} * {orderItem.Part.Name}")
                                       .Build();

                        shipment.AddShipmentItem(shipmentItem);

                        new OrderShipmentBuilder(session)
                        .WithOrderItem(orderItem)
                        .WithShipmentItem(shipmentItem)
                        .WithQuantity(orderItem.QuantityOrdered)
                        .Build();

                        new ShipmentReceiptBuilder(session)
                        .WithQuantityAccepted(orderItem.QuantityOrdered)
                        .WithShipmentItem(shipmentItem)
                        .WithOrderItem(orderItem)
                        .Build();

                        if (orderItem.Part.InventoryItemKind.Serialised)
                        {
                            var serialisedItem = orderItem.SerialisedItem;
                            if (!orderItem.ExistSerialisedItem)
                            {
                                serialisedItem = new SerialisedItemBuilder(session)
                                                 .WithSerialNumber(orderItem.SerialNumber)
                                                 .Build();

                                orderItem.Part.AddSerialisedItem(serialisedItem);
                            }

                            shipmentItem.SerialisedItem = serialisedItem;

                            // HACK: DerivedRoles (WIP)
                            var serialisedItemDeriveRoles = (SerialisedItemDerivedRoles)serialisedItem;
                            serialisedItemDeriveRoles.PurchaseOrder = this;
                            serialisedItemDeriveRoles.SuppliedBy    = this.TakenViaSupplier;
                            serialisedItem.RemoveAssignedPurchasePrice();
                            serialisedItemDeriveRoles.PurchasePrice = orderItem.TotalExVat;
                            serialisedItem.AcquiredDate             = orderItem.PurchaseOrderWherePurchaseOrderItem.OrderDate;

                            if (serialisedItem.ExistAcquiredDate && serialisedItem.ExistAcquisitionYear)
                            {
                                serialisedItem.RemoveAcquisitionYear();
                            }

                            serialisedItem.OwnedBy   = this.OrderedBy;
                            serialisedItem.Buyer     = this.OrderedBy;
                            serialisedItem.Ownership = new Ownerships(this.Session()).Own;
                            serialisedItem.SerialisedItemAvailability = new SerialisedItemAvailabilities(this.Session()).Available;

                            var inventoryItem = serialisedItem.SerialisedInventoryItemsWhereSerialisedItem
                                                .FirstOrDefault(v => v.SerialisedItem.Equals(serialisedItem) && v.Facility.Equals(this.Facility));

                            if (inventoryItem == null)
                            {
                                new SerialisedInventoryItemBuilder(this.Session())
                                .WithSerialisedItem(serialisedItem)
                                .WithSerialisedInventoryItemState(new SerialisedInventoryItemStates(this.Session()).Good)
                                .WithPart(orderItem.Part)
                                .WithUnitOfMeasure(new UnitsOfMeasure(this.Session()).Piece)
                                .WithFacility(this.Facility)
                                .Build();
                            }
                        }
                    }
                }

                if (shipment.ShipToParty is InternalOrganisation internalOrganisation)
                {
                    if (internalOrganisation.IsAutomaticallyReceived)
                    {
                        shipment.Receive();
                    }
                }
            }

            foreach (PurchaseOrderItem orderItem in this.ValidOrderItems.Where(v => !((PurchaseOrderItem)v).ExistPart))
            {
                var orderItemDerivedRoles = (PurchaseOrderItemDerivedRoles)orderItem;
                orderItemDerivedRoles.QuantityReceived = 1;
            }
        }
Example #5
0
        public void AppsOnDeriveQuantityDecreased(IDerivation derivation, ShipmentItem shipmentItem, Allors.Domain.SalesOrderItem orderItem, decimal correction)
        {
            var remainingCorrection = correction;

            foreach (OrderShipment orderShipment in shipmentItem.OrderShipmentsWhereShipmentItem)
            {
                if (orderShipment.SalesOrderItem.Equals(orderItem) && remainingCorrection > 0)
                {
                    decimal quantity;
                    if (orderShipment.Quantity < remainingCorrection)
                    {
                        quantity = orderShipment.Quantity;
                        remainingCorrection -= quantity;
                    }
                    else
                    {
                        quantity = remainingCorrection;
                        remainingCorrection = 0;
                    }

                    orderShipment.Quantity -= quantity;
                    shipmentItem.Quantity -= quantity;

                    var itemIssuanceCorrection = quantity;
                    foreach (ItemIssuance itemIssuance in shipmentItem.ItemIssuancesWhereShipmentItem)
                    {
                        if (!itemIssuance.PickListItem.PickListWherePickListItem.ExistPicker)
                        {
                            decimal subQuantity;
                            if (itemIssuance.Quantity < itemIssuanceCorrection)
                            {
                                subQuantity = itemIssuance.Quantity;
                                itemIssuanceCorrection -= quantity;
                            }
                            else
                            {
                                subQuantity = itemIssuanceCorrection;
                                itemIssuanceCorrection = 0;
                            }

                            itemIssuance.Quantity -= subQuantity;

                            if (itemIssuanceCorrection == 0)
                            {
                                break;
                            }
                        }
                    }

                    if (orderShipment.Quantity == 0)
                    {
                        foreach (ItemIssuance itemIssuance in orderShipment.ShipmentItem.ItemIssuancesWhereShipmentItem)
                        {
                            if (!itemIssuance.PickListItem.PickListWherePickListItem.ExistPicker)
                            {
                                itemIssuance.Delete();
                            }
                        }

                        orderShipment.Delete();
                    }
                }
            }

            if (this.PendingPickList == null)
            {
                var shipment = (CustomerShipment)shipmentItem.ShipmentWhereShipmentItem;
                this.CreateNegativePickList(derivation, shipment, orderItem, correction);
            }

            if (shipmentItem.Quantity == 0)
            {
                shipmentItem.Delete();
            }

            if (!this.ExistShipmentItems)
            {
                this.Cancel();

                if (this.PendingPickList != null)
                {
                    this.PendingPickList.Cancel();
                }
            }
        }
Example #6
0
        public void BaseQuickReceive(PurchaseOrderQuickReceive method)
        {
            var session = this.Session();

            if (this.ValidOrderItems.Any(v => ((PurchaseOrderItem)v).IsReceivable))
            {
                var shipment = new PurchaseShipmentBuilder(session)
                               .WithShipmentMethod(new ShipmentMethods(session).Ground)
                               .WithShipToParty(this.OrderedBy)
                               .WithShipToAddress(this.DerivedShipToAddress)
                               .WithShipFromParty(this.TakenViaSupplier)
                               .WithShipToFacility(this.StoredInFacility)
                               .Build();

                foreach (PurchaseOrderItem orderItem in this.ValidOrderItems)
                {
                    ShipmentItem shipmentItem = null;
                    if (orderItem.PurchaseOrderItemShipmentState.IsNotReceived && orderItem.ExistPart)
                    {
                        shipmentItem = new ShipmentItemBuilder(session)
                                       .WithPart(orderItem.Part)
                                       .WithStoredInFacility(orderItem.StoredInFacility)
                                       .WithQuantity(orderItem.QuantityOrdered)
                                       .WithUnitPurchasePrice(orderItem.UnitPrice)
                                       .WithContentsDescription($"{orderItem.QuantityOrdered} * {orderItem.Part.Name}")
                                       .Build();

                        shipment.AddShipmentItem(shipmentItem);

                        new OrderShipmentBuilder(session)
                        .WithOrderItem(orderItem)
                        .WithShipmentItem(shipmentItem)
                        .WithQuantity(orderItem.QuantityOrdered)
                        .Build();

                        if (orderItem.Part.InventoryItemKind.IsSerialised)
                        {
                            var serialisedItem = orderItem.SerialisedItem;
                            if (!orderItem.ExistSerialisedItem)
                            {
                                serialisedItem = new SerialisedItemBuilder(session)
                                                 .WithSerialNumber(orderItem.SerialNumber)
                                                 .Build();

                                orderItem.Part.AddSerialisedItem(serialisedItem);
                            }

                            shipmentItem.SerialisedItem = serialisedItem;

                            serialisedItem.AcquiredDate = orderItem.PurchaseOrderWherePurchaseOrderItem.OrderDate;

                            if (serialisedItem.ExistAcquiredDate && serialisedItem.ExistAcquisitionYear)
                            {
                                serialisedItem.RemoveAcquisitionYear();
                            }

                            if (this.OrderedBy.SerialisedItemSoldOns.Contains(new SerialisedItemSoldOns(this.Session()).PurchaseshipmentReceive))
                            {
                                serialisedItem.Buyer     = this.OrderedBy;
                                serialisedItem.OwnedBy   = this.OrderedBy;
                                serialisedItem.Ownership = new Ownerships(this.Session()).Own;
                            }
                        }
                    }
                }

                if (shipment.ShipToParty is InternalOrganisation internalOrganisation)
                {
                    if (internalOrganisation.IsAutomaticallyReceived)
                    {
                        shipment.Receive();
                    }
                }
            }
        }
Example #7
0
        public void BaseShip(SalesOrderShip method)
        {
            if (this.CanShip)
            {
                var addresses = this.ShipToAddresses();
                var shipments = new List <Shipment>();
                if (addresses.Count > 0)
                {
                    foreach (var address in addresses)
                    {
                        var pendingShipment = address.Value.BaseGetPendingCustomerShipmentForStore(address.Key, this.Store, this.ShipmentMethod);

                        if (pendingShipment == null)
                        {
                            pendingShipment = new CustomerShipmentBuilder(this.Strategy.Session)
                                              .WithShipFromParty(this.TakenBy)
                                              .WithShipFromAddress(this.ShipFromAddress)
                                              .WithShipToAddress(address.Key)
                                              .WithShipToParty(address.Value)
                                              .WithStore(this.Store)
                                              .WithShipmentMethod(this.ShipmentMethod)
                                              .WithPaymentMethod(this.PaymentMethod)
                                              .Build();

                            if (this.Store.AutoGenerateShipmentPackage)
                            {
                                pendingShipment.AddShipmentPackage(new ShipmentPackageBuilder(this.Strategy.Session).Build());
                            }
                        }

                        foreach (SalesOrderItem orderItem in this.ValidOrderItems)
                        {
                            var orderItemDerivedRoles = (SalesOrderItemDerivedRoles)orderItem;

                            if (orderItem.ExistProduct && orderItem.ShipToAddress.Equals(address.Key) && orderItem.QuantityRequestsShipping > 0)
                            {
                                var good              = orderItem.Product as Good;
                                var nonUnifiedGood    = orderItem.Product as NonUnifiedGood;
                                var unifiedGood       = orderItem.Product as UnifiedGood;
                                var inventoryItemKind = unifiedGood?.InventoryItemKind ?? nonUnifiedGood?.Part.InventoryItemKind;
                                var part              = unifiedGood ?? nonUnifiedGood?.Part;

                                ShipmentItem shipmentItem = null;
                                foreach (ShipmentItem item in pendingShipment.ShipmentItems)
                                {
                                    if (inventoryItemKind != null &&
                                        inventoryItemKind.Equals(new InventoryItemKinds(this.Session()).NonSerialised) &&
                                        item.Good.Equals(good) &&
                                        !item.ItemIssuancesWhereShipmentItem.Any(v => v.PickListItem.PickListWherePickListItem.PickListState.Equals(new PickListStates(this.Session()).Picked)))
                                    {
                                        shipmentItem = item;
                                        break;
                                    }
                                }

                                if (shipmentItem != null)
                                {
                                    shipmentItem.ContentsDescription = $"{shipmentItem.Quantity} * {good.Name}";
                                }
                                else
                                {
                                    shipmentItem = new ShipmentItemBuilder(this.Strategy.Session)
                                                   .WithGood(good)
                                                   .WithContentsDescription($"{orderItem.QuantityRequestsShipping} * {good}")
                                                   .Build();

                                    if (orderItem.ExistSerialisedItem)
                                    {
                                        shipmentItem.SerialisedItem = orderItem.SerialisedItem;
                                    }

                                    if (orderItem.ExistNextSerialisedItemAvailability)
                                    {
                                        shipmentItem.NextSerialisedItemAvailability = orderItem.NextSerialisedItemAvailability;
                                    }

                                    if (orderItem.ExistReservedFromNonSerialisedInventoryItem)
                                    {
                                        shipmentItem.AddReservedFromInventoryItem(orderItem.ReservedFromNonSerialisedInventoryItem);
                                    }

                                    if (orderItem.ExistReservedFromSerialisedInventoryItem)
                                    {
                                        shipmentItem.AddReservedFromInventoryItem(orderItem.ReservedFromSerialisedInventoryItem);
                                    }

                                    pendingShipment.AddShipmentItem(shipmentItem);
                                }

                                foreach (SalesOrderItem featureItem in orderItem.OrderedWithFeatures)
                                {
                                    shipmentItem.AddProductFeature(featureItem.ProductFeature);
                                }

                                new OrderShipmentBuilder(this.Strategy.Session)
                                .WithOrderItem(orderItem)
                                .WithShipmentItem(shipmentItem)
                                .WithQuantity(orderItem.QuantityRequestsShipping)
                                .Build();

                                shipmentItem.Quantity = shipmentItem.OrderShipmentsWhereShipmentItem.Sum(v => v.Quantity);

                                orderItemDerivedRoles.QuantityRequestsShipping = 0;

                                orderItemDerivedRoles.CostOfGoodsSold = orderItem.QuantityOrdered * part.PartWeightedAverage.AverageCost;
                            }
                        }

                        shipments.Add(pendingShipment);
                        this.AddDeniedPermission(new Permissions(this.Strategy.Session).Get(this.Meta.Class, this.Meta.Ship, Operations.Execute));
                    }
                }
            }
        }