示例#1
0
        public void AppsOnDeriveQuantities(IDerivation derivation)
        {
            NonSerialisedInventoryItem inventoryItem = null;

            if (this.ExistPart)
            {
                var inventoryItems = this.Part.InventoryItemsWherePart;
                inventoryItems.Filter.AddEquals(M.InventoryItem.Facility, this.PurchaseOrderWherePurchaseOrderItem.Facility);
                inventoryItem = inventoryItems.First as NonSerialisedInventoryItem;
            }

            if (this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).InProcess))
            {
                if (!this.ExistPreviousQuantity || !this.QuantityOrdered.Equals(this.PreviousQuantity))
                {
                    if (inventoryItem != null)
                    {
                        inventoryItem.OnDerive(x => x.WithDerivation(derivation));
                    }
                }
            }

            if (this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).Cancelled) ||
                this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).Rejected))
            {
                if (inventoryItem != null)
                {
                    inventoryItem.OnDerive(x => x.WithDerivation(derivation));
                }
            }
        }
        public void BaseOnDerive(ObjectOnDerive method)
        {
            var derivation = method.Derivation;

            // States
            var states = new PurchaseOrderItemStates(this.Session());

            var purchaseOrderState = this.PurchaseOrderWherePurchaseOrderItem.PurchaseOrderState;

            if (purchaseOrderState.IsCreated &&
                !this.PurchaseOrderItemState.IsCancelled &&
                !this.PurchaseOrderItemState.IsRejected)
            {
                this.PurchaseOrderItemState = states.Created;
            }

            if (purchaseOrderState.IsInProcess &&
                (this.PurchaseOrderItemState.IsCreated || this.PurchaseOrderItemState.IsOnHold))
            {
                this.PurchaseOrderItemState = states.InProcess;
            }

            if (purchaseOrderState.IsOnHold && this.PurchaseOrderItemState.IsInProcess)
            {
                this.PurchaseOrderItemState = states.OnHold;
            }

            if (purchaseOrderState.IsSent && this.PurchaseOrderItemState.IsInProcess)
            {
                this.PurchaseOrderItemState = states.Sent;
            }

            if (this.IsValid && purchaseOrderState.IsFinished)
            {
                this.PurchaseOrderItemState = states.Finished;
            }

            if (this.IsValid && purchaseOrderState.IsCancelled)
            {
                this.PurchaseOrderItemState = states.Cancelled;
            }

            if (this.IsValid && purchaseOrderState.IsRejected)
            {
                this.PurchaseOrderItemState = states.Rejected;
            }

            if (this.IsValid)
            {
                if (this.AssignedDeliveryDate.HasValue)
                {
                    this.DeliveryDate = this.AssignedDeliveryDate.Value;
                }
                else if (this.PurchaseOrderWherePurchaseOrderItem.DeliveryDate.HasValue)
                {
                    this.DeliveryDate = this.PurchaseOrderWherePurchaseOrderItem.DeliveryDate.Value;
                }

                this.UnitBasePrice = 0;
                this.UnitDiscount  = 0;
                this.UnitSurcharge = 0;

                if (this.AssignedUnitPrice.HasValue)
                {
                    this.UnitBasePrice = this.AssignedUnitPrice.Value;
                    this.UnitPrice     = this.AssignedUnitPrice.Value;
                }
                else
                {
                    var order = this.PurchaseOrderWherePurchaseOrderItem;
                    this.UnitBasePrice = new SupplierOfferings(this.Strategy.Session).PurchasePrice(order.TakenViaSupplier, order.OrderDate, this.Part);
                }

                this.VatRegime = this.AssignedVatRegime ?? this.PurchaseOrderWherePurchaseOrderItem.VatRegime;
                this.VatRate   = this.VatRegime?.VatRate;

                this.UnitVat        = this.ExistVatRate ? this.UnitPrice * this.VatRate.Rate / 100 : 0;
                this.TotalBasePrice = this.UnitBasePrice * this.QuantityOrdered;
                this.TotalDiscount  = this.UnitDiscount * this.QuantityOrdered;
                this.TotalSurcharge = this.UnitSurcharge * this.QuantityOrdered;
                this.UnitPrice      = this.UnitBasePrice - this.UnitDiscount + this.UnitSurcharge;
                this.TotalVat       = this.UnitVat * this.QuantityOrdered;
                this.TotalExVat     = this.UnitPrice * this.QuantityOrdered;
                this.TotalIncVat    = this.TotalExVat + this.TotalVat;
            }

            if (this.ExistPart && this.Part.InventoryItemKind.Serialised)
            {
                derivation.Validation.AssertAtLeastOne(this, M.PurchaseOrderItem.SerialisedItem, M.PurchaseOrderItem.SerialNumber);
                derivation.Validation.AssertExistsAtMostOne(this, M.PurchaseOrderItem.SerialisedItem, M.PurchaseOrderItem.SerialNumber);
            }

            var purchaseOrderItemShipmentStates = new PurchaseOrderItemShipmentStates(derivation.Session);
            var purchaseOrderItemPaymentStates  = new PurchaseOrderItemPaymentStates(derivation.Session);
            var purchaseOrderItemStates         = new PurchaseOrderItemStates(derivation.Session);

            if (this.IsValid)
            {
                // ShipmentState
                if (this.ExistPart)
                {
                    var quantityReceived = 0M;
                    foreach (ShipmentReceipt shipmentReceipt in this.ShipmentReceiptsWhereOrderItem)
                    {
                        quantityReceived += shipmentReceipt.QuantityAccepted;
                    }

                    this.QuantityReceived = quantityReceived;
                }

                if (this.QuantityReceived == 0)
                {
                    this.PurchaseOrderItemShipmentState = new PurchaseOrderItemShipmentStates(this.Strategy.Session).NotReceived;
                }
                else
                {
                    this.PurchaseOrderItemShipmentState = this.QuantityReceived < this.QuantityOrdered ?
                                                          purchaseOrderItemShipmentStates.PartiallyReceived :
                                                          purchaseOrderItemShipmentStates.Received;
                }

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

                if (orderBilling.Any())
                {
                    if (orderBilling.All(v => v.PurchaseInvoiceWherePurchaseInvoiceItem.PurchaseInvoiceState.IsPaid))
                    {
                        this.PurchaseOrderItemPaymentState = purchaseOrderItemPaymentStates.Paid;
                    }
                    else if (orderBilling.All(v => !v.PurchaseInvoiceWherePurchaseInvoiceItem.PurchaseInvoiceState.IsPaid))
                    {
                        this.PurchaseOrderItemPaymentState = purchaseOrderItemPaymentStates.NotPaid;
                    }
                    else
                    {
                        this.PurchaseOrderItemPaymentState = purchaseOrderItemPaymentStates.PartiallyPaid;
                    }
                }

                // PurchaseOrderItem States
                if (this.PurchaseOrderItemShipmentState.IsReceived)
                {
                    this.PurchaseOrderItemState = purchaseOrderItemStates.Completed;
                }

                if (this.PurchaseOrderItemState.IsCompleted && this.PurchaseOrderItemPaymentState.IsPaid)
                {
                    this.PurchaseOrderItemState = purchaseOrderItemStates.Finished;
                }
            }

            if (this.PurchaseOrderItemState.Equals(states.InProcess) ||
                this.PurchaseOrderItemState.Equals(states.Cancelled) ||
                this.PurchaseOrderItemState.Equals(states.Rejected))
            {
                NonSerialisedInventoryItem inventoryItem = null;

                if (this.ExistPart)
                {
                    var inventoryItems = this.Part.InventoryItemsWherePart;
                    inventoryItems.Filter.AddEquals(M.InventoryItem.Facility, this.PurchaseOrderWherePurchaseOrderItem.Facility);
                    inventoryItem = inventoryItems.First as NonSerialisedInventoryItem;
                }

                if (this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).InProcess))
                {
                    if (!this.ExistPreviousQuantity || !this.QuantityOrdered.Equals(this.PreviousQuantity))
                    {
                        // TODO: Remove OnDerive
                        inventoryItem?.OnDerive(x => x.WithDerivation(derivation));
                    }
                }

                if (this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).Cancelled) ||
                    this.PurchaseOrderItemState.Equals(new PurchaseOrderItemStates(this.Strategy.Session).Rejected))
                {
                    // TODO: Remove OnDerive
                    inventoryItem?.OnDerive(x => x.WithDerivation(derivation));
                }
            }

            if ((this.IsValid && !this.ExistOrderItemBillingsWhereOrderItem &&
                 this.PurchaseOrderItemShipmentState.IsReceived) || this.PurchaseOrderItemShipmentState.IsPartiallyReceived || (!this.ExistPart && this.QuantityReceived == 1))
            {
                this.CanInvoice = true;
            }
            else
            {
                this.CanInvoice = false;
            }
        }