Ejemplo n.º 1
0
        public PurchaseOrder(Guid id, int reference, PurchaseOrderStatus status, Producer producer, Order order)
        {
            if (producer == null)
            {
                throw SheaftException.Validation("Impossible de créer la commande, le producteur est requis.");
            }

            Id = id;

            Products     = new List <PurchaseOrderProduct>();
            DomainEvents = new List <DomainEvent> {
                new PurchaseOrderCreatedEvent(Id)
            };

            SetSender(order.User);
            SetVendor(producer);

            var delivery = order.Deliveries.Single(d => d.DeliveryMode.ProducerId == producer.Id);
            var address  = (int)delivery.DeliveryMode.Kind <= 4
                ? new ExpectedAddress(delivery.DeliveryMode.Address.Line1, delivery.DeliveryMode.Address.Line2,
                                      delivery.DeliveryMode.Address.Zipcode, delivery.DeliveryMode.Address.City,
                                      delivery.DeliveryMode.Address.Country, delivery.DeliveryMode.Address.Longitude,
                                      delivery.DeliveryMode.Address.Latitude)
                : new ExpectedAddress(order.User.Address.Line1, order.User.Address.Line2, order.User.Address.Zipcode,
                                      order.User.Address.City, order.User.Address.Country, order.User.Address.Longitude,
                                      order.User.Address.Latitude);

            ExpectedDelivery = new ExpectedPurchaseOrderDelivery(delivery, address);

            SetComment(delivery.Comment);
            SetReference(reference);
            SetStatus(status, true);

            var orderProducts = order.Products.Where(p => p.ProducerId == producer.Id);

            foreach (var orderProduct in orderProducts)
            {
                AddProduct(orderProduct);
            }

            var shouldAddDeliveryFees = delivery.DeliveryMode.ApplyDeliveryFeesWhen is DeliveryFeesApplication.Always ||
                                        (delivery.DeliveryMode.ApplyDeliveryFeesWhen is DeliveryFeesApplication
                                         .TotalLowerThanPurchaseOrderAmount &&
                                         delivery.DeliveryMode.DeliveryFeesMinPurchaseOrdersAmount.HasValue &&
                                         delivery.DeliveryMode.DeliveryFeesMinPurchaseOrdersAmount.Value >
                                         TotalWholeSalePrice);

            if (shouldAddDeliveryFees)
            {
                ExpectedDelivery?.ApplyDeliveryModeFees(delivery.DeliveryMode);
                RefreshOrder();
            }
        }
Ejemplo n.º 2
0
        public PurchaseOrder(Guid id, int reference, PurchaseOrderStatus status, DateTimeOffset expectedDeliveryDate, TimeSpan from, TimeSpan to, DeliveryMode deliveryMode, Producer producer, User client, IEnumerable <KeyValuePair <Domain.Product, int> > products, Catalog catalog, bool skipNotification, string comment = null)
        {
            if (producer == null)
            {
                throw SheaftException.Validation("Impossible de créer la commande, le producteur est requis.");
            }

            Id           = id;
            Products     = new List <PurchaseOrderProduct>();
            DomainEvents = new List <DomainEvent> {
                new PurchaseOrderCreatedEvent(Id)
            };
            Status    = PurchaseOrderStatus.Waiting;
            CreatedOn = DateTimeOffset.UtcNow;

            SetSender(client);
            SetVendor(producer);

            var address = (int)deliveryMode.Kind <= 4
                ? new ExpectedAddress(deliveryMode.Address.Line1, deliveryMode.Address.Line2,
                                      deliveryMode.Address.Zipcode, deliveryMode.Address.City,
                                      deliveryMode.Address.Country, deliveryMode.Address.Longitude,
                                      deliveryMode.Address.Latitude)
                : new ExpectedAddress(client.Address.Line1, client.Address.Line2, client.Address.Zipcode,
                                      client.Address.City, client.Address.Country, client.Address.Longitude,
                                      client.Address.Latitude);

            ExpectedDelivery = new ExpectedPurchaseOrderDelivery(deliveryMode, expectedDeliveryDate, from, to, address);

            SetComment(comment);
            SetReference(reference);

            foreach (var product in products)
            {
                AddProduct(new PurchaseOrderProduct(product.Key, catalog.Id, product.Value));
            }

            var shouldAddDeliveryFees = deliveryMode.ApplyDeliveryFeesWhen is DeliveryFeesApplication.Always ||
                                        (deliveryMode.ApplyDeliveryFeesWhen is DeliveryFeesApplication
                                         .TotalLowerThanPurchaseOrderAmount &&
                                         deliveryMode.DeliveryFeesMinPurchaseOrdersAmount.HasValue &&
                                         deliveryMode.DeliveryFeesMinPurchaseOrdersAmount.Value >
                                         TotalWholeSalePrice);

            if (shouldAddDeliveryFees)
            {
                ExpectedDelivery?.ApplyDeliveryModeFees(deliveryMode);
                RefreshOrder();
            }

            SetStatus(status, skipNotification);
        }