/// <summary>
        /// Builds the invoice
        /// </summary>
        /// <returns>Attempt{IInvoice}</returns>
        public override Attempt <IInvoice> Build()
        {
            var unpaid =
                _salePreparation.MerchelloContext.Services.InvoiceService.GetInvoiceStatusByKey(
                    Constants.DefaultKeys.InvoiceStatus.Unpaid);

            if (unpaid == null)
            {
                return(Attempt <IInvoice> .Fail(new NullReferenceException("Unpaid invoice status query returned null")));
            }

            var attempt = (TaskHandlers.Any())
                       ? TaskHandlers.First().Execute(new Invoice(unpaid)
            {
                VersionKey = _salePreparation.ItemCache.VersionKey
            })
                       : Attempt <IInvoice> .Fail(new InvalidOperationException("The configuration Chain Task List could not be instantiated"));

            if (!attempt.Success)
            {
                return(attempt);
            }

            // total the invoice
            attempt.Result.Total = attempt.Result.Items.Sum(x => x.TotalPrice);

            return(attempt);
        }
        /// <summary>
        /// Builds the order
        /// </summary>
        /// <returns>Attempt{IOrder}</returns>
        public override Attempt <IOrder> Build()
        {
            var attempt = (TaskHandlers.Any())
                ? TaskHandlers.First().Execute(new Order(_orderStatus, _invoice.Key)
            {
                OrderNumberPrefix = _invoice.InvoiceNumberPrefix,
                VersionKey        = _invoice.VersionKey
            }) :
                          Attempt <IOrder> .Fail(new InvalidOperationException("The configuration Chain Task List could not be instantiated."));

            return(attempt);
        }
        /// <summary>
        /// The validate.
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public Attempt <ValidationResult <CustomerItemCacheBase> > Validate(CustomerItemCacheBase value)
        {
            var validated = new ValidationResult <CustomerItemCacheBase>()
            {
                Validated = value
            };

            if (!value.Items.Any())
            {
                return(Attempt <ValidationResult <CustomerItemCacheBase> > .Succeed(validated));
            }

            return(TaskHandlers.Any()
            ? TaskHandlers.First().Execute(validated)
            : Attempt <ValidationResult <CustomerItemCacheBase> > .Succeed(validated));
        }
        /// <summary>
        /// Builds the order
        /// </summary>
        /// <returns>The Attempt{IShipment}</returns>
        public override Attempt <IShipment> Build()
        {
            // invoice
            var invoice = _merchelloContext.Services.InvoiceService.GetByKey(_order.InvoiceKey);

            if (invoice == null)
            {
                return(Attempt <IShipment> .Fail(new NullReferenceException("An invoice could not be found for the order passed")));
            }

            // shipment line item
            var shipmentLineItem = invoice.Items.FirstOrDefault(x => x.LineItemType == LineItemType.Shipping);

            if (shipmentLineItem == null)
            {
                return(Attempt <IShipment> .Fail(new NullReferenceException("An shipment could not be found in the invoice assoiciated with the order passed")));
            }

            // the shipment
            var quoted = shipmentLineItem.ExtendedData.GetShipment <InvoiceLineItem>();

            if (quoted == null)
            {
                return(Attempt <IShipment> .Fail(new NullReferenceException("An shipment could not be found in the invoice assoiciated with the order passed")));
            }

            var status = _merchelloContext.Services.ShipmentService.GetShipmentStatusByKey(_shipmentStatusKey) ??
                         _merchelloContext.Services.ShipmentService.GetShipmentStatusByKey(Core.Constants.ShipmentStatus.Quoted);

            // execute the change
            var attempt = TaskHandlers.Any()
                ? TaskHandlers.First().Execute(
                new Shipment(status, quoted.GetOriginAddress(), quoted.GetDestinationAddress())
            {
                ShipMethodKey = _shipMethodKey,
                VersionKey    = quoted.VersionKey,
                Carrier       = _carrier,
                TrackingCode  = _trackingNumber,
                TrackingUrl   = _trackingUrl
            })
                : Attempt <IShipment> .Fail(new InvalidOperationException("The configuration Chain Task List could not be instantiated."));

            return(attempt);
        }
        /// <summary>
        /// Builds the invoice
        /// </summary>
        /// <returns>The Attempt{IInvoice} representing the successful creation of an invoice</returns>
        public override Attempt <IInvoice> Build()
        {
            var unpaid =
                _checkoutManager.Context.Services.InvoiceService.GetInvoiceStatusByKey(Core.Constants.InvoiceStatus.Unpaid);

            if (unpaid == null)
            {
                return(Attempt <IInvoice> .Fail(new NullReferenceException("Unpaid invoice status query returned null")));
            }

            //// Invoice needs to be created via the service so that the Creating / Created events get fired.
            //// see http://issues.merchello.com/youtrack/issue/M-1290
            var invoice = _checkoutManager.Context.Services.InvoiceService.CreateInvoice(unpaid.Key);

            invoice.VersionKey = _checkoutManager.Context.VersionKey;

            //// var invoice = new Invoice(unpaid) { VersionKey = _checkoutManager.Context.VersionKey };

            // Associate a customer with the invoice if it is a known customer.
            if (!_checkoutManager.Context.Customer.IsAnonymous)
            {
                invoice.CustomerKey = _checkoutManager.Context.Customer.Key;
            }

            var attempt = TaskHandlers.Any()
                       ? TaskHandlers.First().Execute(invoice)
                       : Attempt <IInvoice> .Fail(new InvalidOperationException("The configuration Chain Task List could not be instantiated"));

            if (!attempt.Success)
            {
                return(attempt);
            }


            var charges   = attempt.Result.Items.Where(x => x.LineItemType != LineItemType.Discount).Sum(x => x.TotalPrice);
            var discounts = attempt.Result.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice);

            // total the invoice
            decimal converted;

            attempt.Result.Total = Math.Round(decimal.TryParse((charges - discounts).ToString(CultureInfo.InvariantCulture), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out converted) ? converted : 0, 2);

            return(attempt);
        }
