Example #1
0
        public ActionResult EditShipping(long id, string method, string cost)
        {
            var transaction = new Transaction(repository.GetOrder(subdomainid.Value, id), repository, sessionid.Value);

            transaction.UpdateShippingCost(cost);
            transaction.UpdateShippingMethod(method);
            transaction.UpdateTotal();
            transaction.AddComment("Shipping cost and method updated.", sessionid.Value);
            transaction.SaveUpdatedTransaction();

            repository.AddActivity(transaction.GetOwner().id,
                                   new ActivityMessage(transaction.GetID(), transaction.GetReceiver().id,
                                                       ActivityMessageType.ORDER_SHIPPED,
                                                       new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(transaction.GetType())), subdomainid.Value);


            var data = new ChangeHistory
            {
                documentType = (transaction.GetType() == TransactionType.ORDER? ChangeHistoryType.ORDERS : ChangeHistoryType.INVOICE).ToDocumentType(),
                documentName = transaction.GetOrderNumber(),
                documentLoc  = accountHostname.ToDomainUrl(transaction.GetOrderLink())
            };
            var message = new Message(transaction.GetReceiver(), transaction.GetOwner(), subdomainid.Value);

            message.SendMessage(this, repository, EmailViewType.INVOICEORDER_CHANGED, data,
                                string.Format("{0} #{1} Updated", data.documentType, transaction.GetOrderNumber()), data.documentLoc);

            return(Json("Shipping cost updated.".ToJsonOKMessage()));
        }
Example #2
0
        public ActionResult Send(long id, string subject, string message, string viewloc)
        {
            var transaction = new Transaction(repository.GetOrder(subdomainid.Value, id), repository, sessionid.Value);

            // get necessary information
            var me     = repository.GetUserById(sessionid.Value, subdomainid.Value);
            var sender = string.Concat(me.organisation1.name, " (", me.email, ")");

            // send mail (do this last in case we can't send email)
            var data = new NewOrderEmailContent
            {
                message = message,
                viewloc = viewloc,
                sender  = sender,
            };

            var msg    = new Message(transaction.GetReceiver(), transaction.GetOwner(), subdomainid.Value);
            var result = msg.SendMessage(this, repository, EmailViewType.INVOICEORDER_NEW, data, subject, viewloc);

            if (!result.success)
            {
                return(Json(result.message.ToJsonFail()));
            }

            // update order status
            transaction.UpdateOrderStatus(OrderStatus.SENT);

            // log activity
            string comment;

            if (transaction.GetType() == TransactionType.ORDER)
            {
                // PURCHASE ORDER
                comment = "Purchase Order sent.";

                repository.AddActivity(transaction.GetOwner().id,
                                       new ActivityMessage(transaction.GetID(), transaction.GetOwner().id,
                                                           ActivityMessageType.ORDER_SENT,
                                                           new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(TransactionType.ORDER)), subdomainid.Value);
                repository.AddActivity(transaction.GetOwner().id,
                                       new ActivityMessage(transaction.GetID(), transaction.GetReceiver().id,
                                                           ActivityMessageType.ORDER_RECEIVED,
                                                           new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(TransactionType.ORDER),
                                                           new HtmlLink(transaction.GetOwner().ToEmailName(true), transaction.GetOwner().id).ToContactString()), subdomainid.Value);
            }
            else
            {
                // SALES INVOICE
                comment = "Sales Invoice sent.";
                repository.AddActivity(transaction.GetOwner().id,
                                       new ActivityMessage(transaction.GetID(), transaction.GetOwner().id,
                                                           ActivityMessageType.INVOICE_SENT,
                                                           new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(TransactionType.INVOICE)), subdomainid.Value);
                repository.AddActivity(transaction.GetOwner().id,
                                       new ActivityMessage(transaction.GetID(), transaction.GetReceiver().id,
                                                           ActivityMessageType.INVOICE_RECEIVED,
                                                           new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(TransactionType.INVOICE),
                                                           new HtmlLink(transaction.GetOwner().ToEmailName(true), transaction.GetOwner().id).ToContactString()), subdomainid.Value);
            }

            transaction.AddComment(comment, sessionid.Value);

            return(Json("".ToJsonOKMessage()));
        }
