protected override void BaseSecure(Security config)
        {
            var created   = new ShipmentStates(this.Session).Created;
            var picking   = new ShipmentStates(this.Session).Picking;
            var picked    = new ShipmentStates(this.Session).Picked;
            var packed    = new ShipmentStates(this.Session).Packed;
            var shipped   = new ShipmentStates(this.Session).Shipped;
            var cancelled = new ShipmentStates(this.Session).Cancelled;
            var onHold    = new ShipmentStates(this.Session).OnHold;

            var pick      = this.Meta.Pick;
            var setPacked = this.Meta.SetPacked;
            var hold      = this.Meta.Hold;
            var @continue = this.Meta.Continue;
            var ship      = this.Meta.Ship;
            var delete    = this.Meta.Delete;

            var except = new HashSet <IOperandType>
            {
                this.Meta.ElectronicDocuments.RoleType,
            };

            config.Deny(this.ObjectType, onHold, pick, setPacked, ship, hold, delete);
            config.Deny(this.ObjectType, created, setPacked, ship, @continue);
            config.Deny(this.ObjectType, picked, ship, pick, @continue, delete);
            config.Deny(this.ObjectType, packed, pick, @continue, delete);
            config.Deny(this.ObjectType, picking, pick, setPacked, ship, @continue, delete);

            config.Deny(this.ObjectType, cancelled, Operations.Execute, Operations.Write);
            config.DenyExcept(this.ObjectType, picking, except, Operations.Write);
            config.DenyExcept(this.ObjectType, picked, except, Operations.Write);
            config.DenyExcept(this.ObjectType, packed, except, Operations.Write);
            config.DenyExcept(this.ObjectType, onHold, except, Operations.Write);
            config.DenyExcept(this.ObjectType, shipped, except, Operations.Execute, Operations.Write);
        }
Example #2
0
        protected override void BaseSecure(Security config)
        {
            var received  = new ShipmentStates(this.Session).Received;
            var cancelled = new ShipmentStates(this.Session).Cancelled;

            var except = new HashSet <IOperandType>
            {
                this.Meta.ElectronicDocuments.RoleType,
            };

            config.Deny(this.ObjectType, cancelled, Operations.Execute, Operations.Write);
            config.DenyExcept(this.ObjectType, received, except, Operations.Execute, Operations.Write);
        }