Beispiel #6
0
        /// <summary>
        /// Builds the invoice
        /// </summary>
        /// <returns>The Attempt{IInvoice} representing the successful creation of an invoice</returns>
        public override Attempt <IInvoice> Build()
        {
            var unpaid =
                _salePreparation.MerchelloContext.Services.InvoiceService.GetInvoiceStatusByKey(Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);

            if (unpaid == null)
            {
                return(Attempt <IInvoice> .Fail(new NullReferenceException("Unpaid invoice status query returned null")));
            }

            var invoice = new Invoice(unpaid)
            {
                VersionKey = _salePreparation.ItemCache.VersionKey
            };

            // Associate a customer with the invoice if it is a known customer.
            if (!_salePreparation.Customer.IsAnonymous)
            {
                invoice.CustomerKey = _salePreparation.Customer.Key;
            }

            var attempt = TaskHandlers.Any()
                       ? TaskHandlers.First().Execute(invoice)
                       : Attempt <IInvoice> .Fail(new InvalidOperationException("The configuration Chain Task List could not be instantiated"));

            if (!attempt.Success)
            {
                return(attempt);
            }


            var charges   = attempt.Result.Items.Where(x => x.LineItemType != LineItemType.Discount).Sum(x => x.TotalPrice);
            var discounts = attempt.Result.Items.Where(x => x.LineItemType == LineItemType.Discount).Sum(x => x.TotalPrice);

            // total the invoice
            decimal converted;

            attempt.Result.Total = Math.Round(decimal.TryParse((charges - discounts).ToString(CultureInfo.InvariantCulture), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out converted) ? converted : 0, 2);

            return(attempt);
        }