Example #3
0
        // json is true when checkout is done from an iframe, eg. facebook page
        public ActionResult create(CheckoutStatus status, string shippingmethod, string paymentmethod, bool isJson = false)
        {
            Debug.Assert(!cart.orderid.HasValue);

            var shop_owner = cart.MASTERsubdomain.organisation.users.First();
            var currency   = cart.MASTERsubdomain.currency.ToCurrency();
            var buyer      = cart.user;

            var transaction = new Transaction(cart.MASTERsubdomain, buyer, TransactionType.INVOICE, repository, sessionid.Value);

            transaction.CreateTransaction(
                repository.GetNewOrderNumber(subdomainid.Value, TransactionType.INVOICE),
                DateTime.UtcNow,
                cart.MASTERsubdomain.paymentTerms,
                currency.id);

            // mark as sent
            transaction.UpdateOrderStatus(OrderStatus.SENT);

            var shoppingcart = new ShoppingCart(currency.code)
            {
                shippingMethod = shippingmethod
            };

            foreach (var item in cart.cartitems)
            {
                var checkOutItem = item.product_variant.ToCheckoutItem(item.quantity, sessionid);
                var orderItem    = new orderItem
                {
                    description = item.product_variant.ToProductFullTitle(),
                    variantid   = item.product_variant.id,
                    unitPrice   = item.product_variant.product.ToUserPrice(cart.userid.Value),
                    tax         = item.product_variant.product.tax,
                    quantity    = item.quantity
                };
                transaction.AddOrderItem(orderItem, item.product_variant.product.products_digitals);
                // update inventory
                transaction.UpdateInventoryItem(orderItem, item.quantity);

                shoppingcart.items.Add(checkOutItem);
            }

            if (!cart.isDigitalOrder())
            {
                shoppingcart.CalculateShippingCost(cart.cartitems.Select(x => x.product_variant).AsQueryable(), cart.MASTERsubdomain, buyer);

                if (cart.cartitems.Select(x => x.product_variant.product.shippingProfile).UseShipwire())
                {
                    transaction.UpdateShippingMethod(shoppingcart.shipwireShippingName, shoppingcart.shippingMethod);
                }
                else
                {
                    transaction.UpdateShippingMethod(shoppingcart.shippingMethod);
                }
            }

            transaction.UpdateTotal(cart.coupon);
            transaction.SaveNewTransaction(); ////////////////////// SAVE INVOICE

            repository.AddActivity(buyer.id,
                                   new ActivityMessage(transaction.GetID(), shop_owner.id,
                                                       ActivityMessageType.INVOICE_NEW,
                                                       new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(TransactionType.INVOICE)), subdomainid.Value);

            // add checkout note as a comment
            if (!string.IsNullOrEmpty(cart.note))
            {
                transaction.AddComment(cart.note, cart.userid.Value);
            }

            // add comment if shipping method not specified
            if (!transaction.HasShippingMethod() && !cart.isDigitalOrder())
            {
                transaction.AddComment(OrderComment.SHIPPING_WAIT_FOR_COST);
            }

            // set cart as processed
            cart.orderid = transaction.GetID();

            // save payment method
            if (!string.IsNullOrEmpty(paymentmethod))
            {
                switch (paymentmethod)
                {
                case "paypal":
                    cart.paymentMethod = PaymentMethodType.Paypal.ToString();
                    break;

                default:
                    cart.paymentMethod   = PaymentMethodType.Custom.ToString();
                    cart.paymentCustomId = long.Parse(paymentmethod);
                    break;
                }
            }

            repository.Save();

            // send emails
            // send mail to buyer
            var buyerEmailContent = new OrderReceipt()
            {
                viewloc =
                    cart.MASTERsubdomain.ToHostName().ToDomainUrl(transaction.GetOrderLink()),
                shopname        = cart.MASTERsubdomain.storeName,
                date            = transaction.GetOrderDate().ToShortDateString(),
                shippingAddress = transaction.GetShippingAddress().ToHtmlString(),
                billingAddress  = transaction.GetBillingAddress().ToHtmlString(),
                subtotal        = string.Format("{0}{1}", currency.symbol, transaction.GetSubTotal().ToString("n" + currency.decimalCount)),
                shippingcost    = string.Format("{0}{1}", currency.symbol, transaction.GetShippingCost().ToString("n" + currency.decimalCount)),
                discount        = string.Format("{0}{1}", currency.symbol, transaction.GetDiscount().ToString("n" + currency.decimalCount)),
                totalcost       = string.Format("{0}{1}{2}", currency.code, currency.symbol, transaction.GetTotal().ToString("n" + currency.decimalCount)),
                orderitems      = transaction
                                  .GetOrderItems()
                                  .Select(x => string.Format("{0} x {1}{2} {3}",
                                                             x.quantity,
                                                             currency.symbol,
                                                             x.unitPrice.Value.ToString("n" + currency.decimalCount),
                                                             x.description))
            };

            // send mail to seller
            var sellerEmailContent = new NewOrderEmailContent
            {
                viewloc =
                    cart.MASTERsubdomain.ToHostName().ToDomainUrl(transaction.GetOrderLink()),
                sender = buyer.organisation1.name
            };

            string buyer_subject;
            string seller_subject;

            switch (status)
            {
            case CheckoutStatus.SHIPPING_FAIL:
                buyer_subject = string.Format("[{0}]Invoice #{1}", cart.MASTERsubdomain.name,
                                              transaction.GetOrderNumber());
                seller_subject = string.Format("[{0}]New Invoice #{1} : ACTION REQUIRED", cart.MASTERsubdomain.name,
                                               transaction.GetOrderNumber());
                buyerEmailContent.message =
                    "Thank you for placing an order with us. Unfortunately, we are not able to provide a shipping cost at this moment. We will contact you once we have the shipping costs. You can check the status of your order by following the link below:";
                sellerEmailContent.message = "A customer has placed an order on your online store. However, the shipping cost could not be calculated. You will need to manually update the invoice with the shipping cost. To update the invoice, follow the link below:";
                break;

            case CheckoutStatus.SHIPPING_NONE:
            case CheckoutStatus.SHIPPING_OK:
                buyer_subject = string.Format("[{0}]Invoice #{1} confirmed", cart.MASTERsubdomain.name,
                                              transaction.GetOrderNumber());
                seller_subject = string.Format("[{0}]New Invoice #{1}", cart.MASTERsubdomain.name,
                                               transaction.GetOrderNumber());

                if (cart.isDigitalOrder())
                {
                    buyerEmailContent.message = "Download links will be provided once payment is confirmed";
                }
                sellerEmailContent.message = "A customer has placed an order on your online store. To view the invoice, follow the link below:";
                break;

            default:
                throw new ArgumentOutOfRangeException("status");
            }

            this.SendEmail(EmailViewType.INVOICEORDER_NEW, sellerEmailContent, seller_subject,
                           shop_owner.GetEmailAddress(), shop_owner.ToFullName(), buyer);

            this.SendEmail(EmailViewType.ORDER_RECEIPT, buyerEmailContent, buyer_subject,
                           buyer.GetEmailAddress(), buyer.ToFullName(), shop_owner);

            // handle payment
            string redirecturl = "";

            if (!string.IsNullOrEmpty(paymentmethod))
            {
                switch (paymentmethod)
                {
                case "paypal":
                    string returnUrl;
                    if (isJson)
                    {
                        returnUrl = string.Format("{0}/checkout/order/{1}/close", GeneralConstants.HTTP_SECURE, cart.id);
                    }
                    else
                    {
                        returnUrl = string.Format("{0}/checkout/order/{1}", GeneralConstants.HTTP_SECURE, cart.id);
                    }
                    var pworker = new PaypalWorker(cart.id.ToString(),
                                                   transaction,
                                                   repository,
                                                   cart.MASTERsubdomain.GetPaypalID(),
                                                   transaction.GetCurrency().id,
                                                   returnUrl);
                    try
                    {
                        redirecturl = pworker.GetPaymentUrl();
                    }
                    catch (Exception ex)
                    {
                        Syslog.Write(ex);
                        return(RedirectToAction("Index", "Error"));
                    }
                    break;

                default:
                    break;
                }
            }

            if (!string.IsNullOrEmpty(redirecturl))
            {
                return(Redirect(redirecturl));
            }

            if (isJson)
            {
                return(View("close"));
            }

            return(RedirectToAction("Index"));
        }
        private static bool ShipwirePollForShippedStatus(ITradelrRepository repository)
        {
            bool changed = false;
            var  orders  =
                repository.GetOrders().Where(
                    x =>
                    x.shipwireTransaction != null && x.shipwireTransaction.state == ShipwireState.ORDER_SUBMITTED.ToString());

            foreach (var entry in orders)
            {
                var transaction = new Transaction(entry, repository, null);
                if (transaction.GetReceiver() == null)
                {
                    continue;
                }

                var shipwire = transaction.GetShipWireService();
                if (shipwire == null)
                {
                    continue;
                }

                var transactionid = transaction.GetShipwireTransactionID();
                shipwire.CreateTrackingUpdate(transactionid);
                var resp = shipwire.SubmitTrackingUpdate();
                if (resp == null)
                {
                    continue;
                }
                // try get matching order
                var responseOrder = resp.Orders.Where(x => x.id == transactionid).SingleOrDefault();
                if (responseOrder != null)
                {
                    if (responseOrder.shipped)
                    {
                        transaction.UpdateShipwireState(ShipwireState.ORDER_SHIPPED);

                        // add comment
                        string comment = string.Format(OrderComment.ORDER_SHIP_DETAILED, responseOrder.shipperFullName, responseOrder.TrackingNumber.Value, responseOrder.TrackingNumber.href);
                        transaction.AddComment(comment);

                        transaction.SaveUpdatedTransaction();

                        changed = true;

                        // notify buyer that order has been shipped
                        var subdomain = transaction.GetOwner().organisation1.MASTERsubdomain;
                        repository.AddActivity(transaction.GetReceiver().id,
                                               new ActivityMessage(transaction.GetID(), transaction.GetOwner().id,
                                                                   ActivityMessageType.ORDER_SHIPPED,
                                                                   new HtmlLink(transaction.GetOrderNumber(), transaction.GetID()).ToTransactionString(transaction.GetType())), subdomain.id);

                        var viewloc = subdomain.ToHostName().ToDomainUrl(transaction.GetOrderLink());

                        // notify buyer that order has been shipped
                        var emailContent = new OrderShippedEmailContent
                        {
                            orderNumber     = transaction.GetOrderNumber(),
                            shippingAddress = transaction.GetShippingAddress().ToHtmlString(),
                            sender          = transaction.GetOwner().ToEmailName(false),
                            viewloc         = viewloc
                        };

                        string subject    = "Invoice #" + emailContent.orderNumber + " has shipped";
                        var    msg        = new Models.message.Message(transaction.GetReceiver(), transaction.GetOwner(), subdomain.id);
                        var    controller = new dummyController();
                        msg.SendMessage(controller, repository, EmailViewType.ORDER_SHIPPED, emailContent, subject, viewloc);
                    }
                    else
                    {
                        Syslog.Write("Shipwire order not shipped {0} : {1}",
                                     transaction.GetShipwireTransactionID(),
                                     shipwire.GetXmlResponse());
                    }
                }
                else
                {
                    transaction.UpdateShipwireState(ShipwireState.ORDER_NOTFOUND);
                    transaction.SaveUpdatedTransaction();
                    changed = true;
                    Syslog.Write("Cannot find order for {0} : {1}",
                                 transaction.GetShipwireTransactionID(),
                                 shipwire.GetXmlResponse());
                }
            }
            return(changed);
        }