Example #3
0
        public void BaseOnDerive(ObjectOnDerive method)
        {
            var derivation = method.Derivation;
            var salesOrder = this.SalesOrderWhereSalesOrderItem;
            var shipped    = new ShipmentStates(this.Session()).Shipped;

            if (!this.ExistDerivationTrigger)
            {
                this.DerivationTrigger = Guid.NewGuid();
            }

            if (this.ExistSerialisedItem && !this.ExistNextSerialisedItemAvailability)
            {
                derivation.Validation.AssertExists(this, this.Meta.NextSerialisedItemAvailability);
            }

            if (this.Part != null && this.Part.InventoryItemKind.IsSerialised && this.QuantityOrdered != 1)
            {
                derivation.Validation.AddError(this, M.SalesOrderItem.QuantityOrdered, ErrorMessages.InvalidQuantity);
            }

            if (this.Part != null && this.Part.InventoryItemKind.IsNonSerialised && this.QuantityOrdered == 0)
            {
                derivation.Validation.AddError(this, M.SalesOrderItem.QuantityOrdered, ErrorMessages.InvalidQuantity);
            }

            if (this.ExistInvoiceItemType && this.InvoiceItemType.MaxQuantity.HasValue && this.QuantityOrdered > this.InvoiceItemType.MaxQuantity.Value)
            {
                derivation.Validation.AddError(this, M.SalesOrderItem.QuantityOrdered, ErrorMessages.InvalidQuantity);
            }

            var salesOrderItemShipmentStates = new SalesOrderItemShipmentStates(derivation.Session);
            var salesOrderItemPaymentStates  = new SalesOrderItemPaymentStates(derivation.Session);
            var salesOrderItemInvoiceStates  = new SalesOrderItemInvoiceStates(derivation.Session);
            var salesOrderItemStates         = new SalesOrderItemStates(derivation.Session);

            // Shipments
            this.QuantityPendingShipment = this.OrderShipmentsWhereOrderItem
                                           .Where(v => v.ExistShipmentItem && !((CustomerShipment)v.ShipmentItem.ShipmentWhereShipmentItem).ShipmentState.Equals(shipped))
                                           .Sum(v => v.Quantity);

            this.QuantityShipped = this.OrderShipmentsWhereOrderItem
                                   .Where(v => v.ExistShipmentItem && ((CustomerShipment)v.ShipmentItem.ShipmentWhereShipmentItem).ShipmentState.Equals(shipped))
                                   .Sum(v => v.Quantity);

            if (this.SalesOrderItemState.IsInProcess &&
                this.ExistPreviousReservedFromNonSerialisedInventoryItem &&
                this.ReservedFromNonSerialisedInventoryItem != this.PreviousReservedFromNonSerialisedInventoryItem)
            {
                derivation.Validation.AddError(this, this.Meta.ReservedFromNonSerialisedInventoryItem, ErrorMessages.ReservedFromNonSerialisedInventoryItem);
            }

            if (this.ExistSerialisedItem && this.QuantityOrdered != 1)
            {
                derivation.Validation.AddError(this, this.Meta.QuantityOrdered, ErrorMessages.SerializedItemQuantity);
            }

            if (this.QuantityOrdered < this.QuantityPendingShipment || this.QuantityOrdered < this.QuantityShipped)
            {
                derivation.Validation.AddError(this, M.SalesOrderItem.QuantityOrdered, ErrorMessages.SalesOrderItemQuantityToShipNowIsLargerThanQuantityRemaining);
            }

            if (this.SalesOrderItemInventoryAssignments.FirstOrDefault() != null)
            {
                this.QuantityCommittedOut = this.SalesOrderItemInventoryAssignments
                                            .SelectMany(v => v.InventoryItemTransactions)
                                            .Where(t => t.Reason.Equals(new InventoryTransactionReasons(this.Session()).Reservation))
                                            .Sum(v => v.Quantity);
            }
            else
            {
                this.QuantityCommittedOut = 0;
            }

            // SalesOrderItem States
            if (this.IsValid)
            {
                if (salesOrder.SalesOrderState.IsProvisional)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Provisional;
                }

                if (salesOrder.SalesOrderState.IsReadyForPosting &&
                    (this.SalesOrderItemState.IsProvisional || this.SalesOrderItemState.IsRequestsApproval || this.SalesOrderItemState.IsOnHold))
                {
                    this.SalesOrderItemState = salesOrderItemStates.ReadyForPosting;
                }

                if (salesOrder.SalesOrderState.IsRequestsApproval &&
                    (this.SalesOrderItemState.IsProvisional || this.SalesOrderItemState.IsOnHold))
                {
                    this.SalesOrderItemState = salesOrderItemStates.RequestsApproval;
                }

                if (salesOrder.SalesOrderState.IsAwaitingAcceptance &&
                    this.SalesOrderItemState.IsReadyForPosting)
                {
                    this.SalesOrderItemState = salesOrderItemStates.AwaitingAcceptance;
                }

                if (salesOrder.SalesOrderState.IsInProcess &&
                    this.SalesOrderItemState.IsAwaitingAcceptance || this.SalesOrderItemState.IsOnHold)
                {
                    this.SalesOrderItemState = salesOrderItemStates.InProcess;
                }

                if (salesOrder.SalesOrderState.IsOnHold && this.SalesOrderItemState.IsInProcess)
                {
                    this.SalesOrderItemState = salesOrderItemStates.OnHold;
                }

                if (salesOrder.SalesOrderState.IsFinished)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Finished;
                }

                if (salesOrder.SalesOrderState.IsCancelled)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Cancelled;
                }

                if (salesOrder.SalesOrderState.IsRejected)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Rejected;
                }
            }

            if (this.IsValid)
            {
                // ShipmentState
                if (!this.ExistOrderShipmentsWhereOrderItem)
                {
                    this.SalesOrderItemShipmentState = salesOrderItemShipmentStates.NotShipped;
                }
                else if (this.QuantityShipped == 0 && this.QuantityPendingShipment > 0)
                {
                    this.SalesOrderItemShipmentState = salesOrderItemShipmentStates.InProgress;
                }
                else
                {
                    this.SalesOrderItemShipmentState = this.QuantityShipped < this.QuantityOrdered ?
                                                       salesOrderItemShipmentStates.PartiallyShipped :
                                                       salesOrderItemShipmentStates.Shipped;
                }

                // PaymentState
                var orderBilling = this.OrderItemBillingsWhereOrderItem.Select(v => v.InvoiceItem).OfType <SalesInvoiceItem>().ToArray();

                if (orderBilling.Any())
                {
                    if (orderBilling.All(v => v.SalesInvoiceWhereSalesInvoiceItem.SalesInvoiceState.IsPaid))
                    {
                        this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.Paid;
                    }
                    else if (orderBilling.All(v => !v.SalesInvoiceWhereSalesInvoiceItem.SalesInvoiceState.IsPaid))
                    {
                        this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.NotPaid;
                    }
                    else
                    {
                        this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.PartiallyPaid;
                    }
                }
                else
                {
                    var shipmentBilling = this.OrderShipmentsWhereOrderItem.SelectMany(v => v.ShipmentItem.ShipmentItemBillingsWhereShipmentItem).Select(v => v.InvoiceItem).OfType <SalesInvoiceItem>().ToArray();
                    if (shipmentBilling.Any())
                    {
                        if (shipmentBilling.All(v => v.SalesInvoiceWhereSalesInvoiceItem.SalesInvoiceState.IsPaid))
                        {
                            this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.Paid;
                        }
                        else if (shipmentBilling.All(v => !v.SalesInvoiceWhereSalesInvoiceItem.SalesInvoiceState.IsPaid))
                        {
                            this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.NotPaid;
                        }
                        else
                        {
                            this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.PartiallyPaid;
                        }
                    }
                    else
                    {
                        this.SalesOrderItemPaymentState = salesOrderItemPaymentStates.NotPaid;
                    }
                }

                // InvoiceState
                var amountAlreadyInvoiced = this.OrderItemBillingsWhereOrderItem?.Sum(v => v.Amount);
                if (amountAlreadyInvoiced == 0)
                {
                    amountAlreadyInvoiced = this.OrderShipmentsWhereOrderItem
                                            .SelectMany(orderShipment => orderShipment.ShipmentItem.ShipmentItemBillingsWhereShipmentItem)
                                            .Aggregate(amountAlreadyInvoiced, (current, shipmentItemBilling) => current + shipmentItemBilling.Amount);
                }

                var leftToInvoice = this.TotalExVat - amountAlreadyInvoiced;

                if (amountAlreadyInvoiced == 0)
                {
                    this.SalesOrderItemInvoiceState = salesOrderItemInvoiceStates.NotInvoiced;
                }
                else if (amountAlreadyInvoiced > 0 && leftToInvoice > 0)
                {
                    this.SalesOrderItemInvoiceState = salesOrderItemInvoiceStates.PartiallyInvoiced;
                }
                else
                {
                    this.SalesOrderItemInvoiceState = salesOrderItemInvoiceStates.Invoiced;
                }

                // SalesOrderItem States
                if (this.SalesOrderItemShipmentState.Shipped && this.SalesOrderItemInvoiceState.Invoiced)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Completed;
                }

                if (this.SalesOrderItemState.IsCompleted && this.SalesOrderItemPaymentState.Paid)
                {
                    this.SalesOrderItemState = salesOrderItemStates.Finished;
                }
            }

            this.CalculatePrice(derivation, salesOrder);

            if (!this.IsValid && this.WasValid)
            {
                if (this.ExistReservedFromNonSerialisedInventoryItem && this.ExistQuantityCommittedOut)
                {
                    var inventoryAssignment = this.SalesOrderItemInventoryAssignments.FirstOrDefault();
                    if (inventoryAssignment != null)
                    {
                        inventoryAssignment.Quantity = 0 - this.QuantityCommittedOut;
                    }
                }
            }

            // TODO: Move to Custom
            if (derivation.ChangeSet.IsCreated(this) && !this.ExistDescription)
            {
                if (this.ExistSerialisedItem)
                {
                    var builder = new StringBuilder();
                    var part    = this.SerialisedItem.PartWhereSerialisedItem;

                    if (part != null && part.ExistManufacturedBy)
                    {
                        builder.Append($", Manufacturer: {part.ManufacturedBy.PartyName}");
                    }

                    if (part != null && part.ExistBrand)
                    {
                        builder.Append($", Brand: {part.Brand.Name}");
                    }

                    if (part != null && part.ExistModel)
                    {
                        builder.Append($", Model: {part.Model.Name}");
                    }

                    builder.Append($", SN: {this.SerialisedItem.SerialNumber}");

                    if (this.SerialisedItem.ExistManufacturingYear)
                    {
                        builder.Append($", YOM: {this.SerialisedItem.ManufacturingYear}");
                    }

                    foreach (SerialisedItemCharacteristic characteristic in this.SerialisedItem.SerialisedItemCharacteristics)
                    {
                        if (characteristic.ExistValue)
                        {
                            var characteristicType = characteristic.SerialisedItemCharacteristicType;
                            if (characteristicType.ExistUnitOfMeasure)
                            {
                                var uom = characteristicType.UnitOfMeasure.ExistAbbreviation
                                                ? characteristicType.UnitOfMeasure.Abbreviation
                                                : characteristicType.UnitOfMeasure.Name;
                                builder.Append(
                                    $", {characteristicType.Name}: {characteristic.Value} {uom}");
                            }
                            else
                            {
                                builder.Append($", {characteristicType.Name}: {characteristic.Value}");
                            }
                        }
                    }

                    var details = builder.ToString();

                    if (details.StartsWith(","))
                    {
                        details = details.Substring(2);
                    }

                    this.Description = details;
                }
                else if (this.ExistProduct && this.Product is UnifiedGood unifiedGood)
                {
                    var builder = new StringBuilder();

                    if (unifiedGood != null && unifiedGood.ExistManufacturedBy)
                    {
                        builder.Append($", Manufacturer: {unifiedGood.ManufacturedBy.PartyName}");
                    }

                    if (unifiedGood != null && unifiedGood.ExistBrand)
                    {
                        builder.Append($", Brand: {unifiedGood.Brand.Name}");
                    }

                    if (unifiedGood != null && unifiedGood.ExistModel)
                    {
                        builder.Append($", Model: {unifiedGood.Model.Name}");
                    }

                    foreach (SerialisedItemCharacteristic characteristic in unifiedGood.SerialisedItemCharacteristics)
                    {
                        if (characteristic.ExistValue)
                        {
                            var characteristicType = characteristic.SerialisedItemCharacteristicType;
                            if (characteristicType.ExistUnitOfMeasure)
                            {
                                var uom = characteristicType.UnitOfMeasure.ExistAbbreviation
                                                ? characteristicType.UnitOfMeasure.Abbreviation
                                                : characteristicType.UnitOfMeasure.Name;
                                builder.Append($", {characteristicType.Name}: {characteristic.Value} {uom}");
                            }
                            else
                            {
                                builder.Append($", {characteristicType.Name}: {characteristic.Value}");
                            }
                        }
                    }

                    var details = builder.ToString();

                    if (details.StartsWith(","))
                    {
                        details = details.Substring(2);
                    }

                    this.Description = details;
                }
            }

            this.Sync();
        }